| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-xdg-module.c: A test PKCS#11 module implementation |
| 3 |
|
| 4 |
Copyright (C) 2010 Stefan Walter |
| 5 |
Copyright (C) 2010 Collabora Ltd |
| 6 |
|
| 7 |
The Gnome Keyring Library is free software; you can redistribute it and/or |
| 8 |
modify it under the terms of the GNU Library General Public License as |
| 9 |
published by the Free Software Foundation; either version 2 of the |
| 10 |
License, or (at your option) any later version. |
| 11 |
|
| 12 |
The Gnome Keyring Library is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
Library General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU Library General Public |
| 18 |
License along with the Gnome Library; see the file COPYING.LIB. If not, |
| 19 |
<http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
Author: Stef Walter <[email protected]> |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "config.h" |
| 25 |
|
| 26 |
#include "mock-xdg-module.h" |
| 27 |
|
| 28 |
#include "xdg-store/gkm-xdg-store.h" |
| 29 |
|
| 30 |
#include "egg/egg-secure-memory.h" |
| 31 |
#include "egg/egg-testing.h" |
| 32 |
|
| 33 |
#include "gkm/gkm-session.h" |
| 34 |
#include "gkm/gkm-module.h" |
| 35 |
|
| 36 |
#include <glib/gstdio.h> |
| 37 |
|
| 38 |
#include <errno.h> |
| 39 |
#include <sys/time.h> |
| 40 |
|
| 41 |
#include <string.h> |
| 42 |
|
| 43 |
EGG_SECURE_DEFINE_GLIB_GLOBALS (); |
| 44 |
|
| 45 |
static GMutex *mutex = NULL; |
| 46 |
static gchar *directory = NULL; |
| 47 |
|
| 48 |
GkmModule* _gkm_xdg_store_get_module_for_testing (void); |
| 49 |
GMutex* _gkm_module_get_scary_mutex_that_you_should_not_touch (GkmModule *module); |
| 50 |
|
| 51 |
void |
| 52 |
mock_xdg_module_remove_file (const gchar *name) |
| 53 |
{ |
| 54 |
gchar *filename; |
| 55 |
gchar *basename; |
| 56 |
|
| 57 |
basename = g_path_get_basename (name); |
| 58 |
filename = g_build_filename (directory, basename, NULL); |
| 59 |
if (g_unlink (filename) < 0) |
| 60 |
g_error ("couldn't remove: %s", filename); |
| 61 |
g_free (filename); |
| 62 |
g_free (basename); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
void |
| 67 |
mock_xdg_module_empty_file (const gchar *name) |
| 68 |
{ |
| 69 |
gchar *filename; |
| 70 |
gchar *basename; |
| 71 |
|
| 72 |
basename = g_path_get_basename (name); |
| 73 |
filename = g_build_filename (directory, basename, NULL); |
| 74 |
if (!g_file_set_contents (filename, "", 0, NULL)) |
| 75 |
g_error ("couldn't write: %s", filename); |
| 76 |
g_free (filename); |
| 77 |
g_free (basename); |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
mock_xdg_module_touch_file (const gchar *name, gint future) |
| 82 |
{ |
| 83 |
gchar *basename; |
| 84 |
gchar *filename; |
| 85 |
struct timeval tv[2]; |
| 86 |
|
| 87 |
basename = g_path_get_basename (name); |
| 88 |
filename = g_build_filename (directory, basename, NULL); |
| 89 |
|
| 90 |
/* Initialize the access and modification times */ |
| 91 |
gettimeofday (tv, NULL); |
| 92 |
tv[0].tv_sec += future; |
| 93 |
memcpy (tv + 1, tv, sizeof (struct timeval)); |
| 94 |
|
| 95 |
if (utimes (filename, tv) < 0) |
| 96 |
g_error ("couldn't update file time: %s: %s", filename, g_strerror (errno)); |
| 97 |
|
| 98 |
g_free (basename); |
| 99 |
g_free (filename); |
| 100 |
} |
| 101 |
|
| 102 |
GkmModule* |
| 103 |
mock_xdg_module_initialize_and_enter (void) |
| 104 |
{ |
| 105 |
CK_FUNCTION_LIST_PTR funcs; |
| 106 |
CK_C_INITIALIZE_ARGS args; |
| 107 |
GkmModule *module; |
| 108 |
gchar *string; |
| 109 |
CK_RV rv; |
| 110 |
|
| 111 |
directory = egg_tests_create_scratch_directory ( |
| 112 |
SRCDIR "/pkcs11/xdg-store/fixtures/test-refer-1.trust", |
| 113 |
SRCDIR "/pkcs11/xdg-store/fixtures/test-certificate-1.cer", |
| 114 |
NULL); |
| 115 |
|
| 116 |
/* Setup test directory to work in */ |
| 117 |
memset (&args, 0, sizeof (args)); |
| 118 |
string = g_strdup_printf ("directory='%s'", directory); |
| 119 |
args.pReserved = string; |
| 120 |
args.flags = CKF_OS_LOCKING_OK; |
| 121 |
|
| 122 |
/* Copy fixtures from test-data to scratch */ |
| 123 |
mock_xdg_module_empty_file ("invalid-without-ext"); |
| 124 |
mock_xdg_module_empty_file ("test-file.unknown"); |
| 125 |
mock_xdg_module_empty_file ("test-invalid.trust"); |
| 126 |
|
| 127 |
funcs = gkm_xdg_store_get_functions (); |
| 128 |
rv = (funcs->C_Initialize) (&args); |
| 129 |
g_return_val_if_fail (rv == CKR_OK, NULL); |
| 130 |
|
| 131 |
module = _gkm_xdg_store_get_module_for_testing (); |
| 132 |
g_return_val_if_fail (module, NULL); |
| 133 |
|
| 134 |
mutex = _gkm_module_get_scary_mutex_that_you_should_not_touch (module); |
| 135 |
mock_xdg_module_enter (); |
| 136 |
|
| 137 |
g_free (string); |
| 138 |
|
| 139 |
return module; |
| 140 |
} |
| 141 |
|
| 142 |
void |
| 143 |
mock_xdg_module_leave_and_finalize (void) |
| 144 |
{ |
| 145 |
CK_FUNCTION_LIST_PTR funcs; |
| 146 |
CK_RV rv; |
| 147 |
|
| 148 |
mock_xdg_module_leave (); |
| 149 |
|
| 150 |
funcs = gkm_xdg_store_get_functions (); |
| 151 |
rv = (funcs->C_Finalize) (NULL); |
| 152 |
g_return_if_fail (rv == CKR_OK); |
| 153 |
|
| 154 |
egg_tests_remove_scratch_directory (directory); |
| 155 |
g_free (directory); |
| 156 |
directory = NULL; |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
mock_xdg_module_leave (void) |
| 161 |
{ |
| 162 |
g_assert (mutex); |
| 163 |
g_mutex_unlock (mutex); |
| 164 |
} |
| 165 |
|
| 166 |
void |
| 167 |
mock_xdg_module_enter (void) |
| 168 |
{ |
| 169 |
g_assert (mutex); |
| 170 |
g_mutex_lock (mutex); |
| 171 |
} |
| 172 |
|
| 173 |
GkmSession* |
| 174 |
mock_xdg_module_open_session (gboolean writable) |
| 175 |
{ |
| 176 |
CK_ULONG flags = CKF_SERIAL_SESSION; |
| 177 |
CK_SESSION_HANDLE handle; |
| 178 |
GkmModule *module; |
| 179 |
GkmSession *session; |
| 180 |
CK_RV rv; |
| 181 |
|
| 182 |
module = _gkm_xdg_store_get_module_for_testing (); |
| 183 |
g_return_val_if_fail (module, NULL); |
| 184 |
|
| 185 |
if (writable) |
| 186 |
flags |= CKF_RW_SESSION; |
| 187 |
|
| 188 |
rv = gkm_module_C_OpenSession (module, 1, flags, NULL, NULL, &handle); |
| 189 |
g_assert (rv == CKR_OK); |
| 190 |
|
| 191 |
session = gkm_module_lookup_session (module, handle); |
| 192 |
g_assert (session); |
| 193 |
|
| 194 |
return session; |
| 195 |
} |