| 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 "egg/egg-secure-memory.h" |
| 24 |
#include "egg/egg-testing.h" |
| 25 |
|
| 26 |
#include "gkm/gkm-attributes.h" |
| 27 |
#include "gkm/gkm-mock.h" |
| 28 |
#include "gkm/gkm-test.h" |
| 29 |
|
| 30 |
#include "wrap-layer/gkm-wrap-layer.h" |
| 31 |
#include "wrap-layer/gkm-wrap-login.h" |
| 32 |
|
| 33 |
#include <glib-object.h> |
| 34 |
|
| 35 |
extern CK_FUNCTION_LIST mock_secret_store; |
| 36 |
|
| 37 |
typedef struct { |
| 38 |
CK_FUNCTION_LIST functions; |
| 39 |
CK_FUNCTION_LIST_PTR module; |
| 40 |
} Test; |
| 41 |
|
| 42 |
static void |
| 43 |
setup (Test *test, gconstpointer unused) |
| 44 |
{ |
| 45 |
CK_RV rv; |
| 46 |
|
| 47 |
/* Always start off with test test->functions */ |
| 48 |
memcpy (&test->functions, &mock_secret_store, sizeof (test->functions)); |
| 49 |
gkm_wrap_layer_reset_modules (); |
| 50 |
gkm_wrap_layer_add_module (&test->functions); |
| 51 |
test->module = gkm_wrap_layer_get_functions (); |
| 52 |
|
| 53 |
/* Initialize */ |
| 54 |
rv = (test->module->C_Initialize) (NULL); |
| 55 |
gkm_assert_cmprv (rv, ==, CKR_OK); |
| 56 |
} |
| 57 |
|
| 58 |
static void |
| 59 |
teardown (Test *test, gconstpointer unused) |
| 60 |
{ |
| 61 |
CK_RV rv; |
| 62 |
|
| 63 |
rv = (test->module->C_Finalize) (NULL); |
| 64 |
gkm_assert_cmprv (rv, ==, CKR_OK); |
| 65 |
test->module = NULL; |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
test_is_usable (Test *test, gconstpointer unused) |
| 70 |
{ |
| 71 |
gboolean ret; |
| 72 |
|
| 73 |
ret = gkm_wrap_login_is_usable (); |
| 74 |
g_assert (ret == TRUE); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
test_usable_fail_open_session (Test *test, gconstpointer unused) |
| 79 |
{ |
| 80 |
gboolean ret; |
| 81 |
|
| 82 |
test->functions.C_OpenSession = gkm_mock_fail_C_OpenSession; |
| 83 |
ret = gkm_wrap_login_is_usable (); |
| 84 |
g_assert (ret == FALSE); |
| 85 |
} |
| 86 |
|
| 87 |
static void |
| 88 |
test_usable_fail_not_trusted (Test *test, gconstpointer unused) |
| 89 |
{ |
| 90 |
CK_OBJECT_HANDLE object; |
| 91 |
CK_ATTRIBUTE attr; |
| 92 |
CK_BBOOL bval; |
| 93 |
gboolean ret; |
| 94 |
|
| 95 |
ret = gkm_wrap_login_is_usable (); |
| 96 |
g_assert (ret == TRUE); |
| 97 |
|
| 98 |
bval = CK_TRUE; |
| 99 |
attr.type = CKA_G_LOGIN_COLLECTION; |
| 100 |
attr.pValue = &bval; |
| 101 |
attr.ulValueLen = sizeof (bval); |
| 102 |
|
| 103 |
object = gkm_mock_module_find_object (0, &attr, 1); |
| 104 |
gkm_assert_cmpulong (object, !=, 0); |
| 105 |
|
| 106 |
bval = CK_FALSE; |
| 107 |
attr.type = CKA_TRUSTED; |
| 108 |
attr.pValue = &bval; |
| 109 |
attr.ulValueLen = sizeof (bval); |
| 110 |
|
| 111 |
gkm_mock_module_set_object (object, &attr, 1); |
| 112 |
|
| 113 |
/* Not trusted, so no longer usable */ |
| 114 |
ret = gkm_wrap_login_is_usable (); |
| 115 |
g_assert (ret == FALSE); |
| 116 |
} |
| 117 |
|
| 118 |
static void |
| 119 |
test_usable_fail_locked (Test *test, gconstpointer unused) |
| 120 |
{ |
| 121 |
CK_OBJECT_HANDLE object; |
| 122 |
CK_ATTRIBUTE attr; |
| 123 |
CK_BBOOL bval; |
| 124 |
gboolean ret; |
| 125 |
|
| 126 |
ret = gkm_wrap_login_is_usable (); |
| 127 |
g_assert (ret == TRUE); |
| 128 |
|
| 129 |
bval = CK_TRUE; |
| 130 |
attr.type = CKA_G_LOGIN_COLLECTION; |
| 131 |
attr.pValue = &bval; |
| 132 |
attr.ulValueLen = sizeof (bval); |
| 133 |
|
| 134 |
object = gkm_mock_module_find_object (0, &attr, 1); |
| 135 |
gkm_assert_cmpulong (object, !=, 0); |
| 136 |
|
| 137 |
bval = CK_TRUE; |
| 138 |
attr.type = CKA_G_LOCKED; |
| 139 |
attr.pValue = &bval; |
| 140 |
attr.ulValueLen = sizeof (bval); |
| 141 |
|
| 142 |
gkm_mock_module_set_object (object, &attr, 1); |
| 143 |
|
| 144 |
/* Not unlocked, so no longer usable */ |
| 145 |
ret = gkm_wrap_login_is_usable (); |
| 146 |
g_assert (ret == FALSE); |
| 147 |
} |
| 148 |
|
| 149 |
static void |
| 150 |
test_lookup_secret_no_match (Test *test, gconstpointer unused) |
| 151 |
{ |
| 152 |
gchar *password; |
| 153 |
|
| 154 |
password = gkm_wrap_login_lookup_secret ("invalid", "attribute", |
| 155 |
"second", "attribute", NULL); |
| 156 |
g_assert_cmpstr (password, ==, NULL); |
| 157 |
} |
| 158 |
|
| 159 |
static void |
| 160 |
test_lookup_secret_and_match (Test *test, gconstpointer unused) |
| 161 |
{ |
| 162 |
gchar *password; |
| 163 |
|
| 164 |
/* Secret stored in mock-secret-store.c */ |
| 165 |
password = gkm_wrap_login_lookup_secret ("one", "1", |
| 166 |
"two", "2", NULL); |
| 167 |
g_assert_cmpstr (password, ==, "mock"); |
| 168 |
|
| 169 |
egg_secure_free (password); |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
test_lookup_store_secret (Test *test, gconstpointer unused) |
| 174 |
{ |
| 175 |
CK_OBJECT_CLASS klass = CKO_SECRET_KEY; |
| 176 |
CK_BBOOL tval = CK_TRUE; |
| 177 |
CK_ATTRIBUTE attrs[] = { |
| 178 |
{ CKA_LABEL, "Unlock password for: The label", 30 }, |
| 179 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 180 |
{ CKA_VALUE, "the password", 12 }, |
| 181 |
{ CKA_TOKEN, &tval, sizeof (tval) }, |
| 182 |
{ CKA_G_COLLECTION, "login", 5 }, |
| 183 |
{ CKA_G_FIELDS, "one\0" "1\0" "three\0" "3\0", 14 }, |
| 184 |
}; |
| 185 |
|
| 186 |
CK_OBJECT_HANDLE object; |
| 187 |
|
| 188 |
/* Secret stored in mock-secret-store.c */ |
| 189 |
gkm_wrap_login_attach_secret ("The label", "the password", |
| 190 |
"one", "1", |
| 191 |
"three", "3", NULL); |
| 192 |
|
| 193 |
object = gkm_mock_module_find_object (0, attrs, G_N_ELEMENTS (attrs)); |
| 194 |
gkm_assert_cmpulong (object, !=, 0); |
| 195 |
} |
| 196 |
|
| 197 |
static void |
| 198 |
test_lookup_store_secret_overwrite (Test *test, gconstpointer unused) |
| 199 |
{ |
| 200 |
CK_OBJECT_CLASS klass = CKO_SECRET_KEY; |
| 201 |
CK_BBOOL tval = CK_TRUE; |
| 202 |
CK_ATTRIBUTE attrs[] = { |
| 203 |
{ CKA_VALUE, "the password", 12 }, |
| 204 |
{ CKA_LABEL, "Unlock password for: The label", 30 }, |
| 205 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 206 |
{ CKA_TOKEN, &tval, sizeof (tval) }, |
| 207 |
{ CKA_G_COLLECTION, "login", 5 }, |
| 208 |
{ CKA_G_FIELDS, "one\0" "1\0" "three\0" "3\0", 14 }, |
| 209 |
}; |
| 210 |
|
| 211 |
CK_OBJECT_HANDLE object1, object2; |
| 212 |
|
| 213 |
/* Secret stored in mock-secret-store.c */ |
| 214 |
gkm_wrap_login_attach_secret ("The label", "the password", |
| 215 |
"one", "1", |
| 216 |
"three", "3", NULL); |
| 217 |
|
| 218 |
object1 = gkm_mock_module_find_object (0, attrs, G_N_ELEMENTS (attrs)); |
| 219 |
gkm_assert_cmpulong (object1, !=, 0); |
| 220 |
|
| 221 |
/* Secret stored in mock-secret-store.c */ |
| 222 |
gkm_wrap_login_attach_secret ("The label", "other", |
| 223 |
"one", "1", |
| 224 |
"three", "3", NULL); |
| 225 |
|
| 226 |
attrs[0].pValue = "other"; |
| 227 |
attrs[0].ulValueLen = 5; |
| 228 |
|
| 229 |
object2 = gkm_mock_module_find_object (0, attrs, G_N_ELEMENTS (attrs)); |
| 230 |
gkm_assert_cmpulong (object2, !=, 0); |
| 231 |
|
| 232 |
/* Should have been stored on same object */ |
| 233 |
gkm_assert_cmpulong (object1, ==, object2); |
| 234 |
} |
| 235 |
|
| 236 |
static void |
| 237 |
test_lookup_store_null_secret (Test *test, gconstpointer unused) |
| 238 |
{ |
| 239 |
CK_OBJECT_CLASS klass = CKO_SECRET_KEY; |
| 240 |
CK_BBOOL tval = CK_TRUE; |
| 241 |
CK_ATTRIBUTE attrs[] = { |
| 242 |
{ CKA_LABEL, "Unlock password for: The label", 30 }, |
| 243 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 244 |
{ CKA_VALUE, "", 0 }, |
| 245 |
{ CKA_TOKEN, &tval, sizeof (tval) }, |
| 246 |
{ CKA_G_COLLECTION, "login", 5 }, |
| 247 |
{ CKA_G_FIELDS, "one\0" "1\0" "three\0" "3\0", 14 }, |
| 248 |
}; |
| 249 |
|
| 250 |
CK_OBJECT_HANDLE object; |
| 251 |
|
| 252 |
gkm_wrap_login_attach_secret ("The label", NULL, |
| 253 |
"one", "1", |
| 254 |
"three", "3", NULL); |
| 255 |
|
| 256 |
object = gkm_mock_module_find_object (0, attrs, G_N_ELEMENTS (attrs)); |
| 257 |
gkm_assert_cmpulong (object, !=, 0); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
test_lookup_store_no_attributes_not_stored (Test *test, gconstpointer unused) |
| 262 |
{ |
| 263 |
CK_OBJECT_CLASS klass = CKO_SECRET_KEY; |
| 264 |
CK_BBOOL tval = CK_TRUE; |
| 265 |
CK_ATTRIBUTE attrs[] = { |
| 266 |
{ CKA_LABEL, "Unlock password for: The label", 30 }, |
| 267 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 268 |
{ CKA_VALUE, "the password", 0 }, |
| 269 |
{ CKA_TOKEN, &tval, sizeof (tval) }, |
| 270 |
{ CKA_G_COLLECTION, "login", 5 }, |
| 271 |
{ CKA_G_FIELDS, "", 0 }, |
| 272 |
}; |
| 273 |
|
| 274 |
CK_OBJECT_HANDLE object; |
| 275 |
|
| 276 |
gkm_wrap_login_attach_secret ("The label", "the password", NULL); |
| 277 |
|
| 278 |
object = gkm_mock_module_find_object (0, attrs, G_N_ELEMENTS (attrs)); |
| 279 |
gkm_assert_cmpulong (object, ==, 0); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
static void |
| 284 |
test_lookup_remove_present (Test *test, gconstpointer unused) |
| 285 |
{ |
| 286 |
CK_OBJECT_CLASS klass = CKO_SECRET_KEY; |
| 287 |
CK_BBOOL tval = CK_TRUE; |
| 288 |
CK_ATTRIBUTE attrs[] = { |
| 289 |
{ CKA_LABEL, "Unlock password for: Mock", 25 }, |
| 290 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 291 |
{ CKA_VALUE, "mock", 4 }, |
| 292 |
{ CKA_TOKEN, &tval, sizeof (tval) }, |
| 293 |
{ CKA_G_COLLECTION, "login", 5 }, |
| 294 |
{ CKA_G_FIELDS, "one\0" "1\0" "two\0" "2\0", 12 }, |
| 295 |
}; |
| 296 |
|
| 297 |
CK_OBJECT_HANDLE object; |
| 298 |
|
| 299 |
/* This object is created in mock-secret-store.c */ |
| 300 |
object = gkm_mock_module_find_object (0, attrs, G_N_ELEMENTS (attrs)); |
| 301 |
gkm_assert_cmpulong (object, !=, 0); |
| 302 |
|
| 303 |
gkm_wrap_login_remove_secret ("one", "1", "two", "2", NULL); |
| 304 |
|
| 305 |
/* No longer there */ |
| 306 |
object = gkm_mock_module_find_object (0, attrs, G_N_ELEMENTS (attrs)); |
| 307 |
gkm_assert_cmpulong (object, ==, 0); |
| 308 |
} |
| 309 |
|
| 310 |
static void |
| 311 |
test_lookup_remove_no_attributes (Test *test, gconstpointer unused) |
| 312 |
{ |
| 313 |
guint n_objects, check; |
| 314 |
|
| 315 |
n_objects = gkm_mock_module_count_objects (0); |
| 316 |
g_assert_cmpuint (n_objects, >, 0); |
| 317 |
|
| 318 |
/* Shouldn't remove anything if no attributes */ |
| 319 |
gkm_wrap_login_remove_secret (NULL); |
| 320 |
|
| 321 |
check = gkm_mock_module_count_objects (0); |
| 322 |
g_assert_cmpuint (check, ==, n_objects); |
| 323 |
} |
| 324 |
|
| 325 |
int |
| 326 |
main (int argc, char **argv) |
| 327 |
{ |
| 328 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 329 |
g_type_init (); |
| 330 |
#endif |
| 331 |
g_test_init (&argc, &argv, NULL); |
| 332 |
|
| 333 |
g_test_add ("/wrap-layer/login-keyring/is_usable", Test, NULL, setup, test_is_usable, teardown); |
| 334 |
g_test_add ("/wrap-layer/login-keyring/usable_fail_open_session", Test, NULL, setup, test_usable_fail_open_session, teardown); |
| 335 |
g_test_add ("/wrap-layer/login-keyring/usable_fail_not_trusted", Test, NULL, setup, test_usable_fail_not_trusted, teardown); |
| 336 |
g_test_add ("/wrap-layer/login-keyring/usable_fail_locked", Test, NULL, setup, test_usable_fail_locked, teardown); |
| 337 |
g_test_add ("/wrap-layer/login-keyring/lookup_secret_no_match", Test, NULL, setup, test_lookup_secret_no_match, teardown); |
| 338 |
g_test_add ("/wrap-layer/login-keyring/lookup_secret_and_match", Test, NULL, setup, test_lookup_secret_and_match, teardown); |
| 339 |
g_test_add ("/wrap-layer/login-keyring/lookup_store_secret", Test, NULL, setup, test_lookup_store_secret, teardown); |
| 340 |
g_test_add ("/wrap-layer/login-keyring/lookup_store_secret_overwrite", Test, NULL, setup, test_lookup_store_secret_overwrite, teardown); |
| 341 |
g_test_add ("/wrap-layer/login-keyring/lookup_store_null_secret", Test, NULL, setup, test_lookup_store_null_secret, teardown); |
| 342 |
g_test_add ("/wrap-layer/login-keyring/lookup_store_no_attributes_not_stored", Test, NULL, setup, test_lookup_store_no_attributes_not_stored, teardown); |
| 343 |
g_test_add ("/wrap-layer/login-keyring/lookup_remove_present", Test, NULL, setup, test_lookup_remove_present, teardown); |
| 344 |
g_test_add ("/wrap-layer/login-keyring/lookup_remove_no_attributes", Test, NULL, setup, test_lookup_remove_no_attributes, teardown); |
| 345 |
|
| 346 |
return egg_tests_run_in_thread_with_loop (); |
| 347 |
} |