2022-05-06 16:49:46 +00:00
|
|
|
/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */
|
2019-09-11 15:27:58 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2020-06-03 04:13:30 +00:00
|
|
|
#ifndef WIN32
|
2019-09-11 15:27:58 +00:00
|
|
|
#include <unistd.h>
|
2020-06-03 04:13:30 +00:00
|
|
|
#else
|
|
|
|
#include "vc_compat.h"
|
|
|
|
#include <winbase.h>
|
|
|
|
#endif
|
2019-09-11 15:27:58 +00:00
|
|
|
|
|
|
|
#include "lsquic.h"
|
|
|
|
#include "lsquic_stock_shi.h"
|
|
|
|
|
|
|
|
static const struct pair {
|
|
|
|
const char *key, *value;
|
|
|
|
} pairs[] = {
|
|
|
|
{ "Dude,", "where is my car?", },
|
|
|
|
{ "Balls of fur", "y", },
|
|
|
|
{ "Grand", "piano", },
|
|
|
|
{ "sWeet", "Potato", },
|
|
|
|
{ "Mac ", "and CHEESE!", },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define N_PAIRS (sizeof(pairs) / sizeof(pairs[0]))
|
|
|
|
|
|
|
|
static const struct order {
|
|
|
|
int order[N_PAIRS];
|
|
|
|
int expire;
|
|
|
|
} orderings[] = {
|
|
|
|
{{ 0, 1, 2, 3, 4, }, 1, },
|
|
|
|
{{ 0, 2, 3, 1, 4, }, 2, },
|
|
|
|
{{ 2, 1, 0, 4, 3, }, 3, },
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
test_shi (const struct order *order)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
int s;
|
|
|
|
struct stock_shared_hash *hash;
|
|
|
|
const struct pair *pair;
|
|
|
|
unsigned data_sz;
|
|
|
|
const time_t now = time(NULL);
|
|
|
|
time_t expiry;
|
|
|
|
void *datap;
|
|
|
|
|
2020-03-12 12:41:53 +00:00
|
|
|
hash = lsquic_stock_shared_hash_new();
|
2019-09-11 15:27:58 +00:00
|
|
|
|
|
|
|
for (i = 0; i < N_PAIRS; ++i)
|
|
|
|
{
|
|
|
|
pair = &pairs[ order->order[i] ];
|
|
|
|
if (order->order[i] == order->expire)
|
|
|
|
expiry = now + 1;
|
|
|
|
else
|
|
|
|
expiry = 0;
|
2022-08-16 16:16:46 +00:00
|
|
|
s = stock_shi.shi_insert(hash, (char *)pair->key, strlen(pair->key),
|
|
|
|
(char *)pair->value, strlen(pair->value), expiry);
|
2019-09-11 15:27:58 +00:00
|
|
|
assert(0 == s);
|
|
|
|
}
|
|
|
|
|
2020-06-03 04:13:30 +00:00
|
|
|
#ifndef WIN32
|
2019-09-11 15:27:58 +00:00
|
|
|
sleep(2); /* Let the thing expire */
|
2020-06-03 04:13:30 +00:00
|
|
|
#else
|
|
|
|
Sleep(2000); /* Let the thing expire */
|
|
|
|
#endif
|
2019-09-11 15:27:58 +00:00
|
|
|
|
|
|
|
for (i = 0; i < N_PAIRS; ++i)
|
|
|
|
{
|
|
|
|
pair = &pairs[ order->order[i] ];
|
|
|
|
s = stock_shi.shi_lookup(hash, pair->key, strlen(pair->key),
|
|
|
|
&datap, &data_sz);
|
|
|
|
if (order->order[i] == order->expire)
|
|
|
|
{
|
|
|
|
assert(0 == s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert(1 == s);
|
2022-08-16 16:16:46 +00:00
|
|
|
assert(data_sz == strlen(pair->value));
|
|
|
|
assert(0 == strncmp(pair->value, datap, data_sz));
|
2019-09-11 15:27:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < N_PAIRS; ++i)
|
|
|
|
{
|
|
|
|
pair = &pairs[ order->order[i] ];
|
|
|
|
s = stock_shi.shi_delete(hash, pair->key, strlen(pair->key));
|
|
|
|
if (order->order[i] == order->expire)
|
|
|
|
assert(0 != s);
|
|
|
|
else
|
|
|
|
assert(0 == s);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < N_PAIRS; ++i)
|
|
|
|
{
|
|
|
|
pair = &pairs[ order->order[i] ];
|
|
|
|
s = stock_shi.shi_lookup(hash, pair->key, strlen(pair->key),
|
|
|
|
&datap, &data_sz);
|
|
|
|
assert(0 == s);
|
|
|
|
}
|
|
|
|
|
2020-03-12 12:41:53 +00:00
|
|
|
lsquic_stock_shared_hash_destroy(hash);
|
2019-09-11 15:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
for (i = 0; i < sizeof(orderings) / sizeof(orderings[0]); ++i)
|
|
|
|
test_shi(&orderings[i]);
|
|
|
|
return 0;
|
|
|
|
}
|