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);
82 on_complete_get_result (GObject *source,
86 GAsyncResult **ret = user_data;
87 g_assert (ret != NULL);
88 g_assert (*ret == NULL);
89 *ret = g_object_ref (result);
90 egg_test_wait_stop ();
94 test_lookup_sync (Test *test,
100 password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
106 g_assert_no_error (error);
107 g_assert_cmpstr (password, ==, "111");
109 secret_password_free (password);
113 test_lookup_async (Test *test,
116 GAsyncResult *result = NULL;
117 GError *error = NULL;
120 secret_password_lookup (&MOCK_SCHEMA, NULL, on_complete_get_result, &result,
125 g_assert (result == NULL);
129 password = secret_password_lookup_nonpageable_finish (result, &error);
130 g_assert_no_error (error);
131 g_object_unref (result);
133 g_assert_cmpstr (password, ==, "111");
134 secret_password_free (password);
138 test_lookup_no_name (Test *test,
141 GError *error = NULL;
144 /* should return null, because nothing with mock schema and 5 */
145 password = secret_password_lookup_sync (&MOCK_SCHEMA, NULL, &error,
148 g_assert_no_error (error);
149 g_assert (password == NULL);
151 /* should return an item, because we have a prime schema with 5, and flags not to match name */
152 password = secret_password_lookup_sync (&NO_NAME_SCHEMA, NULL, &error,
156 g_assert_no_error (error);
157 g_assert_cmpstr (password, ==, "555");
159 secret_password_free (password);
163 test_store_sync (Test *test,
166 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
167 GError *error = NULL;
171 ret = secret_password_store_sync (&MOCK_SCHEMA, collection_path,
172 "Label here", "the password", NULL, &error,
178 g_assert_no_error (error);
179 g_assert (ret == TRUE);
181 password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
185 g_assert_no_error (error);
186 g_assert_cmpstr (password, ==, "the password");
188 secret_password_free (password);
192 test_store_async (Test *test,
195 const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
196 GAsyncResult *result = NULL;
197 GError *error = NULL;
201 secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
202 "the password", NULL, on_complete_get_result, &result,
207 g_assert (result == NULL);
211 ret = secret_password_store_finish (result, &error);
212 g_assert_no_error (error);
213 g_assert (ret == TRUE);
214 g_object_unref (result);
216 password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
220 g_assert_no_error (error);
221 g_assert_cmpstr (password, ==, "the password");
223 secret_password_free (password);
227 test_delete_sync (Test *test,
230 GError *error = NULL;
233 ret = secret_password_remove_sync (&MOCK_SCHEMA, NULL, &error,
239 g_assert_no_error (error);
240 g_assert (ret == TRUE);
244 test_delete_async (Test *test,
247 GError *error = NULL;
248 GAsyncResult *result = NULL;
251 secret_password_remove (&MOCK_SCHEMA, NULL,
252 on_complete_get_result, &result,
258 g_assert (result == NULL);
262 ret = secret_password_remove_finish (result, &error);
263 g_assert_no_error (error);
264 g_assert (ret == TRUE);
266 g_object_unref (result);
270 test_remove_no_name (Test *test,
273 const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
274 SecretService *service;
275 GError *error = NULL;
278 /* Shouldn't match anything, because no item with 5 in mock schema */
279 ret = secret_password_remove_sync (&MOCK_SCHEMA, NULL, &error,
282 g_assert_no_error (error);
283 g_assert (ret == FALSE);
285 /* We need this collection unlocked for the next test */
286 service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
287 g_assert_no_error (error);
288 secret_service_unlock_dbus_paths_sync (service, paths, NULL, NULL, &error);
289 g_assert_no_error (error);
290 g_object_unref (service);
292 /* We have an item with 5 in prime schema, but should match anyway becase of flags */
293 ret = secret_password_remove_sync (&NO_NAME_SCHEMA, NULL, &error,
297 g_assert_no_error (error);
298 g_assert (ret == TRUE);
302 test_password_free_null (void)
304 secret_password_free (NULL);
308 main (int argc, char **argv)
310 g_test_init (&argc, &argv, NULL);
311 g_set_prgname ("test-password");
314 g_test_add ("/password/lookup-sync", Test, "mock-service-normal.py", setup, test_lookup_sync, teardown);
315 g_test_add ("/password/lookup-async", Test, "mock-service-normal.py", setup, test_lookup_async, teardown);
316 g_test_add ("/password/lookup-no-name", Test, "mock-service-normal.py", setup, test_lookup_no_name, teardown);
318 g_test_add ("/password/store-sync", Test, "mock-service-normal.py", setup, test_store_sync, teardown);
319 g_test_add ("/password/store-async", Test, "mock-service-normal.py", setup, test_store_async, teardown);
321 g_test_add ("/password/delete-sync", Test, "mock-service-delete.py", setup, test_delete_sync, teardown);
322 g_test_add ("/password/delete-async", Test, "mock-service-delete.py", setup, test_delete_async, teardown);
323 g_test_add ("/password/remove-no-name", Test, "mock-service-delete.py", setup, test_remove_no_name, teardown);
325 g_test_add_func ("/password/free-null", test_password_free_null);
327 return egg_tests_run_with_loop ();