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"
39 const gchar
*resolved
;
46 example_s ex1
= {"2.1.1", "joint-iso-itu-t.1.1", 2, "\x51\x01", 3, {2,1,1} };
47 example_s ex2rel
= {".81.1", ".81.1", 2, "\x51\x01", 2, {81,1} };
48 example_s ex3
= {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
49 "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
50 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",
51 10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
53 example_s ex4
= {"2.1", "joint-iso-itu-t.1", 1, "\x51", 2, {2,1} };
54 example_s ex5
= {"2", "joint-iso-itu-t", 0, NULL
, 1, {2} };
55 example_s ex6rel
= {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
56 ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
57 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",
58 9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
59 example_s ex7
= {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, "\x51\x01", 3, {2,1,1} };
62 * These test are organized in order of the appearance, in oids.h, of
63 * the basic oids.c functions that they test. This makes it easier to
64 * get a quick understanding of both the testing and the organization
67 * Tests are named /oids/2<desttype>/<srctype>[<extra>]
68 * where <desttype> is the resulting type of the conversion,
69 * <srctype> is the source type and <extra> is any additional
70 * information to make the test name unique.
72 * The types, for the purpose of this naming convention, are
73 * encoded, subids, string and resolved, both, struct.
76 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
79 oids_test_2subids_encoded(void)
81 guint32
*subids
= NULL
;
85 len
= oid_encoded2subid(ex1
.encoded
, ex1
.encoded_len
, &subids
);
86 g_assert(len
== ex1
.subids_len
);
87 for (i
=0; i
< len
; i
++)
88 g_assert(subids
[i
] == ex1
.subids
[i
]);
92 oids_test_2subids_encoded_long(void)
94 guint32
*subids
= NULL
;
98 len
= oid_encoded2subid(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
]);
105 oids_test_2subids_encoded_absviasub(void)
107 guint32
*subids
= NULL
;
111 len
= oid_encoded2subid_sub(ex1
.encoded
, ex1
.encoded_len
, &subids
, TRUE
);
112 g_assert(len
== ex1
.subids_len
);
113 for (i
=0; i
< len
; i
++)
114 g_assert(subids
[i
] == ex1
.subids
[i
]);
118 oids_test_2subids_encoded_relviasub(void)
120 guint32
*subids
= NULL
;
124 len
= oid_encoded2subid_sub(ex2rel
.encoded
, ex2rel
.encoded_len
, &subids
, FALSE
);
125 g_assert(len
== ex2rel
.subids_len
);
126 for (i
=0; i
< len
; i
++)
127 g_assert(subids
[i
] == ex2rel
.subids
[i
]);
131 oids_test_2subids_string(void)
133 guint32
*subids
= NULL
;
136 len
= oid_string2subid(ex1
.string
, &subids
);
137 g_assert(len
== ex1
.subids_len
);
138 for (i
=0; i
< len
; i
++)
139 g_assert(subids
[i
] == ex1
.subids
[i
]);
143 oids_test_2subids_string_tooshort(void)
145 guint32
*subids
= NULL
;
148 len
= oid_string2subid(ex5
.string
, &subids
);
149 g_assert(len
== ex5
.subids_len
);
150 for (i
=0; i
< len
; i
++)
151 g_assert(subids
[i
] == ex5
.subids
[i
]);
154 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
157 oids_test_2encoded_string_simple(void)
159 guint8
*encoded
= NULL
;
162 len
= oid_string2encoded(ex1
.string
, &encoded
);
163 g_assert(len
== ex1
.encoded_len
);
164 g_assert(0 == memcmp(encoded
, ex1
.encoded
, len
));
168 oids_test_2encoded_string_short(void)
170 guint8
*encoded
= NULL
;
173 len
= oid_string2encoded(ex4
.string
, &encoded
);
174 g_assert(len
== ex4
.encoded_len
);
175 g_assert(0 == memcmp(encoded
, ex4
.encoded
, len
));
179 oids_test_2encoded_string_long(void)
181 guint8
*encoded
= NULL
;
184 len
= oid_string2encoded(ex3
.string
, &encoded
);
185 g_assert(len
== ex3
.encoded_len
);
186 g_assert(0 == memcmp(encoded
, ex3
.encoded
, len
));
190 oids_test_2encoded_string_tooshort(void)
192 guint8
*encoded
= NULL
;
195 len
= oid_string2encoded(ex5
.string
, &encoded
);
196 g_assert(len
== ex5
.encoded_len
);
197 g_assert(0 == memcmp(encoded
, ex5
.encoded
, len
));
201 oids_test_2encoded_subids_simple(void)
203 guint8
*encoded
= NULL
;
206 len
= oid_subid2encoded(ex1
.subids_len
, ex1
.subids
, &encoded
);
207 g_assert(len
== ex1
.encoded_len
);
208 g_assert(0 == memcmp(encoded
, ex1
.encoded
, len
));
212 oids_test_2encoded_subids_bad(void)
214 guint8
*encoded
= NULL
;
217 len
= oid_subid2encoded(ex5
.subids_len
, ex5
.subids
, &encoded
);
218 g_assert(len
== ex5
.encoded_len
);
219 g_assert(0 == memcmp(encoded
, ex5
.encoded
, len
));
222 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
225 oids_test_2string_encoded(void)
229 oid
= oid_encoded2string(ex3
.encoded
, ex3
.encoded_len
);
230 g_assert_cmpstr(oid
, ==, ex3
.string
);
234 oids_test_2string_encoded_rel(void)
238 oid
= rel_oid_encoded2string(ex6rel
.encoded
, ex3
.encoded_len
);
239 g_assert_cmpstr(oid
, ==, ex6rel
.string
);
244 oids_test_2string_subids_abs(void)
248 oid
= oid_subid2string(ex1
.subids
, ex1
.subids_len
);
249 g_assert_cmpstr(oid
, ==, ex1
.string
);
253 oids_test_2string_subids_rel(void)
257 oid
= rel_oid_subid2string(ex2rel
.subids
, ex2rel
.subids_len
, FALSE
);
258 g_assert_cmpstr(oid
, ==, ex2rel
.string
);
262 oids_test_2string_subids_absviarel(void)
266 oid
= rel_oid_subid2string(ex1
.subids
, ex1
.subids_len
, TRUE
);
267 g_assert_cmpstr(oid
, ==, ex1
.string
);
271 oids_test_2string_subids_relsizes(void)
275 oid
= rel_oid_subid2string(ex6rel
.subids
, ex6rel
.subids_len
, FALSE
);
276 g_assert_cmpstr(oid
, ==, ex6rel
.string
);
279 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
282 oids_test_2resolved_subids(void)
286 oid
= oid_resolved(ex1
.subids_len
, ex1
.subids
);
287 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
291 oids_test_2resolved_encoded(void)
295 oid
= oid_resolved_from_encoded(ex1
.encoded
, ex1
.encoded_len
);
296 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
300 oids_test_2resolved_encoded_rel(void)
304 oid
= rel_oid_resolved_from_encoded(ex2rel
.encoded
, ex2rel
.encoded_len
);
305 g_assert_cmpstr(oid
, ==, ex2rel
.string
);
309 oids_test_2resolved_string(void)
313 oid
= oid_resolved_from_string(ex1
.string
);
314 g_assert_cmpstr(oid
, ==, ex1
.resolved
);
317 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
320 oids_test_2both_subids(void)
325 oid_both(ex1
.subids_len
, ex1
.subids
, &resolved
, &oid
);
326 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
327 g_assert_cmpstr(oid
, ==, ex1
.string
);
331 oids_test_2both_encoded(void)
336 oid_both_from_encoded(ex1
.encoded
, ex1
.encoded_len
, &resolved
, &oid
);
337 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
338 g_assert_cmpstr(oid
, ==, ex1
.string
);
342 oids_test_2both_string(void)
347 oid_both_from_string(ex1
.string
, &resolved
, &oid
);
348 g_assert_cmpstr(resolved
, ==, ex1
.resolved
);
349 g_assert_cmpstr(oid
, ==, ex1
.string
);
352 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
355 oids_test_2struct_subids(void)
361 st
= oid_get(ex1
.subids_len
, ex1
.subids
, &matched
, &left
);
362 g_assert(matched
== 1);
363 g_assert(left
== ex1
.subids_len
- 1);
364 g_assert(st
!= NULL
);
365 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
369 oids_test_2struct_encoded(void)
377 st
= oid_get_from_encoded(ex1
.encoded
, ex1
.encoded_len
, &subids
, &matched
, &left
);
378 g_assert(matched
== 1);
379 g_assert(left
== ex1
.subids_len
- 1);
380 g_assert(st
!= NULL
);
381 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
382 len
= matched
+ left
;
383 g_assert(len
== ex1
.subids_len
);
384 for (i
=0; i
< len
; i
++)
385 g_assert(subids
[i
] == ex1
.subids
[i
]);
389 oids_test_2struct_string(void)
397 st
= oid_get_from_string(ex1
.string
, &subids
, &matched
, &left
);
398 g_assert(matched
== 1);
399 g_assert(left
== ex1
.subids_len
- 1);
400 g_assert(st
!= NULL
);
401 g_assert_cmpstr(st
->name
, ==, "joint-iso-itu-t");
402 len
= matched
+ left
;
403 g_assert(len
== ex1
.subids_len
);
404 for (i
=0; i
< len
; i
++)
405 g_assert(subids
[i
] == ex1
.subids
[i
]);
409 oids_test_add_subids(void)
413 oid_add(ex7
.resolved
, ex7
.subids_len
, ex7
.subids
);
414 oid
= oid_resolved(ex7
.subids_len
, ex7
.subids
);
415 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
419 oids_test_add_encoded(void)
423 oid_add_from_encoded(ex7
.resolved
, ex7
.encoded
, ex7
.encoded_len
);
424 oid
= oid_resolved(ex7
.subids_len
, ex7
.subids
);
425 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
429 oids_test_add_string(void)
433 oid_add_from_string(ex7
.resolved
, ex7
.string
);
434 oid
= oid_resolved(ex7
.subids_len
, ex7
.subids
);
435 g_assert_cmpstr(oid
, ==, ex7
.resolved
);
439 main(int argc
, char **argv
)
443 g_test_init(&argc
, &argv
, NULL
);
446 g_test_add_func("/oids/2encoded/subids/simple", oids_test_2encoded_subids_simple
);
447 g_test_add_func("/oids/2encoded/subids/bad", oids_test_2encoded_subids_bad
);
448 g_test_add_func("/oids/2encoded/string/simple", oids_test_2encoded_string_simple
);
449 g_test_add_func("/oids/2encoded/string/short", oids_test_2encoded_string_short
);
450 g_test_add_func("/oids/2encoded/string/long", oids_test_2encoded_string_long
);
451 g_test_add_func("/oids/2encoded/string/tooshort", oids_test_2encoded_string_tooshort
);
454 g_test_add_func("/oids/2subids/string", oids_test_2subids_string
);
455 g_test_add_func("/oids/2subids/string/tooshort", oids_test_2subids_string_tooshort
);
456 g_test_add_func("/oids/2subids/encoded", oids_test_2subids_encoded
);
457 g_test_add_func("/oids/2subids/encoded/long", oids_test_2subids_encoded_long
);
458 g_test_add_func("/oids/2subids/encoded/absviasub", oids_test_2subids_encoded_absviasub
);
459 g_test_add_func("/oids/2subids/encoded/relviasub", oids_test_2subids_encoded_relviasub
);
463 g_test_add_func("/oids/2string/subids/abs", oids_test_2string_subids_abs
);
464 g_test_add_func("/oids/2string/subids/rel", oids_test_2string_subids_rel
);
465 g_test_add_func("/oids/2string/subids/absviarel", oids_test_2string_subids_absviarel
);
466 g_test_add_func("/oids/2string/subids/relsizes", oids_test_2string_subids_relsizes
);
467 g_test_add_func("/oids/2string/encoded", oids_test_2string_encoded
);
468 g_test_add_func("/oids/2string/encoded/rel", oids_test_2string_encoded_rel
);
470 /* /oids/2resolved */
471 g_test_add_func("/oids/2resolved/subids", oids_test_2resolved_subids
);
472 g_test_add_func("/oids/2resolved/encoded", oids_test_2resolved_encoded
);
473 g_test_add_func("/oids/2resolved/encoded/rel", oids_test_2resolved_encoded_rel
);
474 g_test_add_func("/oids/2resolved/string", oids_test_2resolved_string
);
477 g_test_add_func("/oids/2both/subids", oids_test_2both_subids
);
478 g_test_add_func("/oids/2both/encoded", oids_test_2both_encoded
);
479 g_test_add_func("/oids/2both/string", oids_test_2both_string
);
482 g_test_add_func("/oids/2struct/subids", oids_test_2struct_subids
);
483 g_test_add_func("/oids/2struct/encoded", oids_test_2struct_encoded
);
484 g_test_add_func("/oids/2struct/string", oids_test_2struct_string
);
487 g_test_add_func("/oids/add/subids", oids_test_add_subids
);
488 g_test_add_func("/oids/add/encoded", oids_test_add_encoded
);
489 g_test_add_func("/oids/add/string", oids_test_add_string
);
494 result
= g_test_run();
498 * This might have been a good place for a call to ep_free_all() but that is not part of the
499 * public interface for emem.h
505 * Editor modelines - http://www.wireshark.org/tools/modelines.html
510 * indent-tabs-mode: nil
513 * vi: set shiftwidth=4 tabstop=8 expandtab:
514 * :indentSize=4:tabSize=8:noTabs=true: