summaryrefslogtreecommitdiff
path: root/pkcs11/gnome2-store/tests/test-gnome2-storage.c
blob: d3f315b94e5a413e0b9f341ba7e54d66ed27a929 (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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* unit-test-file-store.c: Test file store functionality

   Copyright (C) 2008 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,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

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

#include "config.h"

#include "mock-gnome2-module.h"

#include "gnome2-store/gkm-gnome2-storage.h"

#include "gkm/gkm-certificate.h"
#include "gkm/gkm-module.h"
#include "gkm/gkm-serializable.h"
#include "gkm/gkm-test.h"

#include "egg/egg-libgcrypt.h"
#include "egg/egg-testing.h"

#include <glib/gstdio.h>

#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

typedef struct {
	gchar *directory;
	GkmModule *module;
	GkmGnome2Storage *storage;
	GkmObject *new_object;
	gchar *new_filename;
	GkmObject *old_object;
} Test;

#define MSEC(x) ((x) * 1000)

static void
setup_directory (Test *test,
                 gconstpointer unused)
{
	test->directory = egg_tests_create_scratch_directory (
		SRCDIR "/files/Thawte_Personal_Premium_CA.cer",
		SRCDIR "/files/user.keystore",
		NULL);
}

static void
setup_module (Test *test,
              gconstpointer unused)
{
	CK_ATTRIBUTE url = { CKA_URL, NULL, 0 };
	gchar *contents;
	gsize length;
	GBytes *bytes;
	GkmManager *manager;
	GError *error = NULL;
	GkmSession *session;
	CK_ATTRIBUTE attr;
	CK_OBJECT_CLASS klass;
	CK_RV rv;

	g_assert (test->directory != NULL);

	test->module = mock_gnome2_module_initialize_and_enter ();
	manager = gkm_module_get_manager (test->module);
	session = mock_gnome2_module_open_session (TRUE);

	test->storage = gkm_gnome2_storage_new (test->module, test->directory);
	rv = gkm_gnome2_storage_refresh (test->storage);
	gkm_assert_cmprv (rv, ==, CKR_OK);

	/* We already have the CKA_LABEL attribute */
	gkm_store_register_schema (GKM_STORE (test->storage), &url, NULL, 0);

	/*
	 * Create a new object that hasn't yet been stored in the storage.
	 * It's a certificate because that's easiest.
	 */
	test->new_object = g_object_new (GKM_TYPE_CERTIFICATE,
	                                 "unique", "test.cer",
	                                 "module", test->module,
	                                 "manager", manager,
	                                 NULL);
	g_file_get_contents (SRCDIR "/files/test-certificate.cer", &contents, &length, &error);
	g_assert_no_error (error);

	bytes = g_bytes_new_take (contents, length);
	if (!gkm_serializable_load (GKM_SERIALIZABLE (test->new_object), NULL, bytes))
		g_assert_not_reached ();
	g_bytes_unref (bytes);

	/* We happen to know this certificate will get named */
	test->new_filename = g_build_filename (test->directory, "CA_Cert_Signing_Authority.cer", NULL);

	/*
	 * Find the object stored in the storage, it's a certificate, and we happen to
	 * know there's only one
	 */
	klass = CKO_CERTIFICATE;
	attr.type = CKA_CLASS;
	attr.pValue = &klass;
	attr.ulValueLen = sizeof (klass);
	test->old_object = gkm_manager_find_one_by_attributes (manager, session, &attr, 1);
	g_assert (GKM_IS_OBJECT (test->old_object));
	g_object_ref (test->old_object);
}

static void
setup_all (Test *test,
           gconstpointer unused)
{
	setup_directory (test, unused);
	setup_module (test, unused);
}

static void
teardown_directory (Test *test,
                    gconstpointer unused)
{
	egg_tests_remove_scratch_directory (test->directory);
	g_free (test->directory);
}

static void
teardown_module (Test *test,
                 gconstpointer unused)
{
	g_assert (test->directory);

	g_object_unref (test->new_object);
	g_object_unref (test->old_object);
	g_object_unref (test->storage);
	g_assert (!G_IS_OBJECT (test->storage));

	mock_gnome2_module_leave_and_finalize ();

	g_free (test->new_filename);
}

static void
teardown_all (Test *test,
              gconstpointer unused)
{
	teardown_module (test, unused);
	teardown_directory (test, unused);
}

static void
test_create (Test *test,
             gconstpointer unused)
{
	GkmTransaction *transaction;

	transaction = gkm_transaction_new ();

	gkm_gnome2_storage_create (test->storage, transaction, test->new_object);
	gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

	gkm_transaction_complete_and_unref (transaction);

	g_assert (g_file_test (test->new_filename, G_FILE_TEST_EXISTS));
}

static void
test_create_and_fail (Test *test,
                      gconstpointer unused)
{
	GkmTransaction *transaction;

	transaction = gkm_transaction_new ();

	gkm_gnome2_storage_create (test->storage, transaction, test->new_object);
	gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

	gkm_transaction_fail (transaction, CKR_FUNCTION_FAILED);
	gkm_transaction_complete_and_unref (transaction);

	g_assert (!g_file_test (test->new_filename, G_FILE_TEST_EXISTS));
}

static void
test_write_value (Test *test,
                  gconstpointer unused)
{
	CK_ATTRIBUTE label = { CKA_LABEL, "Hello", 5 };
	CK_ATTRIBUTE url = { CKA_URL, "http://example.com", 18 };
	GkmTransaction *transaction;
	gchar *string;

	transaction = gkm_transaction_new ();

	gkm_store_write_value (GKM_STORE (test->storage), transaction,
	                       test->old_object, &label);
	gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

	gkm_store_write_value (GKM_STORE (test->storage), transaction,
	                       test->old_object, &url);
	gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

	gkm_transaction_complete_and_unref (transaction);

	string = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_URL);
	g_assert_cmpstr (string, ==, "http://example.com");
	g_free (string);

	string = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_LABEL);
	g_assert_cmpstr (string, ==, "Hello");
	g_free (string);

}

static void
test_locking_transaction (Test *test,
                          gconstpointer unused)
{
	guint iterations = 30;
	guint i;
	pid_t pid;

	/* Fork before setting up the model, as it may start threads */
	pid = fork ();
	g_assert (pid >= 0);

	/*
	 * This is the child. It initializes, writes a value, waits 100 ms,
	 * writes a second value, and then writes another value.
	 */
	if (pid == 0) {
		CK_ATTRIBUTE attr;
		GkmTransaction *transaction;
		gchar *string;

		setup_module (test, unused);

		for (i = 0; i < iterations; i++) {
			g_printerr ("c");

			transaction = gkm_transaction_new ();

			string = g_strdup_printf ("%d", i);

			attr.type = CKA_LABEL;
			attr.pValue = string;
			attr.ulValueLen = strlen (string);

			gkm_store_write_value (GKM_STORE (test->storage), transaction,
			                       test->old_object, &attr);
			gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

			g_usleep (100 * 1000);

			attr.type = CKA_URL;
			attr.pValue = string;
			attr.ulValueLen = strlen (string);

			gkm_store_write_value (GKM_STORE (test->storage), transaction,
			                       test->old_object, &attr);
			gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

			g_free (string);

			gkm_transaction_complete_and_unref (transaction);

			g_usleep (10 * 1000);
		}

		teardown_module (test, unused);
		_exit (0);
		g_assert_not_reached ();

	/*
	 * This is the parent. it initializes, waits 100 ms, writes a value that
	 * should override the one from the child, because the file is locked
	 * when it tries to write, so it waits for the child to finish. The other
	 * attribute from the child (the label) should come through.
	 */
	} else {
		gchar *string1;
		gchar *string2;
		pid_t wpid;
		int status;
		CK_RV rv;

		g_assert (pid != -1);

		setup_module (test, unused);

		for (i = 0; i < iterations; i++) {
			g_printerr ("p");

			string1 = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_URL);

			g_usleep (g_random_int_range (1, 200) * 1000);

			string2 = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_LABEL);

			g_assert_cmpstr (string1, ==, string2);
			g_free (string1);
			g_free (string2);

			rv = gkm_gnome2_storage_refresh (test->storage);
			gkm_assert_cmprv (rv, ==, CKR_OK);
		}

		/* wait for the child to finish */
		wpid = waitpid (pid, &status, 0);
		g_assert_cmpint (wpid, ==, pid);
		g_assert_cmpint (status, ==, 0);

		teardown_module (test, unused);
	}
}

static void
test_lock_writes (Test *test,
                  gconstpointer unused)
{
	pid_t pid;

	/* Fork before setting up the model, as it may start threads */
	pid = fork ();
	g_assert (pid >= 0);

	/*
	 * This is the child. It initializes, writes a value, waits 100 ms,
	 * writes a second value, and then writes another value.
	 */
	if (pid == 0) {
		CK_ATTRIBUTE label = { CKA_LABEL, "Hello from child", 16 };
		CK_ATTRIBUTE url = { CKA_URL, "http://child.example.com", 24 };
		GkmTransaction *transaction;

		setup_module (test, unused);

		transaction = gkm_transaction_new ();

		gkm_store_write_value (GKM_STORE (test->storage), transaction,
		                       test->old_object, &label);
		gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

		g_usleep (MSEC (100));

		gkm_store_write_value (GKM_STORE (test->storage), transaction,
		                       test->old_object, &url);
		gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

		gkm_transaction_complete_and_unref (transaction);

		teardown_module (test, unused);
		_exit (0);
		g_assert_not_reached ();

	/*
	 * This is the parent. it initializes, waits 100 ms, writes a value that
	 * should override the one from the child, because the file is locked
	 * when it tries to write, so it waits for the child to finish. The other
	 * attribute from the child (the label) should come through.
	 */
	} else {
		CK_ATTRIBUTE url = { CKA_URL, "http://parent.example.com", 25 };
		GkmTransaction *transaction;
		gchar *string;
		pid_t wpid;
		int status;
		CK_RV rv;

		g_assert (pid != -1);

		setup_module (test, unused);

		/* Refresh the store, and check values are not set */
		string = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_URL);
		g_assert (string == NULL);

		string = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_LABEL);
		g_assert (string == NULL);

		g_usleep (MSEC (1000));

		transaction = gkm_transaction_new ();

		gkm_store_write_value (GKM_STORE (test->storage), transaction,
		                       test->old_object, &url);
		gkm_assert_cmprv (gkm_transaction_get_result (transaction), ==, CKR_OK);

		/* wait for the child to finish */
		wpid = waitpid (pid, &status, 0);
		g_assert_cmpint (wpid, ==, pid);
		g_assert_cmpint (status, ==, 0);

		gkm_transaction_complete_and_unref (transaction);

		g_usleep (MSEC (1000));

		rv = gkm_gnome2_storage_refresh (test->storage);
		gkm_assert_cmprv (rv, ==, CKR_OK);

		string = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_URL);
		g_assert_cmpstr (string, ==, "http://parent.example.com");
		g_free (string);

		string = gkm_store_read_string (GKM_STORE (test->storage), test->old_object, CKA_LABEL);
		g_assert_cmpstr (string, ==, "Hello from child");
		g_free (string);

		teardown_module (test, unused);
	}
}

static void
test_relock (Test *test,
             gconstpointer unused)
{
	GkmTransaction *transaction;
	GkmSecret *old_login;
	GkmSecret *new_login;

	transaction = gkm_transaction_new ();

	old_login = NULL;
	new_login = gkm_secret_new_from_password ("blah");

	gkm_gnome2_storage_relock (test->storage, transaction, old_login, new_login);
	gkm_assert_cmprv (gkm_transaction_complete_and_unref (transaction), ==, CKR_OK);

	g_object_unref (new_login);
}

static void
null_log_handler (const gchar *log_domain,
                  GLogLevelFlags log_level,
                  const gchar *message,
                  gpointer user_data)
{

}

int
main (int argc, char **argv)
{
	g_type_init ();
	g_test_init (&argc, &argv, NULL);

	egg_libgcrypt_initialize ();

	/* Suppress these messages in tests */
	g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
	                   null_log_handler, NULL);

	g_test_add ("/gnome2-store/storage/create", Test, NULL,
	            setup_all, test_create, teardown_all);
	g_test_add ("/gnome2-store/storage/create_and_fail", Test, NULL,
	            setup_all, test_create_and_fail, teardown_all);
	g_test_add ("/gnome2-store/storage/write_value", Test, NULL,
	            setup_all, test_write_value, teardown_all);
	g_test_add ("/gnome2-store/storage/relock", Test, NULL,
	            setup_all, test_relock, teardown_all);

	if (!g_test_quick ()) {
		g_test_add ("/gnome2-store/storage/locking_transaction", Test, NULL,
		            setup_directory, test_locking_transaction, teardown_directory);
		g_test_add ("/gnome2-store/storage/lock_writes", Test, NULL,
		            setup_directory, test_lock_writes, teardown_directory);
	}

	return g_test_run ();
}