Branch data Line data Source code
1 : : /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 : : /*
3 : : Copyright (C) 2011 Collabora Ltd
4 : :
5 : : The Gnome Keyring Library is free software; you can redistribute it and/or
6 : : modify it under the terms of the GNU Library General Public License as
7 : : published by the Free Software Foundation; either version 2 of the
8 : : License, or (at your option) any later version.
9 : :
10 : : The Gnome Keyring Library is distributed in the hope that it will be useful,
11 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
12 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 : : Library General Public License for more details.
14 : :
15 : : You should have received a copy of the GNU Library General Public
16 : : License along with the Gnome Library; see the file COPYING.LIB. If not,
17 : : see <http://www.gnu.org/licenses/>.
18 : :
19 : : Author: Stef Walter <stefw@collabora.co.uk>
20 : : */
21 : :
22 : : #include "config.h"
23 : :
24 : : #include "gcr/gcr.h"
25 : : #include "gcr/gcr-callback-output-stream.h"
26 : : #include "gcr/gcr-gnupg-process.h"
27 : :
28 : : #include "egg/egg-testing.h"
29 : :
30 : : #include <glib.h>
31 : :
32 : : #include <errno.h>
33 : : #include <string.h>
34 : :
35 : : #define WAIT 50000
36 : :
37 : : typedef struct {
38 : : GcrGnupgProcess *process;
39 : : GAsyncResult *result;
40 : : GString *output_buf;
41 : : GString *error_buf;
42 : : GString *attribute_buf;
43 : : GcrRecord *record;
44 : : } Test;
45 : :
46 : : static void
47 : 13 : setup (Test *test, gconstpointer unused)
48 : : {
49 : 13 : test->output_buf = g_string_new ("");
50 : 13 : test->error_buf = g_string_new ("");
51 : 13 : test->attribute_buf = g_string_new ("");
52 : 13 : }
53 : :
54 : : static void
55 : 13 : teardown (Test *test, gconstpointer unused)
56 : : {
57 [ + + ]: 13 : if (test->result)
58 : 12 : g_object_unref (test->result);
59 [ + - ]: 13 : if (test->process)
60 : 13 : g_object_unref (test->process);
61 : :
62 [ - + ]: 13 : while (g_main_context_iteration (NULL, FALSE));
63 : :
64 [ - + ]: 13 : if (test->result)
65 [ # # ]: 0 : g_assert (test->result == NULL);
66 [ - + ]: 13 : if (test->process)
67 [ # # ]: 0 : g_assert (test->process == NULL);
68 : :
69 [ + - ]: 13 : if (test->output_buf)
70 : 13 : g_string_free (test->output_buf, TRUE);
71 [ + - ]: 13 : if (test->error_buf)
72 : 13 : g_string_free (test->error_buf, TRUE);
73 [ + - ]: 13 : if (test->attribute_buf)
74 : 13 : g_string_free (test->attribute_buf, TRUE);
75 : 13 : _gcr_record_free (test->record);
76 : 13 : }
77 : :
78 : : static void
79 : 1 : test_create (Test *test, gconstpointer unused)
80 : : {
81 : : gchar *value;
82 : :
83 : 1 : test->process = _gcr_gnupg_process_new ("/the/directory", "/path/to/executable");
84 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
85 : :
86 : 1 : g_object_get (test->process, "directory", &value, NULL);
87 [ - + ]: 1 : g_assert_cmpstr (value, ==, "/the/directory");
88 : 1 : g_free (value);
89 : :
90 : 1 : g_object_get (test->process, "executable", &value, NULL);
91 [ - + ]: 1 : g_assert_cmpstr (value, ==, "/path/to/executable");
92 : 1 : g_free (value);
93 : 1 : }
94 : :
95 : : static void
96 : 12 : on_async_ready (GObject *source, GAsyncResult *result, gpointer user_data)
97 : : {
98 : 12 : Test *test = user_data;
99 : : GObject *result_source;
100 : :
101 [ - + ]: 12 : g_assert (G_OBJECT (test->process) == source);
102 [ - + ]: 12 : g_assert (test->result == NULL);
103 : :
104 : 12 : result_source = g_async_result_get_source_object (result);
105 [ - + ]: 12 : g_assert (result_source == source);
106 : 12 : g_object_unref (result_source);
107 : :
108 : 12 : test->result = g_object_ref (result);
109 : 12 : g_object_add_weak_pointer (G_OBJECT (test->result), (gpointer *)&test->result);
110 : 12 : egg_test_wait_stop ();
111 : 12 : }
112 : :
113 : : static gchar*
114 : 12 : build_script_path (const gchar *name)
115 : : {
116 : : gchar *path;
117 : :
118 : 12 : path = g_build_filename (SRCDIR, "gcr", "fixtures", "gnupg-mock", name, NULL);
119 : :
120 : 12 : return path;
121 : : }
122 : :
123 : : static gssize
124 : 4 : on_process_output_data (gconstpointer buffer,
125 : : gsize count,
126 : : GCancellable *cancellable,
127 : : gpointer user_data,
128 : : GError **error)
129 : : {
130 : 4 : Test *test = user_data;
131 : 4 : g_string_append_len (test->output_buf, buffer, count);
132 : 4 : return count;
133 : : }
134 : :
135 : : static gssize
136 : 1 : on_process_attribute_data (gconstpointer buffer,
137 : : gsize count,
138 : : GCancellable *cancellable,
139 : : gpointer user_data,
140 : : GError **error)
141 : : {
142 : 1 : Test *test = user_data;
143 : 1 : g_string_append_len (test->attribute_buf, buffer, count);
144 : 1 : return count;
145 : : }
146 : :
147 : : static void
148 : 4 : on_process_error_line (GcrGnupgProcess *process, const gchar *line, gpointer user_data)
149 : : {
150 : 4 : Test *test = user_data;
151 : :
152 [ - + ]: 4 : g_assert (process == test->process);
153 [ - + ]: 4 : g_assert (line);
154 [ - + ]: 4 : g_assert (!strchr (line, '\n'));
155 : :
156 : 4 : g_string_append_printf (test->error_buf, "%s\n", line);
157 : 4 : }
158 : :
159 : : static void
160 : 2 : on_process_status_record (GcrGnupgProcess *process, GcrRecord *record, gpointer user_data)
161 : : {
162 : 2 : Test *test = user_data;
163 : :
164 [ - + ]: 2 : g_assert (process == test->process);
165 [ - + ]: 2 : g_assert (record);
166 : :
167 [ - + ]: 2 : g_assert (!test->record);
168 : 2 : test->record = _gcr_record_copy (record);
169 : 2 : }
170 : :
171 : : static void
172 : 1 : test_run_simple_output (Test *test, gconstpointer unused)
173 : : {
174 : 1 : const gchar *argv[] = { NULL };
175 : : GOutputStream *output;
176 : 1 : GError *error = NULL;
177 : : gboolean ret;
178 : : gchar *script;
179 : :
180 : 1 : script = build_script_path ("mock-simple-output");
181 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
182 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
183 : 1 : g_free (script);
184 : :
185 : 1 : output = _gcr_callback_output_stream_new (on_process_output_data, test, NULL);
186 : 1 : _gcr_gnupg_process_set_output_stream (test->process, output);
187 : 1 : g_object_unref (output);
188 : :
189 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, NULL, on_async_ready, test);
190 : 1 : egg_test_wait_until (WAIT);
191 : :
192 [ - + ]: 1 : g_assert (test->result);
193 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
194 [ - + ]: 1 : g_assert_no_error (error);
195 [ - + ]: 1 : g_assert (ret == TRUE);
196 : :
197 [ - + ]: 1 : g_assert_cmpstr ("simple-output\n", ==, test->output_buf->str);
198 : 1 : }
199 : :
200 : : static void
201 : 1 : test_run_simple_error (Test *test, gconstpointer unused)
202 : : {
203 : 1 : const gchar *argv[] = { NULL };
204 : 1 : GError *error = NULL;
205 : : gchar *script;
206 : : gboolean ret;
207 : :
208 : 1 : script = build_script_path ("mock-simple-error");
209 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
210 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
211 : 1 : g_free (script);
212 : :
213 : 1 : g_signal_connect (test->process, "error-line", G_CALLBACK (on_process_error_line), test);
214 : :
215 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, NULL, on_async_ready, test);
216 : 1 : egg_test_wait_until (WAIT);
217 : :
218 [ - + ]: 1 : g_assert (test->result);
219 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
220 [ - + ]: 1 : g_assert_no_error (error);
221 [ - + ]: 1 : g_assert (ret == TRUE);
222 : :
223 [ - + ]: 1 : g_assert_cmpstr ("line 1: more line 1\nline 2\nline 3\n", ==, test->error_buf->str);
224 : 1 : }
225 : :
226 : : static void
227 : 1 : test_run_status_and_output (Test *test, gconstpointer unused)
228 : : {
229 : 1 : const gchar *argv[] = { NULL };
230 : : GOutputStream *output;
231 : 1 : GError *error = NULL;
232 : : gchar *script;
233 : : gboolean ret;
234 : :
235 : 1 : script = build_script_path ("mock-status-and-output");
236 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
237 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
238 : 1 : g_free (script);
239 : :
240 : 1 : output = _gcr_callback_output_stream_new (on_process_output_data, test, NULL);
241 : 1 : _gcr_gnupg_process_set_output_stream (test->process, output);
242 : 1 : g_object_unref (output);
243 : :
244 : 1 : g_signal_connect (test->process, "status-record", G_CALLBACK (on_process_status_record), test);
245 : :
246 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, GCR_GNUPG_PROCESS_WITH_STATUS,
247 : : NULL, on_async_ready, test);
248 : 1 : egg_test_wait_until (WAIT);
249 : :
250 [ - + ]: 1 : g_assert (test->result);
251 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
252 [ - + ]: 1 : g_assert_no_error (error);
253 [ - + ]: 1 : g_assert (ret == TRUE);
254 : :
255 [ - + ]: 1 : g_assert (test->record);
256 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 0), ==, "SCHEMA");
257 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 1), ==, "one");
258 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 2), ==, "two");
259 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 3), ==, "three");
260 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 4), ==, "four");
261 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 5), ==, NULL);
262 [ - + ]: 1 : g_assert_cmpstr ("Here's some output\nMore output\n", ==, test->output_buf->str);
263 : 1 : }
264 : :
265 : : static void
266 : 1 : test_run_status_and_attribute (Test *test, gconstpointer unused)
267 : : {
268 : 1 : const gchar *argv[] = { NULL };
269 : : GOutputStream *output;
270 : 1 : GError *error = NULL;
271 : : gchar *script;
272 : : gboolean ret;
273 : :
274 : 1 : script = build_script_path ("mock-status-and-attribute");
275 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
276 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
277 : 1 : g_free (script);
278 : :
279 : 1 : output = _gcr_callback_output_stream_new (on_process_attribute_data, test, NULL);
280 : 1 : _gcr_gnupg_process_set_attribute_stream (test->process, output);
281 : 1 : g_object_unref (output);
282 : :
283 : 1 : g_signal_connect (test->process, "status-record", G_CALLBACK (on_process_status_record), test);
284 : :
285 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL,
286 : : GCR_GNUPG_PROCESS_WITH_STATUS | GCR_GNUPG_PROCESS_WITH_ATTRIBUTES,
287 : : NULL, on_async_ready, test);
288 : 1 : egg_test_wait_until (WAIT);
289 : :
290 [ - + ]: 1 : g_assert (test->result);
291 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
292 [ - + ]: 1 : g_assert_no_error (error);
293 [ - + ]: 1 : g_assert (ret == TRUE);
294 : :
295 [ - + ]: 1 : g_assert (test->record);
296 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 0), ==, "SCHEMA");
297 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 1), ==, "one");
298 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 2), ==, "two");
299 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 3), ==, "three");
300 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 4), ==, "four");
301 [ - + ]: 1 : g_assert_cmpstr (_gcr_record_get_raw (test->record, 5), ==, NULL);
302 [ - + ]: 1 : g_assert_cmpstr ("1lc923g4laoeurc23rc241lcg2r23c4gr3", ==, test->attribute_buf->str);
303 : 1 : }
304 : :
305 : :
306 : : static void
307 : 1 : test_run_arguments_and_environment (Test *test, gconstpointer unused)
308 : : {
309 : 1 : GError *error = NULL;
310 : : GOutputStream *output;
311 : : gchar *script;
312 : : gboolean ret;
313 : :
314 : 1 : const gchar *argv[] = {
315 : : "-1", "value1",
316 : : "-2", "value2",
317 : : NULL
318 : : };
319 : :
320 : 1 : const gchar *envp[] = {
321 : : "ENVIRON1=VALUE1",
322 : : "ENVIRON2=VALUE2",
323 : : NULL
324 : : };
325 : :
326 : 1 : script = build_script_path ("mock-arguments-environ");
327 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
328 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
329 : 1 : g_free (script);
330 : :
331 : 1 : output = _gcr_callback_output_stream_new (on_process_output_data, test, NULL);
332 : 1 : _gcr_gnupg_process_set_output_stream (test->process, output);
333 : 1 : g_object_unref (output);
334 : :
335 : 1 : g_signal_connect (test->process, "error-line", G_CALLBACK (on_process_error_line), test);
336 : :
337 : 1 : _gcr_gnupg_process_run_async (test->process, argv, envp, 0, NULL, on_async_ready, test);
338 : 1 : egg_test_wait_until (WAIT);
339 : :
340 [ - + ]: 1 : g_assert (test->result);
341 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
342 [ - + ]: 1 : if (error) {
343 : 0 : g_printerr ("%s\n", test->error_buf->str);
344 [ # # ]: 0 : g_assert_no_error (error);
345 : : }
346 [ - + ]: 1 : g_assert (ret == TRUE);
347 : :
348 [ - + ]: 1 : g_assert_cmpstr ("value1\nvalue2\n", ==, test->output_buf->str);
349 [ - + ]: 1 : g_assert_cmpstr ("VALUE1VALUE2\n", ==, test->error_buf->str);
350 : 1 : }
351 : :
352 : : static void
353 : 1 : test_run_with_homedir (Test *test, gconstpointer unused)
354 : : {
355 : 1 : const gchar *argv[] = { NULL };
356 : : GOutputStream *output;
357 : 1 : GError *error = NULL;
358 : : gchar *script;
359 : : gchar *check;
360 : : gboolean ret;
361 : :
362 : 1 : script = build_script_path ("mock-with-homedir");
363 : 1 : test->process = _gcr_gnupg_process_new (SRCDIR, script);
364 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
365 : 1 : g_free (script);
366 : :
367 : 1 : output = _gcr_callback_output_stream_new (on_process_output_data, test, NULL);
368 : 1 : _gcr_gnupg_process_set_output_stream (test->process, output);
369 : 1 : g_object_unref (output);
370 : :
371 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, NULL, on_async_ready, test);
372 : 1 : egg_test_wait_until (WAIT);
373 : :
374 [ - + ]: 1 : g_assert (test->result);
375 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
376 [ - + ]: 1 : g_assert_no_error (error);
377 [ - + ]: 1 : g_assert (ret == TRUE);
378 : :
379 : 1 : check = g_strdup_printf ("DIR: %s\n", SRCDIR);
380 [ - + ]: 1 : g_assert_cmpstr (check, ==, test->output_buf->str);
381 : 1 : g_free (check);
382 : 1 : }
383 : :
384 : : static void
385 : 1 : test_run_with_input_and_output (Test *test,
386 : : gconstpointer unused)
387 : : {
388 : 1 : const gchar *argv[] = { NULL };
389 : 1 : const gchar *data = "one\ntwenty two\nthree\nfourty four\n";
390 : : GInputStream *input;
391 : : GOutputStream *output;
392 : 1 : GError *error = NULL;
393 : : GString *string;
394 : : gchar *script;
395 : : gboolean ret;
396 : :
397 : 1 : script = build_script_path ("mock-echo");
398 : 1 : test->process = _gcr_gnupg_process_new (SRCDIR, script);
399 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
400 : 1 : g_free (script);
401 : :
402 : 1 : input = g_memory_input_stream_new_from_data ((gpointer)data, -1, NULL);
403 : 1 : output = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
404 : :
405 : 1 : _gcr_gnupg_process_set_input_stream (test->process, input);
406 : 1 : _gcr_gnupg_process_set_output_stream (test->process, output);
407 : :
408 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, NULL, on_async_ready, test);
409 : 1 : egg_test_wait_until (WAIT);
410 : :
411 [ - + ]: 1 : g_assert (test->result);
412 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
413 [ - + ]: 1 : g_assert_no_error (error);
414 [ - + ]: 1 : g_assert (ret == TRUE);
415 : :
416 : 1 : string = g_string_new_len (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (output)),
417 : 1 : g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (output)));
418 [ - + ]: 1 : g_assert_cmpstr (data, ==, string->str);
419 : 1 : g_string_free (string, TRUE);
420 : :
421 [ + - ]: 1 : g_clear_object (&input);
422 [ + - ]: 1 : g_clear_object (&output);
423 : 1 : }
424 : :
425 : : static void
426 : 1 : test_run_bad_executable (Test *test, gconstpointer unused)
427 : : {
428 : 1 : GError *error = NULL;
429 : : gchar *script;
430 : 1 : const gchar *argv[] = { NULL };
431 : : gboolean ret;
432 : :
433 : 1 : script = build_script_path ("mock-invalid");
434 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
435 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
436 : 1 : g_free (script);
437 : :
438 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, NULL, on_async_ready, test);
439 : 1 : egg_test_wait_until (WAIT);
440 : :
441 [ - + ]: 1 : g_assert (test->result);
442 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
443 [ + - + - : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT);
- + ]
444 : 1 : g_clear_error (&error);
445 [ - + ]: 1 : g_assert (ret == FALSE);
446 : 1 : }
447 : :
448 : : static void
449 : 1 : test_run_fail_exit (Test *test, gconstpointer unused)
450 : : {
451 : 1 : GError *error = NULL;
452 : : gchar *script;
453 : 1 : const gchar *argv[] = { "55", NULL };
454 : : gboolean ret;
455 : :
456 : 1 : script = build_script_path ("mock-fail-exit");
457 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
458 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
459 : 1 : g_free (script);
460 : :
461 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, NULL, on_async_ready, test);
462 : 1 : egg_test_wait_until (WAIT);
463 : :
464 [ - + ]: 1 : g_assert (test->result);
465 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
466 [ + - + - : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED);
- + ]
467 [ - + ]: 1 : g_assert_cmpstr (error->message, ==, "Gnupg process exited with code: 55");
468 : 1 : g_clear_error (&error);
469 [ - + ]: 1 : g_assert (ret == FALSE);
470 : 1 : }
471 : :
472 : : static void
473 : 1 : test_run_fail_signal (Test *test, gconstpointer unused)
474 : : {
475 : 1 : GError *error = NULL;
476 : : gchar *script;
477 : 1 : const gchar *argv[] = { "15", NULL };
478 : : gboolean ret;
479 : :
480 : 1 : script = build_script_path ("mock-fail-signal");
481 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
482 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
483 : 1 : g_free (script);
484 : :
485 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, NULL, on_async_ready, test);
486 : 1 : egg_test_wait_until (WAIT);
487 : :
488 [ - + ]: 1 : g_assert (test->result);
489 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
490 [ + - + - : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED);
- + ]
491 [ - + ]: 1 : g_assert_cmpstr (error->message, ==, "Gnupg process was terminated with signal: 15");
492 : 1 : g_clear_error (&error);
493 [ - + ]: 1 : g_assert (ret == FALSE);
494 : 1 : }
495 : :
496 : : static void
497 : 1 : test_run_and_cancel (Test *test, gconstpointer unused)
498 : : {
499 : 1 : GError *error = NULL;
500 : : gchar *script;
501 : 1 : const gchar *argv[] = { "15", NULL };
502 : : GCancellable *cancellable;
503 : : gboolean ret;
504 : :
505 : 1 : cancellable = g_cancellable_new ();
506 : :
507 : 1 : script = build_script_path ("mock-simple-output");
508 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
509 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
510 : 1 : g_free (script);
511 : :
512 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, cancellable, on_async_ready, test);
513 : 1 : g_cancellable_cancel (cancellable);
514 : 1 : egg_test_wait_until (WAIT);
515 : :
516 [ - + ]: 1 : g_assert (test->result);
517 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
518 [ + - + - : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
- + ]
519 : 1 : g_clear_error (&error);
520 [ - + ]: 1 : g_assert (ret == FALSE);
521 : :
522 : 1 : g_object_unref (cancellable);
523 : 1 : }
524 : :
525 : : static gssize
526 : 1 : on_process_output_cancel (gconstpointer buffer,
527 : : gsize count,
528 : : GCancellable *cancellable,
529 : : gpointer user_data,
530 : : GError **error)
531 : : {
532 : 1 : g_cancellable_cancel (cancellable);
533 : 1 : g_cancellable_set_error_if_cancelled (cancellable, error);
534 : 1 : return -1;
535 : : }
536 : :
537 : : static void
538 : 1 : test_run_and_cancel_later (Test *test, gconstpointer unused)
539 : : {
540 : 1 : GError *error = NULL;
541 : : GOutputStream *output;
542 : : gchar *script;
543 : 1 : const gchar *argv[] = { "15", NULL };
544 : : GCancellable *cancellable;
545 : : gboolean ret;
546 : :
547 : 1 : cancellable = g_cancellable_new ();
548 : :
549 : 1 : script = build_script_path ("mock-simple-output");
550 : 1 : test->process = _gcr_gnupg_process_new (NULL, script);
551 : 1 : g_object_add_weak_pointer (G_OBJECT (test->process), (gpointer *)&test->process);
552 : 1 : output = _gcr_callback_output_stream_new (on_process_output_cancel, NULL, NULL);
553 : 1 : _gcr_gnupg_process_set_output_stream (test->process, output);
554 : 1 : g_object_unref (output);
555 : 1 : g_free (script);
556 : :
557 : 1 : _gcr_gnupg_process_run_async (test->process, argv, NULL, 0, cancellable, on_async_ready, test);
558 : 1 : egg_test_wait_until (WAIT);
559 : :
560 [ - + ]: 1 : g_assert (test->result);
561 : 1 : ret = _gcr_gnupg_process_run_finish (test->process, test->result, &error);
562 [ + - + - : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
- + ]
563 : 1 : g_clear_error (&error);
564 [ - + ]: 1 : g_assert (ret == FALSE);
565 : :
566 : 1 : g_object_unref (cancellable);
567 : 1 : }
568 : :
569 : : int
570 : 1 : main (int argc, char **argv)
571 : : {
572 : 1 : g_test_init (&argc, &argv, NULL);
573 : 1 : g_set_prgname ("test-gnupg-process");
574 : :
575 : 1 : g_test_add ("/gcr/gnupg-process/create", Test, NULL, setup, test_create, teardown);
576 : 1 : g_test_add ("/gcr/gnupg-process/run_simple_output", Test, NULL, setup, test_run_simple_output, teardown);
577 : 1 : g_test_add ("/gcr/gnupg-process/run_simple_error", Test, NULL, setup, test_run_simple_error, teardown);
578 : 1 : g_test_add ("/gcr/gnupg-process/run_status_and_output", Test, NULL, setup, test_run_status_and_output, teardown);
579 : 1 : g_test_add ("/gcr/gnupg-process/run_status_and_attribute", Test, NULL, setup, test_run_status_and_attribute, teardown);
580 : 1 : g_test_add ("/gcr/gnupg-process/run_arguments_and_environment", Test, NULL, setup, test_run_arguments_and_environment, teardown);
581 : 1 : g_test_add ("/gcr/gnupg-process/run_with_homedir", Test, NULL, setup, test_run_with_homedir, teardown);
582 : 1 : g_test_add ("/gcr/gnupg-process/run_with_input_and_output", Test, NULL, setup, test_run_with_input_and_output, teardown);
583 : 1 : g_test_add ("/gcr/gnupg-process/run_fail_exit", Test, NULL, setup, test_run_fail_exit, teardown);
584 : 1 : g_test_add ("/gcr/gnupg-process/run_fail_signal", Test, NULL, setup, test_run_fail_signal, teardown);
585 : 1 : g_test_add ("/gcr/gnupg-process/run_and_cancel", Test, NULL, setup, test_run_and_cancel, teardown);
586 : 1 : g_test_add ("/gcr/gnupg-process/run_and_cancel_later", Test, NULL, setup, test_run_and_cancel_later, teardown);
587 : :
588 : : /* Valgrind seems to have problems with g_spawn_async_xxx() failing */
589 [ + - ]: 1 : if (!egg_testing_on_valgrind ())
590 : 1 : g_test_add ("/gcr/gnupg-process/run_bad_executable", Test, NULL, setup, test_run_bad_executable, teardown);
591 : :
592 : 1 : return egg_tests_run_with_loop ();
593 : : }
|