test_rechist.c revision 9a690580
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc. See LICENSE. */ 2#include <assert.h> 3#include <inttypes.h> 4#include <stdio.h> 5#include <stdlib.h> 6#include <string.h> 7 8#include "lsquic_types.h" 9#include "lsquic_int_types.h" 10#include "lsquic_rechist.h" 11#include "lsquic_parse.h" 12#include "lsquic_util.h" 13#include "lsquic_logger.h" 14#include "lsquic.h" 15#include "lsquic_hash.h" 16#include "lsquic_conn.h" 17 18 19static struct lsquic_conn lconn = LSCONN_INITIALIZER_CIDLEN(lconn, 0); 20 21 22static void 23test4 (void) 24{ 25 lsquic_rechist_t rechist; 26 const struct lsquic_packno_range *range; 27 lsquic_packno_t packno; 28 29 lsquic_rechist_init(&rechist, &lconn, 0); 30 31 for (packno = 11917; packno <= 11941; ++packno) 32 lsquic_rechist_received(&rechist, packno, 0); 33 for (packno = 11946; packno <= 11994; ++packno) 34 lsquic_rechist_received(&rechist, packno, 0); 35 36 range = lsquic_rechist_first(&rechist); 37 assert(range); 38 assert(range->high == 11994); 39 assert(range->low == 11946); 40 range = lsquic_rechist_next(&rechist); 41 assert(range); 42 assert(range->high == 11941); 43 assert(range->low == 11917); 44 range = lsquic_rechist_next(&rechist); 45 assert(!range); 46 47 lsquic_rechist_received(&rechist, 11995, 0); 48 lsquic_rechist_received(&rechist, 11996, 0); 49 50 range = lsquic_rechist_first(&rechist); 51 assert(range); 52 assert(range->high == 11996); 53 assert(range->low == 11946); 54 range = lsquic_rechist_next(&rechist); 55 assert(range); 56 assert(range->high == 11941); 57 assert(range->low == 11917); 58 range = lsquic_rechist_next(&rechist); 59 assert(!range); 60 61 lsquic_rechist_received(&rechist, 11912, 0); 62 lsquic_rechist_stop_wait(&rechist, 11860); 63 64 range = lsquic_rechist_first(&rechist); 65 assert(range); 66 assert(range->high == 11996); 67 assert(range->low == 11946); 68 range = lsquic_rechist_next(&rechist); 69 assert(range); 70 assert(range->high == 11941); 71 assert(range->low == 11917); 72 range = lsquic_rechist_next(&rechist); 73 assert(range); 74 assert(range->high == 11912); 75 assert(range->low == 11912); 76 range = lsquic_rechist_next(&rechist); 77 assert(!range); 78 79 for (packno = 12169; packno <= 12193; ++packno) 80 lsquic_rechist_received(&rechist, packno, 0); 81 82 range = lsquic_rechist_first(&rechist); 83 assert(range); 84 assert(range->high == 12193); 85 assert(range->low == 12169); 86 range = lsquic_rechist_next(&rechist); 87 assert(range); 88 assert(range->high == 11996); 89 assert(range->low == 11946); 90 range = lsquic_rechist_next(&rechist); 91 assert(range); 92 assert(range->high == 11941); 93 assert(range->low == 11917); 94 range = lsquic_rechist_next(&rechist); 95 assert(range); 96 assert(range->high == 11912); 97 assert(range->low == 11912); 98 range = lsquic_rechist_next(&rechist); 99 assert(!range); 100 101 lsquic_rechist_cleanup(&rechist); 102} 103 104 105static void 106rechist2str (lsquic_rechist_t *rechist, char *buf, size_t bufsz) 107{ 108 const struct lsquic_packno_range *range; 109 size_t off; 110 int n; 111 112 for (off = 0, range = lsquic_rechist_first(rechist); 113 range && off < bufsz; 114 off += n, range = lsquic_rechist_next(rechist)) 115 { 116 n = snprintf(buf + off, bufsz - off, "[%"PRIu64"-%"PRIu64"]", 117 range->high, range->low); 118 if (n < 0 || (size_t) n >= bufsz - off) 119 break; 120 } 121} 122 123 124static void 125test5 (void) 126{ 127 lsquic_rechist_t rechist; 128 char buf[100]; 129 130 lsquic_rechist_init(&rechist, &lconn, 0); 131 132 lsquic_rechist_received(&rechist, 1, 0); 133 /* Packet 2 omitted because it could not be decrypted */ 134 lsquic_rechist_received(&rechist, 3, 0); 135 lsquic_rechist_received(&rechist, 12, 0); 136 137 rechist2str(&rechist, buf, sizeof(buf)); 138 assert(0 == strcmp(buf, "[12-12][3-3][1-1]")); 139 140 lsquic_rechist_received(&rechist, 4, 0); 141 lsquic_rechist_received(&rechist, 10, 0); 142 143 rechist2str(&rechist, buf, sizeof(buf)); 144 assert(0 == strcmp(buf, "[12-12][10-10][4-3][1-1]")); 145 146 lsquic_rechist_received(&rechist, 6, 0); 147 148 rechist2str(&rechist, buf, sizeof(buf)); 149 assert(0 == strcmp(buf, "[12-12][10-10][6-6][4-3][1-1]")); 150 151 lsquic_rechist_received(&rechist, 7, 0); 152 lsquic_rechist_received(&rechist, 8, 0); 153 154 rechist2str(&rechist, buf, sizeof(buf)); 155 assert(0 == strcmp(buf, "[12-12][10-10][8-6][4-3][1-1]")); 156 157 lsquic_rechist_received(&rechist, 9, 0); 158 159 rechist2str(&rechist, buf, sizeof(buf)); 160 assert(0 == strcmp(buf, "[12-12][10-6][4-3][1-1]")); 161 162 lsquic_rechist_received(&rechist, 5, 0); 163 lsquic_rechist_received(&rechist, 11, 0); 164 165 rechist2str(&rechist, buf, sizeof(buf)); 166 assert(0 == strcmp(buf, "[12-3][1-1]")); 167 168 lsquic_rechist_cleanup(&rechist); 169} 170 171 172int 173main (void) 174{ 175 enum received_st st; 176 lsquic_rechist_t rechist; 177 unsigned i; 178 const struct lsquic_packno_range *range; 179 180 lsquic_global_init(LSQUIC_GLOBAL_SERVER); 181 182 lsquic_log_to_fstream(stderr, 0); 183 lsq_log_levels[LSQLM_PARSE] = LSQ_LOG_DEBUG; 184 lsq_log_levels[LSQLM_RECHIST] = LSQ_LOG_DEBUG; 185 186 lsquic_rechist_init(&rechist, &lconn, 0); 187 188 lsquic_time_t now = lsquic_time_now(); 189 st = lsquic_rechist_received(&rechist, 0, now); 190 assert(("inserting packet number zero results in error", st == REC_ST_ERR)); 191 192 st = lsquic_rechist_received(&rechist, 1, now); 193 assert(("inserting packet number one is successful", st == REC_ST_OK)); 194 195 st = lsquic_rechist_received(&rechist, 1, now); 196 assert(("inserting packet number one again results in duplicate error", 197 st == REC_ST_DUP)); 198 199 range = lsquic_rechist_first(&rechist); 200 assert(("first range returned correctly", range)); 201 assert(("first range low value checks out", range->low == 1)); 202 assert(("first range high value checks out", range->high == 1)); 203 range = lsquic_rechist_next(&rechist); 204 assert(("second range does not exist", !range)); 205 206 for (i = 3; i <= 5; ++i) 207 { 208 st = lsquic_rechist_received(&rechist, i, now); 209 assert(("inserting packet", st == REC_ST_OK)); 210 } 211 212 range = lsquic_rechist_first(&rechist); 213 assert(("first range returned correctly", range)); 214 assert(("first range low value checks out", range->low == 3)); 215 assert(("first range high value checks out", range->high == 5)); 216 range = lsquic_rechist_next(&rechist); 217 assert(("second range returned correctly", range)); 218 assert(("second range low value checks out", range->low == 1)); 219 assert(("second range high value checks out", range->high == 1)); 220 range = lsquic_rechist_next(&rechist); 221 assert(("third range does not exist", !range)); 222 223 lsquic_rechist_stop_wait(&rechist, 3); 224 225 st = lsquic_rechist_received(&rechist, 1, now); 226 assert(("inserting packet number one is unsuccessful after cutoff 3", 227 st == REC_ST_DUP)); 228 229 range = lsquic_rechist_first(&rechist); 230 assert(("first range returned correctly", range)); 231 assert(("first range low value checks out", range->low == 3)); 232 assert(("first range high value checks out", range->high == 5)); 233 range = lsquic_rechist_next(&rechist); 234 assert(("second range does not exist", !range)); 235 236 for (i = 9; i >= 7; --i) 237 { 238 st = lsquic_rechist_received(&rechist, i, now); 239 assert(("inserting packet", st == REC_ST_OK)); 240 } 241 242 range = lsquic_rechist_first(&rechist); 243 assert(("first range returned correctly", range)); 244 assert(("first range low value checks out", range->low == 7)); 245 assert(("first range high value checks out", range->high == 9)); 246 range = lsquic_rechist_next(&rechist); 247 assert(("second range returned correctly", range)); 248 assert(("second range low value checks out", range->low == 3)); 249 assert(("second range high value checks out", range->high == 5)); 250 range = lsquic_rechist_next(&rechist); 251 assert(("third range does not exist", !range)); 252 253 lsquic_rechist_cleanup(&rechist); 254 255 test4(); 256 257 test5(); 258 259 return 0; 260} 261