Don't double-close the input.
[metze/wireshark/wip.git] / epan / oids_test.c
1 /* oids_test.c
2  * ASN.1 Object Identifier handling tests
3  * Copyright 2013, Edward J. Beroset <beroset@ieee.org>
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
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.
13  *
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.
18  *
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.
22  */
23
24 #include "config.h"
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <glib.h>
29
30 #include "oids.h"
31 #include "wmem/wmem.h"
32
33 static wmem_allocator_t *test_scope;
34
35 typedef struct
36 {
37     const gchar *string;
38     const gchar *resolved;
39     guint encoded_len;
40     const gchar *encoded;
41     guint subids_len;
42     guint32 subids[];
43 } example_s;
44
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} };
51
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} };
59
60 /*
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
64  * of oids.h.
65  *
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.
70  *
71  * The types, for the purpose of this naming convention, are
72  * encoded, subids, string and resolved, both, struct.
73  */
74
75 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
76
77 static void
78 oids_test_2subids_encoded(void)
79 {
80     guint32 *subids = NULL;
81     guint len;
82     guint i;
83
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);
89 }
90
91 static void
92 oids_test_2subids_encoded_long(void)
93 {
94     guint32 *subids = NULL;
95     guint len;
96     guint i;
97
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);
103 }
104
105 static void
106 oids_test_2subids_encoded_absviasub(void)
107 {
108     guint32 *subids = NULL;
109     guint len;
110     guint i;
111
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);
117 }
118
119 static void
120 oids_test_2subids_encoded_relviasub(void)
121 {
122     guint32 *subids = NULL;
123     guint len;
124     guint i;
125
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);
131 }
132
133 static void
134 oids_test_2subids_string(void)
135 {
136     guint32 *subids = NULL;
137     guint len, i;
138
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]);
143 }
144
145 static void
146 oids_test_2subids_string_tooshort(void)
147 {
148     guint32 *subids = NULL;
149     guint len, i;
150
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]);
155 }
156
157 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
158
159 static void
160 oids_test_2encoded_string_simple(void)
161 {
162     guint8 *encoded = NULL;
163     guint len;
164
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);
169 }
170
171 static void
172 oids_test_2encoded_string_short(void)
173 {
174     guint8 *encoded = NULL;
175     guint len;
176
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);
181 }
182
183 static void
184 oids_test_2encoded_string_long(void)
185 {
186     guint8 *encoded = NULL;
187     guint len;
188
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);
193 }
194
195 static void
196 oids_test_2encoded_string_tooshort(void)
197 {
198     guint8 *encoded = NULL;
199     guint len;
200
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);
205 }
206
207 static void
208 oids_test_2encoded_subids_simple(void)
209 {
210     guint8 *encoded = NULL;
211     guint len;
212
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);
217 }
218
219 static void
220 oids_test_2encoded_subids_bad(void)
221 {
222     guint8 *encoded = NULL;
223     guint len;
224
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);
229 }
230
231 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
232
233 static void
234 oids_test_2string_encoded(void)
235 {
236     gchar* oid;
237
238     oid = oid_encoded2string(NULL, ex3.encoded, ex3.encoded_len);
239     g_assert_cmpstr(oid, ==, ex3.string);
240     wmem_free(NULL, oid);
241 }
242
243 static void
244 oids_test_2string_encoded_rel(void)
245 {
246     gchar* oid;
247
248     oid = rel_oid_encoded2string(NULL, ex6rel.encoded, ex3.encoded_len);
249     g_assert_cmpstr(oid, ==, ex6rel.string);
250     wmem_free(NULL, oid);
251 }
252
253
254 static void
255 oids_test_2string_subids_abs(void)
256 {
257     gchar* oid;
258
259     oid = oid_subid2string(NULL, ex1.subids, ex1.subids_len);
260     g_assert_cmpstr(oid, ==, ex1.string);
261     wmem_free(NULL, oid);
262 }
263
264 static void
265 oids_test_2string_subids_rel(void)
266 {
267     gchar* oid;
268
269     oid = rel_oid_subid2string(NULL, ex2rel.subids, ex2rel.subids_len, FALSE);
270     g_assert_cmpstr(oid, ==, ex2rel.string);
271     wmem_free(NULL, oid);
272 }
273
274 static void
275 oids_test_2string_subids_absviarel(void)
276 {
277     gchar* oid;
278
279     oid = rel_oid_subid2string(NULL, ex1.subids, ex1.subids_len, TRUE);
280     g_assert_cmpstr(oid, ==, ex1.string);
281     wmem_free(NULL, oid);
282 }
283
284 static void
285 oids_test_2string_subids_relsizes(void)
286 {
287     gchar* oid;
288
289     oid = rel_oid_subid2string(NULL, ex6rel.subids, ex6rel.subids_len, FALSE);
290     g_assert_cmpstr(oid, ==, ex6rel.string);
291     wmem_free(NULL, oid);
292 }
293
294 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
295
296 static void
297 oids_test_2resolved_subids(void)
298 {
299     gchar* oid;
300
301     oid = oid_resolved(NULL, ex1.subids_len, ex1.subids);
302     g_assert_cmpstr(oid, ==, ex1.resolved);
303     wmem_free(NULL, oid);
304 }
305
306 static void
307 oids_test_2resolved_encoded(void)
308 {
309     gchar* oid;
310
311     oid = oid_resolved_from_encoded(NULL, ex1.encoded, ex1.encoded_len);
312     g_assert_cmpstr(oid, ==, ex1.resolved);
313     wmem_free(NULL, oid);
314 }
315
316 static void
317 oids_test_2resolved_encoded_rel(void)
318 {
319     gchar* oid;
320
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);
324 }
325
326 static void
327 oids_test_2resolved_string(void)
328 {
329     gchar* oid;
330
331     oid = oid_resolved_from_string(NULL, ex1.string);
332     g_assert_cmpstr(oid, ==, ex1.resolved);
333     wmem_free(NULL, oid);
334 }
335
336 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
337
338 static void
339 oids_test_2both_subids(void)
340 {
341     gchar* resolved;
342     gchar* oid;
343
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);
349 }
350
351 static void
352 oids_test_2both_encoded(void)
353 {
354     gchar* resolved;
355     gchar* oid;
356
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);
362 }
363
364 static void
365 oids_test_2both_string(void)
366 {
367     gchar* resolved;
368     gchar* oid;
369
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);
375 }
376
377 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
378
379 static void
380 oids_test_2struct_subids(void)
381 {
382     guint matched;
383     guint left;
384     oid_info_t *st;
385
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");
391 }
392
393 static void
394 oids_test_2struct_encoded(void)
395 {
396     guint matched;
397     guint left;
398     guint32 *subids = NULL;
399     oid_info_t *st;
400     guint len, i;
401
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);
412 }
413
414 static void
415 oids_test_2struct_string(void)
416 {
417     guint matched;
418     guint left;
419     guint32 *subids;
420     oid_info_t *st;
421     guint len, i;
422
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]);
432 }
433
434 static void
435 oids_test_add_subids(void)
436 {
437     gchar* oid;
438
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);
443 }
444
445 static void
446 oids_test_add_encoded(void)
447 {
448     gchar* oid;
449
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);
454 }
455
456 static void
457 oids_test_add_string(void)
458 {
459     gchar* oid;
460
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);
465 }
466
467 int
468 main(int argc, char **argv)
469 {
470     int result;
471
472     g_test_init(&argc, &argv, NULL);
473
474     /* /oids/2encoded */
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);
481
482     /* /oids/2subids */
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);
489
490
491     /* /oids/2string */
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);
498
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);
504
505     /* /oids/2both */
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);
509
510     /* /oids/2struct */
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);
514
515     /* /oids/add */
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);
519
520     wmem_init();
521     test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
522     oids_init();
523     result = g_test_run();
524     oids_cleanup();
525     wmem_destroy_allocator(test_scope);
526     wmem_cleanup();
527
528     return result;
529 }
530
531 /*
532  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
533  *
534  * Local variables:
535  * c-basic-offset: 4
536  * tab-width: 8
537  * indent-tabs-mode: nil
538  * End:
539  *
540  * vi: set shiftwidth=4 tabstop=8 expandtab:
541  * :indentSize=4:tabSize=8:noTabs=true:
542  */