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