Debian Codegraph @

pkcs11/gnome2-store/test-import.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-file-store.c: Test file store functionality
3
4 Copyright (C) 2008 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 <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "egg/egg-testing.h"
26
27 #include "gkm/gkm-module.h"
28 #include "gkm/gkm-test.h"
29
30 #include "gnome2-store/gkm-gnome2-store.h"
31
32 #include <gck/gck.h>
33 #include <gcr/gcr-base.h>
34
35 #include <p11-kit/p11-kit.h>
36
37 #include <glib/gstdio.h>
38
39 #include <string.h>
40
41 typedef struct {
42 CK_FUNCTION_LIST_PTR funcs;
43 GList *importers;
44 gchar *directory;
45 } Test;
46
47 static void
48 setup (Test *test,
49 gconstpointer unused)
50 {
51 CK_C_INITIALIZE_ARGS args;
52 CK_SESSION_HANDLE session;
53 GckModule *module;
54 GList *modules;
55 CK_RV rv;
56
57 test->directory = egg_tests_create_scratch_directory (NULL, NULL);
58
59 memset (&args, 0, sizeof (args));
60 args.flags = CKF_OS_LOCKING_OK;
61 args.pReserved = g_strdup_printf ("directory='%s'", test->directory);
62
63 test->funcs = gkm_gnome2_store_get_functions ();
64 rv = (test->funcs->C_Initialize) (&args);
65 g_free (args.pReserved);
66 gkm_assert_cmprv (rv, ==, CKR_OK);
67
68 /* And now need to log in */
69 rv = (test->funcs->C_OpenSession) (GKM_SLOT_ID, CKF_SERIAL_SESSION | CKF_RW_SESSION,
70 NULL, NULL, &session);
71 gkm_assert_cmprv (rv, ==, CKR_OK);
72
73 /* The directory is empty, so we need to initialize */
74 rv = (test->funcs->C_SetPIN) (session, NULL, 0, (CK_BYTE_PTR)"mypin", 5);
75 gkm_assert_cmprv (rv, ==, CKR_OK);
76
77 /* Login so the importer doesn't have to */
78 rv = (test->funcs->C_Login) (session, CKU_USER, (CK_BYTE_PTR)"mypin", 5);
79 gkm_assert_cmprv (rv, ==, CKR_OK);
80
81 module = gck_module_new (test->funcs);
82 modules = g_list_prepend (NULL, module);
83 gcr_pkcs11_set_modules (modules);
84 g_list_free (modules);
85 g_object_unref (module);
86 }
87
88 static void
89 teardown (Test *test,
90 gconstpointer unused)
91 {
92 CK_RV rv;
93
94 g_list_free_full (test->importers, g_object_unref);
95
96 gcr_pkcs11_set_modules (NULL);
97
98 rv = (test->funcs->C_Finalize) (NULL);
99 gkm_assert_cmprv (rv, ==, CKR_OK);
100
101 /* Cleanup the directory */
102 egg_tests_remove_scratch_directory (test->directory);
103 g_free (test->directory);
104 }
105
106 static void
107 on_parser_parsed (GcrParser *parser,
108 gpointer user_data)
109 {
110 Test *test = user_data;
111 GcrParsed *parsed;
112 GList *importers;
113
114 parsed = gcr_parser_get_parsed (parser);
115
116 if (test->importers == NULL)
117 importers = gcr_importer_create_for_parsed (parsed);
118 else
119 importers = gcr_importer_queue_and_filter_for_parsed (test->importers, parsed);
120
121 g_list_free_full (test->importers, g_object_unref);
122 test->importers = importers;
123 }
124
125 static void
126 test_pkcs12_import (Test *test,
127 gconstpointer unused)
128 {
129 GcrParser *parser;
130 GError *error;
131 gchar *contents;
132 gsize length;
133 GList *l;
134
135 error = NULL;
136 g_file_get_contents (SRCDIR "/pkcs11/gnome2-store/fixtures/personal.p12", &contents, &length, &error);
137 g_assert_no_error (error);
138
139 /* Parse the pkcs12 file */
140 parser = gcr_parser_new ();
141 gcr_parser_add_password (parser, "booo");
142 gcr_parser_format_enable (parser, GCR_FORMAT_DER_PKCS12);
143 g_signal_connect (parser, "parsed", G_CALLBACK (on_parser_parsed), test);
144 gcr_parser_parse_data (parser, (const guchar *)contents, length, &error);
145 g_assert_no_error (error);
146 g_object_unref (parser);
147 g_free (contents);
148
149 /* Should have found importers */
150 g_assert (test->importers != NULL);
151
152 for (l = test->importers; l != NULL; l = g_list_next (l)) {
153 gcr_importer_import (l->data, NULL, &error);
154 g_assert_no_error (error);
155 }
156 }
157
158 static void
159 null_log_handler (const gchar *log_domain,
160 GLogLevelFlags log_level,
161 const gchar *message,
162 gpointer user_data)
163 {
164
165 }
166
167 int
168 main (int argc, char **argv)
169 {
170 #if !GLIB_CHECK_VERSION(2,35,0)
171 g_type_init ();
172 #endif
173 g_test_init (&argc, &argv, NULL);
174
175 g_set_prgname ("test-import");
176
177 /* Suppress these messages in tests */
178 g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
179 null_log_handler, NULL);
180
181 g_test_add ("/gnome2-store/import/pkcs12", Test, NULL,
182 setup, test_pkcs12_import, teardown);
183
184 return g_test_run ();
185 }