1229fce07SDmitri Tikhonov/* Copyright (c) 2017 - 2019 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov#include <assert.h>
350aadb33SDmitri Tikhonov#include <stdio.h>
450aadb33SDmitri Tikhonov#include <stdlib.h>
550aadb33SDmitri Tikhonov#include <string.h>
6461e84d8SAmol Deshpande#ifndef WIN32
750aadb33SDmitri Tikhonov#include <sys/time.h>
8461e84d8SAmol Deshpande#endif
950aadb33SDmitri Tikhonov
1050aadb33SDmitri Tikhonov#include "lsquic.h"
1150aadb33SDmitri Tikhonov#include "lsquic_types.h"
1250aadb33SDmitri Tikhonov#include "lsquic_alarmset.h"
1350aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1450aadb33SDmitri Tikhonov
15052a1c28SDmitri Tikhonovstatic const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_035);
1650aadb33SDmitri Tikhonov
1750aadb33SDmitri Tikhonov
1850aadb33SDmitri Tikhonovstruct parse_test {
1950aadb33SDmitri Tikhonov    /* Input: */
2050aadb33SDmitri Tikhonov    unsigned char   buf[0x10];
2150aadb33SDmitri Tikhonov    size_t          buf_len;
2250aadb33SDmitri Tikhonov    lsquic_packno_t cur_packno;
23c7d81ce1SDmitri Tikhonov    enum packno_bits
2450aadb33SDmitri Tikhonov                    bits;
2550aadb33SDmitri Tikhonov    /* Expected values: */
2650aadb33SDmitri Tikhonov    int             retval;
2750aadb33SDmitri Tikhonov    lsquic_packno_t least_unacked;
2850aadb33SDmitri Tikhonov};
2950aadb33SDmitri Tikhonov
3050aadb33SDmitri Tikhonovstatic const struct parse_test parse_tests[] = {
3150aadb33SDmitri Tikhonov    {
3250aadb33SDmitri Tikhonov        .buf            = { 0x06, 0x12, 0x34, 0x45, 0x67, 0x89, 0xAB, },
3350aadb33SDmitri Tikhonov        .buf_len        = 3,
3450aadb33SDmitri Tikhonov        .least_unacked  = 0x1111,
3550aadb33SDmitri Tikhonov        .cur_packno     = 0x4523,
36c7d81ce1SDmitri Tikhonov        .bits           = GQUIC_PACKNO_LEN_2,
3750aadb33SDmitri Tikhonov        .retval         = 3,
3850aadb33SDmitri Tikhonov    },
3950aadb33SDmitri Tikhonov
4050aadb33SDmitri Tikhonov    {
4150aadb33SDmitri Tikhonov        .buf            = { 0x06, 0x12, 0x34, 0x45, 0x67, 0x89, 0xAB, },
4250aadb33SDmitri Tikhonov        .buf_len        = 2,
4350aadb33SDmitri Tikhonov        .least_unacked  = 0x1111,
4450aadb33SDmitri Tikhonov        .cur_packno     = 0x4523,
45c7d81ce1SDmitri Tikhonov        .bits           = GQUIC_PACKNO_LEN_2,
4650aadb33SDmitri Tikhonov        .retval         = -1,
4750aadb33SDmitri Tikhonov    },
4850aadb33SDmitri Tikhonov
4950aadb33SDmitri Tikhonov    {
5050aadb33SDmitri Tikhonov        .buf            = { 0x06, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, },
5150aadb33SDmitri Tikhonov        .buf_len        = 7,
5250aadb33SDmitri Tikhonov        .least_unacked  = 0x1122324252627282,
5350aadb33SDmitri Tikhonov        .cur_packno     = 0x1122334455667788,
54c7d81ce1SDmitri Tikhonov        .bits           = GQUIC_PACKNO_LEN_6,
5550aadb33SDmitri Tikhonov        .retval         = 7,
5650aadb33SDmitri Tikhonov    },
5750aadb33SDmitri Tikhonov
5850aadb33SDmitri Tikhonov    {   .buf            = { 0 },    }
5950aadb33SDmitri Tikhonov};
6050aadb33SDmitri Tikhonov
6150aadb33SDmitri Tikhonov
6250aadb33SDmitri Tikhonovstatic void
6350aadb33SDmitri Tikhonovrun_parse_tests (void)
6450aadb33SDmitri Tikhonov{
6550aadb33SDmitri Tikhonov    const struct parse_test *test;
6650aadb33SDmitri Tikhonov    for (test = parse_tests; test->buf[0]; ++test)
6750aadb33SDmitri Tikhonov    {
6850aadb33SDmitri Tikhonov        lsquic_packno_t least;
6950aadb33SDmitri Tikhonov        memset(&least, 0x33, sizeof(least));
7050aadb33SDmitri Tikhonov        int sz = pf->pf_parse_stop_waiting_frame(test->buf, test->buf_len,
7150aadb33SDmitri Tikhonov                                          test->cur_packno, test->bits, &least);
7250aadb33SDmitri Tikhonov        assert(("return value is correct", sz == test->retval));
7350aadb33SDmitri Tikhonov        if (test->retval > 0)
7450aadb33SDmitri Tikhonov            assert(("least ACKed value is correct", least == test->least_unacked));
7550aadb33SDmitri Tikhonov    }
7650aadb33SDmitri Tikhonov}
7750aadb33SDmitri Tikhonov
7850aadb33SDmitri Tikhonov
7950aadb33SDmitri Tikhonovstatic void
8050aadb33SDmitri Tikhonovrun_gen_tests (void)
8150aadb33SDmitri Tikhonov{
8250aadb33SDmitri Tikhonov    const struct parse_test *test;
8350aadb33SDmitri Tikhonov    for (test = parse_tests; test->buf[0]; ++test)
8450aadb33SDmitri Tikhonov    {
8550aadb33SDmitri Tikhonov        unsigned char buf[0x100];
8650aadb33SDmitri Tikhonov        int sz = pf->pf_gen_stop_waiting_frame(buf, test->buf_len,
8750aadb33SDmitri Tikhonov                      test->cur_packno, test->bits, test->least_unacked);
8850aadb33SDmitri Tikhonov        assert(("return value is correct", sz == test->retval));
8950aadb33SDmitri Tikhonov        if (test->retval > 0)
9050aadb33SDmitri Tikhonov            assert(("generated frame is correct", 0 == memcmp(test->buf, buf, sz)));
9150aadb33SDmitri Tikhonov    }
9250aadb33SDmitri Tikhonov}
9350aadb33SDmitri Tikhonov
9450aadb33SDmitri Tikhonov
9550aadb33SDmitri Tikhonovint
9650aadb33SDmitri Tikhonovmain (void)
9750aadb33SDmitri Tikhonov{
9850aadb33SDmitri Tikhonov    run_parse_tests();
9950aadb33SDmitri Tikhonov    run_gen_tests();
10050aadb33SDmitri Tikhonov    return 0;
10150aadb33SDmitri Tikhonov}
102