1 /* libsecret - GLib wrapper for Secret Service
3 * Copyright 2011 Collabora Ltd.
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.
16 #include "secret-collection.h"
17 #include "secret-item.h"
18 #include "secret-service.h"
19 #include "secret-paths.h"
20 #include "secret-private.h"
22 #include "mock-service.h"
24 #include "egg/egg-testing.h"
32 SecretService *service;
36 setup_mock (Test *test,
40 const gchar *mock_script = data;
42 mock_service_start (mock_script, &error);
43 g_assert_no_error (error);
47 teardown_mock (Test *test,
50 secret_service_disconnect ();
55 on_complete_get_result (GObject *source,
59 GAsyncResult **ret = user_data;
60 g_assert (ret != NULL);
61 g_assert (*ret == NULL);
62 *ret = g_object_ref (result);
63 egg_test_wait_stop ();
67 test_get_sync (Test *test,
70 SecretService *service1;
71 SecretService *service2;
72 SecretService *service3;
75 /* Both these sohuld point to the same thing */
77 service1 = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
78 g_assert_no_error (error);
80 service2 = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
81 g_assert_no_error (error);
82 g_object_add_weak_pointer (G_OBJECT (service2), (gpointer *)&service2);
84 g_assert (SECRET_IS_SERVICE (service1));
85 g_assert (service1 == service2);
87 g_object_unref (service1);
88 g_assert (G_IS_OBJECT (service1));
90 g_object_unref (service2);
91 secret_service_disconnect ();
92 g_assert (service2 == NULL);
94 /* Services were disconnected, so this should create a new one */
95 service3 = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
96 g_assert (SECRET_IS_SERVICE (service3));
97 g_assert_no_error (error);
98 g_object_add_weak_pointer (G_OBJECT (service3), (gpointer *)&service3);
100 g_object_unref (service3);
101 secret_service_disconnect ();
102 g_assert (service3 == NULL);
106 test_get_async (Test *test,
109 SecretService *service1;
110 SecretService *service2;
111 SecretService *service3;
112 GAsyncResult *result = NULL;
113 GError *error = NULL;
115 /* Both these sohuld point to the same thing */
117 secret_service_get (SECRET_SERVICE_NONE, NULL, on_complete_get_result, &result);
118 g_assert (result == NULL);
120 service1 = secret_service_get_finish (result, &error);
121 g_assert_no_error (error);
122 g_clear_object (&result);
124 secret_service_get (SECRET_SERVICE_NONE, NULL, on_complete_get_result, &result);
125 g_assert (result == NULL);
127 service2 = secret_service_get_finish (result, &error);
128 g_assert_no_error (error);
129 g_clear_object (&result);
130 g_object_add_weak_pointer (G_OBJECT (service2), (gpointer *)&service2);
132 g_assert (SECRET_IS_SERVICE (service1));
133 g_assert (service1 == service2);
135 g_object_unref (service1);
136 g_assert (G_IS_OBJECT (service1));
138 g_object_unref (service2);
139 secret_service_disconnect ();
140 g_assert (service2 == NULL);
142 /* Services were unreffed, so this should create a new one */
143 secret_service_get (SECRET_SERVICE_NONE, NULL, on_complete_get_result, &result);
144 g_assert (result == NULL);
146 service3 = secret_service_get_finish (result, &error);
147 g_assert_no_error (error);
148 g_clear_object (&result);
149 g_object_add_weak_pointer (G_OBJECT (service3), (gpointer *)&service3);
151 g_object_unref (service3);
152 secret_service_disconnect ();
153 g_assert (service3 == NULL);
157 test_get_more_sync (Test *test,
160 SecretService *service;
161 SecretService *service2;
162 GError *error = NULL;
166 service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
167 g_assert_no_error (error);
168 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
170 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_NONE);
172 service2 = secret_service_get_sync (SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &error);
173 g_assert_no_error (error);
175 g_assert (SECRET_IS_SERVICE (service));
176 g_assert (service == service2);
178 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_LOAD_COLLECTIONS);
179 collections = secret_service_get_collections (service);
180 g_assert (collections != NULL);
181 g_list_free_full (collections, g_object_unref);
183 g_object_unref (service2);
185 service2 = secret_service_get_sync (SECRET_SERVICE_OPEN_SESSION, NULL, &error);
186 g_assert_no_error (error);
188 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_OPEN_SESSION | SECRET_SERVICE_LOAD_COLLECTIONS);
189 path = secret_service_get_session_dbus_path (service);
190 g_assert (path != NULL);
192 g_object_unref (service2);
194 g_object_unref (service);
195 secret_service_disconnect ();
196 g_assert (service == NULL);
200 test_get_more_async (Test *test,
203 GAsyncResult *result = NULL;
204 SecretService *service;
205 GError *error = NULL;
209 secret_service_get (SECRET_SERVICE_LOAD_COLLECTIONS | SECRET_SERVICE_OPEN_SESSION, NULL, on_complete_get_result, &result);
210 g_assert (result == NULL);
214 service = secret_service_get_finish (result, &error);
215 g_assert_no_error (error);
216 g_object_unref (result);
218 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
220 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_OPEN_SESSION | SECRET_SERVICE_LOAD_COLLECTIONS);
221 path = secret_service_get_session_dbus_path (service);
222 g_assert (path != NULL);
224 collections = secret_service_get_collections (service);
225 g_assert (collections != NULL);
226 g_list_free_full (collections, g_object_unref);
228 g_object_unref (service);
229 secret_service_disconnect ();
230 g_assert (service == NULL);
232 /* Now get a session with just collections */
234 secret_service_get (SECRET_SERVICE_LOAD_COLLECTIONS, NULL, on_complete_get_result, &result);
235 g_assert (result == NULL);
239 service = secret_service_get_finish (result, &error);
240 g_assert_no_error (error);
241 g_object_unref (result);
242 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
244 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_LOAD_COLLECTIONS);
245 path = secret_service_get_session_dbus_path (service);
246 g_assert (path == NULL);
248 collections = secret_service_get_collections (service);
249 g_assert (collections != NULL);
250 g_list_free_full (collections, g_object_unref);
252 g_object_unref (service);
253 secret_service_disconnect ();
254 g_assert (service == NULL);
258 test_open_sync (Test *test,
261 SecretService *service1;
262 SecretService *service2;
263 GError *error = NULL;
265 /* Both these sohuld point to different things */
267 service1 = secret_service_open_sync (SECRET_TYPE_SERVICE, NULL,
268 SECRET_SERVICE_NONE, NULL, &error);
269 g_assert_no_error (error);
270 g_object_add_weak_pointer (G_OBJECT (service1), (gpointer *)&service1);
272 service2 = secret_service_open_sync (SECRET_TYPE_SERVICE, NULL,
273 SECRET_SERVICE_NONE, NULL, &error);
274 g_assert_no_error (error);
275 g_object_add_weak_pointer (G_OBJECT (service2), (gpointer *)&service2);
277 g_assert (SECRET_IS_SERVICE (service1));
278 g_assert (SECRET_IS_SERVICE (service2));
279 g_assert (service1 != service2);
281 g_object_unref (service1);
282 g_assert (service1 == NULL);
284 g_object_unref (service2);
285 g_assert (service2 == NULL);
289 test_open_async (Test *test,
292 SecretService *service1;
293 SecretService *service2;
294 GAsyncResult *result = NULL;
295 GError *error = NULL;
297 /* Both these sohuld point to different things */
299 secret_service_open (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_NONE,
300 NULL, on_complete_get_result, &result);
301 g_assert (result == NULL);
303 service1 = secret_service_open_finish (result, &error);
304 g_assert_no_error (error);
305 g_clear_object (&result);
306 g_object_add_weak_pointer (G_OBJECT (service1), (gpointer *)&service1);
308 secret_service_open (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_NONE, NULL,
309 on_complete_get_result, &result);
310 g_assert (result == NULL);
312 service2 = secret_service_open_finish (result, &error);
313 g_assert_no_error (error);
314 g_clear_object (&result);
315 g_object_add_weak_pointer (G_OBJECT (service2), (gpointer *)&service2);
317 g_assert (SECRET_IS_SERVICE (service1));
318 g_assert (SECRET_IS_SERVICE (service2));
319 g_assert (service1 != service2);
321 g_object_unref (service1);
322 g_assert (service1 == NULL);
324 g_object_unref (service2);
325 g_assert (service2 == NULL);
329 test_open_more_sync (Test *test,
332 SecretService *service;
333 GError *error = NULL;
337 service = secret_service_open_sync (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_NONE,
339 g_assert_no_error (error);
340 g_assert (SECRET_IS_SERVICE (service));
341 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
343 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_NONE);
344 g_assert (secret_service_get_collections (service) == NULL);
345 g_assert (secret_service_get_session_dbus_path (service) == NULL);
347 g_object_unref (service);
348 g_assert (service == NULL);
350 service = secret_service_open_sync (SECRET_TYPE_SERVICE, NULL,
351 SECRET_SERVICE_LOAD_COLLECTIONS, NULL, &error);
352 g_assert_no_error (error);
353 g_assert (SECRET_IS_SERVICE (service));
354 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
356 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_LOAD_COLLECTIONS);
357 collections = secret_service_get_collections (service);
358 g_assert (collections != NULL);
359 g_list_free_full (collections, g_object_unref);
360 g_assert (secret_service_get_session_dbus_path (service) == NULL);
362 g_object_unref (service);
363 g_assert (service == NULL);
365 service = secret_service_open_sync (SECRET_TYPE_SERVICE, NULL,
366 SECRET_SERVICE_OPEN_SESSION, NULL, &error);
367 g_assert_no_error (error);
368 g_assert (SECRET_IS_SERVICE (service));
369 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
371 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_OPEN_SESSION);
372 g_assert (secret_service_get_collections (service) == NULL);
373 path = secret_service_get_session_dbus_path (service);
374 g_assert (path != NULL);
376 g_object_unref (service);
377 g_assert (service == NULL);
381 test_open_more_async (Test *test,
384 GAsyncResult *result = NULL;
385 SecretService *service;
386 GError *error = NULL;
390 secret_service_open (SECRET_TYPE_SERVICE, NULL,
391 SECRET_SERVICE_LOAD_COLLECTIONS | SECRET_SERVICE_OPEN_SESSION, NULL, on_complete_get_result, &result);
392 g_assert (result == NULL);
396 service = secret_service_open_finish (result, &error);
397 g_assert_no_error (error);
398 g_object_unref (result);
400 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
402 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_OPEN_SESSION | SECRET_SERVICE_LOAD_COLLECTIONS);
403 path = secret_service_get_session_dbus_path (service);
404 g_assert (path != NULL);
406 collections = secret_service_get_collections (service);
407 g_assert (collections != NULL);
408 g_list_free_full (collections, g_object_unref);
410 g_object_unref (service);
411 g_assert (service == NULL);
413 /* Now get a session with just collections */
415 secret_service_open (SECRET_TYPE_SERVICE, NULL, SECRET_SERVICE_LOAD_COLLECTIONS,
416 NULL, on_complete_get_result, &result);
417 g_assert (result == NULL);
421 service = secret_service_open_finish (result, &error);
422 g_assert_no_error (error);
423 g_object_unref (result);
424 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
426 g_assert_cmpuint (secret_service_get_flags (service), ==, SECRET_SERVICE_LOAD_COLLECTIONS);
427 path = secret_service_get_session_dbus_path (service);
428 g_assert (path == NULL);
430 collections = secret_service_get_collections (service);
431 g_assert (collections != NULL);
432 g_list_free_full (collections, g_object_unref);
434 g_object_unref (service);
435 g_assert (service == NULL);
439 test_connect_async (Test *test,
442 GError *error = NULL;
443 GAsyncResult *result = NULL;
444 SecretService *service;
447 /* Passing false, not session */
448 secret_service_get (SECRET_SERVICE_NONE, NULL, on_complete_get_result, &result);
449 g_assert (result == NULL);
453 service = secret_service_get_finish (result, &error);
454 g_assert (SECRET_IS_SERVICE (service));
455 g_assert_no_error (error);
456 g_object_unref (result);
457 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
459 path = secret_service_get_session_dbus_path (service);
460 g_assert (path == NULL);
462 g_object_unref (service);
463 secret_service_disconnect ();
464 g_assert (service == NULL);
468 test_connect_ensure_async (Test *test,
471 GError *error = NULL;
472 GAsyncResult *result = NULL;
473 SecretService *service;
476 /* Passing true, ensures session is established */
477 secret_service_get (SECRET_SERVICE_OPEN_SESSION, NULL, on_complete_get_result, &result);
478 g_assert (result == NULL);
482 service = secret_service_get_finish (result, &error);
483 g_assert_no_error (error);
484 g_assert (SECRET_IS_SERVICE (service));
485 g_object_unref (result);
486 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
488 path = secret_service_get_session_dbus_path (service);
489 g_assert (path != NULL);
491 g_object_unref (service);
492 secret_service_disconnect ();
493 g_assert (service == NULL);
497 test_ensure_sync (Test *test,
500 GError *error = NULL;
501 SecretService *service;
502 SecretServiceFlags flags;
505 /* Passing true, ensures session is established */
506 service = secret_service_open_sync (SECRET_TYPE_SERVICE, NULL,
507 SECRET_SERVICE_NONE, NULL, &error);
508 g_assert_no_error (error);
509 g_assert (service != NULL);
510 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
512 flags = secret_service_get_flags (service);
513 g_assert_cmpuint (flags, ==, SECRET_SERVICE_NONE);
515 ret = secret_service_load_collections_sync (service, NULL, &error);
516 g_assert_no_error (error);
517 g_assert (ret == TRUE);
519 g_object_get (service, "flags", &flags, NULL);
520 g_assert_cmpuint (flags, ==, SECRET_SERVICE_LOAD_COLLECTIONS);
522 ret = secret_service_ensure_session_sync (service, NULL, &error);
523 g_assert_no_error (error);
524 g_assert (ret == TRUE);
526 flags = secret_service_get_flags (service);
527 g_assert_cmpuint (flags, ==, SECRET_SERVICE_OPEN_SESSION | SECRET_SERVICE_LOAD_COLLECTIONS);
529 g_object_unref (service);
530 g_assert (service == NULL);
534 test_ensure_async (Test *test,
537 GAsyncResult *result = NULL;
538 SecretServiceFlags flags;
539 SecretService *service;
540 GError *error = NULL;
543 /* Passing true, ensures session is established */
544 service = secret_service_open_sync (SECRET_TYPE_SERVICE, NULL,
545 SECRET_SERVICE_NONE, NULL, &error);
546 g_assert_no_error (error);
547 g_assert (service != NULL);
549 flags = secret_service_get_flags (service);
550 g_assert_cmpuint (flags, ==, SECRET_SERVICE_NONE);
552 secret_service_load_collections (service, NULL, on_complete_get_result, &result);
553 g_assert (result == NULL);
557 ret = secret_service_load_collections_finish (service, result, &error);
558 g_assert_no_error (error);
559 g_assert (ret == TRUE);
560 g_object_unref (result);
563 g_object_get (service, "flags", &flags, NULL);
564 g_assert_cmpuint (flags, ==, SECRET_SERVICE_LOAD_COLLECTIONS);
566 secret_service_ensure_session (service, NULL, on_complete_get_result, &result);
567 g_assert (result == NULL);
571 ret = secret_service_ensure_session_finish (service, result, &error);
572 g_assert_no_error (error);
573 g_assert (ret == TRUE);
574 g_object_unref (result);
576 g_object_add_weak_pointer (G_OBJECT (service), (gpointer *)&service);
578 flags = secret_service_get_flags (service);
579 g_assert_cmpuint (flags, ==, SECRET_SERVICE_OPEN_SESSION | SECRET_SERVICE_LOAD_COLLECTIONS);
581 g_object_unref (service);
582 g_assert (service == NULL);
586 main (int argc, char **argv)
588 g_test_init (&argc, &argv, NULL);
589 g_set_prgname ("test-service");
590 #if !GLIB_CHECK_VERSION(2,35,0)
594 g_test_add ("/service/get-sync", Test, "mock-service-normal.py", setup_mock, test_get_sync, teardown_mock);
595 g_test_add ("/service/get-async", Test, "mock-service-normal.py", setup_mock, test_get_async, teardown_mock);
596 g_test_add ("/service/get-more-sync", Test, "mock-service-normal.py", setup_mock, test_get_more_sync, teardown_mock);
597 g_test_add ("/service/get-more-async", Test, "mock-service-normal.py", setup_mock, test_get_more_async, teardown_mock);
599 g_test_add ("/service/open-sync", Test, "mock-service-normal.py", setup_mock, test_open_sync, teardown_mock);
600 g_test_add ("/service/open-async", Test, "mock-service-normal.py", setup_mock, test_open_async, teardown_mock);
601 g_test_add ("/service/open-more-sync", Test, "mock-service-normal.py", setup_mock, test_open_more_sync, teardown_mock);
602 g_test_add ("/service/open-more-async", Test, "mock-service-normal.py", setup_mock, test_open_more_async, teardown_mock);
604 g_test_add ("/service/connect-sync", Test, "mock-service-normal.py", setup_mock, test_connect_async, teardown_mock);
605 g_test_add ("/service/connect-ensure-sync", Test, "mock-service-normal.py", setup_mock, test_connect_ensure_async, teardown_mock);
606 g_test_add ("/service/ensure-sync", Test, "mock-service-normal.py", setup_mock, test_ensure_sync, teardown_mock);
607 g_test_add ("/service/ensure-async", Test, "mock-service-normal.py", setup_mock, test_ensure_async, teardown_mock);
609 return egg_tests_run_with_loop ();