Debian Codegraph @

pkcs11/wrap-layer/test-login-user.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_SLOT_ID slot_id;
46 CK_ULONG n_slots = 1;
47 const gchar *prompter;
48 CK_RV rv;
49
50 /* Always start off with test functions */
51 rv = gkm_mock_C_GetFunctionList (&funcs);
52 gkm_assert_cmprv (rv, ==, CKR_OK);
53 memcpy (&test->prompt_login_functions, funcs, sizeof (test->prompt_login_functions));
54
55 gkm_wrap_layer_reset_modules ();
56 gkm_wrap_layer_add_module (&test->prompt_login_functions);
57 test->module = gkm_wrap_layer_get_functions ();
58
59 prompter = gcr_mock_prompter_start ();
60 gkm_wrap_prompt_set_prompter_name (prompter);
61
62 /* Open a test->session */
63 rv = (test->module->C_Initialize) (NULL);
64 gkm_assert_cmprv (rv, ==, CKR_OK);
65
66 rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
67 gkm_assert_cmprv (rv, ==, CKR_OK);
68
69 rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session);
70 gkm_assert_cmprv (rv, ==, CKR_OK);
71 }
72
73 static void
74 teardown (Test *test, gconstpointer unused)
75 {
76 CK_RV rv;
77
78 g_assert (!gcr_mock_prompter_is_expecting ());
79 gcr_mock_prompter_stop ();
80
81 rv = (test->module->C_CloseSession) (test->session);
82 gkm_assert_cmprv (rv, ==, CKR_OK);
83 test->session = 0;
84
85 rv = (test->module->C_Finalize) (NULL);
86 gkm_assert_cmprv (rv, ==, CKR_OK);
87 test->module = NULL;
88 }
89
90 static void
91 test_fail_unsupported_so (Test *test, gconstpointer unused)
92 {
93 CK_RV rv;
94
95 rv = (test->module->C_Login) (test->session, CKU_SO, NULL, 0);
96 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
97 }
98
99 static void
100 test_skip_prompt_because_pin (Test *test, gconstpointer unused)
101 {
102 CK_RV rv;
103
104 rv = (test->module->C_Login) (test->session, CKU_USER, (guchar*)"booo", 4);
105 gkm_assert_cmprv (rv, ==, CKR_OK);
106 }
107
108 static void
109 test_ok_password (Test *test, gconstpointer unused)
110 {
111 CK_RV rv;
112
113 gcr_mock_prompter_expect_password_ok ("booo", NULL);
114
115 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
116 gkm_assert_cmprv (rv, ==, CKR_OK);
117 }
118
119 static void
120 test_bad_password_then_cancel (Test *test, gconstpointer unused)
121 {
122 CK_RV rv;
123
124 gcr_mock_prompter_expect_password_ok ("bad password", NULL);
125 gcr_mock_prompter_expect_password_cancel ();
126
127 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
128 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
129 }
130
131 static void
132 test_cancel_immediately (Test *test, gconstpointer unused)
133 {
134 CK_RV rv;
135
136 gcr_mock_prompter_expect_password_cancel ();
137
138 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
139 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
140 }
141
142 static void
143 test_fail_get_session_info (Test *test, gconstpointer unused)
144 {
145 CK_RV rv;
146
147 test->prompt_login_functions.C_GetSessionInfo = gkm_mock_fail_C_GetSessionInfo;
148 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
149 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
150 }
151
152 static void
153 test_fail_get_token_info (Test *test, gconstpointer unused)
154 {
155 CK_RV rv;
156
157 test->prompt_login_functions.C_GetTokenInfo = gkm_mock_fail_C_GetTokenInfo;
158 rv = (test->module->C_Login) (test->session, CKU_USER, NULL, 0);
159 gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
160 }
161
162 int
163 main (int argc, char **argv)
164 {
165 #if !GLIB_CHECK_VERSION(2,35,0)
166 g_type_init ();
167 #endif
168 g_test_init (&argc, &argv, NULL);
169
170 g_test_add ("/wrap-layer/login-user/fail_unsupported_so", Test, NULL, setup, test_fail_unsupported_so, teardown);
171 g_test_add ("/wrap-layer/login-user/skip_prompt_because_pin", Test, NULL, setup, test_skip_prompt_because_pin, teardown);
172 g_test_add ("/wrap-layer/login-user/ok_password", Test, NULL, setup, test_ok_password, teardown);
173 g_test_add ("/wrap-layer/login-user/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown);
174 g_test_add ("/wrap-layer/login-user/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown);
175 g_test_add ("/wrap-layer/login-user/fail_get_session_info", Test, NULL, setup, test_fail_get_session_info, teardown);
176 g_test_add ("/wrap-layer/login-user/fail_get_token_info", Test, NULL, setup, test_fail_get_token_info, teardown);
177
178 return egg_tests_run_in_thread_with_loop ();
179 }