2 * This file Copyright (C) 2017 Mnemosyne LLC
4 * It may be used under the GNU GPL versions 2 or 3
5 * or any future license endorsed by Mnemosyne LLC.
11 #include "transmission.h"
14 #include "subprocess.h"
17 #include "libtransmission-test.h"
19 static char arg_dump_args[] = "--dump-args";
20 static char arg_dump_env[] = "--dump-env";
21 static char arg_dump_cwd[] = "--dump-cwd";
23 static char* self_path = NULL;
25 static int test_spawn_async_missing_exe(void)
27 char missing_exe_path[] = TR_IF_WIN32("C:\\", "/") "tr-missing-test-exe" TR_IF_WIN32(".exe", "");
35 tr_error* error = NULL;
36 bool const ret = tr_spawn_async(args, NULL, NULL, &error);
37 check_bool(ret, ==, false);
38 check_ptr(error, !=, NULL);
39 check_int(error->code, !=, 0);
40 check_str(error->message, !=, NULL);
42 tr_error_clear(&error);
47 static int test_spawn_async_args(void)
49 char* const test_dir = libtest_sandbox_create();
50 char* const result_path = tr_sys_path_native_separators(tr_buildPath(test_dir, "result.txt", NULL));
51 bool const allow_batch_metachars = TR_IF_WIN32(false, true) || !tr_str_has_suffix(self_path, ".cmd");
53 char test_arg_1[] = "arg1 ";
54 char test_arg_2[] = " arg2";
55 char test_arg_3[] = "";
56 char test_arg_4[] = "\"arg3'^! $PATH %PATH% \\";
66 allow_batch_metachars ? test_arg_4 : NULL,
70 tr_error* error = NULL;
71 bool const ret = tr_spawn_async(args, NULL, NULL, &error);
72 check_bool(ret, ==, true);
73 check_ptr(error, ==, NULL);
75 while (!tr_sys_path_exists(result_path, NULL))
80 tr_sys_file_t fd = tr_sys_file_open(result_path, TR_SYS_FILE_READ, 0, NULL);
81 check_int(fd, !=, TR_BAD_SYS_FILE);
85 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
86 check_str(buffer, ==, test_arg_1);
88 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
89 check_str(buffer, ==, test_arg_2);
91 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
92 check_str(buffer, ==, test_arg_3);
94 if (allow_batch_metachars)
96 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
97 check_str(buffer, ==, test_arg_4);
100 check(!tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
102 tr_sys_file_close(fd, NULL);
104 tr_free(result_path);
105 libtest_sandbox_destroy(test_dir);
110 static int test_spawn_async_env(void)
112 char* const test_dir = libtest_sandbox_create();
113 char* const result_path = tr_sys_path_native_separators(tr_buildPath(test_dir, "result.txt", NULL));
115 char test_env_key_1[] = "VAR1";
116 char test_env_key_2[] = "_VAR_2_";
117 char test_env_key_3[] = "vAr#";
118 char test_env_key_4[] = "FOO";
119 char test_env_key_5[] = "ZOO";
120 char test_env_key_6[] = "TR_MISSING_TEST_ENV_KEY";
122 char test_env_value_1[] = "value1 ";
123 char test_env_value_2[] = " value2";
124 char test_env_value_3[] = " \"value3'^! $PATH %PATH% ";
125 char test_env_value_4[] = "bar";
126 char test_env_value_5[] = "jar";
144 tr_strdup_printf("%s=%s", test_env_key_1, test_env_value_1),
145 tr_strdup_printf("%s=%s", test_env_key_2, test_env_value_2),
146 tr_strdup_printf("%s=%s", test_env_key_3, test_env_value_3),
147 tr_strdup_printf("%s=%s", test_env_key_5, test_env_value_5),
152 char foo_env_value[] = "FOO=bar";
153 putenv(foo_env_value);
156 char zoo_env_value[] = "ZOO=tar";
157 putenv(zoo_env_value);
159 tr_error* error = NULL;
160 bool const ret = tr_spawn_async(args, env, NULL, &error);
161 check_bool(ret, ==, true);
162 check_ptr(error, ==, NULL);
164 while (!tr_sys_path_exists(result_path, NULL))
169 tr_sys_file_t fd = tr_sys_file_open(result_path, TR_SYS_FILE_READ, 0, NULL);
170 check_int(fd, !=, TR_BAD_SYS_FILE);
174 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
175 check_str(buffer, ==, test_env_value_1);
177 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
178 check_str(buffer, ==, test_env_value_2);
180 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
181 check_str(buffer, ==, test_env_value_3);
183 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
184 check_str(buffer, ==, test_env_value_4);
186 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
187 check_str(buffer, ==, test_env_value_5);
189 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
190 check_str(buffer, ==, "<null>");
192 check(!tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
194 tr_sys_file_close(fd, NULL);
196 tr_free_ptrv((void* const*)env);
197 tr_free(result_path);
198 libtest_sandbox_destroy(test_dir);
203 static int test_spawn_async_cwd_explicit(void)
205 char* const test_dir = libtest_sandbox_create();
206 char* const result_path = tr_sys_path_native_separators(tr_buildPath(test_dir, "result.txt", NULL));
216 tr_error* error = NULL;
217 bool const ret = tr_spawn_async(args, NULL, test_dir, &error);
218 check_bool(ret, ==, true);
219 check_ptr(error, ==, NULL);
221 while (!tr_sys_path_exists(result_path, NULL))
226 tr_sys_file_t fd = tr_sys_file_open(result_path, TR_SYS_FILE_READ, 0, NULL);
227 check_int(fd, !=, TR_BAD_SYS_FILE);
231 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
232 check_str(tr_sys_path_native_separators(buffer), ==, tr_sys_path_native_separators(test_dir));
234 check(!tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
236 tr_sys_file_close(fd, NULL);
238 tr_free(result_path);
239 libtest_sandbox_destroy(test_dir);
244 static int test_spawn_async_cwd_inherit(void)
246 char* const test_dir = libtest_sandbox_create();
247 char* const result_path = tr_sys_path_native_separators(tr_buildPath(test_dir, "result.txt", NULL));
249 char* const expected_cwd = tr_sys_dir_get_current(NULL);
259 tr_error* error = NULL;
260 bool const ret = tr_spawn_async(args, NULL, NULL, &error);
261 check_bool(ret, ==, true);
262 check_ptr(error, ==, NULL);
264 while (!tr_sys_path_exists(result_path, NULL))
269 tr_sys_file_t fd = tr_sys_file_open(result_path, TR_SYS_FILE_READ, 0, NULL);
270 check_int(fd, !=, TR_BAD_SYS_FILE);
274 check(tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
275 check_str(tr_sys_path_native_separators(buffer), ==, tr_sys_path_native_separators(expected_cwd));
277 check(!tr_sys_file_read_line(fd, buffer, sizeof(buffer), NULL));
279 tr_sys_file_close(fd, NULL);
281 tr_free(expected_cwd);
282 tr_free(result_path);
283 libtest_sandbox_destroy(test_dir);
288 static int test_spawn_async_cwd_missing(void)
290 char* const test_dir = libtest_sandbox_create();
291 char* const result_path = tr_sys_path_native_separators(tr_buildPath(test_dir, "result.txt", NULL));
301 tr_error* error = NULL;
302 bool const ret = tr_spawn_async(args, NULL, TR_IF_WIN32("C:\\", "/") "tr-missing-test-work-dir", &error);
303 check_bool(ret, ==, false);
304 check_ptr(error, !=, NULL);
305 check_int(error->code, !=, 0);
306 check_str(error->message, !=, NULL);
308 tr_error_clear(&error);
310 tr_free(result_path);
311 libtest_sandbox_destroy(test_dir);
316 int main(int argc, char** argv)
318 self_path = tr_sys_path_resolve(argv[0], NULL);
322 char* const result_path = argv[1];
323 char* const test_action = argv[2];
325 char* const tmp_result_path = tr_strdup_printf("%s.tmp", result_path);
327 tr_sys_file_t const fd = tr_sys_file_open(tmp_result_path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE |
328 TR_SYS_FILE_TRUNCATE, 0644, NULL);
330 if (fd == TR_BAD_SYS_FILE)
332 tr_free(tmp_result_path);
336 if (strcmp(test_action, arg_dump_args) == 0)
338 for (int i = 3; i < argc; ++i)
340 tr_sys_file_write_line(fd, argv[i], NULL);
343 else if (strcmp(test_action, arg_dump_env) == 0)
345 for (int i = 3; i < argc; ++i)
347 char* const value = tr_env_get_string(argv[i], "<null>");
348 tr_sys_file_write_line(fd, value, NULL);
352 else if (strcmp(test_action, arg_dump_cwd) == 0)
354 char* const value = tr_sys_dir_get_current(NULL);
355 tr_sys_file_write_line(fd, value != NULL ? value : "<null>", NULL);
360 tr_sys_file_close(fd, NULL);
361 tr_sys_path_remove(tmp_result_path, NULL);
363 tr_free(tmp_result_path);
367 tr_sys_file_close(fd, NULL);
368 tr_sys_path_rename(tmp_result_path, result_path, NULL);
370 tr_free(tmp_result_path);
374 testFunc const tests[] =
376 test_spawn_async_missing_exe,
377 test_spawn_async_args,
378 test_spawn_async_env,
379 test_spawn_async_cwd_explicit,
380 test_spawn_async_cwd_inherit,
381 test_spawn_async_cwd_missing
384 int ret = runTests(tests, NUM_TESTS(tests));
388 strcpy(self_path + strlen(self_path) - 4, ".cmd");
390 int ret2 = runTests(tests, NUM_TESTS(tests));