1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
/* unit-test-secret-item.c: Test secret item
4
Copyright (C) 2009 Stefan Walter
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.
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.
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
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19
Boston, MA 02111-1307, USA.
21
Author: Stef Walter <stef@memberwebs.com>
26
#include "mock-secret-module.h"
28
#include "secret-store/gkm-secret-collection.h"
29
#include "secret-store/gkm-secret-fields.h"
30
#include "secret-store/gkm-secret-item.h"
32
#include "gkm/gkm-credential.h"
33
#include "gkm/gkm-session.h"
34
#include "gkm/gkm-transaction.h"
36
#include "pkcs11/pkcs11i.h"
47
GkmSecretCollection *collection;
51
setup (Test *test, gconstpointer unused)
53
test->module = test_secret_module_initialize_and_enter ();
54
test->session = test_secret_module_open_session (TRUE);
56
test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
57
"module", test->module,
63
teardown (Test *test, gconstpointer unused)
66
g_object_unref (test->collection);
67
test_secret_module_leave_and_finalize ();
71
unlock_collection (Test *test)
77
/* Create credential, which unlocks test->collection */
78
object = GKM_OBJECT (test->collection);
79
rv = gkm_credential_create (gkm_object_get_module (object),
80
gkm_session_get_manager (test->session),
81
object, NULL, 0, &cred);
82
g_assert (rv == CKR_OK);
84
gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred));
85
g_object_unref (cred);
89
test_new (Test *test, gconstpointer unused)
93
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
94
g_assert (GKM_IS_SECRET_ITEM (item));
95
g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier");
99
test_create (Test *test, gconstpointer unused)
101
GkmTransaction *transaction;
104
transaction = gkm_transaction_new ();
105
item = gkm_secret_collection_create_item (test->collection, transaction);
106
g_assert (GKM_IS_SECRET_ITEM (item));
108
g_assert (gkm_secret_collection_has_item (test->collection, item));
110
gkm_transaction_complete (transaction);
111
g_object_unref (transaction);
113
/* Should still be there */
114
g_assert (gkm_secret_collection_has_item (test->collection, item));
115
g_object_unref (item);
119
test_create_failed (Test *test, gconstpointer unused)
121
GkmTransaction *transaction;
124
transaction = gkm_transaction_new ();
125
item = gkm_secret_collection_create_item (test->collection, transaction);
126
g_assert (GKM_IS_SECRET_ITEM (item));
128
g_assert (gkm_secret_collection_has_item (test->collection, item));
130
gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
131
gkm_transaction_complete (transaction);
132
g_object_unref (transaction);
134
/* Should no longer be there */
135
g_assert (!gkm_secret_collection_has_item (test->collection, item));
136
g_object_unref (item);
140
test_destroy (Test *test, gconstpointer unused)
142
GkmTransaction *transaction;
145
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
146
g_assert (gkm_secret_collection_has_item (test->collection, item));
149
transaction = gkm_transaction_new ();
150
gkm_secret_collection_destroy_item (test->collection, transaction, item);
151
g_assert (!gkm_secret_collection_has_item (test->collection, item));
153
gkm_transaction_complete (transaction);
154
g_object_unref (transaction);
156
/* Should not be there */
157
g_assert (!gkm_secret_collection_has_item (test->collection, item));
158
g_object_unref (item);
162
test_destroy_failed (Test *test, gconstpointer unused)
164
GkmTransaction *transaction;
167
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
168
g_assert (gkm_secret_collection_has_item (test->collection, item));
171
transaction = gkm_transaction_new ();
172
gkm_secret_collection_destroy_item (test->collection, transaction, item);
173
g_assert (!gkm_secret_collection_has_item (test->collection, item));
175
gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
176
gkm_transaction_complete (transaction);
177
g_object_unref (transaction);
179
/* Should be there */
180
g_assert (gkm_secret_collection_has_item (test->collection, item));
181
g_object_unref (item);
185
test_collection_get (Test *test, gconstpointer unused)
187
GkmSecretItem *item, *check;
189
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
190
g_assert (GKM_IS_SECRET_ITEM (item));
192
check = gkm_secret_collection_get_item (test->collection, "the-identifier");
193
g_assert (item == check);
197
test_collection_items (Test *test, gconstpointer unused)
200
const gchar *identifier;
202
gkm_secret_collection_new_item (test->collection, "one-identifier");
203
gkm_secret_collection_new_item (test->collection, "two-identifier");
204
gkm_secret_collection_new_item (test->collection, "three-identifier");
206
items = gkm_secret_collection_get_items (test->collection);
207
g_assert_cmpuint (g_list_length (items), ==, 3);
208
for (l = items; l; l = g_list_next (l)) {
209
identifier = gkm_secret_object_get_identifier (l->data);
210
if (!g_str_equal (identifier, "one-identifier") &&
211
!g_str_equal (identifier, "two-identifier") &&
212
!g_str_equal (identifier, "three-identifier"))
213
g_assert_not_reached ();
220
test_collection_remove (Test *test, gconstpointer unused)
224
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
225
g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == item);
227
gkm_secret_collection_remove_item (test->collection, item);
228
g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == NULL);
232
test_is_locked (Test *test, gconstpointer unused)
236
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
237
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) ==
238
gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session));
240
unlock_collection (test);
242
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
246
test_get_collection (Test *test, gconstpointer unused)
249
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
250
g_assert (gkm_secret_item_get_collection (item) == test->collection);
254
test_tracks_collection (Test *test, gconstpointer unused)
257
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
260
unlock_collection (test);
262
/* At this point the item should be 'unlocked' */
263
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
265
g_object_unref (test->collection);
266
test->collection = NULL;
268
/* Collection went away */
269
g_assert (gkm_secret_item_get_collection (item) == NULL);
270
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == TRUE);
272
g_object_unref (item);
276
test_get_set_fields (Test *test, gconstpointer unused)
278
GHashTable *fields = gkm_secret_fields_new ();
282
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
283
gkm_secret_item_set_fields (item, fields);
284
gkm_secret_item_set_fields (item, fields);
286
check = gkm_secret_item_get_fields (item);
287
g_assert (check == fields);
289
g_hash_table_unref (fields);
293
test_collection_attr (Test *test, gconstpointer unused)
296
CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 };
300
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
301
rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
302
g_assert (rv == CKR_OK);
303
g_assert (check.ulValueLen == 4);
304
g_assert (memcmp (buffer, "test", 4) == 0);
308
test_secret_attr (Test *test, gconstpointer unused)
310
GkmTransaction *transaction = gkm_transaction_new ();
311
CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
313
CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
317
unlock_collection (test);
319
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
320
gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
321
g_assert (gkm_transaction_get_failed (transaction) == FALSE);
322
gkm_transaction_complete (transaction);
323
g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
325
g_object_unref (transaction);
327
rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
328
g_assert (rv == CKR_OK);
329
g_assert (check.ulValueLen == 5);
330
g_assert (memcmp (buffer, "hello", 5) == 0);
334
test_secret_attr_locked (Test *test, gconstpointer unused)
336
GkmTransaction *transaction = gkm_transaction_new ();
337
CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
339
CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
343
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
344
gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
345
g_assert (gkm_transaction_get_failed (transaction) == TRUE);
346
gkm_transaction_complete (transaction);
347
g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
349
g_object_unref (transaction);
351
rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
352
g_assert (rv == CKR_USER_NOT_LOGGED_IN);
356
test_fields_attr (Test *test, gconstpointer unused)
358
GkmTransaction *transaction = gkm_transaction_new ();
359
CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
361
CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
367
unlock_collection (test);
369
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
370
gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
371
g_assert (gkm_transaction_get_failed (transaction) == FALSE);
372
gkm_transaction_complete (transaction);
373
g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
375
g_object_unref (transaction);
377
rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
378
g_assert (rv == CKR_OK);
379
g_assert (check.ulValueLen == 26);
380
g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0);
382
fields = gkm_secret_item_get_fields (item);
384
value = gkm_secret_fields_get (fields, "name1");
385
g_assert_cmpstr (value, ==, "value1");
386
value = gkm_secret_fields_get (fields, "name2");
387
g_assert_cmpstr (value, ==, "value2");
391
test_fields_attr_locked (Test *test, gconstpointer unused)
393
GkmTransaction *transaction = gkm_transaction_new ();
394
CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
397
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
398
gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
399
g_assert (gkm_transaction_get_failed (transaction) == TRUE);
400
gkm_transaction_complete (transaction);
401
g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
403
g_object_unref (transaction);
407
test_fields_attr_reverts (Test *test, gconstpointer unused)
409
GkmTransaction *transaction = gkm_transaction_new ();
410
CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 };
412
CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
417
unlock_collection (test);
419
item = gkm_secret_collection_new_item (test->collection, "the-identifier");
421
/* Set the old value like so */
422
fields = gkm_secret_fields_new ();
423
gkm_secret_fields_add (fields, "old", "value");
424
gkm_secret_item_set_fields (item, fields);
425
g_hash_table_unref (fields);
427
/* Should show old value */
428
rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
429
g_assert (rv == CKR_OK);
430
g_assert (check.ulValueLen == 10);
431
g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
433
/* Set the new values */
434
gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
435
g_assert (gkm_transaction_get_failed (transaction) == FALSE);
437
/* Should have the new value */
438
rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
439
g_assert (rv == CKR_OK);
440
g_assert (check.ulValueLen == 10);
441
g_assert (memcmp (buffer, "new\0value\0", 10) == 0);
443
/* Fail the transaction */
444
gkm_transaction_fail (transaction, CKR_CANCEL);
445
gkm_transaction_complete (transaction);
447
/* Should show the old value */
448
rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
449
g_assert (rv == CKR_OK);
450
g_assert (check.ulValueLen == 10);
451
g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
453
g_object_unref (transaction);
457
main (int argc, char **argv)
460
g_test_init (&argc, &argv, NULL);
462
g_test_add ("/secret-store/item/new", Test, NULL, setup, test_new, teardown);
463
g_test_add ("/secret-store/item/create", Test, NULL, setup, test_create, teardown);
464
g_test_add ("/secret-store/item/create_failed", Test, NULL, setup, test_create_failed, teardown);
465
g_test_add ("/secret-store/item/destroy", Test, NULL, setup, test_destroy, teardown);
466
g_test_add ("/secret-store/item/destroy_failed", Test, NULL, setup, test_destroy_failed, teardown);
467
g_test_add ("/secret-store/item/collection_get", Test, NULL, setup, test_collection_get, teardown);
468
g_test_add ("/secret-store/item/collection_items", Test, NULL, setup, test_collection_items, teardown);
469
g_test_add ("/secret-store/item/collection_remove", Test, NULL, setup, test_collection_remove, teardown);
470
g_test_add ("/secret-store/item/is_locked", Test, NULL, setup, test_is_locked, teardown);
471
g_test_add ("/secret-store/item/get_collection", Test, NULL, setup, test_get_collection, teardown);
472
g_test_add ("/secret-store/item/tracks_collection", Test, NULL, setup, test_tracks_collection, teardown);
473
g_test_add ("/secret-store/item/get_set_fields", Test, NULL, setup, test_get_set_fields, teardown);
474
g_test_add ("/secret-store/item/collection_attr", Test, NULL, setup, test_collection_attr, teardown);
475
g_test_add ("/secret-store/item/secret_attr", Test, NULL, setup, test_secret_attr, teardown);
476
g_test_add ("/secret-store/item/secret_attr_locked", Test, NULL, setup, test_secret_attr_locked, teardown);
477
g_test_add ("/secret-store/item/fields_attr", Test, NULL, setup, test_fields_attr, teardown);
478
g_test_add ("/secret-store/item/fields_attr_locked", Test, NULL, setup, test_fields_attr_locked, teardown);
479
g_test_add ("/secret-store/item/fields_attr_reverts", Test, NULL, setup, test_fields_attr_reverts, teardown);
481
return g_test_run ();