2 * ASN.1 Object Identifier handling tests
3 * Copyright 2013, Edward J. Beroset <beroset@ieee.org>
7 * Wireshark - Network traffic analyzer
8 * By Gerald Combs <gerald@wireshark.org>
9 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include "wmem/wmem.h"
36 static wmem_allocator_t *test_scope;
41 const gchar *resolved;
48 example_s ex1 = {"2.1.1", "joint-iso-itu-t.1.1", 2, "\x51\x01", 3, {2,1,1} };
49 example_s ex2rel = {".81.1", ".81.1", 2, "\x51\x01", 2, {81,1} };
50 example_s ex3 = {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
51 "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
52 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",
53 10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
55 example_s ex4 = {"2.1", "joint-iso-itu-t.1", 1, "\x51", 2, {2,1} };
56 example_s ex5 = {"2", "joint-iso-itu-t", 0, NULL, 1, {2} };
57 example_s ex6rel = {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
58 ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
59 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",
60 9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
61 example_s ex7 = {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, "\x51\x01", 3, {2,1,1} };
64 * These test are organized in order of the appearance, in oids.h, of
65 * the basic oids.c functions that they test. This makes it easier to
66 * get a quick understanding of both the testing and the organization
69 * Tests are named /oids/2<desttype>/<srctype>[<extra>]
70 * where <desttype> is the resulting type of the conversion,
71 * <srctype> is the source type and <extra> is any additional
72 * information to make the test name unique.
74 * The types, for the purpose of this naming convention, are
75 * encoded, subids, string and resolved, both, struct.
78 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
81 oids_test_2subids_encoded(void)
83 guint32 *subids = NULL;
87 len = oid_encoded2subid(ex1.encoded, ex1.encoded_len, &subids);
88 g_assert(len == ex1.subids_len);
89 for (i=0; i < len; i++)
90 g_assert(subids[i] == ex1.subids[i]);
94 oids_test_2subids_encoded_long(void)
96 guint32 *subids = NULL;
100 len = oid_encoded2subid(ex3.encoded, ex3.encoded_len, &subids);
101 g_assert(len == ex3.subids_len);
102 for (i=0; i < len; i++)
103 g_assert(subids[i] == ex3.subids[i]);
107 oids_test_2subids_encoded_absviasub(void)
109 guint32 *subids = NULL;
113 len = oid_encoded2subid_sub(ex1.encoded, ex1.encoded_len, &subids, TRUE);
114 g_assert(len == ex1.subids_len);
115 for (i=0; i < len; i++)
116 g_assert(subids[i] == ex1.subids[i]);
120 oids_test_2subids_encoded_relviasub(void)
122 guint32 *subids = NULL;
126 len = oid_encoded2subid_sub(ex2rel.encoded, ex2rel.encoded_len, &subids, FALSE);
127 g_assert(len == ex2rel.subids_len);
128 for (i=0; i < len; i++)
129 g_assert(subids[i] == ex2rel.subids[i]);
133 oids_test_2subids_string(void)
135 guint32 *subids = NULL;
138 len = oid_string2subid(test_scope, ex1.string, &subids);
139 g_assert(len == ex1.subids_len);
140 for (i=0; i < len; i++)
141 g_assert(subids[i] == ex1.subids[i]);
145 oids_test_2subids_string_tooshort(void)
147 guint32 *subids = NULL;
150 len = oid_string2subid(test_scope, ex5.string, &subids);
151 g_assert(len == ex5.subids_len);
152 for (i=0; i < len; i++)
153 g_assert(subids[i] == ex5.subids[i]);
156 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
159 oids_test_2encoded_string_simple(void)
161 guint8 *encoded = NULL;
164 len = oid_string2encoded(ex1.string, &encoded);
165 g_assert(len == ex1.encoded_len);
166 g_assert(0 == memcmp(encoded, ex1.encoded, len));
170 oids_test_2encoded_string_short(void)
172 guint8 *encoded = NULL;
175 len = oid_string2encoded(ex4.string, &encoded);
176 g_assert(len == ex4.encoded_len);
177 g_assert(0 == memcmp(encoded, ex4.encoded, len));
181 oids_test_2encoded_string_long(void)
183 guint8 *encoded = NULL;
186 len = oid_string2encoded(ex3.string, &encoded);
187 g_assert(len == ex3.encoded_len);
188 g_assert(0 == memcmp(encoded, ex3.encoded, len));
192 oids_test_2encoded_string_tooshort(void)
194 guint8 *encoded = NULL;
197 len = oid_string2encoded(ex5.string, &encoded);
198 g_assert(len == ex5.encoded_len);
199 g_assert(0 == memcmp(encoded, ex5.encoded, len));
203 oids_test_2encoded_subids_simple(void)
205 guint8 *encoded = NULL;
208 len = oid_subid2encoded(ex1.subids_len, ex1.subids, &encoded);
209 g_assert(len == ex1.encoded_len);
210 g_assert(0 == memcmp(encoded, ex1.encoded, len));
214 oids_test_2encoded_subids_bad(void)
216 guint8 *encoded = NULL;
219 len = oid_subid2encoded(ex5.subids_len, ex5.subids, &encoded);
220 g_assert(len == ex5.encoded_len);
221 g_assert(0 == memcmp(encoded, ex5.encoded, len));
224 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
227 oids_test_2string_encoded(void)
231 oid = oid_encoded2string(ex3.encoded, ex3.encoded_len);
232 g_assert_cmpstr(oid, ==, ex3.string);
236 oids_test_2string_encoded_rel(void)
240 oid = rel_oid_encoded2string(ex6rel.encoded, ex3.encoded_len);
241 g_assert_cmpstr(oid, ==, ex6rel.string);
246 oids_test_2string_subids_abs(void)
250 oid = oid_subid2string(ex1.subids, ex1.subids_len);
251 g_assert_cmpstr(oid, ==, ex1.string);
255 oids_test_2string_subids_rel(void)
259 oid = rel_oid_subid2string(ex2rel.subids, ex2rel.subids_len, FALSE);
260 g_assert_cmpstr(oid, ==, ex2rel.string);
264 oids_test_2string_subids_absviarel(void)
268 oid = rel_oid_subid2string(ex1.subids, ex1.subids_len, TRUE);
269 g_assert_cmpstr(oid, ==, ex1.string);
273 oids_test_2string_subids_relsizes(void)
277 oid = rel_oid_subid2string(ex6rel.subids, ex6rel.subids_len, FALSE);
278 g_assert_cmpstr(oid, ==, ex6rel.string);
281 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
284 oids_test_2resolved_subids(void)
288 oid = oid_resolved(ex1.subids_len, ex1.subids);
289 g_assert_cmpstr(oid, ==, ex1.resolved);
293 oids_test_2resolved_encoded(void)
297 oid = oid_resolved_from_encoded(ex1.encoded, ex1.encoded_len);
298 g_assert_cmpstr(oid, ==, ex1.resolved);
302 oids_test_2resolved_encoded_rel(void)
306 oid = rel_oid_resolved_from_encoded(ex2rel.encoded, ex2rel.encoded_len);
307 g_assert_cmpstr(oid, ==, ex2rel.string);
311 oids_test_2resolved_string(void)
315 oid = oid_resolved_from_string(ex1.string);
316 g_assert_cmpstr(oid, ==, ex1.resolved);
319 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
322 oids_test_2both_subids(void)
324 const gchar* resolved;
327 oid_both(ex1.subids_len, ex1.subids, &resolved, &oid);
328 g_assert_cmpstr(resolved, ==, ex1.resolved);
329 g_assert_cmpstr(oid, ==, ex1.string);
333 oids_test_2both_encoded(void)
335 const gchar* resolved;
338 oid_both_from_encoded(ex1.encoded, ex1.encoded_len, &resolved, &oid);
339 g_assert_cmpstr(resolved, ==, ex1.resolved);
340 g_assert_cmpstr(oid, ==, ex1.string);
344 oids_test_2both_string(void)
346 const gchar* resolved;
349 oid_both_from_string(ex1.string, &resolved, &oid);
350 g_assert_cmpstr(resolved, ==, ex1.resolved);
351 g_assert_cmpstr(oid, ==, ex1.string);
354 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
357 oids_test_2struct_subids(void)
363 st = oid_get(ex1.subids_len, ex1.subids, &matched, &left);
364 g_assert(matched == 1);
365 g_assert(left == ex1.subids_len - 1);
366 g_assert(st != NULL);
367 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
371 oids_test_2struct_encoded(void)
379 st = oid_get_from_encoded(ex1.encoded, ex1.encoded_len, &subids, &matched, &left);
380 g_assert(matched == 1);
381 g_assert(left == ex1.subids_len - 1);
382 g_assert(st != NULL);
383 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
384 len = matched + left;
385 g_assert(len == ex1.subids_len);
386 for (i=0; i < len; i++)
387 g_assert(subids[i] == ex1.subids[i]);
391 oids_test_2struct_string(void)
399 st = oid_get_from_string(test_scope, ex1.string, &subids, &matched, &left);
400 g_assert(matched == 1);
401 g_assert(left == ex1.subids_len - 1);
402 g_assert(st != NULL);
403 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
404 len = matched + left;
405 g_assert(len == ex1.subids_len);
406 for (i=0; i < len; i++)
407 g_assert(subids[i] == ex1.subids[i]);
411 oids_test_add_subids(void)
415 oid_add(ex7.resolved, ex7.subids_len, ex7.subids);
416 oid = oid_resolved(ex7.subids_len, ex7.subids);
417 g_assert_cmpstr(oid, ==, ex7.resolved);
421 oids_test_add_encoded(void)
425 oid_add_from_encoded(ex7.resolved, ex7.encoded, ex7.encoded_len);
426 oid = oid_resolved(ex7.subids_len, ex7.subids);
427 g_assert_cmpstr(oid, ==, ex7.resolved);
431 oids_test_add_string(void)
435 oid_add_from_string(ex7.resolved, ex7.string);
436 oid = oid_resolved(ex7.subids_len, ex7.subids);
437 g_assert_cmpstr(oid, ==, ex7.resolved);
441 main(int argc, char **argv)
445 g_test_init(&argc, &argv, NULL);
448 g_test_add_func("/oids/2encoded/subids/simple", oids_test_2encoded_subids_simple);
449 g_test_add_func("/oids/2encoded/subids/bad", oids_test_2encoded_subids_bad);
450 g_test_add_func("/oids/2encoded/string/simple", oids_test_2encoded_string_simple);
451 g_test_add_func("/oids/2encoded/string/short", oids_test_2encoded_string_short);
452 g_test_add_func("/oids/2encoded/string/long", oids_test_2encoded_string_long);
453 g_test_add_func("/oids/2encoded/string/tooshort", oids_test_2encoded_string_tooshort);
456 g_test_add_func("/oids/2subids/string", oids_test_2subids_string);
457 g_test_add_func("/oids/2subids/string/tooshort", oids_test_2subids_string_tooshort);
458 g_test_add_func("/oids/2subids/encoded", oids_test_2subids_encoded);
459 g_test_add_func("/oids/2subids/encoded/long", oids_test_2subids_encoded_long);
460 g_test_add_func("/oids/2subids/encoded/absviasub", oids_test_2subids_encoded_absviasub);
461 g_test_add_func("/oids/2subids/encoded/relviasub", oids_test_2subids_encoded_relviasub);
465 g_test_add_func("/oids/2string/subids/abs", oids_test_2string_subids_abs);
466 g_test_add_func("/oids/2string/subids/rel", oids_test_2string_subids_rel);
467 g_test_add_func("/oids/2string/subids/absviarel", oids_test_2string_subids_absviarel);
468 g_test_add_func("/oids/2string/subids/relsizes", oids_test_2string_subids_relsizes);
469 g_test_add_func("/oids/2string/encoded", oids_test_2string_encoded);
470 g_test_add_func("/oids/2string/encoded/rel", oids_test_2string_encoded_rel);
472 /* /oids/2resolved */
473 g_test_add_func("/oids/2resolved/subids", oids_test_2resolved_subids);
474 g_test_add_func("/oids/2resolved/encoded", oids_test_2resolved_encoded);
475 g_test_add_func("/oids/2resolved/encoded/rel", oids_test_2resolved_encoded_rel);
476 g_test_add_func("/oids/2resolved/string", oids_test_2resolved_string);
479 g_test_add_func("/oids/2both/subids", oids_test_2both_subids);
480 g_test_add_func("/oids/2both/encoded", oids_test_2both_encoded);
481 g_test_add_func("/oids/2both/string", oids_test_2both_string);
484 g_test_add_func("/oids/2struct/subids", oids_test_2struct_subids);
485 g_test_add_func("/oids/2struct/encoded", oids_test_2struct_encoded);
486 g_test_add_func("/oids/2struct/string", oids_test_2struct_string);
489 g_test_add_func("/oids/add/subids", oids_test_add_subids);
490 g_test_add_func("/oids/add/encoded", oids_test_add_encoded);
491 g_test_add_func("/oids/add/string", oids_test_add_string);
495 test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
497 result = g_test_run();
499 wmem_destroy_allocator(test_scope);
502 * This might have been a good place for a call to ep_free_all() but that is not part of the
503 * public interface for emem.h
509 * Editor modelines - http://www.wireshark.org/tools/modelines.html
514 * indent-tabs-mode: nil
517 * vi: set shiftwidth=4 tabstop=8 expandtab:
518 * :indentSize=4:tabSize=8:noTabs=true: