Debian Codegraph @

pkcs11/secret-store/test-secret-item.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* unit-test-secret-item.c: Test secret item
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-collection.h"
28 #include "secret-store/gkm-secret-fields.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 GkmSecretCollection *collection;
47 } Test;
48
49 static void
50 setup (Test *test, gconstpointer unused)
51 {
52 test->module = test_secret_module_initialize_and_enter ();
53 test->session = test_secret_module_open_session (TRUE);
54
55 test->collection = g_object_new (GKM_TYPE_SECRET_COLLECTION,
56 "module", test->module,
57 "identifier", "test",
58 NULL);
59 }
60
61 static void
62 teardown (Test *test, gconstpointer unused)
63 {
64 if (test->collection)
65 g_object_unref (test->collection);
66 test_secret_module_leave_and_finalize ();
67 }
68
69 static void
70 unlock_collection (Test *test)
71 {
72 GkmCredential *cred;
73 GkmObject *object;
74 CK_RV rv;
75
76 /* Create credential, which unlocks test->collection */
77 object = GKM_OBJECT (test->collection);
78 rv = gkm_credential_create (gkm_object_get_module (object),
79 gkm_session_get_manager (test->session),
80 object, NULL, 0, &cred);
81 g_assert (rv == CKR_OK);
82
83 gkm_session_add_session_object (test->session, NULL, GKM_OBJECT (cred));
84 g_object_unref (cred);
85 }
86
87 static void
88 test_new (Test *test, gconstpointer unused)
89 {
90 GkmSecretItem *item;
91
92 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
93 g_assert (GKM_IS_SECRET_ITEM (item));
94 g_assert_cmpstr (gkm_secret_object_get_identifier (GKM_SECRET_OBJECT (item)), ==, "the-identifier");
95 }
96
97 static void
98 test_create (Test *test, gconstpointer unused)
99 {
100 GkmTransaction *transaction;
101 GkmSecretItem *item;
102
103 transaction = gkm_transaction_new ();
104 item = gkm_secret_collection_create_item (test->collection, transaction);
105 g_assert (GKM_IS_SECRET_ITEM (item));
106 g_object_ref (item);
107 g_assert (gkm_secret_collection_has_item (test->collection, item));
108
109 gkm_transaction_complete (transaction);
110 g_object_unref (transaction);
111
112 /* Should still be there */
113 g_assert (gkm_secret_collection_has_item (test->collection, item));
114 g_object_unref (item);
115 }
116
117 static void
118 test_create_failed (Test *test, gconstpointer unused)
119 {
120 GkmTransaction *transaction;
121 GkmSecretItem *item;
122
123 transaction = gkm_transaction_new ();
124 item = gkm_secret_collection_create_item (test->collection, transaction);
125 g_assert (GKM_IS_SECRET_ITEM (item));
126 g_object_ref (item);
127 g_assert (gkm_secret_collection_has_item (test->collection, item));
128
129 gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
130 gkm_transaction_complete (transaction);
131 g_object_unref (transaction);
132
133 /* Should no longer be there */
134 g_assert (!gkm_secret_collection_has_item (test->collection, item));
135 g_object_unref (item);
136 }
137
138 static void
139 test_destroy (Test *test, gconstpointer unused)
140 {
141 GkmTransaction *transaction;
142 GkmSecretItem *item;
143
144 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
145 g_assert (gkm_secret_collection_has_item (test->collection, item));
146 g_object_ref (item);
147
148 transaction = gkm_transaction_new ();
149 gkm_secret_collection_destroy_item (test->collection, transaction, item);
150 g_assert (!gkm_secret_collection_has_item (test->collection, item));
151
152 gkm_transaction_complete (transaction);
153 g_object_unref (transaction);
154
155 /* Should not be there */
156 g_assert (!gkm_secret_collection_has_item (test->collection, item));
157 g_object_unref (item);
158 }
159
160 static void
161 test_destroy_failed (Test *test, gconstpointer unused)
162 {
163 GkmTransaction *transaction;
164 GkmSecretItem *item;
165
166 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
167 g_assert (gkm_secret_collection_has_item (test->collection, item));
168 g_object_ref (item);
169
170 transaction = gkm_transaction_new ();
171 gkm_secret_collection_destroy_item (test->collection, transaction, item);
172 g_assert (!gkm_secret_collection_has_item (test->collection, item));
173
174 gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
175 gkm_transaction_complete (transaction);
176 g_object_unref (transaction);
177
178 /* Should be there */
179 g_assert (gkm_secret_collection_has_item (test->collection, item));
180 g_object_unref (item);
181 }
182
183 static void
184 test_collection_get (Test *test, gconstpointer unused)
185 {
186 GkmSecretItem *item, *check;
187
188 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
189 g_assert (GKM_IS_SECRET_ITEM (item));
190
191 check = gkm_secret_collection_get_item (test->collection, "the-identifier");
192 g_assert (item == check);
193 }
194
195 static void
196 test_collection_items (Test *test, gconstpointer unused)
197 {
198 GList *l, *items;
199 const gchar *identifier;
200
201 gkm_secret_collection_new_item (test->collection, "one-identifier");
202 gkm_secret_collection_new_item (test->collection, "two-identifier");
203 gkm_secret_collection_new_item (test->collection, "three-identifier");
204
205 items = gkm_secret_collection_get_items (test->collection);
206 g_assert_cmpuint (g_list_length (items), ==, 3);
207 for (l = items; l; l = g_list_next (l)) {
208 identifier = gkm_secret_object_get_identifier (l->data);
209 if (!g_str_equal (identifier, "one-identifier") &&
210 !g_str_equal (identifier, "two-identifier") &&
211 !g_str_equal (identifier, "three-identifier"))
212 g_assert_not_reached ();
213 }
214
215 g_list_free (items);
216 }
217
218 static void
219 test_collection_remove (Test *test, gconstpointer unused)
220 {
221 GkmSecretItem *item;
222
223 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
224 g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == item);
225
226 gkm_secret_collection_remove_item (test->collection, item);
227 g_assert (gkm_secret_collection_get_item (test->collection, "the-identifier") == NULL);
228 }
229
230 static void
231 test_is_locked (Test *test, gconstpointer unused)
232 {
233 GkmSecretItem *item;
234
235 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
236 g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) ==
237 gkm_secret_object_is_locked (GKM_SECRET_OBJECT (test->collection), test->session));
238
239 unlock_collection (test);
240
241 g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
242 }
243
244 static void
245 test_get_collection (Test *test, gconstpointer unused)
246 {
247 GkmSecretItem *item;
248 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
249 g_assert (gkm_secret_item_get_collection (item) == test->collection);
250 }
251
252 static void
253 test_tracks_collection (Test *test, gconstpointer unused)
254 {
255 GkmSecretItem *item;
256 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
257 g_object_ref (item);
258
259 unlock_collection (test);
260
261 /* At this point the item should be 'unlocked' */
262 g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == FALSE);
263
264 g_object_unref (test->collection);
265 test->collection = NULL;
266
267 /* Collection went away */
268 g_assert (gkm_secret_item_get_collection (item) == NULL);
269 g_assert (gkm_secret_object_is_locked (GKM_SECRET_OBJECT (item), test->session) == TRUE);
270
271 g_object_unref (item);
272 }
273
274 static void
275 test_get_set_fields (Test *test, gconstpointer unused)
276 {
277 GHashTable *fields = gkm_secret_fields_new ();
278 GHashTable *check;
279 GkmSecretItem *item;
280
281 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
282 gkm_secret_item_set_fields (item, fields);
283 gkm_secret_item_set_fields (item, fields);
284
285 check = gkm_secret_item_get_fields (item);
286 g_assert (check == fields);
287
288 g_hash_table_unref (fields);
289 }
290
291 static void
292 test_collection_attr (Test *test, gconstpointer unused)
293 {
294 gchar buffer[32];
295 CK_ATTRIBUTE check = { CKA_G_COLLECTION, buffer, 32 };
296 GkmSecretItem *item;
297 CK_RV rv;
298
299 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
300 rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
301 g_assert (rv == CKR_OK);
302 g_assert (check.ulValueLen == 4);
303 g_assert (memcmp (buffer, "test", 4) == 0);
304 }
305
306 static void
307 test_secret_attr (Test *test, gconstpointer unused)
308 {
309 GkmTransaction *transaction = gkm_transaction_new ();
310 CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
311 gchar buffer[32];
312 CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
313 GkmSecretItem *item;
314 CK_RV rv;
315
316 unlock_collection (test);
317
318 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
319 gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
320 g_assert (gkm_transaction_get_failed (transaction) == FALSE);
321 gkm_transaction_complete (transaction);
322 g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
323
324 g_object_unref (transaction);
325
326 rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
327 g_assert (rv == CKR_OK);
328 g_assert (check.ulValueLen == 5);
329 g_assert (memcmp (buffer, "hello", 5) == 0);
330 }
331
332 static void
333 test_secret_attr_locked (Test *test, gconstpointer unused)
334 {
335 GkmTransaction *transaction = gkm_transaction_new ();
336 CK_ATTRIBUTE attr = { CKA_VALUE, "hello", 5 };
337 gchar buffer[32];
338 CK_ATTRIBUTE check = { CKA_VALUE, buffer, 32 };
339 GkmSecretItem *item;
340 CK_RV rv;
341
342 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
343 gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
344 g_assert (gkm_transaction_get_failed (transaction) == TRUE);
345 gkm_transaction_complete (transaction);
346 g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
347
348 g_object_unref (transaction);
349
350 rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
351 g_assert (rv == CKR_USER_NOT_LOGGED_IN);
352 }
353
354 static void
355 test_fields_attr (Test *test, gconstpointer unused)
356 {
357 GkmTransaction *transaction = gkm_transaction_new ();
358 CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
359 gchar buffer[32];
360 CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
361 GkmSecretItem *item;
362 GHashTable *fields;
363 const gchar *value;
364 CK_RV rv;
365
366 unlock_collection (test);
367
368 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
369 gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
370 g_assert (gkm_transaction_get_failed (transaction) == FALSE);
371 gkm_transaction_complete (transaction);
372 g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
373
374 g_object_unref (transaction);
375
376 rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
377 g_assert (rv == CKR_OK);
378 g_assert (check.ulValueLen == 26);
379 g_assert (memcmp (buffer, "name1\0value1\0name2\0value2", 26) == 0);
380
381 fields = gkm_secret_item_get_fields (item);
382 g_assert (fields);
383 value = gkm_secret_fields_get (fields, "name1");
384 g_assert_cmpstr (value, ==, "value1");
385 value = gkm_secret_fields_get (fields, "name2");
386 g_assert_cmpstr (value, ==, "value2");
387 }
388
389 static void
390 test_fields_attr_locked (Test *test, gconstpointer unused)
391 {
392 GkmTransaction *transaction = gkm_transaction_new ();
393 CK_ATTRIBUTE attr = { CKA_G_FIELDS, "name1\0value1\0name2\0value2", 26 };
394 GkmSecretItem *item;
395
396 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
397 gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
398 g_assert (gkm_transaction_get_failed (transaction) == TRUE);
399 gkm_transaction_complete (transaction);
400 g_assert (gkm_transaction_get_result (transaction) == CKR_USER_NOT_LOGGED_IN);
401
402 g_object_unref (transaction);
403 }
404
405 static void
406 test_fields_attr_reverts (Test *test, gconstpointer unused)
407 {
408 GkmTransaction *transaction = gkm_transaction_new ();
409 CK_ATTRIBUTE attr = { CKA_G_FIELDS, "new\0value\0", 10 };
410 gchar buffer[32];
411 CK_ATTRIBUTE check = { CKA_G_FIELDS, buffer, 32 };
412 GkmSecretItem *item;
413 GHashTable *fields;
414 CK_RV rv;
415
416 unlock_collection (test);
417
418 item = gkm_secret_collection_new_item (test->collection, "the-identifier");
419
420 /* Set the old value like so */
421 fields = gkm_secret_fields_new ();
422 gkm_secret_fields_add (fields, "old", "value");
423 gkm_secret_item_set_fields (item, fields);
424 g_hash_table_unref (fields);
425
426 /* Should show old value */
427 rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
428 g_assert (rv == CKR_OK);
429 g_assert (check.ulValueLen == 10);
430 g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
431
432 /* Set the new values */
433 gkm_object_set_attribute (GKM_OBJECT (item), test->session, transaction, &attr);
434 g_assert (gkm_transaction_get_failed (transaction) == FALSE);
435
436 /* Should have the new value */
437 rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
438 g_assert (rv == CKR_OK);
439 g_assert (check.ulValueLen == 10);
440 g_assert (memcmp (buffer, "new\0value\0", 10) == 0);
441
442 /* Fail the transaction */
443 gkm_transaction_fail (transaction, CKR_CANCEL);
444 gkm_transaction_complete (transaction);
445
446 /* Should show the old value */
447 rv = gkm_object_get_attribute (GKM_OBJECT (item), test->session, &check);
448 g_assert (rv == CKR_OK);
449 g_assert (check.ulValueLen == 10);
450 g_assert (memcmp (buffer, "old\0value\0", 10) == 0);
451
452 g_object_unref (transaction);
453 }
454
455 int
456 main (int argc, char **argv)
457 {
458 #if !GLIB_CHECK_VERSION(2,35,0)
459 g_type_init ();
460 #endif
461 g_test_init (&argc, &argv, NULL);
462
463 g_test_add ("/secret-store/item/new", Test, NULL, setup, test_new, teardown);
464 g_test_add ("/secret-store/item/create", Test, NULL, setup, test_create, teardown);
465 g_test_add ("/secret-store/item/create_failed", Test, NULL, setup, test_create_failed, teardown);
466 g_test_add ("/secret-store/item/destroy", Test, NULL, setup, test_destroy, teardown);
467 g_test_add ("/secret-store/item/destroy_failed", Test, NULL, setup, test_destroy_failed, teardown);
468 g_test_add ("/secret-store/item/collection_get", Test, NULL, setup, test_collection_get, teardown);
469 g_test_add ("/secret-store/item/collection_items", Test, NULL, setup, test_collection_items, teardown);
470 g_test_add ("/secret-store/item/collection_remove", Test, NULL, setup, test_collection_remove, teardown);
471 g_test_add ("/secret-store/item/is_locked", Test, NULL, setup, test_is_locked, teardown);
472 g_test_add ("/secret-store/item/get_collection", Test, NULL, setup, test_get_collection, teardown);
473 g_test_add ("/secret-store/item/tracks_collection", Test, NULL, setup, test_tracks_collection, teardown);
474 g_test_add ("/secret-store/item/get_set_fields", Test, NULL, setup, test_get_set_fields, teardown);
475 g_test_add ("/secret-store/item/collection_attr", Test, NULL, setup, test_collection_attr, teardown);
476 g_test_add ("/secret-store/item/secret_attr", Test, NULL, setup, test_secret_attr, teardown);
477 g_test_add ("/secret-store/item/secret_attr_locked", Test, NULL, setup, test_secret_attr_locked, teardown);
478 g_test_add ("/secret-store/item/fields_attr", Test, NULL, setup, test_fields_attr, teardown);
479 g_test_add ("/secret-store/item/fields_attr_locked", Test, NULL, setup, test_fields_attr_locked, teardown);
480 g_test_add ("/secret-store/item/fields_attr_reverts", Test, NULL, setup, test_fields_attr_reverts, teardown);
481
482 return g_test_run ();
483 }