Rename the library subdirectory to libsecret
[platform/upstream/libsecret.git] / libsecret / tests / test-password.c
1 /* libsecret - GLib wrapper for Secret Service
2  *
3  * Copyright 2012 Red Hat Inc.
4  *
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.
9  *
10  * See the included COPYING file for more information.
11  *
12  * Author: Stef Walter <stefw@gnome.org>
13  */
14
15 #include "config.h"
16
17 #include "secret-password.h"
18 #include "secret-paths.h"
19 #include "secret-private.h"
20
21 #include "mock-service.h"
22
23 #include "egg/egg-testing.h"
24
25 #include <glib.h>
26
27 #include <errno.h>
28 #include <stdlib.h>
29
30 static const SecretSchema MOCK_SCHEMA = {
31         "org.mock.Schema",
32         SECRET_SCHEMA_NONE,
33         {
34                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
35                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
36                 { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
37         }
38 };
39
40 static const SecretSchema PRIME_SCHEMA = {
41         "org.mock.Prime",
42         SECRET_SCHEMA_NONE,
43         {
44                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
45                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
46                 { "prime", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
47         }
48 };
49
50 static const SecretSchema NO_NAME_SCHEMA = {
51         "unused.Schema.Name",
52         SECRET_SCHEMA_DONT_MATCH_NAME,
53         {
54                 { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
55                 { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
56         }
57 };
58
59 typedef struct {
60         GPid pid;
61 } Test;
62
63 static void
64 setup (Test *test,
65        gconstpointer data)
66 {
67         GError *error = NULL;
68         const gchar *mock_script = data;
69
70         mock_service_start (mock_script, &error);
71         g_assert_no_error (error);
72 }
73
74 static void
75 teardown (Test *test,
76           gconstpointer unused)
77 {
78         mock_service_stop ();
79 }
80
81 static void
82 on_complete_get_result (GObject *source,
83                         GAsyncResult *result,
84                         gpointer user_data)
85 {
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 ();
91 }
92
93 static void
94 test_lookup_sync (Test *test,
95                   gconstpointer used)
96 {
97         gchar *password;
98         GError *error = NULL;
99
100         password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
101                                                             "even", FALSE,
102                                                             "string", "one",
103                                                             "number", 1,
104                                                             NULL);
105
106         g_assert_no_error (error);
107         g_assert_cmpstr (password, ==, "111");
108
109         secret_password_free (password);
110 }
111
112 static void
113 test_lookup_async (Test *test,
114                    gconstpointer used)
115 {
116         GAsyncResult *result = NULL;
117         GError *error = NULL;
118         gchar *password;
119
120         secret_password_lookup (&MOCK_SCHEMA, NULL, on_complete_get_result, &result,
121                                 "even", FALSE,
122                                 "string", "one",
123                                 "number", 1,
124                                 NULL);
125         g_assert (result == NULL);
126
127         egg_test_wait ();
128
129         password = secret_password_lookup_nonpageable_finish (result, &error);
130         g_assert_no_error (error);
131         g_object_unref (result);
132
133         g_assert_cmpstr (password, ==, "111");
134         secret_password_free (password);
135 }
136
137 static void
138 test_lookup_no_name (Test *test,
139                      gconstpointer used)
140 {
141         GError *error = NULL;
142         gchar *password;
143
144         /* should return null, because nothing with mock schema and 5 */
145         password = secret_password_lookup_sync (&MOCK_SCHEMA, NULL, &error,
146                                                 "number", 5,
147                                                 NULL);
148         g_assert_no_error (error);
149         g_assert (password == NULL);
150
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,
153                                                 "number", 5,
154                                                 NULL);
155
156         g_assert_no_error (error);
157         g_assert_cmpstr (password, ==, "555");
158
159         secret_password_free (password);
160 }
161
162 static void
163 test_store_sync (Test *test,
164                   gconstpointer used)
165 {
166         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
167         GError *error = NULL;
168         gchar *password;
169         gboolean ret;
170
171         ret = secret_password_store_sync (&MOCK_SCHEMA, collection_path,
172                                           "Label here", "the password", NULL, &error,
173                                           "even", TRUE,
174                                           "string", "twelve",
175                                           "number", 12,
176                                           NULL);
177
178         g_assert_no_error (error);
179         g_assert (ret == TRUE);
180
181         password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
182                                                             "string", "twelve",
183                                                             NULL);
184
185         g_assert_no_error (error);
186         g_assert_cmpstr (password, ==, "the password");
187
188         secret_password_free (password);
189 }
190
191 static void
192 test_store_async (Test *test,
193                   gconstpointer used)
194 {
195         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
196         GAsyncResult *result = NULL;
197         GError *error = NULL;
198         gchar *password;
199         gboolean ret;
200
201         secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
202                                "the password", NULL, on_complete_get_result, &result,
203                                "even", TRUE,
204                                "string", "twelve",
205                                "number", 12,
206                                NULL);
207         g_assert (result == NULL);
208
209         egg_test_wait ();
210
211         ret = secret_password_store_finish (result, &error);
212         g_assert_no_error (error);
213         g_assert (ret == TRUE);
214         g_object_unref (result);
215
216         password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
217                                                             "string", "twelve",
218                                                             NULL);
219
220         g_assert_no_error (error);
221         g_assert_cmpstr (password, ==, "the password");
222
223         secret_password_free (password);
224 }
225
226 static void
227 test_delete_sync (Test *test,
228                   gconstpointer used)
229 {
230         GError *error = NULL;
231         gboolean ret;
232
233         ret = secret_password_remove_sync (&MOCK_SCHEMA, NULL, &error,
234                                            "even", FALSE,
235                                            "string", "one",
236                                            "number", 1,
237                                            NULL);
238
239         g_assert_no_error (error);
240         g_assert (ret == TRUE);
241 }
242
243 static void
244 test_delete_async (Test *test,
245                    gconstpointer used)
246 {
247         GError *error = NULL;
248         GAsyncResult *result = NULL;
249         gboolean ret;
250
251         secret_password_remove (&MOCK_SCHEMA, NULL,
252                                 on_complete_get_result, &result,
253                                 "even", FALSE,
254                                 "string", "one",
255                                 "number", 1,
256                                 NULL);
257
258         g_assert (result == NULL);
259
260         egg_test_wait ();
261
262         ret = secret_password_remove_finish (result, &error);
263         g_assert_no_error (error);
264         g_assert (ret == TRUE);
265
266         g_object_unref (result);
267 }
268
269 static void
270 test_remove_no_name (Test *test,
271                      gconstpointer used)
272 {
273         const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
274         SecretService *service;
275         GError *error = NULL;
276         gboolean ret;
277
278         /* Shouldn't match anything, because no item with 5 in mock schema */
279         ret = secret_password_remove_sync (&MOCK_SCHEMA, NULL, &error,
280                                            "number", 5,
281                                            NULL);
282         g_assert_no_error (error);
283         g_assert (ret == FALSE);
284
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);
291
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,
294                                           "number", 5,
295                                           NULL);
296
297         g_assert_no_error (error);
298         g_assert (ret == TRUE);
299 }
300
301 static void
302 test_password_free_null (void)
303 {
304         secret_password_free (NULL);
305 }
306
307 int
308 main (int argc, char **argv)
309 {
310         g_test_init (&argc, &argv, NULL);
311         g_set_prgname ("test-password");
312         g_type_init ();
313
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);
317
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);
320
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);
324
325         g_test_add_func ("/password/free-null", test_password_free_null);
326
327         return egg_tests_run_with_loop ();
328 }