test_ack_merge.c revision 9a690580
1/* Copyright (c) 2017 - 2020 LiteSpeed Technologies Inc.  See LICENSE. */
2/* Test ACK merge */
3
4#include <assert.h>
5#include <regex.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include "lsquic.h"
10#include "lsquic_types.h"
11#include "lsquic_int_types.h"
12#include "lsquic_sizes.h"
13#include "lsquic_parse.h"
14
15struct test
16{
17    int                 lineno;
18    int                 expect_failure;
19    char                a[MAX_ACKI_STR_SZ];
20    char                b[MAX_ACKI_STR_SZ];
21    char                result[MAX_ACKI_STR_SZ];
22};
23
24
25static const struct test tests[] =
26{
27
28    {
29        .lineno     = __LINE__,
30        .expect_failure = 1,
31        .a          = "",
32        .b          = "[3-3]",
33    },
34
35    {
36        .lineno     = __LINE__,
37        .expect_failure = 1,
38        .a          = "",
39        .b          = "",
40    },
41
42    {
43        .lineno     = __LINE__,
44        .a          = "[3-3]",
45        .b          = "[3-3]",
46        .result     = "[3-3]",
47    },
48
49    {
50        .lineno     = __LINE__,
51        .a          = "[3-2]",
52        .b          = "[1-1]",
53        .result     = "[3-1]",
54    },
55
56    {
57        .lineno     = __LINE__,
58        .a          = "[15-15][3-2]",
59        .b          = "[1-1]",
60        .result     = "[15-15][3-1]",
61    },
62
63    {
64        .lineno     = __LINE__,
65        .a          = "[15-10][5-2]",
66        .b          = "[9-6][1-1]",
67        .result     = "[15-1]",
68    },
69
70    {
71        .lineno     = __LINE__,
72        .a          = "[15-10][5-2]",
73        .b          = "[15-10][5-2]",
74        .result     = "[15-10][5-2]",
75    },
76
77    {
78        .lineno     = __LINE__,
79        .a          = "[33803-33803][33800-33800][33788-33788][33775-33775][33759-33759][33744-33744][33732-33732][33717-33717][33706-33706][33691-33691][33679-33679][33664-33664][33649-33649][33638-33638][33622-33622][33613-33613][33585-33585][33574-33574][33562-33562][33546-33546][33531-33531][33516-33516][33504-33504][33490-33490][33483-33483][33481-32910][32906-32906][32894-32894][32880-32880][32869-32869][32854-32854][32844-32844][32817-32817][32806-32806][32791-32791][32780-32780][32766-32766][32752-32752][32741-32741][32726-32726][32716-32716][32702-32702][32697-32697][32682-32682][32672-32672][32657-32657][32651-32651][32636-32636][32626-32626][32611-32611][32600-32600][32589-32589][32574-32574][32564-32564][32551-32551][32536-32536][32525-32525][32511-32511][32500-32500][32485-32485][32475-32475][32460-32460][32449-32449][32438-32438][32423-32423][32412-32412][32399-32399][32385-32385][32375-32375][32360-32360][32349-32349][32334-32334][32326-32326][32312-32312][32302-32302][32287-32287][32277-32277][32262-32262][32252-32252][32239-32239][32224-32224][32214-32214][32200-32200][32190-32190][32175-32175][32161-32161][32151-32151][32136-32136][32126-32126][32111-32111][32103-32103][32090-32090][32080-32080][32069-32033][32008-30698][30696-30467]",
80        .b          = "[33480-32910][32906-32906][32894-32894][32880-32880][32869-32869][32854-32854][32844-32844][32817-32817][32806-32806][32791-32791][32780-32780][32766-32766][32752-32752][32741-32741][32726-32726][32716-32716][32702-32702][32697-32697][32682-32682][32672-32672][32657-32657][32651-32651][32636-32636][32626-32626][32611-32611][32600-32600][32589-32589][32574-32574][32564-32564][32551-32551][32536-32536][32525-32525][32511-32511][32500-32500][32485-32485][32475-32475][32460-32460][32449-32449][32438-32438][32423-32423][32412-32412][32399-32399][32385-32385][32375-32375][32360-32360][32349-32349][32334-32334][32326-32326][32312-32312][32302-32302][32287-32287][32277-32277][32262-32262][32252-32252][32239-32239][32224-32224][32214-32214][32200-32200][32190-32190][32175-32175][32161-32161][32151-32151][32136-32136][32126-32126][32111-32111][32103-32103][32090-32090][32080-32080][32069-32033][32008-30698][30696-30467]",
81        .result     = "[33803-33803][33800-33800][33788-33788][33775-33775][33759-33759][33744-33744][33732-33732][33717-33717][33706-33706][33691-33691][33679-33679][33664-33664][33649-33649][33638-33638][33622-33622][33613-33613][33585-33585][33574-33574][33562-33562][33546-33546][33531-33531][33516-33516][33504-33504][33490-33490][33483-33483][33481-32910][32906-32906][32894-32894][32880-32880][32869-32869][32854-32854][32844-32844][32817-32817][32806-32806][32791-32791][32780-32780][32766-32766][32752-32752][32741-32741][32726-32726][32716-32716][32702-32702][32697-32697][32682-32682][32672-32672][32657-32657][32651-32651][32636-32636][32626-32626][32611-32611][32600-32600][32589-32589][32574-32574][32564-32564][32551-32551][32536-32536][32525-32525][32511-32511][32500-32500][32485-32485][32475-32475][32460-32460][32449-32449][32438-32438][32423-32423][32412-32412][32399-32399][32385-32385][32375-32375][32360-32360][32349-32349][32334-32334][32326-32326][32312-32312][32302-32302][32287-32287][32277-32277][32262-32262][32252-32252][32239-32239][32224-32224][32214-32214][32200-32200][32190-32190][32175-32175][32161-32161][32151-32151][32136-32136][32126-32126][32111-32111][32103-32103][32090-32090][32080-32080][32069-32033][32008-30698][30696-30467]",
82    },
83
84};
85
86
87static regex_t re;
88
89
90static void
91init_ranges (struct ack_info *a, const char *s)
92{
93    regmatch_t m[3];
94
95    while (0 == regexec(&re, s, sizeof(m) / sizeof(m[0]), m, 0))
96    {
97        a->ranges[a->n_ranges].high = atoi(s + m[1].rm_so);
98        a->ranges[a->n_ranges].low  = atoi(s + m[2].rm_so);
99        /* Self-check: */
100        assert(a->ranges[a->n_ranges].high >= a->ranges[a->n_ranges].low);
101        ++a->n_ranges;
102        s += m[0].rm_eo;
103    }
104}
105
106
107static void
108run_test_ext (const struct test *test, const char *a_str, const char *b_str)
109{
110    struct ack_info a, b;
111    int s;
112    char ack_str[MAX_ACKI_STR_SZ];
113
114    memset(&a, 0, sizeof(a));
115    memset(&b, 0, sizeof(b));
116
117    init_ranges(&a, a_str);
118    init_ranges(&b, b_str);
119
120    s = lsquic_merge_acks(&a, &b);
121    if (test->expect_failure)
122    {
123        assert(s != 0);
124        return;
125    }
126
127    assert(s == 0);
128    lsquic_acki2str(&a, ack_str, sizeof(ack_str));
129    assert(0 == strcmp(ack_str, test->result));
130}
131
132
133static void
134run_test (const struct test *test)
135{
136    run_test_ext(test, test->a, test->b);
137    /* If flipped result should be the same: */
138    run_test_ext(test, test->b, test->a);
139}
140
141
142static void
143test_out_of_ranges (int success)
144{
145    struct ack_info a, b;
146    int i, s;
147
148    memset(&a, 0, sizeof(a));
149    memset(&b, 0, sizeof(b));
150
151    for (i = 0; i < 129 - success; ++i)
152    {
153        a.ranges[i].high = a.ranges[i].low = 100000 - i * 10;
154        b.ranges[i].high = b.ranges[i].low = 100000 - 5 - i * 10;
155    }
156    a.n_ranges = i;
157    b.n_ranges = i;
158
159    s = lsquic_merge_acks(&a, &b);
160    if (success)
161        assert(s == 0);
162    else
163        assert(s != 0);
164}
165
166
167int
168main (void)
169{
170    const struct test *test;
171    int s;
172
173    s = regcomp(&re, "\\[([0-9][0-9]*)-([0-9][0-9]*)\\]", REG_EXTENDED);
174    assert(s == 0);
175
176    for (test = tests; test < tests + sizeof(tests) / sizeof(tests[0]); ++test)
177        run_test(test);
178
179    test_out_of_ranges(0);
180    test_out_of_ranges(1);
181
182    regfree(&re);
183
184    return 0;
185}
186