Branch data Line data Source code
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 : : #undef G_DISABLE_ASSERT
18 : :
19 : : #include "secret-password.h"
20 : : #include "secret-paths.h"
21 : : #include "secret-private.h"
22 : :
23 : : #include "mock-service.h"
24 : :
25 : : #include "egg/egg-testing.h"
26 : :
27 : : #include <glib.h>
28 : :
29 : : #include <errno.h>
30 : : #include <stdlib.h>
31 : :
32 : : static const SecretSchema MOCK_SCHEMA = {
33 : : "org.mock.Schema",
34 : : SECRET_SCHEMA_NONE,
35 : : {
36 : : { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
37 : : { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
38 : : { "even", SECRET_SCHEMA_ATTRIBUTE_BOOLEAN },
39 : : }
40 : : };
41 : :
42 : : static const SecretSchema NO_NAME_SCHEMA = {
43 : : "unused.Schema.Name",
44 : : SECRET_SCHEMA_DONT_MATCH_NAME,
45 : : {
46 : : { "number", SECRET_SCHEMA_ATTRIBUTE_INTEGER },
47 : : { "string", SECRET_SCHEMA_ATTRIBUTE_STRING },
48 : : }
49 : : };
50 : :
51 : : typedef struct {
52 : : GPid pid;
53 : : } Test;
54 : :
55 : : static void
56 : 14 : setup (Test *test,
57 : : gconstpointer data)
58 : : {
59 : 14 : GError *error = NULL;
60 : 14 : const gchar *mock_script = data;
61 : :
62 : 14 : mock_service_start (mock_script, &error);
63 [ - + ]: 14 : g_assert_no_error (error);
64 : 14 : }
65 : :
66 : : static void
67 : 14 : teardown (Test *test,
68 : : gconstpointer unused)
69 : : {
70 : 14 : secret_service_disconnect ();
71 : 14 : mock_service_stop ();
72 : 14 : }
73 : :
74 : : static void
75 : 7 : on_complete_get_result (GObject *source,
76 : : GAsyncResult *result,
77 : : gpointer user_data)
78 : : {
79 : 7 : GAsyncResult **ret = user_data;
80 [ - + ]: 7 : g_assert_nonnull (ret);
81 [ - + ]: 7 : g_assert_null (*ret);
82 : 7 : *ret = g_object_ref (result);
83 : 7 : egg_test_wait_stop ();
84 : 7 : }
85 : :
86 : : static void
87 : 1 : test_lookup_sync (Test *test,
88 : : gconstpointer used)
89 : : {
90 : : gchar *password;
91 : 1 : GError *error = NULL;
92 : :
93 : 1 : password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
94 : : "even", FALSE,
95 : : "string", "one",
96 : : "number", 1,
97 : : NULL);
98 : :
99 [ - + ]: 1 : g_assert_no_error (error);
100 [ - + ]: 1 : g_assert_cmpstr (password, ==, "111");
101 : :
102 : 1 : secret_password_free (password);
103 : 1 : }
104 : :
105 : : static void
106 : 1 : test_lookup_async (Test *test,
107 : : gconstpointer used)
108 : : {
109 : 1 : GAsyncResult *result = NULL;
110 : 1 : GError *error = NULL;
111 : : gchar *password;
112 : :
113 : 1 : secret_password_lookup (&MOCK_SCHEMA, NULL, on_complete_get_result, &result,
114 : : "even", FALSE,
115 : : "string", "one",
116 : : "number", 1,
117 : : NULL);
118 [ - + ]: 1 : g_assert_null (result);
119 : :
120 [ - + ]: 1 : egg_test_wait ();
121 : :
122 : 1 : password = secret_password_lookup_nonpageable_finish (result, &error);
123 [ - + ]: 1 : g_assert_no_error (error);
124 : 1 : g_object_unref (result);
125 : :
126 [ - + ]: 1 : g_assert_cmpstr (password, ==, "111");
127 : 1 : secret_password_free (password);
128 : 1 : }
129 : :
130 : : static void
131 : 1 : test_lookup_no_name (Test *test,
132 : : gconstpointer used)
133 : : {
134 : 1 : GError *error = NULL;
135 : : gchar *password;
136 : :
137 : : /* should return null, because nothing with mock schema and 5 */
138 : 1 : password = secret_password_lookup_sync (&MOCK_SCHEMA, NULL, &error,
139 : : "number", 5,
140 : : NULL);
141 [ - + ]: 1 : g_assert_no_error (error);
142 [ - + ]: 1 : g_assert_null (password);
143 : :
144 : : /* should return an item, because we have a prime schema with 5, and flags not to match name */
145 : 1 : password = secret_password_lookup_sync (&NO_NAME_SCHEMA, NULL, &error,
146 : : "number", 5,
147 : : NULL);
148 : :
149 [ - + ]: 1 : g_assert_no_error (error);
150 [ - + ]: 1 : g_assert_cmpstr (password, ==, "555");
151 : :
152 : 1 : secret_password_free (password);
153 : 1 : }
154 : :
155 : : static void
156 : 1 : test_store_sync (Test *test,
157 : : gconstpointer used)
158 : : {
159 : 1 : const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
160 : 1 : GError *error = NULL;
161 : : gchar *password;
162 : : gboolean ret;
163 : :
164 : 1 : ret = secret_password_store_sync (&MOCK_SCHEMA, collection_path,
165 : : "Label here", "the password", NULL, &error,
166 : : "even", TRUE,
167 : : "string", "twelve",
168 : : "number", 12,
169 : : NULL);
170 : :
171 [ - + ]: 1 : g_assert_no_error (error);
172 [ - + ]: 1 : g_assert_true (ret);
173 : :
174 : 1 : password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
175 : : "string", "twelve",
176 : : NULL);
177 : :
178 [ - + ]: 1 : g_assert_no_error (error);
179 [ - + ]: 1 : g_assert_cmpstr (password, ==, "the password");
180 : :
181 : 1 : secret_password_free (password);
182 : 1 : }
183 : :
184 : : static void
185 : 1 : test_store_async (Test *test,
186 : : gconstpointer used)
187 : : {
188 : 1 : const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
189 : 1 : GAsyncResult *result = NULL;
190 : 1 : GError *error = NULL;
191 : : gchar *password;
192 : : gboolean ret;
193 : :
194 : 1 : secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
195 : : "the password", NULL, on_complete_get_result, &result,
196 : : "even", TRUE,
197 : : "string", "twelve",
198 : : "number", 12,
199 : : NULL);
200 [ - + ]: 1 : g_assert_null (result);
201 : :
202 [ - + ]: 1 : egg_test_wait ();
203 : :
204 : 1 : ret = secret_password_store_finish (result, &error);
205 [ - + ]: 1 : g_assert_no_error (error);
206 [ - + ]: 1 : g_assert_true (ret);
207 : 1 : g_object_unref (result);
208 : :
209 : 1 : password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
210 : : "string", "twelve",
211 : : NULL);
212 : :
213 [ - + ]: 1 : g_assert_no_error (error);
214 [ - + ]: 1 : g_assert_cmpstr (password, ==, "the password");
215 : :
216 : 1 : secret_password_free (password);
217 : 1 : }
218 : :
219 : : static void
220 : 1 : test_store_unlock (Test *test,
221 : : gconstpointer unused)
222 : : {
223 : 1 : const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
224 : 1 : GAsyncResult *result = NULL;
225 : : SecretCollection *collection;
226 : : SecretService *service;
227 : 1 : GError *error = NULL;
228 : : gchar *password;
229 : : gboolean ret;
230 : : GList *objects;
231 : : gint count;
232 : :
233 : 1 : service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
234 [ - + ]: 1 : g_assert_no_error (error);
235 : :
236 : : /* Check collection state */
237 : 1 : collection = secret_collection_new_for_dbus_path_sync (service, collection_path,
238 : : SECRET_COLLECTION_NONE, NULL, &error);
239 [ - + ]: 1 : g_assert_no_error (error);
240 [ - + ]: 1 : g_assert_false (secret_collection_get_locked (collection));
241 : :
242 : : /* Lock it, use async, so collection properties update */
243 : 1 : objects = g_list_append (NULL, collection);
244 : 1 : secret_service_lock (service, objects, NULL, on_complete_get_result, &result);
245 [ - + ]: 1 : egg_test_wait ();
246 : 1 : count = secret_service_lock_finish (service, result, NULL, &error);
247 [ - + ]: 1 : g_assert_cmpint (count, ==, 1);
248 [ + - ]: 1 : g_clear_object (&result);
249 : 1 : g_list_free (objects);
250 : :
251 : : /* Check collection state */
252 [ - + ]: 1 : g_assert_true (secret_collection_get_locked (collection));
253 : :
254 : : /* Store the password, use async so collection properties update */
255 : 1 : secret_password_store (&MOCK_SCHEMA, collection_path, "Label here",
256 : : "the password", NULL, on_complete_get_result, &result,
257 : : "even", TRUE,
258 : : "string", "twelve",
259 : : "number", 12,
260 : : NULL);
261 [ - + ]: 1 : g_assert_null (result);
262 [ - + ]: 1 : egg_test_wait ();
263 : 1 : ret = secret_password_store_finish (result, &error);
264 [ - + ]: 1 : g_assert_no_error (error);
265 [ - + ]: 1 : g_assert_true (ret);
266 [ + - ]: 1 : g_clear_object (&result);
267 : :
268 : : /* Check collection state */
269 [ - + ]: 1 : g_assert_false (secret_collection_get_locked (collection));
270 : :
271 : :
272 : 1 : password = secret_password_lookup_nonpageable_sync (&MOCK_SCHEMA, NULL, &error,
273 : : "string", "twelve",
274 : : NULL);
275 : :
276 [ - + ]: 1 : g_assert_no_error (error);
277 [ - + ]: 1 : g_assert_cmpstr (password, ==, "the password");
278 : :
279 : 1 : secret_password_free (password);
280 : 1 : g_object_unref (collection);
281 : 1 : g_object_unref (service);
282 : 1 : }
283 : :
284 : : static void
285 : 1 : test_delete_sync (Test *test,
286 : : gconstpointer used)
287 : : {
288 : 1 : GError *error = NULL;
289 : : gboolean ret;
290 : :
291 : 1 : ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
292 : : "even", FALSE,
293 : : "string", "one",
294 : : "number", 1,
295 : : NULL);
296 : :
297 [ - + ]: 1 : g_assert_no_error (error);
298 [ - + ]: 1 : g_assert_true (ret);
299 : 1 : }
300 : :
301 : : static void
302 : 1 : test_delete_async (Test *test,
303 : : gconstpointer used)
304 : : {
305 : 1 : GError *error = NULL;
306 : 1 : GAsyncResult *result = NULL;
307 : : gboolean ret;
308 : :
309 : 1 : secret_password_clear (&MOCK_SCHEMA, NULL,
310 : : on_complete_get_result, &result,
311 : : "even", FALSE,
312 : : "string", "one",
313 : : "number", 1,
314 : : NULL);
315 : :
316 [ - + ]: 1 : g_assert_null (result);
317 : :
318 [ - + ]: 1 : egg_test_wait ();
319 : :
320 : 1 : ret = secret_password_clear_finish (result, &error);
321 [ - + ]: 1 : g_assert_no_error (error);
322 [ - + ]: 1 : g_assert_true (ret);
323 : :
324 : 1 : g_object_unref (result);
325 : 1 : }
326 : :
327 : : static void
328 : 1 : test_clear_no_name (Test *test,
329 : : gconstpointer used)
330 : : {
331 : 1 : const gchar *paths[] = { "/org/freedesktop/secrets/collection/german", NULL };
332 : : SecretService *service;
333 : 1 : GError *error = NULL;
334 : : gboolean ret;
335 : :
336 : : /* Shouldn't match anything, because no item with 5 in mock schema */
337 : 1 : ret = secret_password_clear_sync (&MOCK_SCHEMA, NULL, &error,
338 : : "number", 5,
339 : : NULL);
340 [ - + ]: 1 : g_assert_no_error (error);
341 [ - + ]: 1 : g_assert_false (ret);
342 : :
343 : : /* We need this collection unlocked for the next test */
344 : 1 : service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
345 [ - + ]: 1 : g_assert_no_error (error);
346 : 1 : secret_service_unlock_dbus_paths_sync (service, paths, NULL, NULL, &error);
347 [ - + ]: 1 : g_assert_no_error (error);
348 : 1 : g_object_unref (service);
349 : :
350 : : /* We have an item with 5 in prime schema, but should match anyway becase of flags */
351 : 1 : ret = secret_password_clear_sync (&NO_NAME_SCHEMA, NULL, &error,
352 : : "number", 5,
353 : : NULL);
354 : :
355 [ - + ]: 1 : g_assert_no_error (error);
356 [ - + ]: 1 : g_assert_true (ret);
357 : 1 : }
358 : :
359 : : static void
360 : 3 : free_attributes (gpointer data,
361 : : gpointer user_data)
362 : : {
363 : 3 : g_object_unref ((GObject *)data);
364 : 3 : }
365 : :
366 : : static void
367 : 1 : test_search_sync (Test *test,
368 : : gconstpointer used)
369 : : {
370 : : GList *items;
371 : 1 : GError *error = NULL;
372 : :
373 : 1 : items = secret_password_search_sync (&MOCK_SCHEMA, SECRET_SEARCH_ALL,
374 : : NULL, &error,
375 : : "even", FALSE,
376 : : "string", "one",
377 : : "number", 1,
378 : : NULL);
379 : :
380 [ - + ]: 1 : g_assert_no_error (error);
381 [ - + ]: 1 : g_assert_cmpint (g_list_length (items), ==, 1);
382 : :
383 : 1 : g_list_foreach (items, free_attributes, NULL);
384 : 1 : g_list_free (items);
385 : 1 : }
386 : :
387 : : static void
388 : 1 : test_search_async (Test *test,
389 : : gconstpointer used)
390 : : {
391 : 1 : GAsyncResult *result = NULL;
392 : 1 : GError *error = NULL;
393 : : GList *items;
394 : :
395 : 1 : secret_password_search (&MOCK_SCHEMA, SECRET_SEARCH_ALL,
396 : : NULL, on_complete_get_result, &result,
397 : : "even", FALSE,
398 : : "string", "one",
399 : : "number", 1,
400 : : NULL);
401 [ - + ]: 1 : g_assert (result == NULL);
402 : :
403 [ - + ]: 1 : egg_test_wait ();
404 : :
405 : 1 : items = secret_password_search_finish (result, &error);
406 [ - + ]: 1 : g_assert_no_error (error);
407 : 1 : g_object_unref (result);
408 : :
409 [ - + ]: 1 : g_assert_cmpint (g_list_length (items), ==, 1);
410 : :
411 : 1 : g_list_foreach (items, free_attributes, NULL);
412 : 1 : g_list_free (items);
413 : 1 : }
414 : :
415 : : static void
416 : 1 : test_search_no_name (Test *test,
417 : : gconstpointer used)
418 : : {
419 : 1 : GError *error = NULL;
420 : : GList *items;
421 : :
422 : : /* should return null, because nothing with mock schema and 5 */
423 : 1 : items = secret_password_search_sync (&MOCK_SCHEMA, SECRET_SEARCH_ALL,
424 : : NULL, &error,
425 : : "number", 5,
426 : : NULL);
427 [ - + ]: 1 : g_assert_no_error (error);
428 [ - + ]: 1 : g_assert (items == NULL);
429 : :
430 : : /* should return an item, because we have a prime schema with 5, and flags not to match name */
431 : 1 : items = secret_password_search_sync (&NO_NAME_SCHEMA, SECRET_SEARCH_ALL,
432 : : NULL, &error,
433 : : "number", 5,
434 : : NULL);
435 : :
436 [ - + ]: 1 : g_assert_no_error (error);
437 [ - + ]: 1 : g_assert_cmpint (g_list_length (items), ==, 1);
438 : :
439 : 1 : g_list_foreach (items, free_attributes, NULL);
440 : 1 : g_list_free (items);
441 : 1 : }
442 : :
443 : : static void
444 : 1 : test_binary_sync (Test *test,
445 : : gconstpointer used)
446 : : {
447 : 1 : const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
448 : 1 : GError *error = NULL;
449 : : SecretValue *value;
450 : : gboolean ret;
451 : :
452 : 1 : value = secret_value_new ("the password", -1, "text/plain");
453 : 1 : ret = secret_password_store_binary_sync (&MOCK_SCHEMA, collection_path,
454 : : "Label here", value, NULL, &error,
455 : : "even", TRUE,
456 : : "string", "twelve",
457 : : "number", 12,
458 : : NULL);
459 : :
460 [ - + ]: 1 : g_assert_no_error (error);
461 [ - + ]: 1 : g_assert_true (ret);
462 : 1 : secret_value_unref (value);
463 : :
464 : 1 : value = secret_password_lookup_binary_sync (&MOCK_SCHEMA, NULL, &error,
465 : : "string", "twelve",
466 : : NULL);
467 : :
468 [ - + ]: 1 : g_assert_no_error (error);
469 [ - + ]: 1 : g_assert_cmpstr (secret_value_get_text (value), ==, "the password");
470 : :
471 : 1 : secret_value_unref (value);
472 : 1 : }
473 : :
474 : : static void
475 : 1 : test_binary_async (Test *test,
476 : : gconstpointer used)
477 : : {
478 : 1 : const gchar *collection_path = "/org/freedesktop/secrets/collection/english";
479 : 1 : GAsyncResult *result = NULL;
480 : 1 : GError *error = NULL;
481 : : SecretValue *value;
482 : : gboolean ret;
483 : :
484 : 1 : value = secret_value_new ("the password", -1, "text/plain");
485 : 1 : secret_password_store_binary (&MOCK_SCHEMA, collection_path, "Label here",
486 : : value, NULL, on_complete_get_result, &result,
487 : : "even", TRUE,
488 : : "string", "twelve",
489 : : "number", 12,
490 : : NULL);
491 [ - + ]: 1 : g_assert_null (result);
492 : 1 : secret_value_unref (value);
493 : :
494 [ - + ]: 1 : egg_test_wait ();
495 : :
496 : 1 : ret = secret_password_store_finish (result, &error);
497 [ - + ]: 1 : g_assert_no_error (error);
498 [ - + ]: 1 : g_assert_true (ret);
499 : 1 : g_object_unref (result);
500 : :
501 : 1 : value = secret_password_lookup_binary_sync (&MOCK_SCHEMA, NULL, &error,
502 : : "string", "twelve",
503 : : NULL);
504 : :
505 [ - + ]: 1 : g_assert_no_error (error);
506 [ - + ]: 1 : g_assert_nonnull (value);
507 : :
508 [ - + ]: 1 : g_assert_cmpstr (secret_value_get_text (value), ==, "the password");
509 : :
510 : 1 : secret_value_unref (value);
511 : 1 : }
512 : :
513 : : static void
514 : 1 : test_password_free_null (void)
515 : : {
516 : 1 : secret_password_free (NULL);
517 : 1 : }
518 : :
519 : : int
520 : 1 : main (int argc, char **argv)
521 : : {
522 : 1 : g_test_init (&argc, &argv, NULL);
523 : 1 : g_set_prgname ("test-password");
524 : :
525 : 1 : g_test_add ("/password/lookup-sync", Test, "mock-service-normal.py", setup, test_lookup_sync, teardown);
526 : 1 : g_test_add ("/password/lookup-async", Test, "mock-service-normal.py", setup, test_lookup_async, teardown);
527 : 1 : g_test_add ("/password/lookup-no-name", Test, "mock-service-normal.py", setup, test_lookup_no_name, teardown);
528 : :
529 : 1 : g_test_add ("/password/store-sync", Test, "mock-service-normal.py", setup, test_store_sync, teardown);
530 : 1 : g_test_add ("/password/store-async", Test, "mock-service-normal.py", setup, test_store_async, teardown);
531 : 1 : g_test_add ("/password/store-unlock", Test, "mock-service-normal.py", setup, test_store_unlock, teardown);
532 : :
533 : 1 : g_test_add ("/password/delete-sync", Test, "mock-service-delete.py", setup, test_delete_sync, teardown);
534 : 1 : g_test_add ("/password/delete-async", Test, "mock-service-delete.py", setup, test_delete_async, teardown);
535 : 1 : g_test_add ("/password/clear-no-name", Test, "mock-service-delete.py", setup, test_clear_no_name, teardown);
536 : :
537 : 1 : g_test_add ("/password/search-sync", Test, "mock-service-normal.py", setup, test_search_sync, teardown);
538 : 1 : g_test_add ("/password/search-async", Test, "mock-service-normal.py", setup, test_search_async, teardown);
539 : 1 : g_test_add ("/password/search-no-name", Test, "mock-service-normal.py", setup, test_search_no_name, teardown);
540 : :
541 : 1 : g_test_add ("/password/binary-sync", Test, "mock-service-normal.py", setup, test_binary_sync, teardown);
542 : 1 : g_test_add ("/password/binary-async", Test, "mock-service-normal.py", setup, test_binary_async, teardown);
543 : :
544 : 1 : g_test_add_func ("/password/free-null", test_password_free_null);
545 : :
546 : 1 : return egg_tests_run_with_loop ();
547 : : }
|