packet-ldap: fix regression for SASL handling
[wireshark-sm.git] / epan / oids_test.c
blob99da830aa2e845b9fd6074c54ec946193deff774
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"
14 #include <stdio.h>
15 #include <string.h>
16 #include <glib.h>
18 #include "oids.h"
19 #include "wmem/wmem.h"
21 #include <ws_diag_control.h>
23 static wmem_allocator_t *test_scope;
25 typedef struct
27 const gchar *string;
28 const gchar *resolved;
29 guint encoded_len;
30 const guint8 *encoded;
31 guint subids_len;
32 guint32 subids[];
33 } example_s;
35 DIAG_OFF_PEDANTIC
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} };
50 DIAG_ON_PEDANTIC
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
56 * of oids.h.
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/) */
69 static void
70 oids_test_2subids_encoded(void)
72 guint32 *subids = NULL;
73 guint len;
74 guint i;
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);
83 static void
84 oids_test_2subids_encoded_long(void)
86 guint32 *subids = NULL;
87 guint len;
88 guint i;
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);
97 static void
98 oids_test_2subids_encoded_absviasub(void)
100 guint32 *subids = NULL;
101 guint len;
102 guint i;
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);
111 static void
112 oids_test_2subids_encoded_relviasub(void)
114 guint32 *subids = NULL;
115 guint len;
116 guint i;
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);
125 static void
126 oids_test_2subids_string(void)
128 guint32 *subids = NULL;
129 guint len, i;
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]);
137 static void
138 oids_test_2subids_string_tooshort(void)
140 guint32 *subids = NULL;
141 guint len, i;
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/) */
151 static void
152 oids_test_2encoded_string_simple(void)
154 guint8 *encoded = NULL;
155 guint len;
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);
163 static void
164 oids_test_2encoded_string_short(void)
166 guint8 *encoded = NULL;
167 guint len;
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);
175 static void
176 oids_test_2encoded_string_long(void)
178 guint8 *encoded = NULL;
179 guint len;
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);
187 static void
188 oids_test_2encoded_string_tooshort(void)
190 guint8 *encoded = NULL;
191 guint len;
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);
199 static void
200 oids_test_2encoded_subids_simple(void)
202 guint8 *encoded = NULL;
203 guint len;
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);
211 static void
212 oids_test_2encoded_subids_bad(void)
214 guint8 *encoded = NULL;
215 guint len;
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/) */
225 static void
226 oids_test_2string_encoded(void)
228 gchar* oid;
230 oid = oid_encoded2string(NULL, ex3.encoded, ex3.encoded_len);
231 g_assert_cmpstr(oid, ==, ex3.string);
232 wmem_free(NULL, oid);
235 static void
236 oids_test_2string_encoded_rel(void)
238 gchar* oid;
240 oid = rel_oid_encoded2string(NULL, ex6rel.encoded, ex3.encoded_len);
241 g_assert_cmpstr(oid, ==, ex6rel.string);
242 wmem_free(NULL, oid);
246 static void
247 oids_test_2string_subids_abs(void)
249 gchar* oid;
251 oid = oid_subid2string(NULL, ex1.subids, ex1.subids_len);
252 g_assert_cmpstr(oid, ==, ex1.string);
253 wmem_free(NULL, oid);
256 static void
257 oids_test_2string_subids_rel(void)
259 gchar* oid;
261 oid = rel_oid_subid2string(NULL, ex2rel.subids, ex2rel.subids_len, FALSE);
262 g_assert_cmpstr(oid, ==, ex2rel.string);
263 wmem_free(NULL, oid);
266 static void
267 oids_test_2string_subids_absviarel(void)
269 gchar* oid;
271 oid = rel_oid_subid2string(NULL, ex1.subids, ex1.subids_len, TRUE);
272 g_assert_cmpstr(oid, ==, ex1.string);
273 wmem_free(NULL, oid);
276 static void
277 oids_test_2string_subids_relsizes(void)
279 gchar* oid;
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/) */
288 static void
289 oids_test_2resolved_subids(void)
291 gchar* oid;
293 oid = oid_resolved(NULL, ex1.subids_len, ex1.subids);
294 g_assert_cmpstr(oid, ==, ex1.resolved);
295 wmem_free(NULL, oid);
298 static void
299 oids_test_2resolved_encoded(void)
301 gchar* oid;
303 oid = oid_resolved_from_encoded(NULL, ex1.encoded, ex1.encoded_len);
304 g_assert_cmpstr(oid, ==, ex1.resolved);
305 wmem_free(NULL, oid);
308 static void
309 oids_test_2resolved_encoded_rel(void)
311 gchar* oid;
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);
318 static void
319 oids_test_2resolved_string(void)
321 gchar* oid;
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/) */
330 static void
331 oids_test_2both_subids(void)
333 gchar* resolved;
334 gchar* oid;
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);
343 static void
344 oids_test_2both_encoded(void)
346 gchar* resolved;
347 gchar* oid;
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);
356 static void
357 oids_test_2both_string(void)
359 gchar* resolved;
360 gchar* oid;
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/) */
371 static void
372 oids_test_2struct_subids(void)
374 guint matched;
375 guint left;
376 oid_info_t *st;
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");
385 static void
386 oids_test_2struct_encoded(void)
388 guint matched;
389 guint left;
390 guint32 *subids = NULL;
391 oid_info_t *st;
392 guint len, i;
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);
406 static void
407 oids_test_2struct_string(void)
409 guint matched;
410 guint left;
411 guint32 *subids;
412 oid_info_t *st;
413 guint len, i;
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]);
426 static void
427 oids_test_add_subids(void)
429 gchar* oid;
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);
437 static void
438 oids_test_add_encoded(void)
440 gchar* oid;
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);
448 static void
449 oids_test_add_string(void)
451 gchar* oid;
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)
462 int result;
464 g_test_init(&argc, &argv, NULL);
466 /* /oids/2encoded */
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);
474 /* /oids/2subids */
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);
483 /* /oids/2string */
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);
497 /* /oids/2both */
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);
502 /* /oids/2struct */
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);
507 /* /oids/add */
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);
512 wmem_init();
513 test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
514 oids_init();
515 result = g_test_run();
516 oids_cleanup();
517 wmem_destroy_allocator(test_scope);
518 wmem_cleanup();
520 return result;
524 * Editor modelines - https://www.wireshark.org/tools/modelines.html
526 * Local variables:
527 * c-basic-offset: 4
528 * tab-width: 8
529 * indent-tabs-mode: nil
530 * End:
532 * vi: set shiftwidth=4 tabstop=8 expandtab:
533 * :indentSize=4:tabSize=8:noTabs=true: