Line data Source code
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 <stef@thewalter.net>
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 59248 : 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 6 : mock_gnome2_module_initialize_and_enter (void)
43 : {
44 : CK_FUNCTION_LIST_PTR funcs;
45 : GkmModule *module;
46 : CK_RV rv;
47 :
48 6 : funcs = gkm_gnome2_store_get_functions ();
49 6 : rv = (funcs->C_Initialize) (NULL);
50 6 : g_return_val_if_fail (rv == CKR_OK, NULL);
51 :
52 6 : module = _gkm_gnome2_store_get_module_for_testing ();
53 6 : g_return_val_if_fail (module, NULL);
54 :
55 6 : mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module);
56 6 : mock_gnome2_module_enter ();
57 :
58 6 : return module;
59 : }
60 :
61 : void
62 6 : mock_gnome2_module_leave_and_finalize (void)
63 : {
64 : CK_FUNCTION_LIST_PTR funcs;
65 : CK_RV rv;
66 :
67 6 : mock_gnome2_module_leave ();
68 :
69 6 : funcs = gkm_gnome2_store_get_functions ();
70 6 : rv = (funcs->C_Finalize) (NULL);
71 6 : g_return_if_fail (rv == CKR_OK);
72 : }
73 :
74 : void
75 6 : mock_gnome2_module_leave (void)
76 : {
77 6 : g_assert (mutex);
78 6 : g_mutex_unlock (mutex);
79 6 : }
80 :
81 : void
82 6 : mock_gnome2_module_enter (void)
83 : {
84 6 : g_assert (mutex);
85 6 : g_mutex_lock (mutex);
86 6 : }
87 :
88 : GkmSession *
89 6 : mock_gnome2_module_open_session (gboolean writable)
90 : {
91 6 : CK_ULONG flags = CKF_SERIAL_SESSION;
92 : CK_SESSION_HANDLE handle;
93 : GkmModule *module;
94 : GkmSession *session;
95 : CK_RV rv;
96 :
97 6 : module = _gkm_gnome2_store_get_module_for_testing ();
98 6 : g_return_val_if_fail (module, NULL);
99 :
100 6 : if (writable)
101 6 : flags |= CKF_RW_SESSION;
102 :
103 6 : rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle);
104 6 : g_assert (rv == CKR_OK);
105 :
106 6 : session = gkm_module_lookup_session (module, handle);
107 6 : g_assert (session);
108 :
109 6 : return session;
110 : }
|