test_conn_close_gquic_be.c revision 06b2a236
1/* Copyright (c) 2017 - 2021 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_parse.h"
13
14static const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_043);
15
16
17struct conn_close_parse_test {
18    int             lineno;
19    unsigned char   buf[0x100];
20    size_t          buf_len;
21    uint32_t        error_code;
22    uint16_t        reason_len;
23    uint8_t         reason_off;
24    int             retval;
25};
26
27static const struct conn_close_parse_test parse_tests[] = {
28
29    {
30        .lineno         = __LINE__,
31        .buf            = { 0x02, 0x00, 0x00, 0x00, 0x31, 0x00, 0x05, 'D', 'u', 'd', 'e', '!', },
32        .buf_len        = 0x100,
33        .error_code     = 0x31,
34        .reason_len     = 0x05,
35        .reason_off     = 7,
36        .retval         = 7 + 5,
37    },
38
39    {
40        .lineno         = __LINE__,
41        .buf            = { 0x02, 0x00, 0x00, 0x00, 0x31, 0x00, 0x05, 'D', 'u', 'd', 'e', '!', },
42        .buf_len        = 6,    /* Too short #1 */
43        .error_code     = 0x31,
44        .reason_len     = 0x05,
45        .reason_off     = 7,
46        .retval         = -1,
47    },
48
49    {
50        .lineno         = __LINE__,
51        .buf            = { 0x02, 0x00, 0x00, 0x00, 0x31, 0x00, 0x05, 'D', 'u', 'd', 'e', '!', },
52        .buf_len        = 9,    /* Too short #2 */
53        .error_code     = 0x31,
54        .reason_len     = 0x05,
55        .reason_off     = 7,
56        .retval         = -2,
57    },
58
59    {   .buf            = { 0 },    }
60};
61
62
63struct conn_close_gen_test {
64    int             lineno;
65    uint32_t        error_code;
66    const char     *reason;
67    int             retval;
68    unsigned char   buf[0x100];
69    size_t          buf_len;
70};
71
72
73static const struct conn_close_gen_test gen_tests[] = {
74
75    {
76        .lineno         = __LINE__,
77        .error_code     = 0x12345678,
78        .reason         = "Dude, where is my car?",
79        .retval         = 7 + sizeof("Dude, where is my car?") - 1,
80        .buf_len        = 0x100,
81        .buf            = {
82            0x02,
83            0x12, 0x34, 0x56, 0x78,
84            0x00, sizeof("Dude, where is my car?") - 1,
85            'D', 'u', 'd', 'e', ',', ' ', 'w', 'h', 'e', 'r', 'e', ' ', 'i', 's', ' ', 'm', 'y', ' ', 'c', 'a', 'r', '?',
86        },
87    },
88
89    {
90        .lineno         = __LINE__,
91        .error_code     = 0x12345678,
92        .reason         = NULL,
93        .retval         = 7,
94        .buf_len        = 0x100,
95        .buf            = {
96            0x02,
97            0x12, 0x34, 0x56, 0x78,
98            0x00, 0x00, /* Zero-sized string */
99        },
100    },
101
102    {
103        .lineno         = __LINE__,
104        .error_code     = 0x12345678,
105        .reason         = "Dude, where is my car?",
106        .retval         = -1,   /* Too short */
107        .buf_len        = 0x10,
108    },
109
110    {   .buf            = { 0 },    }
111
112};
113
114
115static void
116run_parse_tests (void)
117{
118    const struct conn_close_parse_test *test;
119    for (test = parse_tests; test->buf[0]; ++test)
120    {
121        uint64_t error_code = ~0;
122        uint16_t reason_len = ~0;
123        uint8_t reason_off = ~0;
124        int sz = pf->pf_parse_connect_close_frame(test->buf, test->buf_len,
125                                NULL, &error_code, &reason_len, &reason_off);
126        assert(sz == test->retval);
127        if (0 == sz)
128        {
129            assert(test->error_code == error_code);
130            assert(test->reason_len == reason_len);
131            assert(test->reason_off == reason_off);
132        }
133    }
134}
135
136
137static void
138run_gen_tests (void)
139{
140    const struct conn_close_gen_test *test;
141    for (test = gen_tests; test->buf[0]; ++test)
142    {
143        unsigned char buf[0x100];
144        int sz = pf->pf_gen_connect_close_frame(buf, sizeof(buf),
145                    0, test->error_code, test->reason,
146                    test->reason ? strlen(test->reason) : 0);
147        assert(sz == test->retval);
148        if (0 == sz)
149            assert(0 == memcmp(test->buf, buf, sz));
150    }
151}
152
153
154int
155main (void)
156{
157    run_parse_tests();
158    run_gen_tests();
159    return 0;
160}
161