summaryrefslogtreecommitdiff
path: root/daemon/dbus/tests/test-service.c
blob: 98c545f5945cd1b3e2a7530a39aaa645da7a05f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* test-service.c: Common service code

   Copyright (C) 2013 Red Hat Inc

   The Gnome Keyring Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   The Gnome Keyring Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with the Gnome Library; see the file COPYING.LIB.  If not,
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.

   Author: Stef Walter <stefw@gnome.org>
*/

#include "config.h"

#include "test-service.h"

#include "gkd-secret-types.h"

#include "egg/egg-testing.h"

#include <gcr/gcr-base.h>

#include <glib/gstdio.h>

#include <fcntl.h>

static void
on_test_service_appeared (GDBusConnection *connection,
                          const gchar *name,
                          const gchar *name_owner,
                          gpointer user_data)
{
	TestService *test = user_data;
	if (!test->connection)
		test->connection = g_object_ref (connection);
	test->available = TRUE;
	egg_test_wait_stop ();
}

static void
on_test_service_vanished (GDBusConnection *connection,
                          const gchar *name,
                          gpointer user_data)
{
	TestService *test = user_data;
	if (test->available) {
		test->available = FALSE;
		egg_test_wait_stop ();
	}
}

static void
on_service_spawned (gpointer user_data)
{
	TestService *test = user_data;
	int fd;

	g_setenv ("GNOME_KEYRING_TEST_PATH", test->directory, TRUE);
	g_setenv ("GNOME_KEYRING_TEST_SERVICE", test->bus_name, TRUE);
	if (test->mock_prompter)
		g_setenv ("GNOME_KEYRING_TEST_PROMPTER", test->mock_prompter, TRUE);

	fd = g_open ("/dev/null", O_WRONLY, 0);
	if (fd != -1)
		dup2 (fd, 1);
}

void
test_service_setup (TestService *test)
{
	GError *error = NULL;
	GVariant *retval;
	GVariant *output;

	gchar *args[] = {
		TOP_BUILDDIR "/daemon/gnome-keyring-daemon",
		"--foreground",
		"--control-directory",
		"/tmp/keyring-test",
		"--components",
		"secrets",
		NULL,
	};

	test->bus_name = g_strdup_printf ("org.gnome.keyring.Test.t%d", getpid ());

	test->watch_id = g_bus_watch_name (G_BUS_TYPE_SESSION, test->bus_name,
	                                   G_BUS_NAME_WATCHER_FLAGS_NONE,
	                                   on_test_service_appeared,
	                                   on_test_service_vanished,
	                                   test, NULL);

	test->directory = egg_tests_create_scratch_directory (
		SRCDIR "/files/test.keyring",
		NULL);

	if (!g_spawn_async (NULL, args, NULL,
	                    G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_DO_NOT_REAP_CHILD,
	                    on_service_spawned, test, &test->pid, &error)) {
		g_error ("couldn't start gnome-keyring-daemon for testing: %s", error->message);
		g_assert_not_reached ();
	}

	if (!test->available) {
		egg_test_wait ();

		if (!test->available) {
			g_warning ("Couldn't start gnome-keyring-daemon test service. ");
			g_assert_not_reached ();
		}
	}

	/* Set by on_test_service_appeared */
	g_assert (test->connection != NULL);

	/* Establish a plain session with the daemon */
	retval = g_dbus_connection_call_sync (test->connection,
	                                      test->bus_name,
	                                      SECRET_SERVICE_PATH,
	                                      SECRET_SERVICE_INTERFACE,
	                                      "OpenSession",
	                                      g_variant_new ("(s@v)", "plain",
	                                                     g_variant_new_variant (g_variant_new_string (""))),
	                                      G_VARIANT_TYPE ("(vo)"),
	                                      G_DBUS_CALL_FLAGS_NO_AUTO_START,
	                                      -1, NULL, &error);
	g_assert_no_error (error);

	g_variant_get (retval, "(@vo)", &output, &test->session);
	g_variant_unref (output);
	g_variant_unref (retval);
}

void
test_service_teardown (TestService *test)
{
	if (test->pid)
		kill (test->pid, SIGTERM);

	if (test->available) {
		egg_test_wait ();
		if (test->available) {
			g_warning ("Couldn't stop gnome-keyring-daemon test service.");
			g_assert_not_reached ();
		}
	}

	if (test->watch_id)
		g_bus_unwatch_name (test->watch_id);

	g_free (test->bus_name);
	g_free (test->session);

	if (test->connection)
		g_object_unref (test->connection);

	egg_tests_remove_scratch_directory (test->directory);
	g_free (test->directory);
}


GVariant *
test_service_build_secret (TestService *test,
                           const gchar *value)
{
	return g_variant_new ("(o@ay@ays)", test->session,
	                      g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, "", 0, 1),
	                      g_variant_new_fixed_array (G_VARIANT_TYPE_BYTE, value, strlen (value), 1),
	                      "text/plain");
}