Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-secret-portal.c: Test secret portal
3 :
4 : Copyright (C) 2013-2019 Red Hat, Inc
5 :
6 : The Gnome Keyring Library is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public License as
8 : published by the Free Software Foundation; either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : The Gnome Keyring Library is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public
17 : License along with the Gnome Library; see the file COPYING.LIB. If not,
18 : <http://www.gnu.org/licenses/>.
19 :
20 : Author: Stef Walter <stefw@gnome.org>, Daiki Ueno
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "test-service.h"
26 :
27 : #include "gkd-secret-types.h"
28 :
29 : #include "egg/egg-testing.h"
30 :
31 : #include <gcr/gcr-base.h>
32 : #include <glib.h>
33 : #include <glib/gstdio.h>
34 : #include <gio/gio.h>
35 : #include <glib-unix.h>
36 : #include <gio/gunixfdlist.h>
37 : #include <gio/gunixinputstream.h>
38 : #include <fcntl.h>
39 :
40 : typedef struct {
41 : TestService service;
42 : guint8 buffer[128];
43 : gsize bytes_read;
44 : } Test;
45 :
46 : static void
47 2 : setup (Test *test,
48 : gconstpointer unused)
49 : {
50 : GVariant *retval;
51 2 : GError *error = NULL;
52 :
53 2 : test->service.mock_prompter = gcr_mock_prompter_start ();
54 2 : g_assert (test->service.mock_prompter != NULL);
55 :
56 2 : test_service_setup (&test->service);
57 :
58 : /* Unlock the test collection */
59 2 : retval = g_dbus_connection_call_sync (test->service.connection,
60 2 : test->service.bus_name,
61 : SECRET_SERVICE_PATH,
62 : INTERNAL_SERVICE_INTERFACE,
63 : "UnlockWithMasterPassword",
64 : g_variant_new ("(o@(oayays))",
65 : "/org/freedesktop/secrets/collection/test",
66 : test_service_build_secret (&test->service, "booo")),
67 : G_VARIANT_TYPE ("()"),
68 : G_DBUS_CALL_FLAGS_NO_AUTO_START,
69 : -1, NULL, &error);
70 2 : g_assert_no_error (error);
71 2 : g_variant_unref (retval);
72 2 : }
73 :
74 : static void
75 2 : teardown (Test *test,
76 : gconstpointer unused)
77 : {
78 2 : test_service_teardown (&test->service);
79 :
80 2 : gcr_mock_prompter_stop ();
81 2 : }
82 :
83 : static void
84 1 : setup_locked (Test *test,
85 : gconstpointer unused)
86 : {
87 : GVariant *element;
88 : GVariant *retval;
89 1 : GError *error = NULL;
90 : const gchar *prompt;
91 : GVariant *locked;
92 :
93 : /* Main setup */
94 1 : setup (test, unused);
95 :
96 1 : element = g_variant_new_object_path ("/org/freedesktop/secrets/collection/test");
97 1 : retval = g_dbus_connection_call_sync (test->service.connection,
98 1 : test->service.bus_name,
99 : SECRET_SERVICE_PATH,
100 : SECRET_SERVICE_INTERFACE,
101 : "Lock",
102 : g_variant_new ("(@ao)",
103 : g_variant_new_array (G_VARIANT_TYPE ("o"), &element, 1)),
104 : G_VARIANT_TYPE ("(aoo)"),
105 : G_DBUS_CALL_FLAGS_NO_AUTO_START,
106 : -1,
107 : NULL,
108 : &error);
109 1 : g_assert_no_error (error);
110 :
111 : /* Not expecting a prompt */
112 1 : g_variant_get (retval, "(@ao&o)", &locked, &prompt);
113 1 : g_assert_cmpstr (prompt, ==, "/");
114 1 : g_variant_unref (locked);
115 1 : g_variant_unref (retval);
116 1 : }
117 :
118 : static void
119 4 : call_retrieve_secret (Test *test)
120 : {
121 : GUnixFDList *fd_list;
122 : gint fds[2];
123 : gint fd_index;
124 4 : GError *error = NULL;
125 : gboolean ret;
126 4 : GVariant *reply = NULL;
127 4 : GInputStream *stream = NULL;
128 : GVariantBuilder options;
129 :
130 4 : ret = g_unix_open_pipe (fds, FD_CLOEXEC, &error);
131 4 : g_assert_true (ret);
132 4 : g_assert_no_error (error);
133 :
134 4 : fd_list = g_unix_fd_list_new ();
135 4 : fd_index = g_unix_fd_list_append (fd_list, fds[1], &error);
136 4 : g_assert_no_error (error);
137 4 : close (fds[1]);
138 :
139 4 : g_variant_builder_init (&options, G_VARIANT_TYPE ("a{sv}"));
140 4 : reply = g_dbus_connection_call_with_unix_fd_list_sync (test->service.connection,
141 4 : test->service.bus_name,
142 : PORTAL_SERVICE_PATH,
143 : PORTAL_SERVICE_INTERFACE,
144 : "RetrieveSecret",
145 : g_variant_new ("(osh@a{sv})",
146 : "/org/gnome/keyring/Portal/Request",
147 : "org.gnome.keyring.Test",
148 : fd_index,
149 : g_variant_builder_end (&options)),
150 : G_VARIANT_TYPE ("(ua{sv})"),
151 : G_DBUS_CALL_FLAGS_NONE,
152 : 30000,
153 : fd_list, NULL,
154 : NULL,
155 : &error);
156 4 : g_object_unref (fd_list);
157 4 : g_assert_no_error (error);
158 4 : g_assert_nonnull (reply);
159 :
160 4 : stream = g_unix_input_stream_new (fds[0], TRUE);
161 4 : ret = g_input_stream_read_all (stream, test->buffer, sizeof(test->buffer),
162 : &test->bytes_read, NULL, &error);
163 4 : g_assert_no_error (error);
164 4 : g_assert_true (ret);
165 :
166 4 : g_object_unref (stream);
167 4 : }
168 :
169 : static void
170 1 : test_portal_retrieve_secret (Test *test,
171 : gconstpointer unused)
172 : {
173 : guint8 buffer[128];
174 : gsize bytes_read;
175 :
176 1 : call_retrieve_secret (test);
177 1 : memcpy (buffer, test->buffer, sizeof(test->buffer));
178 1 : bytes_read = test->bytes_read;
179 :
180 1 : call_retrieve_secret (test);
181 1 : g_assert_cmpmem (buffer, bytes_read, test->buffer, test->bytes_read);
182 1 : }
183 :
184 : static void
185 1 : test_portal_retrieve_secret_locked (Test *test,
186 : gconstpointer unused)
187 : {
188 : guint8 buffer[128];
189 : gsize bytes_read;
190 :
191 1 : gcr_mock_prompter_expect_password_ok ("booo", NULL);
192 :
193 1 : call_retrieve_secret (test);
194 1 : memcpy (buffer, test->buffer, sizeof(test->buffer));
195 1 : bytes_read = test->bytes_read;
196 :
197 1 : call_retrieve_secret (test);
198 1 : g_assert_cmpmem (buffer, bytes_read, test->buffer, test->bytes_read);
199 1 : }
200 :
201 : int
202 1 : main (int argc, char **argv)
203 : {
204 : #if !GLIB_CHECK_VERSION(2,35,0)
205 : g_type_init ();
206 : #endif
207 1 : g_test_init (&argc, &argv, NULL);
208 :
209 1 : g_test_add ("/secret-portal/portal-retrieve-secret", Test, NULL,
210 : setup, test_portal_retrieve_secret, teardown);
211 1 : g_test_add ("/secret-portal/portal-retrieve-secret-locked", Test, NULL,
212 : setup_locked, test_portal_retrieve_secret_locked, teardown);
213 :
214 1 : return egg_tests_run_with_loop ();
215 : }
|