summaryrefslogtreecommitdiff
path: root/pkcs11/secret-store/tests/test-secret-object.c
blob: 9855c0614a053869a4ddbc55a4a19acc1087472c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/*
   Copyright (C) 2009 Stefan Walter

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   <http://www.gnu.org/licenses/>.

   Author: Stef Walter <stef@memberwebs.com>
*/

#include "config.h"

#include "mock-secret-module.h"

#include "secret-store/gkm-secret-object.h"

#include "gkm/gkm-transaction.h"

#include "pkcs11/pkcs11i.h"

#include <glib.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct {
	GkmModule *module;
	GkmSession *session;
	GkmSecretObject *object;
} Test;

static void
setup (Test *test, gconstpointer unused)
{
	test->module = test_secret_module_initialize_and_enter ();
	test->session = test_secret_module_open_session (TRUE);

	test->object = g_object_new (GKM_TYPE_SECRET_OBJECT,
	                       "module", test->module,
	                       "identifier", "my-identifier",
	                       NULL);

	g_assert (GKM_IS_SECRET_OBJECT (test->object));
}

static void
teardown (Test *test, gconstpointer unused)
{
	g_object_unref (test->object);
	test_secret_module_leave_and_finalize ();
}

static void
test_is_locked (Test *test, gconstpointer unused)
{
	/* Plain GkmSecretObject is never locked */
	g_assert (!gkm_secret_object_is_locked (test->object, test->session));
}

static void
test_identifier_prop (Test *test, gconstpointer unused)
{
	const gchar *identifier;
	identifier = gkm_secret_object_get_identifier (test->object);
	g_assert_cmpstr (identifier, ==, "my-identifier");
}

static void
was_notified (GObject *obj, GParamSpec *pspec, gpointer user_data)
{
	gboolean* notified = user_data;
	g_assert (user_data);
	*notified = TRUE;
}

static void
test_created_prop (Test *test, gconstpointer unused)
{
	glong created;

	/* Monitor for changes */
	gboolean notified = FALSE;
	g_signal_connect (test->object, "notify::created", G_CALLBACK (was_notified), &notified);

	/* Default value */
	created = gkm_secret_object_get_created (test->object);
	g_assert (created == 0);

	/* Set a new value */
	gkm_secret_object_set_created (test->object, 1247930171);
	g_assert (notified);
	created = gkm_secret_object_get_created (test->object);
	g_assert (created == 1247930171);
}

static void
test_modified_prop (Test *test, gconstpointer unused)
{
	glong modified;

	/* Monitor for changes */
	gboolean notified = FALSE;
	g_signal_connect (test->object, "notify::modified", G_CALLBACK (was_notified), &notified);

	/* Default value */
	modified = gkm_secret_object_get_modified (test->object);
	g_assert (modified == 0);

	/* Set a new value */
	gkm_secret_object_set_modified (test->object, 1247930171);
	g_assert (notified);
	modified = gkm_secret_object_get_modified (test->object);
	g_assert (modified == 1247930171);
}

static void
test_was_modified (Test *test, gconstpointer unused)
{
	GkmTransaction *transaction;
	GTimeVal tv;

	g_get_current_time (&tv);

	transaction = gkm_transaction_new ();
	gkm_secret_object_begin_modified (test->object, transaction);
	g_assert_cmpuint (gkm_transaction_complete_and_unref (transaction), ==, CKR_OK);

	g_assert (tv.tv_sec == gkm_secret_object_get_modified (test->object));
}

static void
test_label_prop (Test *test, gconstpointer unused)
{
	const gchar *label;

	/* Monitor for changes */
	gboolean notified = FALSE;
	g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);

	/* Default value */
	label = gkm_secret_object_get_label (test->object);
	g_assert_cmpstr (label, ==, "");

	/* Set a new value */
	gkm_secret_object_set_label (test->object, "hello");
	g_assert (notified);
	label = gkm_secret_object_get_label (test->object);
	g_assert_cmpstr (label, ==, "hello");
}

static void
test_identifier_get_attr (Test *test, gconstpointer unused)
{
	gchar buffer[32];
	CK_ATTRIBUTE attr = { CKA_ID, buffer, 32 };
	CK_RV rv;

	rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
	g_assert (rv == CKR_OK);
	g_assert (attr.ulValueLen == 13);
	g_assert (memcmp (buffer, "my-identifier", 13) == 0);
}

static void
test_label_get_attr (Test *test, gconstpointer unused)
{
	gchar buffer[32];
	CK_ATTRIBUTE attr = { CKA_LABEL, buffer, 32 };
	CK_RV rv;

	gkm_secret_object_set_label (test->object, "hello");
	rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
	g_assert (rv == CKR_OK);
	g_assert (attr.ulValueLen == 5);
	g_assert (memcmp (buffer, "hello", 5) == 0);
}

static void
test_label_set_attr (Test *test, gconstpointer unused)
{
	CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
	GkmTransaction *transaction = gkm_transaction_new ();

	/* Monitor for changes */
	gboolean notified = FALSE;
	g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);

	gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
	g_assert (!gkm_transaction_get_failed (transaction));
	g_assert (!notified); /* Not notified yet */

	g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");

	gkm_transaction_complete (transaction);
	g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");
	g_assert (notified); /* Notified after transaction complete */

	g_object_unref (transaction);
}

static void
test_label_set_attr_fail (Test *test, gconstpointer unused)
{
	CK_ATTRIBUTE attr = { CKA_LABEL, "hello", 5 };
	GkmTransaction *transaction = gkm_transaction_new ();
	gboolean notified = FALSE;

	/* Set an old value */
	gkm_secret_object_set_label (test->object, "old");

	/* Monitor for changes */
	g_signal_connect (test->object, "notify::label", G_CALLBACK (was_notified), &notified);

	/* Set a new value */
	gkm_object_set_attribute (GKM_OBJECT (test->object), test->session, transaction, &attr);
	g_assert (!gkm_transaction_get_failed (transaction));
	g_assert (!notified); /* Not notified yet */

	/* Temporarily has new value */
	g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "hello");

	/* Fail and complete transaction */
	gkm_transaction_fail (transaction, CKR_CANCEL);
	gkm_transaction_complete (transaction);

	/* Back to old value */
	g_assert_cmpstr (gkm_secret_object_get_label (test->object), ==, "old");
	g_assert (!notified); /* Should never have notified */

	g_object_unref (transaction);
}

static void
test_modified_get_attr (Test *test, gconstpointer unused)
{
	gchar buffer[32];
	CK_ATTRIBUTE attr = { CKA_G_MODIFIED, buffer, 32 };
	CK_RV rv;

	gkm_secret_object_set_modified (test->object, 1247930171);
	rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
	g_assert (rv == CKR_OK);
	g_assert (attr.ulValueLen == 16);
	g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
}

static void
test_created_get_attr (Test *test, gconstpointer unused)
{
	gchar buffer[32];
	CK_ATTRIBUTE attr = { CKA_G_CREATED, buffer, 32 };
	CK_RV rv;

	gkm_secret_object_set_created (test->object, 1247930171);
	rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
	g_assert (rv == CKR_OK);
	g_assert (attr.ulValueLen == 16);
	g_assert (memcmp (buffer, "2009071815161100", 16) == 0);
}

static void
test_locked_get_attr (Test *test, gconstpointer unused)
{
	gchar buffer[32];
	CK_ATTRIBUTE attr = { CKA_G_LOCKED, buffer, 32 };
	CK_RV rv;

	rv = gkm_object_get_attribute (GKM_OBJECT (test->object), test->session, &attr);
	g_assert (rv == CKR_OK);
	g_assert (attr.ulValueLen == 1);
	g_assert (memcmp (buffer, "\0", 1) == 0);
}

int
main (int argc, char **argv)
{
#if !GLIB_CHECK_VERSION(2,35,0)
	g_type_init ();
#endif
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/secret-store/object/is_locked", Test, NULL, setup, test_is_locked, teardown);
	g_test_add ("/secret-store/object/identifier_prop", Test, NULL, setup, test_identifier_prop, teardown);
	g_test_add ("/secret-store/object/created_prop", Test, NULL, setup, test_created_prop, teardown);
	g_test_add ("/secret-store/object/modified_prop", Test, NULL, setup, test_modified_prop, teardown);
	g_test_add ("/secret-store/object/was_modified", Test, NULL, setup, test_was_modified, teardown);
	g_test_add ("/secret-store/object/label_prop", Test, NULL, setup, test_label_prop, teardown);
	g_test_add ("/secret-store/object/identifier_get_attr", Test, NULL, setup, test_identifier_get_attr, teardown);
	g_test_add ("/secret-store/object/label_get_attr", Test, NULL, setup, test_label_get_attr, teardown);
	g_test_add ("/secret-store/object/label_set_attr", Test, NULL, setup, test_label_set_attr, teardown);
	g_test_add ("/secret-store/object/label_set_attr_fail", Test, NULL, setup, test_label_set_attr_fail, teardown);
	g_test_add ("/secret-store/object/modified_get_attr", Test, NULL, setup, test_modified_get_attr, teardown);
	g_test_add ("/secret-store/object/created_get_attr", Test, NULL, setup, test_created_get_attr, teardown);
	g_test_add ("/secret-store/object/locked_get_attr", Test, NULL, setup, test_locked_get_attr, teardown);

	return g_test_run ();
}