]>
Commit | Line | Data |
---|---|---|
bc4fafce A |
1 | // TEST_CFLAGS -lobjc |
2 | ||
3 | #include "test.h" | |
4 | #include <dlfcn.h> | |
5 | ||
6 | // We use DYLD_LIBRARY_PATH to run the tests against a particular copy of | |
7 | // libobjc. If this fails somehow (path is wrong, codesigning prevents loading, | |
8 | // etc.) then the typical result is a silent failure and we end up testing | |
9 | // /usr/lib/libobjc.A.dylib instead. This test detects when DYLD_LIBRARY_PATH is | |
10 | // set but libobjc isn't loaded from it. | |
34d5b5e8 A |
11 | int main(int argc __unused, char **argv) { |
12 | char *containingDirectory = realpath(dirname(argv[0]), NULL); | |
13 | testprintf("containingDirectory is %s\n", containingDirectory); | |
14 | ||
bc4fafce A |
15 | char *dyldLibraryPath = getenv("DYLD_LIBRARY_PATH"); |
16 | testprintf("DYLD_LIBRARY_PATH is %s\n", dyldLibraryPath); | |
34d5b5e8 | 17 | |
bc4fafce A |
18 | if (dyldLibraryPath != NULL && strlen(dyldLibraryPath) > 0) { |
19 | int foundMatch = 0; | |
34d5b5e8 | 20 | int foundNonMatch = 0; |
bc4fafce A |
21 | |
22 | dyldLibraryPath = strdup(dyldLibraryPath); | |
23 | ||
24 | Dl_info info; | |
25 | int success = dladdr((void *)objc_msgSend, &info); | |
26 | testassert(success); | |
27 | ||
28 | testprintf("libobjc is located at %s\n", info.dli_fname); | |
29 | ||
30 | char *cursor = dyldLibraryPath; | |
31 | char *path; | |
32 | while ((path = strsep(&cursor, ":"))) { | |
33 | char *resolved = realpath(path, NULL); | |
34 | testprintf("Resolved %s to %s\n", path, resolved); | |
34d5b5e8 A |
35 | if (strcmp(resolved, containingDirectory) == 0) { |
36 | testprintf("This is equal to our containing directory, ignoring.\n"); | |
37 | continue; | |
38 | } | |
bc4fafce A |
39 | testprintf("Comparing %s and %s\n", resolved, info.dli_fname); |
40 | int comparison = strncmp(resolved, info.dli_fname, strlen(resolved)); | |
41 | free(resolved); | |
42 | if (comparison == 0) { | |
43 | testprintf("Found a match!\n"); | |
44 | foundMatch = 1; | |
45 | break; | |
34d5b5e8 A |
46 | } else { |
47 | foundNonMatch = 1; | |
bc4fafce A |
48 | } |
49 | } | |
50 | ||
34d5b5e8 A |
51 | testprintf("Finished searching, foundMatch=%d foundNonMatch=%d\n", foundMatch, foundNonMatch); |
52 | testassert(foundMatch || !foundNonMatch); | |
bc4fafce A |
53 | } |
54 | succeed(__FILE__); | |
55 | } |