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