]> git.saurik.com Git - apple/objc4.git/blob - test/libraryPath.c
objc4-787.1.tar.gz
[apple/objc4.git] / test / libraryPath.c
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.
11 int main() {
12 char *dyldLibraryPath = getenv("DYLD_LIBRARY_PATH");
13 testprintf("DYLD_LIBRARY_PATH is %s\n", dyldLibraryPath);
14 if (dyldLibraryPath != NULL && strlen(dyldLibraryPath) > 0) {
15 int foundMatch = 0;
16
17 dyldLibraryPath = strdup(dyldLibraryPath);
18
19 Dl_info info;
20 int success = dladdr((void *)objc_msgSend, &info);
21 testassert(success);
22
23 testprintf("libobjc is located at %s\n", info.dli_fname);
24
25 char *cursor = dyldLibraryPath;
26 char *path;
27 while ((path = strsep(&cursor, ":"))) {
28 char *resolved = realpath(path, NULL);
29 testprintf("Resolved %s to %s\n", path, resolved);
30 testprintf("Comparing %s and %s\n", resolved, info.dli_fname);
31 int comparison = strncmp(resolved, info.dli_fname, strlen(resolved));
32 free(resolved);
33 if (comparison == 0) {
34 testprintf("Found a match!\n");
35 foundMatch = 1;
36 break;
37 }
38 }
39
40 testprintf("Finished searching, foundMatch=%d\n", foundMatch);
41 testassert(foundMatch);
42 }
43 succeed(__FILE__);
44 }