Valgrind integration, refactor build process
[platform/upstream/libsecret.git] / library / tests / test-session.c
1 /* GSecret - GLib wrapper for Secret Service
2  *
3  * Copyright 2011 Collabora Ltd.
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
13
14 #include "config.h"
15
16 #include "gsecret-item.h"
17 #include "gsecret-service.h"
18 #include "gsecret-private.h"
19
20 #include "egg/egg-testing.h"
21
22 #include <glib.h>
23
24 #include <errno.h>
25 #include <stdlib.h>
26
27 static gchar *MOCK_NAME = "org.mock.Service";
28
29 typedef struct {
30         GPid pid;
31         GDBusConnection *connection;
32         GSecretService *service;
33 } Test;
34
35 static void
36 setup (Test *test,
37        gconstpointer data)
38 {
39         GError *error = NULL;
40         const gchar *mock_script = data;
41         gchar *argv[] = {
42                 "python", (gchar *)mock_script,
43                 "--name", MOCK_NAME,
44                 NULL
45         };
46
47         g_spawn_async (SRCDIR, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &test->pid, &error);
48         g_assert_no_error (error);
49         g_usleep (200 * 1000);
50
51         test->connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
52         g_assert_no_error (error);
53
54         test->service = _gsecret_service_bare_instance (test->connection, MOCK_NAME);
55 }
56
57 static void
58 teardown (Test *test,
59           gconstpointer unused)
60 {
61         GError *error = NULL;
62
63         g_clear_object (&test->service);
64
65         g_assert (test->pid);
66         if (kill (test->pid, SIGTERM) < 0)
67                 g_error ("kill() failed: %s", g_strerror (errno));
68         g_spawn_close_pid (test->pid);
69
70         g_dbus_connection_flush_sync (test->connection, NULL, &error);
71         g_assert_no_error (error);
72         g_clear_object (&test->connection);
73 }
74
75 static void
76 test_ensure (Test *test,
77              gconstpointer unused)
78 {
79         GError *error = NULL;
80         const gchar *path;
81
82         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, NULL);
83         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, NULL);
84
85         path = gsecret_service_ensure_session_sync (test->service, NULL, &error);
86         g_assert_no_error (error);
87         g_assert (path != NULL);
88         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
89         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "dh-ietf1024-sha256-aes128-cbc-pkcs7");
90 }
91
92 static void
93 test_ensure_twice (Test *test,
94                    gconstpointer unused)
95 {
96         GError *error = NULL;
97         const gchar *path;
98
99         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, NULL);
100         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, NULL);
101
102         path = gsecret_service_ensure_session_sync (test->service, NULL, &error);
103         g_assert_no_error (error);
104         g_assert (path != NULL);
105         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
106         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "dh-ietf1024-sha256-aes128-cbc-pkcs7");
107
108         path = gsecret_service_ensure_session_sync (test->service, NULL, &error);
109         g_assert_no_error (error);
110         g_assert (path != NULL);
111         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
112         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "dh-ietf1024-sha256-aes128-cbc-pkcs7");
113 }
114
115 static void
116 test_ensure_plain (Test *test,
117                    gconstpointer unused)
118 {
119         GError *error = NULL;
120         const gchar *path;
121
122         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, NULL);
123         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, NULL);
124
125         path = gsecret_service_ensure_session_sync (test->service, NULL, &error);
126         g_assert_no_error (error);
127
128         g_assert (path != NULL);
129         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
130         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "plain");
131 }
132
133 static void
134 on_complete_get_result (GObject *source,
135                         GAsyncResult *result,
136                         gpointer user_data)
137 {
138         GAsyncResult **ret = user_data;
139         g_assert (ret != NULL);
140         g_assert (*ret == NULL);
141         *ret = g_object_ref (result);
142 }
143
144 static void
145 test_ensure_async_plain (Test *test,
146                          gconstpointer unused)
147 {
148         GAsyncResult *result = NULL;
149         GError *error = NULL;
150         const gchar *path;
151
152         gsecret_service_ensure_session (test->service, NULL, on_complete_get_result, &result);
153         egg_test_wait_until (500);
154
155         g_assert (G_IS_ASYNC_RESULT (result));
156         path = gsecret_service_ensure_session_finish (test->service, result, &error);
157         g_assert_no_error (error);
158
159         g_assert (path != NULL);
160         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
161         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "plain");
162
163         g_object_unref (result);
164 }
165
166 static void
167 test_ensure_async_aes (Test *test,
168                        gconstpointer unused)
169 {
170         GAsyncResult *result = NULL;
171         GError *error = NULL;
172         const gchar *path;
173
174         gsecret_service_ensure_session (test->service, NULL, on_complete_get_result, &result);
175         egg_test_wait_until (500);
176
177         g_assert (G_IS_ASYNC_RESULT (result));
178         path = gsecret_service_ensure_session_finish (test->service, result, &error);
179         g_assert_no_error (error);
180
181         g_assert (path != NULL);
182         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
183         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "dh-ietf1024-sha256-aes128-cbc-pkcs7");
184
185         g_object_unref (result);
186 }
187
188 static void
189 test_ensure_async_twice (Test *test,
190                          gconstpointer unused)
191 {
192         GAsyncResult *result = NULL;
193         GError *error = NULL;
194         const gchar *path;
195
196         gsecret_service_ensure_session (test->service, NULL, on_complete_get_result, &result);
197         egg_test_wait_until (500);
198
199         g_assert (G_IS_ASYNC_RESULT (result));
200         path = gsecret_service_ensure_session_finish (test->service, result, &error);
201         g_assert_no_error (error);
202
203         g_assert (path != NULL);
204         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
205         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "plain");
206
207         g_object_unref (result);
208         result = NULL;
209
210         gsecret_service_ensure_session (test->service, NULL, on_complete_get_result, &result);
211         egg_test_wait_until (500);
212
213         g_assert (G_IS_ASYNC_RESULT (result));
214         path = gsecret_service_ensure_session_finish (test->service, result, &error);
215         g_assert_no_error (error);
216
217         g_assert (path != NULL);
218         g_assert_cmpstr (gsecret_service_get_session_path (test->service), ==, path);
219         g_assert_cmpstr (gsecret_service_get_session_algorithms (test->service), ==, "plain");
220
221         g_object_unref (result);
222 }
223
224 int
225 main (int argc, char **argv)
226 {
227         g_test_init (&argc, &argv, NULL);
228         g_set_prgname ("test-session");
229         g_type_init ();
230
231         g_test_add ("/session/ensure-aes", Test, "mock-service-normal.py", setup, test_ensure, teardown);
232         g_test_add ("/session/ensure-twice", Test, "mock-service-normal.py", setup, test_ensure_twice, teardown);
233         g_test_add ("/session/ensure-plain", Test, "mock-service-only-plain.py", setup, test_ensure_plain, teardown);
234         g_test_add ("/session/ensure-async-aes", Test, "mock-service-normal.py", setup, test_ensure_async_aes, teardown);
235         g_test_add ("/session/ensure-async-plain", Test, "mock-service-only-plain.py", setup, test_ensure_async_plain, teardown);
236         g_test_add ("/session/ensure-async-twice", Test, "mock-service-only-plain.py", setup, test_ensure_async_twice, teardown);
237
238         return egg_tests_run_with_loop ();
239 }