]> git.saurik.com Git - apple/objc4.git/blob - test/libraryPath.c
objc4-818.2.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(int argc __unused, char **argv) {
12 char *containingDirectory = realpath(dirname(argv[0]), NULL);
13 testprintf("containingDirectory is %s\n", containingDirectory);
14
15 char *dyldLibraryPath = getenv("DYLD_LIBRARY_PATH");
16 testprintf("DYLD_LIBRARY_PATH is %s\n", dyldLibraryPath);
17
18 if (dyldLibraryPath != NULL && strlen(dyldLibraryPath) > 0) {
19 int foundMatch = 0;
20 int foundNonMatch = 0;
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);
35 if (strcmp(resolved, containingDirectory) == 0) {
36 testprintf("This is equal to our containing directory, ignoring.\n");
37 continue;
38 }
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;
46 } else {
47 foundNonMatch = 1;
48 }
49 }
50
51 testprintf("Finished searching, foundMatch=%d foundNonMatch=%d\n", foundMatch, foundNonMatch);
52 testassert(foundMatch || !foundNonMatch);
53 }
54 succeed(__FILE__);
55 }