1 /* libsecret - GLib wrapper for Secret Service
3 * Copyright 2011 Collabora Ltd.
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.
10 * See the included COPYING file for more information.
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"
23 #include "mock-service.h"
25 #include "egg/egg-testing.h"
32 static const SecretSchema MOCK_SCHEMA = {
36 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
37 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
38 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
42 static const SecretSchema PRIME_SCHEMA = {
46 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
47 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
48 { "prime", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
52 static const SecretSchema NO_NAME_SCHEMA = {
54 SECRET_SCHEMA_DONT_MATCH_NAME,
56 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
57 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
62 SecretService *service;
66 setup_mock (Test *test,
70 const gchar *mock_script = data;
72 mock_service_start (mock_script, &error);
73 g_assert_no_error (error);
82 setup_mock (test, data);
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);
90 teardown_mock (Test *test,
93 secret_service_disconnect ();
101 egg_test_wait_idle ();
103 g_object_unref (test->service);
104 secret_service_disconnect ();
105 g_assert (test->service == NULL);
107 teardown_mock (test, unused);
111 on_complete_get_result (GObject *source,
112 GAsyncResult *result,
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 ();
123 test_search_paths_sync (Test *test,
126 GHashTable *attributes;
130 GError *error = NULL;
132 attributes = g_hash_table_new (g_str_hash, g_str_equal);
133 g_hash_table_insert (attributes, "number", "1");
135 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
136 &unlocked, &locked, &error);
137 g_assert_no_error (error);
138 g_assert (ret == TRUE);
141 g_assert_cmpstr (locked[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
144 g_assert_cmpstr (unlocked[0], ==, "/org/freedesktop/secrets/collection/english/1");
146 g_strfreev (unlocked);
149 g_hash_table_unref (attributes);
153 test_search_paths_async (Test *test,
156 GAsyncResult *result = NULL;
157 GHashTable *attributes;
161 GError *error = NULL;
163 attributes = g_hash_table_new (g_str_hash, g_str_equal);
164 g_hash_table_insert (attributes, "number", "1");
166 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
167 on_complete_get_result, &result);
170 g_assert (G_IS_ASYNC_RESULT (result));
171 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
174 g_assert_no_error (error);
175 g_assert (ret == TRUE);
178 g_assert_cmpstr (locked[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
181 g_assert_cmpstr (unlocked[0], ==, "/org/freedesktop/secrets/collection/english/1");
183 g_strfreev (unlocked);
185 g_object_unref (result);
187 g_hash_table_unref (attributes);
191 test_search_paths_nulls (Test *test,
194 GAsyncResult *result = NULL;
195 GHashTable *attributes;
198 GError *error = NULL;
200 attributes = g_hash_table_new (g_str_hash, g_str_equal);
201 g_hash_table_insert (attributes, "number", "1");
203 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
204 &paths, NULL, &error);
205 g_assert_no_error (error);
206 g_assert (ret == TRUE);
207 g_assert (paths != NULL);
208 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/english/1");
211 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
212 NULL, &paths, &error);
213 g_assert_no_error (error);
214 g_assert (ret == TRUE);
215 g_assert (paths != NULL);
216 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
219 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
221 g_assert_no_error (error);
222 g_assert (ret == TRUE);
224 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
225 on_complete_get_result, &result);
227 g_assert (G_IS_ASYNC_RESULT (result));
228 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
229 &paths, NULL, &error);
230 g_assert_no_error (error);
231 g_assert (ret == TRUE);
232 g_assert (paths != NULL);
233 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/english/1");
235 g_clear_object (&result);
237 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
238 on_complete_get_result, &result);
240 g_assert (G_IS_ASYNC_RESULT (result));
241 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
242 NULL, &paths, &error);
243 g_assert_no_error (error);
244 g_assert (ret == TRUE);
245 g_assert (paths != NULL);
246 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
248 g_clear_object (&result);
250 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
251 on_complete_get_result, &result);
253 g_assert (G_IS_ASYNC_RESULT (result));
254 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
256 g_assert_no_error (error);
257 g_assert (ret == TRUE);
258 g_clear_object (&result);
260 g_hash_table_unref (attributes);
264 test_secret_for_path_sync (Test *test,
268 GError *error = NULL;
270 const gchar *password;
273 path = "/org/freedesktop/secrets/collection/english/1";
274 value = secret_service_get_secret_for_dbus_path_sync (test->service, path, NULL, &error);
275 g_assert_no_error (error);
276 g_assert (value != NULL);
278 password = secret_value_get (value, &length);
279 g_assert_cmpuint (length, ==, 3);
280 g_assert_cmpstr (password, ==, "111");
282 password = secret_value_get (value, NULL);
283 g_assert_cmpstr (password, ==, "111");
285 secret_value_unref (value);
289 test_secret_for_path_async (Test *test,
293 GError *error = NULL;
295 const gchar *password;
296 GAsyncResult *result = NULL;
299 path = "/org/freedesktop/secrets/collection/english/1";
300 secret_service_get_secret_for_dbus_path (test->service, path, NULL,
301 on_complete_get_result, &result);
302 g_assert (result == NULL);
305 value = secret_service_get_secret_for_dbus_path_finish (test->service, result, &error);
306 g_assert_no_error (error);
307 g_assert (value != NULL);
308 g_object_unref (result);
310 password = secret_value_get (value, &length);
311 g_assert_cmpuint (length, ==, 3);
312 g_assert_cmpstr (password, ==, "111");
314 password = secret_value_get (value, NULL);
315 g_assert_cmpstr (password, ==, "111");
317 secret_value_unref (value);
321 test_secrets_for_paths_sync (Test *test,
324 const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
325 const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
326 const gchar *paths[] = {
330 /* This one is locked, and not returned */
331 "/org/freedesktop/secrets/collection/spanish/10",
337 GError *error = NULL;
338 const gchar *password;
341 values = secret_service_get_secrets_for_dbus_paths_sync (test->service, paths, NULL, &error);
342 g_assert_no_error (error);
344 g_assert (values != NULL);
345 g_assert_cmpuint (g_hash_table_size (values), ==, 2);
347 value = g_hash_table_lookup (values, path_item_one);
348 g_assert (value != NULL);
349 password = secret_value_get (value, &length);
350 g_assert_cmpuint (length, ==, 3);
351 g_assert_cmpstr (password, ==, "111");
353 value = g_hash_table_lookup (values, path_item_two);
354 g_assert (value != NULL);
355 password = secret_value_get (value, &length);
356 g_assert_cmpuint (length, ==, 3);
357 g_assert_cmpstr (password, ==, "222");
359 g_hash_table_unref (values);
363 test_secrets_for_paths_async (Test *test,
366 const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
367 const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
368 const gchar *paths[] = {
372 /* This one is locked, and not returned */
373 "/org/freedesktop/secrets/collection/spanish/10",
379 GError *error = NULL;
380 const gchar *password;
381 GAsyncResult *result = NULL;
384 secret_service_get_secrets_for_dbus_paths (test->service, paths, NULL,
385 on_complete_get_result, &result);
386 g_assert (result == NULL);
389 values = secret_service_get_secrets_for_dbus_paths_finish (test->service, result, &error);
390 g_assert_no_error (error);
391 g_object_unref (result);
393 g_assert (values != NULL);
394 g_assert_cmpuint (g_hash_table_size (values), ==, 2);
396 value = g_hash_table_lookup (values, path_item_one);
397 g_assert (value != NULL);
398 password = secret_value_get (value, &length);
399 g_assert_cmpuint (length, ==, 3);
400 g_assert_cmpstr (password, ==, "111");
402 value = g_hash_table_lookup (values, path_item_two);
403 g_assert (value != NULL);
404 password = secret_value_get (value, &length);
405 g_assert_cmpuint (length, ==, 3);
406 g_assert_cmpstr (password, ==, "222");
408 g_hash_table_unref (values);
412 test_delete_for_path_sync (Test *test,
416 const gchar *path_item_one = "/org/freedesktop/secrets/collection/todelete/item";
417 GError *error = NULL;
420 ret = secret_service_delete_item_dbus_path_sync (test->service, path_item_one, NULL, &error);
421 g_assert_no_error (error);
422 g_assert (ret == TRUE);
426 test_delete_for_path_sync_prompt (Test *test,
430 const gchar *path_item_one = "/org/freedesktop/secrets/collection/todelete/confirm";
431 GError *error = NULL;
434 ret = secret_service_delete_item_dbus_path_sync (test->service, path_item_one, NULL, &error);
435 g_assert_no_error (error);
436 g_assert (ret == TRUE);
440 test_lock_paths_sync (Test *test,
443 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
444 const gchar *paths[] = {
449 GError *error = NULL;
450 gchar **locked = NULL;
453 ret = secret_service_lock_dbus_paths_sync (test->service, paths, NULL, &locked, &error);
454 g_assert_no_error (error);
455 g_assert (ret == TRUE);
457 g_assert (locked != NULL);
458 g_assert_cmpstr (locked[0], ==, collection_path);
459 g_assert (locked[1] == NULL);
464 test_lock_prompt_sync (Test *test,
467 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockprompt";
468 const gchar *paths[] = {
473 GError *error = NULL;
474 gchar **locked = NULL;
477 ret = secret_service_lock_dbus_paths_sync (test->service, paths, NULL, &locked, &error);
478 g_assert_no_error (error);
479 g_assert (ret == TRUE);
481 g_assert (locked != NULL);
482 g_assert_cmpstr (locked[0], ==, collection_path);
483 g_assert (locked[1] == NULL);
488 test_unlock_paths_sync (Test *test,
491 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
492 const gchar *paths[] = {
497 GError *error = NULL;
498 gchar **unlocked = NULL;
501 ret = secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, &unlocked, &error);
502 g_assert_no_error (error);
503 g_assert (ret == TRUE);
505 g_assert (unlocked != NULL);
506 g_assert_cmpstr (unlocked[0], ==, collection_path);
507 g_assert (unlocked[1] == NULL);
508 g_strfreev (unlocked);
512 test_unlock_prompt_sync (Test *test,
515 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockprompt";
516 const gchar *paths[] = {
521 GError *error = NULL;
522 gchar **unlocked = NULL;
525 ret = secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, &unlocked, &error);
526 g_assert_no_error (error);
527 g_assert (ret == TRUE);
529 g_assert (unlocked != NULL);
530 g_assert_cmpstr (unlocked[0], ==, collection_path);
531 g_assert (unlocked[1] == NULL);
532 g_strfreev (unlocked);
536 test_collection_sync (Test *test,
539 GHashTable *properties;
540 GError *error = NULL;
543 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
544 (GDestroyNotify)g_variant_unref);
545 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
546 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
548 path = secret_service_create_collection_dbus_path_sync (test->service, properties,
549 NULL, SECRET_COLLECTION_CREATE_NONE,
552 g_hash_table_unref (properties);
554 g_assert_no_error (error);
555 g_assert (path != NULL);
556 g_assert (g_str_has_prefix (path, "/org/freedesktop/secrets/collection/"));
562 test_collection_async (Test *test,
565 GAsyncResult *result = NULL;
566 GHashTable *properties;
567 GError *error = NULL;
570 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
571 (GDestroyNotify)g_variant_unref);
572 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
573 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
575 secret_service_create_collection_dbus_path (test->service, properties,
576 NULL, SECRET_COLLECTION_CREATE_NONE,
577 NULL, on_complete_get_result, &result);
579 g_hash_table_unref (properties);
580 g_assert (result == NULL);
584 path = secret_service_create_collection_dbus_path_finish (test->service, result, &error);
585 g_object_unref (result);
587 g_assert_no_error (error);
588 g_assert (path != NULL);
589 g_assert (g_str_has_prefix (path, "/org/freedesktop/secrets/collection/"));
595 test_item_sync (Test *test,
598 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
599 GHashTable *properties;
600 GHashTable *attributes;
602 GError *error = NULL;
605 attributes = g_hash_table_new (g_str_hash, g_str_equal);
606 g_hash_table_insert (attributes, "even", "true");
607 g_hash_table_insert (attributes, "string", "ten");
608 g_hash_table_insert (attributes, "number", "10");
610 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
611 (GDestroyNotify)g_variant_unref);
612 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
613 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
614 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Attributes",
615 g_variant_ref_sink (_secret_attributes_to_variant (attributes, "org.gnome.Test")));
617 g_hash_table_unref (attributes);
619 value = secret_value_new ("andmoreandmore", -1, "text/plain");
621 path = secret_service_create_item_dbus_path_sync (test->service, collection_path,
622 properties, value, SECRET_ITEM_CREATE_NONE,
625 secret_value_unref (value);
626 g_hash_table_unref (properties);
628 g_assert_no_error (error);
629 g_assert (path != NULL);
630 g_assert (g_str_has_prefix (path, collection_path));
636 test_item_async (Test *test,
639 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
640 GHashTable *properties;
641 GHashTable *attributes;
643 GError *error = NULL;
644 GAsyncResult *result = NULL;
647 attributes = g_hash_table_new (g_str_hash, g_str_equal);
648 g_hash_table_insert (attributes, "even", "true");
649 g_hash_table_insert (attributes, "string", "ten");
650 g_hash_table_insert (attributes, "number", "10");
652 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
653 (GDestroyNotify)g_variant_unref);
654 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
655 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
656 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Attributes",
657 g_variant_ref_sink (_secret_attributes_to_variant (attributes, NULL)));
659 g_hash_table_unref (attributes);
661 value = secret_value_new ("andmoreandmore", -1, "text/plain");
663 secret_service_create_item_dbus_path (test->service, collection_path,
664 properties, value, SECRET_ITEM_CREATE_NONE,
665 NULL, on_complete_get_result, &result);
667 g_assert (result == NULL);
668 secret_value_unref (value);
669 g_hash_table_unref (properties);
673 path = secret_service_create_item_dbus_path_finish (test->service, result, &error);
674 g_object_unref (result);
676 g_assert_no_error (error);
677 g_assert (path != NULL);
678 g_assert (g_str_has_prefix (path, collection_path));
684 test_set_alias_path (Test *test,
688 GError *error = NULL;
691 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
692 g_assert_no_error (error);
693 g_assert (path == NULL);
695 ret = secret_service_set_alias_to_dbus_path_sync (test->service, "blah", "/org/freedesktop/secrets/collection/english", NULL, &error);
696 g_assert_no_error (error);
697 g_assert (ret == TRUE);
699 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
700 g_assert_no_error (error);
701 g_assert_cmpstr (path, ==, "/org/freedesktop/secrets/collection/english");
704 ret = secret_service_set_alias_to_dbus_path_sync (test->service, "blah", NULL, NULL, &error);
705 g_assert_no_error (error);
706 g_assert (ret == TRUE);
708 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
709 g_assert_no_error (error);
710 g_assert (path == NULL);
714 test_encode_decode_secret (Test *test,
715 gconstpointer unused)
719 SecretValue *decoded;
720 GError *error = NULL;
722 value = secret_value_new ("zerogjuggs", -1, "text/plain");
724 secret_service_ensure_session_sync (test->service, NULL, &error);
725 g_assert_no_error (error);
727 variant = secret_service_encode_dbus_secret (test->service, value);
728 g_assert (variant != NULL);
729 g_assert_cmpstr (g_variant_get_type_string (variant), ==, "(oayays)");
730 secret_value_unref (value);
732 decoded = secret_service_decode_dbus_secret (test->service, variant);
733 g_assert (variant != NULL);
734 g_variant_unref (variant);
736 g_assert_cmpstr (secret_value_get_text (decoded), ==, "zerogjuggs");
737 secret_value_unref (decoded);
741 main (int argc, char **argv)
743 g_test_init (&argc, &argv, NULL);
744 g_set_prgname ("test-service");
745 #if !GLIB_CHECK_VERSION(2,35,0)
749 g_test_add ("/service/search-for-paths", Test, "mock-service-normal.py", setup, test_search_paths_sync, teardown);
750 g_test_add ("/service/search-for-paths-async", Test, "mock-service-normal.py", setup, test_search_paths_async, teardown);
751 g_test_add ("/service/search-for-paths-nulls", Test, "mock-service-normal.py", setup, test_search_paths_nulls, teardown);
753 g_test_add ("/service/secret-for-path-sync", Test, "mock-service-normal.py", setup, test_secret_for_path_sync, teardown);
754 g_test_add ("/service/secret-for-path-plain", Test, "mock-service-only-plain.py", setup, test_secret_for_path_sync, teardown);
755 g_test_add ("/service/secret-for-path-async", Test, "mock-service-normal.py", setup, test_secret_for_path_async, teardown);
756 g_test_add ("/service/secrets-for-paths-sync", Test, "mock-service-normal.py", setup, test_secrets_for_paths_sync, teardown);
757 g_test_add ("/service/secrets-for-paths-async", Test, "mock-service-normal.py", setup, test_secrets_for_paths_async, teardown);
759 g_test_add ("/service/delete-for-path", Test, "mock-service-delete.py", setup, test_delete_for_path_sync, teardown);
760 g_test_add ("/service/delete-for-path-with-prompt", Test, "mock-service-delete.py", setup, test_delete_for_path_sync_prompt, teardown);
762 g_test_add ("/service/lock-paths-sync", Test, "mock-service-lock.py", setup, test_lock_paths_sync, teardown);
763 g_test_add ("/service/lock-prompt-sync", Test, "mock-service-lock.py", setup, test_lock_prompt_sync, teardown);
765 g_test_add ("/service/unlock-paths-sync", Test, "mock-service-lock.py", setup, test_unlock_paths_sync, teardown);
766 g_test_add ("/service/unlock-prompt-sync", Test, "mock-service-lock.py", setup, test_unlock_prompt_sync, teardown);
768 g_test_add ("/service/create-collection-sync", Test, "mock-service-normal.py", setup, test_collection_sync, teardown);
769 g_test_add ("/service/create-collection-async", Test, "mock-service-normal.py", setup, test_collection_async, teardown);
771 g_test_add ("/service/create-item-sync", Test, "mock-service-normal.py", setup, test_item_sync, teardown);
772 g_test_add ("/service/create-item-async", Test, "mock-service-normal.py", setup, test_item_async, teardown);
774 g_test_add ("/service/set-alias-path", Test, "mock-service-normal.py", setup, test_set_alias_path, teardown);
776 g_test_add ("/service/encode-decode-secret", Test, "mock-service-normal.py", setup, test_encode_decode_secret, teardown);
778 return egg_tests_run_with_loop ();