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