Line data Source code
1 : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : /* test-service.c: Common service code
3 :
4 : Copyright (C) 2013 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>
21 : */
22 :
23 : #include "config.h"
24 :
25 : #include "test-service.h"
26 :
27 : #include "gkd-secret-types.h"
28 :
29 : #include "daemon/gkd-test.h"
30 :
31 : #include "egg/egg-testing.h"
32 :
33 : #include <glib/gstdio.h>
34 :
35 : #include <fcntl.h>
36 :
37 : static void
38 16 : on_test_service_appeared (GDBusConnection *connection,
39 : const gchar *name,
40 : const gchar *name_owner,
41 : gpointer user_data)
42 : {
43 16 : TestService *test = user_data;
44 16 : if (!test->connection)
45 16 : test->connection = g_object_ref (connection);
46 16 : test->available = TRUE;
47 16 : egg_test_wait_stop ();
48 16 : }
49 :
50 : static void
51 16 : on_test_service_vanished (GDBusConnection *connection,
52 : const gchar *name,
53 : gpointer user_data)
54 : {
55 16 : TestService *test = user_data;
56 16 : if (test->available) {
57 16 : test->available = FALSE;
58 16 : egg_test_wait_stop ();
59 : }
60 16 : }
61 :
62 : void
63 16 : test_service_setup (TestService *test)
64 : {
65 16 : GError *error = NULL;
66 : GVariant *retval;
67 : GVariant *output;
68 : gchar **env;
69 :
70 16 : const gchar *args[] = {
71 : BUILDDIR "/gnome-keyring-daemon",
72 : "--foreground",
73 : "--control-directory",
74 : "/tmp/keyring-test",
75 : "--components",
76 : "secrets",
77 : NULL,
78 : };
79 :
80 16 : test->bus_name = g_strdup_printf ("org.gnome.keyring.Test.t%d", getpid ());
81 :
82 16 : test->watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION, test->bus_name,
83 : G_BUS_NAME_WATCHER_FLAGS_NONE,
84 : on_test_service_appeared,
85 : on_test_service_vanished,
86 : test, NULL);
87 :
88 16 : test->directory = egg_tests_create_scratch_directory (
89 : SRCDIR "/daemon/dbus/fixtures/test.keyring",
90 : NULL);
91 :
92 16 : env = gkd_test_launch_daemon (test->directory, args, &test->pid,
93 : "GNOME_KEYRING_TEST_SERVICE", test->bus_name,
94 : "GNOME_KEYRING_TEST_LOGIN", "test",
95 16 : test->mock_prompter ? "GNOME_KEYRING_TEST_PROMPTER" : NULL, test->mock_prompter,
96 : NULL);
97 16 : g_strfreev (env);
98 :
99 16 : if (!test->available) {
100 16 : egg_test_wait ();
101 :
102 16 : if (!test->available) {
103 0 : g_warning ("Couldn't start gnome-keyring-daemon test service. ");
104 0 : g_assert_not_reached ();
105 : }
106 : }
107 :
108 : /* Set by on_test_service_appeared */
109 16 : g_assert (test->connection != NULL);
110 :
111 : /* Establish a plain session with the daemon */
112 16 : retval = g_dbus_connection_call_sync (test->connection,
113 16 : test->bus_name,
114 : SECRET_SERVICE_PATH,
115 : SECRET_SERVICE_INTERFACE,
116 : "OpenSession",
117 : g_variant_new ("(s@v)", "plain",
118 : g_variant_new_variant (g_variant_new_string (""))),
119 : G_VARIANT_TYPE ("(vo)"),
120 : G_DBUS_CALL_FLAGS_NO_AUTO_START,
121 : -1, NULL, &error);
122 16 : g_assert_no_error (error);
123 :
124 16 : g_variant_get (retval, "(@vo)", &output, &test->session);
125 16 : g_variant_unref (output);
126 16 : g_variant_unref (retval);
127 16 : }
128 :
129 : void
130 16 : test_service_teardown (TestService *test)
131 : {
132 16 : if (test->pid)
133 16 : kill (test->pid, SIGTERM);
134 :
135 16 : if (test->available) {
136 16 : egg_test_wait ();
137 16 : if (test->available) {
138 0 : g_warning ("Couldn't stop gnome-keyring-daemon test service.");
139 0 : g_assert_not_reached ();
140 : }
141 : }
142 :
143 16 : if (test->watch_id)
144 16 : g_bus_unwatch_name (test->watch_id);
145 :
146 16 : g_free (test->bus_name);
147 16 : g_free (test->session);
148 :
149 16 : if (test->connection)
150 16 : g_object_unref (test->connection);
151 :
152 16 : egg_tests_remove_scratch_directory (test->directory);
153 16 : g_free (test->directory);
154 16 : }
155 :
156 :
157 : GVariant *
158 18 : test_service_build_secret (TestService *test,
159 : const gchar *value)
160 : {
161 18 : return g_variant_new ("(o@ay@ays)", test->session,
162 : g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, "", 0, 1),
163 : g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, value, strlen (value), 1),
164 : "text/plain");
165 : }
|