Branch data Line data Source code
1 : : /*
2 : : * gnome-keyring
3 : : *
4 : : * Copyright (C) 2018 Red Hat, Inc.
5 : : *
6 : : * This program is free software; you can redistribute it and/or modify
7 : : * it under the terms of the GNU Lesser General Public License as
8 : : * published by the Free Software Foundation; either version 2.1 of
9 : : * the License, or (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful, but
12 : : * WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General Public
17 : : * License along with this program; if not, see
18 : : * <http://www.gnu.org/licenses/>.
19 : : *
20 : : * Author: Daiki Ueno
21 : : */
22 : :
23 : : #include "config.h"
24 : :
25 : : #include "gcr-ssh-agent-private.h"
26 : : #include "gcr-ssh-agent-process.h"
27 : : #include "gcr-ssh-agent-util.h"
28 : : #include "gcr-ssh-agent-test.h"
29 : : #include "egg/egg-testing.h"
30 : :
31 : : #include <glib.h>
32 : :
33 : : typedef struct {
34 : : gchar *directory;
35 : : EggBuffer req;
36 : : EggBuffer resp;
37 : : GcrSshAgentProcess *process;
38 : : GSocketConnection *connection;
39 : : GMainLoop *loop;
40 : : } Test;
41 : :
42 : : static void
43 : 7 : setup (Test *test, gconstpointer unused)
44 : : {
45 : : gchar *path;
46 : :
47 : 7 : test->directory = egg_tests_create_scratch_directory (NULL, NULL);
48 : :
49 : 7 : egg_buffer_init_full (&test->req, 128, (EggBufferAllocator)g_realloc);
50 : 7 : egg_buffer_init_full (&test->resp, 128, (EggBufferAllocator)g_realloc);
51 : :
52 : 7 : path = g_strdup_printf ("%s/.ssh.sock", test->directory);
53 : 7 : test->process = gcr_ssh_agent_process_new (path);
54 : 7 : g_free (path);
55 [ - + ]: 7 : g_assert_nonnull (test->process);
56 : 7 : test->connection = NULL;
57 : 7 : }
58 : :
59 : : static void
60 : 7 : teardown (Test *test, gconstpointer unused)
61 : : {
62 [ + - ]: 7 : g_clear_object (&test->process);
63 [ + - ]: 7 : g_clear_object (&test->connection);
64 : :
65 : 7 : egg_buffer_uninit (&test->req);
66 : 7 : egg_buffer_uninit (&test->resp);
67 : :
68 : 7 : egg_tests_remove_scratch_directory (test->directory);
69 : 7 : free (test->directory);
70 : 7 : }
71 : :
72 : : static void
73 : 8 : connect_to_process (Test *test)
74 : : {
75 : : GError *error;
76 : :
77 : 8 : error = NULL;
78 : 8 : test->connection = gcr_ssh_agent_process_connect (test->process, NULL, &error);
79 [ - + ]: 8 : g_assert_nonnull (test->connection);
80 [ - + ]: 8 : g_assert_no_error (error);
81 : 8 : }
82 : :
83 : : static void
84 : 1 : test_connect (Test *test, gconstpointer unused)
85 : : {
86 : 1 : connect_to_process (test);
87 : 1 : }
88 : :
89 : : static void
90 : 16 : call (Test *test)
91 : : {
92 : : GError *error;
93 : : gboolean ret;
94 : :
95 : 16 : error = NULL;
96 : 16 : ret = _gcr_ssh_agent_call (test->connection, &test->req, &test->resp, NULL, &error);
97 [ - + ]: 16 : g_assert_true (ret);
98 [ - + ]: 16 : g_assert_no_error (error);
99 : 16 : }
100 : :
101 : 16 : DEFINE_CALL_FUNCS(Test, call)
102 : :
103 : : static void
104 : 1 : test_list (Test *test, gconstpointer unused)
105 : : {
106 : 1 : connect_to_process (test);
107 : 1 : call_request_identities(test, 0);
108 : 1 : }
109 : :
110 : : static void
111 : 1 : test_add (Test *test, gconstpointer unused)
112 : : {
113 : 1 : connect_to_process (test);
114 : 1 : call_add_identity (test);
115 : 1 : call_request_identities (test, 1);
116 : 1 : }
117 : :
118 : : static void
119 : 1 : test_remove (Test *test, gconstpointer unused)
120 : : {
121 : 1 : connect_to_process (test);
122 : 1 : call_add_identity (test);
123 : 1 : call_request_identities (test, 1);
124 : :
125 : 1 : call_remove_identity (test);
126 : 1 : call_request_identities (test, 0);
127 : 1 : }
128 : :
129 : : static void
130 : 1 : test_remove_all (Test *test, gconstpointer unused)
131 : : {
132 : 1 : connect_to_process (test);
133 : 1 : call_add_identity (test);
134 : 1 : call_request_identities (test, 1);
135 : :
136 : 1 : call_remove_all_identities (test);
137 : 1 : call_request_identities (test, 0);
138 : 1 : }
139 : :
140 : : static void
141 : 1 : test_sign (Test *test, gconstpointer unused)
142 : : {
143 : 1 : connect_to_process (test);
144 : 1 : call_add_identity (test);
145 : 1 : call_request_identities (test, 1);
146 : :
147 : 1 : call_sign (test);
148 : :
149 : 1 : call_remove_all_identities (test);
150 : 1 : call_request_identities (test, 0);
151 : 1 : }
152 : :
153 : : static gpointer
154 : 1 : kill_thread (gpointer data)
155 : : {
156 : 1 : Test *test = data;
157 : : GPid pid;
158 : :
159 : 1 : pid = gcr_ssh_agent_process_get_pid (test->process);
160 [ - + ]: 1 : g_assert_cmpint (-1, !=, pid);
161 : :
162 : 1 : kill (pid, SIGTERM);
163 : :
164 : 1 : return NULL;
165 : : }
166 : :
167 : : static void
168 : 1 : on_closed (GcrSshAgentProcess *self, gpointer data)
169 : : {
170 : 1 : GMainLoop *loop = data;
171 : :
172 : 1 : g_main_loop_quit (loop);
173 : 1 : g_main_loop_unref (loop);
174 : 1 : }
175 : :
176 : : static void
177 : 1 : test_restart (Test *test, gconstpointer unused)
178 : : {
179 : : GPid pid;
180 : : GMainLoop *loop;
181 : : GThread *thread;
182 : :
183 : 1 : connect_to_process (test);
184 : :
185 : 1 : pid = gcr_ssh_agent_process_get_pid (test->process);
186 [ - + ]: 1 : g_assert_cmpint (0, !=, pid);
187 : :
188 : 1 : thread = g_thread_new ("kill", kill_thread, test);
189 : :
190 : 1 : loop = g_main_loop_new (NULL, FALSE);
191 : 1 : g_signal_connect (test->process, "closed", G_CALLBACK (on_closed), loop);
192 : 1 : g_main_loop_run (loop);
193 : :
194 : 1 : g_thread_join (thread);
195 : :
196 : 1 : pid = gcr_ssh_agent_process_get_pid (test->process);
197 [ - + ]: 1 : g_assert_cmpint (0, ==, pid);
198 : :
199 : 1 : connect_to_process (test);
200 : :
201 : 1 : pid = gcr_ssh_agent_process_get_pid (test->process);
202 [ - + ]: 1 : g_assert_cmpint (0, !=, pid);
203 : 1 : }
204 : :
205 : : int
206 : 1 : main (int argc, char **argv)
207 : : {
208 : 1 : g_test_init (&argc, &argv, NULL);
209 : :
210 : 1 : g_test_add ("/ssh-agent/process/connect", Test, NULL, setup, test_connect, teardown);
211 : 1 : g_test_add ("/ssh-agent/process/list", Test, NULL, setup, test_list, teardown);
212 : 1 : g_test_add ("/ssh-agent/process/add", Test, NULL, setup, test_add, teardown);
213 : 1 : g_test_add ("/ssh-agent/process/remove", Test, NULL, setup, test_remove, teardown);
214 : 1 : g_test_add ("/ssh-agent/process/remove_all", Test, NULL, setup, test_remove_all, teardown);
215 : 1 : g_test_add ("/ssh-agent/process/sign", Test, NULL, setup, test_sign, teardown);
216 : 1 : g_test_add ("/ssh-agent/process/restart", Test, NULL, setup, test_restart, teardown);
217 : :
218 : 1 : return g_test_run ();
219 : : }
|