Debian Codegraph @

pkcs11/gkm/test-transaction.c

raw
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-transaction.c: Test transaction functionality
3
4 Copyright (C) 2008 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 "gkm/gkm-transaction.h"
26
27 #include <glib/gstdio.h>
28
29 typedef struct {
30 int unused;
31 } Test;
32
33 static void
34 setup (Test* test, gconstpointer unused)
35 {
36 GDir *dir;
37 const gchar *directory;
38 const gchar *basename;
39 gchar *filename;
40
41 directory = "/tmp";
42 dir = g_dir_open (directory, 0, NULL);
43 g_assert (dir);
44
45 for (;;) {
46 basename = g_dir_read_name (dir);
47 if (basename == NULL)
48 break;
49 if (g_str_has_prefix (basename, "transaction-")) {
50 filename = g_build_filename (directory, basename, NULL);
51 g_unlink (filename);
52 g_free (filename);
53 }
54 }
55
56 g_dir_close (dir);
57 }
58
59 static void
60 teardown (Test *test, gconstpointer unused)
61 {
62
63 }
64
65 static void
66 test_transaction_empty (Test* test, gconstpointer unused)
67 {
68 GkmTransaction *transaction;
69 gboolean completed, failed;
70 CK_RV result;
71
72 transaction = gkm_transaction_new ();
73 g_assert (GKM_IS_TRANSACTION (transaction));
74
75 g_assert (gkm_transaction_get_failed (transaction) == FALSE);
76 g_assert (gkm_transaction_get_completed (transaction) == FALSE);
77 g_assert (gkm_transaction_get_result (transaction) == CKR_OK);
78
79 gkm_transaction_complete (transaction);
80
81 /* Make sure values are actually set */
82 result = (CK_RV)-1;
83 completed = failed = FALSE;
84
85 g_object_get (transaction, "completed", &completed, "failed", &failed, "result", &result, NULL);
86 g_assert (result == CKR_OK);
87 g_assert (completed == TRUE);
88 g_assert (failed == FALSE);
89
90 g_object_unref (transaction);
91 }
92
93 static void
94 test_transaction_fail (Test* test, gconstpointer unused)
95 {
96 GkmTransaction *transaction;
97
98 transaction = gkm_transaction_new ();
99
100 gkm_transaction_fail (transaction, CKR_ARGUMENTS_BAD);
101
102 g_assert (gkm_transaction_get_failed (transaction) == TRUE);
103 g_assert (gkm_transaction_get_completed (transaction) == FALSE);
104 g_assert (gkm_transaction_get_result (transaction) == CKR_ARGUMENTS_BAD);
105
106 gkm_transaction_complete (transaction);
107
108 g_assert (gkm_transaction_get_failed (transaction) == TRUE);
109 g_assert (gkm_transaction_get_completed (transaction) == TRUE);
110 g_assert (gkm_transaction_get_result (transaction) == CKR_ARGUMENTS_BAD);
111
112 g_object_unref (transaction);
113 }
114
115
116 static gboolean
117 completed_signal (GkmTransaction *transaction, gpointer data)
118 {
119 g_assert (GKM_IS_TRANSACTION (transaction));
120 g_assert (data);
121
122 *((guint*)data) = TRUE;
123 return TRUE;
124 }
125
126 static gboolean
127 completed_callback (GkmTransaction *transaction, GObject *object, gpointer data)
128 {
129 g_assert (GKM_IS_TRANSACTION (transaction));
130 g_assert (data);
131
132 /* In this case we set the object to the transaction for fun */
133 g_assert (GKM_IS_TRANSACTION (transaction));
134 g_assert (transaction == GKM_TRANSACTION (object));
135
136 *((guint*)data) = gkm_transaction_get_failed (transaction);
137 return TRUE;
138 }
139
140 static void
141 test_transaction_signals_success (Test* test, gconstpointer unused)
142 {
143 GkmTransaction *transaction = gkm_transaction_new ();
144
145 /* Initialize with some invalid values */
146 guint completed = 3;
147 guint failed = 3;
148
149 g_signal_connect (transaction, "complete", G_CALLBACK (completed_signal), &completed);
150 gkm_transaction_add (transaction, transaction, completed_callback, &failed);
151
152 /* No callbacks called yet */
153 g_assert (completed == 3);
154 g_assert (failed == 3);
155
156 gkm_transaction_complete (transaction);
157
158 g_assert (completed == TRUE);
159 g_assert (failed == FALSE);
160
161 g_object_unref (transaction);
162 }
163
164 static void
165 test_transaction_signals_failure (Test* test, gconstpointer unused)
166 {
167 GkmTransaction *transaction = gkm_transaction_new ();
168
169 /* Initialize with some invalid values */
170 guint completed = 3;
171 guint failed = 3;
172
173 g_signal_connect (transaction, "complete", G_CALLBACK (completed_signal), &completed);
174 gkm_transaction_add (transaction, transaction, completed_callback, &failed);
175
176 gkm_transaction_fail (transaction, CKR_ARGUMENTS_BAD);
177
178 /* No callbacks called yet */
179 g_assert (completed == 3);
180 g_assert (failed == 3);
181
182 gkm_transaction_complete (transaction);
183
184 g_assert (completed == TRUE);
185 g_assert (failed == TRUE);
186
187 g_object_unref (transaction);
188 }
189
190 static guint order_value = 3;
191
192 static gboolean
193 order_callback (GkmTransaction *transaction, GObject *object, gpointer data)
194 {
195 g_assert (GKM_IS_TRANSACTION (transaction));
196 g_assert (data);
197 g_assert (GPOINTER_TO_UINT (data) == order_value);
198 --order_value;
199 return TRUE;
200 }
201
202 static void
203 test_transaction_order_is_reverse (Test* test, gconstpointer unused)
204 {
205 GkmTransaction *transaction = gkm_transaction_new ();
206
207 order_value = 3;
208 gkm_transaction_add (transaction, transaction, order_callback, GUINT_TO_POINTER (1));
209 gkm_transaction_add (transaction, transaction, order_callback, GUINT_TO_POINTER (2));
210 gkm_transaction_add (transaction, transaction, order_callback, GUINT_TO_POINTER (3));
211
212 gkm_transaction_complete (transaction);
213 g_object_unref (transaction);
214 }
215
216 static void
217 test_transaction_dispose_completes (Test* test, gconstpointer unused)
218 {
219 GkmTransaction *transaction = gkm_transaction_new ();
220
221 /* Initialize with some invalid values */
222 guint completed = 3;
223
224 g_signal_connect (transaction, "complete", G_CALLBACK (completed_signal), &completed);
225
226 g_object_run_dispose (G_OBJECT (transaction));
227
228 g_assert (completed == TRUE);
229
230 g_object_unref (transaction);
231 }
232
233 static void
234 test_remove_file_success (Test* test, gconstpointer unused)
235 {
236 GkmTransaction *transaction = gkm_transaction_new ();
237 const gchar *filename = "/tmp/transaction-remove";
238
239 g_assert (g_file_set_contents (filename, "xxx", 3, NULL));
240 g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR));
241
242 gkm_transaction_remove_file (transaction, filename);
243 g_assert (!gkm_transaction_get_failed (transaction));
244
245 g_assert (!g_file_test (filename, G_FILE_TEST_IS_REGULAR));
246
247 gkm_transaction_complete (transaction);
248 g_assert (!g_file_test (filename, G_FILE_TEST_IS_REGULAR));
249
250 g_object_unref (transaction);
251 }
252
253 static void
254 test_remove_file_abort (Test* test, gconstpointer unused)
255 {
256 GkmTransaction *transaction = gkm_transaction_new ();
257 const gchar *filename = "/tmp/transaction-remove";
258 gchar *data;
259 gsize n_data;
260
261 g_assert (g_file_set_contents (filename, "xxx", 3, NULL));
262 g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR));
263
264 gkm_transaction_remove_file (transaction, filename);
265 g_assert (!gkm_transaction_get_failed (transaction));
266
267 g_assert (!g_file_test (filename, G_FILE_TEST_IS_REGULAR));
268
269 /* Fail the transaction */
270 gkm_transaction_fail (transaction, CKR_FUNCTION_FAILED);
271
272 gkm_transaction_complete (transaction);
273 g_assert (gkm_transaction_get_failed (transaction));
274 g_assert (g_file_test (filename, G_FILE_TEST_IS_REGULAR));
275
276 g_assert (g_file_get_contents (filename, &data, &n_data, NULL));
277 g_assert_cmpuint (n_data, ==, 3);
278 g_assert_cmpstr (data, ==, "xxx");
279 g_free (data);
280
281 g_unlink (filename);
282 g_object_unref (transaction);
283 }
284
285 static void
286 test_remove_file_non_exist (Test* test, gconstpointer unused)
287 {
288 GkmTransaction *transaction = gkm_transaction_new ();
289 const gchar *filename = "/tmp/transaction-non-existant";
290
291 g_unlink (filename);
292
293 /* Should succeed even though not exist */
294 gkm_transaction_remove_file (transaction, filename);
295 g_assert (!gkm_transaction_get_failed (transaction));
296
297 gkm_transaction_complete (transaction);
298 g_object_unref (transaction);
299 }
300
301 static void
302 do_test_write_file (Test* test)
303 {
304 GkmTransaction *transaction = gkm_transaction_new ();
305 const gchar *filename = "/tmp/transaction-test";
306 gchar *data;
307 gsize n_data;
308
309 gkm_transaction_write_file (transaction, filename, (const guchar*)"value", 5);
310 g_assert (!gkm_transaction_get_failed (transaction));
311
312 g_assert (g_file_get_contents (filename, &data, &n_data, NULL));
313 g_assert_cmpuint (n_data, ==, 5);
314 g_assert_cmpstr (data, ==, "value");
315 g_free (data);
316
317 gkm_transaction_complete (transaction);
318
319 g_assert (g_file_get_contents (filename, &data, &n_data, NULL));
320 g_assert_cmpuint (n_data, ==, 5);
321 g_assert_cmpstr (data, ==, "value");
322 g_free (data);
323
324 g_object_unref (transaction);
325 }
326
327 static void
328 test_write_file (Test* test, gconstpointer unused)
329 {
330 /* Run it two times so that that the second one works on an
331 existing file "/tmp/transaction-test". */
332 do_test_write_file (test);
333 do_test_write_file (test);
334 }
335
336 static void
337 test_write_large_file (Test* test, gconstpointer test_data)
338 {
339 guint buffersize = GPOINTER_TO_UINT (test_data);
340 GkmTransaction *transaction = gkm_transaction_new ();
341 const gchar *filename = "/tmp/transaction-test";
342 gchar *data;
343 gsize n_data;
344 guchar *buffer;
345 int i;
346
347 buffer = g_malloc (buffersize);
348
349 for (i=0; i < buffersize; i++)
350 buffer[i] = i;
351
352 gkm_transaction_write_file (transaction, filename,
353 buffer, buffersize);
354 g_assert (!gkm_transaction_get_failed (transaction));
355
356 g_assert (g_file_get_contents (filename, &data, &n_data, NULL));
357 g_assert_cmpuint (n_data, ==, buffersize);
358 for (i=0; i < buffersize; i++)
359 g_assert_cmpuint (buffer[i], ==, ((guchar*)data)[i]);
360 g_free (data);
361
362 gkm_transaction_complete (transaction);
363
364 g_assert (g_file_get_contents (filename, &data, &n_data, NULL));
365 g_assert_cmpuint (n_data, ==, buffersize);
366 for (i=0; i < buffersize; i++)
367 g_assert_cmpuint (buffer[i], ==, ((guchar*)data)[i]);
368 g_free (data);
369
370 g_object_unref (transaction);
371
372 g_free (buffer);
373 }
374
375
376 static void
377 do_test_write_file_abort_gone (Test* test)
378 {
379 GkmTransaction *transaction = gkm_transaction_new ();
380 const gchar *filename = "/tmp/transaction-test";
381 gchar *data;
382 gsize n_data;
383
384 g_unlink (filename);
385
386 gkm_transaction_write_file (transaction, filename, (const guchar*)"value", 5);
387 g_assert (!gkm_transaction_get_failed (transaction));
388
389 g_assert (g_file_get_contents (filename, &data, &n_data, NULL));
390 g_assert_cmpuint (n_data, ==, 5);
391 g_assert_cmpstr (data, ==, "value");
392 g_free (data);
393
394 gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
395 gkm_transaction_complete (transaction);
396
397 g_assert (!g_file_test (filename, G_FILE_TEST_IS_REGULAR));
398
399 g_object_unref (transaction);
400 }
401
402 static void
403 test_write_file_abort_gone (Test* test, gconstpointer unused)
404 {
405 do_test_write_file_abort_gone (test);
406 do_test_write_file_abort_gone (test);
407 }
408
409 static void
410 do_test_write_file_abort_revert (Test* test)
411 {
412 GkmTransaction *transaction = gkm_transaction_new ();
413 const gchar *filename = "/tmp/transaction-test";
414 gchar *data;
415
416 g_assert (g_file_set_contents (filename, "my original", -1, NULL));
417
418 gkm_transaction_write_file (transaction, filename, (const guchar*)"new value", 9);
419 g_assert (!gkm_transaction_get_failed (transaction));
420
421 g_assert (g_file_get_contents (filename, &data, NULL, NULL));
422 g_assert_cmpstr (data, ==, "new value");
423 g_free (data);
424
425 gkm_transaction_fail (transaction, CKR_GENERAL_ERROR);
426 gkm_transaction_complete (transaction);
427
428 g_assert (g_file_get_contents (filename, &data, NULL, NULL));
429 g_assert_cmpstr (data, ==, "my original");
430 g_free (data);
431
432 g_object_unref (transaction);
433 }
434
435 static void
436 test_write_file_abort_revert (Test* test, gconstpointer unused)
437 {
438 do_test_write_file_abort_revert (test);
439 do_test_write_file_abort_revert (test);
440 }
441
442 static void
443 test_unique_file_conflict (Test* test, gconstpointer unused)
444 {
445 GkmTransaction *transaction = gkm_transaction_new ();
446 const gchar *filename = "/tmp/transaction-test";
447 gchar *dirname;
448 gchar *basename;
449 gchar *result;
450
451 dirname = g_path_get_dirname (filename);
452 basename = g_path_get_basename (filename);
453
454 g_assert (g_file_set_contents (filename, "data", -1, NULL));
455
456 result = gkm_transaction_unique_file (transaction, dirname, basename);
457 g_assert (!gkm_transaction_get_failed (transaction));
458
459 g_assert (result);
460 g_assert_cmpstr (result, !=, basename);
461 g_assert_cmpstr (result, ==, "transaction-test_1");
462
463 g_free (dirname);
464 g_free (basename);
465 g_free (result);
466
467 g_object_unref (transaction);
468 }
469
470 static void
471 test_unique_file_conflict_with_ext (Test* test, gconstpointer unused)
472 {
473 GkmTransaction *transaction = gkm_transaction_new ();
474 const gchar *filename = "/tmp/transaction-test.ext";
475 gchar *dirname;
476 gchar *basename;
477 gchar *result;
478
479 dirname = g_path_get_dirname (filename);
480 basename = g_path_get_basename (filename);
481
482 g_assert (g_file_set_contents (filename, "data", -1, NULL));
483
484 result = gkm_transaction_unique_file (transaction, dirname, basename);
485 g_assert (!gkm_transaction_get_failed (transaction));
486
487 g_assert (result);
488 g_assert_cmpstr (result, !=, basename);
489 g_assert_cmpstr (result, ==, "transaction-test_1.ext");
490
491 g_free (dirname);
492 g_free (basename);
493 g_free (result);
494
495 g_object_unref (transaction);
496 }
497
498 static void
499 test_unique_file_no_conflict (Test* test, gconstpointer unused)
500 {
501 GkmTransaction *transaction = gkm_transaction_new ();
502 const gchar *dirname = "/tmp";
503 gchar *result;
504
505 result = gkm_transaction_unique_file (transaction, dirname, "transaction-another");
506 g_assert (!gkm_transaction_get_failed (transaction));
507
508 g_assert (result);
509 g_assert_cmpstr (result, ==, "transaction-another");
510
511 g_free (result);
512
513 g_object_unref (transaction);
514 }
515
516 int
517 main (int argc, char **argv)
518 {
519 #if !GLIB_CHECK_VERSION(2,35,0)
520 g_type_init ();
521 #endif
522 g_test_init (&argc, &argv, NULL);
523
524 g_test_add ("/gkm/transaction/transaction_empty", Test, NULL, setup, test_transaction_empty, teardown);
525 g_test_add ("/gkm/transaction/transaction_fail", Test, NULL, setup, test_transaction_fail, teardown);
526 g_test_add ("/gkm/transaction/transaction_signals_success", Test, NULL, setup, test_transaction_signals_success, teardown);
527 g_test_add ("/gkm/transaction/transaction_signals_failure", Test, NULL, setup, test_transaction_signals_failure, teardown);
528 g_test_add ("/gkm/transaction/transaction_order_is_reverse", Test, NULL, setup, test_transaction_order_is_reverse, teardown);
529 g_test_add ("/gkm/transaction/transaction_dispose_completes", Test, NULL, setup, test_transaction_dispose_completes, teardown);
530 g_test_add ("/gkm/transaction/remove_file_success", Test, NULL, setup, test_remove_file_success, teardown);
531 g_test_add ("/gkm/transaction/remove_file_abort", Test, NULL, setup, test_remove_file_abort, teardown);
532 g_test_add ("/gkm/transaction/remove_file_non_exist", Test, NULL, setup, test_remove_file_non_exist, teardown);
533 g_test_add ("/gkm/transaction/write_file", Test, NULL, setup, test_write_file, teardown);
534
535 {
536 /* Several test runs at different sizes. Note that
537 the copy function, used if hardlinks do not work,
538 uses a 512 byte buffer. */
539 guint buffersizes[] = {
540 2000, 1024, 510, 511, 512, 513, 514, 0
541 };
542 int i;
543
544 gchar test_name[39];
545 for (i = 0; buffersizes[i]; i++)
546 {
547 g_sprintf (test_name, "/gkm/transaction/write_large_file_%u", buffersizes[i]);
548 g_test_add (test_name, Test, GUINT_TO_POINTER (buffersizes[i]),
549 setup, test_write_large_file, teardown);
550 }
551 }
552
553 g_test_add ("/gkm/transaction/write_file_abort_gone", Test, NULL, setup, test_write_file_abort_gone, teardown);
554 g_test_add ("/gkm/transaction/write_file_abort_revert", Test, NULL, setup, test_write_file_abort_revert, teardown);
555 g_test_add ("/gkm/transaction/unique_file_conflict", Test, NULL, setup, test_unique_file_conflict, teardown);
556 g_test_add ("/gkm/transaction/unique_file_conflict_with_ext", Test, NULL, setup, test_unique_file_conflict_with_ext, teardown);
557 g_test_add ("/gkm/transaction/unique_file_no_conflict", Test, NULL, setup, test_unique_file_no_conflict, teardown);
558
559 return g_test_run ();
560 }