collection: Have gobject-introspection and glib-mkenums recognize flags
[platform/upstream/libsecret.git] / libsecret / tests / test-prompt.c
1 /* libsecret - GLib wrapper for Secret Service
2  *
3  * Copyright 2011 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
16 #include "config.h"
17
18 #include "secret-item.h"
19 #include "secret-service.h"
20 #include "secret-private.h"
21 #include "secret-prompt.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 typedef struct {
33         SecretService *service;
34 } Test;
35
36 static void
37 setup (Test *test,
38        gconstpointer data)
39 {
40         GError *error = NULL;
41         const gchar *mock_script = data;
42
43         mock_service_start (mock_script, &error);
44         g_assert_no_error (error);
45
46         test->service = secret_service_get_sync (SECRET_SERVICE_NONE, NULL, &error);
47         g_assert_no_error (error);
48 }
49
50 static void
51 teardown (Test *test,
52           gconstpointer unused)
53 {
54         g_object_unref (test->service);
55         secret_service_disconnect ();
56         egg_assert_not_object (test->service);
57
58         mock_service_stop ();
59 }
60
61 static void
62 on_async_result (GObject *source,
63                  GAsyncResult *result,
64                  gpointer user_data)
65 {
66         GAsyncResult **ret = user_data;
67         g_assert (ret != NULL);
68         g_assert (*ret == NULL);
69         *ret = g_object_ref (result);
70         egg_test_wait_stop ();
71 }
72
73 static gboolean
74 on_idle_increment (gpointer user_data)
75 {
76         guint *value = user_data;
77         ++(*value);
78         return TRUE;
79 }
80
81 static void
82 test_perform_sync (Test *test,
83                    gconstpointer unused)
84 {
85         SecretPrompt *prompt;
86         GError *error = NULL;
87         GVariant *retval;
88         guint value = 0;
89         guint increment_id;
90
91         /* Verify that main loop does not run during this call */
92         increment_id = g_idle_add (on_idle_increment, &value);
93
94         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
95
96         retval = secret_prompt_perform_sync (prompt, 0, NULL, NULL, &error);
97         g_assert_no_error (error);
98         g_assert (retval != NULL);
99         g_variant_unref (retval);
100
101         g_assert_cmpuint (value, ==, 0);
102         g_source_remove (increment_id);
103
104         g_object_unref (prompt);
105         egg_assert_not_object (prompt);
106 }
107
108 static void
109 test_perform_run (Test *test,
110                   gconstpointer unused)
111 {
112         SecretPrompt *prompt;
113         GError *error = NULL;
114         GVariant *retval;
115         guint value = 0;
116         guint increment_id;
117
118         /* Verify that main loop does run during this call */
119         increment_id = g_idle_add (on_idle_increment, &value);
120
121         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
122
123         retval = secret_prompt_run (prompt, 0, NULL, NULL, &error);
124         g_assert_no_error (error);
125         g_assert (retval != NULL);
126         g_variant_unref (retval);
127
128         g_assert_cmpuint (value, >, 0);
129         g_source_remove (increment_id);
130
131         /* Make sure everything completes */
132         egg_test_wait_idle ();
133
134         g_object_unref (prompt);
135         egg_assert_not_object (prompt);
136 }
137
138 static void
139 test_perform_async (Test *test,
140                     gconstpointer unused)
141 {
142         SecretPrompt *prompt;
143         GError *error = NULL;
144         GAsyncResult *result = NULL;
145         GVariant *retval;
146
147         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
148
149         secret_prompt_perform (prompt, 0, NULL, on_async_result, &result);
150         g_assert (result == NULL);
151
152         egg_test_wait ();
153
154         retval = secret_prompt_perform_finish (prompt, result, NULL, &error);
155         g_assert_no_error (error);
156         g_assert (retval != NULL);
157         g_variant_unref (retval);
158         g_object_unref (result);
159
160         /* Make sure everything completes */
161         egg_test_wait_idle ();
162
163         g_object_unref (prompt);
164         egg_assert_not_object (prompt);
165 }
166
167 static void
168 test_perform_cancel (Test *test,
169                      gconstpointer unused)
170 {
171         SecretPrompt *prompt;
172         GError *error = NULL;
173         GAsyncResult *result = NULL;
174         GCancellable *cancellable;
175         GVariant *retval;
176
177         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/delay");
178
179         cancellable = g_cancellable_new ();
180         secret_prompt_perform (prompt, 0, cancellable, on_async_result, &result);
181         g_assert (result == NULL);
182
183         g_cancellable_cancel (cancellable);
184         g_object_unref (cancellable);
185
186         egg_test_wait ();
187
188         retval = secret_prompt_perform_finish (prompt, result, NULL, &error);
189         g_assert_no_error (error);
190         g_assert (retval != NULL);
191         g_variant_unref (retval);
192
193         g_object_unref (result);
194         g_object_unref (prompt);
195
196         /* Due to GDBus threading races */
197         egg_test_wait_until (100);
198
199         egg_assert_not_object (prompt);
200 }
201
202 static void
203 test_perform_fail (Test *test,
204                    gconstpointer unused)
205 {
206         SecretPrompt *prompt;
207         GError *error = NULL;
208         GVariant *retval;
209
210         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/error");
211
212         retval = secret_prompt_perform_sync (prompt, 0, NULL, NULL, &error);
213         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED);
214         g_assert (retval == NULL);
215
216         g_object_unref (prompt);
217         egg_assert_not_object (prompt);
218 }
219
220 static void
221 test_perform_vanish (Test *test,
222                      gconstpointer unused)
223 {
224         SecretPrompt *prompt;
225         GError *error = NULL;
226         GVariant *retval;
227
228         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/vanish");
229
230         retval = secret_prompt_perform_sync (prompt, 0, NULL, NULL, &error);
231         g_assert_no_error (error);
232         g_assert (retval == NULL);
233
234         g_object_unref (prompt);
235         egg_assert_not_object (prompt);
236 }
237
238 static void
239 test_prompt_result (Test *test,
240                     gconstpointer unused)
241 {
242         SecretPrompt *prompt;
243         GError *error = NULL;
244         GVariant *retval;
245
246         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/result");
247
248         retval = secret_prompt_perform_sync (prompt, 0, NULL, G_VARIANT_TYPE_STRING, &error);
249         g_assert_no_error (error);
250         g_assert (retval != NULL);
251         g_assert_cmpstr (g_variant_get_string (retval, NULL), ==, "Special Result");
252         g_variant_unref (retval);
253
254         g_object_unref (prompt);
255         egg_assert_not_object (prompt);
256 }
257
258 static void
259 test_prompt_window_id (Test *test,
260                        gconstpointer unused)
261 {
262         SecretPrompt *prompt;
263         GError *error = NULL;
264         GVariant *retval;
265
266         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/window");
267
268         retval = secret_prompt_perform_sync (prompt, 555, NULL, G_VARIANT_TYPE_STRING, &error);
269         g_assert_no_error (error);
270         g_assert (retval != NULL);
271         g_assert_cmpstr (g_variant_get_string (retval, NULL), ==, "555");
272         g_variant_unref (retval);
273
274         g_object_unref (prompt);
275         egg_assert_not_object (prompt);
276 }
277
278 static void
279 test_service_sync (Test *test,
280                    gconstpointer unused)
281 {
282         SecretPrompt *prompt;
283         GError *error = NULL;
284         GVariant *retval;
285
286         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
287
288         retval = secret_service_prompt_sync (test->service, prompt, NULL, NULL, &error);
289         g_assert_no_error (error);
290         g_assert (retval != NULL);
291         g_variant_unref (retval);
292
293         g_object_unref (prompt);
294         egg_assert_not_object (prompt);
295 }
296
297 static void
298 test_service_async (Test *test,
299                     gconstpointer unused)
300 {
301         SecretPrompt *prompt;
302         GError *error = NULL;
303         GAsyncResult *result = NULL;
304         GVariant *retval;
305
306         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
307
308         secret_service_prompt (test->service, prompt, NULL, on_async_result, &result);
309         g_assert (result == NULL);
310
311         egg_test_wait ();
312
313         retval = secret_service_prompt_finish (test->service, result, NULL, &error);
314         g_assert_no_error (error);
315         g_assert (retval != NULL);
316         g_variant_unref (retval);
317         g_object_unref (result);
318
319         /* Make sure everything completes */
320         egg_test_wait_idle ();
321
322         g_object_unref (prompt);
323         egg_assert_not_object (prompt);
324 }
325
326 static void
327 test_service_fail (Test *test,
328                     gconstpointer unused)
329 {
330         SecretPrompt *prompt;
331         GError *error = NULL;
332         GAsyncResult *result = NULL;
333         GVariant *retval;
334
335         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/error");
336
337         secret_service_prompt (test->service, prompt, NULL, on_async_result, &result);
338         g_assert (result == NULL);
339
340         egg_test_wait ();
341
342         retval = secret_service_prompt_finish (test->service, result, NULL, &error);
343         g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_NOT_SUPPORTED);
344         g_assert (retval == NULL);
345         g_object_unref (result);
346
347         /* Make sure everything completes */
348         egg_test_wait_idle ();
349
350         g_object_unref (prompt);
351         egg_assert_not_object (prompt);
352 }
353
354 static void
355 test_service_path (Test *test,
356                     gconstpointer unused)
357 {
358         GError *error = NULL;
359         GAsyncResult *result = NULL;
360         SecretPrompt *prompt;
361         GVariant *retval;
362
363         prompt = _secret_prompt_instance (test->service, "/org/freedesktop/secrets/prompts/simple");
364
365         secret_service_prompt (test->service, prompt, NULL, on_async_result, &result);
366         g_assert (result == NULL);
367
368         g_object_unref (prompt);
369         egg_test_wait ();
370
371         retval = secret_service_prompt_finish (test->service, result, NULL, &error);
372         g_assert_no_error (error);
373         g_assert (retval != NULL);
374         g_variant_unref (retval);
375         g_object_unref (result);
376
377         /* Make sure everything completes */
378         egg_test_wait_idle ();
379 }
380
381 static void
382 null_log_handler (const gchar *log_domain, GLogLevelFlags log_level,
383                   const gchar *message, gpointer user_data)
384 {
385
386 }
387
388 int
389 main (int argc, char **argv)
390 {
391         g_test_init (&argc, &argv, NULL);
392         g_set_prgname ("test-prompt");
393 #if !GLIB_CHECK_VERSION(2,35,0)
394         g_type_init ();
395 #endif
396
397         /* Suppress these messages in tests */
398         g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG,
399                            null_log_handler, NULL);
400
401         g_test_add ("/prompt/run", Test, "mock-service-prompt.py", setup, test_perform_run, teardown);
402         g_test_add ("/prompt/perform-sync", Test, "mock-service-prompt.py", setup, test_perform_sync, teardown);
403         g_test_add ("/prompt/perform-async", Test, "mock-service-prompt.py", setup, test_perform_async, teardown);
404         g_test_add ("/prompt/perform-cancel", Test, "mock-service-prompt.py", setup, test_perform_cancel, teardown);
405         g_test_add ("/prompt/perform-fail", Test, "mock-service-prompt.py", setup, test_perform_fail, teardown);
406         g_test_add ("/prompt/perform-vanish", Test, "mock-service-prompt.py", setup, test_perform_vanish, teardown);
407         g_test_add ("/prompt/result", Test, "mock-service-prompt.py", setup, test_prompt_result, teardown);
408         g_test_add ("/prompt/window-id", Test, "mock-service-prompt.py", setup, test_prompt_window_id, teardown);
409
410         g_test_add ("/prompt/service-sync", Test, "mock-service-prompt.py", setup, test_service_sync, teardown);
411         g_test_add ("/prompt/service-async", Test, "mock-service-prompt.py", setup, test_service_async, teardown);
412         g_test_add ("/prompt/service-path", Test, "mock-service-prompt.py", setup, test_service_path, teardown);
413         g_test_add ("/prompt/service-fail", Test, "mock-service-prompt.py", setup, test_service_fail, teardown);
414
415         return egg_tests_run_with_loop ();
416 }