From fa1da1854946bf9517ca8c0e76dff8e41bb35963 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Fri, 27 Mar 2020 23:37:51 +0100 Subject: [PATCH] test: canonicalize argv[0] in exepath test Commit ff29322b ("test: canonicalize test runner path") from 2014 changed the test runner to call `realpath(3)` on `argv[0]` in order to fix the `get_currentexe` test failing with the autotools build when the executable path contained symbolic links but that is now causing the `spawn_reads_child_path` test to fail on z/os with the cmake build. Fix that by only doing path canonicalization in the `get_currentexe` test, not always. An auxiliary fix is applied to the `process_title_threadsafe` test because it assumed that setting the process title to a long string, then reading it back produces in the original string. On some platforms however the maximum size of the process title is limited to the size of the `argv` vector. Because the test runner used absolute paths until now, the argv vector was bigger than it is with relative paths, big enough to let this bad assumption go unnoticed until now. Minor fixes are applied to tests that assumed 1024 for the maximum path size because this commit makes visible that some of the CI matrix systems support much longer paths. PR-URL: https://github.com/libuv/libuv/pull/2755 Refs: https://github.com/libuv/libuv/pull/2737#issuecomment-602800431 Refs: https://github.com/libuv/libuv/pull/2754#issuecomment-604015785 Reviewed-By: Richard Lau --- test/runner-unix.c | 9 ++------- test/runner.h | 4 ---- test/test-cwd-and-chdir.c | 2 +- test/test-fs.c | 2 +- test/test-get-currentexe.c | 21 +++++++++++---------- test/test-homedir.c | 2 +- test/test-process-title-threadsafe.c | 17 +++++++++++++---- test/test-tmpdir.c | 2 +- 8 files changed, 30 insertions(+), 29 deletions(-) diff --git a/test/runner-unix.c b/test/runner-unix.c index dbb33bfa..7a5fa945 100644 --- a/test/runner-unix.c +++ b/test/runner-unix.c @@ -26,7 +26,7 @@ #include /* uintptr_t */ #include -#include /* readlink, usleep */ +#include /* usleep */ #include /* strdup */ #include #include @@ -72,12 +72,7 @@ int platform_init(int argc, char **argv) { setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); signal(SIGPIPE, SIG_IGN); - - if (realpath(argv[0], executable_path) == NULL) { - perror("realpath"); - return -1; - } - + snprintf(executable_path, sizeof(executable_path), "%s", argv[0]); return 0; } diff --git a/test/runner.h b/test/runner.h index 1a339508..ddb1e417 100644 --- a/test/runner.h +++ b/test/runner.h @@ -84,11 +84,7 @@ typedef struct { #define TEST_HELPER HELPER_ENTRY #define BENCHMARK_HELPER HELPER_ENTRY -#ifdef PATH_MAX -extern char executable_path[PATH_MAX]; -#else extern char executable_path[4096]; -#endif /* * Include platform-dependent definitions diff --git a/test/test-cwd-and-chdir.c b/test/test-cwd-and-chdir.c index 451566b5..faeed021 100644 --- a/test/test-cwd-and-chdir.c +++ b/test/test-cwd-and-chdir.c @@ -23,7 +23,7 @@ #include "task.h" #include -#define PATHMAX 1024 +#define PATHMAX 4096 TEST_IMPL(cwd_and_chdir) { char buffer_orig[PATHMAX]; diff --git a/test/test-fs.c b/test/test-fs.c index b6b2b199..d491efb4 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -55,7 +55,7 @@ #endif #define TOO_LONG_NAME_LENGTH 65536 -#define PATHMAX 1024 +#define PATHMAX 4096 typedef struct { const char* path; diff --git a/test/test-get-currentexe.c b/test/test-get-currentexe.c index 0e9d6965..8c730334 100644 --- a/test/test-get-currentexe.c +++ b/test/test-get-currentexe.c @@ -23,28 +23,29 @@ #include "task.h" #include -#define PATHMAX 1024 +#ifndef _WIN32 +#include +#endif + +#define PATHMAX 4096 extern char executable_path[]; TEST_IMPL(get_currentexe) { char buffer[PATHMAX]; + char path[PATHMAX]; size_t size; char* match; - char* path; int r; size = sizeof(buffer) / sizeof(buffer[0]); r = uv_exepath(buffer, &size); ASSERT(!r); - /* uv_exepath can return an absolute path on darwin, so if the test runner - * was run with a relative prefix of "./", we need to strip that prefix off - * executable_path or we'll fail. */ - if (executable_path[0] == '.' && executable_path[1] == '/') { - path = executable_path + 2; - } else { - path = executable_path; - } +#ifdef _WIN32 + snprintf(path, sizeof(path), "%s", executable_path); +#else + ASSERT(NULL != realpath(executable_path, path)); +#endif match = strstr(buffer, path); /* Verify that the path returned from uv_exepath is a subdirectory of diff --git a/test/test-homedir.c b/test/test-homedir.c index 856534a4..508351f7 100644 --- a/test/test-homedir.c +++ b/test/test-homedir.c @@ -23,7 +23,7 @@ #include "task.h" #include -#define PATHMAX 1024 +#define PATHMAX 4096 #define SMALLPATH 1 TEST_IMPL(homedir) { diff --git a/test/test-process-title-threadsafe.c b/test/test-process-title-threadsafe.c index 2f12b74e..3b4168be 100644 --- a/test/test-process-title-threadsafe.c +++ b/test/test-process-title-threadsafe.c @@ -40,14 +40,23 @@ static const char* titles[] = { static void getter_thread_body(void* arg) { char buffer[512]; + size_t len; for (;;) { ASSERT(0 == uv_get_process_title(buffer, sizeof(buffer))); + + /* The maximum size of the process title on some platforms depends on + * the total size of the argv vector. It's therefore possible to read + * back a title that's shorter than what we submitted. + */ + len = strlen(buffer); + ASSERT_GT(len, 0); + ASSERT( - 0 == strcmp(buffer, titles[0]) || - 0 == strcmp(buffer, titles[1]) || - 0 == strcmp(buffer, titles[2]) || - 0 == strcmp(buffer, titles[3])); + 0 == strncmp(buffer, titles[0], len) || + 0 == strncmp(buffer, titles[1], len) || + 0 == strncmp(buffer, titles[2], len) || + 0 == strncmp(buffer, titles[3], len)); uv_sleep(0); } diff --git a/test/test-tmpdir.c b/test/test-tmpdir.c index dac488d0..86f72e25 100644 --- a/test/test-tmpdir.c +++ b/test/test-tmpdir.c @@ -23,7 +23,7 @@ #include "task.h" #include -#define PATHMAX 1024 +#define PATHMAX 4096 #define SMALLPATH 1 TEST_IMPL(tmpdir) {