| 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 <string.h> |
| 34 |
|
| 35 |
typedef struct { |
| 36 |
CK_FUNCTION_LIST 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 test->functions */ |
| 51 |
rv = gkm_mock_C_GetFunctionList (&funcs); |
| 52 |
gkm_assert_cmprv (rv, ==, CKR_OK); |
| 53 |
memcpy (&test->functions, funcs, sizeof (test->functions)); |
| 54 |
|
| 55 |
gkm_wrap_layer_reset_modules (); |
| 56 |
gkm_wrap_layer_add_module (&test->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_ok_password (Test *test, gconstpointer unused) |
| 92 |
{ |
| 93 |
CK_RV rv; |
| 94 |
|
| 95 |
gcr_mock_prompter_expect_password_ok ("new", NULL); |
| 96 |
|
| 97 |
rv = (test->module->C_InitPIN) (test->session, NULL, 0); |
| 98 |
gkm_assert_cmprv (rv, ==, CKR_OK); |
| 99 |
|
| 100 |
rv = (test->module->C_Login) (test->session, CKU_USER, (guchar*)"new", 3); |
| 101 |
gkm_assert_cmprv (rv, ==, CKR_OK); |
| 102 |
} |
| 103 |
|
| 104 |
int |
| 105 |
main (int argc, char **argv) |
| 106 |
{ |
| 107 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 108 |
g_type_init (); |
| 109 |
#endif |
| 110 |
g_test_init (&argc, &argv, NULL); |
| 111 |
|
| 112 |
g_test_add ("/wrap-layer/init-pin/ok_password", Test, NULL, setup, test_ok_password, teardown); |
| 113 |
|
| 114 |
return egg_tests_run_in_thread_with_loop (); |
| 115 |
} |