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,
99 egg_test_wait_idle ();
101 g_object_unref (test->service);
102 secret_service_disconnect ();
103 egg_assert_not_object (test->service);
105 teardown_mock (test, unused);
109 on_complete_get_result (GObject *source,
110 GAsyncResult *result,
113 GAsyncResult **ret = user_data;
114 g_assert (ret != NULL);
115 g_assert (*ret == NULL);
116 *ret = g_object_ref (result);
117 egg_test_wait_stop ();
121 test_search_paths_sync (Test *test,
124 GHashTable *attributes;
128 GError *error = NULL;
130 attributes = g_hash_table_new (g_str_hash, g_str_equal);
131 g_hash_table_insert (attributes, "number", "1");
133 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
134 &unlocked, &locked, &error);
135 g_assert_no_error (error);
136 g_assert (ret == TRUE);
139 g_assert_cmpstr (locked[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
142 g_assert_cmpstr (unlocked[0], ==, "/org/freedesktop/secrets/collection/english/1");
144 g_strfreev (unlocked);
147 g_hash_table_unref (attributes);
151 test_search_paths_async (Test *test,
154 GAsyncResult *result = NULL;
155 GHashTable *attributes;
159 GError *error = NULL;
161 attributes = g_hash_table_new (g_str_hash, g_str_equal);
162 g_hash_table_insert (attributes, "number", "1");
164 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
165 on_complete_get_result, &result);
168 g_assert (G_IS_ASYNC_RESULT (result));
169 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
172 g_assert_no_error (error);
173 g_assert (ret == TRUE);
176 g_assert_cmpstr (locked[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
179 g_assert_cmpstr (unlocked[0], ==, "/org/freedesktop/secrets/collection/english/1");
181 g_strfreev (unlocked);
183 g_object_unref (result);
185 g_hash_table_unref (attributes);
189 test_search_paths_nulls (Test *test,
192 GAsyncResult *result = NULL;
193 GHashTable *attributes;
196 GError *error = NULL;
198 attributes = g_hash_table_new (g_str_hash, g_str_equal);
199 g_hash_table_insert (attributes, "number", "1");
201 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
202 &paths, NULL, &error);
203 g_assert_no_error (error);
204 g_assert (ret == TRUE);
205 g_assert (paths != NULL);
206 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/english/1");
209 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
210 NULL, &paths, &error);
211 g_assert_no_error (error);
212 g_assert (ret == TRUE);
213 g_assert (paths != NULL);
214 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
217 ret = secret_service_search_for_dbus_paths_sync (test->service, &MOCK_SCHEMA, attributes, NULL,
219 g_assert_no_error (error);
220 g_assert (ret == TRUE);
222 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
223 on_complete_get_result, &result);
225 g_assert (G_IS_ASYNC_RESULT (result));
226 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
227 &paths, NULL, &error);
228 g_assert_no_error (error);
229 g_assert (ret == TRUE);
230 g_assert (paths != NULL);
231 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/english/1");
233 g_clear_object (&result);
235 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
236 on_complete_get_result, &result);
238 g_assert (G_IS_ASYNC_RESULT (result));
239 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
240 NULL, &paths, &error);
241 g_assert_no_error (error);
242 g_assert (ret == TRUE);
243 g_assert (paths != NULL);
244 g_assert_cmpstr (paths[0], ==, "/org/freedesktop/secrets/collection/spanish/10");
246 g_clear_object (&result);
248 secret_service_search_for_dbus_paths (test->service, &MOCK_SCHEMA, attributes, NULL,
249 on_complete_get_result, &result);
251 g_assert (G_IS_ASYNC_RESULT (result));
252 ret = secret_service_search_for_dbus_paths_finish (test->service, result,
254 g_assert_no_error (error);
255 g_assert (ret == TRUE);
256 g_clear_object (&result);
258 g_hash_table_unref (attributes);
262 test_secret_for_path_sync (Test *test,
266 GError *error = NULL;
268 const gchar *password;
271 path = "/org/freedesktop/secrets/collection/english/1";
272 value = secret_service_get_secret_for_dbus_path_sync (test->service, path, NULL, &error);
273 g_assert_no_error (error);
274 g_assert (value != NULL);
276 password = secret_value_get (value, &length);
277 g_assert_cmpuint (length, ==, 3);
278 g_assert_cmpstr (password, ==, "111");
280 password = secret_value_get (value, NULL);
281 g_assert_cmpstr (password, ==, "111");
283 secret_value_unref (value);
287 test_secret_for_path_async (Test *test,
291 GError *error = NULL;
293 const gchar *password;
294 GAsyncResult *result = NULL;
297 path = "/org/freedesktop/secrets/collection/english/1";
298 secret_service_get_secret_for_dbus_path (test->service, path, NULL,
299 on_complete_get_result, &result);
300 g_assert (result == NULL);
303 value = secret_service_get_secret_for_dbus_path_finish (test->service, result, &error);
304 g_assert_no_error (error);
305 g_assert (value != NULL);
306 g_object_unref (result);
308 password = secret_value_get (value, &length);
309 g_assert_cmpuint (length, ==, 3);
310 g_assert_cmpstr (password, ==, "111");
312 password = secret_value_get (value, NULL);
313 g_assert_cmpstr (password, ==, "111");
315 secret_value_unref (value);
319 test_secrets_for_paths_sync (Test *test,
322 const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
323 const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
324 const gchar *paths[] = {
328 /* This one is locked, and not returned */
329 "/org/freedesktop/secrets/collection/spanish/10",
335 GError *error = NULL;
336 const gchar *password;
339 values = secret_service_get_secrets_for_dbus_paths_sync (test->service, paths, NULL, &error);
340 g_assert_no_error (error);
342 g_assert (values != NULL);
343 g_assert_cmpuint (g_hash_table_size (values), ==, 2);
345 value = g_hash_table_lookup (values, path_item_one);
346 g_assert (value != NULL);
347 password = secret_value_get (value, &length);
348 g_assert_cmpuint (length, ==, 3);
349 g_assert_cmpstr (password, ==, "111");
351 value = g_hash_table_lookup (values, path_item_two);
352 g_assert (value != NULL);
353 password = secret_value_get (value, &length);
354 g_assert_cmpuint (length, ==, 3);
355 g_assert_cmpstr (password, ==, "222");
357 g_hash_table_unref (values);
361 test_secrets_for_paths_async (Test *test,
364 const gchar *path_item_one = "/org/freedesktop/secrets/collection/english/1";
365 const gchar *path_item_two = "/org/freedesktop/secrets/collection/english/2";
366 const gchar *paths[] = {
370 /* This one is locked, and not returned */
371 "/org/freedesktop/secrets/collection/spanish/10",
377 GError *error = NULL;
378 const gchar *password;
379 GAsyncResult *result = NULL;
382 secret_service_get_secrets_for_dbus_paths (test->service, paths, NULL,
383 on_complete_get_result, &result);
384 g_assert (result == NULL);
387 values = secret_service_get_secrets_for_dbus_paths_finish (test->service, result, &error);
388 g_assert_no_error (error);
389 g_object_unref (result);
391 g_assert (values != NULL);
392 g_assert_cmpuint (g_hash_table_size (values), ==, 2);
394 value = g_hash_table_lookup (values, path_item_one);
395 g_assert (value != NULL);
396 password = secret_value_get (value, &length);
397 g_assert_cmpuint (length, ==, 3);
398 g_assert_cmpstr (password, ==, "111");
400 value = g_hash_table_lookup (values, path_item_two);
401 g_assert (value != NULL);
402 password = secret_value_get (value, &length);
403 g_assert_cmpuint (length, ==, 3);
404 g_assert_cmpstr (password, ==, "222");
406 g_hash_table_unref (values);
410 test_delete_for_path_sync (Test *test,
414 const gchar *path_item_one = "/org/freedesktop/secrets/collection/todelete/item";
415 GError *error = NULL;
418 ret = secret_service_delete_item_dbus_path_sync (test->service, path_item_one, NULL, &error);
419 g_assert_no_error (error);
420 g_assert (ret == TRUE);
424 test_delete_for_path_sync_prompt (Test *test,
428 const gchar *path_item_one = "/org/freedesktop/secrets/collection/todelete/confirm";
429 GError *error = NULL;
432 ret = secret_service_delete_item_dbus_path_sync (test->service, path_item_one, NULL, &error);
433 g_assert_no_error (error);
434 g_assert (ret == TRUE);
438 test_lock_paths_sync (Test *test,
441 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
442 const gchar *paths[] = {
447 GError *error = NULL;
448 gchar **locked = NULL;
451 ret = secret_service_lock_dbus_paths_sync (test->service, paths, NULL, &locked, &error);
452 g_assert_no_error (error);
453 g_assert (ret == TRUE);
455 g_assert (locked != NULL);
456 g_assert_cmpstr (locked[0], ==, collection_path);
457 g_assert (locked[1] == NULL);
462 test_lock_prompt_sync (Test *test,
465 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockprompt";
466 const gchar *paths[] = {
471 GError *error = NULL;
472 gchar **locked = NULL;
475 ret = secret_service_lock_dbus_paths_sync (test->service, paths, NULL, &locked, &error);
476 g_assert_no_error (error);
477 g_assert (ret == TRUE);
479 g_assert (locked != NULL);
480 g_assert_cmpstr (locked[0], ==, collection_path);
481 g_assert (locked[1] == NULL);
486 test_unlock_paths_sync (Test *test,
489 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockone";
490 const gchar *paths[] = {
495 GError *error = NULL;
496 gchar **unlocked = NULL;
499 ret = secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, &unlocked, &error);
500 g_assert_no_error (error);
501 g_assert (ret == TRUE);
503 g_assert (unlocked != NULL);
504 g_assert_cmpstr (unlocked[0], ==, collection_path);
505 g_assert (unlocked[1] == NULL);
506 g_strfreev (unlocked);
510 test_unlock_prompt_sync (Test *test,
513 const gchar *collection_path = "/org/freedesktop/secrets/collection/lockprompt";
514 const gchar *paths[] = {
519 GError *error = NULL;
520 gchar **unlocked = NULL;
523 ret = secret_service_unlock_dbus_paths_sync (test->service, paths, NULL, &unlocked, &error);
524 g_assert_no_error (error);
525 g_assert (ret == TRUE);
527 g_assert (unlocked != NULL);
528 g_assert_cmpstr (unlocked[0], ==, collection_path);
529 g_assert (unlocked[1] == NULL);
530 g_strfreev (unlocked);
534 test_collection_sync (Test *test,
537 GHashTable *properties;
538 GError *error = NULL;
541 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
542 (GDestroyNotify)g_variant_unref);
543 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
544 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
546 path = secret_service_create_collection_dbus_path_sync (test->service, properties,
547 NULL, SECRET_COLLECTION_CREATE_NONE,
550 g_hash_table_unref (properties);
552 g_assert_no_error (error);
553 g_assert (path != NULL);
554 g_assert (g_str_has_prefix (path, "/org/freedesktop/secrets/collection/"));
560 test_collection_async (Test *test,
563 GAsyncResult *result = NULL;
564 GHashTable *properties;
565 GError *error = NULL;
568 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
569 (GDestroyNotify)g_variant_unref);
570 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
571 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
573 secret_service_create_collection_dbus_path (test->service, properties,
574 NULL, SECRET_COLLECTION_CREATE_NONE,
575 NULL, on_complete_get_result, &result);
577 g_hash_table_unref (properties);
578 g_assert (result == NULL);
582 path = secret_service_create_collection_dbus_path_finish (test->service, result, &error);
583 g_object_unref (result);
585 g_assert_no_error (error);
586 g_assert (path != NULL);
587 g_assert (g_str_has_prefix (path, "/org/freedesktop/secrets/collection/"));
593 test_item_sync (Test *test,
596 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
597 GHashTable *properties;
598 GHashTable *attributes;
600 GError *error = NULL;
603 attributes = g_hash_table_new (g_str_hash, g_str_equal);
604 g_hash_table_insert (attributes, "even", "true");
605 g_hash_table_insert (attributes, "string", "ten");
606 g_hash_table_insert (attributes, "number", "10");
608 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
609 (GDestroyNotify)g_variant_unref);
610 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
611 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
612 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Attributes",
613 g_variant_ref_sink (_secret_attributes_to_variant (attributes, "org.gnome.Test")));
615 g_hash_table_unref (attributes);
617 value = secret_value_new ("andmoreandmore", -1, "text/plain");
619 path = secret_service_create_item_dbus_path_sync (test->service, collection_path,
620 properties, value, SECRET_ITEM_CREATE_NONE,
623 secret_value_unref (value);
624 g_hash_table_unref (properties);
626 g_assert_no_error (error);
627 g_assert (path != NULL);
628 g_assert (g_str_has_prefix (path, collection_path));
634 test_item_async (Test *test,
637 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
638 GHashTable *properties;
639 GHashTable *attributes;
641 GError *error = NULL;
642 GAsyncResult *result = NULL;
645 attributes = g_hash_table_new (g_str_hash, g_str_equal);
646 g_hash_table_insert (attributes, "even", "true");
647 g_hash_table_insert (attributes, "string", "ten");
648 g_hash_table_insert (attributes, "number", "10");
650 properties = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
651 (GDestroyNotify)g_variant_unref);
652 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Label",
653 g_variant_ref_sink (g_variant_new_string ("Wheeee")));
654 g_hash_table_insert (properties, SECRET_COLLECTION_INTERFACE ".Attributes",
655 g_variant_ref_sink (_secret_attributes_to_variant (attributes, NULL)));
657 g_hash_table_unref (attributes);
659 value = secret_value_new ("andmoreandmore", -1, "text/plain");
661 secret_service_create_item_dbus_path (test->service, collection_path,
662 properties, value, SECRET_ITEM_CREATE_NONE,
663 NULL, on_complete_get_result, &result);
665 g_assert (result == NULL);
666 secret_value_unref (value);
667 g_hash_table_unref (properties);
671 path = secret_service_create_item_dbus_path_finish (test->service, result, &error);
672 g_object_unref (result);
674 g_assert_no_error (error);
675 g_assert (path != NULL);
676 g_assert (g_str_has_prefix (path, collection_path));
682 test_set_alias_path (Test *test,
686 GError *error = NULL;
689 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
690 g_assert_no_error (error);
691 g_assert (path == NULL);
693 ret = secret_service_set_alias_to_dbus_path_sync (test->service, "blah", "/org/freedesktop/secrets/collection/english", NULL, &error);
694 g_assert_no_error (error);
695 g_assert (ret == TRUE);
697 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
698 g_assert_no_error (error);
699 g_assert_cmpstr (path, ==, "/org/freedesktop/secrets/collection/english");
702 ret = secret_service_set_alias_to_dbus_path_sync (test->service, "blah", NULL, NULL, &error);
703 g_assert_no_error (error);
704 g_assert (ret == TRUE);
706 path = secret_service_read_alias_dbus_path_sync (test->service, "blah", NULL, &error);
707 g_assert_no_error (error);
708 g_assert (path == NULL);
712 main (int argc, char **argv)
714 g_test_init (&argc, &argv, NULL);
715 g_set_prgname ("test-service");
718 g_test_add ("/service/search-for-paths", Test, "mock-service-normal.py", setup, test_search_paths_sync, teardown);
719 g_test_add ("/service/search-for-paths-async", Test, "mock-service-normal.py", setup, test_search_paths_async, teardown);
720 g_test_add ("/service/search-for-paths-nulls", Test, "mock-service-normal.py", setup, test_search_paths_nulls, teardown);
722 g_test_add ("/service/secret-for-path-sync", Test, "mock-service-normal.py", setup, test_secret_for_path_sync, teardown);
723 g_test_add ("/service/secret-for-path-plain", Test, "mock-service-only-plain.py", setup, test_secret_for_path_sync, teardown);
724 g_test_add ("/service/secret-for-path-async", Test, "mock-service-normal.py", setup, test_secret_for_path_async, teardown);
725 g_test_add ("/service/secrets-for-paths-sync", Test, "mock-service-normal.py", setup, test_secrets_for_paths_sync, teardown);
726 g_test_add ("/service/secrets-for-paths-async", Test, "mock-service-normal.py", setup, test_secrets_for_paths_async, teardown);
728 g_test_add ("/service/delete-for-path", Test, "mock-service-delete.py", setup, test_delete_for_path_sync, teardown);
729 g_test_add ("/service/delete-for-path-with-prompt", Test, "mock-service-delete.py", setup, test_delete_for_path_sync_prompt, teardown);
731 g_test_add ("/service/lock-paths-sync", Test, "mock-service-lock.py", setup, test_lock_paths_sync, teardown);
732 g_test_add ("/service/lock-prompt-sync", Test, "mock-service-lock.py", setup, test_lock_prompt_sync, teardown);
734 g_test_add ("/service/unlock-paths-sync", Test, "mock-service-lock.py", setup, test_unlock_paths_sync, teardown);
735 g_test_add ("/service/unlock-prompt-sync", Test, "mock-service-lock.py", setup, test_unlock_prompt_sync, teardown);
737 g_test_add ("/service/create-collection-sync", Test, "mock-service-normal.py", setup, test_collection_sync, teardown);
738 g_test_add ("/service/create-collection-async", Test, "mock-service-normal.py", setup, test_collection_async, teardown);
740 g_test_add ("/service/create-item-sync", Test, "mock-service-normal.py", setup, test_item_sync, teardown);
741 g_test_add ("/service/create-item-async", Test, "mock-service-normal.py", setup, test_item_async, teardown);
743 g_test_add ("/service/set-alias-path", Test, "mock-service-normal.py", setup, test_set_alias_path, teardown);
745 return egg_tests_run_with_loop ();