Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-module.c: A test PKCS#11 module implementation
3 :
4 : Copyright (C) 2009 Stefan Walter
5 :
6 : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : The Gnome Keyring Library is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public
17 : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stef@memberwebs.com>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "mock-ssh-module.h"
26 :
27 : #include "egg/egg-secure-memory.h"
28 :
29 : #include "gkm/gkm-module.h"
30 :
31 : #include "ssh-store/gkm-ssh-store.h"
32 :
33 1000 : EGG_SECURE_DEFINE_GLIB_GLOBALS ();
34 :
35 : static GMutex *mutex = NULL;
36 :
37 : GkmModule* _gkm_ssh_store_get_module_for_testing (void);
38 : GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
39 :
40 : GkmModule*
41 5 : test_ssh_module_initialize_and_enter (void)
42 : {
43 : CK_FUNCTION_LIST_PTR funcs;
44 : GkmModule *module;
45 : CK_RV rv;
46 :
47 5 : funcs = gkm_ssh_store_get_functions ();
48 5 : rv = (funcs->C_Initialize) (NULL);
49 5 : g_return_val_if_fail (rv == CKR_OK, NULL);
50 :
51 5 : module = _gkm_ssh_store_get_module_for_testing ();
52 5 : g_return_val_if_fail (module, NULL);
53 :
54 5 : mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module);
55 5 : test_ssh_module_enter ();
56 :
57 5 : return module;
58 : }
59 :
60 : void
61 5 : test_ssh_module_leave_and_finalize (void)
62 : {
63 : CK_FUNCTION_LIST_PTR funcs;
64 : CK_RV rv;
65 :
66 5 : test_ssh_module_leave ();
67 :
68 5 : funcs = gkm_ssh_store_get_functions ();
69 5 : rv = (funcs->C_Finalize) (NULL);
70 5 : g_return_if_fail (rv == CKR_OK);
71 : }
72 :
73 : void
74 5 : test_ssh_module_leave (void)
75 : {
76 5 : g_assert (mutex);
77 5 : g_mutex_unlock (mutex);
78 5 : }
79 :
80 : void
81 5 : test_ssh_module_enter (void)
82 : {
83 5 : g_assert (mutex);
84 5 : g_mutex_lock (mutex);
85 5 : }
86 :
87 : GkmSession*
88 5 : test_ssh_module_open_session (gboolean writable)
89 : {
90 5 : CK_ULONG flags = CKF_SERIAL_SESSION;
91 : CK_SESSION_HANDLE handle;
92 : GkmModule *module;
93 : GkmSession *session;
94 : CK_RV rv;
95 :
96 5 : module = _gkm_ssh_store_get_module_for_testing ();
97 5 : g_return_val_if_fail (module, NULL);
98 :
99 5 : if (writable)
100 5 : flags |= CKF_RW_SESSION;
101 :
102 5 : rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
103 5 : g_assert (rv == CKR_OK);
104 :
105 5 : session = gkm_module_lookup_session (module, handle);
106 5 : g_assert (session);
107 :
108 5 : return session;
109 : }
|