Debian Codegraph @

pkcs11/wrap-layer/test-create-credential.c

raw
1 /*
2 * gnome-keyring
3 *
4 * Copyright (C) 2010 Stefan Walter
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "wrap-layer/gkm-wrap-layer.h"
24 #include "wrap-layer/gkm-wrap-prompt.h"
25
26 #include "gkm/gkm-mock.h"
27 #include "gkm/gkm-test.h"
28
29 #include "egg/egg-testing.h"
30
31 #include <gcr/gcr-base.h>
32
33 #include <glib-object.h>
34
35 #include <string.h>
36
37 typedef struct {
38 CK_FUNCTION_LIST functions;
39 CK_FUNCTION_LIST_PTR module;
40 CK_SESSION_HANDLE session;
41 CK_OBJECT_HANDLE object;
42 } Test;
43
44 static void
45 setup (Test *test, gconstpointer unused)
46 {
47 CK_FUNCTION_LIST_PTR funcs;
48 CK_SLOT_ID slot_id;
49 CK_ULONG n_slots = 1;
50 const gchar *prompter;
51 CK_ULONG count;
52 CK_RV rv;
53
54 CK_BBOOL always = TRUE;
55 CK_ATTRIBUTE attrs[] = {
56 { CKA_ALWAYS_AUTHENTICATE, &always, sizeof (always) }
57 };
58
59 /* Always start off with test functions */
60 rv = gkm_mock_C_GetFunctionList (&funcs);
61 gkm_assert_cmprv (rv, ==, CKR_OK);
62 memcpy (&test->functions, funcs, sizeof (test->functions));
63
64 gkm_wrap_layer_reset_modules ();
65 gkm_wrap_layer_add_module (&test->functions);
66 test->module = gkm_wrap_layer_get_functions ();
67
68 prompter = gcr_mock_prompter_start ();
69 gkm_wrap_prompt_set_prompter_name (prompter);
70
71 /* Open a test->session */
72 rv = (test->module->C_Initialize) (NULL);
73 gkm_assert_cmprv (rv, ==, CKR_OK);
74
75 rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
76 gkm_assert_cmprv (rv, ==, CKR_OK);
77
78 rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session);
79 gkm_assert_cmprv (rv, ==, CKR_OK);
80
81 /* Find the always authenticate test->object */
82 rv = (test->module->C_FindObjectsInit) (test->session, attrs, 1);
83 gkm_assert_cmprv (rv, ==, CKR_OK);
84
85 rv = (test->module->C_FindObjects) (test->session, &test->object, 1, &count);
86 gkm_assert_cmprv (rv, ==, CKR_OK);
87 gkm_assert_cmpulong (count, ==, 1);
88 gkm_assert_cmpulong (test->object, !=, 0);
89
90 rv = (test->module->C_FindObjectsFinal) (test->session);
91 gkm_assert_cmprv (rv, ==, CKR_OK);
92 }
93
94 static void
95 teardown (Test *test, gconstpointer unused)
96 {
97 CK_RV rv;
98
99 g_assert (!gcr_mock_prompter_is_expecting ());
100 gcr_mock_prompter_stop ();
101
102 rv = (test->module->C_CloseSession) (test->session);
103 gkm_assert_cmprv (rv, ==, CKR_OK);
104
105 rv = (test->module->C_Finalize) (NULL);
106 gkm_assert_cmprv (rv, ==, CKR_OK);
107 }
108
109 static void
110 test_ok_password (Test *test, gconstpointer unused)
111 {
112 CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
113 CK_ATTRIBUTE attrs[] = {
114 { CKA_CLASS, &klass, sizeof (klass) },
115 { CKA_G_OBJECT, &test->object, sizeof (test->object) },
116 { CKA_VALUE, NULL, 0 }
117 };
118
119 CK_OBJECT_HANDLE cred = 0;
120 CK_RV rv;
121
122 gcr_mock_prompter_expect_password_ok ("booo", NULL);
123
124 rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred);
125 gkm_assert_cmprv (rv, ==, CKR_OK);
126 gkm_assert_cmpulong (cred, !=, 0);
127 }
128
129 static void
130 test_bad_password_then_cancel (Test *test, gconstpointer unused)
131 {
132 CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
133 CK_ATTRIBUTE attrs[] = {
134 { CKA_CLASS, &klass, sizeof (klass) },
135 { CKA_G_OBJECT, &test->object, sizeof (test->object) },
136 { CKA_VALUE, NULL, 0 }
137 };
138
139 CK_OBJECT_HANDLE cred = 0;
140 CK_RV rv;
141
142 gcr_mock_prompter_expect_password_ok ("bad password", NULL);
143 gcr_mock_prompter_expect_password_cancel ();
144
145 rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred);
146 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
147 }
148
149 static void
150 test_cancel_immediately (Test *test, gconstpointer unused)
151 {
152 CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL;
153 CK_ATTRIBUTE attrs[] = {
154 { CKA_CLASS, &klass, sizeof (klass) },
155 { CKA_G_OBJECT, &test->object, sizeof (test->object) },
156 { CKA_VALUE, NULL, 0 }
157 };
158
159 CK_OBJECT_HANDLE cred = 0;
160 CK_RV rv;
161
162 gcr_mock_prompter_expect_password_cancel ();
163
164 rv = (test->module->C_CreateObject) (test->session, attrs, G_N_ELEMENTS (attrs), &cred);
165 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
166 }
167
168 int
169 main (int argc, char **argv)
170 {
171 #if !GLIB_CHECK_VERSION(2,35,0)
172 g_type_init ();
173 #endif
174 g_test_init (&argc, &argv, NULL);
175
176 g_test_add ("/wrap-layer/create-credential/ok_password", Test, NULL, setup, test_ok_password, teardown);
177 g_test_add ("/wrap-layer/create-credential/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown);
178 g_test_add ("/wrap-layer/create-credential/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown);
179
180 return egg_tests_run_in_thread_with_loop ();
181 }