HACK: pinfo->private_data points to smb_info again
[wireshark-wip.git] / epan / oids_test.c
blobc73ead83d7cfe4d4e5016175627700454ce1edb6
1 /* oids_test.c
2 * ASN.1 Object Identifier handling tests
3 * Copyright 2013, Edward J. Beroset <beroset@ieee.org>
5 * $Id: oids_test.c$
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.
26 #include "config.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <glib.h>
32 #include "emem.h"
33 #include "oids.h"
34 #include "wmem/wmem.h"
36 typedef struct
38 const gchar *string;
39 const gchar *resolved;
40 guint encoded_len;
41 const gchar *encoded;
42 guint subids_len;
43 guint32 subids[];
44 } example_s;
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
65 * of oids.h.
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/) */
78 static void
79 oids_test_2subids_encoded(void)
81 guint32 *subids = NULL;
82 guint len;
83 guint i;
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]);
91 static void
92 oids_test_2subids_encoded_long(void)
94 guint32 *subids = NULL;
95 guint len;
96 guint i;
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]);
104 static void
105 oids_test_2subids_encoded_absviasub(void)
107 guint32 *subids = NULL;
108 guint len;
109 guint i;
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]);
117 static void
118 oids_test_2subids_encoded_relviasub(void)
120 guint32 *subids = NULL;
121 guint len;
122 guint i;
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]);
130 static void
131 oids_test_2subids_string(void)
133 guint32 *subids = NULL;
134 guint len, i;
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]);
142 static void
143 oids_test_2subids_string_tooshort(void)
145 guint32 *subids = NULL;
146 guint len, i;
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/) */
156 static void
157 oids_test_2encoded_string_simple(void)
159 guint8 *encoded = NULL;
160 guint len;
162 len = oid_string2encoded(ex1.string, &encoded);
163 g_assert(len == ex1.encoded_len);
164 g_assert(0 == memcmp(encoded, ex1.encoded, len));
167 static void
168 oids_test_2encoded_string_short(void)
170 guint8 *encoded = NULL;
171 guint len;
173 len = oid_string2encoded(ex4.string, &encoded);
174 g_assert(len == ex4.encoded_len);
175 g_assert(0 == memcmp(encoded, ex4.encoded, len));
178 static void
179 oids_test_2encoded_string_long(void)
181 guint8 *encoded = NULL;
182 guint len;
184 len = oid_string2encoded(ex3.string, &encoded);
185 g_assert(len == ex3.encoded_len);
186 g_assert(0 == memcmp(encoded, ex3.encoded, len));
189 static void
190 oids_test_2encoded_string_tooshort(void)
192 guint8 *encoded = NULL;
193 guint len;
195 len = oid_string2encoded(ex5.string, &encoded);
196 g_assert(len == ex5.encoded_len);
197 g_assert(0 == memcmp(encoded, ex5.encoded, len));
200 static void
201 oids_test_2encoded_subids_simple(void)
203 guint8 *encoded = NULL;
204 guint len;
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));
211 static void
212 oids_test_2encoded_subids_bad(void)
214 guint8 *encoded = NULL;
215 guint len;
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/) */
224 static void
225 oids_test_2string_encoded(void)
227 const gchar* oid;
229 oid = oid_encoded2string(ex3.encoded, ex3.encoded_len);
230 g_assert_cmpstr(oid, ==, ex3.string);
233 static void
234 oids_test_2string_encoded_rel(void)
236 const gchar* oid;
238 oid = rel_oid_encoded2string(ex6rel.encoded, ex3.encoded_len);
239 g_assert_cmpstr(oid, ==, ex6rel.string);
243 static void
244 oids_test_2string_subids_abs(void)
246 const gchar* oid;
248 oid = oid_subid2string(ex1.subids, ex1.subids_len);
249 g_assert_cmpstr(oid, ==, ex1.string);
252 static void
253 oids_test_2string_subids_rel(void)
255 const gchar* oid;
257 oid = rel_oid_subid2string(ex2rel.subids, ex2rel.subids_len, FALSE);
258 g_assert_cmpstr(oid, ==, ex2rel.string);
261 static void
262 oids_test_2string_subids_absviarel(void)
264 const gchar* oid;
266 oid = rel_oid_subid2string(ex1.subids, ex1.subids_len, TRUE);
267 g_assert_cmpstr(oid, ==, ex1.string);
270 static void
271 oids_test_2string_subids_relsizes(void)
273 const gchar* oid;
275 oid = rel_oid_subid2string(ex6rel.subids, ex6rel.subids_len, FALSE);
276 g_assert_cmpstr(oid, ==, ex6rel.string);
279 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
281 static void
282 oids_test_2resolved_subids(void)
284 const gchar* oid;
286 oid = oid_resolved(ex1.subids_len, ex1.subids);
287 g_assert_cmpstr(oid, ==, ex1.resolved);
290 static void
291 oids_test_2resolved_encoded(void)
293 const gchar* oid;
295 oid = oid_resolved_from_encoded(ex1.encoded, ex1.encoded_len);
296 g_assert_cmpstr(oid, ==, ex1.resolved);
299 static void
300 oids_test_2resolved_encoded_rel(void)
302 const gchar* oid;
304 oid = rel_oid_resolved_from_encoded(ex2rel.encoded, ex2rel.encoded_len);
305 g_assert_cmpstr(oid, ==, ex2rel.string);
308 static void
309 oids_test_2resolved_string(void)
311 const gchar* oid;
313 oid = oid_resolved_from_string(ex1.string);
314 g_assert_cmpstr(oid, ==, ex1.resolved);
317 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
319 static void
320 oids_test_2both_subids(void)
322 gchar* resolved;
323 gchar* oid;
325 oid_both(ex1.subids_len, ex1.subids, &resolved, &oid);
326 g_assert_cmpstr(resolved, ==, ex1.resolved);
327 g_assert_cmpstr(oid, ==, ex1.string);
330 static void
331 oids_test_2both_encoded(void)
333 gchar* resolved;
334 gchar* oid;
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);
341 static void
342 oids_test_2both_string(void)
344 gchar* resolved;
345 gchar* oid;
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/) */
354 static void
355 oids_test_2struct_subids(void)
357 guint matched;
358 guint left;
359 oid_info_t *st;
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");
368 static void
369 oids_test_2struct_encoded(void)
371 guint matched;
372 guint left;
373 guint32 *subids;
374 oid_info_t *st;
375 guint len, i;
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]);
388 static void
389 oids_test_2struct_string(void)
391 guint matched;
392 guint left;
393 guint32 *subids;
394 oid_info_t *st;
395 guint len, i;
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]);
408 static void
409 oids_test_add_subids(void)
411 const gchar* oid;
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);
418 static void
419 oids_test_add_encoded(void)
421 const gchar* oid;
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);
428 static void
429 oids_test_add_string(void)
431 const gchar* oid;
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)
441 int result;
443 g_test_init(&argc, &argv, NULL);
445 /* /oids/2encoded */
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);
453 /* /oids/2subids */
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);
462 /* /oids/2string */
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);
476 /* /oids/2both */
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);
481 /* /oids/2struct */
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);
486 /* /oids/add */
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);
491 emem_init();
492 wmem_init();
493 oids_init();
494 result = g_test_run();
495 oids_cleanup();
496 wmem_cleanup();
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
501 return result;
505 * Editor modelines - http://www.wireshark.org/tools/modelines.html
507 * Local variables:
508 * c-basic-offset: 4
509 * tab-width: 8
510 * indent-tabs-mode: nil
511 * End:
513 * vi: set shiftwidth=4 tabstop=8 expandtab:
514 * :indentSize=4:tabSize=8:noTabs=true: