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 "daemon/control/gkd-control.h"
26 :
27 : #include "egg/egg-testing.h"
28 :
29 : #include <glib.h>
30 : #include <glib/gstdio.h>
31 : #include <gio/gio.h>
32 :
33 : #include <sys/wait.h>
34 : #include <errno.h>
35 : #include <fcntl.h>
36 :
37 : typedef struct {
38 : GTestDBus *dbus;
39 : gchar *directory;
40 : GPid pid;
41 : } Test;
42 :
43 : static void
44 3 : setup (Test *test,
45 : gconstpointer unused)
46 : {
47 3 : test->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
48 3 : g_test_dbus_up (test->dbus);
49 :
50 2 : test->directory = egg_tests_create_scratch_directory (NULL, NULL);
51 2 : }
52 :
53 : static void
54 2 : teardown (Test *test,
55 : gconstpointer unused)
56 : {
57 2 : if (test->pid) {
58 0 : if (waitpid (test->pid, NULL, WNOHANG) != test->pid) {
59 0 : kill (test->pid, SIGTERM);
60 0 : g_assert_cmpint (waitpid (test->pid, NULL, 0), ==, test->pid);
61 : }
62 0 : g_spawn_close_pid (test->pid);
63 : }
64 :
65 2 : egg_tests_remove_scratch_directory (test->directory);
66 2 : g_free (test->directory);
67 :
68 2 : if (test->dbus) {
69 1 : g_test_dbus_down (test->dbus);
70 1 : g_object_unref (test->dbus);
71 : }
72 2 : }
73 :
74 : static void
75 1 : test_sigterm (Test *test,
76 : gconstpointer unused)
77 : {
78 1 : const gchar *argv[] = {
79 : BUILDDIR "/gnome-keyring-daemon", "--foreground",
80 1 : "--control-directory", test->directory,
81 : "--components=secrets,pkcs11", NULL
82 : };
83 :
84 : gchar **output;
85 : gint status;
86 : GPid pid;
87 :
88 1 : output = gkd_test_launch_daemon (test->directory, argv, &pid, NULL);
89 :
90 1 : g_assert (gkd_control_unlock (test->directory, "booo"));
91 1 : g_strfreev (output);
92 :
93 : /* Terminate the daemon */
94 1 : g_assert_cmpint (kill (pid, SIGTERM), ==, 0);
95 :
96 : /* Daemon should exit cleanly */
97 1 : g_assert_cmpint (waitpid (pid, &status, 0), ==, pid);
98 1 : g_assert_cmpint (status, ==, 0);
99 1 : }
100 :
101 : static void
102 1 : test_close_connection (Test *test,
103 : gconstpointer unused)
104 : {
105 1 : const gchar *argv[] = {
106 : BUILDDIR "/gnome-keyring-daemon", "--foreground",
107 1 : "--control-directory", test->directory,
108 : "--components=secrets,pkcs11", NULL
109 : };
110 :
111 : gchar **output;
112 : gint status;
113 : GPid pid;
114 :
115 1 : output = gkd_test_launch_daemon (test->directory, argv, &pid, NULL);
116 :
117 1 : g_assert (gkd_control_unlock (test->directory, "booo"));
118 1 : g_strfreev (output);
119 :
120 : /* Now close the dbus connection */
121 1 : g_test_dbus_down (test->dbus);
122 1 : g_object_unref (test->dbus);
123 1 : test->dbus = NULL;
124 :
125 : /* Daemon should exit */
126 1 : g_assert_cmpint (waitpid (pid, &status, 0), ==, pid);
127 1 : g_assert_cmpint (status, ==, 0);
128 1 : }
129 :
130 : int
131 2 : main (int argc, char **argv)
132 : {
133 2 : g_test_init (&argc, &argv, NULL);
134 :
135 2 : g_test_add ("/daemon/shutdown/dbus-connection", Test, NULL,
136 : setup, test_close_connection, teardown);
137 2 : g_test_add ("/daemon/shutdown/sigterm", Test, NULL,
138 : setup, test_sigterm, teardown);
139 :
140 2 : return g_test_run ();
141 : }
|