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