2 * ASN.1 Object Identifier handling tests
3 * Copyright 2013, Edward J. Beroset <beroset@ieee.org>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
13 #undef G_DISABLE_ASSERT
20 #include <epan/wmem_scopes.h>
22 #include <ws_diag_control.h>
24 static wmem_allocator_t
*test_scope
;
31 const uint8_t *encoded
;
37 example_s ex1
= {"2.1.1", "joint-iso-itu-t.1.1", 2, (const uint8_t *)"\x51\x01", 3, {2,1,1} };
38 example_s ex2rel
= {".81.1", ".81.1", 2, (const uint8_t*)"\x51\x01", 2, {81,1} };
39 example_s ex3
= {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
40 "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
41 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",
42 10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
44 example_s ex4
= {"2.1", "joint-iso-itu-t.1", 1, (const uint8_t*)"\x51", 2, {2,1} };
45 example_s ex5
= {"2", "joint-iso-itu-t", 0, NULL
, 1, {2} };
46 example_s ex6rel
= {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
47 ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
48 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",
49 9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
50 example_s ex7
= {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, (const uint8_t*)"\x51\x01", 3, {2,1,1} };
54 * These test are organized in order of the appearance, in oids.h, of
55 * the basic oids.c functions that they test. This makes it easier to
56 * get a quick understanding of both the testing and the organization
59 * Tests are named /oids/2<desttype>/<srctype>[<extra>]
60 * where <desttype> is the resulting type of the conversion,
61 * <srctype> is the source type and <extra> is any additional
62 * information to make the test name unique.
64 * The types, for the purpose of this naming convention, are
65 * encoded, subids, string and resolved, both, struct.
68 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
71 oids_test_2subids_encoded(void)
73 uint32_t *subids
= NULL
;
77 len
= oid_encoded2subid(NULL
, ex1
.encoded
, ex1
.encoded_len
, &subids
);
78 g_assert_true(len
== ex1
.subids_len
);
79 for (i
=0; i
< len
; i
++)
80 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
81 wmem_free(NULL
, subids
);
85 oids_test_2subids_encoded_long(void)
87 uint32_t *subids
= NULL
;
91 len
= oid_encoded2subid(NULL
, ex3
.encoded
, ex3
.encoded_len
, &subids
);
92 g_assert_true(len
== ex3
.subids_len
);
93 for (i
=0; i
< len
; i
++)
94 g_assert_true(subids
[i
] == ex3
.subids
[i
]);
95 wmem_free(NULL
, subids
);
99 oids_test_2subids_encoded_absviasub(void)
101 uint32_t *subids
= NULL
;
105 len
= oid_encoded2subid_sub(NULL
, ex1
.encoded
, ex1
.encoded_len
, &subids
, true);
106 g_assert_true(len
== ex1
.subids_len
);
107 for (i
=0; i
< len
; i
++)
108 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
109 wmem_free(NULL
, subids
);
113 oids_test_2subids_encoded_relviasub(void)
115 uint32_t *subids
= NULL
;
119 len
= oid_encoded2subid_sub(NULL
, ex2rel
.encoded
, ex2rel
.encoded_len
, &subids
, false);
120 g_assert_true(len
== ex2rel
.subids_len
);
121 for (i
=0; i
< len
; i
++)
122 g_assert_true(subids
[i
] == ex2rel
.subids
[i
]);
123 wmem_free(NULL
, subids
);
127 oids_test_2subids_string(void)
129 uint32_t *subids
= NULL
;
132 len
= oid_string2subid(test_scope
, ex1
.string
, &subids
);
133 g_assert_true(len
== ex1
.subids_len
);
134 for (i
=0; i
< len
; i
++)
135 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
139 oids_test_2subids_string_tooshort(void)
141 uint32_t *subids
= NULL
;
144 len
= oid_string2subid(test_scope
, ex5
.string
, &subids
);
145 g_assert_true(len
== ex5
.subids_len
);
146 for (i
=0; i
< len
; i
++)
147 g_assert_true(subids
[i
] == ex5
.subids
[i
]);
150 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
153 oids_test_2encoded_string_simple(void)
155 uint8_t *encoded
= NULL
;
158 len
= oid_string2encoded(NULL
, ex1
.string
, &encoded
);
159 g_assert_true(len
== ex1
.encoded_len
);
160 g_assert_true(0 == memcmp(encoded
, ex1
.encoded
, len
));
161 wmem_free(NULL
, encoded
);
165 oids_test_2encoded_string_short(void)
167 uint8_t *encoded
= NULL
;
170 len
= oid_string2encoded(NULL
, ex4
.string
, &encoded
);
171 g_assert_true(len
== ex4
.encoded_len
);
172 g_assert_true(0 == memcmp(encoded
, ex4
.encoded
, len
));
173 wmem_free(NULL
, encoded
);
177 oids_test_2encoded_string_long(void)
179 uint8_t *encoded
= NULL
;
182 len
= oid_string2encoded(NULL
, ex3
.string
, &encoded
);
183 g_assert_true(len
== ex3
.encoded_len
);
184 g_assert_true(0 == memcmp(encoded
, ex3
.encoded
, len
));
185 wmem_free(NULL
, encoded
);
189 oids_test_2encoded_string_tooshort(void)
191 uint8_t *encoded
= NULL
;
194 len
= oid_string2encoded(NULL
, ex5
.string
, &encoded
);
195 g_assert_true(len
== ex5
.encoded_len
);
196 g_assert_true(0 == memcmp(encoded
, ex5
.encoded
, len
));
197 wmem_free(NULL
, encoded
);
201 oids_test_2encoded_subids_simple(void)
203 uint8_t *encoded
= NULL
;
206 len
= oid_subid2encoded(NULL
, ex1
.subids_len
, ex1
.subids
, &encoded
);
207 g_assert_true(len
== ex1
.encoded_len
);
208 g_assert_true(0 == memcmp(encoded
, ex1
.encoded
, len
));
209 wmem_free(NULL
, encoded
);
213 oids_test_2encoded_subids_bad(void)
215 uint8_t *encoded
= NULL
;
218 len
= oid_subid2encoded(NULL
, ex5
.subids_len
, ex5
.subids
, &encoded
);
219 g_assert_true(len
== ex5
.encoded_len
);
220 g_assert_true(0 == memcmp(encoded
, ex5
.encoded
, len
));
221 wmem_free(NULL
, encoded
);
224 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
227 oids_test_2string_encoded(void)
231 oid
= oid_encoded2string(NULL
, ex3
.encoded
, ex3
.encoded_len
);
232 g_assert_cmpstr(oid
, ==, ex3
.string
);
233 wmem_free(NULL
, oid
);
237 oids_test_2string_encoded_rel(void)
241 oid
= rel_oid_encoded2string(NULL
, ex6rel
.encoded
, ex3
.encoded_len
);
242 g_assert_cmpstr(oid
, ==, ex6rel
.string
);
243 wmem_free(NULL
, oid
);
248 oids_test_2string_subids_abs(void)
252 oid
= oid_subid2string(NULL
, ex1
.subids
, ex1
.subids_len
);
253 g_assert_cmpstr(oid
, ==, ex1
.string
);
254 wmem_free(NULL
, oid
);
258 oids_test_2string_subids_rel(void)
262 oid
= rel_oid_subid2string(NULL
, ex2rel
.subids
, ex2rel
.subids_len
, false);
263 g_assert_cmpstr(oid
, ==, ex2rel
.string
);
264 wmem_free(NULL
, oid
);
268 oids_test_2string_subids_absviarel(void)
272 oid
= rel_oid_subid2string(NULL
, ex1
.subids
, ex1
.subids_len
, true);
273 g_assert_cmpstr(oid
, ==, ex1
.string
);
274 wmem_free(NULL
, oid
);
278 oids_test_2string_subids_relsizes(void)
282 oid
= rel_oid_subid2string(NULL
, ex6rel
.subids
, ex6rel
.subids_len
, false);
283 g_assert_cmpstr(oid
, ==, ex6rel
.string
);
284 wmem_free(NULL
, oid
);
287 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
290 oids_test_2resolved_subids(void)
294 oid
= oid_resolved(NULL
, ex1
.subids_len
, ex1
.subids
);
295 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
296 wmem_free(NULL
, oid
);
300 oids_test_2resolved_encoded(void)
304 oid
= oid_resolved_from_encoded(NULL
, ex1
.encoded
, ex1
.encoded_len
);
305 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
306 wmem_free(NULL
, oid
);
310 oids_test_2resolved_encoded_rel(void)
314 oid
= rel_oid_resolved_from_encoded(NULL
, ex2rel
.encoded
, ex2rel
.encoded_len
);
315 g_assert_cmpstr(oid
, ==, ex2rel
.string
);
316 wmem_free(NULL
, oid
);
320 oids_test_2resolved_string(void)
324 oid
= oid_resolved_from_string(NULL
, ex1
.string
);
325 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
326 wmem_free(NULL
, oid
);
329 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
332 oids_test_2both_subids(void)
337 oid_both(NULL
, ex1
.subids_len
, ex1
.subids
, &resolved
, &oid
);
338 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
339 g_assert_cmpstr(oid
, ==, ex1
.string
);
340 wmem_free(NULL
, resolved
);
341 wmem_free(NULL
, oid
);
345 oids_test_2both_encoded(void)
350 oid_both_from_encoded(NULL
, ex1
.encoded
, ex1
.encoded_len
, &resolved
, &oid
);
351 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
352 g_assert_cmpstr(oid
, ==, ex1
.string
);
353 wmem_free(NULL
, resolved
);
354 wmem_free(NULL
, oid
);
358 oids_test_2both_string(void)
363 oid_both_from_string(NULL
, ex1
.string
, &resolved
, &oid
);
364 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
365 g_assert_cmpstr(oid
, ==, ex1
.string
);
366 wmem_free(NULL
, resolved
);
367 wmem_free(NULL
, oid
);
370 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
373 oids_test_2struct_subids(void)
379 st
= oid_get(ex1
.subids_len
, ex1
.subids
, &matched
, &left
);
380 g_assert_true(matched
== 1);
381 g_assert_true(left
== ex1
.subids_len
- 1);
382 g_assert_true(st
!= NULL
);
383 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
387 oids_test_2struct_encoded(void)
391 uint32_t *subids
= NULL
;
395 st
= oid_get_from_encoded(NULL
, ex1
.encoded
, ex1
.encoded_len
, &subids
, &matched
, &left
);
396 g_assert_true(matched
== 1);
397 g_assert_true(left
== ex1
.subids_len
- 1);
398 g_assert_true(st
!= NULL
);
399 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
400 len
= matched
+ left
;
401 g_assert_true(len
== ex1
.subids_len
);
402 for (i
=0; i
< len
; i
++)
403 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
404 wmem_free(NULL
, subids
);
408 oids_test_2struct_string(void)
416 st
= oid_get_from_string(test_scope
, ex1
.string
, &subids
, &matched
, &left
);
417 g_assert_true(matched
== 1);
418 g_assert_true(left
== ex1
.subids_len
- 1);
419 g_assert_true(st
!= NULL
);
420 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
421 len
= matched
+ left
;
422 g_assert_true(len
== ex1
.subids_len
);
423 for (i
=0; i
< len
; i
++)
424 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
428 oids_test_add_subids(void)
432 oid_add(ex7
.resolved
, ex7
.subids_len
, ex7
.subids
);
433 oid
= oid_resolved(NULL
, ex7
.subids_len
, ex7
.subids
);
434 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
435 wmem_free(NULL
, oid
);
439 oids_test_add_encoded(void)
443 oid_add_from_encoded(ex7
.resolved
, ex7
.encoded
, ex7
.encoded_len
);
444 oid
= oid_resolved(NULL
, ex7
.subids_len
, ex7
.subids
);
445 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
446 wmem_free(NULL
, oid
);
450 oids_test_add_string(void)
454 oid_add_from_string(ex7
.resolved
, ex7
.string
);
455 oid
= oid_resolved(NULL
, ex7
.subids_len
, ex7
.subids
);
456 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
457 wmem_free(NULL
, oid
);
461 main(int argc
, char **argv
)
465 g_test_init(&argc
, &argv
, NULL
);
468 g_test_add_func("/oids/2encoded/subids/simple", oids_test_2encoded_subids_simple
);
469 g_test_add_func("/oids/2encoded/subids/bad", oids_test_2encoded_subids_bad
);
470 g_test_add_func("/oids/2encoded/string/simple", oids_test_2encoded_string_simple
);
471 g_test_add_func("/oids/2encoded/string/short", oids_test_2encoded_string_short
);
472 g_test_add_func("/oids/2encoded/string/long", oids_test_2encoded_string_long
);
473 g_test_add_func("/oids/2encoded/string/tooshort", oids_test_2encoded_string_tooshort
);
476 g_test_add_func("/oids/2subids/string", oids_test_2subids_string
);
477 g_test_add_func("/oids/2subids/string/tooshort", oids_test_2subids_string_tooshort
);
478 g_test_add_func("/oids/2subids/encoded", oids_test_2subids_encoded
);
479 g_test_add_func("/oids/2subids/encoded/long", oids_test_2subids_encoded_long
);
480 g_test_add_func("/oids/2subids/encoded/absviasub", oids_test_2subids_encoded_absviasub
);
481 g_test_add_func("/oids/2subids/encoded/relviasub", oids_test_2subids_encoded_relviasub
);
485 g_test_add_func("/oids/2string/subids/abs", oids_test_2string_subids_abs
);
486 g_test_add_func("/oids/2string/subids/rel", oids_test_2string_subids_rel
);
487 g_test_add_func("/oids/2string/subids/absviarel", oids_test_2string_subids_absviarel
);
488 g_test_add_func("/oids/2string/subids/relsizes", oids_test_2string_subids_relsizes
);
489 g_test_add_func("/oids/2string/encoded", oids_test_2string_encoded
);
490 g_test_add_func("/oids/2string/encoded/rel", oids_test_2string_encoded_rel
);
492 /* /oids/2resolved */
493 g_test_add_func("/oids/2resolved/subids", oids_test_2resolved_subids
);
494 g_test_add_func("/oids/2resolved/encoded", oids_test_2resolved_encoded
);
495 g_test_add_func("/oids/2resolved/encoded/rel", oids_test_2resolved_encoded_rel
);
496 g_test_add_func("/oids/2resolved/string", oids_test_2resolved_string
);
499 g_test_add_func("/oids/2both/subids", oids_test_2both_subids
);
500 g_test_add_func("/oids/2both/encoded", oids_test_2both_encoded
);
501 g_test_add_func("/oids/2both/string", oids_test_2both_string
);
504 g_test_add_func("/oids/2struct/subids", oids_test_2struct_subids
);
505 g_test_add_func("/oids/2struct/encoded", oids_test_2struct_encoded
);
506 g_test_add_func("/oids/2struct/string", oids_test_2struct_string
);
509 g_test_add_func("/oids/add/subids", oids_test_add_subids
);
510 g_test_add_func("/oids/add/encoded", oids_test_add_encoded
);
511 g_test_add_func("/oids/add/string", oids_test_add_string
);
514 test_scope
= wmem_allocator_new(WMEM_ALLOCATOR_STRICT
);
516 result
= g_test_run();
518 wmem_destroy_allocator(test_scope
);
519 wmem_cleanup_scopes();
525 * Editor modelines - https://www.wireshark.org/tools/modelines.html
530 * indent-tabs-mode: nil
533 * vi: set shiftwidth=4 tabstop=8 expandtab:
534 * :indentSize=4:tabSize=8:noTabs=true: