PA-PK-AS-REP-Win2k ::= PaPkAsRep
[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  * SPDX-License-Identifier: GPL-2.0-or-later
10  */
11
12 #include "config.h"
13
14 #include <stdio.h>
15 #include <string.h>
16 #include <glib.h>
17
18 #include "oids.h"
19 #include "wmem/wmem.h"
20
21 static wmem_allocator_t *test_scope;
22
23 typedef struct
24 {
25     const gchar *string;
26     const gchar *resolved;
27     guint encoded_len;
28     const gchar *encoded;
29     guint subids_len;
30     guint32 subids[];
31 } example_s;
32
33 example_s ex1 = {"2.1.1", "joint-iso-itu-t.1.1", 2, "\x51\x01", 3, {2,1,1} };
34 example_s ex2rel = {".81.1", ".81.1", 2, "\x51\x01", 2, {81,1} };
35 example_s ex3 = {"2.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
36     "joint-iso-itu-t.1.127.16383.2097151.268435455.128.16384.2097152.268435456",
37     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",
38     10, { 2, 1, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
39
40 example_s ex4 = {"2.1", "joint-iso-itu-t.1", 1, "\x51", 2, {2,1} };
41 example_s ex5 = {"2", "joint-iso-itu-t", 0, NULL, 1, {2} };
42 example_s ex6rel = {".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
43     ".81.127.16383.2097151.268435455.128.16384.2097152.268435456",
44     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",
45     9, { 81, 0x7F, 0x3FFF, 0x1FFFFF, 0x0FFFFFFF, 1+0x7F, 1+0x3FFF, 1+0x1FFFFF, 1+0x0FFFFFFF} };
46 example_s ex7 = {"2.1.1", "joint-iso-itu-t.asn1.basic-encoding", 2, "\x51\x01", 3, {2,1,1} };
47
48 /*
49  * These test are organized in order of the appearance, in oids.h, of
50  * the basic oids.c functions that they test.  This makes it easier to
51  * get a quick understanding of both the testing and the organization
52  * of oids.h.
53  *
54  * Tests are named /oids/2<desttype>/<srctype>[<extra>]
55  * where <desttype> is the resulting type of the conversion,
56  * <srctype> is the source type and <extra> is any additional
57  * information to make the test name unique.
58  *
59  * The types, for the purpose of this naming convention, are
60  * encoded, subids, string and resolved, both, struct.
61  */
62
63 /* OIDS TESTING FUNCTIONS (/oids/2subids/) */
64
65 static void
66 oids_test_2subids_encoded(void)
67 {
68     guint32 *subids = NULL;
69     guint len;
70     guint i;
71
72     len = oid_encoded2subid(NULL, ex1.encoded, ex1.encoded_len, &subids);
73     g_assert(len == ex1.subids_len);
74     for (i=0; i < len; i++)
75         g_assert(subids[i] == ex1.subids[i]);
76     wmem_free(NULL, subids);
77 }
78
79 static void
80 oids_test_2subids_encoded_long(void)
81 {
82     guint32 *subids = NULL;
83     guint len;
84     guint i;
85
86     len = oid_encoded2subid(NULL, ex3.encoded, ex3.encoded_len, &subids);
87     g_assert(len == ex3.subids_len);
88     for (i=0; i < len; i++)
89         g_assert(subids[i] == ex3.subids[i]);
90     wmem_free(NULL, subids);
91 }
92
93 static void
94 oids_test_2subids_encoded_absviasub(void)
95 {
96     guint32 *subids = NULL;
97     guint len;
98     guint i;
99
100     len = oid_encoded2subid_sub(NULL, ex1.encoded, ex1.encoded_len, &subids, TRUE);
101     g_assert(len == ex1.subids_len);
102     for (i=0; i < len; i++)
103         g_assert(subids[i] == ex1.subids[i]);
104     wmem_free(NULL, subids);
105 }
106
107 static void
108 oids_test_2subids_encoded_relviasub(void)
109 {
110     guint32 *subids = NULL;
111     guint len;
112     guint i;
113
114     len = oid_encoded2subid_sub(NULL, ex2rel.encoded, ex2rel.encoded_len, &subids, FALSE);
115     g_assert(len == ex2rel.subids_len);
116     for (i=0; i < len; i++)
117         g_assert(subids[i] == ex2rel.subids[i]);
118     wmem_free(NULL, subids);
119 }
120
121 static void
122 oids_test_2subids_string(void)
123 {
124     guint32 *subids = NULL;
125     guint len, i;
126
127     len = oid_string2subid(test_scope, ex1.string, &subids);
128     g_assert(len == ex1.subids_len);
129     for (i=0; i < len; i++)
130         g_assert(subids[i] == ex1.subids[i]);
131 }
132
133 static void
134 oids_test_2subids_string_tooshort(void)
135 {
136     guint32 *subids = NULL;
137     guint len, i;
138
139     len = oid_string2subid(test_scope, ex5.string, &subids);
140     g_assert(len == ex5.subids_len);
141     for (i=0; i < len; i++)
142         g_assert(subids[i] == ex5.subids[i]);
143 }
144
145 /* OIDS TESTING FUNCTIONS (/oids/2encoded/) */
146
147 static void
148 oids_test_2encoded_string_simple(void)
149 {
150     guint8 *encoded = NULL;
151     guint len;
152
153     len = oid_string2encoded(NULL, ex1.string, &encoded);
154     g_assert(len == ex1.encoded_len);
155     g_assert(0 == memcmp(encoded, ex1.encoded, len));
156     wmem_free(NULL, encoded);
157 }
158
159 static void
160 oids_test_2encoded_string_short(void)
161 {
162     guint8 *encoded = NULL;
163     guint len;
164
165     len = oid_string2encoded(NULL, ex4.string, &encoded);
166     g_assert(len == ex4.encoded_len);
167     g_assert(0 == memcmp(encoded, ex4.encoded, len));
168     wmem_free(NULL, encoded);
169 }
170
171 static void
172 oids_test_2encoded_string_long(void)
173 {
174     guint8 *encoded = NULL;
175     guint len;
176
177     len = oid_string2encoded(NULL, ex3.string, &encoded);
178     g_assert(len == ex3.encoded_len);
179     g_assert(0 == memcmp(encoded, ex3.encoded, len));
180     wmem_free(NULL, encoded);
181 }
182
183 static void
184 oids_test_2encoded_string_tooshort(void)
185 {
186     guint8 *encoded = NULL;
187     guint len;
188
189     len = oid_string2encoded(NULL, ex5.string, &encoded);
190     g_assert(len == ex5.encoded_len);
191     g_assert(0 == memcmp(encoded, ex5.encoded, len));
192     wmem_free(NULL, encoded);
193 }
194
195 static void
196 oids_test_2encoded_subids_simple(void)
197 {
198     guint8 *encoded = NULL;
199     guint len;
200
201     len = oid_subid2encoded(NULL, ex1.subids_len, ex1.subids, &encoded);
202     g_assert(len == ex1.encoded_len);
203     g_assert(0 == memcmp(encoded, ex1.encoded, len));
204     wmem_free(NULL, encoded);
205 }
206
207 static void
208 oids_test_2encoded_subids_bad(void)
209 {
210     guint8 *encoded = NULL;
211     guint len;
212
213     len = oid_subid2encoded(NULL, ex5.subids_len, ex5.subids, &encoded);
214     g_assert(len == ex5.encoded_len);
215     g_assert(0 == memcmp(encoded, ex5.encoded, len));
216     wmem_free(NULL, encoded);
217 }
218
219 /* OIDS TESTING FUNCTIONS (/oids/2string/) */
220
221 static void
222 oids_test_2string_encoded(void)
223 {
224     gchar* oid;
225
226     oid = oid_encoded2string(NULL, ex3.encoded, ex3.encoded_len);
227     g_assert_cmpstr(oid, ==, ex3.string);
228     wmem_free(NULL, oid);
229 }
230
231 static void
232 oids_test_2string_encoded_rel(void)
233 {
234     gchar* oid;
235
236     oid = rel_oid_encoded2string(NULL, ex6rel.encoded, ex3.encoded_len);
237     g_assert_cmpstr(oid, ==, ex6rel.string);
238     wmem_free(NULL, oid);
239 }
240
241
242 static void
243 oids_test_2string_subids_abs(void)
244 {
245     gchar* oid;
246
247     oid = oid_subid2string(NULL, ex1.subids, ex1.subids_len);
248     g_assert_cmpstr(oid, ==, ex1.string);
249     wmem_free(NULL, oid);
250 }
251
252 static void
253 oids_test_2string_subids_rel(void)
254 {
255     gchar* oid;
256
257     oid = rel_oid_subid2string(NULL, ex2rel.subids, ex2rel.subids_len, FALSE);
258     g_assert_cmpstr(oid, ==, ex2rel.string);
259     wmem_free(NULL, oid);
260 }
261
262 static void
263 oids_test_2string_subids_absviarel(void)
264 {
265     gchar* oid;
266
267     oid = rel_oid_subid2string(NULL, ex1.subids, ex1.subids_len, TRUE);
268     g_assert_cmpstr(oid, ==, ex1.string);
269     wmem_free(NULL, oid);
270 }
271
272 static void
273 oids_test_2string_subids_relsizes(void)
274 {
275     gchar* oid;
276
277     oid = rel_oid_subid2string(NULL, ex6rel.subids, ex6rel.subids_len, FALSE);
278     g_assert_cmpstr(oid, ==, ex6rel.string);
279     wmem_free(NULL, oid);
280 }
281
282 /* OIDS TESTING FUNCTIONS (/oids/2resolved/) */
283
284 static void
285 oids_test_2resolved_subids(void)
286 {
287     gchar* oid;
288
289     oid = oid_resolved(NULL, ex1.subids_len, ex1.subids);
290     g_assert_cmpstr(oid, ==, ex1.resolved);
291     wmem_free(NULL, oid);
292 }
293
294 static void
295 oids_test_2resolved_encoded(void)
296 {
297     gchar* oid;
298
299     oid = oid_resolved_from_encoded(NULL, ex1.encoded, ex1.encoded_len);
300     g_assert_cmpstr(oid, ==, ex1.resolved);
301     wmem_free(NULL, oid);
302 }
303
304 static void
305 oids_test_2resolved_encoded_rel(void)
306 {
307     gchar* oid;
308
309     oid = rel_oid_resolved_from_encoded(NULL, ex2rel.encoded, ex2rel.encoded_len);
310     g_assert_cmpstr(oid, ==, ex2rel.string);
311     wmem_free(NULL, oid);
312 }
313
314 static void
315 oids_test_2resolved_string(void)
316 {
317     gchar* oid;
318
319     oid = oid_resolved_from_string(NULL, ex1.string);
320     g_assert_cmpstr(oid, ==, ex1.resolved);
321     wmem_free(NULL, oid);
322 }
323
324 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
325
326 static void
327 oids_test_2both_subids(void)
328 {
329     gchar* resolved;
330     gchar* oid;
331
332     oid_both(NULL, ex1.subids_len, ex1.subids, &resolved, &oid);
333     g_assert_cmpstr(resolved, ==, ex1.resolved);
334     g_assert_cmpstr(oid, ==, ex1.string);
335     wmem_free(NULL, resolved);
336     wmem_free(NULL, oid);
337 }
338
339 static void
340 oids_test_2both_encoded(void)
341 {
342     gchar* resolved;
343     gchar* oid;
344
345     oid_both_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &resolved, &oid);
346     g_assert_cmpstr(resolved, ==, ex1.resolved);
347     g_assert_cmpstr(oid, ==, ex1.string);
348     wmem_free(NULL, resolved);
349     wmem_free(NULL, oid);
350 }
351
352 static void
353 oids_test_2both_string(void)
354 {
355     gchar* resolved;
356     gchar* oid;
357
358     oid_both_from_string(NULL, ex1.string, &resolved, &oid);
359     g_assert_cmpstr(resolved, ==, ex1.resolved);
360     g_assert_cmpstr(oid, ==, ex1.string);
361     wmem_free(NULL, resolved);
362     wmem_free(NULL, oid);
363 }
364
365 /* OIDS TESTING FUNCTIONS (/oids/2both/) */
366
367 static void
368 oids_test_2struct_subids(void)
369 {
370     guint matched;
371     guint left;
372     oid_info_t *st;
373
374     st = oid_get(ex1.subids_len, ex1.subids, &matched, &left);
375     g_assert(matched == 1);
376     g_assert(left == ex1.subids_len - 1);
377     g_assert(st != NULL);
378     g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
379 }
380
381 static void
382 oids_test_2struct_encoded(void)
383 {
384     guint matched;
385     guint left;
386     guint32 *subids = NULL;
387     oid_info_t *st;
388     guint len, i;
389
390     st = oid_get_from_encoded(NULL, ex1.encoded, ex1.encoded_len, &subids, &matched, &left);
391     g_assert(matched == 1);
392     g_assert(left == ex1.subids_len - 1);
393     g_assert(st != NULL);
394     g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
395     len = matched + left;
396     g_assert(len == ex1.subids_len);
397     for (i=0; i < len; i++)
398         g_assert(subids[i] == ex1.subids[i]);
399     wmem_free(NULL, subids);
400 }
401
402 static void
403 oids_test_2struct_string(void)
404 {
405     guint matched;
406     guint left;
407     guint32 *subids;
408     oid_info_t *st;
409     guint len, i;
410
411     st = oid_get_from_string(test_scope, ex1.string, &subids, &matched, &left);
412     g_assert(matched == 1);
413     g_assert(left == ex1.subids_len - 1);
414     g_assert(st != NULL);
415     g_assert_cmpstr(st->name, ==, "joint-iso-itu-t");
416     len = matched + left;
417     g_assert(len == ex1.subids_len);
418     for (i=0; i < len; i++)
419         g_assert(subids[i] == ex1.subids[i]);
420 }
421
422 static void
423 oids_test_add_subids(void)
424 {
425     gchar* oid;
426
427     oid_add(ex7.resolved, ex7.subids_len, ex7.subids);
428     oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
429     g_assert_cmpstr(oid, ==, ex7.resolved);
430     wmem_free(NULL, oid);
431 }
432
433 static void
434 oids_test_add_encoded(void)
435 {
436     gchar* oid;
437
438     oid_add_from_encoded(ex7.resolved, ex7.encoded, ex7.encoded_len);
439     oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
440     g_assert_cmpstr(oid, ==, ex7.resolved);
441     wmem_free(NULL, oid);
442 }
443
444 static void
445 oids_test_add_string(void)
446 {
447     gchar* oid;
448
449     oid_add_from_string(ex7.resolved, ex7.string);
450     oid = oid_resolved(NULL, ex7.subids_len, ex7.subids);
451     g_assert_cmpstr(oid, ==, ex7.resolved);
452     wmem_free(NULL, oid);
453 }
454
455 int
456 main(int argc, char **argv)
457 {
458     int result;
459
460     g_test_init(&argc, &argv, NULL);
461
462     /* /oids/2encoded */
463     g_test_add_func("/oids/2encoded/subids/simple",   oids_test_2encoded_subids_simple);
464     g_test_add_func("/oids/2encoded/subids/bad",   oids_test_2encoded_subids_bad);
465     g_test_add_func("/oids/2encoded/string/simple",   oids_test_2encoded_string_simple);
466     g_test_add_func("/oids/2encoded/string/short",   oids_test_2encoded_string_short);
467     g_test_add_func("/oids/2encoded/string/long",   oids_test_2encoded_string_long);
468     g_test_add_func("/oids/2encoded/string/tooshort",   oids_test_2encoded_string_tooshort);
469
470     /* /oids/2subids */
471     g_test_add_func("/oids/2subids/string",   oids_test_2subids_string);
472     g_test_add_func("/oids/2subids/string/tooshort",   oids_test_2subids_string_tooshort);
473     g_test_add_func("/oids/2subids/encoded",   oids_test_2subids_encoded);
474     g_test_add_func("/oids/2subids/encoded/long",   oids_test_2subids_encoded_long);
475     g_test_add_func("/oids/2subids/encoded/absviasub",   oids_test_2subids_encoded_absviasub);
476     g_test_add_func("/oids/2subids/encoded/relviasub",   oids_test_2subids_encoded_relviasub);
477
478
479     /* /oids/2string */
480     g_test_add_func("/oids/2string/subids/abs",   oids_test_2string_subids_abs);
481     g_test_add_func("/oids/2string/subids/rel",   oids_test_2string_subids_rel);
482     g_test_add_func("/oids/2string/subids/absviarel",   oids_test_2string_subids_absviarel);
483     g_test_add_func("/oids/2string/subids/relsizes",   oids_test_2string_subids_relsizes);
484     g_test_add_func("/oids/2string/encoded",   oids_test_2string_encoded);
485     g_test_add_func("/oids/2string/encoded/rel",   oids_test_2string_encoded_rel);
486
487     /* /oids/2resolved */
488     g_test_add_func("/oids/2resolved/subids",   oids_test_2resolved_subids);
489     g_test_add_func("/oids/2resolved/encoded",   oids_test_2resolved_encoded);
490     g_test_add_func("/oids/2resolved/encoded/rel",   oids_test_2resolved_encoded_rel);
491     g_test_add_func("/oids/2resolved/string",   oids_test_2resolved_string);
492
493     /* /oids/2both */
494     g_test_add_func("/oids/2both/subids",   oids_test_2both_subids);
495     g_test_add_func("/oids/2both/encoded",   oids_test_2both_encoded);
496     g_test_add_func("/oids/2both/string",   oids_test_2both_string);
497
498     /* /oids/2struct */
499     g_test_add_func("/oids/2struct/subids",   oids_test_2struct_subids);
500     g_test_add_func("/oids/2struct/encoded",   oids_test_2struct_encoded);
501     g_test_add_func("/oids/2struct/string",   oids_test_2struct_string);
502
503     /* /oids/add */
504     g_test_add_func("/oids/add/subids",   oids_test_add_subids);
505     g_test_add_func("/oids/add/encoded",   oids_test_add_encoded);
506     g_test_add_func("/oids/add/string",   oids_test_add_string);
507
508     wmem_init();
509     test_scope = wmem_allocator_new(WMEM_ALLOCATOR_STRICT);
510     oids_init();
511     result = g_test_run();
512     oids_cleanup();
513     wmem_destroy_allocator(test_scope);
514     wmem_cleanup();
515
516     return result;
517 }
518
519 /*
520  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
521  *
522  * Local variables:
523  * c-basic-offset: 4
524  * tab-width: 8
525  * indent-tabs-mode: nil
526  * End:
527  *
528  * vi: set shiftwidth=4 tabstop=8 expandtab:
529  * :indentSize=4:tabSize=8:noTabs=true:
530  */