Line data Source code
1 : /*
2 : Copyright (C) 2014 Red Hat Inc
3 :
4 : The Gnome Keyring Library is free software; you can redistribute it and/or
5 : modify it under the terms of the GNU Library General Public License as
6 : published by the Free Software Foundation; either version 2 of the
7 : License, or (at your option) any later version.
8 :
9 : The Gnome Keyring Library is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : Library General Public License for more details.
13 :
14 : You should have received a copy of the GNU Library General Public
15 : License along with the Gnome Library; see the file COPYING.LIB. If not,
16 : <http://www.gnu.org/licenses/>.
17 :
18 : Author: Stef Walter <stefw@gnome.org>
19 : */
20 :
21 : #include "config.h"
22 :
23 : #include "gkd-test.h"
24 :
25 : #include "egg/egg-testing.h"
26 :
27 : #include <glib.h>
28 : #include <glib/gstdio.h>
29 : #include <gio/gio.h>
30 :
31 : #include <errno.h>
32 :
33 : gchar **
34 27 : gkd_test_launch_daemon (const gchar *directory,
35 : const gchar **argv,
36 : GPid *pid,
37 : ...)
38 : {
39 27 : GError *error = NULL;
40 : GString *output;
41 : gchar **env;
42 : gsize len;
43 : gssize ret;
44 : int out_fd;
45 : gchar **result;
46 : gchar *cmd;
47 : va_list va;
48 : const gchar *name;
49 : const gchar *value;
50 :
51 27 : env = g_get_environ ();
52 27 : env = g_environ_setenv (env, "GSETTINGS_SCHEMA_DIR", BUILDDIR "/schema", TRUE);
53 27 : env = g_environ_setenv (env, "GNOME_KEYRING_TEST_PATH", directory, TRUE);
54 27 : env = g_environ_setenv (env, "XDG_RUNTIME_DIR", directory, TRUE);
55 27 : env = g_environ_setenv (env, "G_DEBUG", "fatal-warnings,fatal-criticals", FALSE);
56 :
57 27 : va_start (va, pid);
58 : for (;;) {
59 74 : name = va_arg (va, const gchar *);
60 74 : if (!name)
61 27 : break;
62 47 : value = va_arg (va, const gchar *);
63 47 : if (value)
64 47 : env = g_environ_setenv (env, name, value, TRUE);
65 : else
66 0 : env = g_environ_unsetenv (env, name);
67 : }
68 27 : va_end (va);
69 :
70 27 : if (g_test_verbose ()) {
71 0 : cmd = g_strjoinv (" ", (gchar **)argv);
72 0 : g_print ("$ %s\n", cmd);
73 0 : g_free (cmd);
74 : }
75 :
76 27 : if (!g_spawn_async_with_pipes (NULL, (gchar **)argv, env,
77 : G_SPAWN_LEAVE_DESCRIPTORS_OPEN | G_SPAWN_DO_NOT_REAP_CHILD,
78 : NULL, NULL, pid, NULL, &out_fd, NULL, &error)) {
79 0 : g_error ("couldn't start gnome-keyring-daemon for testing: %s", error->message);
80 : }
81 :
82 27 : g_strfreev (env);
83 :
84 : /* The entire stdout of the daemon is environment variables */
85 27 : output = g_string_new ("");
86 : for (;;) {
87 49 : len = output->len;
88 49 : g_string_set_size (output, len + 1024);
89 49 : ret = read (out_fd, output->str + len, 1024);
90 49 : if (ret < 0)
91 0 : g_assert_cmpint (ret, ==, EAGAIN);
92 49 : if (g_test_verbose ())
93 0 : g_print ("%.*s", (int)ret, output->str + len);
94 49 : g_string_set_size (output, len + ret);
95 49 : if (ret == 0)
96 27 : break;
97 : }
98 27 : close (out_fd);
99 :
100 27 : result = g_strsplit (output->str, "\n", -1);
101 27 : g_string_free (output, TRUE);
102 27 : return result;
103 : }
|