1/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc. See LICENSE. */ 2#include <assert.h> 3#include <stdio.h> 4#include <stdlib.h> 5#include <string.h> 6#ifndef WIN32 7#include <sys/time.h> 8#endif 9 10#include "lsquic.h" 11#include "lsquic_types.h" 12#include "lsquic_alarmset.h" 13#include "lsquic_parse.h" 14 15static const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_035); 16 17 18struct parse_test { 19 /* Input: */ 20 unsigned char buf[0x10]; 21 size_t buf_len; 22 lsquic_packno_t cur_packno; 23 enum packno_bits 24 bits; 25 /* Expected values: */ 26 int retval; 27 lsquic_packno_t least_unacked; 28}; 29 30static const struct parse_test parse_tests[] = { 31 { 32 .buf = { 0x06, 0x12, 0x34, 0x45, 0x67, 0x89, 0xAB, }, 33 .buf_len = 3, 34 .least_unacked = 0x1111, 35 .cur_packno = 0x4523, 36 .bits = GQUIC_PACKNO_LEN_2, 37 .retval = 3, 38 }, 39 40 { 41 .buf = { 0x06, 0x12, 0x34, 0x45, 0x67, 0x89, 0xAB, }, 42 .buf_len = 2, 43 .least_unacked = 0x1111, 44 .cur_packno = 0x4523, 45 .bits = GQUIC_PACKNO_LEN_2, 46 .retval = -1, 47 }, 48 49 { 50 .buf = { 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, }, 51 .buf_len = 7, 52 .least_unacked = 0x1122324252627282, 53 .cur_packno = 0x1122334455667788, 54 .bits = GQUIC_PACKNO_LEN_6, 55 .retval = 7, 56 }, 57 58 { .buf = { 0 }, } 59}; 60 61 62static void 63run_parse_tests (void) 64{ 65 const struct parse_test *test; 66 for (test = parse_tests; test->buf[0]; ++test) 67 { 68 lsquic_packno_t least; 69 memset(&least, 0x33, sizeof(least)); 70 int sz = pf->pf_parse_stop_waiting_frame(test->buf, test->buf_len, 71 test->cur_packno, test->bits, &least); 72 assert(("return value is correct", sz == test->retval)); 73 if (test->retval > 0) 74 assert(("least ACKed value is correct", least == test->least_unacked)); 75 } 76} 77 78 79static void 80run_gen_tests (void) 81{ 82 const struct parse_test *test; 83 for (test = parse_tests; test->buf[0]; ++test) 84 { 85 unsigned char buf[0x100]; 86 int sz = pf->pf_gen_stop_waiting_frame(buf, test->buf_len, 87 test->cur_packno, test->bits, test->least_unacked); 88 assert(("return value is correct", sz == test->retval)); 89 if (test->retval > 0) 90 assert(("generated frame is correct", 0 == memcmp(test->buf, buf, sz))); 91 } 92} 93 94 95int 96main (void) 97{ 98 run_parse_tests(); 99 run_gen_tests(); 100 return 0; 101} 102