1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
3
Copyright (C) 2009 Stefan Walter
5
The Gnome Keyring Library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public License as
7
published by the Free Software Foundation; either version 2 of the
8
License, or (at your option) any later version.
10
The Gnome Keyring Library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with the Gnome Library; see the file COPYING.LIB. If not,
17
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
20
Author: Stef Walter <stef@memberwebs.com>
25
#include "mock-secret-module.h"
27
#include "secret-store/gkm-secret-object.h"
29
#include "gkm/gkm-transaction.h"
31
#include "pkcs11/pkcs11i.h"
42
GkmSecretObject *object;
46
setup (Test *test, gconstpointer unused)
48
test->module = test_secret_module_initialize_and_enter ();
49
test->session = test_secret_module_open_session (TRUE);
51
test->object = g_object_new (GKM_TYPE_SECRET_OBJECT,
52
"module", test->module,
53
"identifier", "my-identifier",
56
g_assert (GKM_IS_SECRET_OBJECT (test->object));
60
teardown (Test *test, gconstpointer unused)
62
g_object_unref (test->object);
63
test_secret_module_leave_and_finalize ();
67
test_is_locked (Test *test, gconstpointer unused)
69
/* Plain GkmSecretObject is never locked */
70
g_assert (!gkm_secret_object_is_locked (test->object, test->session));
74
test_identifier_prop (Test *test, gconstpointer unused)
76
const gchar *identifier;
77
identifier = gkm_secret_object_get_identifier (test->object);
78
g_assert_cmpstr (identifier, ==, "my-identifier");
82
was_notified (GObject *obj, GParamSpec *pspec, gpointer user_data)
84
gboolean* notified = user_data;
90
test_created_prop (Test *test, gconstpointer unused)
94
/* Monitor for changes */
95
gboolean notified = FALSE;
96
g_signal_connect (test->object, "notify::created", G_CALLBACK (was_notified), ¬ified);
99
created = gkm_secret_object_get_created (test->object);
100
g_assert (created == 0);
102
/* Set a new value */
103
gkm_secret_object_set_created (test->object, 1247930171);
105
created = gkm_secret_object_get_created (test->object);
106
g_assert (created == 1247930171);
110
test_modified_prop (Test *test, gconstpointer unused)
114
/* Monitor for changes */
115
gboolean notified = FALSE;
116
g_signal_connect (test->object, "notify::modified", G_CALLBACK (was_notified), ¬ified);
119
modified = gkm_secret_object_get_modified (test->object);
120
g_assert (modified == 0);
122
/* Set a new value */
123
gkm_secret_object_set_modified (test->object, 1247930171);
125
modified = gkm_secret_object_get_modified (test->object);
126
g_assert (modified == 1247930171);
130
test_was_modified (Test *test, gconstpointer unused)
133
g_get_current_time (&tv);
134
gkm_secret_object_was_modified (test->object);
135
g_assert (tv.tv_sec == gkm_secret_object_get_modified (test->object));
139
test_label_prop (Test *test, gconstpointer unused)
143
/* Monitor for changes */
144
gboolean notified = FALSE;
145
g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified);
148
label = gkm_secret_object_get_label (test->object);
149
g_assert_cmpstr (label, ==, "");
151
/* Set a new value */
152
gkm_secret_object_set_label (test->object, "hello");
154
label = gkm_secret_object_get_label (test->object);
155
g_assert_cmpstr (label, ==, "hello");
159
test_identifier_get_attr (Test *test, gconstpointer unused)
162
CK_ATTRIBUTE attr = { CKA_ID, buffer, 32 };
165
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
166
g_assert (rv == CKR_OK);
167
g_assert (attr.ulValueLen == 13);
168
g_assert (memcmp (buffer, "my-identifier", 13) == 0);
172
test_label_get_attr (Test *test, gconstpointer unused)
175
CK_ATTRIBUTE attr = { CKA_LABEL, buffer, 32 };
178
gkm_secret_object_set_label (test->object, "hello");
179
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
180
g_assert (rv == CKR_OK);
181
g_assert (attr.ulValueLen == 5);
182
g_assert (memcmp (buffer, "hello", 5) == 0);
186
test_label_set_attr (Test *test, gconstpointer unused)
188
CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
189
GkmTransaction *transaction = gkm_transaction_new ();
191
/* Monitor for changes */
192
gboolean notified = FALSE;
193
g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified);
195
gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
196
g_assert (!gkm_transaction_get_failed (transaction));
197
g_assert (!notified); /* Not notified yet */
199
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
201
gkm_transaction_complete (transaction);
202
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
203
g_assert (notified); /* Notified after transaction complete */
205
g_object_unref (transaction);
209
test_label_set_attr_fail (Test *test, gconstpointer unused)
211
CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
212
GkmTransaction *transaction = gkm_transaction_new ();
213
gboolean notified = FALSE;
215
/* Set an old value */
216
gkm_secret_object_set_label (test->object, "old");
218
/* Monitor for changes */
219
g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), ¬ified);
221
/* Set a new value */
222
gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
223
g_assert (!gkm_transaction_get_failed (transaction));
224
g_assert (!notified); /* Not notified yet */
226
/* Temporarily has new value */
227
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
229
/* Fail and complete transaction */
230
gkm_transaction_fail (transaction, CKR_CANCEL);
231
gkm_transaction_complete (transaction);
233
/* Back to old value */
234
g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "old");
235
g_assert (!notified); /* Should never have notified */
237
g_object_unref (transaction);
241
test_modified_get_attr (Test *test, gconstpointer unused)
244
CK_ATTRIBUTE attr = { CKA_G_MODIFIED, buffer, 32 };
247
gkm_secret_object_set_modified (test->object, 1247930171);
248
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
249
g_assert (rv == CKR_OK);
250
g_assert (attr.ulValueLen == 16);
251
g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
255
test_created_get_attr (Test *test, gconstpointer unused)
258
CK_ATTRIBUTE attr = { CKA_G_CREATED, buffer, 32 };
261
gkm_secret_object_set_created (test->object, 1247930171);
262
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
263
g_assert (rv == CKR_OK);
264
g_assert (attr.ulValueLen == 16);
265
g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
269
test_locked_get_attr (Test *test, gconstpointer unused)
272
CK_ATTRIBUTE attr = { CKA_G_LOCKED, buffer, 32 };
275
rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
276
g_assert (rv == CKR_OK);
277
g_assert (attr.ulValueLen == 1);
278
g_assert (memcmp (buffer, "\0", 1) == 0);
282
main (int argc, char **argv)
285
g_test_init (&argc, &argv, NULL);
287
g_test_add ("/secret-store/object/is_locked", Test, NULL, setup, test_is_locked, teardown);
288
g_test_add ("/secret-store/object/identifier_prop", Test, NULL, setup, test_identifier_prop, teardown);
289
g_test_add ("/secret-store/object/created_prop", Test, NULL, setup, test_created_prop, teardown);
290
g_test_add ("/secret-store/object/modified_prop", Test, NULL, setup, test_modified_prop, teardown);
291
g_test_add ("/secret-store/object/was_modified", Test, NULL, setup, test_was_modified, teardown);
292
g_test_add ("/secret-store/object/label_prop", Test, NULL, setup, test_label_prop, teardown);
293
g_test_add ("/secret-store/object/identifier_get_attr", Test, NULL, setup, test_identifier_get_attr, teardown);
294
g_test_add ("/secret-store/object/label_get_attr", Test, NULL, setup, test_label_get_attr, teardown);
295
g_test_add ("/secret-store/object/label_set_attr", Test, NULL, setup, test_label_set_attr, teardown);
296
g_test_add ("/secret-store/object/label_set_attr_fail", Test, NULL, setup, test_label_set_attr_fail, teardown);
297
g_test_add ("/secret-store/object/modified_get_attr", Test, NULL, setup, test_modified_get_attr, teardown);
298
g_test_add ("/secret-store/object/created_get_attr", Test, NULL, setup, test_created_get_attr, teardown);
299
g_test_add ("/secret-store/object/locked_get_attr", Test, NULL, setup, test_locked_get_attr, teardown);
301
return g_test_run ();