1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-asn1.c: Test ASN1 stuf
4 Copyright (C) 2009 Stefan Walter
6 The Gnome Keyring Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The Gnome Keyring Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the Gnome Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Author: Stef Walter <stef@memberwebs.com>
26 #include "egg/egg-asn1x.h"
27 #include "egg/egg-asn1-defs.h"
28 #include "egg/egg-testing.h"
37 build_personal_name (void)
39 ASN1_TYPE asn1_pkix = NULL, asn;
43 res = asn1_array2tree ((ASN1_ARRAY_TYPE*)pkix_asn1_tab, &asn1_pkix, NULL);
44 g_assert (res == ASN1_SUCCESS);
46 res = asn1_create_element (asn1_pkix, "PKIX1.PersonalName", &asn);
47 g_assert (res == ASN1_SUCCESS);
49 asn1_write_value (asn, "surname", "Turanga", 7);
50 asn1_write_value (asn, "given-name", "Leela", 5);
51 asn1_write_value (asn, "initials", NULL, 0);
52 asn1_write_value (asn, "generation-qualifier", "II", 2);
54 len = sizeof (buffer);
55 res = asn1_der_coding (asn, "", buffer, &len, NULL);
56 g_assert (res == ASN1_SUCCESS);
58 asn1_delete_structure (&asn);
59 asn1_delete_structure (&asn1_pkix);
61 if (!g_file_set_contents ("/tmp/personal-name.der", (gchar*)buffer, len, NULL))
72 const EggAsn1xDef *defs;
73 const gchar *filename;
74 const gchar *identifier;
77 static const Fixture parse_test_fixtures[] = {
78 { pkix_asn1_tab, SRCDIR "/files/test-certificate-1.der", "Certificate" },
79 { pkix_asn1_tab, SRCDIR "/files/test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo" },
80 { pk_asn1_tab, SRCDIR "/files/test-rsakey-1.der", "RSAPrivateKey" },
81 { pkix_asn1_tab, SRCDIR "/files/test-pkcs7-1.der", "pkcs-7-ContentInfo" },
82 { pkix_asn1_tab, SRCDIR "/files/test-pkcs7-2.der", "pkcs-7-ContentInfo" },
89 const gchar *filename = data;
94 g_file_get_contents (filename, (gchar**)&contents, &length, &error);
95 g_assert_no_error (error);
97 test->data = g_bytes_new_take (contents, length);
101 setup_parsing (Test *test,
104 const Fixture *fixture = data;
105 setup (test, fixture->filename);
109 teardown (Test *test,
110 gconstpointer unused)
112 g_bytes_unref (test->data);
116 test_decode_encode (Test *test,
119 const Fixture *fixture = data;
124 asn = egg_asn1x_create (fixture->defs, fixture->identifier);
126 if (g_test_verbose ())
127 egg_asn1x_dump (asn);
129 ret = egg_asn1x_decode (asn, test->data);
130 egg_asn1x_assert (ret == TRUE, asn);
132 encoded = egg_asn1x_encode (asn, NULL);
133 egg_asn1x_assert (encoded != NULL, asn);
135 /* Decode the encoding */
136 ret = egg_asn1x_decode (asn, encoded);
137 egg_asn1x_assert (ret == TRUE, asn);
139 egg_asn1x_clear (asn);
140 egg_asn1x_destroy (asn);
141 g_bytes_unref (encoded);
145 test_personal_name_invalid (Test *test,
146 gconstpointer unused)
151 asn = egg_asn1x_create (pkix_asn1_tab, "PersonalName");
153 if (g_test_verbose ())
154 egg_asn1x_dump (asn);
156 ret = egg_asn1x_decode (asn, test->data);
157 g_assert (ret == FALSE);
158 g_assert (strstr (egg_asn1x_message (asn), "content size is out of bounds") != NULL);
160 egg_asn1x_destroy (asn);
164 test_pkcs12_decode (Test *test,
165 gconstpointer unused)
170 asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-12-PFX");
172 if (g_test_verbose ())
173 egg_asn1x_dump (asn);
175 ret = egg_asn1x_decode (asn, test->data);
176 egg_asn1x_assert (ret == TRUE, asn);
178 egg_asn1x_destroy (asn);
182 main (int argc, char **argv)
187 g_test_init (&argc, &argv, NULL);
189 for (i = 0; i < G_N_ELEMENTS (parse_test_fixtures); i++) {
190 name = g_strdup_printf ("/asn1x/encode-decode-%s", parse_test_fixtures[i].identifier);
191 g_test_add (name, Test, &parse_test_fixtures[i], setup_parsing, test_decode_encode, teardown);
195 g_test_add ("/asn1x/pkcs12-decode/1", Test, SRCDIR "/files/test-pkcs12-1.der",
196 setup, test_pkcs12_decode, teardown);
197 g_test_add ("/asn1x/pkcs5-personal-name/invalid", Test, SRCDIR "/files/test-personalname-invalid.der",
198 setup, test_personal_name_invalid, teardown);
200 return g_test_run ();