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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
31 #include "wmem/wmem.h"
33 static wmem_allocator_t *test_scope;
38 const gchar *resolved;
45 example_s ex1 = {"2.1.1", "joint-iso-itu-t.1.1", 2, "\x51\x01", 3, {2,1,1} };
46 example_s ex2rel = {".81.1", ".81.1", 2, "\x51\x01", 2, {81,1} };
47 example_s ex3 = {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
48 "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
49 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",
50 10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
52 example_s ex4 = {"2.1", "joint-iso-itu-t.1", 1, "\x51", 2, {2,1} };
53 example_s ex5 = {"2", "joint-iso-itu-t", 0, NULL, 1, {2} };
54 example_s ex6rel = {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
55 ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
56 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",
57 9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
58 example_s ex7 = {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, "\x51\x01", 3, {2,1,1} };
61 * These test are organized in order of the appearance, in oids.h, of
62 * the basic oids.c functions that they test. This makes it easier to
63 * get a quick understanding of both the testing and the organization
66 * Tests are named /oids/2<desttype>/<srctype>[<extra>]
67 * where <desttype> is the resulting type of the conversion,
68 * <srctype> is the source type and <extra> is any additional
69 * information to make the test name unique.
71 * The types, for the purpose of this naming convention, are
72 * encoded, subids, string and resolved, both, struct.
75 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
78 oids_test_2subids_encoded(void)
80 guint32 *subids = NULL;
84 len = oid_encoded2subid(NULL, ex1.encoded, ex1.encoded_len, &subids);
85 g_assert(len == ex1.subids_len);
86 for (i=0; i < len; i++)
87 g_assert(subids[i] == ex1.subids[i]);
88 wmem_free(NULL, subids);
92 oids_test_2subids_encoded_long(void)
94 guint32 *subids = NULL;
98 len = oid_encoded2subid(NULL, ex3.encoded, ex3.encoded_len, &subids);
99 g_assert(len == ex3.subids_len);
100 for (i=0; i < len; i++)
101 g_assert(subids[i] == ex3.subids[i]);
102 wmem_free(NULL, subids);
106 oids_test_2subids_encoded_absviasub(void)
108 guint32 *subids = NULL;
112 len = oid_encoded2subid_sub(NULL, ex1.encoded, ex1.encoded_len, &subids, TRUE);
113 g_assert(len == ex1.subids_len);
114 for (i=0; i < len; i++)
115 g_assert(subids[i] == ex1.subids[i]);
116 wmem_free(NULL, subids);
120 oids_test_2subids_encoded_relviasub(void)
122 guint32 *subids = NULL;
126 len = oid_encoded2subid_sub(NULL, 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]);
130 wmem_free(NULL, subids);
134 oids_test_2subids_string(void)
136 guint32 *subids = NULL;
139 len = oid_string2subid(test_scope, ex1.string, &subids);
140 g_assert(len == ex1.subids_len);
141 for (i=0; i < len; i++)
142 g_assert(subids[i] == ex1.subids[i]);
146 oids_test_2subids_string_tooshort(void)
148 guint32 *subids = NULL;
151 len = oid_string2subid(test_scope, ex5.string, &subids);
152 g_assert(len == ex5.subids_len);
153 for (i=0; i < len; i++)
154 g_assert(subids[i] == ex5.subids[i]);
157 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
160 oids_test_2encoded_string_simple(void)
162 guint8 *encoded = NULL;
165 len = oid_string2encoded(NULL, ex1.string, &encoded);
166 g_assert(len == ex1.encoded_len);
167 g_assert(0 == memcmp(encoded, ex1.encoded, len));
168 wmem_free(NULL, encoded);
172 oids_test_2encoded_string_short(void)
174 guint8 *encoded = NULL;
177 len = oid_string2encoded(NULL, ex4.string, &encoded);
178 g_assert(len == ex4.encoded_len);
179 g_assert(0 == memcmp(encoded, ex4.encoded, len));
180 wmem_free(NULL, encoded);
184 oids_test_2encoded_string_long(void)
186 guint8 *encoded = NULL;
189 len = oid_string2encoded(NULL, ex3.string, &encoded);
190 g_assert(len == ex3.encoded_len);
191 g_assert(0 == memcmp(encoded, ex3.encoded, len));
192 wmem_free(NULL, encoded);
196 oids_test_2encoded_string_tooshort(void)
198 guint8 *encoded = NULL;
201 len = oid_string2encoded(NULL, ex5.string, &encoded);
202 g_assert(len == ex5.encoded_len);
203 g_assert(0 == memcmp(encoded, ex5.encoded, len));
204 wmem_free(NULL, encoded);
208 oids_test_2encoded_subids_simple(void)
210 guint8 *encoded = NULL;
213 len = oid_subid2encoded(NULL, ex1.subids_len, ex1.subids, &encoded);
214 g_assert(len == ex1.encoded_len);
215 g_assert(0 == memcmp(encoded, ex1.encoded, len));
216 wmem_free(NULL, encoded);
220 oids_test_2encoded_subids_bad(void)
222 guint8 *encoded = NULL;
225 len = oid_subid2encoded(NULL, ex5.subids_len, ex5.subids, &encoded);
226 g_assert(len == ex5.encoded_len);
227 g_assert(0 == memcmp(encoded, ex5.encoded, len));
228 wmem_free(NULL, encoded);
231 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
234 oids_test_2string_encoded(void)
238 oid = oid_encoded2string(NULL, ex3.encoded, ex3.encoded_len);
239 g_assert_cmpstr(oid, ==, ex3.string);
240 wmem_free(NULL, oid);
244 oids_test_2string_encoded_rel(void)
248 oid = rel_oid_encoded2string(NULL, ex6rel.encoded, ex3.encoded_len);
249 g_assert_cmpstr(oid, ==, ex6rel.string);
250 wmem_free(NULL, oid);
255 oids_test_2string_subids_abs(void)
259 oid = oid_subid2string(NULL, ex1.subids, ex1.subids_len);
260 g_assert_cmpstr(oid, ==, ex1.string);
261 wmem_free(NULL, oid);
265 oids_test_2string_subids_rel(void)
269 oid = rel_oid_subid2string(NULL, ex2rel.subids, ex2rel.subids_len, FALSE);
270 g_assert_cmpstr(oid, ==, ex2rel.string);
271 wmem_free(NULL, oid);
275 oids_test_2string_subids_absviarel(void)
279 oid = rel_oid_subid2string(NULL, ex1.subids, ex1.subids_len, TRUE);
280 g_assert_cmpstr(oid, ==, ex1.string);
281 wmem_free(NULL, oid);
285 oids_test_2string_subids_relsizes(void)
289 oid = rel_oid_subid2string(NULL, ex6rel.subids, ex6rel.subids_len, FALSE);
290 g_assert_cmpstr(oid, ==, ex6rel.string);
291 wmem_free(NULL, oid);
294 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
297 oids_test_2resolved_subids(void)
301 oid = oid_resolved(NULL, ex1.subids_len, ex1.subids);
302 g_assert_cmpstr(oid, ==, ex1.resolved);
303 wmem_free(NULL, oid);
307 oids_test_2resolved_encoded(void)
311 oid = oid_resolved_from_encoded(NULL, ex1.encoded, ex1.encoded_len);
312 g_assert_cmpstr(oid, ==, ex1.resolved);
313 wmem_free(NULL, oid);
317 oids_test_2resolved_encoded_rel(void)
321 oid = rel_oid_resolved_from_encoded(NULL, ex2rel.encoded, ex2rel.encoded_len);
322 g_assert_cmpstr(oid, ==, ex2rel.string);
323 wmem_free(NULL, oid);
327 oids_test_2resolved_string(void)
331 oid = oid_resolved_from_string(NULL, ex1.string);
332 g_assert_cmpstr(oid, ==, ex1.resolved);
333 wmem_free(NULL, oid);
336 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
339 oids_test_2both_subids(void)
344 oid_both(NULL, ex1.subids_len, ex1.subids, &resolved, &oid);
345 g_assert_cmpstr(resolved, ==, ex1.resolved);
346 g_assert_cmpstr(oid, ==, ex1.string);
347 wmem_free(NULL, resolved);
348 wmem_free(NULL, oid);
352 oids_test_2both_encoded(void)
357 oid_both_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &resolved, &oid);
358 g_assert_cmpstr(resolved, ==, ex1.resolved);
359 g_assert_cmpstr(oid, ==, ex1.string);
360 wmem_free(NULL, resolved);
361 wmem_free(NULL, oid);
365 oids_test_2both_string(void)
370 oid_both_from_string(NULL, ex1.string, &resolved, &oid);
371 g_assert_cmpstr(resolved, ==, ex1.resolved);
372 g_assert_cmpstr(oid, ==, ex1.string);
373 wmem_free(NULL, resolved);
374 wmem_free(NULL, oid);
377 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
380 oids_test_2struct_subids(void)
386 st = oid_get(ex1.subids_len, ex1.subids, &matched, &left);
387 g_assert(matched == 1);
388 g_assert(left == ex1.subids_len - 1);
389 g_assert(st != NULL);
390 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
394 oids_test_2struct_encoded(void)
398 guint32 *subids = NULL;
402 st = oid_get_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &subids, &matched, &left);
403 g_assert(matched == 1);
404 g_assert(left == ex1.subids_len - 1);
405 g_assert(st != NULL);
406 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
407 len = matched + left;
408 g_assert(len == ex1.subids_len);
409 for (i=0; i < len; i++)
410 g_assert(subids[i] == ex1.subids[i]);
411 wmem_free(NULL, subids);
415 oids_test_2struct_string(void)
423 st = oid_get_from_string(test_scope, ex1.string, &subids, &matched, &left);
424 g_assert(matched == 1);
425 g_assert(left == ex1.subids_len - 1);
426 g_assert(st != NULL);
427 g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
428 len = matched + left;
429 g_assert(len == ex1.subids_len);
430 for (i=0; i < len; i++)
431 g_assert(subids[i] == ex1.subids[i]);
435 oids_test_add_subids(void)
439 oid_add(ex7.resolved, ex7.subids_len, ex7.subids);
440 oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
441 g_assert_cmpstr(oid, ==, ex7.resolved);
442 wmem_free(NULL, oid);
446 oids_test_add_encoded(void)
450 oid_add_from_encoded(ex7.resolved, ex7.encoded, ex7.encoded_len);
451 oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
452 g_assert_cmpstr(oid, ==, ex7.resolved);
453 wmem_free(NULL, oid);
457 oids_test_add_string(void)
461 oid_add_from_string(ex7.resolved, ex7.string);
462 oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
463 g_assert_cmpstr(oid, ==, ex7.resolved);
464 wmem_free(NULL, oid);
468 main(int argc, char **argv)
472 g_test_init(&argc, &argv, NULL);
475 g_test_add_func("/oids/2encoded/subids/simple", oids_test_2encoded_subids_simple);
476 g_test_add_func("/oids/2encoded/subids/bad", oids_test_2encoded_subids_bad);
477 g_test_add_func("/oids/2encoded/string/simple", oids_test_2encoded_string_simple);
478 g_test_add_func("/oids/2encoded/string/short", oids_test_2encoded_string_short);
479 g_test_add_func("/oids/2encoded/string/long", oids_test_2encoded_string_long);
480 g_test_add_func("/oids/2encoded/string/tooshort", oids_test_2encoded_string_tooshort);
483 g_test_add_func("/oids/2subids/string", oids_test_2subids_string);
484 g_test_add_func("/oids/2subids/string/tooshort", oids_test_2subids_string_tooshort);
485 g_test_add_func("/oids/2subids/encoded", oids_test_2subids_encoded);
486 g_test_add_func("/oids/2subids/encoded/long", oids_test_2subids_encoded_long);
487 g_test_add_func("/oids/2subids/encoded/absviasub", oids_test_2subids_encoded_absviasub);
488 g_test_add_func("/oids/2subids/encoded/relviasub", oids_test_2subids_encoded_relviasub);
492 g_test_add_func("/oids/2string/subids/abs", oids_test_2string_subids_abs);
493 g_test_add_func("/oids/2string/subids/rel", oids_test_2string_subids_rel);
494 g_test_add_func("/oids/2string/subids/absviarel", oids_test_2string_subids_absviarel);
495 g_test_add_func("/oids/2string/subids/relsizes", oids_test_2string_subids_relsizes);
496 g_test_add_func("/oids/2string/encoded", oids_test_2string_encoded);
497 g_test_add_func("/oids/2string/encoded/rel", oids_test_2string_encoded_rel);
499 /* /oids/2resolved */
500 g_test_add_func("/oids/2resolved/subids", oids_test_2resolved_subids);
501 g_test_add_func("/oids/2resolved/encoded", oids_test_2resolved_encoded);
502 g_test_add_func("/oids/2resolved/encoded/rel", oids_test_2resolved_encoded_rel);
503 g_test_add_func("/oids/2resolved/string", oids_test_2resolved_string);
506 g_test_add_func("/oids/2both/subids", oids_test_2both_subids);
507 g_test_add_func("/oids/2both/encoded", oids_test_2both_encoded);
508 g_test_add_func("/oids/2both/string", oids_test_2both_string);
511 g_test_add_func("/oids/2struct/subids", oids_test_2struct_subids);
512 g_test_add_func("/oids/2struct/encoded", oids_test_2struct_encoded);
513 g_test_add_func("/oids/2struct/string", oids_test_2struct_string);
516 g_test_add_func("/oids/add/subids", oids_test_add_subids);
517 g_test_add_func("/oids/add/encoded", oids_test_add_encoded);
518 g_test_add_func("/oids/add/string", oids_test_add_string);
521 test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
523 result = g_test_run();
525 wmem_destroy_allocator(test_scope);
532 * Editor modelines - http://www.wireshark.org/tools/modelines.html
537 * indent-tabs-mode: nil
540 * vi: set shiftwidth=4 tabstop=8 expandtab:
541 * :indentSize=4:tabSize=8:noTabs=true: