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 <pwd.h>
30 : #include <stdlib.h>
31 : #include <unistd.h>
32 :
33 : #if 0
34 : #include <libtasn1.h>
35 : static void
36 : build_personal_name (void)
37 : {
38 : ASN1_TYPE asn1_pkix = NULL, asn;
39 : guchar buffer[10024];
40 : int res, len;
41 :
42 : res = asn1_array2tree ((ASN1_ARRAY_TYPE*)pkix_asn1_tab, &asn1_pkix, NULL);
43 : g_assert (res == ASN1_SUCCESS);
44 :
45 : res = asn1_create_element (asn1_pkix, "PKIX1.PersonalName", &asn);
46 : g_assert (res == ASN1_SUCCESS);
47 :
48 : asn1_write_value (asn, "surname", "Turanga", 7);
49 : asn1_write_value (asn, "given-name", "Leela", 5);
50 : asn1_write_value (asn, "initials", NULL, 0);
51 : asn1_write_value (asn, "generation-qualifier", "II", 2);
52 :
53 : len = sizeof (buffer);
54 : res = asn1_der_coding (asn, "", buffer, &len, NULL);
55 : g_assert (res == ASN1_SUCCESS);
56 :
57 : asn1_delete_structure (&asn);
58 : asn1_delete_structure (&asn1_pkix);
59 :
60 : if (!g_file_set_contents ("/tmp/personal-name.der", (gchar*)buffer, len, NULL))
61 : g_assert (FALSE);
62 :
63 : }
64 : #endif
65 :
66 : typedef struct {
67 : GBytes *data;
68 : } Test;
69 :
70 : typedef struct {
71 : const EggAsn1xDef *defs;
72 : const gchar *filename;
73 : const gchar *identifier;
74 : } Fixture;
75 :
76 : static const Fixture parse_test_fixtures[] = {
77 : { pkix_asn1_tab, SRCDIR "/egg/fixtures/test-certificate-1.der", "Certificate" },
78 : { pkix_asn1_tab, SRCDIR "/egg/fixtures/test-pkcs8-1.der", "pkcs-8-PrivateKeyInfo" },
79 : { pk_asn1_tab, SRCDIR "/egg/fixtures/test-rsakey-1.der", "RSAPrivateKey" },
80 : { pkix_asn1_tab, SRCDIR "/egg/fixtures/test-pkcs7-1.der", "pkcs-7-ContentInfo" },
81 : { pkix_asn1_tab, SRCDIR "/egg/fixtures/test-pkcs7-2.der", "pkcs-7-ContentInfo" },
82 : };
83 :
84 : static void
85 7 : setup (Test *test,
86 : gconstpointer data)
87 : {
88 7 : const gchar *filename = data;
89 7 : GError *error = NULL;
90 : gchar *contents;
91 : gsize length;
92 :
93 7 : g_file_get_contents (filename, (gchar**)&contents, &length, &error);
94 7 : g_assert_no_error (error);
95 :
96 7 : test->data = g_bytes_new_take (contents, length);
97 7 : }
98 :
99 : static void
100 5 : setup_parsing (Test *test,
101 : gconstpointer data)
102 : {
103 5 : const Fixture *fixture = data;
104 5 : setup (test, fixture->filename);
105 5 : }
106 :
107 : static void
108 7 : teardown (Test *test,
109 : gconstpointer unused)
110 : {
111 7 : g_bytes_unref (test->data);
112 7 : }
113 :
114 : static void
115 5 : test_decode_encode (Test *test,
116 : gconstpointer data)
117 : {
118 5 : const Fixture *fixture = data;
119 : GNode *asn;
120 : GBytes *encoded;
121 : gboolean ret;
122 :
123 5 : asn = egg_asn1x_create (fixture->defs, fixture->identifier);
124 :
125 5 : if (g_test_verbose ())
126 0 : egg_asn1x_dump (asn);
127 :
128 5 : ret = egg_asn1x_decode (asn, test->data);
129 5 : egg_asn1x_assert (ret == TRUE, asn);
130 :
131 5 : encoded = egg_asn1x_encode (asn, NULL);
132 5 : egg_asn1x_assert (encoded != NULL, asn);
133 :
134 : /* Decode the encoding */
135 5 : ret = egg_asn1x_decode (asn, encoded);
136 5 : egg_asn1x_assert (ret == TRUE, asn);
137 :
138 5 : egg_asn1x_clear (asn);
139 5 : egg_asn1x_destroy (asn);
140 5 : g_bytes_unref (encoded);
141 5 : }
142 :
143 : static void
144 1 : test_personal_name_invalid (Test *test,
145 : gconstpointer unused)
146 : {
147 : GNode *asn;
148 : gboolean ret;
149 :
150 1 : asn = egg_asn1x_create (pkix_asn1_tab, "PersonalName");
151 :
152 1 : if (g_test_verbose ())
153 0 : egg_asn1x_dump (asn);
154 :
155 1 : ret = egg_asn1x_decode (asn, test->data);
156 1 : g_assert (ret == FALSE);
157 1 : g_assert (strstr (egg_asn1x_message (asn), "content size is out of bounds") != NULL);
158 :
159 1 : egg_asn1x_destroy (asn);
160 1 : }
161 :
162 : static void
163 1 : test_pkcs12_decode (Test *test,
164 : gconstpointer unused)
165 : {
166 : GNode *asn;
167 : gboolean ret;
168 :
169 1 : asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-12-PFX");
170 :
171 1 : if (g_test_verbose ())
172 0 : egg_asn1x_dump (asn);
173 :
174 1 : ret = egg_asn1x_decode (asn, test->data);
175 1 : egg_asn1x_assert (ret == TRUE, asn);
176 :
177 1 : egg_asn1x_destroy (asn);
178 1 : }
179 :
180 : int
181 1 : main (int argc, char **argv)
182 : {
183 : gchar *name;
184 : gint i;
185 :
186 1 : g_test_init (&argc, &argv, NULL);
187 :
188 6 : for (i = 0; i < G_N_ELEMENTS (parse_test_fixtures); i++) {
189 5 : name = g_strdup_printf ("/asn1x/encode-decode-%d-%s", i, parse_test_fixtures[i].identifier);
190 5 : g_test_add (name, Test, &parse_test_fixtures[i], setup_parsing, test_decode_encode, teardown);
191 5 : g_free (name);
192 : }
193 :
194 1 : g_test_add ("/asn1x/pkcs12-decode/1", Test, SRCDIR "/egg/fixtures/test-pkcs12-1.der",
195 : setup, test_pkcs12_decode, teardown);
196 1 : g_test_add ("/asn1x/pkcs5-personal-name/invalid", Test, SRCDIR "/egg/fixtures/test-personalname-invalid.der",
197 : setup, test_personal_name_invalid, teardown);
198 :
199 1 : return g_test_run ();
200 : }
|