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
19 #include "wmem/wmem.h"
21 #include <ws_diag_control.h>
23 static wmem_allocator_t
*test_scope
;
28 const gchar
*resolved
;
30 const guint8
*encoded
;
36 example_s ex1
= {"2.1.1", "joint-iso-itu-t.1.1", 2, (const guint8
*)"\x51\x01", 3, {2,1,1} };
37 example_s ex2rel
= {".81.1", ".81.1", 2, (const guint8
*)"\x51\x01", 2, {81,1} };
38 example_s ex3
= {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
39 "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
40 25, (const guint8
*)"\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",
41 10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
43 example_s ex4
= {"2.1", "joint-iso-itu-t.1", 1, (const guint8
*)"\x51", 2, {2,1} };
44 example_s ex5
= {"2", "joint-iso-itu-t", 0, NULL
, 1, {2} };
45 example_s ex6rel
= {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
46 ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
47 25, (const guint8
*)"\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",
48 9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
49 example_s ex7
= {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, (const guint8
*)"\x51\x01", 3, {2,1,1} };
53 * These test are organized in order of the appearance, in oids.h, of
54 * the basic oids.c functions that they test. This makes it easier to
55 * get a quick understanding of both the testing and the organization
58 * Tests are named /oids/2<desttype>/<srctype>[<extra>]
59 * where <desttype> is the resulting type of the conversion,
60 * <srctype> is the source type and <extra> is any additional
61 * information to make the test name unique.
63 * The types, for the purpose of this naming convention, are
64 * encoded, subids, string and resolved, both, struct.
67 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
70 oids_test_2subids_encoded(void)
72 guint32
*subids
= NULL
;
76 len
= oid_encoded2subid(NULL
, ex1
.encoded
, ex1
.encoded_len
, &subids
);
77 g_assert_true(len
== ex1
.subids_len
);
78 for (i
=0; i
< len
; i
++)
79 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
80 wmem_free(NULL
, subids
);
84 oids_test_2subids_encoded_long(void)
86 guint32
*subids
= NULL
;
90 len
= oid_encoded2subid(NULL
, ex3
.encoded
, ex3
.encoded_len
, &subids
);
91 g_assert_true(len
== ex3
.subids_len
);
92 for (i
=0; i
< len
; i
++)
93 g_assert_true(subids
[i
] == ex3
.subids
[i
]);
94 wmem_free(NULL
, subids
);
98 oids_test_2subids_encoded_absviasub(void)
100 guint32
*subids
= NULL
;
104 len
= oid_encoded2subid_sub(NULL
, ex1
.encoded
, ex1
.encoded_len
, &subids
, TRUE
);
105 g_assert_true(len
== ex1
.subids_len
);
106 for (i
=0; i
< len
; i
++)
107 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
108 wmem_free(NULL
, subids
);
112 oids_test_2subids_encoded_relviasub(void)
114 guint32
*subids
= NULL
;
118 len
= oid_encoded2subid_sub(NULL
, ex2rel
.encoded
, ex2rel
.encoded_len
, &subids
, FALSE
);
119 g_assert_true(len
== ex2rel
.subids_len
);
120 for (i
=0; i
< len
; i
++)
121 g_assert_true(subids
[i
] == ex2rel
.subids
[i
]);
122 wmem_free(NULL
, subids
);
126 oids_test_2subids_string(void)
128 guint32
*subids
= NULL
;
131 len
= oid_string2subid(test_scope
, ex1
.string
, &subids
);
132 g_assert_true(len
== ex1
.subids_len
);
133 for (i
=0; i
< len
; i
++)
134 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
138 oids_test_2subids_string_tooshort(void)
140 guint32
*subids
= NULL
;
143 len
= oid_string2subid(test_scope
, ex5
.string
, &subids
);
144 g_assert_true(len
== ex5
.subids_len
);
145 for (i
=0; i
< len
; i
++)
146 g_assert_true(subids
[i
] == ex5
.subids
[i
]);
149 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
152 oids_test_2encoded_string_simple(void)
154 guint8
*encoded
= NULL
;
157 len
= oid_string2encoded(NULL
, ex1
.string
, &encoded
);
158 g_assert_true(len
== ex1
.encoded_len
);
159 g_assert_true(0 == memcmp(encoded
, ex1
.encoded
, len
));
160 wmem_free(NULL
, encoded
);
164 oids_test_2encoded_string_short(void)
166 guint8
*encoded
= NULL
;
169 len
= oid_string2encoded(NULL
, ex4
.string
, &encoded
);
170 g_assert_true(len
== ex4
.encoded_len
);
171 g_assert_true(0 == memcmp(encoded
, ex4
.encoded
, len
));
172 wmem_free(NULL
, encoded
);
176 oids_test_2encoded_string_long(void)
178 guint8
*encoded
= NULL
;
181 len
= oid_string2encoded(NULL
, ex3
.string
, &encoded
);
182 g_assert_true(len
== ex3
.encoded_len
);
183 g_assert_true(0 == memcmp(encoded
, ex3
.encoded
, len
));
184 wmem_free(NULL
, encoded
);
188 oids_test_2encoded_string_tooshort(void)
190 guint8
*encoded
= NULL
;
193 len
= oid_string2encoded(NULL
, ex5
.string
, &encoded
);
194 g_assert_true(len
== ex5
.encoded_len
);
195 g_assert_true(0 == memcmp(encoded
, ex5
.encoded
, len
));
196 wmem_free(NULL
, encoded
);
200 oids_test_2encoded_subids_simple(void)
202 guint8
*encoded
= NULL
;
205 len
= oid_subid2encoded(NULL
, ex1
.subids_len
, ex1
.subids
, &encoded
);
206 g_assert_true(len
== ex1
.encoded_len
);
207 g_assert_true(0 == memcmp(encoded
, ex1
.encoded
, len
));
208 wmem_free(NULL
, encoded
);
212 oids_test_2encoded_subids_bad(void)
214 guint8
*encoded
= NULL
;
217 len
= oid_subid2encoded(NULL
, ex5
.subids_len
, ex5
.subids
, &encoded
);
218 g_assert_true(len
== ex5
.encoded_len
);
219 g_assert_true(0 == memcmp(encoded
, ex5
.encoded
, len
));
220 wmem_free(NULL
, encoded
);
223 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
226 oids_test_2string_encoded(void)
230 oid
= oid_encoded2string(NULL
, ex3
.encoded
, ex3
.encoded_len
);
231 g_assert_cmpstr(oid
, ==, ex3
.string
);
232 wmem_free(NULL
, oid
);
236 oids_test_2string_encoded_rel(void)
240 oid
= rel_oid_encoded2string(NULL
, ex6rel
.encoded
, ex3
.encoded_len
);
241 g_assert_cmpstr(oid
, ==, ex6rel
.string
);
242 wmem_free(NULL
, oid
);
247 oids_test_2string_subids_abs(void)
251 oid
= oid_subid2string(NULL
, ex1
.subids
, ex1
.subids_len
);
252 g_assert_cmpstr(oid
, ==, ex1
.string
);
253 wmem_free(NULL
, oid
);
257 oids_test_2string_subids_rel(void)
261 oid
= rel_oid_subid2string(NULL
, ex2rel
.subids
, ex2rel
.subids_len
, FALSE
);
262 g_assert_cmpstr(oid
, ==, ex2rel
.string
);
263 wmem_free(NULL
, oid
);
267 oids_test_2string_subids_absviarel(void)
271 oid
= rel_oid_subid2string(NULL
, ex1
.subids
, ex1
.subids_len
, TRUE
);
272 g_assert_cmpstr(oid
, ==, ex1
.string
);
273 wmem_free(NULL
, oid
);
277 oids_test_2string_subids_relsizes(void)
281 oid
= rel_oid_subid2string(NULL
, ex6rel
.subids
, ex6rel
.subids_len
, FALSE
);
282 g_assert_cmpstr(oid
, ==, ex6rel
.string
);
283 wmem_free(NULL
, oid
);
286 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
289 oids_test_2resolved_subids(void)
293 oid
= oid_resolved(NULL
, ex1
.subids_len
, ex1
.subids
);
294 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
295 wmem_free(NULL
, oid
);
299 oids_test_2resolved_encoded(void)
303 oid
= oid_resolved_from_encoded(NULL
, ex1
.encoded
, ex1
.encoded_len
);
304 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
305 wmem_free(NULL
, oid
);
309 oids_test_2resolved_encoded_rel(void)
313 oid
= rel_oid_resolved_from_encoded(NULL
, ex2rel
.encoded
, ex2rel
.encoded_len
);
314 g_assert_cmpstr(oid
, ==, ex2rel
.string
);
315 wmem_free(NULL
, oid
);
319 oids_test_2resolved_string(void)
323 oid
= oid_resolved_from_string(NULL
, ex1
.string
);
324 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
325 wmem_free(NULL
, oid
);
328 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
331 oids_test_2both_subids(void)
336 oid_both(NULL
, ex1
.subids_len
, ex1
.subids
, &resolved
, &oid
);
337 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
338 g_assert_cmpstr(oid
, ==, ex1
.string
);
339 wmem_free(NULL
, resolved
);
340 wmem_free(NULL
, oid
);
344 oids_test_2both_encoded(void)
349 oid_both_from_encoded(NULL
, ex1
.encoded
, ex1
.encoded_len
, &resolved
, &oid
);
350 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
351 g_assert_cmpstr(oid
, ==, ex1
.string
);
352 wmem_free(NULL
, resolved
);
353 wmem_free(NULL
, oid
);
357 oids_test_2both_string(void)
362 oid_both_from_string(NULL
, ex1
.string
, &resolved
, &oid
);
363 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
364 g_assert_cmpstr(oid
, ==, ex1
.string
);
365 wmem_free(NULL
, resolved
);
366 wmem_free(NULL
, oid
);
369 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
372 oids_test_2struct_subids(void)
378 st
= oid_get(ex1
.subids_len
, ex1
.subids
, &matched
, &left
);
379 g_assert_true(matched
== 1);
380 g_assert_true(left
== ex1
.subids_len
- 1);
381 g_assert_true(st
!= NULL
);
382 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
386 oids_test_2struct_encoded(void)
390 guint32
*subids
= NULL
;
394 st
= oid_get_from_encoded(NULL
, ex1
.encoded
, ex1
.encoded_len
, &subids
, &matched
, &left
);
395 g_assert_true(matched
== 1);
396 g_assert_true(left
== ex1
.subids_len
- 1);
397 g_assert_true(st
!= NULL
);
398 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
399 len
= matched
+ left
;
400 g_assert_true(len
== ex1
.subids_len
);
401 for (i
=0; i
< len
; i
++)
402 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
403 wmem_free(NULL
, subids
);
407 oids_test_2struct_string(void)
415 st
= oid_get_from_string(test_scope
, ex1
.string
, &subids
, &matched
, &left
);
416 g_assert_true(matched
== 1);
417 g_assert_true(left
== ex1
.subids_len
- 1);
418 g_assert_true(st
!= NULL
);
419 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
420 len
= matched
+ left
;
421 g_assert_true(len
== ex1
.subids_len
);
422 for (i
=0; i
< len
; i
++)
423 g_assert_true(subids
[i
] == ex1
.subids
[i
]);
427 oids_test_add_subids(void)
431 oid_add(ex7
.resolved
, ex7
.subids_len
, ex7
.subids
);
432 oid
= oid_resolved(NULL
, ex7
.subids_len
, ex7
.subids
);
433 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
434 wmem_free(NULL
, oid
);
438 oids_test_add_encoded(void)
442 oid_add_from_encoded(ex7
.resolved
, ex7
.encoded
, ex7
.encoded_len
);
443 oid
= oid_resolved(NULL
, ex7
.subids_len
, ex7
.subids
);
444 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
445 wmem_free(NULL
, oid
);
449 oids_test_add_string(void)
453 oid_add_from_string(ex7
.resolved
, ex7
.string
);
454 oid
= oid_resolved(NULL
, ex7
.subids_len
, ex7
.subids
);
455 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
456 wmem_free(NULL
, oid
);
460 main(int argc
, char **argv
)
464 g_test_init(&argc
, &argv
, NULL
);
467 g_test_add_func("/oids/2encoded/subids/simple", oids_test_2encoded_subids_simple
);
468 g_test_add_func("/oids/2encoded/subids/bad", oids_test_2encoded_subids_bad
);
469 g_test_add_func("/oids/2encoded/string/simple", oids_test_2encoded_string_simple
);
470 g_test_add_func("/oids/2encoded/string/short", oids_test_2encoded_string_short
);
471 g_test_add_func("/oids/2encoded/string/long", oids_test_2encoded_string_long
);
472 g_test_add_func("/oids/2encoded/string/tooshort", oids_test_2encoded_string_tooshort
);
475 g_test_add_func("/oids/2subids/string", oids_test_2subids_string
);
476 g_test_add_func("/oids/2subids/string/tooshort", oids_test_2subids_string_tooshort
);
477 g_test_add_func("/oids/2subids/encoded", oids_test_2subids_encoded
);
478 g_test_add_func("/oids/2subids/encoded/long", oids_test_2subids_encoded_long
);
479 g_test_add_func("/oids/2subids/encoded/absviasub", oids_test_2subids_encoded_absviasub
);
480 g_test_add_func("/oids/2subids/encoded/relviasub", oids_test_2subids_encoded_relviasub
);
484 g_test_add_func("/oids/2string/subids/abs", oids_test_2string_subids_abs
);
485 g_test_add_func("/oids/2string/subids/rel", oids_test_2string_subids_rel
);
486 g_test_add_func("/oids/2string/subids/absviarel", oids_test_2string_subids_absviarel
);
487 g_test_add_func("/oids/2string/subids/relsizes", oids_test_2string_subids_relsizes
);
488 g_test_add_func("/oids/2string/encoded", oids_test_2string_encoded
);
489 g_test_add_func("/oids/2string/encoded/rel", oids_test_2string_encoded_rel
);
491 /* /oids/2resolved */
492 g_test_add_func("/oids/2resolved/subids", oids_test_2resolved_subids
);
493 g_test_add_func("/oids/2resolved/encoded", oids_test_2resolved_encoded
);
494 g_test_add_func("/oids/2resolved/encoded/rel", oids_test_2resolved_encoded_rel
);
495 g_test_add_func("/oids/2resolved/string", oids_test_2resolved_string
);
498 g_test_add_func("/oids/2both/subids", oids_test_2both_subids
);
499 g_test_add_func("/oids/2both/encoded", oids_test_2both_encoded
);
500 g_test_add_func("/oids/2both/string", oids_test_2both_string
);
503 g_test_add_func("/oids/2struct/subids", oids_test_2struct_subids
);
504 g_test_add_func("/oids/2struct/encoded", oids_test_2struct_encoded
);
505 g_test_add_func("/oids/2struct/string", oids_test_2struct_string
);
508 g_test_add_func("/oids/add/subids", oids_test_add_subids
);
509 g_test_add_func("/oids/add/encoded", oids_test_add_encoded
);
510 g_test_add_func("/oids/add/string", oids_test_add_string
);
513 test_scope
= wmem_allocator_new(WMEM_ALLOCATOR_STRICT
);
515 result
= g_test_run();
517 wmem_destroy_allocator(test_scope
);
524 * Editor modelines - https://www.wireshark.org/tools/modelines.html
529 * indent-tabs-mode: nil
532 * vi: set shiftwidth=4 tabstop=8 expandtab:
533 * :indentSize=4:tabSize=8:noTabs=true: