| 1 |
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
| 2 |
/* test-secret-test->collection.c: Test the test->collection keyring |
| 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 <[email protected]> |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include "mock-secret-module.h" |
| 26 |
|
| 27 |
#include "secret-store/gkm-secret-data.h" |
| 28 |
#include "secret-store/gkm-secret-collection.h" |
| 29 |
#include "secret-store/gkm-secret-item.h" |
| 30 |
|
| 31 |
#include "gkm/gkm-credential.h" |
| 32 |
#include "gkm/gkm-session.h" |
| 33 |
#include "gkm/gkm-transaction.h" |
| 34 |
|
| 35 |
#include "pkcs11/pkcs11i.h" |
| 36 |
|
| 37 |
#include <glib.h> |
| 38 |
|
| 39 |
#include <stdlib.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <string.h> |
| 42 |
|
| 43 |
typedef struct { |
| 44 |
GkmModule *module; |
| 45 |
GkmSession *session; |
| 46 |
CK_OBJECT_HANDLE credential; |
| 47 |
CK_OBJECT_HANDLE credential2; |
| 48 |
GkmSecretCollection *collection; |
| 49 |
} Test; |
| 50 |
|
| 51 |
static void |
| 52 |
setup (Test *test, gconstpointer unused) |
| 53 |
{ |
| 54 |
CK_OBJECT_CLASS klass = CKO_G_CREDENTIAL; |
| 55 |
GkmObject *cred; |
| 56 |
|
| 57 |
CK_ATTRIBUTE attrs[] = { |
| 58 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 59 |
{ CKA_VALUE, NULL, 0 } |
| 60 |
}; |
| 61 |
|
| 62 |
test->module = test_secret_module_initialize_and_enter (); |
| 63 |
test->session = test_secret_module_open_session (TRUE); |
| 64 |
|
| 65 |
test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION, |
| 66 |
"module", test->module, |
| 67 |
"identifier", "test", |
| 68 |
NULL); |
| 69 |
g_assert (GKM_IS_SECRET_COLLECTION (test->collection)); |
| 70 |
|
| 71 |
/* Make two credentials */ |
| 72 |
cred = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_CREDENTIAL, NULL, |
| 73 |
attrs, G_N_ELEMENTS (attrs)); |
| 74 |
g_assert (cred != NULL); |
| 75 |
test->credential = gkm_object_get_handle (GKM_OBJECT (cred)); |
| 76 |
g_object_unref (cred); |
| 77 |
|
| 78 |
cred = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_CREDENTIAL, NULL, |
| 79 |
attrs, G_N_ELEMENTS (attrs)); |
| 80 |
g_assert (cred != NULL); |
| 81 |
test->credential2 = gkm_object_get_handle (GKM_OBJECT (cred)); |
| 82 |
g_object_unref (cred); |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
teardown (Test *test, gconstpointer unused) |
| 87 |
{ |
| 88 |
if (test->collection) |
| 89 |
g_object_unref (test->collection); |
| 90 |
test->collection = NULL; |
| 91 |
|
| 92 |
test_secret_module_leave_and_finalize (); |
| 93 |
test->module = NULL; |
| 94 |
test->session = NULL; |
| 95 |
test->credential = 0; |
| 96 |
} |
| 97 |
|
| 98 |
static void |
| 99 |
test_is_locked (Test *test, gconstpointer unused) |
| 100 |
{ |
| 101 |
gboolean locked; |
| 102 |
|
| 103 |
/* By default is locked */ |
| 104 |
locked = gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session); |
| 105 |
g_assert (locked == TRUE); |
| 106 |
} |
| 107 |
|
| 108 |
static void |
| 109 |
test_unlocked_data (Test *test, gconstpointer unused) |
| 110 |
{ |
| 111 |
GkmCredential *cred; |
| 112 |
GkmSecretData *sdata; |
| 113 |
CK_RV rv; |
| 114 |
|
| 115 |
/* Create test->credential, which unlocks test->collection */ |
| 116 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), NULL, 0, &cred); |
| 117 |
g_assert (rv == CKR_OK); |
| 118 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 119 |
g_object_unref (cred); |
| 120 |
|
| 121 |
/* Collection should now be unlocked */ |
| 122 |
sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); |
| 123 |
g_assert (GKM_IS_SECRET_DATA (sdata)); |
| 124 |
g_assert (!gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session)); |
| 125 |
g_object_unref (sdata); |
| 126 |
} |
| 127 |
|
| 128 |
static void |
| 129 |
test_get_filename (Test *test, gconstpointer unused) |
| 130 |
{ |
| 131 |
GkmSecretCollection *other; |
| 132 |
const gchar *filename; |
| 133 |
|
| 134 |
other = g_object_new (GKM_TYPE_SECRET_COLLECTION, |
| 135 |
"module", test->module, |
| 136 |
"identifier", "test", |
| 137 |
"filename", "/tmp/filename.keyring", |
| 138 |
NULL); |
| 139 |
|
| 140 |
filename = gkm_secret_collection_get_filename (other); |
| 141 |
g_assert_cmpstr (filename, ==, "/tmp/filename.keyring"); |
| 142 |
|
| 143 |
g_object_unref (other); |
| 144 |
} |
| 145 |
|
| 146 |
static void |
| 147 |
test_set_filename (Test *test, gconstpointer unused) |
| 148 |
{ |
| 149 |
const gchar *filename; |
| 150 |
|
| 151 |
gkm_secret_collection_set_filename (test->collection, "/tmp/filename.keyring"); |
| 152 |
|
| 153 |
filename = gkm_secret_collection_get_filename (test->collection); |
| 154 |
g_assert_cmpstr (filename, ==, "/tmp/filename.keyring"); |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
test_has_item (Test *test, gconstpointer unused) |
| 159 |
{ |
| 160 |
GkmSecretItem *item; |
| 161 |
|
| 162 |
item = gkm_secret_collection_new_item (test->collection, "testo"); |
| 163 |
g_assert (gkm_secret_collection_has_item (test->collection, item)); |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
test_load_unlock_plain (Test *test, gconstpointer unused) |
| 168 |
{ |
| 169 |
GkmCredential *cred; |
| 170 |
GkmSecretData *sdata; |
| 171 |
GkmDataResult res; |
| 172 |
CK_RV rv; |
| 173 |
|
| 174 |
gkm_secret_collection_set_filename (test->collection, SRCDIR "/pkcs11/secret-store/fixtures/plain.keyring"); |
| 175 |
|
| 176 |
/* Load the data in the file */ |
| 177 |
res = gkm_secret_collection_load (test->collection); |
| 178 |
g_assert (res == GKM_DATA_SUCCESS); |
| 179 |
|
| 180 |
/* Unlock the keyring, which should load again */ |
| 181 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), NULL, 0, &cred); |
| 182 |
g_assert (rv == CKR_OK); |
| 183 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 184 |
g_object_unref (cred); |
| 185 |
|
| 186 |
sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); |
| 187 |
g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); |
| 188 |
test_secret_collection_validate (test->collection, sdata); |
| 189 |
g_object_unref (sdata); |
| 190 |
} |
| 191 |
|
| 192 |
static void |
| 193 |
test_load_unlock_encrypted (Test *test, gconstpointer unused) |
| 194 |
{ |
| 195 |
GkmCredential *cred; |
| 196 |
GkmSecretData *sdata; |
| 197 |
GkmDataResult res; |
| 198 |
CK_RV rv; |
| 199 |
|
| 200 |
gkm_secret_collection_set_filename (test->collection, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring"); |
| 201 |
|
| 202 |
/* Load the data in the file */ |
| 203 |
res = gkm_secret_collection_load (test->collection); |
| 204 |
g_assert (res == GKM_DATA_SUCCESS); |
| 205 |
|
| 206 |
/* Unlock the keyring, which should load again */ |
| 207 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 208 |
(guchar*)"my-keyring-password", 19, &cred); |
| 209 |
g_assert (rv == CKR_OK); |
| 210 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 211 |
g_object_unref (cred); |
| 212 |
|
| 213 |
sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); |
| 214 |
g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); |
| 215 |
test_secret_collection_validate (test->collection, sdata); |
| 216 |
g_object_unref (sdata); |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
test_load_unlock_bad_password (Test *test, gconstpointer unused) |
| 221 |
{ |
| 222 |
GkmCredential *cred; |
| 223 |
GkmDataResult res; |
| 224 |
CK_RV rv; |
| 225 |
|
| 226 |
gkm_secret_collection_set_filename (test->collection, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring"); |
| 227 |
|
| 228 |
/* Load the data in the file */ |
| 229 |
res = gkm_secret_collection_load (test->collection); |
| 230 |
g_assert (res == GKM_DATA_SUCCESS); |
| 231 |
|
| 232 |
/* Unlock the keyring, which should load again */ |
| 233 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 234 |
(guchar*)"wrong", 5, &cred); |
| 235 |
g_assert (rv == CKR_PIN_INCORRECT); |
| 236 |
} |
| 237 |
|
| 238 |
static void |
| 239 |
test_unlock_without_load (Test *test, gconstpointer unused) |
| 240 |
{ |
| 241 |
GkmCredential *cred; |
| 242 |
GkmSecretData *sdata; |
| 243 |
CK_RV rv; |
| 244 |
|
| 245 |
gkm_secret_collection_set_filename (test->collection, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring"); |
| 246 |
|
| 247 |
/* Unlock the keyring, which should load it */ |
| 248 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 249 |
(guchar*)"my-keyring-password", 19, &cred); |
| 250 |
g_assert (rv == CKR_OK); |
| 251 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 252 |
g_object_unref (cred); |
| 253 |
|
| 254 |
sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); |
| 255 |
g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); |
| 256 |
test_secret_collection_validate (test->collection, sdata); |
| 257 |
g_object_unref (sdata); |
| 258 |
} |
| 259 |
|
| 260 |
static void |
| 261 |
test_twice_unlock (Test *test, gconstpointer unused) |
| 262 |
{ |
| 263 |
GkmCredential *cred; |
| 264 |
GkmSecretData *sdata; |
| 265 |
CK_RV rv; |
| 266 |
|
| 267 |
gkm_secret_collection_set_filename (test->collection, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring"); |
| 268 |
|
| 269 |
/* Unlock the keyring, which should load */ |
| 270 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 271 |
(guchar*)"my-keyring-password", 19, &cred); |
| 272 |
g_assert (rv == CKR_OK); |
| 273 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 274 |
g_object_unref (cred); |
| 275 |
|
| 276 |
/* Unlock the keyring again, which should not reload */ |
| 277 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 278 |
(guchar*)"my-keyring-password", 19, &cred); |
| 279 |
g_assert (rv == CKR_OK); |
| 280 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 281 |
g_object_unref (cred); |
| 282 |
|
| 283 |
sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); |
| 284 |
g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); |
| 285 |
test_secret_collection_validate (test->collection, sdata); |
| 286 |
g_object_unref (sdata); |
| 287 |
} |
| 288 |
|
| 289 |
static void |
| 290 |
test_twice_unlock_bad_password (Test *test, gconstpointer unused) |
| 291 |
{ |
| 292 |
GkmCredential *cred; |
| 293 |
GkmSecretData *sdata; |
| 294 |
CK_RV rv; |
| 295 |
|
| 296 |
gkm_secret_collection_set_filename (test->collection, SRCDIR "/pkcs11/secret-store/fixtures/encrypted.keyring"); |
| 297 |
|
| 298 |
/* Unlock the keyring, which should load */ |
| 299 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 300 |
(guchar*)"my-keyring-password", 19, &cred); |
| 301 |
g_assert (rv == CKR_OK); |
| 302 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 303 |
g_object_unref (cred); |
| 304 |
|
| 305 |
/* Unlock the keyring again, wrong password */ |
| 306 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 307 |
(guchar*)"wrong", 5, &cred); |
| 308 |
g_assert (rv == CKR_PIN_INCORRECT); |
| 309 |
|
| 310 |
sdata = gkm_secret_collection_unlocked_use (test->collection, test->session); |
| 311 |
g_assert (sdata != NULL && GKM_IS_SECRET_DATA (sdata)); |
| 312 |
test_secret_collection_validate (test->collection, sdata); |
| 313 |
g_object_unref (sdata); |
| 314 |
} |
| 315 |
|
| 316 |
static void |
| 317 |
test_memory_unlock (Test *test, gconstpointer unused) |
| 318 |
{ |
| 319 |
GkmCredential *cred; |
| 320 |
GkmDataResult res; |
| 321 |
CK_RV rv; |
| 322 |
|
| 323 |
/* Load the data in the file */ |
| 324 |
res = gkm_secret_collection_load (test->collection); |
| 325 |
g_assert (res == GKM_DATA_SUCCESS); |
| 326 |
|
| 327 |
/* Unlock the keyring, which should load again */ |
| 328 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 329 |
NULL, 0, &cred); |
| 330 |
g_assert (rv == CKR_OK); |
| 331 |
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred)); |
| 332 |
g_object_unref (cred); |
| 333 |
} |
| 334 |
|
| 335 |
static void |
| 336 |
test_memory_unlock_bad_password (Test *test, gconstpointer unused) |
| 337 |
{ |
| 338 |
GkmCredential *cred; |
| 339 |
GkmDataResult res; |
| 340 |
CK_RV rv; |
| 341 |
|
| 342 |
/* Load the data in the file */ |
| 343 |
res = gkm_secret_collection_load (test->collection); |
| 344 |
g_assert (res == GKM_DATA_SUCCESS); |
| 345 |
|
| 346 |
/* Unlock the keyring, which should load again */ |
| 347 |
rv = gkm_credential_create (test->module, gkm_session_get_manager (test->session), GKM_OBJECT (test->collection), |
| 348 |
(guchar*)"wrong", 5, &cred); |
| 349 |
g_assert (rv == CKR_PIN_INCORRECT); |
| 350 |
} |
| 351 |
|
| 352 |
static void |
| 353 |
test_factory (Test *test, gconstpointer unused) |
| 354 |
{ |
| 355 |
CK_OBJECT_CLASS klass = CKO_G_COLLECTION; |
| 356 |
GkmObject *object; |
| 357 |
|
| 358 |
CK_ATTRIBUTE attrs[] = { |
| 359 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 360 |
{ CKA_LABEL, "blah", 4 }, |
| 361 |
{ CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, |
| 362 |
}; |
| 363 |
|
| 364 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 365 |
attrs, G_N_ELEMENTS (attrs)); |
| 366 |
g_assert (object != NULL); |
| 367 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 368 |
|
| 369 |
g_assert_cmpstr (gkm_secret_object_get_label (GKM_SECRET_OBJECT (object)), ==, "blah"); |
| 370 |
g_object_unref (object); |
| 371 |
} |
| 372 |
|
| 373 |
static void |
| 374 |
test_factory_unnamed (Test *test, gconstpointer unused) |
| 375 |
{ |
| 376 |
CK_OBJECT_CLASS klass = CKO_G_COLLECTION; |
| 377 |
const gchar *identifier; |
| 378 |
GkmObject *object; |
| 379 |
|
| 380 |
CK_ATTRIBUTE attrs[] = { |
| 381 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 382 |
{ CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, |
| 383 |
}; |
| 384 |
|
| 385 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 386 |
attrs, G_N_ELEMENTS (attrs)); |
| 387 |
g_assert (object != NULL); |
| 388 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 389 |
|
| 390 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); |
| 391 |
g_assert_cmpstr (identifier, !=, ""); |
| 392 |
g_object_unref (object); |
| 393 |
} |
| 394 |
|
| 395 |
static void |
| 396 |
test_factory_token (Test *test, gconstpointer unused) |
| 397 |
{ |
| 398 |
CK_OBJECT_CLASS klass = CKO_G_COLLECTION; |
| 399 |
const gchar *identifier; |
| 400 |
GkmObject *object; |
| 401 |
CK_BBOOL token = CK_TRUE; |
| 402 |
|
| 403 |
CK_ATTRIBUTE attrs[] = { |
| 404 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 405 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 406 |
{ CKA_LABEL, "blah", 4 }, |
| 407 |
{ CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, |
| 408 |
}; |
| 409 |
|
| 410 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 411 |
attrs, G_N_ELEMENTS (attrs)); |
| 412 |
g_assert (object != NULL); |
| 413 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 414 |
|
| 415 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); |
| 416 |
g_assert (strstr (identifier, "blah")); |
| 417 |
g_object_unref (object); |
| 418 |
} |
| 419 |
|
| 420 |
static void |
| 421 |
test_factory_duplicate (Test *test, gconstpointer unused) |
| 422 |
{ |
| 423 |
CK_OBJECT_CLASS klass = CKO_G_COLLECTION; |
| 424 |
const gchar *identifier1, *identifier2; |
| 425 |
GkmObject *object; |
| 426 |
|
| 427 |
CK_ATTRIBUTE attrs[] = { |
| 428 |
{ CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, |
| 429 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 430 |
{ CKA_LABEL, "blah", 4 }, |
| 431 |
}; |
| 432 |
|
| 433 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 434 |
attrs, G_N_ELEMENTS (attrs)); |
| 435 |
g_assert (object != NULL); |
| 436 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 437 |
|
| 438 |
identifier1 = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); |
| 439 |
g_assert (strstr (identifier1, "blah")); |
| 440 |
g_object_unref (object); |
| 441 |
|
| 442 |
/* Use second test->credential for second object */ |
| 443 |
attrs[0].pValue = &test->credential2; |
| 444 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 445 |
attrs, G_N_ELEMENTS (attrs)); |
| 446 |
g_assert (object != NULL); |
| 447 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 448 |
|
| 449 |
identifier2 = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); |
| 450 |
g_assert (strstr (identifier2, "blah")); |
| 451 |
g_object_unref (object); |
| 452 |
|
| 453 |
g_assert_cmpstr (identifier1, !=, identifier2); |
| 454 |
} |
| 455 |
|
| 456 |
static void |
| 457 |
test_factory_item (Test *test, gconstpointer unused) |
| 458 |
{ |
| 459 |
CK_OBJECT_CLASS c_klass = CKO_G_COLLECTION; |
| 460 |
CK_OBJECT_CLASS i_klass = CKO_SECRET_KEY; |
| 461 |
const gchar *identifier; |
| 462 |
GkmObject *object; |
| 463 |
CK_BBOOL token = CK_TRUE; |
| 464 |
|
| 465 |
CK_ATTRIBUTE c_attrs[] = { |
| 466 |
{ CKA_CLASS, &c_klass, sizeof (c_klass) }, |
| 467 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 468 |
{ CKA_LABEL, "three", 5 }, |
| 469 |
{ CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, |
| 470 |
}; |
| 471 |
|
| 472 |
CK_ATTRIBUTE i_attrs[] = { |
| 473 |
{ CKA_G_COLLECTION, NULL, 0 }, /* Filled below */ |
| 474 |
{ CKA_CLASS, &i_klass, sizeof (i_klass) }, |
| 475 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 476 |
{ CKA_LABEL, "Item", 4 }, |
| 477 |
}; |
| 478 |
|
| 479 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 480 |
c_attrs, G_N_ELEMENTS (c_attrs)); |
| 481 |
g_assert (object != NULL); |
| 482 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 483 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); |
| 484 |
g_object_unref (object); |
| 485 |
|
| 486 |
i_attrs[0].pValue = (gpointer)identifier; |
| 487 |
i_attrs[0].ulValueLen = strlen (identifier); |
| 488 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_ITEM, NULL, |
| 489 |
i_attrs, G_N_ELEMENTS (i_attrs)); |
| 490 |
g_assert (object != NULL); |
| 491 |
g_assert (GKM_IS_SECRET_ITEM (object)); |
| 492 |
g_object_unref (object); |
| 493 |
} |
| 494 |
|
| 495 |
static void |
| 496 |
test_token_remove (Test *test, gconstpointer unused) |
| 497 |
{ |
| 498 |
CK_OBJECT_CLASS klass = CKO_G_COLLECTION; |
| 499 |
GkmTransaction *transaction; |
| 500 |
GkmObject *object; |
| 501 |
CK_BBOOL token = CK_TRUE; |
| 502 |
|
| 503 |
CK_ATTRIBUTE attrs[] = { |
| 504 |
{ CKA_CLASS, &klass, sizeof (klass) }, |
| 505 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 506 |
{ CKA_LABEL, "blah", 4 }, |
| 507 |
{ CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, |
| 508 |
}; |
| 509 |
|
| 510 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 511 |
attrs, G_N_ELEMENTS (attrs)); |
| 512 |
g_assert (object != NULL); |
| 513 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 514 |
|
| 515 |
transaction = gkm_transaction_new (); |
| 516 |
gkm_module_remove_token_object (test->module, transaction, object); |
| 517 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 518 |
gkm_transaction_complete (transaction); |
| 519 |
g_object_unref (transaction); |
| 520 |
g_object_unref (object); |
| 521 |
} |
| 522 |
|
| 523 |
static void |
| 524 |
test_token_item_remove (Test *test, gconstpointer unused) |
| 525 |
{ |
| 526 |
CK_OBJECT_CLASS c_klass = CKO_G_COLLECTION; |
| 527 |
CK_OBJECT_CLASS i_klass = CKO_SECRET_KEY; |
| 528 |
GkmTransaction *transaction; |
| 529 |
const gchar *identifier; |
| 530 |
GkmObject *object; |
| 531 |
CK_BBOOL token = CK_TRUE; |
| 532 |
|
| 533 |
CK_ATTRIBUTE c_attrs[] = { |
| 534 |
{ CKA_CLASS, &c_klass, sizeof (c_klass) }, |
| 535 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 536 |
{ CKA_LABEL, "three", 5 }, |
| 537 |
{ CKA_G_CREDENTIAL, &test->credential, sizeof (test->credential) }, |
| 538 |
}; |
| 539 |
|
| 540 |
CK_ATTRIBUTE i_attrs[] = { |
| 541 |
{ CKA_G_COLLECTION, NULL, 0 }, /* Filled below */ |
| 542 |
{ CKA_CLASS, &i_klass, sizeof (i_klass) }, |
| 543 |
{ CKA_TOKEN, &token, sizeof (token) }, |
| 544 |
{ CKA_LABEL, "Item", 4 }, |
| 545 |
}; |
| 546 |
|
| 547 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_COLLECTION, NULL, |
| 548 |
c_attrs, G_N_ELEMENTS (c_attrs)); |
| 549 |
g_assert (object != NULL); |
| 550 |
g_assert (GKM_IS_SECRET_COLLECTION (object)); |
| 551 |
identifier = gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (object)); |
| 552 |
g_object_unref (object); |
| 553 |
|
| 554 |
i_attrs[0].pValue = (gpointer)identifier; |
| 555 |
i_attrs[0].ulValueLen = strlen (identifier); |
| 556 |
object = gkm_session_create_object_for_factory (test->session, GKM_FACTORY_SECRET_ITEM, NULL, |
| 557 |
i_attrs, G_N_ELEMENTS (i_attrs)); |
| 558 |
g_assert (object != NULL); |
| 559 |
g_assert (GKM_IS_SECRET_ITEM (object)); |
| 560 |
|
| 561 |
transaction = gkm_transaction_new (); |
| 562 |
gkm_module_remove_token_object (test->module, transaction, object); |
| 563 |
g_assert (!gkm_transaction_get_failed (transaction)); |
| 564 |
gkm_transaction_complete (transaction); |
| 565 |
g_object_unref (transaction); |
| 566 |
g_object_unref (object); |
| 567 |
} |
| 568 |
|
| 569 |
int |
| 570 |
main (int argc, char **argv) |
| 571 |
{ |
| 572 |
#if !GLIB_CHECK_VERSION(2,35,0) |
| 573 |
g_type_init (); |
| 574 |
#endif |
| 575 |
g_test_init (&argc, &argv, NULL); |
| 576 |
|
| 577 |
g_test_add ("/secret-store/collection/is_locked", Test, NULL, setup, test_is_locked, teardown); |
| 578 |
g_test_add ("/secret-store/collection/unlocked_data", Test, NULL, setup, test_unlocked_data, teardown); |
| 579 |
g_test_add ("/secret-store/collection/get_filename", Test, NULL, setup, test_get_filename, teardown); |
| 580 |
g_test_add ("/secret-store/collection/set_filename", Test, NULL, setup, test_set_filename, teardown); |
| 581 |
g_test_add ("/secret-store/collection/has_item", Test, NULL, setup, test_has_item, teardown); |
| 582 |
g_test_add ("/secret-store/collection/load_unlock_plain", Test, NULL, setup, test_load_unlock_plain, teardown); |
| 583 |
g_test_add ("/secret-store/collection/load_unlock_encrypted", Test, NULL, setup, test_load_unlock_encrypted, teardown); |
| 584 |
g_test_add ("/secret-store/collection/load_unlock_bad_password", Test, NULL, setup, test_load_unlock_bad_password, teardown); |
| 585 |
g_test_add ("/secret-store/collection/unlock_without_load", Test, NULL, setup, test_unlock_without_load, teardown); |
| 586 |
g_test_add ("/secret-store/collection/twice_unlock", Test, NULL, setup, test_twice_unlock, teardown); |
| 587 |
g_test_add ("/secret-store/collection/twice_unlock_bad_password", Test, NULL, setup, test_twice_unlock_bad_password, teardown); |
| 588 |
g_test_add ("/secret-store/collection/memory_unlock", Test, NULL, setup, test_memory_unlock, teardown); |
| 589 |
g_test_add ("/secret-store/collection/memory_unlock_bad_password", Test, NULL, setup, test_memory_unlock_bad_password, teardown); |
| 590 |
g_test_add ("/secret-store/collection/factory", Test, NULL, setup, test_factory, teardown); |
| 591 |
g_test_add ("/secret-store/collection/factory_unnamed", Test, NULL, setup, test_factory_unnamed, teardown); |
| 592 |
g_test_add ("/secret-store/collection/factory_token", Test, NULL, setup, test_factory_token, teardown); |
| 593 |
g_test_add ("/secret-store/collection/factory_duplicate", Test, NULL, setup, test_factory_duplicate, teardown); |
| 594 |
g_test_add ("/secret-store/collection/factory_item", Test, NULL, setup, test_factory_item, teardown); |
| 595 |
g_test_add ("/secret-store/collection/token_remove", Test, NULL, setup, test_token_remove, teardown); |
| 596 |
g_test_add ("/secret-store/collection/token_item_remove", Test, NULL, setup, test_token_item_remove, teardown); |
| 597 |
|
| 598 |
return g_test_run (); |
| 599 |
} |