Debian Codegraph @

pkcs11/rpc-layer/test-initialize.c

raw
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 <[email protected]>
19 */
20
21 #include "config.h"
22
23 #include "daemon/gkd-test.h"
24
25 #include "egg/egg-testing.h"
26
27 #include <gck/gck.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 setup (Test *test,
45 gconstpointer unused)
46 {
47 test->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
48 g_test_dbus_up (test->dbus);
49
50 test->directory = egg_tests_create_scratch_directory (NULL, NULL);
51
52 g_unsetenv ("GNOME_KEYRING_CONTROL");
53 g_setenv ("XDG_RUNTIME_DIR", test->directory, TRUE);
54 }
55
56 static void
57 teardown (Test *test,
58 gconstpointer unused)
59 {
60 if (test->pid) {
61 if (waitpid (test->pid, NULL, WNOHANG) != test->pid) {
62 kill (test->pid, SIGTERM);
63 g_assert_cmpint (waitpid (test->pid, NULL, 0), ==, test->pid);
64 }
65 g_spawn_close_pid (test->pid);
66 }
67
68 egg_tests_remove_scratch_directory (test->directory);
69 g_free (test->directory);
70
71 g_test_dbus_down (test->dbus);
72 g_object_unref (test->dbus);
73 }
74
75 static void
76 test_initialize_normal (Test *test,
77 gconstpointer unused)
78 {
79 const gchar *argv[] = {
80 TEST_GKR_DAEMON_BIN, "--foreground",
81 "--components=pkcs11", NULL
82 };
83
84 const gchar *control;
85 gchar **output;
86 GckModule *module;
87 GckModuleInfo *info;
88 GError *error = NULL;
89
90 /* Start the first daemon */
91 output = gkd_test_launch_daemon (test->directory, argv, &test->pid, NULL);
92 control = g_environ_getenv (output, "GNOME_KEYRING_CONTROL");
93 g_assert_cmpstr (control, ==, NULL);
94 g_strfreev (output);
95
96 module = gck_module_initialize (TEST_GKR_PKCS11_MODULE,
97 NULL, &error);
98 g_assert_no_error (error);
99
100 info = gck_module_get_info (module);
101 g_assert (info != NULL);
102 g_assert_cmpstr (info->library_description, ==, "GNOME Keyring Daemon Core");
103 gck_module_info_free (info);
104
105 g_object_unref (module);
106 }
107
108 static void
109 test_initialize_control (Test *test,
110 gconstpointer unused)
111 {
112 const gchar *argv[] = {
113 TEST_GKR_DAEMON_BIN, "--foreground",
114 "--control-directory", test->directory,
115 "--components=pkcs11", NULL
116 };
117
118 const gchar *control;
119 gchar **output;
120 GckModule *module;
121 GckModuleInfo *info;
122 GError *error = NULL;
123
124 /* Start the first daemon */
125 output = gkd_test_launch_daemon (test->directory, argv, &test->pid, NULL);
126 control = g_environ_getenv (output, "GNOME_KEYRING_CONTROL");
127 g_assert_cmpstr (control, ==, test->directory);
128 g_setenv ("GNOME_KEYRING_CONTROL", control, TRUE);
129 g_strfreev (output);
130
131 module = gck_module_initialize (TEST_GKR_PKCS11_MODULE,
132 NULL, &error);
133 g_assert_no_error (error);
134
135 info = gck_module_get_info (module);
136 g_assert (info != NULL);
137 g_assert_cmpstr (info->library_description, ==, "GNOME Keyring Daemon Core");
138 gck_module_info_free (info);
139
140 g_object_unref (module);
141 }
142
143 static void
144 test_initialize_no_daemon (Test *test,
145 gconstpointer unused)
146 {
147 GckModule *module;
148 GckModuleInfo *info;
149 GError *error = NULL;
150
151 /* No daemon to connect to */
152 g_unsetenv ("GNOME_KEYRING_CONTROL");
153 g_unsetenv ("XDG_RUNTIME_DIR");
154
155 module = gck_module_initialize (TEST_GKR_PKCS11_MODULE,
156 NULL, &error);
157 g_assert_no_error (error);
158
159 info = gck_module_get_info (module);
160 g_assert (info != NULL);
161 g_assert_cmpstr (info->library_description, ==, "GNOME Keyring (without daemon)");
162 gck_module_info_free (info);
163
164 g_object_unref (module);
165 }
166
167 int
168 main (int argc, char **argv)
169 {
170 g_test_init (&argc, &argv, NULL);
171
172 g_test_add ("/pkcs11/rpc-layer/initialize/normal", Test, NULL,
173 setup, test_initialize_normal, teardown);
174 g_test_add ("/pkcs11/rpc-layer/initialize/control", Test, NULL,
175 setup, test_initialize_control, teardown);
176 g_test_add ("/pkcs11/rpc-layer/initialize/no-daemon", Test, NULL,
177 setup, test_initialize_no_daemon, teardown);
178
179 return g_test_run ();
180 }