Revert "LATER... ei_kerberos_kdc_session_key ..."
[wireshark-sm.git] / epan / oids_test.c
blob4c57f2d05350a5b0d7ed60bc42e3e8c22e79bf99
1 /* oids_test.c
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
12 #include "config.h"
13 #undef G_DISABLE_ASSERT
15 #include <stdio.h>
16 #include <string.h>
17 #include <glib.h>
19 #include "oids.h"
20 #include <epan/wmem_scopes.h>
22 #include <ws_diag_control.h>
24 static wmem_allocator_t *test_scope;
26 typedef struct
28 const char *string;
29 const char *resolved;
30 unsigned encoded_len;
31 const uint8_t *encoded;
32 unsigned subids_len;
33 uint32_t subids[];
34 } example_s;
36 DIAG_OFF_PEDANTIC
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} };
51 DIAG_ON_PEDANTIC
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
57 * of oids.h.
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/) */
70 static void
71 oids_test_2subids_encoded(void)
73 uint32_t *subids = NULL;
74 unsigned len;
75 unsigned i;
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);
84 static void
85 oids_test_2subids_encoded_long(void)
87 uint32_t *subids = NULL;
88 unsigned len;
89 unsigned i;
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);
98 static void
99 oids_test_2subids_encoded_absviasub(void)
101 uint32_t *subids = NULL;
102 unsigned len;
103 unsigned i;
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);
112 static void
113 oids_test_2subids_encoded_relviasub(void)
115 uint32_t *subids = NULL;
116 unsigned len;
117 unsigned i;
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);
126 static void
127 oids_test_2subids_string(void)
129 uint32_t *subids = NULL;
130 unsigned len, i;
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]);
138 static void
139 oids_test_2subids_string_tooshort(void)
141 uint32_t *subids = NULL;
142 unsigned len, i;
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/) */
152 static void
153 oids_test_2encoded_string_simple(void)
155 uint8_t *encoded = NULL;
156 unsigned len;
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);
164 static void
165 oids_test_2encoded_string_short(void)
167 uint8_t *encoded = NULL;
168 unsigned len;
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);
176 static void
177 oids_test_2encoded_string_long(void)
179 uint8_t *encoded = NULL;
180 unsigned len;
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);
188 static void
189 oids_test_2encoded_string_tooshort(void)
191 uint8_t *encoded = NULL;
192 unsigned len;
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);
200 static void
201 oids_test_2encoded_subids_simple(void)
203 uint8_t *encoded = NULL;
204 unsigned len;
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);
212 static void
213 oids_test_2encoded_subids_bad(void)
215 uint8_t *encoded = NULL;
216 unsigned len;
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/) */
226 static void
227 oids_test_2string_encoded(void)
229 char* oid;
231 oid = oid_encoded2string(NULL, ex3.encoded, ex3.encoded_len);
232 g_assert_cmpstr(oid, ==, ex3.string);
233 wmem_free(NULL, oid);
236 static void
237 oids_test_2string_encoded_rel(void)
239 char* oid;
241 oid = rel_oid_encoded2string(NULL, ex6rel.encoded, ex3.encoded_len);
242 g_assert_cmpstr(oid, ==, ex6rel.string);
243 wmem_free(NULL, oid);
247 static void
248 oids_test_2string_subids_abs(void)
250 char* oid;
252 oid = oid_subid2string(NULL, ex1.subids, ex1.subids_len);
253 g_assert_cmpstr(oid, ==, ex1.string);
254 wmem_free(NULL, oid);
257 static void
258 oids_test_2string_subids_rel(void)
260 char* oid;
262 oid = rel_oid_subid2string(NULL, ex2rel.subids, ex2rel.subids_len, false);
263 g_assert_cmpstr(oid, ==, ex2rel.string);
264 wmem_free(NULL, oid);
267 static void
268 oids_test_2string_subids_absviarel(void)
270 char* oid;
272 oid = rel_oid_subid2string(NULL, ex1.subids, ex1.subids_len, true);
273 g_assert_cmpstr(oid, ==, ex1.string);
274 wmem_free(NULL, oid);
277 static void
278 oids_test_2string_subids_relsizes(void)
280 char* oid;
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/) */
289 static void
290 oids_test_2resolved_subids(void)
292 char* oid;
294 oid = oid_resolved(NULL, ex1.subids_len, ex1.subids);
295 g_assert_cmpstr(oid, ==, ex1.resolved);
296 wmem_free(NULL, oid);
299 static void
300 oids_test_2resolved_encoded(void)
302 char* oid;
304 oid = oid_resolved_from_encoded(NULL, ex1.encoded, ex1.encoded_len);
305 g_assert_cmpstr(oid, ==, ex1.resolved);
306 wmem_free(NULL, oid);
309 static void
310 oids_test_2resolved_encoded_rel(void)
312 char* oid;
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);
319 static void
320 oids_test_2resolved_string(void)
322 char* oid;
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/) */
331 static void
332 oids_test_2both_subids(void)
334 char* resolved;
335 char* oid;
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);
344 static void
345 oids_test_2both_encoded(void)
347 char* resolved;
348 char* oid;
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);
357 static void
358 oids_test_2both_string(void)
360 char* resolved;
361 char* oid;
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/) */
372 static void
373 oids_test_2struct_subids(void)
375 unsigned matched;
376 unsigned left;
377 oid_info_t *st;
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");
386 static void
387 oids_test_2struct_encoded(void)
389 unsigned matched;
390 unsigned left;
391 uint32_t *subids = NULL;
392 oid_info_t *st;
393 unsigned len, i;
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);
407 static void
408 oids_test_2struct_string(void)
410 unsigned matched;
411 unsigned left;
412 uint32_t *subids;
413 oid_info_t *st;
414 unsigned len, i;
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]);
427 static void
428 oids_test_add_subids(void)
430 char* oid;
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);
438 static void
439 oids_test_add_encoded(void)
441 char* oid;
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);
449 static void
450 oids_test_add_string(void)
452 char* oid;
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)
463 int result;
465 g_test_init(&argc, &argv, NULL);
467 /* /oids/2encoded */
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);
475 /* /oids/2subids */
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);
484 /* /oids/2string */
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);
498 /* /oids/2both */
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);
503 /* /oids/2struct */
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);
508 /* /oids/add */
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);
513 wmem_init_scopes();
514 test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
515 oids_init();
516 result = g_test_run();
517 oids_cleanup();
518 wmem_destroy_allocator(test_scope);
519 wmem_cleanup_scopes();
521 return result;
525 * Editor modelines - https://www.wireshark.org/tools/modelines.html
527 * Local variables:
528 * c-basic-offset: 4
529 * tab-width: 8
530 * indent-tabs-mode: nil
531 * End:
533 * vi: set shiftwidth=4 tabstop=8 expandtab:
534 * :indentSize=4:tabSize=8:noTabs=true: