Bump version number
[platform/upstream/libsecret.git] / library / tests / test-methods.c
1 /* libsecret - GLib wrapper for Secret Service
2  *
3  * Copyright 2011 Collabora Ltd.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 2 of the licence or (at
8  * your option) any later version.
9  *
10  * See the included COPYING file for more information.
11  */
12
13
14 #include "config.h"
15
16 #include "secret-attributes.h"
17 #include "secret-collection.h"
18 #include "secret-item.h"
19 #include "secret-paths.h"
20 #include "secret-private.h"
21 #include "secret-service.h"
22
23 #include "mock-service.h"
24
25 #include "egg/egg-testing.h"
26
27 #include <glib.h>
28
29 #include <errno.h>
30 #include <stdlib.h>
31
32 static const SecretSchema MOCK_SCHEMA = {
33         "org.mock.Schema",
34         SECRET_SCHEMA_NONE,
35         {
36                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
37                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
38                 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
39         }
40 };
41
42 static const SecretSchema PRIME_SCHEMA = {
43         "org.mock.Prime",
44         SECRET_SCHEMA_NONE,
45         {
46                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
47                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
48                 { "prime", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
49         }
50 };
51
52 static const SecretSchema NO_NAME_SCHEMA = {
53         "unused.Schema.Name",
54         SECRET_SCHEMA_DONT_MATCH_NAME,
55         {
56                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
57                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
58         }
59 };
60
61 typedef struct {
62         SecretService *service;
63 } Test;
64
65 static void
66 setup_mock (Test *test,
67             gconstpointer data)
68 {
69         GError *error = NULL;
70         const gchar *mock_script = data;
71
72         mock_service_start (mock_script, &error);
73         g_assert_no_error (error);
74 }
75
76 static void
77 setup (Test *test,
78        gconstpointer data)
79 {
80         GError *error = NULL;
81
82         setup_mock (test, data);
83
84         test->service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
85         g_assert_no_error (error);
86 }
87
88 static void
89 teardown_mock (Test *test,
90                gconstpointer unused)
91 {
92         mock_service_stop ();
93 }
94
95 static void
96 teardown (Test *test,
97           gconstpointer unused)
98 {
99         egg_test_wait_idle ();
100
101         g_object_unref (test->service);
102         secret_service_disconnect ();
103         egg_assert_not_object (test->service);
104
105         teardown_mock (test, unused);
106 }
107
108 static void
109 on_complete_get_result (GObject *source,
110                         GAsyncResult *result,
111                         gpointer user_data)
112 {
113         GAsyncResult **ret = user_data;
114         g_assert (ret != NULL);
115         g_assert (*ret == NULL);
116         *ret = g_object_ref (result);
117         egg_test_wait_stop ();
118 }
119
120 static void
121 test_search_sync (Test *test,
122                   gconstpointer used)
123 {
124         GHashTable *attributes;
125         GError *error = NULL;
126         GList *items;
127
128         attributes = g_hash_table_new (g_str_hash, g_str_equal);
129         g_hash_table_insert (attributes, "number", "1");
130
131         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
132                                             SECRET_SEARCH_NONE, NULL, &error);
133         g_assert_no_error (error);
134         g_hash_table_unref (attributes);
135
136         g_assert (items != NULL);
137         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
138
139         g_assert (items->next == NULL);
140         g_list_free_full (items, g_object_unref);
141 }
142
143 static void
144 test_search_async (Test *test,
145                    gconstpointer used)
146 {
147         GAsyncResult *result = NULL;
148         GHashTable *attributes;
149         GError *error = NULL;
150         GList *items;
151
152         attributes = g_hash_table_new (g_str_hash, g_str_equal);
153         g_hash_table_insert (attributes, "number", "1");
154
155         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
156                                SECRET_SEARCH_NONE, NULL,
157                                on_complete_get_result, &result);
158         g_hash_table_unref (attributes);
159         g_assert (result == NULL);
160
161         egg_test_wait ();
162
163         g_assert (G_IS_ASYNC_RESULT (result));
164         items = secret_service_search_finish (test->service, result, &error);
165         g_assert_no_error (error);
166         g_object_unref (result);
167
168         g_assert (items != NULL);
169         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
170
171         g_assert (items->next == NULL);
172         g_list_free_full (items, g_object_unref);
173 }
174
175 static void
176 test_search_all_sync (Test *test,
177                   gconstpointer used)
178 {
179         GHashTable *attributes;
180         GError *error = NULL;
181         GList *items;
182
183         attributes = g_hash_table_new (g_str_hash, g_str_equal);
184         g_hash_table_insert (attributes, "number", "1");
185
186         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
187                                             SECRET_SEARCH_ALL, NULL, &error);
188         g_assert_no_error (error);
189         g_hash_table_unref (attributes);
190
191         g_assert (items != NULL);
192         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
193         g_assert (secret_item_get_locked (items->data) == FALSE);
194         g_assert (secret_item_get_secret (items->data) == NULL);
195
196         g_assert (items->next != NULL);
197         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
198         g_assert (secret_item_get_locked (items->next->data) == TRUE);
199         g_assert (secret_item_get_secret (items->next->data) == NULL);
200
201         g_assert (items->next->next == NULL);
202         g_list_free_full (items, g_object_unref);
203 }
204
205 static void
206 test_search_all_async (Test *test,
207                    gconstpointer used)
208 {
209         GAsyncResult *result = NULL;
210         GHashTable *attributes;
211         GError *error = NULL;
212         GList *items;
213
214         attributes = g_hash_table_new (g_str_hash, g_str_equal);
215         g_hash_table_insert (attributes, "number", "1");
216
217         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
218                                SECRET_SEARCH_ALL, NULL,
219                                on_complete_get_result, &result);
220         g_hash_table_unref (attributes);
221         g_assert (result == NULL);
222
223         egg_test_wait ();
224
225         g_assert (G_IS_ASYNC_RESULT (result));
226         items = secret_service_search_finish (test->service, result, &error);
227         g_assert_no_error (error);
228         g_object_unref (result);
229
230         g_assert (items != NULL);
231         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
232         g_assert (secret_item_get_locked (items->data) == FALSE);
233         g_assert (secret_item_get_secret (items->data) == NULL);
234
235         g_assert (items->next != NULL);
236         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
237         g_assert (secret_item_get_locked (items->next->data) == TRUE);
238         g_assert (secret_item_get_secret (items->next->data) == NULL);
239
240         g_assert (items->next->next == NULL);
241         g_list_free_full (items, g_object_unref);
242 }
243
244 static void
245 test_search_unlock_sync (Test *test,
246                          gconstpointer used)
247 {
248         GHashTable *attributes;
249         GError *error = NULL;
250         GList *items;
251
252         attributes = g_hash_table_new (g_str_hash, g_str_equal);
253         g_hash_table_insert (attributes, "number", "1");
254
255         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
256                                             SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK,
257                                             NULL, &error);
258         g_assert_no_error (error);
259         g_hash_table_unref (attributes);
260
261         g_assert (items != NULL);
262         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
263         g_assert (secret_item_get_locked (items->data) == FALSE);
264         g_assert (secret_item_get_secret (items->data) == NULL);
265
266         g_assert (items->next != NULL);
267         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
268         g_assert (secret_item_get_locked (items->next->data) == FALSE);
269         g_assert (secret_item_get_secret (items->next->data) == NULL);
270
271         g_assert (items->next->next == NULL);
272         g_list_free_full (items, g_object_unref);
273 }
274
275 static void
276 test_search_unlock_async (Test *test,
277                           gconstpointer used)
278 {
279         GAsyncResult *result = NULL;
280         GHashTable *attributes;
281         GError *error = NULL;
282         GList *items;
283
284         attributes = g_hash_table_new (g_str_hash, g_str_equal);
285         g_hash_table_insert (attributes, "number", "1");
286
287         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
288                                SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK, NULL,
289                                on_complete_get_result, &result);
290         g_hash_table_unref (attributes);
291         g_assert (result == NULL);
292
293         egg_test_wait ();
294
295         g_assert (G_IS_ASYNC_RESULT (result));
296         items = secret_service_search_finish (test->service, result, &error);
297         g_assert_no_error (error);
298         g_object_unref (result);
299
300         g_assert (items != NULL);
301         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
302         g_assert (secret_item_get_locked (items->data) == FALSE);
303         g_assert (secret_item_get_secret (items->data) == NULL);
304
305         g_assert (items->next != NULL);
306         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
307         g_assert (secret_item_get_locked (items->next->data) == FALSE);
308         g_assert (secret_item_get_secret (items->next->data) == NULL);
309
310         g_assert (items->next->next == NULL);
311         g_list_free_full (items, g_object_unref);
312 }
313
314 static void
315 test_search_secrets_sync (Test *test,
316                           gconstpointer used)
317 {
318         GHashTable *attributes;
319         GError *error = NULL;
320         SecretValue *value;
321         GList *items;
322
323         attributes = g_hash_table_new (g_str_hash, g_str_equal);
324         g_hash_table_insert (attributes, "number", "1");
325
326         items = secret_service_search_sync (test->service, &MOCK_SCHEMA, attributes,
327                                             SECRET_SEARCH_ALL | SECRET_SEARCH_LOAD_SECRETS,
328                                             NULL, &error);
329         g_assert_no_error (error);
330         g_hash_table_unref (attributes);
331
332         g_assert (items != NULL);
333         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
334         g_assert (secret_item_get_locked (items->data) == FALSE);
335         value = secret_item_get_secret (items->data);
336         g_assert (value != NULL);
337         secret_value_unref (value);
338
339         g_assert (items->next != NULL);
340         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
341         g_assert (secret_item_get_locked (items->next->data) == TRUE);
342         g_assert (secret_item_get_secret (items->next->data) == NULL);
343
344         g_assert (items->next->next == NULL);
345         g_list_free_full (items, g_object_unref);
346 }
347
348 static void
349 test_search_secrets_async (Test *test,
350                            gconstpointer used)
351 {
352         GAsyncResult *result = NULL;
353         GHashTable *attributes;
354         GError *error = NULL;
355         SecretValue *value;
356         GList *items;
357
358         attributes = g_hash_table_new (g_str_hash, g_str_equal);
359         g_hash_table_insert (attributes, "number", "1");
360
361         secret_service_search (test->service, &MOCK_SCHEMA, attributes,
362                                SECRET_SEARCH_ALL | SECRET_SEARCH_LOAD_SECRETS, NULL,
363                                on_complete_get_result, &result);
364         g_hash_table_unref (attributes);
365         g_assert (result == NULL);
366
367         egg_test_wait ();
368
369         g_assert (G_IS_ASYNC_RESULT (result));
370         items = secret_service_search_finish (test->service, result, &error);
371         g_assert_no_error (error);
372         g_object_unref (result);
373
374         g_assert (items != NULL);
375         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->data), ==, "/org/freedesktop/secrets/collection/english/1");
376         g_assert (secret_item_get_locked (items->data) == FALSE);
377         value = secret_item_get_secret (items->data);
378         g_assert (value != NULL);
379         secret_value_unref (value);
380
381         g_assert (items->next != NULL);
382         g_assert_cmpstr (g_dbus_proxy_get_object_path (items->next->data), ==, "/org/freedesktop/secrets/collection/spanish/10");
383         g_assert (secret_item_get_locked (items->next->data) == TRUE);
384         g_assert (secret_item_get_secret (items->next->data) == NULL);
385
386         g_assert (items->next->next == NULL);
387         g_list_free_full (items, g_object_unref);
388 }
389
390 static void
391 test_lock_sync (Test *test,
392                 gconstpointer used)
393 {
394         const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
395         SecretCollection *collection;
396         GError *error = NULL;
397         GList *locked;
398         GList *objects;
399         gboolean ret;
400
401         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
402                                                                SECRET_COLLECTION_NONE, NULL, &error);
403         g_assert_no_error (error);
404
405         objects = g_list_append (NULL, collection);
406
407         ret = secret_service_lock_sync (test->service, objects, NULL, &locked, &error);
408         g_assert_no_error (error);
409         g_assert (ret == TRUE);
410
411         g_assert (locked != NULL);
412         g_assert (locked->data == collection);
413         g_assert (locked->next == NULL);
414         g_list_free_full (locked, g_object_unref);
415
416         g_list_free (objects);
417         g_object_unref (collection);
418 }
419
420 static void
421 test_unlock_sync (Test *test,
422                   gconstpointer used)
423 {
424         const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
425         SecretCollection *collection;
426         GError *error = NULL;
427         GList *unlocked;
428         GList *objects;
429         gboolean ret;
430
431         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
432                                                                SECRET_COLLECTION_NONE, NULL, &error);
433         g_assert_no_error (error);
434
435         objects = g_list_append (NULL, collection);
436
437         ret = secret_service_unlock_sync (test->service, objects, NULL, &unlocked, &error);
438         g_assert_no_error (error);
439         g_assert (ret == TRUE);
440
441         g_assert (unlocked != NULL);
442         g_assert (unlocked->data == collection);
443         g_assert (unlocked->next == NULL);
444         g_list_free_full (unlocked, g_object_unref);
445
446         g_list_free (objects);
447         g_object_unref (collection);
448 }
449
450 static void
451 test_remove_sync (Test *test,
452                   gconstpointer used)
453 {
454         GError *error = NULL;
455         GHashTable *attributes;
456         gboolean ret;
457
458         attributes = secret_attributes_build (&MOCK_SCHEMA,
459                                               "even", FALSE,
460                                               "string", "one",
461                                               "number", 1,
462                                               NULL);
463
464         ret = secret_service_remove_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
465
466         g_assert_no_error (error);
467         g_assert (ret == TRUE);
468
469         g_hash_table_unref (attributes);
470 }
471
472 static void
473 test_remove_async (Test *test,
474                    gconstpointer used)
475 {
476         GError *error = NULL;
477         GAsyncResult *result = NULL;
478         GHashTable *attributes;
479         gboolean ret;
480
481         attributes = secret_attributes_build (&MOCK_SCHEMA,
482                                               "even", FALSE,
483                                               "string", "one",
484                                               "number", 1,
485                                               NULL);
486
487         secret_service_remove (test->service, &MOCK_SCHEMA, attributes, NULL,
488                                 on_complete_get_result, &result);
489
490         g_hash_table_unref (attributes);
491         g_assert (result == NULL);
492
493         egg_test_wait ();
494
495         ret = secret_service_remove_finish (test->service, result, &error);
496         g_assert_no_error (error);
497         g_assert (ret == TRUE);
498
499         g_object_unref (result);
500 }
501
502 static void
503 test_remove_locked (Test *test,
504                     gconstpointer used)
505 {
506         GError *error = NULL;
507         GHashTable *attributes;
508         gboolean ret;
509
510         attributes = secret_attributes_build (&MOCK_SCHEMA,
511                                               "even", FALSE,
512                                               "string", "tres",
513                                               "number", 3,
514                                               NULL);
515
516         /* Locked items can't be removed via this API */
517         ret = secret_service_remove_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
518
519         g_hash_table_unref (attributes);
520         g_assert_no_error (error);
521         g_assert (ret == FALSE);
522 }
523
524 static void
525 test_remove_no_match (Test *test,
526                       gconstpointer used)
527 {
528         GError *error = NULL;
529         GHashTable *attributes;
530         gboolean ret;
531
532         attributes = secret_attributes_build (&MOCK_SCHEMA,
533                                               "even", TRUE,
534                                               "string", "one",
535                                               NULL);
536
537         /* Won't match anything */
538         ret = secret_service_remove_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
539
540         g_hash_table_unref (attributes);
541         g_assert_no_error (error);
542         g_assert (ret == FALSE);
543 }
544
545 static void
546 test_remove_no_name (Test *test,
547                      gconstpointer used)
548 {
549         const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
550         GError *error = NULL;
551         GHashTable *attributes;
552         gboolean ret;
553
554         attributes = secret_attributes_build (&MOCK_SCHEMA,
555                                               "number", 5,
556                                               NULL);
557
558         /* Shouldn't match anything, because no item with 5 in mock schema */
559         ret = secret_service_remove_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
560         g_assert_no_error (error);
561         g_assert (ret == FALSE);
562
563         /* We need this collection unlocked for the next test */
564         secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, NULL, &error);
565         g_assert_no_error (error);
566
567         /* We have an item with 5 in prime schema, but should match anyway becase of flags */
568         ret = secret_service_remove_sync (test->service, &NO_NAME_SCHEMA, attributes, NULL, &error);
569         g_assert_no_error (error);
570         g_assert (ret == TRUE);
571
572         g_hash_table_unref (attributes);
573 }
574
575 static void
576 test_lookup_sync (Test *test,
577                   gconstpointer used)
578 {
579         GError *error = NULL;
580         GHashTable *attributes;
581         SecretValue *value;
582         gsize length;
583
584         attributes = secret_attributes_build (&MOCK_SCHEMA,
585                                               "even", FALSE,
586                                               "string", "one",
587                                               "number", 1,
588                                               NULL);
589
590         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
591
592         g_assert_no_error (error);
593         g_hash_table_unref (attributes);
594
595         g_assert (value != NULL);
596         g_assert_cmpstr (secret_value_get (value, &length), ==, "111");
597         g_assert_cmpuint (length, ==, 3);
598
599         secret_value_unref (value);
600 }
601
602 static void
603 test_lookup_async (Test *test,
604                    gconstpointer used)
605 {
606         GError *error = NULL;
607         GHashTable *attributes;
608         GAsyncResult *result = NULL;
609         SecretValue *value;
610         gsize length;
611
612         attributes = secret_attributes_build (&MOCK_SCHEMA,
613                                               "even", FALSE,
614                                               "string", "one",
615                                               "number", 1,
616                                               NULL);
617
618         secret_service_lookup (test->service, &MOCK_SCHEMA, attributes, NULL,
619                                 on_complete_get_result, &result);
620
621         g_assert (result == NULL);
622         g_hash_table_unref (attributes);
623
624         egg_test_wait ();
625
626         value = secret_service_lookup_finish (test->service, result, &error);
627         g_assert_no_error (error);
628
629         g_assert (value != NULL);
630         g_assert_cmpstr (secret_value_get (value, &length), ==, "111");
631         g_assert_cmpuint (length, ==, 3);
632
633         secret_value_unref (value);
634         g_object_unref (result);
635 }
636
637 static void
638 test_lookup_locked (Test *test,
639                     gconstpointer used)
640 {
641         GError *error = NULL;
642         GHashTable *attributes;
643         SecretValue *value;
644         gsize length;
645
646         attributes = secret_attributes_build (&MOCK_SCHEMA,
647                                               "even", FALSE,
648                                               "string", "tres",
649                                               "number", 3,
650                                               NULL);
651
652         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
653
654         g_assert_no_error (error);
655         g_hash_table_unref (attributes);
656
657         g_assert (value != NULL);
658         g_assert_cmpstr (secret_value_get (value, &length), ==, "3333");
659         g_assert_cmpuint (length, ==, 4);
660
661         secret_value_unref (value);
662 }
663
664 static void
665 test_lookup_no_match (Test *test,
666                       gconstpointer used)
667 {
668         GError *error = NULL;
669         GHashTable *attributes;
670         SecretValue *value;
671
672         attributes = secret_attributes_build (&MOCK_SCHEMA,
673                                               "even", TRUE,
674                                               "string", "one",
675                                               NULL);
676
677         /* Won't match anything */
678         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
679
680         g_assert_no_error (error);
681         g_assert (value == NULL);
682         g_hash_table_unref (attributes);
683 }
684
685 static void
686 test_lookup_no_name (Test *test,
687                      gconstpointer used)
688 {
689         GError *error = NULL;
690         GHashTable *attributes;
691         SecretValue *value;
692         gsize length;
693
694         attributes = secret_attributes_build (&MOCK_SCHEMA,
695                                               "number", 5,
696                                               NULL);
697
698         /* should return null, because nothing with mock schema and 5 */
699         value = secret_service_lookup_sync (test->service, &MOCK_SCHEMA, attributes, NULL, &error);
700         g_assert_no_error (error);
701         g_assert (value == NULL);
702
703         /* should return an item, because we have a prime schema with 5, and flags not to match name */
704         value = secret_service_lookup_sync (test->service, &NO_NAME_SCHEMA, attributes, NULL, &error);
705
706         g_assert_no_error (error);
707
708         g_assert (value != NULL);
709         g_assert_cmpstr (secret_value_get (value, &length), ==, "555");
710         g_assert_cmpuint (length, ==, 3);
711
712         secret_value_unref (value);
713         g_hash_table_unref (attributes);
714 }
715
716 static void
717 test_store_sync (Test *test,
718                  gconstpointer used)
719 {
720         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
721         SecretValue *value = secret_value_new ("apassword", -1, "text/plain");
722         GHashTable *attributes;
723         GError *error = NULL;
724         gchar **paths;
725         gboolean ret;
726         gsize length;
727
728         attributes = secret_attributes_build (&MOCK_SCHEMA,
729                                               "even", FALSE,
730                                               "string", "seventeen",
731                                               "number", 17,
732                                               NULL);
733
734         ret = secret_service_store_sync (test->service, &MOCK_SCHEMA, attributes, collection_path,
735                                          "New Item Label", value, NULL, &error);
736         g_assert_no_error (error);
737         secret_value_unref (value);
738         g_hash_table_unref (attributes);
739
740         attributes = g_hash_table_new (g_str_hash, g_str_equal);
741         g_hash_table_insert (attributes, "even", "false");
742         g_hash_table_insert (attributes, "string", "seventeen");
743         g_hash_table_insert (attributes, "number", "17");
744
745         ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes,
746                                                          NULL, &paths, NULL, &error);
747         g_hash_table_unref (attributes);
748         g_assert (ret == TRUE);
749
750         g_assert (paths != NULL);
751         g_assert (paths[0] != NULL);
752         g_assert (paths[1] == NULL);
753
754         value = secret_service_get_secret_for_dbus_path_sync (test->service, paths[0],
755                                                               NULL, &error);
756         g_assert_no_error (error);
757
758         g_assert (value != NULL);
759         g_assert_cmpstr (secret_value_get (value, &length), ==, "apassword");
760         g_assert_cmpuint (length, ==, 9);
761
762         secret_value_unref (value);
763         g_strfreev (paths);
764 }
765
766 static void
767 test_store_replace (Test *test,
768                     gconstpointer used)
769 {
770         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
771         SecretValue *value = secret_value_new ("apassword", -1, "text/plain");
772         GHashTable *attributes;
773         GError *error = NULL;
774         gchar **paths;
775         gboolean ret;
776
777         attributes = secret_attributes_build (&MOCK_SCHEMA,
778                                               "even", FALSE,
779                                               "string", "seventeen",
780                                               "number", 17,
781                                               NULL);
782
783         ret = secret_service_store_sync (test->service, &MOCK_SCHEMA, attributes, collection_path,
784                                           "New Item Label", value, NULL, &error);
785         g_assert_no_error (error);
786
787         ret = secret_service_store_sync (test->service, &MOCK_SCHEMA, attributes, collection_path,
788                                           "Another Label", value, NULL, &error);
789         g_assert_no_error (error);
790         secret_value_unref (value);
791         g_hash_table_unref (attributes);
792
793         attributes = g_hash_table_new (g_str_hash, g_str_equal);
794         g_hash_table_insert (attributes, "even", "false");
795         g_hash_table_insert (attributes, "string", "seventeen");
796         g_hash_table_insert (attributes, "number", "17");
797
798         ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes,
799                                                          NULL, &paths, NULL, &error);
800         g_hash_table_unref (attributes);
801         g_assert (ret == TRUE);
802
803         g_assert (paths != NULL);
804         g_assert (paths[0] != NULL);
805         g_assert (paths[1] == NULL);
806
807         g_strfreev (paths);
808 }
809
810 static void
811 test_store_async (Test *test,
812                   gconstpointer used)
813 {
814         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
815         SecretValue *value = secret_value_new ("apassword", -1, "text/plain");
816         GAsyncResult *result = NULL;
817         GHashTable *attributes;
818         GError *error = NULL;
819         gchar **paths;
820         gboolean ret;
821         gsize length;
822
823         attributes = secret_attributes_build (&MOCK_SCHEMA,
824                                               "even", FALSE,
825                                               "string", "seventeen",
826                                               "number", 17,
827                                               NULL);
828
829         secret_service_store (test->service, &MOCK_SCHEMA, attributes, collection_path,
830                                "New Item Label", value, NULL, on_complete_get_result, &result);
831         g_assert (result == NULL);
832         secret_value_unref (value);
833         g_hash_table_unref (attributes);
834
835         egg_test_wait ();
836
837         ret = secret_service_store_finish (test->service, result, &error);
838         g_assert_no_error (error);
839         g_object_unref (result);
840
841         attributes = g_hash_table_new (g_str_hash, g_str_equal);
842         g_hash_table_insert (attributes, "even", "false");
843         g_hash_table_insert (attributes, "string", "seventeen");
844         g_hash_table_insert (attributes, "number", "17");
845
846         ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes,
847                                                          NULL, &paths, NULL, &error);
848         g_hash_table_unref (attributes);
849         g_assert (ret == TRUE);
850
851         g_assert (paths != NULL);
852         g_assert (paths[0] != NULL);
853         g_assert (paths[1] == NULL);
854
855         value = secret_service_get_secret_for_dbus_path_sync (test->service, paths[0],
856                                                               NULL, &error);
857         g_assert_no_error (error);
858
859         g_assert (value != NULL);
860         g_assert_cmpstr (secret_value_get (value, &length), ==, "apassword");
861         g_assert_cmpuint (length, ==, 9);
862
863         secret_value_unref (value);
864         g_strfreev (paths);
865 }
866
867 static void
868 test_set_alias_sync (Test *test,
869                      gconstpointer used)
870 {
871         SecretCollection *collection;
872         gchar *blah;
873         GError *error = NULL;
874         gboolean ret;
875
876         blah = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
877         g_assert_no_error (error);
878         g_assert (blah == NULL);
879
880         collection = secret_collection_new_for_dbus_path_sync (test->service,
881                                                                "/org/freedesktop/secrets/collection/english",
882                                                                SECRET_COLLECTION_NONE, NULL, &error);
883         g_assert_no_error (error);
884         g_assert (SECRET_IS_COLLECTION (collection));
885
886         ret = secret_service_set_alias_sync (test->service, "blah", collection, NULL, &error);
887         g_assert_no_error (error);
888         g_assert (ret == TRUE);
889
890         blah = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
891         g_assert_no_error (error);
892         g_assert_cmpstr (blah, ==, g_dbus_proxy_get_object_path (G_DBUS_PROXY (collection)));
893         g_free (blah);
894
895         ret = secret_service_set_alias_sync (test->service, "blah", NULL, NULL, &error);
896         g_assert_no_error (error);
897         g_assert (ret == TRUE);
898
899         blah = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
900         g_assert_no_error (error);
901         g_assert (blah == NULL);
902
903         g_object_unref (collection);
904 }
905
906 int
907 main (int argc, char **argv)
908 {
909         g_test_init (&argc, &argv, NULL);
910         g_set_prgname ("test-service");
911         g_type_init ();
912
913         g_test_add ("/service/search-sync", Test, "mock-service-normal.py", setup, test_search_sync, teardown);
914         g_test_add ("/service/search-async", Test, "mock-service-normal.py", setup, test_search_async, teardown);
915         g_test_add ("/service/search-all-sync", Test, "mock-service-normal.py", setup, test_search_all_sync, teardown);
916         g_test_add ("/service/search-all-async", Test, "mock-service-normal.py", setup, test_search_all_async, teardown);
917         g_test_add ("/service/search-unlock-sync", Test, "mock-service-normal.py", setup, test_search_unlock_sync, teardown);
918         g_test_add ("/service/search-unlock-async", Test, "mock-service-normal.py", setup, test_search_unlock_async, teardown);
919         g_test_add ("/service/search-secrets-sync", Test, "mock-service-normal.py", setup, test_search_secrets_sync, teardown);
920         g_test_add ("/service/search-secrets-async", Test, "mock-service-normal.py", setup, test_search_secrets_async, teardown);
921
922         g_test_add ("/service/lock-sync", Test, "mock-service-lock.py", setup, test_lock_sync, teardown);
923
924         g_test_add ("/service/unlock-sync", Test, "mock-service-lock.py", setup, test_unlock_sync, teardown);
925
926         g_test_add ("/service/lookup-sync", Test, "mock-service-normal.py", setup, test_lookup_sync, teardown);
927         g_test_add ("/service/lookup-async", Test, "mock-service-normal.py", setup, test_lookup_async, teardown);
928         g_test_add ("/service/lookup-locked", Test, "mock-service-normal.py", setup, test_lookup_locked, teardown);
929         g_test_add ("/service/lookup-no-match", Test, "mock-service-normal.py", setup, test_lookup_no_match, teardown);
930         g_test_add ("/service/lookup-no-name", Test, "mock-service-normal.py", setup, test_lookup_no_name, teardown);
931
932         g_test_add ("/service/remove-sync", Test, "mock-service-delete.py", setup, test_remove_sync, teardown);
933         g_test_add ("/service/remove-async", Test, "mock-service-delete.py", setup, test_remove_async, teardown);
934         g_test_add ("/service/remove-locked", Test, "mock-service-delete.py", setup, test_remove_locked, teardown);
935         g_test_add ("/service/remove-no-match", Test, "mock-service-delete.py", setup, test_remove_no_match, teardown);
936         g_test_add ("/service/remove-no-name", Test, "mock-service-delete.py", setup, test_remove_no_name, teardown);
937
938         g_test_add ("/service/store-sync", Test, "mock-service-normal.py", setup, test_store_sync, teardown);
939         g_test_add ("/service/store-async", Test, "mock-service-normal.py", setup, test_store_async, teardown);
940         g_test_add ("/service/store-replace", Test, "mock-service-normal.py", setup, test_store_replace, teardown);
941
942         g_test_add ("/service/set-alias-sync", Test, "mock-service-normal.py", setup, test_set_alias_sync, teardown);
943
944         return egg_tests_run_with_loop ();
945 }