diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/framework.h | 1 | ||||
-rw-r--r-- | tests/test_error_handler_spm_strerror.c | 4 | ||||
-rw-r--r-- | tests/test_error_handler_spmerrno_cause.c | 4 | ||||
-rw-r--r-- | tests/test_shlib_spm_shlib_deps.c | 73 |
4 files changed, 78 insertions, 4 deletions
diff --git a/tests/framework.h b/tests/framework.h index 2a7b268..312e917 100644 --- a/tests/framework.h +++ b/tests/framework.h @@ -2,6 +2,7 @@ #define SPM_FRAMEWORK_H #include <limits.h> #include <fcntl.h> +#pragma GCC diagnostic ignored "-Wunused-parameter" union TestValue { const char *sptr; diff --git a/tests/test_error_handler_spm_strerror.c b/tests/test_error_handler_spm_strerror.c index 2164f93..4a40571 100644 --- a/tests/test_error_handler_spm_strerror.c +++ b/tests/test_error_handler_spm_strerror.c @@ -3,10 +3,10 @@ const char *testFmt = "translated error code '%d': returned '%s', expected '%s'\n"; struct TestCase testCase[] = { -#if defined(__APPLE__) && defined(__MACH__) +#if OS_DARWIN {.caseValue.signed_integer = 0, .truthValue.sptr = "Undefined error: 0", .arg[0].signed_integer = 0}, {.caseValue.signed_integer = -1, .truthValue.sptr = "Unknown error: -1", .arg[0].signed_integer = 0}, -#elif defined(__linux) || defined(__linux__) +#elif OS_LINUX {.caseValue.signed_integer = 0, .truthValue.sptr = "Success", .arg[0].signed_integer = 0}, {.caseValue.signed_integer = -1, .truthValue.sptr = "Unknown error -1", .arg[0].signed_integer = 0}, #endif diff --git a/tests/test_error_handler_spmerrno_cause.c b/tests/test_error_handler_spmerrno_cause.c index c24b996..a8b47a0 100644 --- a/tests/test_error_handler_spmerrno_cause.c +++ b/tests/test_error_handler_spmerrno_cause.c @@ -3,10 +3,10 @@ const char *testFmt = "translated error code '%d': returned '%s', expected '%s'\n"; struct TestCase testCase[] = { -#if defined(__APPLE__) && defined(__MACH__) +#if OS_DARWIN {.caseValue.signed_integer = 0, .truthValue.sptr = "Undefined error: 0 (winning)", .arg[0].signed_integer = 0, .arg[1].sptr = "winning"}, {.caseValue.signed_integer = -1, .truthValue.sptr = "Unknown error: -1 (not winning)", .arg[0].signed_integer = 0, .arg[1].sptr = "not winning"}, -#elif defined(__linux) || defined(__linux__) +#elif OS_LINUX {.caseValue.signed_integer = 0, .truthValue.sptr = "Success (winning)", .arg[0].signed_integer = 0, .arg[1].sptr = "winning"}, {.caseValue.signed_integer = -1, .truthValue.sptr = "Unknown error -1 (not winning)", .arg[0].signed_integer = 0, .arg[1].sptr = "not winning"}, #endif diff --git a/tests/test_shlib_spm_shlib_deps.c b/tests/test_shlib_spm_shlib_deps.c new file mode 100644 index 0000000..4f0bfd4 --- /dev/null +++ b/tests/test_shlib_spm_shlib_deps.c @@ -0,0 +1,73 @@ +#include "spm.h" +#include "framework.h" + +static char *LIBRARY_SEARCH_PATH[] = { +#if OS_DARWIN + "/usr/lib", + "/usr/local/lib", +#elif OS_LINUX + "/lib", + "/usr/lib", + "/usr/local/lib", + "/lib64", + "/usr/lib64", + "/usr/local/lib64", +#endif + NULL, +}; + +/** + * Find a library based on known library search paths. This cannot handle macos-style `@string` paths, and + * will not follow RPATHs. + * @param name + * @return path to library (or NULL) + */ +static char *find_library(const char *name) { + char *path = NULL; + + if (strstr(name, DIRSEPS) != NULL) { + return strdup(name); + } + + for (size_t i = 0; LIBRARY_SEARCH_PATH[i] != NULL; i++) { + path = join_ex(DIRSEPS, LIBRARY_SEARCH_PATH[i], name, NULL); + if (path != NULL && exists(path) == 0) { + break; + } + free(path); + path = NULL; + } + return path; +} + +struct TestCase testCase[] = { + {.caseValue.sptr = "/bin/sh", .truthValue.signed_integer = 0}, + {.caseValue.sptr = "/usr/bin/tar", .truthValue.signed_integer = 0}, + {.caseValue.sptr = "/dev/null", .truthValue.signed_integer = -1}, // not an object + {.caseValue.sptr = NULL, .truthValue.signed_integer = -1}, // invalid call +}; +size_t numCases = sizeof(testCase) / sizeof(struct TestCase); + + +int main(int argc, char *argv[]) { + for (size_t i = 0; i < numCases; i++) { + StrList *result = shlib_deps(testCase[i].caseValue.sptr); + if (result == NULL && testCase[i].truthValue.signed_integer < 0) { + // expected failure + fprintf(stderr, "case %zu: trapped expected failure (ignore any stderr text)\n", i); + continue; + } + + myassert(spmerrno == 0, "case %zu: raised unhandled exception %d: %s\n", i, spmerrno, spm_strerror(spmerrno)); + myassert(result != NULL, "case %zu: unexpected NULL", i); + for (size_t j = 0; j < strlist_count(result); j++) { + char *item = strlist_item(result, j); + char *libpath = find_library(item); + myassert(libpath != NULL, + "library record found, but does not exist: '%s' (your OS is broken)\n", item); + free(libpath); + } + strlist_free(result); + } + return 0; +}
\ No newline at end of file |