From a9f644c24f0b2cccf5b38872a50812efb2dd1e79 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 24 Apr 2020 12:26:16 -0400 Subject: Add spm_shlib_deps test --- tests/test_shlib_spm_shlib_deps.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/test_shlib_spm_shlib_deps.c (limited to 'tests/test_shlib_spm_shlib_deps.c') diff --git a/tests/test_shlib_spm_shlib_deps.c b/tests/test_shlib_spm_shlib_deps.c new file mode 100644 index 0000000..2320ea1 --- /dev/null +++ b/tests/test_shlib_spm_shlib_deps.c @@ -0,0 +1,30 @@ +#include "spm.h" +#include "framework.h" + +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 && spmerrno == EINVAL) { + // expected failure + 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); + myassert(exists(item) == testCase[i].truthValue.signed_integer, + "library record found, but does not exist: '%s' (your OS is broken)\n", item); + } + strlist_free(result); + } + return 0; +} \ No newline at end of file -- cgit From 532273c0a91e729bdaf5e522bc2e827ec7f5fd8f Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 24 Apr 2020 12:32:53 -0400 Subject: Remove narrow conditional --- tests/test_shlib_spm_shlib_deps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_shlib_spm_shlib_deps.c') diff --git a/tests/test_shlib_spm_shlib_deps.c b/tests/test_shlib_spm_shlib_deps.c index 2320ea1..40e52f8 100644 --- a/tests/test_shlib_spm_shlib_deps.c +++ b/tests/test_shlib_spm_shlib_deps.c @@ -12,7 +12,7 @@ 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 && spmerrno == EINVAL) { + if (result == NULL && testCase[i].truthValue.signed_integer < 0) { // expected failure continue; } -- cgit From 7b6c6cb4cad10e3aac170dc4204b4ca782974a28 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 24 Apr 2020 13:42:39 -0400 Subject: Find libraries on darwin and linux --- tests/test_shlib_spm_shlib_deps.c | 45 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'tests/test_shlib_spm_shlib_deps.c') diff --git a/tests/test_shlib_spm_shlib_deps.c b/tests/test_shlib_spm_shlib_deps.c index 40e52f8..46a06db 100644 --- a/tests/test_shlib_spm_shlib_deps.c +++ b/tests/test_shlib_spm_shlib_deps.c @@ -1,6 +1,45 @@ #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(path, DIRSEPS, LIBRARY_SEARCH_PATH[i], name); + 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}, @@ -9,11 +48,13 @@ struct TestCase testCase[] = { }; 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; } @@ -21,8 +62,10 @@ int main(int argc, char *argv[]) { myassert(result != NULL, "case %zu: unexpected NULL", i); for (size_t j = 0; j < strlist_count(result); j++) { char *item = strlist_item(result, j); - myassert(exists(item) == testCase[i].truthValue.signed_integer, + 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); } -- cgit From 24992f1f426111f0ce05df341087a55486ae48db Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 24 Apr 2020 14:13:37 -0400 Subject: Fix invocation of join_ex() --- tests/test_shlib_spm_shlib_deps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests/test_shlib_spm_shlib_deps.c') diff --git a/tests/test_shlib_spm_shlib_deps.c b/tests/test_shlib_spm_shlib_deps.c index 46a06db..4f0bfd4 100644 --- a/tests/test_shlib_spm_shlib_deps.c +++ b/tests/test_shlib_spm_shlib_deps.c @@ -30,7 +30,7 @@ static char *find_library(const char *name) { } for (size_t i = 0; LIBRARY_SEARCH_PATH[i] != NULL; i++) { - path = join_ex(path, DIRSEPS, LIBRARY_SEARCH_PATH[i], name); + path = join_ex(DIRSEPS, LIBRARY_SEARCH_PATH[i], name, NULL); if (path != NULL && exists(path) == 0) { break; } -- cgit