Mercurial > pub > dyncall > dyncall
comparison test/dynload_plain/dynload_plain.c @ 314:b2e4e23d9953
- stop using dlinfo() on glibc platforms but use dl_iterate_phdr() instead, as former's implementation is nothing more than a fancy cast and thus dangerously assuming that every provided handle is valid
- dynload_plain test
* now testing getting exec's path
* workdir independent
author | Tassilo Philipp |
---|---|
date | Wed, 06 Nov 2019 12:32:53 +0100 |
parents | 73b5b9e224e2 |
children | 3124f4c4f293 |
comparison
equal
deleted
inserted
replaced
313:73b5b9e224e2 | 314:b2e4e23d9953 |
---|---|
32 # include <io.h> | 32 # include <io.h> |
33 # define F_OK 0 | 33 # define F_OK 0 |
34 #else | 34 #else |
35 # include <unistd.h> | 35 # include <unistd.h> |
36 #endif | 36 #endif |
37 #include <libgen.h> | |
37 | 38 |
38 | 39 |
39 int strlen_utf8(const char *s) | 40 int strlen_utf8(const char *s) |
40 { | 41 { |
41 int i=0, j=0; | 42 int i=0, j=0; |
56 /* can be specified in Makefile; this avoids trying to write portable directory traversal stuff */ | 57 /* can be specified in Makefile; this avoids trying to write portable directory traversal stuff */ |
57 const char* clibs[] = { | 58 const char* clibs[] = { |
58 #if defined(DEF_C_DYLIB) | 59 #if defined(DEF_C_DYLIB) |
59 DEF_C_DYLIB, | 60 DEF_C_DYLIB, |
60 #endif | 61 #endif |
61 /* fallback guessing if not provided by Makefile */ | 62 /* fallback guessing if not provided by Makefile */ |
62 "/lib/libc.so", | 63 "/lib/libc.so", |
63 "/lib32/libc.so", | 64 "/lib32/libc.so", |
64 "/lib64/libc.so", | 65 "/lib64/libc.so", |
65 "/usr/lib/libc.so", | 66 "/usr/lib/libc.so", |
66 "/usr/lib/system/libsystem_c.dylib", /* macos */ | 67 "/usr/lib/system/libsystem_c.dylib", /* macos */ |
135 /* check if dlGetLibraryPath returns 0 when trying to lookup dummy */ | 136 /* check if dlGetLibraryPath returns 0 when trying to lookup dummy */ |
136 bs = dlGetLibraryPath((DLLib*)&r/*dummy addr*/, queriedPath, 200); | 137 bs = dlGetLibraryPath((DLLib*)&r/*dummy addr*/, queriedPath, 200); |
137 printf("path lookup failed as expected with bad lib handle: %d\n", bs == 0); | 138 printf("path lookup failed as expected with bad lib handle: %d\n", bs == 0); |
138 r += (bs == 0); | 139 r += (bs == 0); |
139 | 140 |
141 /* test getting own path */ | |
142 { | |
143 /* get own exec's path */ | |
144 bs = dlGetLibraryPath(NULL, queriedPath, 200); | |
145 printf("dynload_plain's own path is: %s\n", queriedPath); | |
146 r += (bs != 0 && strlen(queriedPath) > 0); | |
147 | |
148 /* change working dir to where our executable is, for following test */ | |
149 chdir(dirname(queriedPath)); | |
150 } | |
151 | |
140 /* test UTF-8 path through dummy library that's created by this test's build */ | 152 /* test UTF-8 path through dummy library that's created by this test's build */ |
141 { | 153 { |
142 static const char* pathU8 = "./dynload_plain_\xc3\x9f_test"; | 154 static const char* pathU8 = "./dynload_plain_\xc3\x9f_test"; |
143 int nu8c, b; | 155 int nu8c, b; |
144 | 156 |
145 //cp(pathU8, "/lib/libz.so.6"); | |
146 pLib = dlLoadLibrary(pathU8); /* check if we can load a lib with a UTF-8 path */ | 157 pLib = dlLoadLibrary(pathU8); /* check if we can load a lib with a UTF-8 path */ |
147 printf("pLib (loaded w/ UTF-8 path %s) handle: %p\n", pathU8, pLib); | 158 printf("pLib (loaded w/ UTF-8 path %s with wd being exec's dir) handle: %p\n", pathU8, pLib); |
148 r += (p != NULL); | 159 r += (p != NULL); |
149 | 160 |
150 if(pLib) { | 161 if(pLib) { |
151 /* get UTF-8 path back */ | 162 /* get UTF-8 path back */ |
152 bs = dlGetLibraryPath((DLLib*)pLib, queriedPath, 200); | 163 bs = dlGetLibraryPath((DLLib*)pLib, queriedPath, 200); |
218 else | 229 else |
219 printf("dlSymsInit failed\n"); | 230 printf("dlSymsInit failed\n"); |
220 } | 231 } |
221 | 232 |
222 /* Check final score of right ones to see if all worked */ | 233 /* Check final score of right ones to see if all worked */ |
223 r = (r == 15); | 234 r = (r == 16); |
224 printf("result: dynload_plain: %d\n", r); | 235 printf("result: dynload_plain: %d\n", r); |
225 return !r; | 236 return !r; |
226 } | 237 } |
227 | 238 |