egg-asn1x: More complete coverage for ASN.1 tests
[platform/upstream/gcr.git] / egg / tests / test-asn1x.c
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    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.
20
21    Author: Stef Walter <stef@memberwebs.com>
22 */
23
24 #include "config.h"
25
26 #include "egg/egg-asn1x.h"
27 #include "egg/egg-asn1-defs.h"
28 #include "egg/egg-testing.h"
29
30 #include <pwd.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #if 0
35 #include <libtasn1.h>
36 static void
37 build_personal_name (void)
38 {
39         ASN1_TYPE asn1_pkix = NULL, asn;
40         guchar buffer[10024];
41         int res, len;
42
43         res = asn1_array2tree ((ASN1_ARRAY_TYPE*)pkix_asn1_tab, &asn1_pkix, NULL);
44         g_assert (res == ASN1_SUCCESS);
45
46         res = asn1_create_element (asn1_pkix, "PKIX1.PersonalName", &asn);
47         g_assert (res == ASN1_SUCCESS);
48
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);
53
54         len = sizeof (buffer);
55         res = asn1_der_coding (asn, "", buffer, &len, NULL);
56         g_assert (res == ASN1_SUCCESS);
57
58         asn1_delete_structure (&asn);
59         asn1_delete_structure (&asn1_pkix);
60
61         if (!g_file_set_contents ("/tmp/personal-name.der", (gchar*)buffer, len, NULL))
62                 g_assert (FALSE);
63
64 }
65 #endif
66
67 typedef struct {
68         GBytes *data;
69 } Test;
70
71 typedef struct {
72         const EggAsn1xDef *defs;
73         const gchar *filename;
74         const gchar *identifier;
75 } Fixture;
76
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" },
83 };
84
85 static void
86 setup (Test *test,
87        gconstpointer data)
88 {
89         const gchar *filename = data;
90         GError *error = NULL;
91         gchar *contents;
92         gsize length;
93
94         g_file_get_contents (filename, (gchar**)&contents, &length, &error);
95         g_assert_no_error (error);
96
97         test->data = g_bytes_new_take (contents, length);
98 }
99
100 static void
101 setup_parsing (Test *test,
102                gconstpointer data)
103 {
104         const Fixture *fixture = data;
105         setup (test, fixture->filename);
106 }
107
108 static void
109 teardown (Test *test,
110           gconstpointer unused)
111 {
112         g_bytes_unref (test->data);
113 }
114
115 static void
116 test_decode_encode (Test *test,
117                     gconstpointer data)
118 {
119         const Fixture *fixture = data;
120         GNode *asn;
121         GBytes *encoded;
122         gboolean ret;
123
124         asn = egg_asn1x_create (fixture->defs, fixture->identifier);
125
126         if (g_test_verbose ())
127                 egg_asn1x_dump (asn);
128
129         ret = egg_asn1x_decode (asn, test->data);
130         egg_asn1x_assert (ret == TRUE, asn);
131
132         encoded = egg_asn1x_encode (asn, NULL);
133         egg_asn1x_assert (encoded != NULL, asn);
134
135         /* Decode the encoding */
136         ret = egg_asn1x_decode (asn, encoded);
137         egg_asn1x_assert (ret == TRUE, asn);
138
139         egg_asn1x_clear (asn);
140         egg_asn1x_destroy (asn);
141         g_bytes_unref (encoded);
142 }
143
144 static void
145 test_personal_name_invalid (Test *test,
146                             gconstpointer unused)
147 {
148         GNode *asn;
149         gboolean ret;
150
151         asn = egg_asn1x_create (pkix_asn1_tab, "PersonalName");
152
153         if (g_test_verbose ())
154                 egg_asn1x_dump (asn);
155
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);
159
160         egg_asn1x_destroy (asn);
161 }
162
163 static void
164 test_pkcs12_decode (Test *test,
165                     gconstpointer unused)
166 {
167         GNode *asn;
168         gboolean ret;
169
170         asn = egg_asn1x_create (pkix_asn1_tab, "pkcs-12-PFX");
171
172         if (g_test_verbose ())
173                 egg_asn1x_dump (asn);
174
175         ret = egg_asn1x_decode (asn, test->data);
176         egg_asn1x_assert (ret == TRUE, asn);
177
178         egg_asn1x_destroy (asn);
179 }
180
181 int
182 main (int argc, char **argv)
183 {
184         gchar *name;
185         gint i;
186
187         g_test_init (&argc, &argv, NULL);
188
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);
192                 g_free (name);
193         }
194
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);
199
200         return g_test_run ();
201 }