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 3 : setup (Test *test, gconstpointer unused)
43 : {
44 : CK_FUNCTION_LIST_PTR funcs;
45 : CK_OBJECT_HANDLE key;
46 : CK_SLOT_ID slot_id;
47 3 : CK_ULONG n_slots = 1;
48 : const gchar *prompter;
49 : CK_ULONG count;
50 : CK_RV rv;
51 :
52 3 : CK_BBOOL always = TRUE;
53 3 : CK_ATTRIBUTE attrs[] = {
54 : { CKA_ALWAYS_AUTHENTICATE, &always, sizeof (always) }
55 : };
56 :
57 3 : CK_MECHANISM mech = { CKM_MOCK_PREFIX, NULL, 0 };
58 :
59 : /* Always start off with test functions */
60 3 : rv = gkm_mock_C_GetFunctionList (&funcs);
61 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
62 3 : memcpy (&test->prompt_login_functions, funcs, sizeof (test->prompt_login_functions));
63 :
64 3 : gkm_wrap_layer_reset_modules ();
65 3 : gkm_wrap_layer_add_module (&test->prompt_login_functions);
66 3 : test->module = gkm_wrap_layer_get_functions ();
67 :
68 3 : prompter = gcr_mock_prompter_start ();
69 3 : gkm_wrap_prompt_set_prompter_name (prompter);
70 :
71 : /* Open a test->session */
72 3 : rv = (test->module->C_Initialize) (NULL);
73 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
74 :
75 3 : rv = (test->module->C_GetSlotList) (CK_TRUE, &slot_id, &n_slots);
76 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
77 :
78 3 : rv = (test->module->C_OpenSession) (slot_id, CKF_SERIAL_SESSION, NULL, NULL, &test->session);
79 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
80 :
81 : /* Find the always authenticate object */
82 3 : rv = (test->module->C_FindObjectsInit) (test->session, attrs, 1);
83 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
84 :
85 3 : rv = (test->module->C_FindObjects) (test->session, &key, 1, &count);
86 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
87 3 : gkm_assert_cmpulong (count, ==, 1);
88 3 : gkm_assert_cmpulong (key, !=, 0);
89 :
90 3 : rv = (test->module->C_FindObjectsFinal) (test->session);
91 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
92 :
93 : /* Start a signing operation, that needs to be authenticated */
94 3 : rv = (test->module->C_SignInit) (test->session, &mech, key);
95 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
96 3 : }
97 :
98 : static void
99 3 : teardown (Test *test, gconstpointer unused)
100 : {
101 : CK_RV rv;
102 :
103 3 : g_assert (!gcr_mock_prompter_is_expecting ());
104 3 : gcr_mock_prompter_stop ();
105 :
106 3 : rv = (test->module->C_CloseSession) (test->session);
107 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
108 3 : test->session = 0;
109 :
110 3 : rv = (test->module->C_Finalize) (NULL);
111 3 : gkm_assert_cmprv (rv, ==, CKR_OK);
112 3 : test->module = NULL;
113 3 : }
114 :
115 : static void
116 1 : test_ok_password (Test *test, gconstpointer unused)
117 : {
118 : CK_RV rv;
119 :
120 1 : gcr_mock_prompter_expect_password_ok ("booo", NULL);
121 :
122 1 : rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
123 1 : gkm_assert_cmprv (rv, ==, CKR_OK);
124 1 : }
125 :
126 : static void
127 1 : test_bad_password_then_cancel (Test *test, gconstpointer unused)
128 : {
129 : CK_RV rv;
130 :
131 1 : gcr_mock_prompter_expect_password_ok ("bad password", NULL);
132 1 : gcr_mock_prompter_expect_password_cancel ();
133 :
134 1 : rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
135 1 : gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
136 1 : }
137 :
138 : static void
139 1 : test_cancel_immediately (Test *test, gconstpointer unused)
140 : {
141 : CK_RV rv;
142 :
143 1 : gcr_mock_prompter_expect_password_cancel ();
144 :
145 1 : rv = (test->module->C_Login) (test->session, CKU_CONTEXT_SPECIFIC, NULL, 0);
146 1 : gkm_assert_cmprv (rv, ==, CKR_PIN_INCORRECT);
147 1 : }
148 :
149 : int
150 1 : main (int argc, char **argv)
151 : {
152 : #if !GLIB_CHECK_VERSION(2,35,0)
153 : g_type_init ();
154 : #endif
155 1 : g_test_init (&argc, &argv, NULL);
156 :
157 1 : g_test_add ("/wrap-layer/login-specific/ok_password", Test, NULL, setup, test_ok_password, teardown);
158 1 : g_test_add ("/wrap-layer/login-specific/bad_password_then_cancel", Test, NULL, setup, test_bad_password_then_cancel, teardown);
159 1 : g_test_add ("/wrap-layer/login-specific/cancel_immediately", Test, NULL, setup, test_cancel_immediately, teardown);
160 :
161 1 : return egg_tests_run_in_thread_with_loop ();
162 : }
|