egg-asn1x: More complete coverage for ASN.1 tests
[platform/upstream/gcr.git] / egg / tests / test-asn1.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-asn1.c: Test ASN1 stuf
3
4    Copyright (C) 2009 Stefan Walter
5
6    The Gnome Keyring Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The Gnome Keyring Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the Gnome Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20
21    Author: Stef Walter <stef@memberwebs.com>
22 */
23
24 #include "config.h"
25
26 #include "egg/egg-asn1x.h"
27 #include "egg/egg-asn1-defs.h"
28 #include "egg/egg-testing.h"
29
30 #include <glib.h>
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 typedef struct _EggAsn1xDef ASN1_ARRAY_TYPE;
37 #include "test.asn.h"
38
39 const gchar I33[] =           "\x02\x01\x2A";
40 const gchar I253[] =           "\x02\x02\x00\xFD";
41 const gchar BFALSE[] =        "\x01\x01\x00";
42 const gchar BTRUE[] =         "\x01\x01\xFF";
43 const gchar SFARNSWORTH[] =   "\x04\x0A""farnsworth";
44 const gchar SIMPLICIT[] =     "\x85\x08""implicit";
45 const gchar SEXPLICIT[] =     "\xA5\x0A\x04\x08""explicit";
46 const gchar SUNIVERSAL[] =    "\x05\x09""universal";
47 const gchar TGENERALIZED[] =  "\x18\x0F""20070725130528Z";
48 const gchar BITS_TEST[] =  "\x03\x04\x06\x6e\x5d\xc0";
49 const gchar BITS_BAD[] =  "\x03\x04\x06\x6e\x5d\xc1";
50 const gchar BITS_ZERO[] =  "\x03\x01\x00";
51 const gchar NULL_TEST[] =  "\x05\x00";
52
53 /* ENUM with value = 2 */
54 const gchar ENUM_TWO[] =           "\x0A\x01\x02";
55
56 /* ENUM with value = 3 */
57 const gchar ENUM_THREE[] =           "\x0A\x01\x03";
58
59 #define XL(x) G_N_ELEMENTS (x) - 1
60
61 static void
62 test_boolean (void)
63 {
64         GBytes *bytes;
65         GNode *asn;
66         gboolean value;
67
68         asn = egg_asn1x_create (test_asn1_tab, "TestBoolean");
69         g_assert (asn);
70
71         g_assert_cmpint (EGG_ASN1X_BOOLEAN, ==, egg_asn1x_type (asn));
72
73         /* Shouldn't succeed */
74         if (egg_asn1x_get_boolean (asn, &value))
75                 g_assert_not_reached ();
76
77         /* Decode a false */
78         bytes = g_bytes_new_static (BFALSE, XL (BFALSE));
79         if (!egg_asn1x_decode (asn, bytes))
80                 g_assert_not_reached ();
81         value = TRUE;
82         if (!egg_asn1x_get_boolean (asn, &value))
83                 g_assert_not_reached ();
84         g_assert (value == FALSE);
85         g_bytes_unref (bytes);
86
87         /* Decode a true */
88         bytes = g_bytes_new_static (BTRUE, XL (BTRUE));
89         if (!egg_asn1x_decode (asn, bytes))
90                 g_assert_not_reached ();
91         value = FALSE;
92         if (!egg_asn1x_get_boolean (asn, &value))
93                 g_assert_not_reached ();
94         g_assert (value == TRUE);
95         g_bytes_unref (bytes);
96
97         egg_asn1x_clear (asn);
98
99         /* Shouldn't suceed after clear */
100         if (egg_asn1x_get_boolean (asn, &value))
101                 g_assert_not_reached ();
102
103         egg_asn1x_destroy (asn);
104 }
105
106 static void
107 test_boolean_decode_bad (void)
108 {
109         const gchar BOOLEAN_INVALID_LENGTH[] =   "\x01\x02\x00\x00";
110         const gchar BOOLEAN_BAD_VALUE[] =        "\x01\x01\x05";
111
112         GBytes *bytes;
113         GNode *asn;
114         gboolean ret;
115
116         asn = egg_asn1x_create (test_asn1_tab, "TestBoolean");
117         g_assert (asn != NULL);
118
119         bytes = g_bytes_new_static (BOOLEAN_INVALID_LENGTH, XL (BOOLEAN_INVALID_LENGTH));
120         ret = egg_asn1x_decode (asn, bytes);
121         g_assert (ret == FALSE);
122         g_assert (strstr (egg_asn1x_message (asn), "invalid length boolean") != NULL);
123         g_bytes_unref (bytes);
124
125         bytes = g_bytes_new_static (BOOLEAN_BAD_VALUE, XL (BOOLEAN_BAD_VALUE));
126         ret = egg_asn1x_decode (asn, bytes);
127         g_assert (ret == FALSE);
128         g_assert (strstr (egg_asn1x_message (asn), "boolean must be true or false") != NULL);
129         g_bytes_unref (bytes);
130
131         egg_asn1x_destroy (asn);
132 }
133
134 static void
135 test_boolean_default (void)
136 {
137         GNode *asn;
138         GBytes *bytes;
139
140         const gchar BOOLEAN[] = "\x30\x00";
141
142         asn = egg_asn1x_create (test_asn1_tab, "TestBooleanDefault");
143         /* This is equal to the default value, and shouldn't be included */
144         egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), TRUE);
145
146         bytes = egg_asn1x_encode (asn, NULL);
147         egg_asn1x_assert (bytes != NULL, asn);
148         egg_assert_cmpbytes (bytes, ==, BOOLEAN, XL (BOOLEAN));
149         g_bytes_unref (bytes);
150
151         egg_asn1x_destroy (asn);
152 }
153
154 static void
155 test_null (void)
156 {
157         GNode *asn;
158         GBytes *data;
159
160         asn = egg_asn1x_create (test_asn1_tab, "TestNull");
161         g_assert (asn);
162
163         g_assert_cmpint (EGG_ASN1X_NULL, ==, egg_asn1x_type (asn));
164
165         egg_asn1x_set_null (asn);
166
167         data = egg_asn1x_encode (asn, g_realloc);
168         egg_assert_cmpmem (NULL_TEST, XL (NULL_TEST), ==, g_bytes_get_data (data, NULL), g_bytes_get_size (data));
169
170         if (!egg_asn1x_decode (asn, data))
171                 g_assert_not_reached ();
172
173         egg_asn1x_destroy (asn);
174         g_bytes_unref (data);
175 }
176
177 static void
178 test_integer (void)
179 {
180         GNode *asn;
181         gulong value;
182         GBytes *bytes;
183
184         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
185         g_assert (asn);
186
187         g_assert_cmpint (EGG_ASN1X_INTEGER, ==, egg_asn1x_type (asn));
188
189         /* Shouldn't succeed */
190         if (egg_asn1x_get_integer_as_ulong (asn, &value))
191                 g_assert_not_reached ();
192
193         /* Should suceed now */
194         bytes = g_bytes_new_static (I33, XL (I33));
195         if (!egg_asn1x_decode (asn, bytes))
196                 g_assert_not_reached ();
197         if (!egg_asn1x_get_integer_as_ulong (asn, &value))
198                 g_assert_not_reached ();
199         g_assert (value == 42);
200         g_bytes_unref (bytes);
201
202         egg_asn1x_clear (asn);
203
204         /* Shouldn't suceed after clear */
205         if (egg_asn1x_get_integer_as_ulong (asn, &value))
206                 g_assert_not_reached ();
207
208         egg_asn1x_destroy (asn);
209 }
210
211 static void
212 test_integer_zero_length (void)
213 {
214         const gchar INTEGER_EMPTY[] =   "\x02\x00";
215
216         GBytes *bytes;
217         GNode *asn;
218         gboolean ret;
219
220         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
221         g_assert (asn != NULL);
222
223         bytes = g_bytes_new_static (INTEGER_EMPTY, XL (INTEGER_EMPTY));
224         ret = egg_asn1x_decode (asn, bytes);
225         g_assert (ret == FALSE);
226         g_assert (strstr (egg_asn1x_message (asn), "zero length integer") != NULL);
227         g_bytes_unref (bytes);
228
229         egg_asn1x_destroy (asn);
230 }
231
232 static void
233 test_unsigned (void)
234 {
235         GNode *asn;
236         gulong value;
237         GBytes *check;
238         guchar val;
239         GBytes *bytes;
240         GBytes *usg;
241
242         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
243         g_assert (asn);
244
245         g_assert_cmpint (EGG_ASN1X_INTEGER, ==, egg_asn1x_type (asn));
246
247         /* Check with ulong */
248         bytes = g_bytes_new_static (I253, XL (I253));
249         if (!egg_asn1x_decode (asn, bytes))
250                 g_assert_not_reached ();
251         if (!egg_asn1x_get_integer_as_ulong (asn, &value))
252                 g_assert_not_reached ();
253         g_assert (value == 253);
254         g_bytes_unref (bytes);
255
256         egg_asn1x_clear (asn);
257
258         egg_asn1x_set_integer_as_ulong (asn, 253);
259
260         check = egg_asn1x_encode (asn, NULL);
261         egg_assert_cmpmem (I253, XL (I253), ==, g_bytes_get_data (check, NULL), g_bytes_get_size (check));
262         g_bytes_unref (check);
263
264         /* Now check with usg */
265         bytes = g_bytes_new_static (I253, XL (I253));
266         if (!egg_asn1x_decode (asn, bytes))
267                 g_assert_not_reached ();
268         g_bytes_unref (bytes);
269
270         val = 0xFD; /* == 253 */
271         usg = egg_asn1x_get_integer_as_usg (asn);
272         egg_assert_cmpmem (&val, 1, ==, g_bytes_get_data (usg, NULL), g_bytes_get_size (usg));
273         g_bytes_unref (usg);
274
275         egg_asn1x_clear (asn);
276
277         egg_asn1x_take_integer_as_usg (asn, g_bytes_new_static (&val, 1));
278
279         check = egg_asn1x_encode (asn, NULL);
280         egg_assert_cmpsize (g_bytes_get_size (check), ==, XL (I253));
281         egg_assert_cmpmem (I253, XL (I253), ==, g_bytes_get_data (check, NULL), g_bytes_get_size (check));
282         g_bytes_unref (check);
283
284         egg_asn1x_destroy (asn);
285 }
286
287 static void
288 test_unsigned_not_set (void)
289 {
290         GNode *asn;
291         GBytes *bytes;
292
293         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
294         g_assert (asn);
295
296         bytes = egg_asn1x_get_integer_as_usg (asn);
297         g_assert (bytes == NULL);
298
299         egg_asn1x_destroy (asn);
300 }
301
302 static void
303 test_unsigned_default (void)
304 {
305         GNode *asn;
306         GBytes *bytes;
307
308         const gchar INTEGERS[] = "\x30\x06\x02\x01\x01\x02\x01\x02";
309
310         asn = egg_asn1x_create (test_asn1_tab, "TestIntegers");
311         egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint1", NULL), 1);
312         egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint2", NULL), 2);
313         /* This is equal to the default value, and shouldn't be included */
314         egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint3", NULL), 8888);
315
316         bytes = egg_asn1x_encode (asn, NULL);
317         egg_assert_cmpbytes (bytes, ==, INTEGERS, XL (INTEGERS));
318         g_bytes_unref (bytes);
319
320         egg_asn1x_destroy (asn);
321 }
322
323 static void
324 test_unsigned_constant (void)
325 {
326         gulong value;
327         GNode *asn;
328
329         /* const gchar SEQ[] = "\x30\x00"; */
330
331         asn = egg_asn1x_create (test_asn1_tab, "TestConstant");
332         if (!egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "version", NULL), &value))
333                 g_assert_not_reached ();
334         g_assert_cmpint (value, ==, 3);
335
336         egg_asn1x_destroy (asn);
337 }
338
339 static void
340 test_unsigned_zero (void)
341 {
342         GBytes *bytes;
343         GNode *asn;
344
345         const gchar DER[] = "\x02\x01\x00";
346
347         /* No bits set in 0 but should still be 1 byte */
348         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
349         egg_asn1x_set_integer_as_ulong (asn, 0);
350
351         bytes = egg_asn1x_encode (asn, NULL);
352         egg_asn1x_assert (bytes != NULL, asn);
353         egg_assert_cmpbytes (bytes, ==, DER, XL (DER));
354         g_bytes_unref (bytes);
355
356         egg_asn1x_destroy (asn);
357 }
358
359 static void
360 test_integer_raw (void)
361 {
362         GNode *asn;
363         GBytes *bytes;
364
365         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
366         g_assert (asn != NULL);
367
368         bytes = g_bytes_new_static ("\x01\x02\x03", 3);
369         egg_asn1x_set_integer_as_raw (asn, bytes);
370         g_bytes_unref (bytes);
371
372         bytes = egg_asn1x_encode (asn, NULL);
373         egg_assert_cmpbytes (bytes, ==, "\x02\x03\x01\x02\x03", 5);
374         g_bytes_unref (bytes);
375
376         bytes = egg_asn1x_get_integer_as_raw (asn);
377         egg_assert_cmpbytes (bytes, ==, "\x01\x02\x03", 3);
378         g_bytes_unref (bytes);
379
380         egg_asn1x_destroy (asn);
381 }
382
383 static void
384 test_integer_raw_not_twos_complement (void)
385 {
386         GNode *asn;
387         GBytes *bytes;
388
389         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
390         g_assert (asn != NULL);
391
392         bytes = g_bytes_new_static ("\x81\x02\x03", 3);
393
394         if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
395                 egg_asn1x_set_integer_as_raw (asn, bytes); /* UNREACHABLE: */
396                 exit(0); /* UNREACHABLE: for code coverage */
397         }
398
399         g_test_trap_assert_failed ();
400         g_test_trap_assert_stderr ("*not two's complement*");
401
402         g_bytes_unref (bytes);
403         egg_asn1x_destroy (asn);
404 }
405
406 static void
407 test_octet_string (void)
408 {
409         GNode *asn;
410         gchar *value;
411         GBytes *bytes;
412
413         asn = egg_asn1x_create (test_asn1_tab, "TestOctetString");
414         g_assert (asn);
415
416         g_assert_cmpint (EGG_ASN1X_OCTET_STRING, ==, egg_asn1x_type (asn));
417
418         /* Shouldn't succeed */
419         if (egg_asn1x_get_string_as_utf8 (asn, NULL))
420                 g_assert_not_reached ();
421
422         /* Should work */
423         bytes = g_bytes_new_static (SFARNSWORTH, XL (SFARNSWORTH));
424         if (!egg_asn1x_decode (asn, bytes))
425                 g_assert_not_reached ();
426         g_bytes_unref (bytes);
427
428         value = egg_asn1x_get_string_as_utf8 (asn, NULL);
429         g_assert_cmpstr (value, ==, "farnsworth");
430         g_free (value);
431
432         egg_asn1x_clear (asn);
433
434         /* Shouldn't succeed */
435         if (egg_asn1x_get_string_as_utf8 (asn, NULL))
436                 g_assert_not_reached ();
437
438         egg_asn1x_destroy (asn);
439 }
440
441 static void
442 test_octet_string_set_bad_utf8 (void)
443 {
444         GNode *asn;
445
446         asn = egg_asn1x_create (test_asn1_tab, "TestOctetString");
447         g_assert (asn);
448
449         if (egg_asn1x_set_string_as_utf8 (asn, "\xFF\xFA", NULL))
450                 g_assert_not_reached ();
451
452         /* Shouldn't succeed */
453         if (egg_asn1x_get_string_as_utf8 (asn, NULL))
454                 g_assert_not_reached ();
455
456         egg_asn1x_destroy (asn);
457 }
458
459 static void
460 test_octet_string_bmp_as_utf8 (void)
461 {
462         GBytes *bytes;
463         GNode *asn;
464         gchar *data;
465
466         const gchar SFUER[] =   "\x04\x06""\x00\x46\x00\xfc\x00\x72";
467
468         bytes = g_bytes_new_static (SFUER, XL (SFUER));
469         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestOctetString", bytes);
470         g_assert (asn != NULL);
471         g_bytes_unref (bytes);
472
473         data = egg_asn1x_get_bmpstring_as_utf8 (asn);
474         g_assert_cmpstr (data, ==, "F\303\274r");
475
476         g_free (data);
477         egg_asn1x_destroy (asn);
478 }
479
480 static void
481 test_octet_string_get_as_bytes (void)
482 {
483         GBytes *bytes;
484         GNode *asn;
485
486         bytes = g_bytes_new_static (SFARNSWORTH, XL (SFARNSWORTH));
487         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestOctetString", bytes);
488         g_assert (asn != NULL);
489         g_bytes_unref (bytes);
490
491         bytes = egg_asn1x_get_string_as_bytes (asn);
492         g_assert (bytes != NULL);
493         egg_assert_cmpbytes (bytes, ==, "farnsworth", 10);
494         g_bytes_unref (bytes);
495
496         egg_asn1x_destroy (asn);
497 }
498
499 static void
500 test_octet_string_set_as_bytes (void)
501 {
502         GBytes *bytes;
503         GNode *asn;
504
505         asn = egg_asn1x_create (test_asn1_tab, "TestOctetString");
506         g_assert (asn != NULL);
507
508         bytes = g_bytes_new_static ("farnsworth", 10);
509         egg_asn1x_set_string_as_bytes (asn, bytes);
510         g_bytes_unref (bytes);
511
512         bytes = egg_asn1x_encode (asn, NULL);
513         g_assert (bytes != NULL);
514         egg_assert_cmpbytes (bytes, ==, SFARNSWORTH, XL (SFARNSWORTH));
515         g_bytes_unref (bytes);
516
517         egg_asn1x_destroy (asn);
518 }
519
520 static void
521 test_octet_string_structured (void)
522 {
523         GBytes *bytes;
524         GNode *asn;
525         guchar *string;
526         gsize n_string = 0;
527
528         const gchar STRUCTURED[] = "\x24\x0c"
529                                        "\x04\x04""blah"
530                                        "\x04\x04""blah";
531
532         bytes = g_bytes_new_static (STRUCTURED, XL (STRUCTURED));
533         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestOctetString", bytes);
534         g_bytes_unref (bytes);
535
536         string = egg_asn1x_get_string_as_raw (asn, NULL, &n_string);
537         g_assert_cmpstr ((gchar *)string, ==, "blahblah");
538         g_assert_cmpint (n_string, ==, 8);
539         g_free (string);
540
541         egg_asn1x_destroy (asn);
542 }
543
544 static void
545 test_octet_string_structured_bad (void)
546 {
547         GBytes *bytes;
548         GNode *asn;
549         guchar *string;
550         gsize n_string = 0;
551
552         const gchar STRUCTURED[] = "\x24\x0c"
553                                        "\x24\x04\x04\02""bl"
554                                        "\x04\x04""blah";
555
556         bytes = g_bytes_new_static (STRUCTURED, XL (STRUCTURED));
557         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestOctetString", bytes);
558         g_bytes_unref (bytes);
559
560         string = egg_asn1x_get_string_as_raw (asn, NULL, &n_string);
561         g_assert (string == NULL);
562
563         egg_asn1x_destroy (asn);
564 }
565
566 static void
567 test_generalized_time (void)
568 {
569         GBytes *bytes;
570         GNode *asn;
571         glong value;
572
573         asn = egg_asn1x_create (test_asn1_tab, "TestGeneralized");
574         g_assert (asn);
575
576         g_assert_cmpint (EGG_ASN1X_TIME, ==, egg_asn1x_type (asn));
577
578         /* Shouldn't succeed */
579         value = egg_asn1x_get_time_as_long (asn);
580         g_assert (value == -1);
581
582         /* Should work */
583         bytes = g_bytes_new_static (TGENERALIZED, XL (TGENERALIZED));
584         if (!egg_asn1x_decode (asn, bytes))
585                 g_assert_not_reached ();
586         g_bytes_unref (bytes);
587         value = egg_asn1x_get_time_as_long (asn);
588         g_assert (value == 1185368728);
589
590         egg_asn1x_clear (asn);
591
592         /* Shouldn't succeed */
593         value = egg_asn1x_get_time_as_long (asn);
594         g_assert (value == -1);
595
596         egg_asn1x_destroy (asn);
597 }
598
599 static void
600 test_time_get_missing (void)
601 {
602         GDate date;
603         GNode *asn;
604
605         asn = egg_asn1x_create (test_asn1_tab, "TestGeneralized");
606         if (egg_asn1x_get_time_as_date (asn, &date))
607                 g_assert_not_reached ();
608         g_assert (egg_asn1x_get_time_as_long (asn) == -1);
609         egg_asn1x_destroy (asn);
610 }
611
612 static void
613 test_implicit_encode (void)
614 {
615         GBytes *bytes;
616         GNode *asn;
617         gchar *value;
618
619         asn = egg_asn1x_create (test_asn1_tab, "TestImplicit");
620         g_assert (asn);
621
622         /* Should work */
623         bytes = g_bytes_new_static (SIMPLICIT, XL (SIMPLICIT));
624         if (!egg_asn1x_decode (asn, bytes))
625                 g_assert_not_reached ();
626         g_bytes_unref (bytes);
627         value = egg_asn1x_get_string_as_utf8 (asn, NULL);
628         g_assert_cmpstr (value, ==, "implicit");
629         g_free (value);
630
631         egg_asn1x_destroy (asn);
632 }
633
634 static void
635 test_implicit_decode (void)
636 {
637         GBytes *bytes;
638         GNode *asn;
639
640         asn = egg_asn1x_create (test_asn1_tab, "TestImplicit");
641         g_assert (asn);
642
643         if (!egg_asn1x_set_string_as_utf8 (asn, g_strdup ("implicit"), g_free))
644                 g_assert_not_reached ();
645
646         bytes = egg_asn1x_encode (asn, NULL);
647         egg_assert_cmpbytes (bytes, ==, SIMPLICIT, XL (SIMPLICIT));
648
649         egg_asn1x_destroy (asn);
650         g_bytes_unref (bytes);
651 }
652
653 static void
654 test_explicit_decode (void)
655 {
656         GBytes *bytes;
657         GNode *asn;
658         gchar *value;
659
660         asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
661         g_assert (asn);
662
663         /* Should work */
664         bytes = g_bytes_new_static (SEXPLICIT, XL (SEXPLICIT));
665         if (!egg_asn1x_decode (asn, bytes))
666                 g_assert_not_reached ();
667         g_bytes_unref (bytes);
668
669         value = egg_asn1x_get_string_as_utf8 (asn, NULL);
670         g_assert_cmpstr (value, ==, "explicit");
671         g_free (value);
672
673         egg_asn1x_destroy (asn);
674 }
675
676 static void
677 test_explicit_no_context_specific (void)
678 {
679         GBytes *bytes;
680         GNode *asn;
681
682         const gchar DER[] =     "\x45\x0A\x04\x08""explicit";
683
684         asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
685         g_assert (asn != NULL);
686
687         bytes = g_bytes_new_static (DER, XL (DER));
688         if (egg_asn1x_decode (asn, bytes))
689                 g_assert_not_reached ();
690         g_assert (strstr (egg_asn1x_message (asn), "missing context specific tag"));
691         g_bytes_unref (bytes);
692
693         egg_asn1x_destroy (asn);
694 }
695
696 static void
697 test_explicit_no_context_child (void)
698 {
699         GBytes *bytes;
700         GNode *asn;
701
702         const gchar DER[] =     "\xA5\x00";
703
704         asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
705         g_assert (asn != NULL);
706
707         bytes = g_bytes_new_static (DER, XL (DER));
708         if (egg_asn1x_decode (asn, bytes))
709                 g_assert_not_reached ();
710         g_assert (strstr (egg_asn1x_message (asn), "missing context specific child"));
711         g_bytes_unref (bytes);
712
713         egg_asn1x_destroy (asn);
714 }
715
716 static void
717 test_explicit_extra_context_child (void)
718 {
719         GBytes *bytes;
720         GNode *asn;
721
722         const gchar DER[] =     "\xA5\x14"
723                                        "\x04\x08""explicit"
724                                        "\x04\x08""explicit";
725
726         asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
727         g_assert (asn != NULL);
728
729         bytes = g_bytes_new_static (DER, XL (DER));
730         if (egg_asn1x_decode (asn, bytes))
731                 g_assert_not_reached ();
732         g_assert (strstr (egg_asn1x_message (asn), "multiple context specific children"));
733         g_bytes_unref (bytes);
734
735         egg_asn1x_destroy (asn);
736 }
737
738 static void
739 test_explicit_encode (void)
740 {
741         GBytes *bytes;
742         GNode *asn;
743
744         asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
745         g_assert (asn);
746
747         if (!egg_asn1x_set_string_as_utf8 (asn, g_strdup ("explicit"), g_free))
748                 g_assert_not_reached ();
749
750         bytes = egg_asn1x_encode (asn, NULL);
751         egg_assert_cmpbytes (bytes, ==, SEXPLICIT, XL (SEXPLICIT));
752
753         egg_asn1x_destroy (asn);
754         g_bytes_unref (bytes);
755 }
756
757 static void
758 test_universal_decode (void)
759 {
760         GBytes *bytes;
761         GNode *asn;
762         gchar *value;
763
764         asn = egg_asn1x_create (test_asn1_tab, "TestUniversal");
765         g_assert (asn);
766
767         /* Should work */
768         bytes = g_bytes_new_static (SUNIVERSAL, XL (SUNIVERSAL));
769         if (!egg_asn1x_decode (asn, bytes))
770                 g_assert_not_reached ();
771         g_bytes_unref (bytes);
772
773         value = egg_asn1x_get_string_as_utf8 (asn, NULL);
774         g_assert_cmpstr (value, ==, "universal");
775         g_free (value);
776
777         egg_asn1x_destroy (asn);
778 }
779
780 static void
781 test_universal_encode (void)
782 {
783         GBytes *bytes;
784         GNode *asn;
785
786         asn = egg_asn1x_create (test_asn1_tab, "TestUniversal");
787         g_assert (asn);
788
789         if (!egg_asn1x_set_string_as_utf8 (asn, g_strdup ("universal"), g_free))
790                 g_assert_not_reached ();
791
792         bytes = egg_asn1x_encode (asn, NULL);
793         egg_assert_cmpbytes (bytes, ==, SUNIVERSAL, XL (SUNIVERSAL));
794
795         egg_asn1x_destroy (asn);
796         g_bytes_unref (bytes);
797 }
798
799 static void
800 test_bit_string_decode (void)
801 {
802         GBytes *bytes;
803         GNode *asn;
804         GBytes *bits;
805         guint n_bits;
806         const guchar *data;
807
808         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
809         g_assert (asn);
810
811         g_assert_cmpint (EGG_ASN1X_BIT_STRING, ==, egg_asn1x_type (asn));
812
813         /* Should work */
814         bytes = g_bytes_new_static (BITS_TEST, XL (BITS_TEST));
815         if (!egg_asn1x_decode (asn, bytes))
816                 g_assert_not_reached ();
817         g_bytes_unref (bytes);
818
819         bits = egg_asn1x_get_bits_as_raw (asn, &n_bits);
820         g_assert (bits != NULL);
821         g_assert_cmpuint (n_bits, ==, 18);
822         data = g_bytes_get_data (bits, NULL);
823         g_assert_cmpint (data[0], ==, 0x6e);
824         g_assert_cmpint (data[1], ==, 0x5d);
825         g_assert_cmpint (data[2], ==, 0xc0);
826
827         g_bytes_unref (bits);
828         egg_asn1x_destroy (asn);
829 }
830
831 static void
832 test_bit_string_decode_bad (void)
833 {
834         GBytes *bytes;
835         GNode *asn;
836
837         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
838         g_assert (asn);
839
840         /* Should not work */
841         bytes = g_bytes_new_static (BITS_BAD, XL (BITS_BAD));
842         if (egg_asn1x_decode (asn, bytes))
843                 g_assert_not_reached ();
844         g_bytes_unref (bytes);
845
846         egg_asn1x_destroy (asn);
847 }
848
849 static void
850 test_bit_string_decode_ulong (void)
851 {
852         GBytes *bytes;
853         GNode *asn;
854         gulong bits;
855         guint n_bits;
856
857         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
858         g_assert (asn);
859
860         /* Should work */
861         bytes = g_bytes_new_static (BITS_TEST, XL (BITS_TEST));
862         if (!egg_asn1x_decode (asn, bytes))
863                 g_assert_not_reached ();
864         g_bytes_unref (bytes);
865
866         if (!egg_asn1x_get_bits_as_ulong (asn, &bits, &n_bits))
867                 g_assert_not_reached ();
868
869         g_assert_cmpuint (n_bits, ==, 18);
870         g_assert_cmphex (bits, ==, 0x1b977);
871
872         egg_asn1x_destroy (asn);
873 }
874
875 static void
876 test_bit_string_ulong_too_long (void)
877 {
878         GBytes *bytes;
879         GNode *asn;
880         gulong bits;
881         guint n_bits;
882
883         const gchar BITS_TEST[] =  "\x03\x20\x00\x01\x02\x03\x04\x05\x06\x07"
884                                            "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
885                                            "\x00\x01\x02\x03\x04\x05\x06\x07"
886                                            "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
887
888         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
889         g_assert (asn);
890
891         /* Should work */
892
893         bytes = g_bytes_new_static (BITS_TEST, XL (BITS_TEST));
894         if (!egg_asn1x_decode (asn, bytes))
895                 g_assert_not_reached ();
896         g_bytes_unref (bytes);
897
898         if (egg_asn1x_get_bits_as_ulong (asn, &bits, &n_bits))
899                 g_assert_not_reached ();
900
901         egg_asn1x_destroy (asn);
902 }
903
904 static void
905 test_bit_string_get_not_set (void)
906 {
907         GNode *asn;
908         gulong bits;
909         guint n_bits;
910
911         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
912
913         if (egg_asn1x_get_bits_as_ulong (asn, &bits, &n_bits))
914                 g_assert_not_reached ();
915         g_assert (egg_asn1x_get_bits_as_raw (asn, &n_bits) == NULL);
916
917         egg_asn1x_destroy (asn);
918 }
919
920 static void
921 test_bit_string_invalid_length (void)
922 {
923         GBytes *bytes;
924         GNode *asn;
925
926         const gchar DER[] =  "\x03\x00";
927
928         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
929         g_assert (asn);
930
931         /* Should work */
932
933         bytes = g_bytes_new_static (DER, XL (DER));
934         if (egg_asn1x_decode (asn, bytes))
935                 g_assert_not_reached ();
936         g_assert (strstr (egg_asn1x_message (asn), "invalid length bit string"));
937         g_bytes_unref (bytes);
938
939         egg_asn1x_destroy (asn);
940 }
941
942 static void
943 test_bit_string_invalid_empty (void)
944 {
945         GBytes *bytes;
946         GNode *asn;
947
948         const gchar DER[] =  "\x03\x01\x09";
949
950         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
951         g_assert (asn);
952
953         /* Should work */
954
955         bytes = g_bytes_new_static (DER, XL (DER));
956         if (egg_asn1x_decode (asn, bytes))
957                 g_assert_not_reached ();
958         g_assert (strstr (egg_asn1x_message (asn), "invalid number of empty bits"));
959         g_bytes_unref (bytes);
960
961         egg_asn1x_destroy (asn);
962 }
963
964 static void
965 test_bit_string_encode_decode (void)
966 {
967         GBytes *data;
968         GNode *asn;
969         guchar bits[] = { 0x5d, 0x6e, 0x83 };
970         GBytes *check;
971         GBytes *bytes;
972         const guchar *ch;
973         guint n_bits = 17;
974         guint n_check;
975
976         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
977         g_assert (asn);
978
979         bytes = g_bytes_new (bits, 3);
980         egg_asn1x_set_bits_as_raw (asn, bytes, n_bits);
981         g_bytes_unref (bytes);
982
983         data = egg_asn1x_encode (asn, NULL);
984         g_assert (data);
985
986         if (!egg_asn1x_decode (asn, data))
987                 g_assert_not_reached ();
988
989         g_bytes_unref (data);
990
991         check = egg_asn1x_get_bits_as_raw (asn, &n_check);
992         g_assert (check != NULL);
993         g_assert_cmpuint (n_check, ==, 17);
994         ch = g_bytes_get_data (check, NULL);
995         g_assert_cmpint (ch[0], ==, 0x5d);
996         g_assert_cmpint (ch[1], ==, 0x6e);
997         g_assert_cmpint (ch[2], ==, 0x80);
998
999         g_bytes_unref (check);
1000         egg_asn1x_destroy (asn);
1001 }
1002
1003 static void
1004 test_bit_string_encode_decode_ulong (void)
1005 {
1006         GBytes *data;
1007         GNode *asn;
1008         gulong check, bits = 0x0101b977;
1009         guint n_check, n_bits = 18;
1010
1011         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
1012         g_assert (asn);
1013
1014         egg_asn1x_set_bits_as_ulong (asn, bits, n_bits);
1015         data = egg_asn1x_encode (asn, NULL);
1016         g_assert (data);
1017
1018         if (!egg_asn1x_decode (asn, data))
1019                 g_assert_not_reached ();
1020
1021         g_bytes_unref (data);
1022
1023         if (!egg_asn1x_get_bits_as_ulong (asn, &check, &n_check))
1024                 g_assert_not_reached ();
1025
1026         g_assert_cmpuint (n_check, ==, 18);
1027         g_assert_cmphex (check, ==, 0x1b977);
1028
1029         egg_asn1x_destroy (asn);
1030 }
1031
1032 static void
1033 test_bit_string_encode_decode_zero (void)
1034 {
1035         GBytes *data;
1036         GNode *asn;
1037
1038         asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
1039         g_assert (asn);
1040
1041         egg_asn1x_take_bits_as_raw (asn, g_bytes_new_static ("", 0), 0);
1042
1043         data = egg_asn1x_encode (asn, NULL);
1044         g_assert (data);
1045
1046         egg_assert_cmpmem (g_bytes_get_data (data, NULL), g_bytes_get_size (data), ==, BITS_ZERO, XL (BITS_ZERO));
1047
1048         g_bytes_unref (data);
1049         egg_asn1x_destroy (asn);
1050 }
1051
1052 static void
1053 test_have (void)
1054 {
1055         GBytes *data;
1056         GNode *asn;
1057
1058         asn = egg_asn1x_create (test_asn1_tab, "TestBoolean");
1059         g_assert (asn);
1060
1061         g_assert (!egg_asn1x_have (asn));
1062
1063         egg_asn1x_set_boolean (asn, TRUE);
1064
1065         g_assert (egg_asn1x_have (asn));
1066
1067         data = egg_asn1x_encode (asn, NULL);
1068         g_assert (data);
1069
1070         g_assert (egg_asn1x_have (asn));
1071
1072         g_bytes_unref (data);
1073         egg_asn1x_destroy (asn);
1074 }
1075
1076 static gboolean is_freed = FALSE;
1077
1078 static void
1079 test_is_freed (gpointer unused)
1080 {
1081         g_assert (!is_freed);
1082         is_freed = TRUE;
1083 }
1084
1085 static void
1086 test_any_raw (void)
1087 {
1088         GBytes *bytes;
1089         GNode *asn, *node;
1090         GBytes *data;
1091         GBytes *check;
1092
1093         /* ENCODED SEQUENCE ANY with OCTET STRING */
1094         const gchar SEQ_ENCODING[] =  "\x30\x0C\x04\x0A""farnsworth";
1095
1096         asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1097         g_assert (asn);
1098
1099         is_freed = FALSE;
1100         node = egg_asn1x_node (asn, "contents", NULL);
1101         g_assert (node);
1102
1103         bytes = g_bytes_new_with_free_func (SFARNSWORTH, XL (SFARNSWORTH),
1104                                               test_is_freed, NULL);
1105         if (!egg_asn1x_set_any_raw (node, bytes))
1106                 g_assert_not_reached ();
1107         g_bytes_unref (bytes);
1108
1109         data = egg_asn1x_encode (asn, NULL);
1110         g_assert (data != NULL);
1111
1112         egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
1113
1114         check = egg_asn1x_get_element_raw (node);
1115         g_assert (check != NULL);
1116         egg_assert_cmpbytes (check, ==, SFARNSWORTH, XL (SFARNSWORTH));
1117         g_bytes_unref (check);
1118
1119         check = egg_asn1x_get_any_raw (node, NULL);
1120         g_assert (check != NULL);
1121         egg_assert_cmpbytes (check, ==, SFARNSWORTH, XL (SFARNSWORTH));
1122         g_bytes_unref (check);
1123
1124         g_bytes_unref (data);
1125         egg_asn1x_destroy (asn);
1126         g_assert (is_freed);
1127 }
1128
1129 static void
1130 test_any_raw_explicit (void)
1131 {
1132         GBytes *bytes;
1133         GNode *asn, *node;
1134         GBytes *data;
1135
1136         /* ENCODED SEQUENCE [89] ANY with OCTET STRING */
1137         const gchar SEQ_ENCODING[] =  "\x30\x0F\xBF\x59\x0C\x04\x0A""farnsworth";
1138
1139         asn = egg_asn1x_create (test_asn1_tab, "TestAnyExp");
1140         g_assert (asn);
1141
1142         is_freed = FALSE;
1143         node = egg_asn1x_node (asn, "contents", NULL);
1144         g_assert (node);
1145
1146         bytes = g_bytes_new_with_free_func (SFARNSWORTH, XL (SFARNSWORTH), test_is_freed, NULL);
1147         if (!egg_asn1x_set_any_raw (node, bytes))
1148                 g_assert_not_reached ();
1149         g_bytes_unref (bytes);
1150
1151         data = egg_asn1x_encode (asn, NULL);
1152         g_assert (data != NULL);
1153
1154         egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
1155
1156         g_bytes_unref (data);
1157         egg_asn1x_destroy (asn);
1158         g_assert (is_freed);
1159 }
1160
1161 static void
1162 test_any_raw_invalid (void)
1163 {
1164         GBytes *bytes;
1165         GNode *asn, *node;
1166
1167         const gchar TRUNCATED[] =  "\x04\x0A""farns";
1168
1169         asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1170         g_assert (asn != NULL);
1171
1172         node = egg_asn1x_node (asn, "contents", NULL);
1173         g_assert (node != NULL);
1174
1175         bytes = g_bytes_new_static (TRUNCATED, XL (TRUNCATED));
1176         if (egg_asn1x_set_any_raw (node, bytes))
1177                 g_assert_not_reached ();
1178         g_assert (strstr (egg_asn1x_message (node), "content is not encoded properly") != NULL);
1179         g_bytes_unref (bytes);
1180
1181         egg_asn1x_destroy (asn);
1182 }
1183
1184 static void
1185 test_any_raw_not_set (void)
1186 {
1187         GBytes *check;
1188         GNode *asn, *node;
1189
1190         asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1191         g_assert (asn != NULL);
1192
1193         node = egg_asn1x_node (asn, "contents", NULL);
1194         g_assert (node != NULL);
1195
1196         check = egg_asn1x_get_any_raw (node, NULL);
1197         g_assert (check == NULL);
1198
1199         egg_asn1x_destroy (asn);
1200 }
1201
1202 static void
1203 test_any_into (void)
1204 {
1205         GBytes *bytes;
1206         GNode *asn, *node;
1207         GNode *part;
1208         GBytes *data;
1209         GBytes *check;
1210
1211         /* ENCODED SEQUENCE ANY with OCTET STRING */
1212         const gchar SEQ_ENCODING[] =  "\x30\x0C\x04\x0A""farnsworth";
1213
1214         asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1215         g_assert (asn != NULL);
1216
1217         is_freed = FALSE;
1218         node = egg_asn1x_node (asn, "contents", NULL);
1219         g_assert (node);
1220
1221         bytes = g_bytes_new_with_free_func (SFARNSWORTH, XL (SFARNSWORTH),
1222                                             test_is_freed, NULL);
1223         part = egg_asn1x_create_and_decode (test_asn1_tab, "TestOctetString", bytes);
1224         g_assert (part != NULL);
1225         g_bytes_unref (bytes);
1226
1227         egg_asn1x_set_any_from (node, part);
1228         egg_asn1x_destroy (part);
1229
1230         data = egg_asn1x_encode (asn, NULL);
1231         g_assert (data != NULL);
1232         egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
1233
1234         part = egg_asn1x_create (test_asn1_tab, "TestOctetString");
1235         if (!egg_asn1x_get_any_into (node, part))
1236                 g_assert_not_reached ();
1237
1238         check = egg_asn1x_encode (part, NULL);
1239         egg_asn1x_destroy (part);
1240         g_assert (check != NULL);
1241         egg_assert_cmpbytes (check, ==, SFARNSWORTH, XL (SFARNSWORTH));
1242         g_bytes_unref (check);
1243
1244         g_bytes_unref (data);
1245         egg_asn1x_destroy (asn);
1246         g_assert (is_freed);
1247 }
1248
1249 static void
1250 test_any_into_explicit (void)
1251 {
1252         GBytes *bytes;
1253         GNode *asn, *node;
1254         GNode *part;
1255         GBytes *data;
1256         GBytes *check;
1257
1258         /* ENCODED SEQUENCE [89] ANY with OCTET STRING */
1259         const gchar SEQ_ENCODING[] =  "\x30\x0F\xBF\x59\x0C\x04\x0A""farnsworth";
1260
1261         asn = egg_asn1x_create (test_asn1_tab, "TestAnyExp");
1262         g_assert (asn != NULL);
1263
1264         is_freed = FALSE;
1265         node = egg_asn1x_node (asn, "contents", NULL);
1266         g_assert (node);
1267
1268         bytes = g_bytes_new_with_free_func (SFARNSWORTH, XL (SFARNSWORTH),
1269                                             test_is_freed, NULL);
1270         part = egg_asn1x_create_and_decode (test_asn1_tab, "TestOctetString", bytes);
1271         g_assert (part != NULL);
1272         g_bytes_unref (bytes);
1273
1274         egg_asn1x_set_any_from (node, part);
1275         egg_asn1x_destroy (part);
1276
1277         data = egg_asn1x_encode (asn, NULL);
1278         g_assert (data != NULL);
1279         egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
1280
1281         part = egg_asn1x_create (test_asn1_tab, "TestOctetString");
1282         if (!egg_asn1x_get_any_into (node, part))
1283                 g_assert_not_reached ();
1284
1285         check = egg_asn1x_encode (part, NULL);
1286         egg_asn1x_destroy (part);
1287         g_assert (check != NULL);
1288         egg_assert_cmpbytes (check, ==, SFARNSWORTH, XL (SFARNSWORTH));
1289         g_bytes_unref (check);
1290
1291         g_bytes_unref (data);
1292         egg_asn1x_destroy (asn);
1293         g_assert (is_freed);
1294 }
1295
1296 static void
1297 test_any_into_explicit_not_set (void)
1298 {
1299         GNode *asn, *node;
1300         GNode *part;
1301
1302         asn = egg_asn1x_create (test_asn1_tab, "TestAnyExp");
1303         g_assert (asn != NULL);
1304
1305         node = egg_asn1x_node (asn, "contents", NULL);
1306         g_assert (node);
1307
1308         part = egg_asn1x_create (test_asn1_tab, "TestOctetString");
1309         if (egg_asn1x_get_any_into (node, part))
1310                 g_assert_not_reached ();
1311
1312         egg_asn1x_destroy (part);
1313         egg_asn1x_destroy (asn);
1314 }
1315
1316 static void
1317 test_choice_not_chosen (void)
1318 {
1319         GBytes *bytes;
1320         GNode *asn, *node;
1321         GBytes *data;
1322
1323         asn = egg_asn1x_create (test_asn1_tab, "TestAnyChoice");
1324         g_assert (asn);
1325
1326         g_assert_cmpint (EGG_ASN1X_CHOICE, ==, egg_asn1x_type (asn));
1327
1328         node = egg_asn1x_node (asn, "choiceShortTag", NULL);
1329         g_assert (node);
1330
1331         bytes = g_bytes_new_static (SFARNSWORTH, XL (SFARNSWORTH));
1332         if (!egg_asn1x_set_any_raw (node, bytes))
1333                 g_assert_not_reached ();
1334         g_bytes_unref (bytes);
1335
1336         /* egg_asn1x_set_choice() was not called */
1337         data = egg_asn1x_encode (asn, NULL);
1338         g_assert (data == NULL);
1339         g_assert (egg_asn1x_message (asn));
1340         g_assert (strstr (egg_asn1x_message (asn), "TestAnyChoice") != NULL);
1341
1342         egg_asn1x_destroy (asn);
1343 }
1344
1345 static void
1346 perform_asn1_any_choice_set_raw (const gchar *choice, const gchar *encoding, gsize n_encoding)
1347 {
1348         GBytes *bytes;
1349         GNode *asn, *node;
1350         GBytes *data;
1351         GBytes *check;
1352
1353         asn = egg_asn1x_create (test_asn1_tab, "TestAnyChoice");
1354         g_assert (asn);
1355
1356         g_assert_cmpint (EGG_ASN1X_CHOICE, ==, egg_asn1x_type (asn));
1357
1358         is_freed = FALSE;
1359         node = egg_asn1x_node (asn, choice, NULL);
1360         g_assert (node);
1361
1362         if (!egg_asn1x_set_choice (asn, node))
1363                 g_assert_not_reached ();
1364
1365         bytes = g_bytes_new_with_free_func (SFARNSWORTH, XL (SFARNSWORTH), test_is_freed, NULL);
1366         if (!egg_asn1x_set_any_raw (node, bytes))
1367                 g_assert_not_reached ();
1368         g_bytes_unref (bytes);
1369
1370         data = egg_asn1x_encode (asn, NULL);
1371         if (data == NULL) {
1372                 g_printerr ("%s\n", egg_asn1x_message (asn));
1373                 g_assert_not_reached ();
1374         }
1375         g_assert (data != NULL);
1376
1377         egg_assert_cmpbytes (data, ==, encoding, n_encoding);
1378
1379         check = egg_asn1x_get_element_raw (node);
1380         g_assert (check != NULL);
1381
1382         egg_assert_cmpbytes (check, ==, SFARNSWORTH, XL (SFARNSWORTH));
1383
1384         g_bytes_unref (data);
1385         g_bytes_unref (check);
1386         egg_asn1x_destroy (asn);
1387         g_assert (is_freed);
1388 }
1389
1390 static void
1391 test_any_choice_set_raw_short_tag (void)
1392 {
1393         const gchar ENCODING[] = "\xBE\x0C\x04\x0A""farnsworth";
1394         perform_asn1_any_choice_set_raw ("choiceShortTag", ENCODING, XL (ENCODING));
1395 }
1396
1397 static void
1398 test_any_choice_set_raw_long_tag (void)
1399 {
1400         const gchar ENCODING[] = "\xBF\x1F\x0C\x04\x0A""farnsworth";
1401         perform_asn1_any_choice_set_raw ("choiceLongTag", ENCODING, XL (ENCODING));
1402 }
1403
1404 static void
1405 test_seq_of_any (void)
1406 {
1407         GNode *asn;
1408         GNode *integer;
1409         GBytes *bytes;
1410         gboolean ret;
1411         gulong value;
1412
1413         const gchar DER[] = "\x30\x06"
1414                                 "\x02\x01\x88"
1415                                 "\x02\x01\x33";
1416
1417         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOfAny");
1418         g_assert (asn != NULL);
1419
1420         egg_asn1x_append (asn);
1421         egg_asn1x_append (asn);
1422
1423         bytes = g_bytes_new_static (DER, XL (DER));
1424         ret = egg_asn1x_decode (asn, bytes);
1425         egg_asn1x_assert (ret == TRUE, asn);
1426         g_bytes_unref (bytes);
1427
1428         integer = egg_asn1x_create (test_asn1_tab, "TestInteger");
1429         g_assert (integer != NULL);
1430
1431         ret = egg_asn1x_get_any_into (egg_asn1x_node (asn, 1, NULL), integer);
1432         egg_asn1x_assert (ret == TRUE, integer);
1433         if (!egg_asn1x_get_integer_as_ulong (integer, &value))
1434                 g_assert_not_reached ();
1435         g_assert_cmpint (value, ==, 0x88);
1436
1437         ret = egg_asn1x_get_any_into (egg_asn1x_node (asn, 2, NULL), integer);
1438         egg_asn1x_assert (ret == TRUE, integer);
1439         if (!egg_asn1x_get_integer_as_ulong (integer, &value))
1440                 g_assert_not_reached ();
1441         g_assert_cmpint (value, ==, 0x33);
1442
1443         egg_asn1x_destroy (integer);
1444         egg_asn1x_destroy (asn);
1445 }
1446
1447 static void
1448 test_seq_of_invalid (void)
1449 {
1450         GNode *asn;
1451         GBytes *bytes;
1452
1453         const gchar DER[] = "\x30\x05"
1454                                 "\x04\x00"
1455                                 "\x02\x01\x88";
1456
1457         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
1458         g_assert (asn != NULL);
1459
1460         bytes = g_bytes_new_static (DER, XL (DER));
1461         if (egg_asn1x_decode (asn, bytes))
1462                 g_assert_not_reached ();
1463         g_bytes_unref (bytes);
1464
1465         egg_asn1x_destroy (asn);
1466 }
1467
1468 static void
1469 test_seq_of_different (void)
1470 {
1471         GNode *asn;
1472         GBytes *bytes;
1473
1474         const gchar DER[] = "\x30\x05"
1475                                 "\x02\x01\x88"
1476                                 "\x04\x00";
1477
1478         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
1479         g_assert (asn != NULL);
1480
1481         bytes = g_bytes_new_static (DER, XL (DER));
1482         if (egg_asn1x_decode (asn, bytes))
1483                 g_assert_not_reached ();
1484         g_bytes_unref (bytes);
1485
1486         egg_asn1x_destroy (asn);
1487 }
1488
1489 static void
1490 test_set_order (void)
1491 {
1492         GNode *asn;
1493         GBytes *bytes;
1494
1495         const gchar DER[] = "\x31\x0f"
1496                                 "\xA2\x03\x02\x01\x99"
1497                                 "\xA1\x03\x02\x01\x88"
1498                                 "\xA3\x03\x02\x01\x88";
1499
1500         asn = egg_asn1x_create (test_asn1_tab, "TestSet");
1501         g_assert (asn != NULL);
1502
1503         bytes = g_bytes_new_static (DER, XL (DER));
1504         if (egg_asn1x_decode (asn, bytes))
1505                 g_assert_not_reached ();
1506         g_assert (strstr (egg_asn1x_message (asn), "content must be in ascending order"));
1507         g_bytes_unref (bytes);
1508
1509         egg_asn1x_destroy (asn);
1510 }
1511
1512 static void
1513 test_append (void)
1514 {
1515         GBytes *bytes;
1516         GNode *asn;
1517         GNode *child;
1518         GBytes *data;
1519
1520         /* SEQUENCE OF with one INTEGER = 1 */
1521         const gchar SEQOF_ONE[] =  "\x30\x03\x02\x01\x01";
1522
1523         /* SEQUENCE OF with two INTEGER = 1, 2 */
1524         const gchar SEQOF_TWO[] =  "\x30\x06\x02\x01\x01\x02\x01\x02";
1525
1526         bytes = g_bytes_new_static (SEQOF_ONE, XL (SEQOF_ONE));
1527         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestSeqOf", bytes);
1528         g_assert (asn);
1529         g_bytes_unref (bytes);
1530
1531         g_assert_cmpint (EGG_ASN1X_SEQUENCE_OF, ==, egg_asn1x_type (asn));
1532
1533         child = egg_asn1x_append (asn);
1534         g_assert (child);
1535
1536         /* Second integer is 2 */
1537         egg_asn1x_set_integer_as_ulong (child, 2);
1538
1539         data = egg_asn1x_encode (asn, NULL);
1540         g_assert (data != NULL);
1541
1542         egg_assert_cmpbytes (data, ==, SEQOF_TWO, XL (SEQOF_TWO));
1543
1544         g_bytes_unref (data);
1545         egg_asn1x_destroy (asn);
1546 }
1547
1548 static void
1549 test_append_and_clear (void)
1550 {
1551         GBytes *data;
1552         GNode *asn;
1553
1554         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
1555         g_assert (asn);
1556
1557         g_assert_cmpuint (egg_asn1x_count (asn), ==, 0);
1558
1559         egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 2);
1560         egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 3);
1561
1562         g_assert_cmpuint (egg_asn1x_count (asn), ==, 2);
1563
1564         data = egg_asn1x_encode (asn, NULL);
1565         g_assert (data != NULL);
1566
1567         g_assert_cmpuint (egg_asn1x_count (asn), ==, 2);
1568
1569         egg_asn1x_clear (asn);
1570         g_assert_cmpuint (egg_asn1x_count (asn), ==, 0);
1571
1572         egg_asn1x_destroy (asn);
1573         g_bytes_unref (data);
1574 }
1575
1576 static void
1577 test_setof (void)
1578 {
1579         GBytes *bytes;
1580         GNode *asn;
1581         GBytes *data;
1582
1583         /* SEQUENCE OF with one INTEGER = 3 */
1584         const gchar SETOF_ONE[] =  "\x31\x03\x02\x01\x03";
1585
1586         /* SET OF with two INTEGER = 1, 3, 8 */
1587         const gchar SETOF_THREE[] =  "\x31\x09\x02\x01\x01\x02\x01\x03\x02\x01\x08";
1588
1589         bytes = g_bytes_new_static (SETOF_ONE, XL (SETOF_ONE));
1590         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestSetOf", bytes);
1591         g_assert (asn != NULL);
1592         g_bytes_unref (bytes);
1593
1594         g_assert_cmpint (EGG_ASN1X_SET_OF, ==, egg_asn1x_type (asn));
1595
1596         /* Add integer 1, in SET OF DER should sort to front */
1597         egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 1);
1598
1599         /* Add integer 8, in SET OF DER should sort to back */
1600         egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 8);
1601
1602         data = egg_asn1x_encode (asn, NULL);
1603         if (data == NULL) {
1604                 g_printerr ("%s\n", egg_asn1x_message (asn));
1605                 g_assert_not_reached ();
1606         }
1607
1608         egg_assert_cmpbytes (data, ==, SETOF_THREE, XL (SETOF_THREE));
1609
1610         g_bytes_unref (data);
1611         egg_asn1x_destroy (asn);
1612 }
1613
1614 static void
1615 test_setof_empty (void)
1616 {
1617         GBytes *data;
1618         GNode *asn;
1619
1620         /* SEQUENCE OF with nothing */
1621         const gchar SETOF_NONE[] =  "\x31\x00";
1622
1623         asn = egg_asn1x_create (test_asn1_tab, "TestSetOf");
1624         g_assert (asn);
1625
1626         data = egg_asn1x_encode (asn, NULL);
1627         if (data == NULL) {
1628                 g_printerr ("%s\n", egg_asn1x_message (asn));
1629                 g_assert_not_reached ();
1630         }
1631
1632         egg_assert_cmpbytes (data, ==, SETOF_NONE, XL (SETOF_NONE));
1633
1634         g_bytes_unref (data);
1635         egg_asn1x_destroy (asn);
1636 }
1637
1638 static void
1639 test_enumerated (void)
1640 {
1641         GBytes *bytes;
1642         GNode *asn;
1643         GBytes *data;
1644         GQuark value;
1645
1646         bytes = g_bytes_new_static (ENUM_TWO, XL (ENUM_TWO));
1647         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestEnumerated", bytes);
1648         g_assert (asn != NULL);
1649         g_bytes_unref (bytes);
1650
1651         g_assert_cmpint (EGG_ASN1X_ENUMERATED, ==, egg_asn1x_type (asn));
1652
1653         value = egg_asn1x_get_enumerated (asn);
1654         g_assert (value);
1655         g_assert_cmpstr (g_quark_to_string (value), ==, "valueTwo");
1656
1657         egg_asn1x_set_enumerated (asn, g_quark_from_static_string ("valueThree"));
1658
1659         data = egg_asn1x_encode (asn, NULL);
1660         g_assert (data != NULL);
1661
1662         egg_assert_cmpbytes (data, ==, ENUM_THREE, XL (ENUM_THREE));
1663
1664         g_bytes_unref (data);
1665         egg_asn1x_destroy (asn);
1666 }
1667
1668 static void
1669 test_enumerated_decode_bad (void)
1670 {
1671         const gchar ENUM_NEGATIVE[] =           "\x0A\x01\x85";
1672
1673         GBytes *bytes;
1674         GNode *asn;
1675         gboolean ret;
1676
1677         asn = egg_asn1x_create (test_asn1_tab, "TestEnumerated");
1678         g_assert (asn != NULL);
1679
1680         bytes = g_bytes_new_static (ENUM_NEGATIVE, XL (ENUM_NEGATIVE));
1681         ret = egg_asn1x_decode (asn, bytes);
1682         g_assert (ret == FALSE);
1683         g_assert (strstr (egg_asn1x_message (asn), "enumerated must be positive") != NULL);
1684         g_bytes_unref (bytes);
1685
1686         egg_asn1x_destroy (asn);
1687 }
1688
1689 static void
1690 test_enumerated_not_in_list (void)
1691 {
1692         const gchar ENUM_OTHER[] =   "\x0A\x01\x08";
1693         const gchar ENUM_LARGE[] =   "\x0A\x20\x00\x01\x02\x03\x04\x05\x06\x07"
1694                                              "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
1695                                              "\x00\x01\x02\x03\x04\x05\x06\x07"
1696                                              "\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f";
1697
1698         GBytes *bytes;
1699         GNode *asn;
1700         gboolean ret;
1701
1702         asn = egg_asn1x_create (test_asn1_tab, "TestEnumerated");
1703         g_assert (asn != NULL);
1704
1705         bytes = g_bytes_new_static (ENUM_OTHER, XL (ENUM_OTHER));
1706         ret = egg_asn1x_decode (asn, bytes);
1707         g_assert (ret == FALSE);
1708         g_assert (strstr (egg_asn1x_message (asn), "not part of list") != NULL);
1709         g_bytes_unref (bytes);
1710
1711         bytes = g_bytes_new_static (ENUM_LARGE, XL (ENUM_LARGE));
1712         ret = egg_asn1x_decode (asn, bytes);
1713         g_assert (ret == FALSE);
1714         g_assert (strstr (egg_asn1x_message (asn), "not part of list") != NULL);
1715         g_bytes_unref (bytes);
1716
1717         egg_asn1x_destroy (asn);
1718 }
1719
1720 static void
1721 test_enumerated_not_set (void)
1722 {
1723         GNode *asn;
1724         GQuark value;
1725
1726         asn = egg_asn1x_create (test_asn1_tab, "TestEnumerated");
1727         g_assert (asn != NULL);
1728
1729         value = egg_asn1x_get_enumerated (asn);
1730         g_assert (value == 0);
1731
1732         egg_asn1x_destroy (asn);
1733 }
1734
1735
1736 typedef struct {
1737         GNode *asn1;
1738         guchar *data;
1739         gsize n_data;
1740 } Test;
1741
1742 static void
1743 setup (Test *test, gconstpointer unused)
1744 {
1745         GBytes *bytes;
1746
1747         if (!g_file_get_contents (SRCDIR "/files/test-certificate-1.der",
1748                                   (gchar**)&test->data, &test->n_data, NULL))
1749                 g_assert_not_reached ();
1750
1751         test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate");
1752         g_assert (test->asn1 != NULL);
1753
1754         bytes = g_bytes_new_static (test->data, test->n_data);
1755         if (!egg_asn1x_decode (test->asn1, bytes))
1756                 g_assert_not_reached ();
1757         g_bytes_unref (bytes);
1758 }
1759
1760 static void
1761 teardown (Test *test, gconstpointer unused)
1762 {
1763         egg_asn1x_destroy (test->asn1);
1764         g_free (test->data);
1765 }
1766
1767 static void
1768 test_node_name (Test* test, gconstpointer unused)
1769 {
1770         g_assert_cmpstr (egg_asn1x_name (test->asn1), ==, "Certificate");
1771 }
1772
1773 static void
1774 test_asn1_integers (Test* test, gconstpointer unused)
1775 {
1776         GBytes *data;
1777         GNode *asn;
1778         gboolean ret;
1779         gulong val;
1780
1781         asn = egg_asn1x_create (test_asn1_tab, "TestIntegers");
1782         g_assert ("asn test structure is null" && asn != NULL);
1783
1784         egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint1", NULL), 35);
1785         egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint2", NULL), 23456);
1786         egg_asn1x_set_integer_as_ulong (egg_asn1x_node (asn, "uint3", NULL), 209384022);
1787
1788         /* Now encode the whole caboodle */
1789         data = egg_asn1x_encode (asn, NULL);
1790         g_assert ("encoding asn1 didn't work" && data != NULL);
1791
1792         egg_asn1x_destroy (asn);
1793
1794         /* Now decode it all nicely */
1795         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestIntegers", data);
1796         g_return_if_fail (asn != NULL);
1797
1798         /* And get out the values */
1799         ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint1", NULL), &val);
1800         g_assert ("couldn't read integer from asn1" && ret);
1801         g_assert_cmpuint (val, ==, 35);
1802
1803         ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint2", NULL), &val);
1804         g_assert ("couldn't read integer from asn1" && ret);
1805         g_assert_cmpuint (val, ==, 23456);
1806
1807         ret = egg_asn1x_get_integer_as_ulong (egg_asn1x_node (asn, "uint3", NULL), &val);
1808         g_assert ("couldn't read integer from asn1" && ret);
1809         g_assert_cmpuint (val, ==, 209384022);
1810
1811         egg_asn1x_destroy (asn);
1812         g_bytes_unref (data);
1813 }
1814
1815 static void
1816 test_boolean_seq (Test* test, gconstpointer unused)
1817 {
1818         GBytes *data;
1819         GNode *asn = NULL;
1820         gboolean value, ret;
1821
1822         /* The first boolean has a default of FALSE, so doesn't get encoded if FALSE */
1823         const gchar SEQ_BOOLEAN_TRUE_FALSE[] = "\x30\x06\x01\x01\xFF\x01\x01\x00";
1824         const gchar SEQ_BOOLEAN_FALSE_FALSE[] = "\x30\x03\x01\x01\x00";
1825
1826         asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq");
1827         g_assert ("asn test structure is null" && asn != NULL);
1828
1829         /* Get the default value */
1830         value = TRUE;
1831         ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value);
1832         g_assert (ret == TRUE);
1833         g_assert (value == FALSE);
1834
1835         egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), TRUE);
1836         egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean2", NULL), FALSE);
1837
1838         data = egg_asn1x_encode (asn, NULL);
1839         g_assert (data != NULL);
1840         egg_assert_cmpbytes (data, ==, SEQ_BOOLEAN_TRUE_FALSE, XL (SEQ_BOOLEAN_TRUE_FALSE));
1841         g_bytes_unref (data);
1842
1843         ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value);
1844         g_assert (ret);
1845         g_assert (value == TRUE);
1846
1847         egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), FALSE);
1848
1849         data = egg_asn1x_encode (asn, NULL);
1850         g_assert (data != NULL);
1851         egg_assert_cmpbytes (data, ==, SEQ_BOOLEAN_FALSE_FALSE, XL (SEQ_BOOLEAN_FALSE_FALSE));
1852
1853         ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value);
1854         g_assert (ret);
1855         g_assert (value == FALSE);
1856
1857         g_bytes_unref (data);
1858         egg_asn1x_destroy (asn);
1859 }
1860
1861 static void
1862 test_write_value (Test* test, gconstpointer unused)
1863 {
1864         GBytes *encoded;
1865         GNode *asn = NULL;
1866         guchar *data;
1867         gsize n_data;
1868
1869         asn = egg_asn1x_create (test_asn1_tab, "TestData");
1870         g_assert ("asn test structure is null" && asn != NULL);
1871
1872         egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL);
1873
1874         encoded = egg_asn1x_encode (asn, NULL);
1875         g_assert (encoded);
1876
1877         data = egg_asn1x_get_string_as_raw (egg_asn1x_node (asn, "data", NULL), NULL, &n_data);
1878         g_assert (data != NULL);
1879         g_assert_cmpuint (n_data, ==, 9);
1880         g_assert (memcmp (data, "SOME DATA", 9) == 0);
1881         g_free (data);
1882
1883         g_bytes_unref (encoded);
1884         egg_asn1x_destroy (asn);
1885 }
1886
1887 static void
1888 test_element_length_content (Test* test, gconstpointer unused)
1889 {
1890         GBytes *buffer;
1891         GNode *asn = NULL;
1892         const guchar *content;
1893         gsize n_content;
1894         gssize length;
1895
1896         asn = egg_asn1x_create (test_asn1_tab, "TestData");
1897         g_assert ("asn test structure is null" && asn != NULL);
1898
1899         egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL);
1900
1901         buffer = egg_asn1x_encode (asn, NULL);
1902         g_assert (buffer != NULL);
1903
1904         /* Now the real test */
1905         length = egg_asn1x_element_length (g_bytes_get_data (buffer, NULL),
1906                                            g_bytes_get_size (buffer) + 1024);
1907         g_assert_cmpint (length, ==, 13);
1908
1909         content = egg_asn1x_element_content (g_bytes_get_data (buffer, NULL),
1910                                              length, &n_content);
1911         g_assert (content != NULL);
1912         g_assert_cmpuint (n_content, ==, 11);
1913
1914         content = egg_asn1x_element_content (content, n_content, &n_content);
1915         g_assert (content);
1916         g_assert_cmpuint (n_content, ==, 9);
1917         g_assert (memcmp (content, "SOME DATA", 9) == 0);
1918
1919         const guchar *BAD_ASN_TAG = (guchar *)"\x00";
1920         content = egg_asn1x_element_content (BAD_ASN_TAG, 1, &n_content);
1921         g_assert (content == NULL);
1922
1923         const guchar *BAD_ASN_LENGTH = (guchar *)"\x30\x80";
1924         content = egg_asn1x_element_content (BAD_ASN_LENGTH, 2, &n_content);
1925         g_assert (content == NULL);
1926
1927         egg_asn1x_destroy (asn);
1928         g_bytes_unref (buffer);
1929 }
1930
1931 static void
1932 test_read_element (Test* test, gconstpointer unused)
1933 {
1934         GBytes *buffer;
1935         GNode *asn = NULL;
1936         GBytes *data;
1937
1938         asn = egg_asn1x_create (test_asn1_tab, "TestData");
1939         g_assert ("asn test structure is null" && asn != NULL);
1940
1941         egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL);
1942
1943         buffer = egg_asn1x_encode (asn, NULL);
1944         g_assert (buffer != NULL);
1945
1946         /* Have to decode before we can get raw elements */
1947         if (!egg_asn1x_decode (asn, buffer))
1948                 g_assert_not_reached ();
1949
1950         /* Now the real test */
1951         data = egg_asn1x_get_element_raw (egg_asn1x_node (asn, "data", NULL));
1952         g_assert (data != NULL);
1953         g_assert_cmpint (g_bytes_get_size (data), ==, 11);
1954         g_bytes_unref (data);
1955
1956         data = egg_asn1x_get_value_raw (egg_asn1x_node (asn, "data", NULL));
1957         g_assert (data != NULL);
1958         egg_assert_cmpbytes (data, ==, "SOME DATA", 9);
1959         g_bytes_unref (data);
1960
1961         egg_asn1x_destroy (asn);
1962         g_bytes_unref (buffer);
1963 }
1964
1965 static void
1966 test_oid (void)
1967 {
1968         GBytes *buffer;
1969         GNode *asn = NULL;
1970         GNode *node;
1971         GQuark oid, check;
1972
1973         asn = egg_asn1x_create (test_asn1_tab, "TestOid");
1974         g_assert ("asn test structure is null" && asn != NULL);
1975
1976         node = egg_asn1x_node (asn, "oid", NULL);
1977         g_assert_cmpint (EGG_ASN1X_OBJECT_ID, ==, egg_asn1x_type (node));
1978
1979         if (!egg_asn1x_set_oid_as_string (node, "1.2.34567.89"))
1980                 g_assert_not_reached ();
1981
1982         buffer = egg_asn1x_encode (asn, NULL);
1983         g_assert (buffer != NULL);
1984
1985         /* Now a quark has been defined */
1986         check = g_quark_from_static_string ("1.2.34567.89");
1987         oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "oid", NULL));
1988         g_assert (oid);
1989         g_assert (check == oid);
1990         g_assert_cmpstr (g_quark_to_string (oid), ==, "1.2.34567.89");
1991
1992         /* Write a different OID */
1993         if (!egg_asn1x_set_oid_as_quark (egg_asn1x_node (asn, "oid", NULL), g_quark_from_static_string ("5.4.3.2.1678")))
1994                 g_assert_not_reached ();
1995
1996         g_bytes_unref (buffer);
1997         buffer = egg_asn1x_encode (asn, NULL);
1998         g_assert (buffer != NULL);
1999
2000         oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "oid", NULL));
2001         g_assert (oid);
2002         g_assert_cmpstr (g_quark_to_string (oid), ==, "5.4.3.2.1678");
2003
2004         g_bytes_unref (buffer);
2005         egg_asn1x_destroy (asn);
2006 }
2007
2008 static void
2009 test_oid_set_invalid (void)
2010 {
2011         GNode *asn;
2012
2013         asn = egg_asn1x_create (test_asn1_tab, "TestOid");
2014         g_assert ("asn test structure is null" && asn != NULL);
2015
2016         if (egg_asn1x_set_oid_as_string (egg_asn1x_node (asn, "oid", NULL), "abcd"))
2017                 g_assert_not_reached ();
2018
2019         egg_asn1x_destroy (asn);
2020 }
2021
2022 static void
2023 test_oid_decode_bad (void)
2024 {
2025         GBytes *bytes;
2026         GNode *asn;
2027         gboolean ret;
2028
2029         /* Has invalid leading integer in oid value */
2030         const gchar INVALID_OID[] = "\x30\x07\x06\x05\x2b\x80\x83\x82\x1a";
2031
2032         asn = egg_asn1x_create (test_asn1_tab, "TestOid");
2033         g_assert ("asn test structure is null" && asn != NULL);
2034
2035         bytes = g_bytes_new_static (INVALID_OID, XL (INVALID_OID));
2036         ret = egg_asn1x_decode (asn, bytes);
2037         g_assert (ret == FALSE);
2038         g_assert (strstr (egg_asn1x_message (asn), "object id encoding is invalid") != NULL);
2039
2040         g_bytes_unref (bytes);
2041         egg_asn1x_destroy (asn);
2042 }
2043
2044 static void
2045 test_oid_get_no_value (void)
2046 {
2047         GNode *asn;
2048         gchar *oid;
2049
2050         asn = egg_asn1x_create (test_asn1_tab, "TestOid");
2051         g_assert ("asn test structure is null" && asn != NULL);
2052
2053         oid = egg_asn1x_get_oid_as_string (egg_asn1x_node (asn, "oid", NULL));
2054         g_assert (oid == NULL);
2055
2056         egg_asn1x_destroy (asn);
2057 }
2058
2059 typedef struct _TimeTestData {
2060         gchar *value;
2061         time_t ref;
2062 } TimeTestData;
2063
2064 static const TimeTestData generalized_time_test_data[] = {
2065         { "20070725130528Z", 1185368728 },
2066         { "20070725130528.2134Z", 1185368728 },
2067         { "20070725140528-0100", 1185368728 },
2068         { "20070725040528+0900", 1185368728 },
2069         { "20070725013528+1130", 1185368728 },
2070         { "20070725Z", 1185321600 },
2071         { "20070725+0000", 1185321600 },
2072
2073         /* Bad ones */
2074         { "200707", -1 },
2075
2076         { NULL, 0 }
2077 };
2078
2079 static const TimeTestData utc_time_test_data[] = {
2080         /* Test the Y2K style wrap arounds */
2081         { "070725130528Z", 1185368728 },  /* The year 2007 */
2082         { "020725130528Z", 1027602328 },  /* The year 2002 */
2083         { "970725130528Z", 869835928 },   /* The year 1997 */
2084         { "370725130528Z", 2132139928 },  /* The year 2037 */
2085
2086         /* Test the time zones and other formats */
2087         { "070725130528.2134Z", 1185368728 },
2088         { "070725140528-0100", 1185368728 },
2089         { "070725040528+0900", 1185368728 },
2090         { "070725013528+1130", 1185368728 },
2091         { "070725Z", 1185321600 },
2092         { "070725+0000", 1185321600 },
2093
2094         /* Bad ones */
2095         { "0707", -1 },
2096
2097         { NULL, 0 }
2098 };
2099
2100 static void
2101 test_general_time (Test* test, gconstpointer unused)
2102 {
2103         time_t when;
2104         const TimeTestData *data;
2105
2106         for (data = generalized_time_test_data; data->value; ++data) {
2107                 when = egg_asn1x_parse_time_general (data->value, -1);
2108                 if (data->ref != when) {
2109                         printf ("%s", data->value);
2110                         printf ("%s != ", ctime (&when));
2111                         printf ("%s\n", ctime (&data->ref));
2112                         fflush (stdout);
2113                 }
2114
2115                 g_assert ("decoded time doesn't match reference" && data->ref == when);
2116         }
2117 }
2118
2119 static void
2120 test_utc_time (Test* test, gconstpointer unused)
2121 {
2122         time_t when;
2123         const TimeTestData *data;
2124
2125         for (data = utc_time_test_data; data->value; ++data) {
2126                 when = egg_asn1x_parse_time_utc (data->value, -1);
2127                 if (data->ref != when) {
2128                         printf ("%s", data->value);
2129                         printf ("%s != ", ctime (&when));
2130                         printf ("%s\n", ctime (&data->ref));
2131                         fflush (stdout);
2132                 }
2133
2134                 g_assert ("decoded time doesn't match reference" && data->ref == when);
2135         }
2136 }
2137
2138 static void
2139 test_read_time (Test* test, gconstpointer unused)
2140 {
2141         glong time;
2142
2143         time = egg_asn1x_get_time_as_long (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notBefore", NULL));
2144         g_assert_cmpint (time, ==, 820454400);
2145 }
2146
2147 static void
2148 test_read_date (Test* test, gconstpointer unused)
2149 {
2150         GDate date;
2151         if (!egg_asn1x_get_time_as_date (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notAfter", NULL), &date))
2152                 g_assert_not_reached ();
2153         g_assert_cmpint (date.day, ==, 31);
2154         g_assert_cmpint (date.month, ==, 12);
2155         g_assert_cmpint (date.year, ==, 2020);
2156 }
2157
2158 static void
2159 test_create_by_oid (Test* test, gconstpointer unused)
2160 {
2161         /* id-at-initials = X520initials */
2162         GNode *node = egg_asn1x_create (pkix_asn1_tab, "2.5.4.43");
2163         g_assert (node != NULL);
2164         g_assert_cmpstr (egg_asn1x_name (node), ==, "X520initials");
2165         egg_asn1x_destroy (node);
2166 }
2167
2168 static void
2169 test_create_by_oid_invalid (Test* test, gconstpointer unused)
2170 {
2171         GNode *node = egg_asn1x_create (pkix_asn1_tab, "23.23.23.23");
2172         g_assert (node == NULL);
2173 }
2174
2175 static void
2176 test_create_by_bad_order (Test* test, gconstpointer unused)
2177 {
2178         /*
2179          * In pkix.asn the definition for parts of this oid
2180          * come in the wrong order. However this should still work.
2181          */
2182
2183         /* id-pe-authorityInfoAccess = AuthorityInfoAccessSyntax */
2184         GNode *node = egg_asn1x_create (pkix_asn1_tab, "1.3.6.1.5.5.7.1.1");
2185         g_assert (node != NULL);
2186         g_assert_cmpstr (egg_asn1x_name (node), ==, "AuthorityInfoAccessSyntax");
2187         egg_asn1x_destroy (node);
2188 }
2189
2190 static void
2191 test_count (Test* test, gconstpointer unused)
2192 {
2193         GNode *node;
2194
2195         node = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL);
2196         g_assert (node);
2197         g_assert_cmpuint (egg_asn1x_count (node), ==, 7);
2198 }
2199
2200 static void
2201 test_nested_fails_with_extra (void)
2202 {
2203         gboolean ret;
2204         GBytes *bytes;
2205         GNode *asn;
2206
2207         const gchar SEQ_NESTED[] =  "\x30\x0C"
2208                                          "\x04\x03""one"
2209                                          "\x04\x05""extra";
2210
2211         asn = egg_asn1x_create (test_asn1_tab, "TestData");
2212         g_assert ("asn test structure is null" && asn != NULL);
2213
2214         bytes = g_bytes_new_static (SEQ_NESTED, XL (SEQ_NESTED));
2215         ret = egg_asn1x_decode (asn, bytes);
2216         egg_asn1x_assert (ret == FALSE, asn);
2217         egg_asn1x_assert (strstr (egg_asn1x_message (asn), "encountered extra tag"), asn);
2218         g_bytes_unref (bytes);
2219
2220         egg_asn1x_destroy (asn);
2221 }
2222
2223 static void
2224 test_nested_unexpected (void)
2225 {
2226         gboolean ret;
2227         GBytes *bytes;
2228         GNode *asn;
2229
2230         const gchar SEQ_NESTED[] =  "\x30\x03"
2231                                          "\x02\x01\x2A";
2232
2233         asn = egg_asn1x_create (test_asn1_tab, "TestData");
2234         g_assert ("asn test structure is null" && asn != NULL);
2235
2236         bytes = g_bytes_new_static (SEQ_NESTED, XL (SEQ_NESTED));
2237         ret = egg_asn1x_decode (asn, bytes);
2238         egg_asn1x_assert (ret == FALSE, asn);
2239         egg_asn1x_assert (strstr (egg_asn1x_message (asn), "decoded tag did not match expected"), asn);
2240         g_bytes_unref (bytes);
2241
2242         egg_asn1x_destroy (asn);
2243 }
2244
2245 static void
2246 test_create_and_decode_invalid (void)
2247 {
2248         GBytes *bytes;
2249         GNode *asn;
2250
2251         bytes = g_bytes_new_static ("", 0);
2252         asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestData", bytes);
2253         g_assert (asn == NULL);
2254         g_bytes_unref (bytes);
2255 }
2256
2257 static void
2258 test_decode_extra (void)
2259 {
2260         GBytes *bytes;
2261         GNode *asn;
2262
2263         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
2264         g_assert (asn != NULL);
2265
2266         bytes = g_bytes_new_static ("\x30\x00\x11", 3);
2267         if (egg_asn1x_decode (asn, bytes))
2268                 g_assert_not_reached ();
2269         g_assert (strstr (egg_asn1x_message (asn), "extra unexpected trailing data"));
2270         g_bytes_unref (bytes);
2271         egg_asn1x_destroy (asn);
2272 }
2273
2274 static void
2275 test_decode_nested_short (void)
2276 {
2277         GBytes *bytes;
2278         GNode *asn;
2279
2280         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOfAny");
2281         g_assert (asn != NULL);
2282
2283         bytes = g_bytes_new_static ("\x30\x02\xA5\x08", 4);
2284         if (egg_asn1x_decode (asn, bytes))
2285                 g_assert_not_reached ();
2286         g_assert (strstr (egg_asn1x_message (asn), "content is not encoded properly"));
2287         g_bytes_unref (bytes);
2288
2289         bytes = g_bytes_new_static ("\x30\x04\x30\x02\xA5\x08", 6);
2290         if (egg_asn1x_decode (asn, bytes))
2291                 g_assert_not_reached ();
2292         g_assert (strstr (egg_asn1x_message (asn), "content is not encoded properly"));
2293         g_bytes_unref (bytes);
2294
2295         egg_asn1x_destroy (asn);
2296 }
2297
2298 static void
2299 test_decode_indefinite_primitive (void)
2300 {
2301         GBytes *bytes;
2302         GNode *asn;
2303
2304         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
2305         g_assert (asn != NULL);
2306
2307         bytes = g_bytes_new_static ("\x04\x80\x04\x01\x55\x00\x00", 7);
2308         if (egg_asn1x_decode (asn, bytes))
2309                 g_assert_not_reached ();
2310         g_assert (strstr (egg_asn1x_message (asn), "indefinite length on non-structured type"));
2311         g_bytes_unref (bytes);
2312
2313         egg_asn1x_destroy (asn);
2314 }
2315
2316 static void
2317 test_decode_invalid_long_length (void)
2318 {
2319         GBytes *bytes;
2320         GNode *asn;
2321
2322         const gchar DER[] = "\x04\xA0"
2323                         "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
2324                         "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01";
2325         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
2326         g_assert (asn != NULL);
2327
2328         bytes = g_bytes_new_static (DER, XL (DER));
2329         if (egg_asn1x_decode (asn, bytes))
2330                 g_assert_not_reached ();
2331         g_assert (strstr (egg_asn1x_message (asn), "content is not encoded properly"));
2332         g_bytes_unref (bytes);
2333
2334         egg_asn1x_destroy (asn);
2335 }
2336
2337 static void
2338 test_decode_truncated_at_tag (void)
2339 {
2340         GBytes *bytes;
2341         GNode *asn;
2342
2343         const gchar DER[] = "\x04";
2344         asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
2345         g_assert (asn != NULL);
2346
2347         bytes = g_bytes_new_static (DER, XL (DER));
2348         if (egg_asn1x_decode (asn, bytes))
2349                 g_assert_not_reached ();
2350         g_assert (strstr (egg_asn1x_message (asn), "content is not encoded properly"));
2351         g_bytes_unref (bytes);
2352
2353         egg_asn1x_destroy (asn);
2354 }
2355
2356 static void
2357 test_decode_long_tag (void)
2358 {
2359         GBytes *bytes;
2360         GNode *asn;
2361         gboolean ret;
2362
2363         const gchar DER[] = "\xbf\x89\x52\x03\x04\x01\x33";
2364
2365         asn = egg_asn1x_create (test_asn1_tab, "TestTagLong");
2366         g_assert (asn != NULL);
2367
2368         bytes = g_bytes_new_static (DER, XL (DER));
2369         ret = egg_asn1x_decode (asn, bytes);
2370         egg_asn1x_assert (ret == TRUE, asn);
2371
2372         g_bytes_unref (bytes);
2373         egg_asn1x_destroy (asn);
2374
2375 }
2376
2377 static void
2378 test_create_quark (void)
2379 {
2380         GNode *asn;
2381
2382         asn = egg_asn1x_create_quark (test_asn1_tab, g_quark_from_static_string ("1.5.13"));
2383         g_assert (asn != NULL);
2384         g_assert_cmpstr (egg_asn1x_name (asn), ==, "TestIntegers");
2385 }
2386
2387 static void
2388 test_validate_default (void)
2389 {
2390         GNode *asn;
2391
2392         asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq");
2393         /* We leave first boolean field empty */
2394         egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean2", NULL), TRUE);
2395         if (!egg_asn1x_validate (asn, TRUE))
2396                 g_assert_not_reached ();
2397         egg_asn1x_destroy (asn);
2398 }
2399
2400 static void
2401 test_validate_missing (void)
2402 {
2403         GNode *asn;
2404
2405         asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq");
2406         /* No fields set */
2407         if (egg_asn1x_validate (asn, TRUE))
2408                 g_assert_not_reached ();
2409         g_assert (strstr (egg_asn1x_message (asn), "missing value") != NULL);
2410         egg_asn1x_destroy (asn);
2411 }
2412
2413 static void
2414 test_validate_seq_of_child_invalid (void)
2415 {
2416         GNode *asn;
2417         GNode *child;
2418
2419         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOfSeq");
2420         child = egg_asn1x_append (asn);
2421         egg_asn1x_set_integer_as_ulong (egg_asn1x_node (child, "uint1", NULL), 5);
2422         /* We didn't set uint2 or uint3 so the child is invalid */
2423         if (egg_asn1x_validate (asn, TRUE))
2424                 g_assert_not_reached ();
2425         g_assert (strstr (egg_asn1x_message (asn), "missing value") != NULL);
2426         egg_asn1x_destroy (asn);
2427
2428 }
2429
2430 static void
2431 test_validate_optional_seq (void)
2432 {
2433         GNode *asn;
2434
2435         asn = egg_asn1x_create (test_asn1_tab, "TestSeqOptional");
2436         if (!egg_asn1x_validate (asn, TRUE))
2437                 g_assert_not_reached ();
2438         egg_asn1x_destroy (asn);
2439 }
2440
2441 static void
2442 test_element_get_not_set (void)
2443 {
2444         GNode *asn;
2445
2446         asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq");
2447         g_assert (egg_asn1x_get_element_raw (asn) == NULL);
2448         egg_asn1x_destroy (asn);
2449 }
2450
2451 int
2452 main (int argc, char **argv)
2453 {
2454         g_test_init (&argc, &argv, NULL);
2455
2456         g_test_add_func ("/asn1/decode/extra", test_decode_extra);
2457         g_test_add_func ("/asn1/decode/nested-short", test_decode_nested_short);
2458         g_test_add_func ("/asn1/decode/indefinite-primitive", test_decode_indefinite_primitive);
2459         g_test_add_func ("/asn1/decode/invalid-long-length", test_decode_invalid_long_length);
2460         g_test_add_func ("/asn1/decode/truncated-at-tag", test_decode_truncated_at_tag);
2461         g_test_add_func ("/asn1/decode/decode-long-tag", test_decode_long_tag);
2462         g_test_add_func ("/asn1/boolean", test_boolean);
2463         g_test_add_func ("/asn1/boolean-bad", test_boolean_decode_bad);
2464         g_test_add_func ("/asn1/boolean-default", test_boolean_default);
2465         g_test_add_func ("/asn1/null", test_null);
2466         g_test_add_func ("/asn1/integer", test_integer);
2467         g_test_add_func ("/asn1/integer-zero-length", test_integer_zero_length);
2468         g_test_add_func ("/asn1/integer/raw", test_integer_raw);
2469         g_test_add_func ("/asn1/integer/raw-not-twos-complement", test_integer_raw_not_twos_complement);
2470         g_test_add_func ("/asn1/unsigned", test_unsigned);
2471         g_test_add_func ("/asn1/unsigned/not-set", test_unsigned_not_set);
2472         g_test_add_func ("/asn1/unsigned/default", test_unsigned_default);
2473         g_test_add_func ("/asn1/unsigned/constant", test_unsigned_constant);
2474         g_test_add_func ("/asn1/unsigned/zero", test_unsigned_zero);
2475         g_test_add_func ("/asn1/octet_string", test_octet_string);
2476         g_test_add_func ("/asn1/octet-string/set-bad-utf8", test_octet_string_set_bad_utf8);
2477         g_test_add_func ("/asn1/octet-string/bmp-as-utf8", test_octet_string_bmp_as_utf8);
2478         g_test_add_func ("/asn1/octet-string/get-as-bytes", test_octet_string_get_as_bytes);
2479         g_test_add_func ("/asn1/octet-string/set-as-bytes", test_octet_string_set_as_bytes);
2480         g_test_add_func ("/asn1/octet-string/structured", test_octet_string_structured);
2481         g_test_add_func ("/asn1/octet-string/structured-bad", test_octet_string_structured_bad);
2482         g_test_add_func ("/asn1/generalized_time", test_generalized_time);
2483         g_test_add_func ("/asn1/time-get-missing", test_time_get_missing);
2484         g_test_add_func ("/asn1/implicit/decode", test_implicit_decode);
2485         g_test_add_func ("/asn1/implicit/encode", test_implicit_encode);
2486         g_test_add_func ("/asn1/explicit/decode", test_explicit_decode);
2487         g_test_add_func ("/asn1/explicit/encode", test_explicit_encode);
2488         g_test_add_func ("/asn1/explicit/no-context-specific", test_explicit_no_context_specific);
2489         g_test_add_func ("/asn1/explicit/no-context-child", test_explicit_no_context_child);
2490         g_test_add_func ("/asn1/explicit/extra-context-child", test_explicit_extra_context_child);
2491         g_test_add_func ("/asn1/universal/decode", test_universal_decode);
2492         g_test_add_func ("/asn1/universal/encode", test_universal_encode);
2493         g_test_add_func ("/asn1/bit_string_decode", test_bit_string_decode);
2494         g_test_add_func ("/asn1/bit_string_decode_bad", test_bit_string_decode_bad);
2495         g_test_add_func ("/asn1/bit_string_decode_ulong", test_bit_string_decode_ulong);
2496         g_test_add_func ("/asn1/bit_string_encode_decode", test_bit_string_encode_decode);
2497         g_test_add_func ("/asn1/bit_string_encode_decode_ulong", test_bit_string_encode_decode_ulong);
2498         g_test_add_func ("/asn1/bit_string_encode_decode_zero", test_bit_string_encode_decode_zero);
2499         g_test_add_func ("/asn1/bit-string/ulong-too-long", test_bit_string_ulong_too_long);
2500         g_test_add_func ("/asn1/bit-string/get-not-set", test_bit_string_get_not_set);
2501         g_test_add_func ("/asn1/bit-string/invalid-length", test_bit_string_invalid_length);
2502         g_test_add_func ("/asn1/bit-string/invalid-empty", test_bit_string_invalid_empty);
2503         g_test_add_func ("/asn1/oid", test_oid);
2504         g_test_add_func ("/asn1/oid/set-invalid", test_oid_set_invalid);
2505         g_test_add_func ("/asn1/oid/get-no-value", test_oid_get_no_value);
2506         g_test_add_func ("/asn1/oid/decode-bad", test_oid_decode_bad);
2507         g_test_add_func ("/asn1/have", test_have);
2508         g_test_add_func ("/asn1/any-raw", test_any_raw);
2509         g_test_add_func ("/asn1/any-raw/explicit", test_any_raw_explicit);
2510         g_test_add_func ("/asn1/any-raw/invalid", test_any_raw_invalid);
2511         g_test_add_func ("/asn1/any-raw/not-set", test_any_raw_not_set);
2512         g_test_add_func ("/asn1/any-into", test_any_into);
2513         g_test_add_func ("/asn1/any-into/explicit", test_any_into_explicit);
2514         g_test_add_func ("/asn1/any-into/explicit-not-set", test_any_into_explicit_not_set);
2515         g_test_add_func ("/asn1/choice_not_chosen", test_choice_not_chosen);
2516         g_test_add_func ("/asn1/any_choice_set_raw_short_tag", test_any_choice_set_raw_short_tag);
2517         g_test_add_func ("/asn1/any_choice_set_raw_long_tag", test_any_choice_set_raw_long_tag);
2518         g_test_add_func ("/asn1/seq-of-any", test_seq_of_any);\
2519         g_test_add_func ("/asn1/seq-of-invalid", test_seq_of_invalid);
2520         g_test_add_func ("/asn1/seq-of-different", test_seq_of_different);
2521         g_test_add_func ("/asn1/set-order", test_set_order);
2522         g_test_add_func ("/asn1/append", test_append);
2523         g_test_add_func ("/asn1/append_and_clear", test_append_and_clear);
2524         g_test_add_func ("/asn1/setof", test_setof);
2525         g_test_add_func ("/asn1/setof_empty", test_setof_empty);
2526         g_test_add_func ("/asn1/enumerated", test_enumerated);
2527         g_test_add_func ("/asn1/enumerated-bad", test_enumerated_decode_bad);
2528         g_test_add_func ("/asn1/enumerated-not-in-list", test_enumerated_not_in_list);
2529         g_test_add_func ("/asn1/enumerated-not-set", test_enumerated_not_set);
2530         g_test_add_func ("/asn1/nested-fails-with-extra", test_nested_fails_with_extra);
2531         g_test_add_func ("/asn1/nested-unexpected", test_nested_unexpected);
2532         g_test_add_func ("/asn1/create-and-decode-invalid", test_create_and_decode_invalid);
2533         g_test_add_func ("/asn1/create-quark", test_create_quark);
2534         g_test_add_func ("/asn1/validate-default", test_validate_default);
2535         g_test_add_func ("/asn1/validate-missing", test_validate_missing);
2536         g_test_add_func ("/asn1/validate-seq-of-child-invalid", test_validate_seq_of_child_invalid);
2537         g_test_add_func ("/asn1/validate-optional-seq", test_validate_optional_seq);
2538         g_test_add_func ("/asn1/get-element/not-set", test_element_get_not_set);
2539         g_test_add ("/asn1/node_name", Test, NULL, setup, test_node_name, teardown);
2540         g_test_add ("/asn1/asn1_integers", Test, NULL, setup, test_asn1_integers, teardown);
2541         g_test_add ("/asn1/boolean_seq", Test, NULL, setup, test_boolean_seq, teardown);
2542         g_test_add ("/asn1/write_value", Test, NULL, setup, test_write_value, teardown);
2543         g_test_add ("/asn1/element_length_content", Test, NULL, setup, test_element_length_content, teardown);
2544         g_test_add ("/asn1/read_element", Test, NULL, setup, test_read_element, teardown);
2545         g_test_add ("/asn1/general_time", Test, NULL, setup, test_general_time, teardown);
2546         g_test_add ("/asn1/utc_time", Test, NULL, setup, test_utc_time, teardown);
2547         g_test_add ("/asn1/read_time", Test, NULL, setup, test_read_time, teardown);
2548         g_test_add ("/asn1/read_date", Test, NULL, setup, test_read_date, teardown);
2549         g_test_add ("/asn1/create_by_oid", Test, NULL, setup, test_create_by_oid, teardown);
2550         g_test_add ("/asn1/create_by_oid_invalid", Test, NULL, setup, test_create_by_oid_invalid, teardown);
2551         g_test_add ("/asn1/create_by_bad_order", Test, NULL, setup, test_create_by_bad_order, teardown);
2552         g_test_add ("/asn1/count", Test, NULL, setup, test_count, teardown);
2553
2554         return g_test_run ();
2555 }