Fix warnings in test binaries
[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  * $Id: oids_test.c$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
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.
15  *
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.
20  *
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.
24  */
25
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <glib.h>
31
32 #include "emem.h"
33 #include "oids.h"
34 #include "wmem/wmem.h"
35
36 static wmem_allocator_t *test_scope;
37
38 typedef struct
39 {
40     const gchar *string;
41     const gchar *resolved;
42     guint encoded_len;
43     const gchar *encoded;
44     guint subids_len;
45     guint32 subids[];
46 } example_s;
47
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} };
54
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} };
62
63 /*
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
67  * of oids.h.
68  *
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.
73  *
74  * The types, for the purpose of this naming convention, are
75  * encoded, subids, string and resolved, both, struct.
76  */
77
78 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
79
80 static void
81 oids_test_2subids_encoded(void)
82 {
83     guint32 *subids = NULL;
84     guint len;
85     guint i;
86
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]);
91 }
92
93 static void
94 oids_test_2subids_encoded_long(void)
95 {
96     guint32 *subids = NULL;
97     guint len;
98     guint i;
99
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]);
104 }
105
106 static void
107 oids_test_2subids_encoded_absviasub(void)
108 {
109     guint32 *subids = NULL;
110     guint len;
111     guint i;
112
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]);
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(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 }
131
132 static void
133 oids_test_2subids_string(void)
134 {
135     guint32 *subids = NULL;
136     guint len, i;
137
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]);
142 }
143
144 static void
145 oids_test_2subids_string_tooshort(void)
146 {
147     guint32 *subids = NULL;
148     guint len, i;
149
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]);
154 }
155
156 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
157
158 static void
159 oids_test_2encoded_string_simple(void)
160 {
161     guint8 *encoded = NULL;
162     guint len;
163
164     len = oid_string2encoded(ex1.string, &encoded);
165     g_assert(len == ex1.encoded_len);
166     g_assert(0 == memcmp(encoded, ex1.encoded, len));
167 }
168
169 static void
170 oids_test_2encoded_string_short(void)
171 {
172     guint8 *encoded = NULL;
173     guint len;
174
175     len = oid_string2encoded(ex4.string, &encoded);
176     g_assert(len == ex4.encoded_len);
177     g_assert(0 == memcmp(encoded, ex4.encoded, len));
178 }
179
180 static void
181 oids_test_2encoded_string_long(void)
182 {
183     guint8 *encoded = NULL;
184     guint len;
185
186     len = oid_string2encoded(ex3.string, &encoded);
187     g_assert(len == ex3.encoded_len);
188     g_assert(0 == memcmp(encoded, ex3.encoded, len));
189 }
190
191 static void
192 oids_test_2encoded_string_tooshort(void)
193 {
194     guint8 *encoded = NULL;
195     guint len;
196
197     len = oid_string2encoded(ex5.string, &encoded);
198     g_assert(len == ex5.encoded_len);
199     g_assert(0 == memcmp(encoded, ex5.encoded, len));
200 }
201
202 static void
203 oids_test_2encoded_subids_simple(void)
204 {
205     guint8 *encoded = NULL;
206     guint len;
207
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));
211 }
212
213 static void
214 oids_test_2encoded_subids_bad(void)
215 {
216     guint8 *encoded = NULL;
217     guint len;
218
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));
222 }
223
224 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
225
226 static void
227 oids_test_2string_encoded(void)
228 {
229     const gchar* oid;
230
231     oid = oid_encoded2string(ex3.encoded, ex3.encoded_len);
232     g_assert_cmpstr(oid, ==, ex3.string);
233 }
234
235 static void
236 oids_test_2string_encoded_rel(void)
237 {
238     const gchar* oid;
239
240     oid = rel_oid_encoded2string(ex6rel.encoded, ex3.encoded_len);
241     g_assert_cmpstr(oid, ==, ex6rel.string);
242 }
243
244
245 static void
246 oids_test_2string_subids_abs(void)
247 {
248     const gchar* oid;
249
250     oid = oid_subid2string(ex1.subids, ex1.subids_len);
251     g_assert_cmpstr(oid, ==, ex1.string);
252 }
253
254 static void
255 oids_test_2string_subids_rel(void)
256 {
257     const gchar* oid;
258
259     oid = rel_oid_subid2string(ex2rel.subids, ex2rel.subids_len, FALSE);
260     g_assert_cmpstr(oid, ==, ex2rel.string);
261 }
262
263 static void
264 oids_test_2string_subids_absviarel(void)
265 {
266     const gchar* oid;
267
268     oid = rel_oid_subid2string(ex1.subids, ex1.subids_len, TRUE);
269     g_assert_cmpstr(oid, ==, ex1.string);
270 }
271
272 static void
273 oids_test_2string_subids_relsizes(void)
274 {
275     const gchar* oid;
276
277     oid = rel_oid_subid2string(ex6rel.subids, ex6rel.subids_len, FALSE);
278     g_assert_cmpstr(oid, ==, ex6rel.string);
279 }
280
281 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
282
283 static void
284 oids_test_2resolved_subids(void)
285 {
286     const gchar* oid;
287
288     oid = oid_resolved(ex1.subids_len, ex1.subids);
289     g_assert_cmpstr(oid, ==, ex1.resolved);
290 }
291
292 static void
293 oids_test_2resolved_encoded(void)
294 {
295     const gchar* oid;
296
297     oid = oid_resolved_from_encoded(ex1.encoded, ex1.encoded_len);
298     g_assert_cmpstr(oid, ==, ex1.resolved);
299 }
300
301 static void
302 oids_test_2resolved_encoded_rel(void)
303 {
304     const gchar* oid;
305
306     oid = rel_oid_resolved_from_encoded(ex2rel.encoded, ex2rel.encoded_len);
307     g_assert_cmpstr(oid, ==, ex2rel.string);
308 }
309
310 static void
311 oids_test_2resolved_string(void)
312 {
313     const gchar* oid;
314
315     oid = oid_resolved_from_string(ex1.string);
316     g_assert_cmpstr(oid, ==, ex1.resolved);
317 }
318
319 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
320
321 static void
322 oids_test_2both_subids(void)
323 {
324     const gchar* resolved;
325     const gchar* oid;
326
327     oid_both(ex1.subids_len, ex1.subids, &resolved, &oid);
328     g_assert_cmpstr(resolved, ==, ex1.resolved);
329     g_assert_cmpstr(oid, ==, ex1.string);
330 }
331
332 static void
333 oids_test_2both_encoded(void)
334 {
335     const gchar* resolved;
336     const gchar* oid;
337
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);
341 }
342
343 static void
344 oids_test_2both_string(void)
345 {
346     const gchar* resolved;
347     const gchar* oid;
348
349     oid_both_from_string(ex1.string, &resolved, &oid);
350     g_assert_cmpstr(resolved, ==, ex1.resolved);
351     g_assert_cmpstr(oid, ==, ex1.string);
352 }
353
354 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
355
356 static void
357 oids_test_2struct_subids(void)
358 {
359     guint matched;
360     guint left;
361     oid_info_t *st;
362
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");
368 }
369
370 static void
371 oids_test_2struct_encoded(void)
372 {
373     guint matched;
374     guint left;
375     guint32 *subids;
376     oid_info_t *st;
377     guint len, i;
378
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]);
388 }
389
390 static void
391 oids_test_2struct_string(void)
392 {
393     guint matched;
394     guint left;
395     guint32 *subids;
396     oid_info_t *st;
397     guint len, i;
398
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]);
408 }
409
410 static void
411 oids_test_add_subids(void)
412 {
413     const gchar* oid;
414
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);
418 }
419
420 static void
421 oids_test_add_encoded(void)
422 {
423     const gchar* oid;
424
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);
428 }
429
430 static void
431 oids_test_add_string(void)
432 {
433     const gchar* oid;
434
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);
438 }
439
440 int
441 main(int argc, char **argv)
442 {
443     int result;
444
445     g_test_init(&argc, &argv, NULL);
446
447     /* /oids/2encoded */
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);
454
455     /* /oids/2subids */
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);
462
463
464     /* /oids/2string */
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);
471
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);
477
478     /* /oids/2both */
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);
482
483     /* /oids/2struct */
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);
487
488     /* /oids/add */
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);
492
493     emem_init();
494     wmem_init();
495     test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
496     oids_init();
497     result = g_test_run();
498     oids_cleanup();
499     wmem_destroy_allocator(test_scope);
500     wmem_cleanup();
501     /*
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
504      */
505     return result;
506 }
507
508 /*
509  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
510  *
511  * Local variables:
512  * c-basic-offset: 4
513  * tab-width: 8
514  * indent-tabs-mode: nil
515  * End:
516  *
517  * vi: set shiftwidth=4 tabstop=8 expandtab:
518  * :indentSize=4:tabSize=8:noTabs=true:
519  */