Bump version number
[platform/upstream/libsecret.git] / library / tests / test-item.c
1 /* libsecret - GLib wrapper for Secret Service
2  *
3  * Copyright 2012 Red Hat Inc.
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  * Author: Stef Walter <stefw@gnome.org>
13  */
14
15
16 #include "config.h"
17
18 #include "secret-collection.h"
19 #include "secret-item.h"
20 #include "secret-service.h"
21 #include "secret-paths.h"
22 #include "secret-private.h"
23
24 #include "mock-service.h"
25
26 #include "egg/egg-testing.h"
27
28 #include <glib.h>
29
30 #include <errno.h>
31 #include <stdlib.h>
32
33 static const SecretSchema MOCK_SCHEMA = {
34         "org.mock.Schema.Item",
35         SECRET_SCHEMA_NONE,
36         {
37                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
38                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
39                 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
40         }
41 };
42
43 typedef struct {
44         SecretService *service;
45 } Test;
46
47 static void
48 setup (Test *test,
49        gconstpointer data)
50 {
51         GError *error = NULL;
52         const gchar *mock_script = data;
53
54         mock_service_start (mock_script, &error);
55         g_assert_no_error (error);
56
57         test->service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
58         g_assert_no_error (error);
59 }
60
61 static void
62 teardown (Test *test,
63           gconstpointer unused)
64 {
65         g_object_unref (test->service);
66         secret_service_disconnect ();
67         egg_assert_not_object (test->service);
68
69         mock_service_stop ();
70 }
71
72 static void
73 on_async_result (GObject *source,
74                  GAsyncResult *result,
75                  gpointer user_data)
76 {
77         GAsyncResult **ret = user_data;
78         g_assert (ret != NULL);
79         g_assert (*ret == NULL);
80         *ret = g_object_ref (result);
81         egg_test_wait_stop ();
82 }
83
84 static void
85 on_notify_stop (GObject *obj,
86                 GParamSpec *spec,
87                 gpointer user_data)
88 {
89         guint *sigs = user_data;
90         g_assert (sigs != NULL);
91         g_assert (*sigs > 0);
92         if (--(*sigs) == 0)
93                 egg_test_wait_stop ();
94 }
95
96 static void
97 test_new_sync (Test *test,
98                gconstpointer unused)
99 {
100         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
101         GError *error = NULL;
102         SecretItem *item;
103
104         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
105         g_assert_no_error (error);
106
107         g_assert_cmpstr (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), ==, item_path);
108
109         g_object_unref (item);
110 }
111
112 static void
113 test_new_sync_noexist (Test *test,
114                        gconstpointer unused)
115 {
116         const gchar *item_path = "/org/freedesktop/secrets/collection/english/0000";
117         GError *error = NULL;
118         SecretItem *item;
119
120         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
121         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
122         g_assert (item == NULL);
123 }
124
125 static void
126 test_new_async (Test *test,
127                 gconstpointer unused)
128 {
129         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
130         GAsyncResult *result = NULL;
131         GError *error = NULL;
132         SecretItem *item;
133
134         secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE,
135                                        NULL, on_async_result, &result);
136         g_assert (result == NULL);
137
138         egg_test_wait ();
139
140         item = secret_item_new_for_dbus_path_finish (result, &error);
141         g_assert_no_error (error);
142         g_object_unref (result);
143
144         g_assert_cmpstr (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), ==, item_path);
145
146         g_object_unref (item);
147 }
148
149 static void
150 test_new_async_noexist (Test *test,
151                         gconstpointer unused)
152 {
153         const gchar *item_path = "/org/freedesktop/secrets/collection/english/0000";
154         GAsyncResult *result = NULL;
155         GError *error = NULL;
156         SecretItem *item;
157
158         secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE,
159                                        NULL, on_async_result, &result);
160         g_assert (result == NULL);
161
162         egg_test_wait ();
163
164         item = secret_item_new_for_dbus_path_finish (result, &error);
165         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
166         g_assert (item == NULL);
167         g_object_unref (result);
168 }
169
170 static void
171 test_create_sync (Test *test,
172                   gconstpointer unused)
173 {
174         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
175         SecretCollection *collection;
176         GError *error = NULL;
177         SecretItem *item;
178         GHashTable *attributes;
179         SecretValue *value;
180
181         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
182                                                                SECRET_COLLECTION_NONE, NULL, &error);
183         g_assert_no_error (error);
184
185         attributes = g_hash_table_new (g_str_hash, g_str_equal);
186         g_hash_table_insert (attributes, "even", "true");
187         g_hash_table_insert (attributes, "string", "ten");
188         g_hash_table_insert (attributes, "number", "10");
189
190         value = secret_value_new ("Hoohah", -1, "text/plain");
191
192         item = secret_item_create_sync (collection, &MOCK_SCHEMA, attributes, "Tunnel",
193                                         value, SECRET_ITEM_CREATE_NONE, NULL, &error);
194         g_assert_no_error (error);
195
196         g_hash_table_unref (attributes);
197         g_object_unref (collection);
198         secret_value_unref (value);
199
200         g_assert (g_str_has_prefix (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), collection_path));
201         g_assert_cmpstr (secret_item_get_label (item), ==, "Tunnel");
202         g_assert (secret_item_get_locked (item) == FALSE);
203
204         g_object_unref (item);
205         egg_assert_not_object (item);
206 }
207
208 static void
209 test_create_async (Test *test,
210                    gconstpointer unused)
211 {
212         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
213         SecretCollection *collection;
214         GAsyncResult *result = NULL;
215         GError *error = NULL;
216         SecretItem *item;
217         GHashTable *attributes;
218         SecretValue *value;
219
220         collection = secret_collection_new_for_dbus_path_sync (test->service, collection_path,
221                                                                SECRET_COLLECTION_NONE, NULL, &error);
222         g_assert_no_error (error);
223
224         attributes = g_hash_table_new (g_str_hash, g_str_equal);
225         g_hash_table_insert (attributes, "even", "true");
226         g_hash_table_insert (attributes, "string", "ten");
227         g_hash_table_insert (attributes, "number", "10");
228
229         value = secret_value_new ("Hoohah", -1, "text/plain");
230
231         secret_item_create (collection, &MOCK_SCHEMA, attributes, "Tunnel",
232                             value, SECRET_ITEM_CREATE_NONE, NULL, on_async_result, &result);
233         g_assert_no_error (error);
234
235         g_hash_table_unref (attributes);
236         g_object_unref (collection);
237         secret_value_unref (value);
238
239         egg_test_wait ();
240
241         item = secret_item_create_finish (result, &error);
242         g_assert_no_error (error);
243         g_object_unref (result);
244
245         g_assert (g_str_has_prefix (g_dbus_proxy_get_object_path (G_DBUS_PROXY (item)), collection_path));
246         g_assert_cmpstr (secret_item_get_label (item), ==, "Tunnel");
247         g_assert (secret_item_get_locked (item) == FALSE);
248
249         g_object_unref (item);
250         egg_assert_not_object (item);
251 }
252
253 static void
254 test_properties (Test *test,
255                  gconstpointer unused)
256 {
257         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
258         GError *error = NULL;
259         GHashTable *attributes;
260         SecretService *service;
261         SecretItem *item;
262         guint64 created;
263         guint64 modified;
264         gboolean locked;
265         gchar *label;
266
267         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
268         g_assert_no_error (error);
269
270         g_assert (secret_item_get_locked (item) == FALSE);
271         g_assert_cmpuint (secret_item_get_created (item), <=, time (NULL));
272         g_assert_cmpuint (secret_item_get_modified (item), <=, time (NULL));
273
274         label = secret_item_get_label (item);
275         g_assert_cmpstr (label, ==, "Item One");
276         g_free (label);
277
278         attributes = secret_item_get_attributes (item);
279         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
280         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
281         g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
282         g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
283         g_hash_table_unref (attributes);
284
285         g_object_get (item,
286                       "locked", &locked,
287                       "created", &created,
288                       "modified", &modified,
289                       "label", &label,
290                       "attributes", &attributes,
291                       "service", &service,
292                       NULL);
293
294         g_assert (locked == FALSE);
295         g_assert_cmpuint (created, <=, time (NULL));
296         g_assert_cmpuint (modified, <=, time (NULL));
297
298         g_assert_cmpstr (label, ==, "Item One");
299         g_free (label);
300
301         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
302         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
303         g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
304         g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
305         g_hash_table_unref (attributes);
306
307         g_assert (service == test->service);
308         g_object_unref (service);
309
310         g_object_unref (item);
311 }
312
313 static void
314 test_set_label_sync (Test *test,
315                      gconstpointer unused)
316 {
317         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
318         GError *error = NULL;
319         SecretItem *item;
320         gboolean ret;
321         gchar *label;
322
323         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
324         g_assert_no_error (error);
325
326         label = secret_item_get_label (item);
327         g_assert_cmpstr (label, ==, "Item One");
328         g_free (label);
329
330         ret = secret_item_set_label_sync (item, "Another label", NULL, &error);
331         g_assert_no_error (error);
332         g_assert (ret == TRUE);
333
334         label = secret_item_get_label (item);
335         g_assert_cmpstr (label, ==, "Another label");
336         g_free (label);
337
338         g_object_unref (item);
339 }
340
341 static void
342 test_set_label_async (Test *test,
343                       gconstpointer unused)
344 {
345         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
346         GAsyncResult *result = NULL;
347         GError *error = NULL;
348         SecretItem *item;
349         gboolean ret;
350         gchar *label;
351
352         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
353         g_assert_no_error (error);
354
355         label = secret_item_get_label (item);
356         g_assert_cmpstr (label, ==, "Item One");
357         g_free (label);
358
359         secret_item_set_label (item, "Another label", NULL, on_async_result, &result);
360         g_assert (result == NULL);
361
362         egg_test_wait ();
363
364         ret = secret_item_set_label_finish (item, result, &error);
365         g_assert_no_error (error);
366         g_assert (ret == TRUE);
367         g_object_unref (result);
368
369         label = secret_item_get_label (item);
370         g_assert_cmpstr (label, ==, "Another label");
371         g_free (label);
372
373         g_object_unref (item);
374 }
375
376 static void
377 test_set_label_prop (Test *test,
378                      gconstpointer unused)
379 {
380         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
381         GAsyncResult *result = NULL;
382         GError *error = NULL;
383         SecretItem *item;
384         guint sigs = 2;
385         gchar *label;
386
387         secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE, NULL, on_async_result, &result);
388         g_assert (result == NULL);
389         egg_test_wait ();
390         item = secret_item_new_for_dbus_path_finish (result, &error);
391         g_assert_no_error (error);
392         g_object_unref (result);
393
394         label = secret_item_get_label (item);
395         g_assert_cmpstr (label, ==, "Item One");
396         g_free (label);
397
398         g_signal_connect (item, "notify::label", G_CALLBACK (on_notify_stop), &sigs);
399         g_object_set (item, "label", "Blah blah", NULL);
400
401         /* Wait for the property to actually 'take' */
402         egg_test_wait ();
403
404         label = secret_item_get_label (item);
405         g_assert_cmpstr (label, ==, "Blah blah");
406         g_free (label);
407
408         g_object_unref (item);
409 }
410
411 static void
412 test_set_attributes_sync (Test *test,
413                            gconstpointer unused)
414 {
415         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
416         GError *error = NULL;
417         SecretItem *item;
418         gboolean ret;
419         GHashTable *attributes;
420         gchar *schema_name;
421
422         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
423         g_assert_no_error (error);
424
425         attributes = secret_item_get_attributes (item);
426         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
427         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
428         g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
429         g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
430         g_hash_table_unref (attributes);
431
432         /* Has some other schema */
433         schema_name = secret_item_get_schema_name (item);
434         g_assert_cmpstr (schema_name, !=, MOCK_SCHEMA.name);
435         g_free (schema_name);
436
437         attributes = g_hash_table_new (g_str_hash, g_str_equal);
438         g_hash_table_insert (attributes, "string", "five");
439         g_hash_table_insert (attributes, "number", "5");
440         ret = secret_item_set_attributes_sync (item, &MOCK_SCHEMA, attributes, NULL, &error);
441         g_hash_table_unref (attributes);
442         g_assert_no_error (error);
443         g_assert (ret == TRUE);
444
445         attributes = secret_item_get_attributes (item);
446         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
447         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5");
448         g_assert_cmpuint (g_hash_table_size (attributes), ==, 3);
449         g_hash_table_unref (attributes);
450
451         /* Now has our schema */
452         schema_name = secret_item_get_schema_name (item);
453         g_assert_cmpstr (schema_name, ==, MOCK_SCHEMA.name);
454         g_free (schema_name);
455
456         g_object_unref (item);
457 }
458
459 static void
460 test_set_attributes_async (Test *test,
461                            gconstpointer unused)
462 {
463         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
464         GHashTable *attributes;
465         GError *error = NULL;
466         GAsyncResult *result = NULL;
467         SecretItem *item;
468         gchar *schema_name;
469         gboolean ret;
470
471         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
472         g_assert_no_error (error);
473
474         attributes = secret_item_get_attributes (item);
475         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
476         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
477         g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
478         g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
479         g_hash_table_unref (attributes);
480
481         /* Has some other schema */
482         schema_name = secret_item_get_schema_name (item);
483         g_assert_cmpstr (schema_name, !=, MOCK_SCHEMA.name);
484         g_free (schema_name);
485
486         attributes = g_hash_table_new (g_str_hash, g_str_equal);
487         g_hash_table_insert (attributes, "string", "five");
488         g_hash_table_insert (attributes, "number", "5");
489         secret_item_set_attributes (item, &MOCK_SCHEMA, attributes, NULL, on_async_result, &result);
490         g_assert (result == NULL);
491
492         egg_test_wait ();
493
494         ret = secret_item_set_attributes_finish (item, result, &error);
495         g_assert_no_error (error);
496         g_assert (ret == TRUE);
497         g_object_unref (result);
498
499         attributes = secret_item_get_attributes (item);
500         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
501         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5");
502         g_assert_cmpuint (g_hash_table_size (attributes), ==, 3);
503         g_hash_table_unref (attributes);
504
505         /* Now has our schema */
506         schema_name = secret_item_get_schema_name (item);
507         g_assert_cmpstr (schema_name, ==, MOCK_SCHEMA.name);
508         g_free (schema_name);
509
510         g_object_unref (item);
511 }
512
513 static void
514 test_set_attributes_prop (Test *test,
515                           gconstpointer unused)
516 {
517         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
518         GAsyncResult *result = NULL;
519         GError *error = NULL;
520         SecretItem *item;
521         GHashTable *attributes;
522         guint sigs = 2;
523
524         secret_item_new_for_dbus_path (test->service, item_path, SECRET_ITEM_NONE, NULL, on_async_result, &result);
525         g_assert (result == NULL);
526         egg_test_wait ();
527         item = secret_item_new_for_dbus_path_finish (result, &error);
528         g_assert_no_error (error);
529         g_object_unref (result);
530
531         attributes = secret_item_get_attributes (item);
532         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "one");
533         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "1");
534         g_assert_cmpstr (g_hash_table_lookup (attributes, "even"), ==, "false");
535         g_assert_cmpuint (g_hash_table_size (attributes), ==, 4);
536         g_hash_table_unref (attributes);
537
538         g_signal_connect (item, "notify::attributes", G_CALLBACK (on_notify_stop), &sigs);
539
540         attributes = g_hash_table_new (g_str_hash, g_str_equal);
541         g_hash_table_insert (attributes, "string", "five");
542         g_hash_table_insert (attributes, "number", "5");
543         g_object_set (item, "attributes", attributes, NULL);
544         g_hash_table_unref (attributes);
545
546         /* Wait for the property to actually 'take' */
547         egg_test_wait ();
548
549         attributes = secret_item_get_attributes (item);
550         g_assert_cmpstr (g_hash_table_lookup (attributes, "string"), ==, "five");
551         g_assert_cmpstr (g_hash_table_lookup (attributes, "number"), ==, "5");
552         g_assert_cmpuint (g_hash_table_size (attributes), ==, 2);
553         g_hash_table_unref (attributes);
554
555         g_object_unref (item);
556 }
557
558 static void
559 test_load_secret_sync (Test *test,
560                        gconstpointer unused)
561 {
562         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
563         GError *error = NULL;
564         SecretItem *item;
565         SecretValue *value;
566         gconstpointer data;
567         gboolean ret;
568         gsize length;
569
570         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
571         g_assert_no_error (error);
572
573         value = secret_item_get_secret (item);
574         g_assert (value == NULL);
575
576         ret = secret_item_load_secret_sync (item, NULL, &error);
577         g_assert_no_error (error);
578         g_assert (ret == TRUE);
579
580         value = secret_item_get_secret (item);
581         g_assert (value != NULL);
582
583         data = secret_value_get (value, &length);
584         egg_assert_cmpmem (data, length, ==, "111", 3);
585
586         secret_value_unref (value);
587
588         g_object_unref (item);
589 }
590
591 static void
592 test_load_secret_async (Test *test,
593                         gconstpointer unused)
594 {
595         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
596         GAsyncResult *result = NULL;
597         GError *error = NULL;
598         SecretItem *item;
599         SecretValue *value;
600         gconstpointer data;
601         gboolean ret;
602         gsize length;
603
604         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
605         g_assert_no_error (error);
606
607         value = secret_item_get_secret (item);
608         g_assert (value == NULL);
609
610         secret_item_load_secret (item, NULL, on_async_result, &result);
611         g_assert (result == NULL);
612
613         egg_test_wait ();
614
615         ret = secret_item_load_secret_finish (item, result, &error);
616         g_assert_no_error (error);
617         g_assert (ret == TRUE);
618         g_object_unref (result);
619
620         value = secret_item_get_secret (item);
621         g_assert (value != NULL);
622
623         data = secret_value_get (value, &length);
624         egg_assert_cmpmem (data, length, ==, "111", 3);
625
626         secret_value_unref (value);
627
628         g_object_unref (item);
629 }
630
631 static void
632 test_set_secret_sync (Test *test,
633                       gconstpointer unused)
634 {
635         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
636         GError *error = NULL;
637         SecretItem *item;
638         gconstpointer data;
639         SecretValue *value;
640         SecretValue *check;
641         gboolean ret;
642         gsize length;
643
644         value = secret_value_new ("Sinking", -1, "strange/content-type");
645
646         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
647         g_assert_no_error (error);
648
649         ret = secret_item_set_secret_sync (item, value, NULL, &error);
650         g_assert_no_error (error);
651         g_assert (ret == TRUE);
652
653         check = secret_item_get_secret (item);
654         g_assert (check == value);
655         secret_value_unref (check);
656         secret_value_unref (value);
657
658         ret = secret_item_load_secret_sync (item, NULL, &error);
659         g_assert_no_error (error);
660         g_assert (ret == TRUE);
661
662         value = secret_item_get_secret (item);
663         g_assert (value != NULL);
664
665         data = secret_value_get (value, &length);
666         egg_assert_cmpmem (data, length, ==, "Sinking", 7);
667         g_assert_cmpstr (secret_value_get_content_type (value), ==, "strange/content-type");
668
669         secret_value_unref (value);
670         g_object_unref (item);
671 }
672
673 static void
674 test_secrets_sync (Test *test,
675                    gconstpointer used)
676 {
677         const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
678         const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
679         const gchar *path_item_three = "/org/freedesktop/secrets/collection/spanish/10";
680
681         SecretValue *value;
682         GError *error = NULL;
683         const gchar *password;
684         SecretItem *item_one, *item_two, *item_three;
685         GList *items = NULL;
686         gboolean ret;
687         gsize length;
688
689         item_one = secret_item_new_for_dbus_path_sync (test->service, path_item_one, SECRET_ITEM_NONE, NULL, &error);
690         item_two = secret_item_new_for_dbus_path_sync (test->service, path_item_two, SECRET_ITEM_NONE, NULL, &error);
691         item_three = secret_item_new_for_dbus_path_sync (test->service, path_item_three, SECRET_ITEM_NONE, NULL, &error);
692
693         items = g_list_append (items, item_one);
694         items = g_list_append (items, item_two);
695         items = g_list_append (items, item_three);
696
697         ret = secret_item_load_secrets_sync (items, NULL, &error);
698         g_assert_no_error (error);
699         g_assert (ret == TRUE);
700
701         value = secret_item_get_secret (item_one);
702         g_assert (value != NULL);
703         password = secret_value_get (value, &length);
704         g_assert_cmpuint (length, ==, 3);
705         g_assert_cmpstr (password, ==, "111");
706         secret_value_unref (value);
707
708         value = secret_item_get_secret (item_two);
709         g_assert (value != NULL);
710         password = secret_value_get (value, &length);
711         g_assert_cmpuint (length, ==, 3);
712         g_assert_cmpstr (password, ==, "222");
713         secret_value_unref (value);
714
715         value = secret_item_get_secret (item_three);
716         g_assert (value == NULL);
717
718         g_list_free_full (items, g_object_unref);
719 }
720
721 static void
722 test_secrets_async (Test *test,
723                               gconstpointer used)
724 {
725         const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
726         const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
727         const gchar *path_item_three = "/org/freedesktop/secrets/collection/spanish/10";
728
729         SecretValue *value;
730         GError *error = NULL;
731         const gchar *password;
732         GAsyncResult *result = NULL;
733         SecretItem *item_one, *item_two, *item_three;
734         GList *items = NULL;
735         gsize length;
736         gboolean ret;
737
738         item_one = secret_item_new_for_dbus_path_sync (test->service, path_item_one, SECRET_ITEM_NONE, NULL, &error);
739         g_assert_no_error (error);
740
741         item_two = secret_item_new_for_dbus_path_sync (test->service, path_item_two, SECRET_ITEM_NONE, NULL, &error);
742         g_assert_no_error (error);
743
744         item_three = secret_item_new_for_dbus_path_sync (test->service, path_item_three, SECRET_ITEM_NONE, NULL, &error);
745         g_assert_no_error (error);
746
747
748         items = g_list_append (items, item_one);
749         items = g_list_append (items, item_two);
750         items = g_list_append (items, item_three);
751
752         secret_item_load_secrets (items, NULL,
753                                   on_async_result, &result);
754         g_assert (result == NULL);
755
756         egg_test_wait ();
757
758         ret = secret_item_load_secrets_finish (result, &error);
759         g_assert_no_error (error);
760         g_object_unref (result);
761         g_assert (ret == TRUE);
762
763         value = secret_item_get_secret (item_one);
764         g_assert (value != NULL);
765         password = secret_value_get (value, &length);
766         g_assert_cmpuint (length, ==, 3);
767         g_assert_cmpstr (password, ==, "111");
768         secret_value_unref (value);
769
770         value = secret_item_get_secret (item_two);
771         g_assert (value != NULL);
772         password = secret_value_get (value, &length);
773         g_assert_cmpuint (length, ==, 3);
774         g_assert_cmpstr (password, ==, "222");
775         secret_value_unref (value);
776
777         value = secret_item_get_secret (item_three);
778         g_assert (value == NULL);
779
780         g_object_unref (item_one);
781         g_object_unref (item_two);
782         g_object_unref (item_three);
783         g_list_free (items);
784 }
785
786 static void
787 test_delete_sync (Test *test,
788                   gconstpointer unused)
789 {
790         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
791         GError *error = NULL;
792         SecretItem *item;
793         gboolean ret;
794
795         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
796         g_assert_no_error (error);
797
798         ret = secret_item_delete_sync (item, NULL, &error);
799         g_assert_no_error (error);
800         g_assert (ret == TRUE);
801
802         g_object_unref (item);
803
804         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
805         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
806         g_assert (item == NULL);
807 }
808
809 static void
810 test_delete_async (Test *test,
811                    gconstpointer unused)
812 {
813         const gchar *item_path = "/org/freedesktop/secrets/collection/english/1";
814         GAsyncResult *result = NULL;
815         GError *error = NULL;
816         SecretItem *item;
817         gboolean ret;
818
819         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
820         g_assert_no_error (error);
821
822         secret_item_delete (item, NULL, on_async_result, &result);
823         g_assert (result == NULL);
824
825         egg_test_wait ();
826
827         ret = secret_item_delete_finish (item, result, &error);
828         g_assert_no_error (error);
829         g_assert (ret == TRUE);
830
831         g_object_unref (item);
832
833         item = secret_item_new_for_dbus_path_sync (test->service, item_path, SECRET_ITEM_NONE, NULL, &error);
834         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_UNKNOWN_METHOD);
835         g_assert (item == NULL);
836 }
837
838 int
839 main (int argc, char **argv)
840 {
841         g_test_init (&argc, &argv, NULL);
842         g_set_prgname ("test-item");
843         g_type_init ();
844
845         g_test_add ("/item/new-sync", Test, "mock-service-normal.py", setup, test_new_sync, teardown);
846         g_test_add ("/item/new-async", Test, "mock-service-normal.py", setup, test_new_async, teardown);
847         g_test_add ("/item/new-sync-noexist", Test, "mock-service-normal.py", setup, test_new_sync_noexist, teardown);
848         g_test_add ("/item/new-async-noexist", Test, "mock-service-normal.py", setup, test_new_async_noexist, teardown);
849         g_test_add ("/item/create-sync", Test, "mock-service-normal.py", setup, test_create_sync, teardown);
850         g_test_add ("/item/create-async", Test, "mock-service-normal.py", setup, test_create_async, teardown);
851         g_test_add ("/item/properties", Test, "mock-service-normal.py", setup, test_properties, teardown);
852         g_test_add ("/item/set-label-sync", Test, "mock-service-normal.py", setup, test_set_label_sync, teardown);
853         g_test_add ("/item/set-label-async", Test, "mock-service-normal.py", setup, test_set_label_async, teardown);
854         g_test_add ("/item/set-label-prop", Test, "mock-service-normal.py", setup, test_set_label_prop, teardown);
855         g_test_add ("/item/set-attributes-sync", Test, "mock-service-normal.py", setup, test_set_attributes_sync, teardown);
856         g_test_add ("/item/set-attributes-async", Test, "mock-service-normal.py", setup, test_set_attributes_async, teardown);
857         g_test_add ("/item/set-attributes-prop", Test, "mock-service-normal.py", setup, test_set_attributes_prop, teardown);
858         g_test_add ("/item/load-secret-sync", Test, "mock-service-normal.py", setup, test_load_secret_sync, teardown);
859         g_test_add ("/item/load-secret-async", Test, "mock-service-normal.py", setup, test_load_secret_async, teardown);
860         g_test_add ("/item/set-secret-sync", Test, "mock-service-normal.py", setup, test_set_secret_sync, teardown);
861         g_test_add ("/item/secrets-sync", Test, "mock-service-normal.py", setup, test_secrets_sync, teardown);
862         g_test_add ("/item/secrets-async", Test, "mock-service-normal.py", setup, test_secrets_async, teardown);
863         g_test_add ("/item/delete-sync", Test, "mock-service-normal.py", setup, test_delete_sync, teardown);
864         g_test_add ("/item/delete-async", Test, "mock-service-normal.py", setup, test_delete_async, teardown);
865
866         return egg_tests_run_with_loop ();
867 }