summaryrefslogtreecommitdiff
path: root/src/ipcpd/unicast/ca/tests/ca_test.c
blob: 4c44d29e87fcbd457b37ff1e44e2190720bb6535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
 * Ouroboros - Copyright (C) 2016 - 2026
 *
 * Unit tests for the congestion-avoidance interface
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#include "config.h"

#include "ca.h"

#include <test/test.h>

#define ADDR_A 0x1111ULL
#define ADDR_B 0x2222ULL

static const struct {
        enum pol_cong_avoid pol;
        const char *        name;
} ca_pols[] = {
        { CA_NONE,   "none"   },
        { CA_MB_ECN, "mb-ecn" }
};

#define CA_POLS (sizeof(ca_pols) / sizeof(ca_pols[0]))

static int test_ca_init_fini(enum pol_cong_avoid pol,
                             const char *        name)
{
        TEST_START("(%s)", name);

        if (ca_init(pol) < 0) {
                printf("Failed to init ca for %s.\n", name);
                goto fail;
        }

        ca_fini();

        TEST_SUCCESS("(%s)", name);

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL("(%s)", name);
        return TEST_RC_FAIL;
}

static int test_ca_init_fini_all(void)
{
        int    ret = 0;
        size_t i;

        for (i = 0; i < CA_POLS; i++)
                ret |= test_ca_init_fini(ca_pols[i].pol, ca_pols[i].name);

        return ret;
}

static int test_ca_init_invalid(void)
{
        TEST_START();

        if (ca_init(CA_INVALID) == 0) {
                printf("Init accepted an invalid policy.\n");
                ca_fini();
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_ca_ctx_share(enum pol_cong_avoid pol,
                             const char *        name)
{
        void * c1;
        void * c2;

        TEST_START("(%s)", name);

        if (ca_init(pol) < 0) {
                printf("Failed to init ca for %s.\n", name);
                goto fail;
        }

        c1 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);
        if (c1 == NULL) {
                printf("Failed to get ctx.\n");
                goto fail_init;
        }

        c2 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);
        if (c2 == NULL) {
                printf("Failed to get second ctx.\n");
                goto fail_c1;
        }

#ifdef IPCP_CA_PER_FLOW
        if (c1 == c2) {
                printf("Per-flow build shared a ctx across flows.\n");
                goto fail_c2;
        }
#else
        if (c1 != c2) {
                printf("Aggregate build did not share ctx per (addr, qc).\n");
                goto fail_c2;
        }
#endif
        ca_ctx_put(c2);
        ca_ctx_put(c1);

        ca_fini();

        TEST_SUCCESS("(%s)", name);

        return TEST_RC_SUCCESS;
 fail_c2:
        ca_ctx_put(c2);
 fail_c1:
        ca_ctx_put(c1);
 fail_init:
        ca_fini();
 fail:
        TEST_FAIL("(%s)", name);
        return TEST_RC_FAIL;
}

static int test_ca_ctx_share_all(void)
{
        int    ret = 0;
        size_t i;

        for (i = 0; i < CA_POLS; i++)
                ret |= test_ca_ctx_share(ca_pols[i].pol, ca_pols[i].name);

        return ret;
}

static int test_ca_ctx_distinct(void)
{
        void * a_be;
        void * b_be;
        void * a_video;

        TEST_START();

        if (ca_init(CA_NONE) < 0) {
                printf("Failed to init ca.\n");
                goto fail;
        }

        a_be = ca_ctx_get(ADDR_A, QOS_CUBE_BE);
        if (a_be == NULL) {
                printf("Failed to get ctx.\n");
                goto fail_init;
        }

        b_be = ca_ctx_get(ADDR_B, QOS_CUBE_BE);
        if (b_be == NULL) {
                printf("Failed to get ctx.\n");
                goto fail_a_be;
        }

        a_video = ca_ctx_get(ADDR_A, QOS_CUBE_VIDEO);
        if (a_video == NULL) {
                printf("Failed to get ctx.\n");
                goto fail_b_be;
        }

        if (a_be == b_be) {
                printf("Distinct addresses shared a ctx.\n");
                goto fail_a_video;
        }

        if (a_be == a_video) {
                printf("Distinct qos cubes shared a ctx.\n");
                goto fail_a_video;
        }

        ca_ctx_put(a_video);
        ca_ctx_put(b_be);
        ca_ctx_put(a_be);

        ca_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_a_video:
        ca_ctx_put(a_video);
 fail_b_be:
        ca_ctx_put(b_be);
 fail_a_be:
        ca_ctx_put(a_be);
 fail_init:
        ca_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* Refcount survival is an aggregate-only property. */
#ifndef IPCP_CA_PER_FLOW
static int test_ca_ctx_refcount(void)
{
        void * c1;
        void * c3;

        TEST_START();

        if (ca_init(CA_NONE) < 0) {
                printf("Failed to init ca.\n");
                goto fail;
        }

        c1 = ca_ctx_get(ADDR_A, QOS_CUBE_BE); /* refs = 1 */
        if (c1 == NULL) {
                printf("Failed to get ctx.\n");
                goto fail_init;
        }

        if (ca_ctx_get(ADDR_A, QOS_CUBE_BE) == NULL) { /* refs = 2 */
                printf("Failed to get second ref.\n");
                goto fail_c1;
        }

        ca_ctx_put(c1); /* refs = 1 */

        c3 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);  /* refs = 2 */
        if (c3 == NULL) {
                printf("Failed to get third ref.\n");
                goto fail_c1;
        }

        if (c3 != c1) {
                printf("Refcounted ctx freed while still referenced.\n");
                goto fail_c3;
        }

        ca_ctx_put(c3);
        ca_ctx_put(c1);

        ca_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_c3:
        ca_ctx_put(c3);
 fail_c1:
        ca_ctx_put(c1);
 fail_init:
        ca_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* The last put frees the interned ctx; a fresh get recreates it. */
static int test_ca_ctx_recreate(void)
{
        void * c1;
        void * c2;
        void * c3;

        TEST_START();

        if (ca_init(CA_NONE) < 0) {
                printf("Failed to init ca.\n");
                goto fail;
        }

        c1 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);  /* refs = 1 */
        if (c1 == NULL) {
                printf("Failed to get ctx.\n");
                goto fail_init;
        }

        ca_ctx_put(c1);  /* refs = 0: freed and de-interned */

        c2 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);  /* fresh entry */
        if (c2 == NULL) {
                printf("Get after release did not recreate.\n");
                goto fail_init;
        }

        c3 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);  /* refs = 2: shares */
        if (c3 == NULL) {
                printf("Failed to share recreated ctx.\n");
                goto fail_c2;
        }

        if (c3 != c2) {
                printf("Recreated ctx did not intern.\n");
                goto fail_c3;
        }

        ca_ctx_put(c3);
        ca_ctx_put(c2);

        ca_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_c3:
        ca_ctx_put(c3);
 fail_c2:
        ca_ctx_put(c2);
 fail_init:
        ca_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

/* ca_fini drains a ctx a flow left interned, with no leak. */
static int test_ca_fini_drains(void)
{
        void * c1;
        void * c2;

        TEST_START();

        if (ca_init(CA_NONE) < 0) {
                printf("Failed to init ca.\n");
                goto fail;
        }

        c1 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);  /* refs = 1 */
        if (c1 == NULL) {
                printf("Failed to get ctx.\n");
                goto fail_init;
        }

        c2 = ca_ctx_get(ADDR_A, QOS_CUBE_BE);  /* refs = 2 */
        if (c2 == NULL) {
                printf("Failed to get second ref.\n");
                goto fail_init;
        }

        /* Leave both refs live: ca_fini must drain and free the ctx. */
        ca_fini();

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail_init:
        ca_fini();
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}
#endif /* !IPCP_CA_PER_FLOW */

int ca_test(int     argc,
            char ** argv)
{
        int ret = 0;

        (void) argc;
        (void) argv;

        ret |= test_ca_init_fini_all();
        ret |= test_ca_init_invalid();
        ret |= test_ca_ctx_share_all();
        ret |= test_ca_ctx_distinct();
#ifndef IPCP_CA_PER_FLOW
        ret |= test_ca_ctx_refcount();
        ret |= test_ca_ctx_recreate();
        ret |= test_ca_fini_drains();
#endif
        return ret;
}