1 /* libsecret - GLib wrapper for Secret Service
3 * Copyright 2012 Red Hat Inc.
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.
12 * Author: Stef Walter <stefw@gnome.org>
17 #include "secret-password.h"
18 #include "secret-paths.h"
19 #include "secret-private.h"
21 #include "mock-service.h"
23 #include "egg/egg-testing.h"
30 static const SecretSchema MOCK_SCHEMA = {
34 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
35 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
36 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
40 static const SecretSchema PRIME_SCHEMA = {
44 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
45 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
46 { "prime", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
50 static const SecretSchema NO_NAME_SCHEMA = {
52 SECRET_SCHEMA_DONT_MATCH_NAME,
54 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
55 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
68 const gchar *mock_script = data;
70 mock_service_start (mock_script, &error);
71 g_assert_no_error (error);
78 secret_service_disconnect ();
83 on_complete_get_result (GObject *source,
87 GAsyncResult **ret = user_data;
88 g_assert (ret != NULL);
89 g_assert (*ret == NULL);
90 *ret = g_object_ref (result);
91 egg_test_wait_stop ();
95 test_lookup_sync (Test *test,
101 password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
107 g_assert_no_error (error);
108 g_assert_cmpstr (password, ==, "111");
110 secret_password_free (password);
114 test_lookup_async (Test *test,
117 GAsyncResult *result = NULL;
118 GError *error = NULL;
121 secret_password_lookup (&MOCK_SCHEMA, NULL, on_complete_get_result, &result,
126 g_assert (result == NULL);
130 password = secret_password_lookup_nonpageable_finish (result, &error);
131 g_assert_no_error (error);
132 g_object_unref (result);
134 g_assert_cmpstr (password, ==, "111");
135 secret_password_free (password);
139 test_lookup_no_name (Test *test,
142 GError *error = NULL;
145 /* should return null, because nothing with mock schema and 5 */
146 password = secret_password_lookup_sync (&MOCK_SCHEMA, NULL, &error,
149 g_assert_no_error (error);
150 g_assert (password == NULL);
152 /* should return an item, because we have a prime schema with 5, and flags not to match name */
153 password = secret_password_lookup_sync (&NO_NAME_SCHEMA, NULL, &error,
157 g_assert_no_error (error);
158 g_assert_cmpstr (password, ==, "555");
160 secret_password_free (password);
164 test_store_sync (Test *test,
167 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
168 GError *error = NULL;
172 ret = secret_password_store_sync (&MOCK_SCHEMA, collection_path,
173 "Label here", "the password", NULL, &error,
179 g_assert_no_error (error);
180 g_assert (ret == TRUE);
182 password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
186 g_assert_no_error (error);
187 g_assert_cmpstr (password, ==, "the password");
189 secret_password_free (password);
193 test_store_async (Test *test,
196 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
197 GAsyncResult *result = NULL;
198 GError *error = NULL;
202 secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
203 "the password", NULL, on_complete_get_result, &result,
208 g_assert (result == NULL);
212 ret = secret_password_store_finish (result, &error);
213 g_assert_no_error (error);
214 g_assert (ret == TRUE);
215 g_object_unref (result);
217 password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
221 g_assert_no_error (error);
222 g_assert_cmpstr (password, ==, "the password");
224 secret_password_free (password);
228 test_store_unlock (Test *test,
229 gconstpointer unused)
231 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
232 GAsyncResult *result = NULL;
233 SecretCollection *collection;
234 SecretService *service;
235 GError *error = NULL;
241 service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
242 g_assert_no_error (error);
244 /* Check collection state */
245 collection = secret_collection_new_for_dbus_path_sync (service, collection_path,
246 SECRET_COLLECTION_NONE, NULL, &error);
247 g_assert_no_error (error);
248 g_assert (secret_collection_get_locked (collection) == FALSE);
250 /* Lock it, use async, so collection properties update */
251 objects = g_list_append (NULL, collection);
252 secret_service_lock (service, objects, NULL, on_complete_get_result, &result);
254 count = secret_service_lock_finish (service, result, NULL, &error);
255 g_assert_cmpint (count, ==, 1);
256 g_clear_object (&result);
257 g_list_free (objects);
259 /* Check collection state */
260 g_assert (secret_collection_get_locked (collection) == TRUE);
262 /* Store the password, use async so collection properties update */
263 secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
264 "the password", NULL, on_complete_get_result, &result,
269 g_assert (result == NULL);
271 ret = secret_password_store_finish (result, &error);
272 g_assert_no_error (error);
273 g_assert (ret == TRUE);
274 g_clear_object (&result);
276 /* Check collection state */
277 g_assert (secret_collection_get_locked (collection) == FALSE);
280 password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
284 g_assert_no_error (error);
285 g_assert_cmpstr (password, ==, "the password");
287 secret_password_free (password);
288 g_object_unref (collection);
289 g_object_unref (service);
293 test_delete_sync (Test *test,
296 GError *error = NULL;
299 ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
305 g_assert_no_error (error);
306 g_assert (ret == TRUE);
310 test_delete_async (Test *test,
313 GError *error = NULL;
314 GAsyncResult *result = NULL;
317 secret_password_clear (&MOCK_SCHEMA, NULL,
318 on_complete_get_result, &result,
324 g_assert (result == NULL);
328 ret = secret_password_clear_finish (result, &error);
329 g_assert_no_error (error);
330 g_assert (ret == TRUE);
332 g_object_unref (result);
336 test_clear_no_name (Test *test,
339 const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
340 SecretService *service;
341 GError *error = NULL;
344 /* Shouldn't match anything, because no item with 5 in mock schema */
345 ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
348 g_assert_no_error (error);
349 g_assert (ret == FALSE);
351 /* We need this collection unlocked for the next test */
352 service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
353 g_assert_no_error (error);
354 secret_service_unlock_dbus_paths_sync (service, paths, NULL, NULL, &error);
355 g_assert_no_error (error);
356 g_object_unref (service);
358 /* We have an item with 5 in prime schema, but should match anyway becase of flags */
359 ret = secret_password_clear_sync (&NO_NAME_SCHEMA, NULL, &error,
363 g_assert_no_error (error);
364 g_assert (ret == TRUE);
368 test_password_free_null (void)
370 secret_password_free (NULL);
374 main (int argc, char **argv)
376 g_test_init (&argc, &argv, NULL);
377 g_set_prgname ("test-password");
378 #if !GLIB_CHECK_VERSION(2,35,0)
382 g_test_add ("/password/lookup-sync", Test, "mock-service-normal.py", setup, test_lookup_sync, teardown);
383 g_test_add ("/password/lookup-async", Test, "mock-service-normal.py", setup, test_lookup_async, teardown);
384 g_test_add ("/password/lookup-no-name", Test, "mock-service-normal.py", setup, test_lookup_no_name, teardown);
386 g_test_add ("/password/store-sync", Test, "mock-service-normal.py", setup, test_store_sync, teardown);
387 g_test_add ("/password/store-async", Test, "mock-service-normal.py", setup, test_store_async, teardown);
388 g_test_add ("/password/store-unlock", Test, "mock-service-normal.py", setup, test_store_unlock, teardown);
390 g_test_add ("/password/delete-sync", Test, "mock-service-delete.py", setup, test_delete_sync, teardown);
391 g_test_add ("/password/delete-async", Test, "mock-service-delete.py", setup, test_delete_async, teardown);
392 g_test_add ("/password/clear-no-name", Test, "mock-service-delete.py", setup, test_clear_no_name, teardown);
394 g_test_add_func ("/password/free-null", test_password_free_null);
396 return egg_tests_run_with_loop ();