Debian Codegraph @

pkcs11/gnome2-store/mock-gnome2-module.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* mock-gnome2-module.c
3
4 Copyright (C) 2011 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 <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "mock-gnome2-module.h"
26
27 #include "egg/egg-secure-memory.h"
28
29 #include "gkm/gkm-module.h"
30
31 #include "gnome2-store/gkm-gnome2-store.h"
32
33 EGG_SECURE_DEFINE_GLIB_GLOBALS ();
34
35 static GMutex *mutex = NULL;
36
37 GkmModule * _gkm_gnome2_store_get_module_for_testing (void);
38
39 GMutex * _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module);
40
41 GkmModule *
42 mock_gnome2_module_initialize_and_enter (void)
43 {
44 CK_FUNCTION_LIST_PTR funcs;
45 GkmModule *module;
46 CK_RV rv;
47
48 funcs = gkm_gnome2_store_get_functions ();
49 rv = (funcs->C_Initialize) (NULL);
50 g_return_val_if_fail (rv == CKR_OK, NULL);
51
52 module = _gkm_gnome2_store_get_module_for_testing ();
53 g_return_val_if_fail (module, NULL);
54
55 mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module);
56 mock_gnome2_module_enter ();
57
58 return module;
59 }
60
61 void
62 mock_gnome2_module_leave_and_finalize (void)
63 {
64 CK_FUNCTION_LIST_PTR funcs;
65 CK_RV rv;
66
67 mock_gnome2_module_leave ();
68
69 funcs = gkm_gnome2_store_get_functions ();
70 rv = (funcs->C_Finalize) (NULL);
71 g_return_if_fail (rv == CKR_OK);
72 }
73
74 void
75 mock_gnome2_module_leave (void)
76 {
77 g_assert (mutex);
78 g_mutex_unlock (mutex);
79 }
80
81 void
82 mock_gnome2_module_enter (void)
83 {
84 g_assert (mutex);
85 g_mutex_lock (mutex);
86 }
87
88 GkmSession *
89 mock_gnome2_module_open_session (gboolean writable)
90 {
91 CK_ULONG flags = CKF_SERIAL_SESSION;
92 CK_SESSION_HANDLE handle;
93 GkmModule *module;
94 GkmSession *session;
95 CK_RV rv;
96
97 module = _gkm_gnome2_store_get_module_for_testing ();
98 g_return_val_if_fail (module, NULL);
99
100 if (writable)
101 flags |= CKF_RW_SESSION;
102
103 rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
104 g_assert (rv == CKR_OK);
105
106 session = gkm_module_lookup_session (module, handle);
107 g_assert (session);
108
109 return session;
110 }