Debian Codegraph @

pkcs11/wrap-layer/test-login-specific.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 "egg/egg-testing.h"
27
28 #include "gkm/gkm-mock.h"
29 #include "gkm/gkm-test.h"
30
31 #include <gcr/gcr-base.h>
32
33 #include <glib-object.h>
34
35 typedef struct {
36 CK_FUNCTION_LIST prompt_login_functions;
37 CK_FUNCTION_LIST_PTR module;
38 CK_SESSION_HANDLE session;
39 } Test;
40
41 static void
42 setup (Test *test, gconstpointer unused)
43 {
44 CK_FUNCTION_LIST_PTR funcs;
45 CK_OBJECT_HANDLE key;
46 CK_SLOT_ID slot_id;
47 CK_ULONG n_slots = 1;
48 const gchar *prompter;
49 CK_ULONG count;
50 CK_RV rv;
51
52 CK_BBOOL always = TRUE;
53 CK_ATTRIBUTE attrs[] = {
54 { CKA_ALWAYS_AUTHENTICATE, &always, sizeof (always) }
55 };
56
57 CK_MECHANISM mech = { CKM_MOCK_PREFIX, NULL, 0 };
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->prompt_login_functions, funcs, sizeof (test->prompt_login_functions));
63
64 gkm_wrap_layer_reset_modules ();
65 gkm_wrap_layer_add_module (&test->prompt_login_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 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, &key, 1, &count);
86 gkm_assert_cmprv (rv, ==, CKR_OK);
87 gkm_assert_cmpulong (count, ==, 1);
88 gkm_assert_cmpulong (key, !=, 0);
89
90 rv = (test->module->C_FindObjectsFinal) (test->session);
91 gkm_assert_cmprv (rv, ==, CKR_OK);
92
93 /* Start a signing operation, that needs to be authenticated */
94 rv = (test->module->C_SignInit) (test->session, &mech, key);
95 gkm_assert_cmprv (rv, ==, CKR_OK);
96 }
97
98 static void
99 teardown (Test *test, gconstpointer unused)
100 {
101 CK_RV rv;
102
103 g_assert (!gcr_mock_prompter_is_expecting ());
104 gcr_mock_prompter_stop ();
105
106 rv = (test->module->C_CloseSession) (test->session);
107 gkm_assert_cmprv (rv, ==, CKR_OK);
108 test->session = 0;
109
110 rv = (test->module->C_Finalize) (NULL);
111 gkm_assert_cmprv (rv, ==, CKR_OK);
112 test->module = NULL;
113 }
114
115 static void
116 test_ok_password (Test *test, gconstpointer unused)
117 {
118 CK_RV rv;
119
120 gcr_mock_prompter_expect_password_ok ("booo", NULL);
121
122 rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
123 gkm_assert_cmprv (rv, ==, CKR_OK);
124 }
125
126 static void
127 test_bad_password_then_cancel (Test *test, gconstpointer unused)
128 {
129 CK_RV rv;
130
131 gcr_mock_prompter_expect_password_ok ("bad password", NULL);
132 gcr_mock_prompter_expect_password_cancel ();
133
134 rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
135 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
136 }
137
138 static void
139 test_cancel_immediately (Test *test, gconstpointer unused)
140 {
141 CK_RV rv;
142
143 gcr_mock_prompter_expect_password_cancel ();
144
145 rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
146 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
147 }
148
149 int
150 main (int argc, char **argv)
151 {
152 #if !GLIB_CHECK_VERSION(2,35,0)
153 g_type_init ();
154 #endif
155 g_test_init (&argc, &argv, NULL);
156
157 g_test_add ("/wrap-layer/login-specific/ok_password", Test, NULL, setup, test_ok_password, teardown);
158 g_test_add ("/wrap-layer/login-specific/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown);
159 g_test_add ("/wrap-layer/login-specific/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown);
160
161 return egg_tests_run_in_thread_with_loop ();
162 }