Use upstream tag
[platform/upstream/libsecret.git] / libsecret / 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         secret_service_disconnect ();
79         mock_service_stop ();
80 }
81
82 static void
83 on_complete_get_result (GObject *source,
84                         GAsyncResult *result,
85                         gpointer user_data)
86 {
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 ();
92 }
93
94 static void
95 test_lookup_sync (Test *test,
96                   gconstpointer used)
97 {
98         gchar *password;
99         GError *error = NULL;
100
101         password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
102                                                             "even", FALSE,
103                                                             "string", "one",
104                                                             "number", 1,
105                                                             NULL);
106
107         g_assert_no_error (error);
108         g_assert_cmpstr (password, ==, "111");
109
110         secret_password_free (password);
111 }
112
113 static void
114 test_lookup_async (Test *test,
115                    gconstpointer used)
116 {
117         GAsyncResult *result = NULL;
118         GError *error = NULL;
119         gchar *password;
120
121         secret_password_lookup (&MOCK_SCHEMA, NULL, on_complete_get_result, &result,
122                                 "even", FALSE,
123                                 "string", "one",
124                                 "number", 1,
125                                 NULL);
126         g_assert (result == NULL);
127
128         egg_test_wait ();
129
130         password = secret_password_lookup_nonpageable_finish (result, &error);
131         g_assert_no_error (error);
132         g_object_unref (result);
133
134         g_assert_cmpstr (password, ==, "111");
135         secret_password_free (password);
136 }
137
138 static void
139 test_lookup_no_name (Test *test,
140                      gconstpointer used)
141 {
142         GError *error = NULL;
143         gchar *password;
144
145         /* should return null, because nothing with mock schema and 5 */
146         password = secret_password_lookup_sync (&MOCK_SCHEMA, NULL, &error,
147                                                 "number", 5,
148                                                 NULL);
149         g_assert_no_error (error);
150         g_assert (password == NULL);
151
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,
154                                                 "number", 5,
155                                                 NULL);
156
157         g_assert_no_error (error);
158         g_assert_cmpstr (password, ==, "555");
159
160         secret_password_free (password);
161 }
162
163 static void
164 test_store_sync (Test *test,
165                   gconstpointer used)
166 {
167         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
168         GError *error = NULL;
169         gchar *password;
170         gboolean ret;
171
172         ret = secret_password_store_sync (&MOCK_SCHEMA, collection_path,
173                                           "Label here", "the password", NULL, &error,
174                                           "even", TRUE,
175                                           "string", "twelve",
176                                           "number", 12,
177                                           NULL);
178
179         g_assert_no_error (error);
180         g_assert (ret == TRUE);
181
182         password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
183                                                             "string", "twelve",
184                                                             NULL);
185
186         g_assert_no_error (error);
187         g_assert_cmpstr (password, ==, "the password");
188
189         secret_password_free (password);
190 }
191
192 static void
193 test_store_async (Test *test,
194                   gconstpointer used)
195 {
196         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
197         GAsyncResult *result = NULL;
198         GError *error = NULL;
199         gchar *password;
200         gboolean ret;
201
202         secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
203                                "the password", NULL, on_complete_get_result, &result,
204                                "even", TRUE,
205                                "string", "twelve",
206                                "number", 12,
207                                NULL);
208         g_assert (result == NULL);
209
210         egg_test_wait ();
211
212         ret = secret_password_store_finish (result, &error);
213         g_assert_no_error (error);
214         g_assert (ret == TRUE);
215         g_object_unref (result);
216
217         password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
218                                                             "string", "twelve",
219                                                             NULL);
220
221         g_assert_no_error (error);
222         g_assert_cmpstr (password, ==, "the password");
223
224         secret_password_free (password);
225 }
226
227 static void
228 test_store_unlock (Test *test,
229                    gconstpointer unused)
230 {
231         const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
232         GAsyncResult *result = NULL;
233         SecretCollection *collection;
234         SecretService *service;
235         GError *error = NULL;
236         gchar *password;
237         gboolean ret;
238         GList *objects;
239         gint count;
240
241         service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
242         g_assert_no_error (error);
243
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);
249
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);
253         egg_test_wait ();
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);
258
259         /* Check collection state */
260         g_assert (secret_collection_get_locked (collection) == TRUE);
261
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,
265                                "even", TRUE,
266                                "string", "twelve",
267                                "number", 12,
268                                NULL);
269         g_assert (result == NULL);
270         egg_test_wait ();
271         ret = secret_password_store_finish (result, &error);
272         g_assert_no_error (error);
273         g_assert (ret == TRUE);
274         g_clear_object (&result);
275
276         /* Check collection state */
277         g_assert (secret_collection_get_locked (collection) == FALSE);
278
279
280         password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
281                                                             "string", "twelve",
282                                                             NULL);
283
284         g_assert_no_error (error);
285         g_assert_cmpstr (password, ==, "the password");
286
287         secret_password_free (password);
288         g_object_unref (collection);
289         g_object_unref (service);
290 }
291
292 static void
293 test_delete_sync (Test *test,
294                   gconstpointer used)
295 {
296         GError *error = NULL;
297         gboolean ret;
298
299         ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
300                                           "even", FALSE,
301                                           "string", "one",
302                                           "number", 1,
303                                           NULL);
304
305         g_assert_no_error (error);
306         g_assert (ret == TRUE);
307 }
308
309 static void
310 test_delete_async (Test *test,
311                    gconstpointer used)
312 {
313         GError *error = NULL;
314         GAsyncResult *result = NULL;
315         gboolean ret;
316
317         secret_password_clear (&MOCK_SCHEMA, NULL,
318                                on_complete_get_result, &result,
319                                "even", FALSE,
320                                "string", "one",
321                                "number", 1,
322                                NULL);
323
324         g_assert (result == NULL);
325
326         egg_test_wait ();
327
328         ret = secret_password_clear_finish (result, &error);
329         g_assert_no_error (error);
330         g_assert (ret == TRUE);
331
332         g_object_unref (result);
333 }
334
335 static void
336 test_clear_no_name (Test *test,
337                     gconstpointer used)
338 {
339         const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
340         SecretService *service;
341         GError *error = NULL;
342         gboolean ret;
343
344         /* Shouldn't match anything, because no item with 5 in mock schema */
345         ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
346                                           "number", 5,
347                                           NULL);
348         g_assert_no_error (error);
349         g_assert (ret == FALSE);
350
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);
357
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,
360                                           "number", 5,
361                                           NULL);
362
363         g_assert_no_error (error);
364         g_assert (ret == TRUE);
365 }
366
367 static void
368 test_password_free_null (void)
369 {
370         secret_password_free (NULL);
371 }
372
373 int
374 main (int argc, char **argv)
375 {
376         g_test_init (&argc, &argv, NULL);
377         g_set_prgname ("test-password");
378 #if !GLIB_CHECK_VERSION(2,35,0)
379         g_type_init ();
380 #endif
381
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);
385
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);
389
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);
393
394         g_test_add_func ("/password/free-null", test_password_free_null);
395
396         return egg_tests_run_with_loop ();
397 }