File: test-shutdown.c

package info (click to toggle)
gnome-keyring 3.28.2-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 12,440 kB
  • sloc: ansic: 77,096; sh: 4,898; makefile: 1,332; xml: 420; python: 401; sed: 16
file content (141 lines) | stat: -rw-r--r-- 3,421 bytes parent folder | download | duplicates (6)
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
/*
   Copyright (C) 2014 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,
   <http://www.gnu.org/licenses/>.

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

#include "config.h"

#include "gkd-test.h"

#include "daemon/control/gkd-control.h"

#include "egg/egg-testing.h"

#include <glib.h>
#include <glib/gstdio.h>
#include <gio/gio.h>

#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>

typedef struct {
	GTestDBus *dbus;
	gchar *directory;
	GPid pid;
} Test;

static void
setup (Test *test,
       gconstpointer unused)
{
	test->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
	g_test_dbus_up (test->dbus);

	test->directory = egg_tests_create_scratch_directory (NULL, NULL);
}

static void
teardown (Test *test,
          gconstpointer unused)
{
	if (test->pid) {
		if (waitpid (test->pid, NULL, WNOHANG) != test->pid) {
			kill (test->pid, SIGTERM);
			g_assert_cmpint (waitpid (test->pid, NULL, 0), ==, test->pid);
		}
		g_spawn_close_pid (test->pid);
	}

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

	if (test->dbus) {
		g_test_dbus_down (test->dbus);
		g_object_unref (test->dbus);
	}
}

static void
test_sigterm (Test *test,
              gconstpointer unused)
{
	const gchar *argv[] = {
		BUILDDIR "/gnome-keyring-daemon", "--foreground",
		"--control-directory", test->directory,
		"--components=secrets,pkcs11", NULL
	};

	gchar **output;
	gint status;
	GPid pid;

	output = gkd_test_launch_daemon (test->directory, argv, &pid, NULL);

	g_assert (gkd_control_unlock (test->directory, "booo"));
	g_strfreev (output);

	/* Terminate the daemon */
	g_assert_cmpint (kill (pid, SIGTERM), ==, 0);

	/* Daemon should exit cleanly */
	g_assert_cmpint (waitpid (pid, &status, 0), ==, pid);
	g_assert_cmpint (status, ==, 0);
}

static void
test_close_connection (Test *test,
                       gconstpointer unused)
{
	const gchar *argv[] = {
		BUILDDIR "/gnome-keyring-daemon", "--foreground",
		"--control-directory", test->directory,
		"--components=secrets,pkcs11", NULL
	};

	gchar **output;
	gint status;
	GPid pid;

	output = gkd_test_launch_daemon (test->directory, argv, &pid, NULL);

	g_assert (gkd_control_unlock (test->directory, "booo"));
	g_strfreev (output);

	/* Now close the dbus connection */
	g_test_dbus_down (test->dbus);
	g_object_unref (test->dbus);
	test->dbus = NULL;

	/* Daemon should exit */
	g_assert_cmpint (waitpid (pid, &status, 0), ==, pid);
	g_assert_cmpint (status, ==, 0);
}

int
main (int argc, char **argv)
{
	g_test_init (&argc, &argv, NULL);

	g_test_add ("/daemon/shutdown/dbus-connection", Test, NULL,
	            setup, test_close_connection, teardown);
	g_test_add ("/daemon/shutdown/sigterm", Test, NULL,
	            setup, test_sigterm, teardown);

	return g_test_run ();
}