summaryrefslogtreecommitdiffstats
path: root/epan/oids_test.c
blob: 4c57f2d05350a5b0d7ed60bc42e3e8c22e79bf99 (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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/* oids_test.c
 * ASN.1 Object Identifier handling tests
 * Copyright 2013, Edward J. Beroset <beroset@ieee.org>
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"
#undef G_DISABLE_ASSERT

#include <stdio.h>
#include <string.h>
#include <glib.h>

#include "oids.h"
#include <epan/wmem_scopes.h>

#include <ws_diag_control.h>

static wmem_allocator_t *test_scope;

typedef struct
{
    const char *string;
    const char *resolved;
    unsigned encoded_len;
    const uint8_t *encoded;
    unsigned subids_len;
    uint32_t subids[];
} example_s;

DIAG_OFF_PEDANTIC
example_s ex1 = {"2.1.1", "joint-iso-itu-t.1.1", 2, (const uint8_t *)"\x51\x01", 3, {2,1,1} };
example_s ex2rel = {".81.1", ".81.1", 2, (const uint8_t*)"\x51\x01", 2, {81,1} };
example_s ex3 = {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
    "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
    25, (const uint8_t*)"\x51\x7f\xff\x7f\xff\xff\x7f\xff\xff\xff\x7f\x81\x00\x81\x80\x00\x81\x80\x80\x00\x81\x80\x80\x80\x00",
    10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };

example_s ex4 = {"2.1", "joint-iso-itu-t.1", 1, (const uint8_t*)"\x51", 2, {2,1} };
example_s ex5 = {"2", "joint-iso-itu-t", 0, NULL, 1, {2} };
example_s ex6rel = {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
    ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
    25, (const uint8_t*)"\x51\x7f\xff\x7f\xff\xff\x7f\xff\xff\xff\x7f\x81\x00\x81\x80\x00\x81\x80\x80\x00\x81\x80\x80\x80\x00",
    9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
example_s ex7 = {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, (const uint8_t*)"\x51\x01", 3, {2,1,1} };
DIAG_ON_PEDANTIC

/*
 * These test are organized in order of the appearance, in oids.h, of
 * the basic oids.c functions that they test.  This makes it easier to
 * get a quick understanding of both the testing and the organization
 * of oids.h.
 *
 * Tests are named /oids/2<desttype>/<srctype>[<extra>]
 * where <desttype> is the resulting type of the conversion,
 * <srctype> is the source type and <extra> is any additional
 * information to make the test name unique.
 *
 * The types, for the purpose of this naming convention, are
 * encoded, subids, string and resolved, both, struct.
 */

/* OIDS TESTING FUNCTIONS (/oids/2subids/) */

static void
oids_test_2subids_encoded(void)
{
    uint32_t *subids = NULL;
    unsigned len;
    unsigned i;

    len = oid_encoded2subid(NULL, ex1.encoded, ex1.encoded_len, &subids);
    g_assert_true(len == ex1.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex1.subids[i]);
    wmem_free(NULL, subids);
}

static void
oids_test_2subids_encoded_long(void)
{
    uint32_t *subids = NULL;
    unsigned len;
    unsigned i;

    len = oid_encoded2subid(NULL, ex3.encoded, ex3.encoded_len, &subids);
    g_assert_true(len == ex3.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex3.subids[i]);
    wmem_free(NULL, subids);
}

static void
oids_test_2subids_encoded_absviasub(void)
{
    uint32_t *subids = NULL;
    unsigned len;
    unsigned i;

    len = oid_encoded2subid_sub(NULL, ex1.encoded, ex1.encoded_len, &subids, true);
    g_assert_true(len == ex1.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex1.subids[i]);
    wmem_free(NULL, subids);
}

static void
oids_test_2subids_encoded_relviasub(void)
{
    uint32_t *subids = NULL;
    unsigned len;
    unsigned i;

    len = oid_encoded2subid_sub(NULL, ex2rel.encoded, ex2rel.encoded_len, &subids, false);
    g_assert_true(len == ex2rel.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex2rel.subids[i]);
    wmem_free(NULL, subids);
}

static void
oids_test_2subids_string(void)
{
    uint32_t *subids = NULL;
    unsigned len, i;

    len = oid_string2subid(test_scope, ex1.string, &subids);
    g_assert_true(len == ex1.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex1.subids[i]);
}

static void
oids_test_2subids_string_tooshort(void)
{
    uint32_t *subids = NULL;
    unsigned len, i;

    len = oid_string2subid(test_scope, ex5.string, &subids);
    g_assert_true(len == ex5.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex5.subids[i]);
}

/* OIDS TESTING FUNCTIONS (/oids/2encoded/) */

static void
oids_test_2encoded_string_simple(void)
{
    uint8_t *encoded = NULL;
    unsigned len;

    len = oid_string2encoded(NULL, ex1.string, &encoded);
    g_assert_true(len == ex1.encoded_len);
    g_assert_true(0 == memcmp(encoded, ex1.encoded, len));
    wmem_free(NULL, encoded);
}

static void
oids_test_2encoded_string_short(void)
{
    uint8_t *encoded = NULL;
    unsigned len;

    len = oid_string2encoded(NULL, ex4.string, &encoded);
    g_assert_true(len == ex4.encoded_len);
    g_assert_true(0 == memcmp(encoded, ex4.encoded, len));
    wmem_free(NULL, encoded);
}

static void
oids_test_2encoded_string_long(void)
{
    uint8_t *encoded = NULL;
    unsigned len;

    len = oid_string2encoded(NULL, ex3.string, &encoded);
    g_assert_true(len == ex3.encoded_len);
    g_assert_true(0 == memcmp(encoded, ex3.encoded, len));
    wmem_free(NULL, encoded);
}

static void
oids_test_2encoded_string_tooshort(void)
{
    uint8_t *encoded = NULL;
    unsigned len;

    len = oid_string2encoded(NULL, ex5.string, &encoded);
    g_assert_true(len == ex5.encoded_len);
    g_assert_true(0 == memcmp(encoded, ex5.encoded, len));
    wmem_free(NULL, encoded);
}

static void
oids_test_2encoded_subids_simple(void)
{
    uint8_t *encoded = NULL;
    unsigned len;

    len = oid_subid2encoded(NULL, ex1.subids_len, ex1.subids, &encoded);
    g_assert_true(len == ex1.encoded_len);
    g_assert_true(0 == memcmp(encoded, ex1.encoded, len));
    wmem_free(NULL, encoded);
}

static void
oids_test_2encoded_subids_bad(void)
{
    uint8_t *encoded = NULL;
    unsigned len;

    len = oid_subid2encoded(NULL, ex5.subids_len, ex5.subids, &encoded);
    g_assert_true(len == ex5.encoded_len);
    g_assert_true(0 == memcmp(encoded, ex5.encoded, len));
    wmem_free(NULL, encoded);
}

/* OIDS TESTING FUNCTIONS (/oids/2string/) */

static void
oids_test_2string_encoded(void)
{
    char* oid;

    oid = oid_encoded2string(NULL, ex3.encoded, ex3.encoded_len);
    g_assert_cmpstr(oid, ==, ex3.string);
    wmem_free(NULL, oid);
}

static void
oids_test_2string_encoded_rel(void)
{
    char* oid;

    oid = rel_oid_encoded2string(NULL, ex6rel.encoded, ex3.encoded_len);
    g_assert_cmpstr(oid, ==, ex6rel.string);
    wmem_free(NULL, oid);
}


static void
oids_test_2string_subids_abs(void)
{
    char* oid;

    oid = oid_subid2string(NULL, ex1.subids, ex1.subids_len);
    g_assert_cmpstr(oid, ==, ex1.string);
    wmem_free(NULL, oid);
}

static void
oids_test_2string_subids_rel(void)
{
    char* oid;

    oid = rel_oid_subid2string(NULL, ex2rel.subids, ex2rel.subids_len, false);
    g_assert_cmpstr(oid, ==, ex2rel.string);
    wmem_free(NULL, oid);
}

static void
oids_test_2string_subids_absviarel(void)
{
    char* oid;

    oid = rel_oid_subid2string(NULL, ex1.subids, ex1.subids_len, true);
    g_assert_cmpstr(oid, ==, ex1.string);
    wmem_free(NULL, oid);
}

static void
oids_test_2string_subids_relsizes(void)
{
    char* oid;

    oid = rel_oid_subid2string(NULL, ex6rel.subids, ex6rel.subids_len, false);
    g_assert_cmpstr(oid, ==, ex6rel.string);
    wmem_free(NULL, oid);
}

/* OIDS TESTING FUNCTIONS (/oids/2resolved/) */

static void
oids_test_2resolved_subids(void)
{
    char* oid;

    oid = oid_resolved(NULL, ex1.subids_len, ex1.subids);
    g_assert_cmpstr(oid, ==, ex1.resolved);
    wmem_free(NULL, oid);
}

static void
oids_test_2resolved_encoded(void)
{
    char* oid;

    oid = oid_resolved_from_encoded(NULL, ex1.encoded, ex1.encoded_len);
    g_assert_cmpstr(oid, ==, ex1.resolved);
    wmem_free(NULL, oid);
}

static void
oids_test_2resolved_encoded_rel(void)
{
    char* oid;

    oid = rel_oid_resolved_from_encoded(NULL, ex2rel.encoded, ex2rel.encoded_len);
    g_assert_cmpstr(oid, ==, ex2rel.string);
    wmem_free(NULL, oid);
}

static void
oids_test_2resolved_string(void)
{
    char* oid;

    oid = oid_resolved_from_string(NULL, ex1.string);
    g_assert_cmpstr(oid, ==, ex1.resolved);
    wmem_free(NULL, oid);
}

/* OIDS TESTING FUNCTIONS (/oids/2both/) */

static void
oids_test_2both_subids(void)
{
    char* resolved;
    char* oid;

    oid_both(NULL, ex1.subids_len, ex1.subids, &resolved, &oid);
    g_assert_cmpstr(resolved, ==, ex1.resolved);
    g_assert_cmpstr(oid, ==, ex1.string);
    wmem_free(NULL, resolved);
    wmem_free(NULL, oid);
}

static void
oids_test_2both_encoded(void)
{
    char* resolved;
    char* oid;

    oid_both_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &resolved, &oid);
    g_assert_cmpstr(resolved, ==, ex1.resolved);
    g_assert_cmpstr(oid, ==, ex1.string);
    wmem_free(NULL, resolved);
    wmem_free(NULL, oid);
}

static void
oids_test_2both_string(void)
{
    char* resolved;
    char* oid;

    oid_both_from_string(NULL, ex1.string, &resolved, &oid);
    g_assert_cmpstr(resolved, ==, ex1.resolved);
    g_assert_cmpstr(oid, ==, ex1.string);
    wmem_free(NULL, resolved);
    wmem_free(NULL, oid);
}

/* OIDS TESTING FUNCTIONS (/oids/2both/) */

static void
oids_test_2struct_subids(void)
{
    unsigned matched;
    unsigned left;
    oid_info_t *st;

    st = oid_get(ex1.subids_len, ex1.subids, &matched, &left);
    g_assert_true(matched == 1);
    g_assert_true(left == ex1.subids_len - 1);
    g_assert_true(st != NULL);
    g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
}

static void
oids_test_2struct_encoded(void)
{
    unsigned matched;
    unsigned left;
    uint32_t *subids = NULL;
    oid_info_t *st;
    unsigned len, i;

    st = oid_get_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &subids, &matched, &left);
    g_assert_true(matched == 1);
    g_assert_true(left == ex1.subids_len - 1);
    g_assert_true(st != NULL);
    g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
    len = matched + left;
    g_assert_true(len == ex1.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex1.subids[i]);
    wmem_free(NULL, subids);
}

static void
oids_test_2struct_string(void)
{
    unsigned matched;
    unsigned left;
    uint32_t *subids;
    oid_info_t *st;
    unsigned len, i;

    st = oid_get_from_string(test_scope, ex1.string, &subids, &matched, &left);
    g_assert_true(matched == 1);
    g_assert_true(left == ex1.subids_len - 1);
    g_assert_true(st != NULL);
    g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
    len = matched + left;
    g_assert_true(len == ex1.subids_len);
    for (i=0; i < len; i++)
        g_assert_true(subids[i] == ex1.subids[i]);
}

static void
oids_test_add_subids(void)
{
    char* oid;

    oid_add(ex7.resolved, ex7.subids_len, ex7.subids);
    oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
    g_assert_cmpstr(oid, ==, ex7.resolved);
    wmem_free(NULL, oid);
}

static void
oids_test_add_encoded(void)
{
    char* oid;

    oid_add_from_encoded(ex7.resolved, ex7.encoded, ex7.encoded_len);
    oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
    g_assert_cmpstr(oid, ==, ex7.resolved);
    wmem_free(NULL, oid);
}

static void
oids_test_add_string(void)
{
    char* oid;

    oid_add_from_string(ex7.resolved, ex7.string);
    oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
    g_assert_cmpstr(oid, ==, ex7.resolved);
    wmem_free(NULL, oid);
}

int
main(int argc, char **argv)
{
    int result;

    g_test_init(&argc, &argv, NULL);

    /* /oids/2encoded */
    g_test_add_func("/oids/2encoded/subids/simple",   oids_test_2encoded_subids_simple);
    g_test_add_func("/oids/2encoded/subids/bad",   oids_test_2encoded_subids_bad);
    g_test_add_func("/oids/2encoded/string/simple",   oids_test_2encoded_string_simple);
    g_test_add_func("/oids/2encoded/string/short",   oids_test_2encoded_string_short);
    g_test_add_func("/oids/2encoded/string/long",   oids_test_2encoded_string_long);
    g_test_add_func("/oids/2encoded/string/tooshort",   oids_test_2encoded_string_tooshort);

    /* /oids/2subids */
    g_test_add_func("/oids/2subids/string",   oids_test_2subids_string);
    g_test_add_func("/oids/2subids/string/tooshort",   oids_test_2subids_string_tooshort);
    g_test_add_func("/oids/2subids/encoded",   oids_test_2subids_encoded);
    g_test_add_func("/oids/2subids/encoded/long",   oids_test_2subids_encoded_long);
    g_test_add_func("/oids/2subids/encoded/absviasub",   oids_test_2subids_encoded_absviasub);
    g_test_add_func("/oids/2subids/encoded/relviasub",   oids_test_2subids_encoded_relviasub);


    /* /oids/2string */
    g_test_add_func("/oids/2string/subids/abs",   oids_test_2string_subids_abs);
    g_test_add_func("/oids/2string/subids/rel",   oids_test_2string_subids_rel);
    g_test_add_func("/oids/2string/subids/absviarel",   oids_test_2string_subids_absviarel);
    g_test_add_func("/oids/2string/subids/relsizes",   oids_test_2string_subids_relsizes);
    g_test_add_func("/oids/2string/encoded",   oids_test_2string_encoded);
    g_test_add_func("/oids/2string/encoded/rel",   oids_test_2string_encoded_rel);

    /* /oids/2resolved */
    g_test_add_func("/oids/2resolved/subids",   oids_test_2resolved_subids);
    g_test_add_func("/oids/2resolved/encoded",   oids_test_2resolved_encoded);
    g_test_add_func("/oids/2resolved/encoded/rel",   oids_test_2resolved_encoded_rel);
    g_test_add_func("/oids/2resolved/string",   oids_test_2resolved_string);

    /* /oids/2both */
    g_test_add_func("/oids/2both/subids",   oids_test_2both_subids);
    g_test_add_func("/oids/2both/encoded",   oids_test_2both_encoded);
    g_test_add_func("/oids/2both/string",   oids_test_2both_string);

    /* /oids/2struct */
    g_test_add_func("/oids/2struct/subids",   oids_test_2struct_subids);
    g_test_add_func("/oids/2struct/encoded",   oids_test_2struct_encoded);
    g_test_add_func("/oids/2struct/string",   oids_test_2struct_string);

    /* /oids/add */
    g_test_add_func("/oids/add/subids",   oids_test_add_subids);
    g_test_add_func("/oids/add/encoded",   oids_test_add_encoded);
    g_test_add_func("/oids/add/string",   oids_test_add_string);

    wmem_init_scopes();
    test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
    oids_init();
    result = g_test_run();
    oids_cleanup();
    wmem_destroy_allocator(test_scope);
    wmem_cleanup_scopes();

    return result;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 4
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=4 tabstop=8 expandtab:
 * :indentSize=4:tabSize=8:noTabs=true:
 */