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);
89 teardown_mock (Test *test,
92 secret_service_disconnect ();
100 egg_test_wait_idle ();
102 g_object_unref (test->service);
103 secret_service_disconnect ();
104 egg_assert_not_object (test->service);
106 teardown_mock (test, unused);
110 on_complete_get_result (GObject *source,
111 GAsyncResult *result,
114 GAsyncResult **ret = user_data;
115 g_assert (ret != NULL);
116 g_assert (*ret == NULL);
117 *ret = g_object_ref (result);
118 egg_test_wait_stop ();
122 test_search_paths_sync (Test *test,
125 GHashTable *attributes;
129 GError *error = NULL;
131 attributes = g_hash_table_new (g_str_hash, g_str_equal);
132 g_hash_table_insert (attributes, "number", "1");
134 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
135 &unlocked, &locked, &error);
136 g_assert_no_error (error);
137 g_assert (ret == TRUE);
140 g_assert_cmpstr (locked[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
143 g_assert_cmpstr (unlocked[0], ==, "/org/freedesktop/secrets/collection/english/1");
145 g_strfreev (unlocked);
148 g_hash_table_unref (attributes);
152 test_search_paths_async (Test *test,
155 GAsyncResult *result = NULL;
156 GHashTable *attributes;
160 GError *error = NULL;
162 attributes = g_hash_table_new (g_str_hash, g_str_equal);
163 g_hash_table_insert (attributes, "number", "1");
165 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
166 on_complete_get_result, &result);
169 g_assert (G_IS_ASYNC_RESULT (result));
170 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
173 g_assert_no_error (error);
174 g_assert (ret == TRUE);
177 g_assert_cmpstr (locked[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
180 g_assert_cmpstr (unlocked[0], ==, "/org/freedesktop/secrets/collection/english/1");
182 g_strfreev (unlocked);
184 g_object_unref (result);
186 g_hash_table_unref (attributes);
190 test_search_paths_nulls (Test *test,
193 GAsyncResult *result = NULL;
194 GHashTable *attributes;
197 GError *error = NULL;
199 attributes = g_hash_table_new (g_str_hash, g_str_equal);
200 g_hash_table_insert (attributes, "number", "1");
202 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
203 &paths, NULL, &error);
204 g_assert_no_error (error);
205 g_assert (ret == TRUE);
206 g_assert (paths != NULL);
207 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/english/1");
210 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
211 NULL, &paths, &error);
212 g_assert_no_error (error);
213 g_assert (ret == TRUE);
214 g_assert (paths != NULL);
215 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
218 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
220 g_assert_no_error (error);
221 g_assert (ret == TRUE);
223 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
224 on_complete_get_result, &result);
226 g_assert (G_IS_ASYNC_RESULT (result));
227 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
228 &paths, NULL, &error);
229 g_assert_no_error (error);
230 g_assert (ret == TRUE);
231 g_assert (paths != NULL);
232 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/english/1");
234 g_clear_object (&result);
236 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
237 on_complete_get_result, &result);
239 g_assert (G_IS_ASYNC_RESULT (result));
240 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
241 NULL, &paths, &error);
242 g_assert_no_error (error);
243 g_assert (ret == TRUE);
244 g_assert (paths != NULL);
245 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
247 g_clear_object (&result);
249 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
250 on_complete_get_result, &result);
252 g_assert (G_IS_ASYNC_RESULT (result));
253 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
255 g_assert_no_error (error);
256 g_assert (ret == TRUE);
257 g_clear_object (&result);
259 g_hash_table_unref (attributes);
263 test_secret_for_path_sync (Test *test,
267 GError *error = NULL;
269 const gchar *password;
272 path = "/org/freedesktop/secrets/collection/english/1";
273 value = secret_service_get_secret_for_dbus_path_sync (test->service, path, NULL, &error);
274 g_assert_no_error (error);
275 g_assert (value != NULL);
277 password = secret_value_get (value, &length);
278 g_assert_cmpuint (length, ==, 3);
279 g_assert_cmpstr (password, ==, "111");
281 password = secret_value_get (value, NULL);
282 g_assert_cmpstr (password, ==, "111");
284 secret_value_unref (value);
288 test_secret_for_path_async (Test *test,
292 GError *error = NULL;
294 const gchar *password;
295 GAsyncResult *result = NULL;
298 path = "/org/freedesktop/secrets/collection/english/1";
299 secret_service_get_secret_for_dbus_path (test->service, path, NULL,
300 on_complete_get_result, &result);
301 g_assert (result == NULL);
304 value = secret_service_get_secret_for_dbus_path_finish (test->service, result, &error);
305 g_assert_no_error (error);
306 g_assert (value != NULL);
307 g_object_unref (result);
309 password = secret_value_get (value, &length);
310 g_assert_cmpuint (length, ==, 3);
311 g_assert_cmpstr (password, ==, "111");
313 password = secret_value_get (value, NULL);
314 g_assert_cmpstr (password, ==, "111");
316 secret_value_unref (value);
320 test_secrets_for_paths_sync (Test *test,
323 const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
324 const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
325 const gchar *paths[] = {
329 /* This one is locked, and not returned */
330 "/org/freedesktop/secrets/collection/spanish/10",
336 GError *error = NULL;
337 const gchar *password;
340 values = secret_service_get_secrets_for_dbus_paths_sync (test->service, paths, NULL, &error);
341 g_assert_no_error (error);
343 g_assert (values != NULL);
344 g_assert_cmpuint (g_hash_table_size (values), ==, 2);
346 value = g_hash_table_lookup (values, path_item_one);
347 g_assert (value != NULL);
348 password = secret_value_get (value, &length);
349 g_assert_cmpuint (length, ==, 3);
350 g_assert_cmpstr (password, ==, "111");
352 value = g_hash_table_lookup (values, path_item_two);
353 g_assert (value != NULL);
354 password = secret_value_get (value, &length);
355 g_assert_cmpuint (length, ==, 3);
356 g_assert_cmpstr (password, ==, "222");
358 g_hash_table_unref (values);
362 test_secrets_for_paths_async (Test *test,
365 const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
366 const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
367 const gchar *paths[] = {
371 /* This one is locked, and not returned */
372 "/org/freedesktop/secrets/collection/spanish/10",
378 GError *error = NULL;
379 const gchar *password;
380 GAsyncResult *result = NULL;
383 secret_service_get_secrets_for_dbus_paths (test->service, paths, NULL,
384 on_complete_get_result, &result);
385 g_assert (result == NULL);
388 values = secret_service_get_secrets_for_dbus_paths_finish (test->service, result, &error);
389 g_assert_no_error (error);
390 g_object_unref (result);
392 g_assert (values != NULL);
393 g_assert_cmpuint (g_hash_table_size (values), ==, 2);
395 value = g_hash_table_lookup (values, path_item_one);
396 g_assert (value != NULL);
397 password = secret_value_get (value, &length);
398 g_assert_cmpuint (length, ==, 3);
399 g_assert_cmpstr (password, ==, "111");
401 value = g_hash_table_lookup (values, path_item_two);
402 g_assert (value != NULL);
403 password = secret_value_get (value, &length);
404 g_assert_cmpuint (length, ==, 3);
405 g_assert_cmpstr (password, ==, "222");
407 g_hash_table_unref (values);
411 test_delete_for_path_sync (Test *test,
415 const gchar *path_item_one = "/org/freedesktop/secrets/collection/todelete/item";
416 GError *error = NULL;
419 ret = secret_service_delete_item_dbus_path_sync (test->service, path_item_one, NULL, &error);
420 g_assert_no_error (error);
421 g_assert (ret == TRUE);
425 test_delete_for_path_sync_prompt (Test *test,
429 const gchar *path_item_one = "/org/freedesktop/secrets/collection/todelete/confirm";
430 GError *error = NULL;
433 ret = secret_service_delete_item_dbus_path_sync (test->service, path_item_one, NULL, &error);
434 g_assert_no_error (error);
435 g_assert (ret == TRUE);
439 test_lock_paths_sync (Test *test,
442 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
443 const gchar *paths[] = {
448 GError *error = NULL;
449 gchar **locked = NULL;
452 ret = secret_service_lock_dbus_paths_sync (test->service, paths, NULL, &locked, &error);
453 g_assert_no_error (error);
454 g_assert (ret == TRUE);
456 g_assert (locked != NULL);
457 g_assert_cmpstr (locked[0], ==, collection_path);
458 g_assert (locked[1] == NULL);
463 test_lock_prompt_sync (Test *test,
466 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockprompt";
467 const gchar *paths[] = {
472 GError *error = NULL;
473 gchar **locked = NULL;
476 ret = secret_service_lock_dbus_paths_sync (test->service, paths, NULL, &locked, &error);
477 g_assert_no_error (error);
478 g_assert (ret == TRUE);
480 g_assert (locked != NULL);
481 g_assert_cmpstr (locked[0], ==, collection_path);
482 g_assert (locked[1] == NULL);
487 test_unlock_paths_sync (Test *test,
490 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
491 const gchar *paths[] = {
496 GError *error = NULL;
497 gchar **unlocked = NULL;
500 ret = secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, &unlocked, &error);
501 g_assert_no_error (error);
502 g_assert (ret == TRUE);
504 g_assert (unlocked != NULL);
505 g_assert_cmpstr (unlocked[0], ==, collection_path);
506 g_assert (unlocked[1] == NULL);
507 g_strfreev (unlocked);
511 test_unlock_prompt_sync (Test *test,
514 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockprompt";
515 const gchar *paths[] = {
520 GError *error = NULL;
521 gchar **unlocked = NULL;
524 ret = secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, &unlocked, &error);
525 g_assert_no_error (error);
526 g_assert (ret == TRUE);
528 g_assert (unlocked != NULL);
529 g_assert_cmpstr (unlocked[0], ==, collection_path);
530 g_assert (unlocked[1] == NULL);
531 g_strfreev (unlocked);
535 test_collection_sync (Test *test,
538 GHashTable *properties;
539 GError *error = NULL;
542 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
543 (GDestroyNotify)g_variant_unref);
544 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
545 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
547 path = secret_service_create_collection_dbus_path_sync (test->service, properties,
548 NULL, SECRET_COLLECTION_CREATE_NONE,
551 g_hash_table_unref (properties);
553 g_assert_no_error (error);
554 g_assert (path != NULL);
555 g_assert (g_str_has_prefix (path, "/org/freedesktop/secrets/collection/"));
561 test_collection_async (Test *test,
564 GAsyncResult *result = NULL;
565 GHashTable *properties;
566 GError *error = NULL;
569 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
570 (GDestroyNotify)g_variant_unref);
571 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
572 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
574 secret_service_create_collection_dbus_path (test->service, properties,
575 NULL, SECRET_COLLECTION_CREATE_NONE,
576 NULL, on_complete_get_result, &result);
578 g_hash_table_unref (properties);
579 g_assert (result == NULL);
583 path = secret_service_create_collection_dbus_path_finish (test->service, result, &error);
584 g_object_unref (result);
586 g_assert_no_error (error);
587 g_assert (path != NULL);
588 g_assert (g_str_has_prefix (path, "/org/freedesktop/secrets/collection/"));
594 test_item_sync (Test *test,
597 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
598 GHashTable *properties;
599 GHashTable *attributes;
601 GError *error = NULL;
604 attributes = g_hash_table_new (g_str_hash, g_str_equal);
605 g_hash_table_insert (attributes, "even", "true");
606 g_hash_table_insert (attributes, "string", "ten");
607 g_hash_table_insert (attributes, "number", "10");
609 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
610 (GDestroyNotify)g_variant_unref);
611 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
612 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
613 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Attributes",
614 g_variant_ref_sink (_secret_attributes_to_variant (attributes, "org.gnome.Test")));
616 g_hash_table_unref (attributes);
618 value = secret_value_new ("andmoreandmore", -1, "text/plain");
620 path = secret_service_create_item_dbus_path_sync (test->service, collection_path,
621 properties, value, SECRET_ITEM_CREATE_NONE,
624 secret_value_unref (value);
625 g_hash_table_unref (properties);
627 g_assert_no_error (error);
628 g_assert (path != NULL);
629 g_assert (g_str_has_prefix (path, collection_path));
635 test_item_async (Test *test,
638 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
639 GHashTable *properties;
640 GHashTable *attributes;
642 GError *error = NULL;
643 GAsyncResult *result = NULL;
646 attributes = g_hash_table_new (g_str_hash, g_str_equal);
647 g_hash_table_insert (attributes, "even", "true");
648 g_hash_table_insert (attributes, "string", "ten");
649 g_hash_table_insert (attributes, "number", "10");
651 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
652 (GDestroyNotify)g_variant_unref);
653 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
654 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
655 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Attributes",
656 g_variant_ref_sink (_secret_attributes_to_variant (attributes, NULL)));
658 g_hash_table_unref (attributes);
660 value = secret_value_new ("andmoreandmore", -1, "text/plain");
662 secret_service_create_item_dbus_path (test->service, collection_path,
663 properties, value, SECRET_ITEM_CREATE_NONE,
664 NULL, on_complete_get_result, &result);
666 g_assert (result == NULL);
667 secret_value_unref (value);
668 g_hash_table_unref (properties);
672 path = secret_service_create_item_dbus_path_finish (test->service, result, &error);
673 g_object_unref (result);
675 g_assert_no_error (error);
676 g_assert (path != NULL);
677 g_assert (g_str_has_prefix (path, collection_path));
683 test_set_alias_path (Test *test,
687 GError *error = NULL;
690 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
691 g_assert_no_error (error);
692 g_assert (path == NULL);
694 ret = secret_service_set_alias_to_dbus_path_sync (test->service, "blah", "/org/freedesktop/secrets/collection/english", NULL, &error);
695 g_assert_no_error (error);
696 g_assert (ret == TRUE);
698 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
699 g_assert_no_error (error);
700 g_assert_cmpstr (path, ==, "/org/freedesktop/secrets/collection/english");
703 ret = secret_service_set_alias_to_dbus_path_sync (test->service, "blah", NULL, NULL, &error);
704 g_assert_no_error (error);
705 g_assert (ret == TRUE);
707 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
708 g_assert_no_error (error);
709 g_assert (path == NULL);
713 main (int argc, char **argv)
715 g_test_init (&argc, &argv, NULL);
716 g_set_prgname ("test-service");
717 #if !GLIB_CHECK_VERSION(2,35,0)
721 g_test_add ("/service/search-for-paths", Test, "mock-service-normal.py", setup, test_search_paths_sync, teardown);
722 g_test_add ("/service/search-for-paths-async", Test, "mock-service-normal.py", setup, test_search_paths_async, teardown);
723 g_test_add ("/service/search-for-paths-nulls", Test, "mock-service-normal.py", setup, test_search_paths_nulls, teardown);
725 g_test_add ("/service/secret-for-path-sync", Test, "mock-service-normal.py", setup, test_secret_for_path_sync, teardown);
726 g_test_add ("/service/secret-for-path-plain", Test, "mock-service-only-plain.py", setup, test_secret_for_path_sync, teardown);
727 g_test_add ("/service/secret-for-path-async", Test, "mock-service-normal.py", setup, test_secret_for_path_async, teardown);
728 g_test_add ("/service/secrets-for-paths-sync", Test, "mock-service-normal.py", setup, test_secrets_for_paths_sync, teardown);
729 g_test_add ("/service/secrets-for-paths-async", Test, "mock-service-normal.py", setup, test_secrets_for_paths_async, teardown);
731 g_test_add ("/service/delete-for-path", Test, "mock-service-delete.py", setup, test_delete_for_path_sync, teardown);
732 g_test_add ("/service/delete-for-path-with-prompt", Test, "mock-service-delete.py", setup, test_delete_for_path_sync_prompt, teardown);
734 g_test_add ("/service/lock-paths-sync", Test, "mock-service-lock.py", setup, test_lock_paths_sync, teardown);
735 g_test_add ("/service/lock-prompt-sync", Test, "mock-service-lock.py", setup, test_lock_prompt_sync, teardown);
737 g_test_add ("/service/unlock-paths-sync", Test, "mock-service-lock.py", setup, test_unlock_paths_sync, teardown);
738 g_test_add ("/service/unlock-prompt-sync", Test, "mock-service-lock.py", setup, test_unlock_prompt_sync, teardown);
740 g_test_add ("/service/create-collection-sync", Test, "mock-service-normal.py", setup, test_collection_sync, teardown);
741 g_test_add ("/service/create-collection-async", Test, "mock-service-normal.py", setup, test_collection_async, teardown);
743 g_test_add ("/service/create-item-sync", Test, "mock-service-normal.py", setup, test_item_sync, teardown);
744 g_test_add ("/service/create-item-async", Test, "mock-service-normal.py", setup, test_item_async, teardown);
746 g_test_add ("/service/set-alias-path", Test, "mock-service-normal.py", setup, test_set_alias_path, teardown);
748 return egg_tests_run_with_loop ();