1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-asn1.c: Test ASN1 stuf
4 Copyright (C) 2009 Stefan Walter
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.
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.
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.
21 Author: Stef Walter <stef@memberwebs.com>
26 #include "egg/egg-asn1x.h"
27 #include "egg/egg-asn1-defs.h"
28 #include "egg/egg-testing.h"
36 typedef struct _EggAsn1xDef ASN1_ARRAY_TYPE;
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";
53 /* ENUM with value = 2 */
54 const gchar ENUM_TWO[] = "\x0A\x01\x02";
56 /* ENUM with value = 3 */
57 const gchar ENUM_THREE[] = "\x0A\x01\x03";
59 #define XL(x) G_N_ELEMENTS (x) - 1
68 asn = egg_asn1x_create (test_asn1_tab, "TestBoolean");
71 g_assert_cmpint (EGG_ASN1X_BOOLEAN, ==, egg_asn1x_type (asn));
73 /* Shouldn't succeed */
74 if (egg_asn1x_get_boolean (asn, &value))
75 g_assert_not_reached ();
78 bytes = g_bytes_new_static (BFALSE, XL (BFALSE));
79 if (!egg_asn1x_decode (asn, bytes))
80 g_assert_not_reached ();
82 if (!egg_asn1x_get_boolean (asn, &value))
83 g_assert_not_reached ();
84 g_assert (value == FALSE);
85 g_bytes_unref (bytes);
88 bytes = g_bytes_new_static (BTRUE, XL (BTRUE));
89 if (!egg_asn1x_decode (asn, bytes))
90 g_assert_not_reached ();
92 if (!egg_asn1x_get_boolean (asn, &value))
93 g_assert_not_reached ();
94 g_assert (value == TRUE);
95 g_bytes_unref (bytes);
97 egg_asn1x_clear (asn);
99 /* Shouldn't suceed after clear */
100 if (egg_asn1x_get_boolean (asn, &value))
101 g_assert_not_reached ();
103 egg_asn1x_destroy (asn);
107 test_boolean_decode_bad (void)
109 const gchar BOOLEAN_INVALID_LENGTH[] = "\x01\x02\x00\x00";
110 const gchar BOOLEAN_BAD_VALUE[] = "\x01\x01\x05";
116 asn = egg_asn1x_create (test_asn1_tab, "TestBoolean");
117 g_assert (asn != NULL);
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);
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);
131 egg_asn1x_destroy (asn);
135 test_boolean_default (void)
140 const gchar BOOLEAN[] = "\x30\x00";
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);
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);
151 egg_asn1x_destroy (asn);
160 asn = egg_asn1x_create (test_asn1_tab, "TestNull");
163 g_assert_cmpint (EGG_ASN1X_NULL, ==, egg_asn1x_type (asn));
165 egg_asn1x_set_null (asn);
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));
170 if (!egg_asn1x_decode (asn, data))
171 g_assert_not_reached ();
173 egg_asn1x_destroy (asn);
174 g_bytes_unref (data);
184 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
187 g_assert_cmpint (EGG_ASN1X_INTEGER, ==, egg_asn1x_type (asn));
189 /* Shouldn't succeed */
190 if (egg_asn1x_get_integer_as_ulong (asn, &value))
191 g_assert_not_reached ();
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);
202 egg_asn1x_clear (asn);
204 /* Shouldn't suceed after clear */
205 if (egg_asn1x_get_integer_as_ulong (asn, &value))
206 g_assert_not_reached ();
208 egg_asn1x_destroy (asn);
212 test_integer_zero_length (void)
214 const gchar INTEGER_EMPTY[] = "\x02\x00";
220 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
221 g_assert (asn != NULL);
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);
229 egg_asn1x_destroy (asn);
242 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
245 g_assert_cmpint (EGG_ASN1X_INTEGER, ==, egg_asn1x_type (asn));
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);
256 egg_asn1x_clear (asn);
258 egg_asn1x_set_integer_as_ulong (asn, 253);
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);
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);
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));
275 egg_asn1x_clear (asn);
277 egg_asn1x_take_integer_as_usg (asn, g_bytes_new_static (&val, 1));
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);
284 egg_asn1x_destroy (asn);
288 test_unsigned_not_set (void)
293 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
296 bytes = egg_asn1x_get_integer_as_usg (asn);
297 g_assert (bytes == NULL);
299 egg_asn1x_destroy (asn);
303 test_unsigned_default (void)
308 const gchar INTEGERS[] = "\x30\x06\x02\x01\x01\x02\x01\x02";
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);
316 bytes = egg_asn1x_encode (asn, NULL);
317 egg_assert_cmpbytes (bytes, ==, INTEGERS, XL (INTEGERS));
318 g_bytes_unref (bytes);
320 egg_asn1x_destroy (asn);
324 test_unsigned_constant (void)
329 /* const gchar SEQ[] = "\x30\x00"; */
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);
336 egg_asn1x_destroy (asn);
340 test_unsigned_zero (void)
345 const gchar DER[] = "\x02\x01\x00";
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);
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);
356 egg_asn1x_destroy (asn);
360 test_integer_raw (void)
365 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
366 g_assert (asn != NULL);
368 bytes = g_bytes_new_static ("\x01\x02\x03", 3);
369 egg_asn1x_set_integer_as_raw (asn, bytes);
370 g_bytes_unref (bytes);
372 bytes = egg_asn1x_encode (asn, NULL);
373 egg_assert_cmpbytes (bytes, ==, "\x02\x03\x01\x02\x03", 5);
374 g_bytes_unref (bytes);
376 bytes = egg_asn1x_get_integer_as_raw (asn);
377 egg_assert_cmpbytes (bytes, ==, "\x01\x02\x03", 3);
378 g_bytes_unref (bytes);
380 egg_asn1x_destroy (asn);
384 test_integer_raw_not_twos_complement (void)
389 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
390 g_assert (asn != NULL);
392 bytes = g_bytes_new_static ("\x81\x02\x03", 3);
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 */
399 g_test_trap_assert_failed ();
400 g_test_trap_assert_stderr ("*not two's complement*");
402 g_bytes_unref (bytes);
403 egg_asn1x_destroy (asn);
407 test_octet_string (void)
413 asn = egg_asn1x_create (test_asn1_tab, "TestOctetString");
416 g_assert_cmpint (EGG_ASN1X_OCTET_STRING, ==, egg_asn1x_type (asn));
418 /* Shouldn't succeed */
419 if (egg_asn1x_get_string_as_utf8 (asn, NULL))
420 g_assert_not_reached ();
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);
428 value = egg_asn1x_get_string_as_utf8 (asn, NULL);
429 g_assert_cmpstr (value, ==, "farnsworth");
432 egg_asn1x_clear (asn);
434 /* Shouldn't succeed */
435 if (egg_asn1x_get_string_as_utf8 (asn, NULL))
436 g_assert_not_reached ();
438 egg_asn1x_destroy (asn);
442 test_octet_string_set_bad_utf8 (void)
446 asn = egg_asn1x_create (test_asn1_tab, "TestOctetString");
449 if (egg_asn1x_set_string_as_utf8 (asn, "\xFF\xFA", NULL))
450 g_assert_not_reached ();
452 /* Shouldn't succeed */
453 if (egg_asn1x_get_string_as_utf8 (asn, NULL))
454 g_assert_not_reached ();
456 egg_asn1x_destroy (asn);
460 test_octet_string_bmp_as_utf8 (void)
466 const gchar SFUER[] = "\x04\x06""\x00\x46\x00\xfc\x00\x72";
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);
473 data = egg_asn1x_get_bmpstring_as_utf8 (asn);
474 g_assert_cmpstr (data, ==, "F\303\274r");
477 egg_asn1x_destroy (asn);
481 test_octet_string_get_as_bytes (void)
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);
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);
496 egg_asn1x_destroy (asn);
500 test_octet_string_set_as_bytes (void)
505 asn = egg_asn1x_create (test_asn1_tab, "TestOctetString");
506 g_assert (asn != NULL);
508 bytes = g_bytes_new_static ("farnsworth", 10);
509 egg_asn1x_set_string_as_bytes (asn, bytes);
510 g_bytes_unref (bytes);
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);
517 egg_asn1x_destroy (asn);
521 test_octet_string_structured (void)
528 const gchar STRUCTURED[] = "\x24\x0c"
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);
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);
541 egg_asn1x_destroy (asn);
545 test_octet_string_structured_bad (void)
552 const gchar STRUCTURED[] = "\x24\x0c"
553 "\x24\x04\x04\02""bl"
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);
560 string = egg_asn1x_get_string_as_raw (asn, NULL, &n_string);
561 g_assert (string == NULL);
563 egg_asn1x_destroy (asn);
567 test_generalized_time (void)
573 asn = egg_asn1x_create (test_asn1_tab, "TestGeneralized");
576 g_assert_cmpint (EGG_ASN1X_TIME, ==, egg_asn1x_type (asn));
578 /* Shouldn't succeed */
579 value = egg_asn1x_get_time_as_long (asn);
580 g_assert (value == -1);
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);
590 egg_asn1x_clear (asn);
592 /* Shouldn't succeed */
593 value = egg_asn1x_get_time_as_long (asn);
594 g_assert (value == -1);
596 egg_asn1x_destroy (asn);
600 test_time_get_missing (void)
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);
613 test_implicit_encode (void)
619 asn = egg_asn1x_create (test_asn1_tab, "TestImplicit");
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");
631 egg_asn1x_destroy (asn);
635 test_implicit_decode (void)
640 asn = egg_asn1x_create (test_asn1_tab, "TestImplicit");
643 if (!egg_asn1x_set_string_as_utf8 (asn, g_strdup ("implicit"), g_free))
644 g_assert_not_reached ();
646 bytes = egg_asn1x_encode (asn, NULL);
647 egg_assert_cmpbytes (bytes, ==, SIMPLICIT, XL (SIMPLICIT));
649 egg_asn1x_destroy (asn);
650 g_bytes_unref (bytes);
654 test_explicit_decode (void)
660 asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
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);
669 value = egg_asn1x_get_string_as_utf8 (asn, NULL);
670 g_assert_cmpstr (value, ==, "explicit");
673 egg_asn1x_destroy (asn);
677 test_explicit_no_context_specific (void)
682 const gchar DER[] = "\x45\x0A\x04\x08""explicit";
684 asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
685 g_assert (asn != NULL);
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);
693 egg_asn1x_destroy (asn);
697 test_explicit_no_context_child (void)
702 const gchar DER[] = "\xA5\x00";
704 asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
705 g_assert (asn != NULL);
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);
713 egg_asn1x_destroy (asn);
717 test_explicit_extra_context_child (void)
722 const gchar DER[] = "\xA5\x14"
724 "\x04\x08""explicit";
726 asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
727 g_assert (asn != NULL);
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);
735 egg_asn1x_destroy (asn);
739 test_explicit_encode (void)
744 asn = egg_asn1x_create (test_asn1_tab, "TestExplicit");
747 if (!egg_asn1x_set_string_as_utf8 (asn, g_strdup ("explicit"), g_free))
748 g_assert_not_reached ();
750 bytes = egg_asn1x_encode (asn, NULL);
751 egg_assert_cmpbytes (bytes, ==, SEXPLICIT, XL (SEXPLICIT));
753 egg_asn1x_destroy (asn);
754 g_bytes_unref (bytes);
758 test_universal_decode (void)
764 asn = egg_asn1x_create (test_asn1_tab, "TestUniversal");
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);
773 value = egg_asn1x_get_string_as_utf8 (asn, NULL);
774 g_assert_cmpstr (value, ==, "universal");
777 egg_asn1x_destroy (asn);
781 test_universal_encode (void)
786 asn = egg_asn1x_create (test_asn1_tab, "TestUniversal");
789 if (!egg_asn1x_set_string_as_utf8 (asn, g_strdup ("universal"), g_free))
790 g_assert_not_reached ();
792 bytes = egg_asn1x_encode (asn, NULL);
793 egg_assert_cmpbytes (bytes, ==, SUNIVERSAL, XL (SUNIVERSAL));
795 egg_asn1x_destroy (asn);
796 g_bytes_unref (bytes);
800 test_bit_string_decode (void)
808 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
811 g_assert_cmpint (EGG_ASN1X_BIT_STRING, ==, egg_asn1x_type (asn));
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);
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);
827 g_bytes_unref (bits);
828 egg_asn1x_destroy (asn);
832 test_bit_string_decode_bad (void)
837 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
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);
846 egg_asn1x_destroy (asn);
850 test_bit_string_decode_ulong (void)
857 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
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);
866 if (!egg_asn1x_get_bits_as_ulong (asn, &bits, &n_bits))
867 g_assert_not_reached ();
869 g_assert_cmpuint (n_bits, ==, 18);
870 g_assert_cmphex (bits, ==, 0x1b977);
872 egg_asn1x_destroy (asn);
876 test_bit_string_ulong_too_long (void)
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";
888 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
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);
898 if (egg_asn1x_get_bits_as_ulong (asn, &bits, &n_bits))
899 g_assert_not_reached ();
901 egg_asn1x_destroy (asn);
905 test_bit_string_get_not_set (void)
911 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
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);
917 egg_asn1x_destroy (asn);
921 test_bit_string_invalid_length (void)
926 const gchar DER[] = "\x03\x00";
928 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
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);
939 egg_asn1x_destroy (asn);
943 test_bit_string_invalid_empty (void)
948 const gchar DER[] = "\x03\x01\x09";
950 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
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);
961 egg_asn1x_destroy (asn);
965 test_bit_string_encode_decode (void)
969 guchar bits[] = { 0x5d, 0x6e, 0x83 };
976 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
979 bytes = g_bytes_new (bits, 3);
980 egg_asn1x_set_bits_as_raw (asn, bytes, n_bits);
981 g_bytes_unref (bytes);
983 data = egg_asn1x_encode (asn, NULL);
986 if (!egg_asn1x_decode (asn, data))
987 g_assert_not_reached ();
989 g_bytes_unref (data);
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);
999 g_bytes_unref (check);
1000 egg_asn1x_destroy (asn);
1004 test_bit_string_encode_decode_ulong (void)
1008 gulong check, bits = 0x0101b977;
1009 guint n_check, n_bits = 18;
1011 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
1014 egg_asn1x_set_bits_as_ulong (asn, bits, n_bits);
1015 data = egg_asn1x_encode (asn, NULL);
1018 if (!egg_asn1x_decode (asn, data))
1019 g_assert_not_reached ();
1021 g_bytes_unref (data);
1023 if (!egg_asn1x_get_bits_as_ulong (asn, &check, &n_check))
1024 g_assert_not_reached ();
1026 g_assert_cmpuint (n_check, ==, 18);
1027 g_assert_cmphex (check, ==, 0x1b977);
1029 egg_asn1x_destroy (asn);
1033 test_bit_string_encode_decode_zero (void)
1038 asn = egg_asn1x_create (test_asn1_tab, "TestBitString");
1041 egg_asn1x_take_bits_as_raw (asn, g_bytes_new_static ("", 0), 0);
1043 data = egg_asn1x_encode (asn, NULL);
1046 egg_assert_cmpmem (g_bytes_get_data (data, NULL), g_bytes_get_size (data), ==, BITS_ZERO, XL (BITS_ZERO));
1048 g_bytes_unref (data);
1049 egg_asn1x_destroy (asn);
1058 asn = egg_asn1x_create (test_asn1_tab, "TestBoolean");
1061 g_assert (!egg_asn1x_have (asn));
1063 egg_asn1x_set_boolean (asn, TRUE);
1065 g_assert (egg_asn1x_have (asn));
1067 data = egg_asn1x_encode (asn, NULL);
1070 g_assert (egg_asn1x_have (asn));
1072 g_bytes_unref (data);
1073 egg_asn1x_destroy (asn);
1076 static gboolean is_freed = FALSE;
1079 test_is_freed (gpointer unused)
1081 g_assert (!is_freed);
1093 /* ENCODED SEQUENCE ANY with OCTET STRING */
1094 const gchar SEQ_ENCODING[] = "\x30\x0C\x04\x0A""farnsworth";
1096 asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1100 node = egg_asn1x_node (asn, "contents", NULL);
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);
1109 data = egg_asn1x_encode (asn, NULL);
1110 g_assert (data != NULL);
1112 egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
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);
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);
1124 g_bytes_unref (data);
1125 egg_asn1x_destroy (asn);
1126 g_assert (is_freed);
1130 test_any_raw_explicit (void)
1136 /* ENCODED SEQUENCE [89] ANY with OCTET STRING */
1137 const gchar SEQ_ENCODING[] = "\x30\x0F\xBF\x59\x0C\x04\x0A""farnsworth";
1139 asn = egg_asn1x_create (test_asn1_tab, "TestAnyExp");
1143 node = egg_asn1x_node (asn, "contents", NULL);
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);
1151 data = egg_asn1x_encode (asn, NULL);
1152 g_assert (data != NULL);
1154 egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
1156 g_bytes_unref (data);
1157 egg_asn1x_destroy (asn);
1158 g_assert (is_freed);
1162 test_any_raw_invalid (void)
1167 const gchar TRUNCATED[] = "\x04\x0A""farns";
1169 asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1170 g_assert (asn != NULL);
1172 node = egg_asn1x_node (asn, "contents", NULL);
1173 g_assert (node != NULL);
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);
1181 egg_asn1x_destroy (asn);
1185 test_any_raw_not_set (void)
1190 asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1191 g_assert (asn != NULL);
1193 node = egg_asn1x_node (asn, "contents", NULL);
1194 g_assert (node != NULL);
1196 check = egg_asn1x_get_any_raw (node, NULL);
1197 g_assert (check == NULL);
1199 egg_asn1x_destroy (asn);
1203 test_any_into (void)
1211 /* ENCODED SEQUENCE ANY with OCTET STRING */
1212 const gchar SEQ_ENCODING[] = "\x30\x0C\x04\x0A""farnsworth";
1214 asn = egg_asn1x_create (test_asn1_tab, "TestAnySeq");
1215 g_assert (asn != NULL);
1218 node = egg_asn1x_node (asn, "contents", NULL);
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);
1227 egg_asn1x_set_any_from (node, part);
1228 egg_asn1x_destroy (part);
1230 data = egg_asn1x_encode (asn, NULL);
1231 g_assert (data != NULL);
1232 egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
1234 part = egg_asn1x_create (test_asn1_tab, "TestOctetString");
1235 if (!egg_asn1x_get_any_into (node, part))
1236 g_assert_not_reached ();
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);
1244 g_bytes_unref (data);
1245 egg_asn1x_destroy (asn);
1246 g_assert (is_freed);
1250 test_any_into_explicit (void)
1258 /* ENCODED SEQUENCE [89] ANY with OCTET STRING */
1259 const gchar SEQ_ENCODING[] = "\x30\x0F\xBF\x59\x0C\x04\x0A""farnsworth";
1261 asn = egg_asn1x_create (test_asn1_tab, "TestAnyExp");
1262 g_assert (asn != NULL);
1265 node = egg_asn1x_node (asn, "contents", NULL);
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);
1274 egg_asn1x_set_any_from (node, part);
1275 egg_asn1x_destroy (part);
1277 data = egg_asn1x_encode (asn, NULL);
1278 g_assert (data != NULL);
1279 egg_assert_cmpbytes (data, ==, SEQ_ENCODING, XL (SEQ_ENCODING));
1281 part = egg_asn1x_create (test_asn1_tab, "TestOctetString");
1282 if (!egg_asn1x_get_any_into (node, part))
1283 g_assert_not_reached ();
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);
1291 g_bytes_unref (data);
1292 egg_asn1x_destroy (asn);
1293 g_assert (is_freed);
1297 test_any_into_explicit_not_set (void)
1302 asn = egg_asn1x_create (test_asn1_tab, "TestAnyExp");
1303 g_assert (asn != NULL);
1305 node = egg_asn1x_node (asn, "contents", NULL);
1308 part = egg_asn1x_create (test_asn1_tab, "TestOctetString");
1309 if (egg_asn1x_get_any_into (node, part))
1310 g_assert_not_reached ();
1312 egg_asn1x_destroy (part);
1313 egg_asn1x_destroy (asn);
1317 test_choice_not_chosen (void)
1323 asn = egg_asn1x_create (test_asn1_tab, "TestAnyChoice");
1326 g_assert_cmpint (EGG_ASN1X_CHOICE, ==, egg_asn1x_type (asn));
1328 node = egg_asn1x_node (asn, "choiceShortTag", NULL);
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);
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);
1342 egg_asn1x_destroy (asn);
1346 perform_asn1_any_choice_set_raw (const gchar *choice, const gchar *encoding, gsize n_encoding)
1353 asn = egg_asn1x_create (test_asn1_tab, "TestAnyChoice");
1356 g_assert_cmpint (EGG_ASN1X_CHOICE, ==, egg_asn1x_type (asn));
1359 node = egg_asn1x_node (asn, choice, NULL);
1362 if (!egg_asn1x_set_choice (asn, node))
1363 g_assert_not_reached ();
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);
1370 data = egg_asn1x_encode (asn, NULL);
1372 g_printerr ("%s\n", egg_asn1x_message (asn));
1373 g_assert_not_reached ();
1375 g_assert (data != NULL);
1377 egg_assert_cmpbytes (data, ==, encoding, n_encoding);
1379 check = egg_asn1x_get_element_raw (node);
1380 g_assert (check != NULL);
1382 egg_assert_cmpbytes (check, ==, SFARNSWORTH, XL (SFARNSWORTH));
1384 g_bytes_unref (data);
1385 g_bytes_unref (check);
1386 egg_asn1x_destroy (asn);
1387 g_assert (is_freed);
1391 test_any_choice_set_raw_short_tag (void)
1393 const gchar ENCODING[] = "\xBE\x0C\x04\x0A""farnsworth";
1394 perform_asn1_any_choice_set_raw ("choiceShortTag", ENCODING, XL (ENCODING));
1398 test_any_choice_set_raw_long_tag (void)
1400 const gchar ENCODING[] = "\xBF\x1F\x0C\x04\x0A""farnsworth";
1401 perform_asn1_any_choice_set_raw ("choiceLongTag", ENCODING, XL (ENCODING));
1405 test_seq_of_any (void)
1413 const gchar DER[] = "\x30\x06"
1417 asn = egg_asn1x_create (test_asn1_tab, "TestSeqOfAny");
1418 g_assert (asn != NULL);
1420 egg_asn1x_append (asn);
1421 egg_asn1x_append (asn);
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);
1428 integer = egg_asn1x_create (test_asn1_tab, "TestInteger");
1429 g_assert (integer != NULL);
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);
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);
1443 egg_asn1x_destroy (integer);
1444 egg_asn1x_destroy (asn);
1448 test_seq_of_invalid (void)
1453 const gchar DER[] = "\x30\x05"
1457 asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
1458 g_assert (asn != NULL);
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);
1465 egg_asn1x_destroy (asn);
1469 test_seq_of_different (void)
1474 const gchar DER[] = "\x30\x05"
1478 asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
1479 g_assert (asn != NULL);
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);
1486 egg_asn1x_destroy (asn);
1490 test_set_order (void)
1495 const gchar DER[] = "\x31\x0f"
1496 "\xA2\x03\x02\x01\x99"
1497 "\xA1\x03\x02\x01\x88"
1498 "\xA3\x03\x02\x01\x88";
1500 asn = egg_asn1x_create (test_asn1_tab, "TestSet");
1501 g_assert (asn != NULL);
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);
1509 egg_asn1x_destroy (asn);
1520 /* SEQUENCE OF with one INTEGER = 1 */
1521 const gchar SEQOF_ONE[] = "\x30\x03\x02\x01\x01";
1523 /* SEQUENCE OF with two INTEGER = 1, 2 */
1524 const gchar SEQOF_TWO[] = "\x30\x06\x02\x01\x01\x02\x01\x02";
1526 bytes = g_bytes_new_static (SEQOF_ONE, XL (SEQOF_ONE));
1527 asn = egg_asn1x_create_and_decode (test_asn1_tab, "TestSeqOf", bytes);
1529 g_bytes_unref (bytes);
1531 g_assert_cmpint (EGG_ASN1X_SEQUENCE_OF, ==, egg_asn1x_type (asn));
1533 child = egg_asn1x_append (asn);
1536 /* Second integer is 2 */
1537 egg_asn1x_set_integer_as_ulong (child, 2);
1539 data = egg_asn1x_encode (asn, NULL);
1540 g_assert (data != NULL);
1542 egg_assert_cmpbytes (data, ==, SEQOF_TWO, XL (SEQOF_TWO));
1544 g_bytes_unref (data);
1545 egg_asn1x_destroy (asn);
1549 test_append_and_clear (void)
1554 asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
1557 g_assert_cmpuint (egg_asn1x_count (asn), ==, 0);
1559 egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 2);
1560 egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 3);
1562 g_assert_cmpuint (egg_asn1x_count (asn), ==, 2);
1564 data = egg_asn1x_encode (asn, NULL);
1565 g_assert (data != NULL);
1567 g_assert_cmpuint (egg_asn1x_count (asn), ==, 2);
1569 egg_asn1x_clear (asn);
1570 g_assert_cmpuint (egg_asn1x_count (asn), ==, 0);
1572 egg_asn1x_destroy (asn);
1573 g_bytes_unref (data);
1583 /* SEQUENCE OF with one INTEGER = 3 */
1584 const gchar SETOF_ONE[] = "\x31\x03\x02\x01\x03";
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";
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);
1594 g_assert_cmpint (EGG_ASN1X_SET_OF, ==, egg_asn1x_type (asn));
1596 /* Add integer 1, in SET OF DER should sort to front */
1597 egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 1);
1599 /* Add integer 8, in SET OF DER should sort to back */
1600 egg_asn1x_set_integer_as_ulong (egg_asn1x_append (asn), 8);
1602 data = egg_asn1x_encode (asn, NULL);
1604 g_printerr ("%s\n", egg_asn1x_message (asn));
1605 g_assert_not_reached ();
1608 egg_assert_cmpbytes (data, ==, SETOF_THREE, XL (SETOF_THREE));
1610 g_bytes_unref (data);
1611 egg_asn1x_destroy (asn);
1615 test_setof_empty (void)
1620 /* SEQUENCE OF with nothing */
1621 const gchar SETOF_NONE[] = "\x31\x00";
1623 asn = egg_asn1x_create (test_asn1_tab, "TestSetOf");
1626 data = egg_asn1x_encode (asn, NULL);
1628 g_printerr ("%s\n", egg_asn1x_message (asn));
1629 g_assert_not_reached ();
1632 egg_assert_cmpbytes (data, ==, SETOF_NONE, XL (SETOF_NONE));
1634 g_bytes_unref (data);
1635 egg_asn1x_destroy (asn);
1639 test_enumerated (void)
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);
1651 g_assert_cmpint (EGG_ASN1X_ENUMERATED, ==, egg_asn1x_type (asn));
1653 value = egg_asn1x_get_enumerated (asn);
1655 g_assert_cmpstr (g_quark_to_string (value), ==, "valueTwo");
1657 egg_asn1x_set_enumerated (asn, g_quark_from_static_string ("valueThree"));
1659 data = egg_asn1x_encode (asn, NULL);
1660 g_assert (data != NULL);
1662 egg_assert_cmpbytes (data, ==, ENUM_THREE, XL (ENUM_THREE));
1664 g_bytes_unref (data);
1665 egg_asn1x_destroy (asn);
1669 test_enumerated_decode_bad (void)
1671 const gchar ENUM_NEGATIVE[] = "\x0A\x01\x85";
1677 asn = egg_asn1x_create (test_asn1_tab, "TestEnumerated");
1678 g_assert (asn != NULL);
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);
1686 egg_asn1x_destroy (asn);
1690 test_enumerated_not_in_list (void)
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";
1702 asn = egg_asn1x_create (test_asn1_tab, "TestEnumerated");
1703 g_assert (asn != NULL);
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);
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);
1717 egg_asn1x_destroy (asn);
1721 test_enumerated_not_set (void)
1726 asn = egg_asn1x_create (test_asn1_tab, "TestEnumerated");
1727 g_assert (asn != NULL);
1729 value = egg_asn1x_get_enumerated (asn);
1730 g_assert (value == 0);
1732 egg_asn1x_destroy (asn);
1743 setup (Test *test, gconstpointer unused)
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 ();
1751 test->asn1 = egg_asn1x_create (pkix_asn1_tab, "Certificate");
1752 g_assert (test->asn1 != NULL);
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);
1761 teardown (Test *test, gconstpointer unused)
1763 egg_asn1x_destroy (test->asn1);
1764 g_free (test->data);
1768 test_node_name (Test* test, gconstpointer unused)
1770 g_assert_cmpstr (egg_asn1x_name (test->asn1), ==, "Certificate");
1774 test_asn1_integers (Test* test, gconstpointer unused)
1781 asn = egg_asn1x_create (test_asn1_tab, "TestIntegers");
1782 g_assert ("asn test structure is null" && asn != NULL);
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);
1788 /* Now encode the whole caboodle */
1789 data = egg_asn1x_encode (asn, NULL);
1790 g_assert ("encoding asn1 didn't work" && data != NULL);
1792 egg_asn1x_destroy (asn);
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);
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);
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);
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);
1811 egg_asn1x_destroy (asn);
1812 g_bytes_unref (data);
1816 test_boolean_seq (Test* test, gconstpointer unused)
1820 gboolean value, ret;
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";
1826 asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq");
1827 g_assert ("asn test structure is null" && asn != NULL);
1829 /* Get the default value */
1831 ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value);
1832 g_assert (ret == TRUE);
1833 g_assert (value == FALSE);
1835 egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), TRUE);
1836 egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean2", NULL), FALSE);
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);
1843 ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value);
1845 g_assert (value == TRUE);
1847 egg_asn1x_set_boolean (egg_asn1x_node (asn, "boolean", NULL), FALSE);
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));
1853 ret = egg_asn1x_get_boolean (egg_asn1x_node (asn, "boolean", NULL), &value);
1855 g_assert (value == FALSE);
1857 g_bytes_unref (data);
1858 egg_asn1x_destroy (asn);
1862 test_write_value (Test* test, gconstpointer unused)
1869 asn = egg_asn1x_create (test_asn1_tab, "TestData");
1870 g_assert ("asn test structure is null" && asn != NULL);
1872 egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL);
1874 encoded = egg_asn1x_encode (asn, NULL);
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);
1883 g_bytes_unref (encoded);
1884 egg_asn1x_destroy (asn);
1888 test_element_length_content (Test* test, gconstpointer unused)
1892 const guchar *content;
1896 asn = egg_asn1x_create (test_asn1_tab, "TestData");
1897 g_assert ("asn test structure is null" && asn != NULL);
1899 egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL);
1901 buffer = egg_asn1x_encode (asn, NULL);
1902 g_assert (buffer != NULL);
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);
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);
1914 content = egg_asn1x_element_content (content, n_content, &n_content);
1916 g_assert_cmpuint (n_content, ==, 9);
1917 g_assert (memcmp (content, "SOME DATA", 9) == 0);
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);
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);
1927 egg_asn1x_destroy (asn);
1928 g_bytes_unref (buffer);
1932 test_read_element (Test* test, gconstpointer unused)
1938 asn = egg_asn1x_create (test_asn1_tab, "TestData");
1939 g_assert ("asn test structure is null" && asn != NULL);
1941 egg_asn1x_set_string_as_raw (egg_asn1x_node (asn, "data", NULL), (guchar*)"SOME DATA", 9, NULL);
1943 buffer = egg_asn1x_encode (asn, NULL);
1944 g_assert (buffer != NULL);
1946 /* Have to decode before we can get raw elements */
1947 if (!egg_asn1x_decode (asn, buffer))
1948 g_assert_not_reached ();
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);
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);
1961 egg_asn1x_destroy (asn);
1962 g_bytes_unref (buffer);
1973 asn = egg_asn1x_create (test_asn1_tab, "TestOid");
1974 g_assert ("asn test structure is null" && asn != NULL);
1976 node = egg_asn1x_node (asn, "oid", NULL);
1977 g_assert_cmpint (EGG_ASN1X_OBJECT_ID, ==, egg_asn1x_type (node));
1979 if (!egg_asn1x_set_oid_as_string (node, "1.2.34567.89"))
1980 g_assert_not_reached ();
1982 buffer = egg_asn1x_encode (asn, NULL);
1983 g_assert (buffer != NULL);
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));
1989 g_assert (check == oid);
1990 g_assert_cmpstr (g_quark_to_string (oid), ==, "1.2.34567.89");
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 ();
1996 g_bytes_unref (buffer);
1997 buffer = egg_asn1x_encode (asn, NULL);
1998 g_assert (buffer != NULL);
2000 oid = egg_asn1x_get_oid_as_quark (egg_asn1x_node (asn, "oid", NULL));
2002 g_assert_cmpstr (g_quark_to_string (oid), ==, "5.4.3.2.1678");
2004 g_bytes_unref (buffer);
2005 egg_asn1x_destroy (asn);
2009 test_oid_set_invalid (void)
2013 asn = egg_asn1x_create (test_asn1_tab, "TestOid");
2014 g_assert ("asn test structure is null" && asn != NULL);
2016 if (egg_asn1x_set_oid_as_string (egg_asn1x_node (asn, "oid", NULL), "abcd"))
2017 g_assert_not_reached ();
2019 egg_asn1x_destroy (asn);
2023 test_oid_decode_bad (void)
2029 /* Has invalid leading integer in oid value */
2030 const gchar INVALID_OID[] = "\x30\x07\x06\x05\x2b\x80\x83\x82\x1a";
2032 asn = egg_asn1x_create (test_asn1_tab, "TestOid");
2033 g_assert ("asn test structure is null" && asn != NULL);
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);
2040 g_bytes_unref (bytes);
2041 egg_asn1x_destroy (asn);
2045 test_oid_get_no_value (void)
2050 asn = egg_asn1x_create (test_asn1_tab, "TestOid");
2051 g_assert ("asn test structure is null" && asn != NULL);
2053 oid = egg_asn1x_get_oid_as_string (egg_asn1x_node (asn, "oid", NULL));
2054 g_assert (oid == NULL);
2056 egg_asn1x_destroy (asn);
2059 typedef struct _TimeTestData {
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 },
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 */
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 },
2101 test_general_time (Test* test, gconstpointer unused)
2104 const TimeTestData *data;
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));
2115 g_assert ("decoded time doesn't match reference" && data->ref == when);
2120 test_utc_time (Test* test, gconstpointer unused)
2123 const TimeTestData *data;
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));
2134 g_assert ("decoded time doesn't match reference" && data->ref == when);
2139 test_read_time (Test* test, gconstpointer unused)
2143 time = egg_asn1x_get_time_as_long (egg_asn1x_node (test->asn1, "tbsCertificate", "validity", "notBefore", NULL));
2144 g_assert_cmpint (time, ==, 820454400);
2148 test_read_date (Test* test, gconstpointer unused)
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);
2159 test_create_by_oid (Test* test, gconstpointer unused)
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);
2169 test_create_by_oid_invalid (Test* test, gconstpointer unused)
2171 GNode *node = egg_asn1x_create (pkix_asn1_tab, "23.23.23.23");
2172 g_assert (node == NULL);
2176 test_create_by_bad_order (Test* test, gconstpointer unused)
2179 * In pkix.asn the definition for parts of this oid
2180 * come in the wrong order. However this should still work.
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);
2191 test_count (Test* test, gconstpointer unused)
2195 node = egg_asn1x_node (test->asn1, "tbsCertificate", "issuer", "rdnSequence", NULL);
2197 g_assert_cmpuint (egg_asn1x_count (node), ==, 7);
2201 test_nested_fails_with_extra (void)
2207 const gchar SEQ_NESTED[] = "\x30\x0C"
2211 asn = egg_asn1x_create (test_asn1_tab, "TestData");
2212 g_assert ("asn test structure is null" && asn != NULL);
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);
2220 egg_asn1x_destroy (asn);
2224 test_nested_unexpected (void)
2230 const gchar SEQ_NESTED[] = "\x30\x03"
2233 asn = egg_asn1x_create (test_asn1_tab, "TestData");
2234 g_assert ("asn test structure is null" && asn != NULL);
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);
2242 egg_asn1x_destroy (asn);
2246 test_create_and_decode_invalid (void)
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);
2258 test_decode_extra (void)
2263 asn = egg_asn1x_create (test_asn1_tab, "TestSeqOf");
2264 g_assert (asn != NULL);
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);
2275 test_decode_nested_short (void)
2280 asn = egg_asn1x_create (test_asn1_tab, "TestSeqOfAny");
2281 g_assert (asn != NULL);
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);
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);
2295 egg_asn1x_destroy (asn);
2299 test_decode_indefinite_primitive (void)
2304 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
2305 g_assert (asn != NULL);
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);
2313 egg_asn1x_destroy (asn);
2317 test_decode_invalid_long_length (void)
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);
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);
2334 egg_asn1x_destroy (asn);
2338 test_decode_truncated_at_tag (void)
2343 const gchar DER[] = "\x04";
2344 asn = egg_asn1x_create (test_asn1_tab, "TestInteger");
2345 g_assert (asn != NULL);
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);
2353 egg_asn1x_destroy (asn);
2357 test_decode_long_tag (void)
2363 const gchar DER[] = "\xbf\x89\x52\x03\x04\x01\x33";
2365 asn = egg_asn1x_create (test_asn1_tab, "TestTagLong");
2366 g_assert (asn != NULL);
2368 bytes = g_bytes_new_static (DER, XL (DER));
2369 ret = egg_asn1x_decode (asn, bytes);
2370 egg_asn1x_assert (ret == TRUE, asn);
2372 g_bytes_unref (bytes);
2373 egg_asn1x_destroy (asn);
2378 test_create_quark (void)
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");
2388 test_validate_default (void)
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);
2401 test_validate_missing (void)
2405 asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq");
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);
2414 test_validate_seq_of_child_invalid (void)
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);
2431 test_validate_optional_seq (void)
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);
2442 test_element_get_not_set (void)
2446 asn = egg_asn1x_create (test_asn1_tab, "TestBooleanSeq");
2447 g_assert (egg_asn1x_get_element_raw (asn) == NULL);
2448 egg_asn1x_destroy (asn);
2452 main (int argc, char **argv)
2454 g_test_init (&argc, &argv, NULL);
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);
2554 return g_test_run ();