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