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 "test-suite.h"
27
#include "test-secret-module.h"
29
#include "gkm-secret-collection.h"
30
#include "gkm-secret-fields.h"
31
#include "gkm-secret-item.h"
33
#include "gkm/gkm-credential.h"
34
#include "gkm/gkm-session.h"
35
#include "gkm/gkm-transaction.h"
37
#include "pkcs11/pkcs11i.h"
45
static GkmModule *module = NULL;
46
static GkmSession *session = NULL;
47
static GkmSecretCollection *collection = NULL;
49
TESTING_SETUP(secret_item)
51
module = test_secret_module_initialize_and_enter ();
52
session = test_secret_module_open_session (TRUE);
54
collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
60
TESTING_TEARDOWN(secret_item)
63
g_object_unref (collection);
66
test_secret_module_leave_and_finalize ();
73
unlock_collection(void)
79
/* Create credential, which unlocks collection */
80
object = GKM_OBJECT (collection);
81
rv = gkm_credential_create (gkm_object_get_module (object),
82
gkm_session_get_manager (session),
83
object, NULL, 0, &cred);
84
g_assert (rv == CKR_OK);
86
gkm_session_add_session_object (session, NULL, GKM_OBJECT (cred));
87
g_object_unref (cred);
90
TESTING_TEST(secret_item_new)
94
item = gkm_secret_collection_new_item (collection, "the-identifier");
95
g_assert (GKM_IS_SECRET_ITEM (item));
96
g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier");
99
TESTING_TEST(secret_item_create)
101
GkmTransaction *transaction;
104
transaction = gkm_transaction_new ();
105
item = gkm_secret_collection_create_item (collection, transaction);
106
g_assert (GKM_IS_SECRET_ITEM (item));
108
g_assert (gkm_secret_collection_has_item (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 (collection, item));
115
g_object_unref (item);
118
TESTING_TEST(secret_item_create_failed)
120
GkmTransaction *transaction;
123
transaction = gkm_transaction_new ();
124
item = gkm_secret_collection_create_item (collection, transaction);
125
g_assert (GKM_IS_SECRET_ITEM (item));
127
g_assert (gkm_secret_collection_has_item (collection, item));
129
gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
130
gkm_transaction_complete (transaction);
131
g_object_unref (transaction);
133
/* Should no longer be there */
134
g_assert (!gkm_secret_collection_has_item (collection, item));
135
g_object_unref (item);
138
TESTING_TEST(secret_item_destroy)
140
GkmTransaction *transaction;
143
item = gkm_secret_collection_new_item (collection, "the-identifier");
144
g_assert (gkm_secret_collection_has_item (collection, item));
147
transaction = gkm_transaction_new ();
148
gkm_secret_collection_destroy_item (collection, transaction, item);
149
g_assert (!gkm_secret_collection_has_item (collection, item));
151
gkm_transaction_complete (transaction);
152
g_object_unref (transaction);
154
/* Should not be there */
155
g_assert (!gkm_secret_collection_has_item (collection, item));
156
g_object_unref (item);
159
TESTING_TEST(secret_item_destroy_failed)
161
GkmTransaction *transaction;
164
item = gkm_secret_collection_new_item (collection, "the-identifier");
165
g_assert (gkm_secret_collection_has_item (collection, item));
168
transaction = gkm_transaction_new ();
169
gkm_secret_collection_destroy_item (collection, transaction, item);
170
g_assert (!gkm_secret_collection_has_item (collection, item));
172
gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
173
gkm_transaction_complete (transaction);
174
g_object_unref (transaction);
176
/* Should be there */
177
g_assert (gkm_secret_collection_has_item (collection, item));
178
g_object_unref (item);
181
TESTING_TEST(secret_item_collection_get)
183
GkmSecretItem *item, *check;
185
item = gkm_secret_collection_new_item (collection, "the-identifier");
186
g_assert (GKM_IS_SECRET_ITEM (item));
188
check = gkm_secret_collection_get_item (collection, "the-identifier");
189
g_assert (item == check);
192
TESTING_TEST(secret_item_collection_items)
195
const gchar *identifier;
197
gkm_secret_collection_new_item (collection, "one-identifier");
198
gkm_secret_collection_new_item (collection, "two-identifier");
199
gkm_secret_collection_new_item (collection, "three-identifier");
201
items = gkm_secret_collection_get_items (collection);
202
g_assert_cmpuint (g_list_length (items), ==, 3);
203
for (l = items; l; l = g_list_next (l)) {
204
identifier = gkm_secret_object_get_identifier (l->data);
205
if (!g_str_equal (identifier, "one-identifier") &&
206
!g_str_equal (identifier, "two-identifier") &&
207
!g_str_equal (identifier, "three-identifier"))
208
g_assert_not_reached ();
214
TESTING_TEST(secret_item_collection_remove)
218
item = gkm_secret_collection_new_item (collection, "the-identifier");
219
g_assert (gkm_secret_collection_get_item (collection, "the-identifier") == item);
221
gkm_secret_collection_remove_item (collection, item);
222
g_assert (gkm_secret_collection_get_item (collection, "the-identifier") == NULL);
225
TESTING_TEST(secret_item_is_locked)
229
item = gkm_secret_collection_new_item (collection, "the-identifier");
230
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) ==
231
gkm_secret_object_is_locked (GKM_SECRET_OBJECT (collection), session));
235
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == FALSE);
238
TESTING_TEST(secret_item_get_collection)
241
item = gkm_secret_collection_new_item (collection, "the-identifier");
242
g_assert (gkm_secret_item_get_collection (item) == collection);
245
TESTING_TEST(secret_item_tracks_collection)
248
item = gkm_secret_collection_new_item (collection, "the-identifier");
253
/* At this point the item should be 'unlocked' */
254
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == FALSE);
256
g_object_unref (collection);
259
/* Collection went away */
260
g_assert (gkm_secret_item_get_collection (item) == NULL);
261
g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), session) == TRUE);
263
g_object_unref (item);
266
TESTING_TEST(secret_item_get_set_fields)
268
GHashTable *fields = gkm_secret_fields_new ();
272
item = gkm_secret_collection_new_item (collection, "the-identifier");
273
gkm_secret_item_set_fields (item, fields);
274
gkm_secret_item_set_fields (item, fields);
276
check = gkm_secret_item_get_fields (item);
277
g_assert (check == fields);
279
g_hash_table_unref (fields);
282
TESTING_TEST(secret_item_collection_attr)
285
CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 };
289
item = gkm_secret_collection_new_item (collection, "the-identifier");
290
rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
291
g_assert (rv == CKR_OK);
292
g_assert (check.ulValueLen == 4);
293
g_assert (memcmp (buffer, "test", 4) == 0);
296
TESTING_TEST(secret_item_secret_attr)
298
GkmTransaction *transaction = gkm_transaction_new ();
299
CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
301
CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
305
unlock_collection ();
307
item = gkm_secret_collection_new_item (collection, "the-identifier");
308
gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
309
g_assert (gkm_transaction_get_failed (transaction) == FALSE);
310
gkm_transaction_complete (transaction);
311
g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
313
g_object_unref (transaction);
315
rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
316
g_assert (rv == CKR_OK);
317
g_assert (check.ulValueLen == 5);
318
g_assert (memcmp (buffer, "hello", 5) == 0);
321
TESTING_TEST(secret_item_secret_attr_locked)
323
GkmTransaction *transaction = gkm_transaction_new ();
324
CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
326
CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
330
item = gkm_secret_collection_new_item (collection, "the-identifier");
331
gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
332
g_assert (gkm_transaction_get_failed (transaction) == TRUE);
333
gkm_transaction_complete (transaction);
334
g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
336
g_object_unref (transaction);
338
rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
339
g_assert (rv == CKR_USER_NOT_LOGGED_IN);
342
TESTING_TEST(secret_item_fields_attr)
344
GkmTransaction *transaction = gkm_transaction_new ();
345
CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
347
CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
353
unlock_collection ();
355
item = gkm_secret_collection_new_item (collection, "the-identifier");
356
gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
357
g_assert (gkm_transaction_get_failed (transaction) == FALSE);
358
gkm_transaction_complete (transaction);
359
g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
361
g_object_unref (transaction);
363
rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
364
g_assert (rv == CKR_OK);
365
g_assert (check.ulValueLen == 26);
366
g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0);
368
fields = gkm_secret_item_get_fields (item);
370
value = gkm_secret_fields_get (fields, "name1");
371
g_assert_cmpstr (value, ==, "value1");
372
value = gkm_secret_fields_get (fields, "name2");
373
g_assert_cmpstr (value, ==, "value2");
376
TESTING_TEST(secret_item_fields_attr_locked)
378
GkmTransaction *transaction = gkm_transaction_new ();
379
CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
382
item = gkm_secret_collection_new_item (collection, "the-identifier");
383
gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
384
g_assert (gkm_transaction_get_failed (transaction) == TRUE);
385
gkm_transaction_complete (transaction);
386
g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
388
g_object_unref (transaction);
391
TESTING_TEST(secret_item_fields_attr_reverts)
393
GkmTransaction *transaction = gkm_transaction_new ();
394
CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 };
396
CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
401
unlock_collection ();
403
item = gkm_secret_collection_new_item (collection, "the-identifier");
405
/* Set the old value like so */
406
fields = gkm_secret_fields_new ();
407
gkm_secret_fields_add (fields, "old", "value");
408
gkm_secret_item_set_fields (item, fields);
409
g_hash_table_unref (fields);
411
/* Should show old value */
412
rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
413
g_assert (rv == CKR_OK);
414
g_assert (check.ulValueLen == 10);
415
g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
417
/* Set the new values */
418
gkm_object_set_attribute (GKM_OBJECT (item), session, transaction, &attr);
419
g_assert (gkm_transaction_get_failed (transaction) == FALSE);
421
/* Should have the new value */
422
rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
423
g_assert (rv == CKR_OK);
424
g_assert (check.ulValueLen == 10);
425
g_assert (memcmp (buffer, "new\0value\0", 10) == 0);
427
/* Fail the transaction */
428
gkm_transaction_fail (transaction, CKR_CANCEL);
429
gkm_transaction_complete (transaction);
431
/* Should show the old value */
432
rv = gkm_object_get_attribute (GKM_OBJECT (item), session, &check);
433
g_assert (rv == CKR_OK);
434
g_assert (check.ulValueLen == 10);
435
g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
437
g_object_unref (transaction);