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