1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc. See LICENSE. */ 2#include <assert.h> 3#include <stdlib.h> 4#include <string.h> 5#include <time.h> 6#ifndef WIN32 7#include <unistd.h> 8#else 9#include "vc_compat.h" 10#include <winbase.h> 11#endif 12 13#include "lsquic.h" 14#include "lsquic_stock_shi.h" 15 16static const struct pair { 17 const char *key, *value; 18} pairs[] = { 19 { "Dude,", "where is my car?", }, 20 { "Balls of fur", "y", }, 21 { "Grand", "piano", }, 22 { "sWeet", "Potato", }, 23 { "Mac ", "and CHEESE!", }, 24}; 25 26 27struct data { 28 size_t size; /* Overall size including the payload */ 29 char *key; 30 char *value; 31 char data[0]; /* key followed by value */ 32}; 33 34 35static struct data * 36new_data (const char *key, const char *value) 37{ 38 size_t klen = strlen(key); 39 size_t vlen = strlen(value); 40 size_t size = klen + vlen + 2 + sizeof(struct data); 41 struct data *data = malloc(size); 42 data->size = size; 43 data->key = data->data; 44 data->value = data->data + klen + 1; 45 memcpy(data->data, key, klen); 46 data->key[klen] = '\0'; 47 memcpy(data->value, value, vlen); 48 data->value[vlen] = '\0'; 49 return data; 50} 51 52 53#define N_PAIRS (sizeof(pairs) / sizeof(pairs[0])) 54 55static const struct order { 56 int order[N_PAIRS]; 57 int expire; 58} orderings[] = { 59 {{ 0, 1, 2, 3, 4, }, 1, }, 60 {{ 0, 2, 3, 1, 4, }, 2, }, 61 {{ 2, 1, 0, 4, 3, }, 3, }, 62}; 63 64 65static void 66test_shi (const struct order *order) 67{ 68 unsigned i; 69 int s; 70 struct stock_shared_hash *hash; 71 const struct pair *pair; 72 unsigned data_sz; 73 const time_t now = time(NULL); 74 time_t expiry; 75 void *datap; 76 struct data *data; 77 78 hash = lsquic_stock_shared_hash_new(); 79 80 for (i = 0; i < N_PAIRS; ++i) 81 { 82 pair = &pairs[ order->order[i] ]; 83 if (order->order[i] == order->expire) 84 expiry = now + 1; 85 else 86 expiry = 0; 87 data = new_data(pair->key, pair->value); 88 s = stock_shi.shi_insert(hash, strdup(data->key), strlen(data->key), 89 data, data->size, expiry); 90 assert(0 == s); 91 } 92 93#ifndef WIN32 94 sleep(2); /* Let the thing expire */ 95#else 96 Sleep(2000); /* Let the thing expire */ 97#endif 98 99 for (i = 0; i < N_PAIRS; ++i) 100 { 101 pair = &pairs[ order->order[i] ]; 102 s = stock_shi.shi_lookup(hash, pair->key, strlen(pair->key), 103 &datap, &data_sz); 104 if (order->order[i] == order->expire) 105 { 106 assert(0 == s); 107 } 108 else 109 { 110 data = datap; 111 assert(1 == s); 112 assert(data_sz == data->size); 113 assert(0 == strcmp(pair->key, data->key)); 114 assert(0 == strcmp(pair->value, data->value)); 115 } 116 } 117 118 for (i = 0; i < N_PAIRS; ++i) 119 { 120 pair = &pairs[ order->order[i] ]; 121 s = stock_shi.shi_delete(hash, pair->key, strlen(pair->key)); 122 if (order->order[i] == order->expire) 123 assert(0 != s); 124 else 125 assert(0 == s); 126 } 127 128 for (i = 0; i < N_PAIRS; ++i) 129 { 130 pair = &pairs[ order->order[i] ]; 131 s = stock_shi.shi_lookup(hash, pair->key, strlen(pair->key), 132 &datap, &data_sz); 133 assert(0 == s); 134 } 135 136 lsquic_stock_shared_hash_destroy(hash); 137} 138 139 140int 141main (void) 142{ 143 unsigned i; 144 for (i = 0; i < sizeof(orderings) / sizeof(orderings[0]); ++i) 145 test_shi(&orderings[i]); 146 return 0; 147} 148