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