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 static wmem_allocator_t *test_scope;
26 const gchar *resolved;
33 example_s ex1 = {"2.1.1", "joint-iso-itu-t.1.1", 2, "\x51\x01", 3, {2,1,1} };
34 example_s ex2rel = {".81.1", ".81.1", 2, "\x51\x01", 2, {81,1} };
35 example_s ex3 = {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
36 "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
37 25, "\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",
38 10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
40 example_s ex4 = {"2.1", "joint-iso-itu-t.1", 1, "\x51", 2, {2,1} };
41 example_s ex5 = {"2", "joint-iso-itu-t", 0, NULL, 1, {2} };
42 example_s ex6rel = {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
43 ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
44 25, "\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",
45 9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
46 example_s ex7 = {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, "\x51\x01", 3, {2,1,1} };
49 * These test are organized in order of the appearance, in oids.h, of
50 * the basic oids.c functions that they test. This makes it easier to
51 * get a quick understanding of both the testing and the organization
54 * Tests are named /oids/2<desttype>/<srctype>[<extra>]
55 * where <desttype> is the resulting type of the conversion,
56 * <srctype> is the source type and <extra> is any additional
57 * information to make the test name unique.
59 * The types, for the purpose of this naming convention, are
60 * encoded, subids, string and resolved, both, struct.
63 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
66 oids_test_2subids_encoded(void)
68 guint32 *subids = NULL;
72 len = oid_encoded2subid(NULL, ex1.encoded, ex1.encoded_len, &subids);
73 g_assert(len == ex1.subids_len);
74 for (i=0; i < len; i++)
75 g_assert(subids[i] == ex1.subids[i]);
76 wmem_free(NULL, subids);
80 oids_test_2subids_encoded_long(void)
82 guint32 *subids = NULL;
86 len = oid_encoded2subid(NULL, ex3.encoded, ex3.encoded_len, &subids);
87 g_assert(len == ex3.subids_len);
88 for (i=0; i < len; i++)
89 g_assert(subids[i] == ex3.subids[i]);
90 wmem_free(NULL, subids);
94 oids_test_2subids_encoded_absviasub(void)
96 guint32 *subids = NULL;
100 len = oid_encoded2subid_sub(NULL, ex1.encoded, ex1.encoded_len, &subids, TRUE);
101 g_assert(len == ex1.subids_len);
102 for (i=0; i < len; i++)
103 g_assert(subids[i] == ex1.subids[i]);
104 wmem_free(NULL, subids);
108 oids_test_2subids_encoded_relviasub(void)
110 guint32 *subids = NULL;
114 len = oid_encoded2subid_sub(NULL, ex2rel.encoded, ex2rel.encoded_len, &subids, FALSE);
115 g_assert(len == ex2rel.subids_len);
116 for (i=0; i < len; i++)
117 g_assert(subids[i] == ex2rel.subids[i]);
118 wmem_free(NULL, subids);
122 oids_test_2subids_string(void)
124 guint32 *subids = NULL;
127 len = oid_string2subid(test_scope, ex1.string, &subids);
128 g_assert(len == ex1.subids_len);
129 for (i=0; i < len; i++)
130 g_assert(subids[i] == ex1.subids[i]);
134 oids_test_2subids_string_tooshort(void)
136 guint32 *subids = NULL;
139 len = oid_string2subid(test_scope, ex5.string, &subids);
140 g_assert(len == ex5.subids_len);
141 for (i=0; i < len; i++)
142 g_assert(subids[i] == ex5.subids[i]);
145 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
148 oids_test_2encoded_string_simple(void)
150 guint8 *encoded = NULL;
153 len = oid_string2encoded(NULL, ex1.string, &encoded);
154 g_assert(len == ex1.encoded_len);
155 g_assert(0 == memcmp(encoded, ex1.encoded, len));
156 wmem_free(NULL, encoded);
160 oids_test_2encoded_string_short(void)
162 guint8 *encoded = NULL;
165 len = oid_string2encoded(NULL, ex4.string, &encoded);
166 g_assert(len == ex4.encoded_len);
167 g_assert(0 == memcmp(encoded, ex4.encoded, len));
168 wmem_free(NULL, encoded);
172 oids_test_2encoded_string_long(void)
174 guint8 *encoded = NULL;
177 len = oid_string2encoded(NULL, ex3.string, &encoded);
178 g_assert(len == ex3.encoded_len);
179 g_assert(0 == memcmp(encoded, ex3.encoded, len));
180 wmem_free(NULL, encoded);
184 oids_test_2encoded_string_tooshort(void)
186 guint8 *encoded = NULL;
189 len = oid_string2encoded(NULL, ex5.string, &encoded);
190 g_assert(len == ex5.encoded_len);
191 g_assert(0 == memcmp(encoded, ex5.encoded, len));
192 wmem_free(NULL, encoded);
196 oids_test_2encoded_subids_simple(void)
198 guint8 *encoded = NULL;
201 len = oid_subid2encoded(NULL, ex1.subids_len, ex1.subids, &encoded);
202 g_assert(len == ex1.encoded_len);
203 g_assert(0 == memcmp(encoded, ex1.encoded, len));
204 wmem_free(NULL, encoded);
208 oids_test_2encoded_subids_bad(void)
210 guint8 *encoded = NULL;
213 len = oid_subid2encoded(NULL, ex5.subids_len, ex5.subids, &encoded);
214 g_assert(len == ex5.encoded_len);
215 g_assert(0 == memcmp(encoded, ex5.encoded, len));
216 wmem_free(NULL, encoded);
219 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
222 oids_test_2string_encoded(void)
226 oid = oid_encoded2string(NULL, ex3.encoded, ex3.encoded_len);
227 g_assert_cmpstr(oid, ==, ex3.string);
228 wmem_free(NULL, oid);
232 oids_test_2string_encoded_rel(void)
236 oid = rel_oid_encoded2string(NULL, ex6rel.encoded, ex3.encoded_len);
237 g_assert_cmpstr(oid, ==, ex6rel.string);
238 wmem_free(NULL, oid);
243 oids_test_2string_subids_abs(void)
247 oid = oid_subid2string(NULL, ex1.subids, ex1.subids_len);
248 g_assert_cmpstr(oid, ==, ex1.string);
249 wmem_free(NULL, oid);
253 oids_test_2string_subids_rel(void)
257 oid = rel_oid_subid2string(NULL, ex2rel.subids, ex2rel.subids_len, FALSE);
258 g_assert_cmpstr(oid, ==, ex2rel.string);
259 wmem_free(NULL, oid);
263 oids_test_2string_subids_absviarel(void)
267 oid = rel_oid_subid2string(NULL, ex1.subids, ex1.subids_len, TRUE);
268 g_assert_cmpstr(oid, ==, ex1.string);
269 wmem_free(NULL, oid);
273 oids_test_2string_subids_relsizes(void)
277 oid = rel_oid_subid2string(NULL, ex6rel.subids, ex6rel.subids_len, FALSE);
278 g_assert_cmpstr(oid, ==, ex6rel.string);
279 wmem_free(NULL, oid);
282 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
285 oids_test_2resolved_subids(void)
289 oid = oid_resolved(NULL, ex1.subids_len, ex1.subids);
290 g_assert_cmpstr(oid, ==, ex1.resolved);
291 wmem_free(NULL, oid);
295 oids_test_2resolved_encoded(void)
299 oid = oid_resolved_from_encoded(NULL, ex1.encoded, ex1.encoded_len);
300 g_assert_cmpstr(oid, ==, ex1.resolved);
301 wmem_free(NULL, oid);
305 oids_test_2resolved_encoded_rel(void)
309 oid = rel_oid_resolved_from_encoded(NULL, ex2rel.encoded, ex2rel.encoded_len);
310 g_assert_cmpstr(oid, ==, ex2rel.string);
311 wmem_free(NULL, oid);
315 oids_test_2resolved_string(void)
319 oid = oid_resolved_from_string(NULL, ex1.string);
320 g_assert_cmpstr(oid, ==, ex1.resolved);
321 wmem_free(NULL, oid);
324 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
327 oids_test_2both_subids(void)
332 oid_both(NULL, ex1.subids_len, ex1.subids, &resolved, &oid);
333 g_assert_cmpstr(resolved, ==, ex1.resolved);
334 g_assert_cmpstr(oid, ==, ex1.string);
335 wmem_free(NULL, resolved);
336 wmem_free(NULL, oid);
340 oids_test_2both_encoded(void)
345 oid_both_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &resolved, &oid);
346 g_assert_cmpstr(resolved, ==, ex1.resolved);
347 g_assert_cmpstr(oid, ==, ex1.string);
348 wmem_free(NULL, resolved);
349 wmem_free(NULL, oid);
353 oids_test_2both_string(void)
358 oid_both_from_string(NULL, ex1.string, &resolved, &oid);
359 g_assert_cmpstr(resolved, ==, ex1.resolved);
360 g_assert_cmpstr(oid, ==, ex1.string);
361 wmem_free(NULL, resolved);
362 wmem_free(NULL, oid);
365 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
368 oids_test_2struct_subids(void)
374 st = oid_get(ex1.subids_len, ex1.subids, &matched, &left);
375 g_assert(matched == 1);
376 g_assert(left == ex1.subids_len - 1);
377 g_assert(st != NULL);
378 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
382 oids_test_2struct_encoded(void)
386 guint32 *subids = NULL;
390 st = oid_get_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &subids, &matched, &left);
391 g_assert(matched == 1);
392 g_assert(left == ex1.subids_len - 1);
393 g_assert(st != NULL);
394 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
395 len = matched + left;
396 g_assert(len == ex1.subids_len);
397 for (i=0; i < len; i++)
398 g_assert(subids[i] == ex1.subids[i]);
399 wmem_free(NULL, subids);
403 oids_test_2struct_string(void)
411 st = oid_get_from_string(test_scope, ex1.string, &subids, &matched, &left);
412 g_assert(matched == 1);
413 g_assert(left == ex1.subids_len - 1);
414 g_assert(st != NULL);
415 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
416 len = matched + left;
417 g_assert(len == ex1.subids_len);
418 for (i=0; i < len; i++)
419 g_assert(subids[i] == ex1.subids[i]);
423 oids_test_add_subids(void)
427 oid_add(ex7.resolved, ex7.subids_len, ex7.subids);
428 oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
429 g_assert_cmpstr(oid, ==, ex7.resolved);
430 wmem_free(NULL, oid);
434 oids_test_add_encoded(void)
438 oid_add_from_encoded(ex7.resolved, ex7.encoded, ex7.encoded_len);
439 oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
440 g_assert_cmpstr(oid, ==, ex7.resolved);
441 wmem_free(NULL, oid);
445 oids_test_add_string(void)
449 oid_add_from_string(ex7.resolved, ex7.string);
450 oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
451 g_assert_cmpstr(oid, ==, ex7.resolved);
452 wmem_free(NULL, oid);
456 main(int argc, char **argv)
460 g_test_init(&argc, &argv, NULL);
463 g_test_add_func("/oids/2encoded/subids/simple", oids_test_2encoded_subids_simple);
464 g_test_add_func("/oids/2encoded/subids/bad", oids_test_2encoded_subids_bad);
465 g_test_add_func("/oids/2encoded/string/simple", oids_test_2encoded_string_simple);
466 g_test_add_func("/oids/2encoded/string/short", oids_test_2encoded_string_short);
467 g_test_add_func("/oids/2encoded/string/long", oids_test_2encoded_string_long);
468 g_test_add_func("/oids/2encoded/string/tooshort", oids_test_2encoded_string_tooshort);
471 g_test_add_func("/oids/2subids/string", oids_test_2subids_string);
472 g_test_add_func("/oids/2subids/string/tooshort", oids_test_2subids_string_tooshort);
473 g_test_add_func("/oids/2subids/encoded", oids_test_2subids_encoded);
474 g_test_add_func("/oids/2subids/encoded/long", oids_test_2subids_encoded_long);
475 g_test_add_func("/oids/2subids/encoded/absviasub", oids_test_2subids_encoded_absviasub);
476 g_test_add_func("/oids/2subids/encoded/relviasub", oids_test_2subids_encoded_relviasub);
480 g_test_add_func("/oids/2string/subids/abs", oids_test_2string_subids_abs);
481 g_test_add_func("/oids/2string/subids/rel", oids_test_2string_subids_rel);
482 g_test_add_func("/oids/2string/subids/absviarel", oids_test_2string_subids_absviarel);
483 g_test_add_func("/oids/2string/subids/relsizes", oids_test_2string_subids_relsizes);
484 g_test_add_func("/oids/2string/encoded", oids_test_2string_encoded);
485 g_test_add_func("/oids/2string/encoded/rel", oids_test_2string_encoded_rel);
487 /* /oids/2resolved */
488 g_test_add_func("/oids/2resolved/subids", oids_test_2resolved_subids);
489 g_test_add_func("/oids/2resolved/encoded", oids_test_2resolved_encoded);
490 g_test_add_func("/oids/2resolved/encoded/rel", oids_test_2resolved_encoded_rel);
491 g_test_add_func("/oids/2resolved/string", oids_test_2resolved_string);
494 g_test_add_func("/oids/2both/subids", oids_test_2both_subids);
495 g_test_add_func("/oids/2both/encoded", oids_test_2both_encoded);
496 g_test_add_func("/oids/2both/string", oids_test_2both_string);
499 g_test_add_func("/oids/2struct/subids", oids_test_2struct_subids);
500 g_test_add_func("/oids/2struct/encoded", oids_test_2struct_encoded);
501 g_test_add_func("/oids/2struct/string", oids_test_2struct_string);
504 g_test_add_func("/oids/add/subids", oids_test_add_subids);
505 g_test_add_func("/oids/add/encoded", oids_test_add_encoded);
506 g_test_add_func("/oids/add/string", oids_test_add_string);
509 test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
511 result = g_test_run();
513 wmem_destroy_allocator(test_scope);
520 * Editor modelines - http://www.wireshark.org/tools/modelines.html
525 * indent-tabs-mode: nil
528 * vi: set shiftwidth=4 tabstop=8 expandtab:
529 * :indentSize=4:tabSize=8:noTabs=true: