]> git.saurik.com Git - apple/dyld.git/blob - testing/test-cases/dlopen-DYLD_LIBRARY_PATH.dtest/main.c
dyld-733.6.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-DYLD_LIBRARY_PATH.dtest / main.c
1
2 // BUILD: mkdir -p $BUILD_DIR/door1 $BUILD_DIR/door2
3 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/door1/libbar.dylib -install_name $RUN_DIR/libbar.dylib -DVALUE=3
4 // BUILD: $CC bar.c -dynamiclib -o $BUILD_DIR/door2/libbar.dylib -install_name $RUN_DIR/libbar.dylib -DVALUE=17
5 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/door1/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib -DVALUE=10 $BUILD_DIR/door1/libbar.dylib
6 // BUILD: $CC foo.c -dynamiclib -o $BUILD_DIR/door2/libfoo.dylib -install_name $RUN_DIR/libfoo.dylib -DVALUE=25 $BUILD_DIR/door2/libbar.dylib
7 // BUILD: $CC main.c -o $BUILD_DIR/dlopen-DYLD_LIBRARY_PATH.exe
8 // BUILD: $DYLD_ENV_VARS_ENABLE $BUILD_DIR/dlopen-DYLD_LIBRARY_PATH.exe
9
10 // RUN: DYLD_LIBRARY_PATH=$RUN_DIR/door1/ ./dlopen-DYLD_LIBRARY_PATH.exe 13
11 // RUN: DYLD_LIBRARY_PATH=$RUN_DIR/door2 ./dlopen-DYLD_LIBRARY_PATH.exe 42
12 // RUN: DYLD_LIBRARY_PATH=$RUN_DIR/door3/:$RUN_DIR/door2/ ./dlopen-DYLD_LIBRARY_PATH.exe 42
13
14 #include <stdio.h>
15 #include <dlfcn.h>
16 #include <stdlib.h>
17
18 // Program dlopen()s libfoo.dylib which was linked against libbar.dylib
19 // Neither have valid paths and must be found via DYLD_LIBRARY_PATH
20 // This test direct and indirect loading.
21
22 int main(int argc, const char* argv[])
23 {
24 const char* env = getenv("DYLD_LIBRARY_PATH");
25 if ( env == NULL ) {
26 printf("[BEGIN] dlopen-DYLD_LIBRARY_PATH\n");
27 printf("[FAIL] dlopen-DYLD_LIBRARY_PATH, env not set\n");
28 return 0;
29 }
30 const char* valueStr = argv[1];
31 if ( valueStr == NULL ) {
32 printf("[BEGIN] dlopen-DYLD_LIBRARY_PATH\n");
33 printf("[FAIL] dlopen-DYLD_LIBRARY_PATH, arg1 value not set\n");
34 return 0;
35 }
36 char* end;
37 long value = strtol(valueStr, &end, 0);
38
39 printf("[BEGIN] dlopen-DYLD_LIBRARY_PATH %s\n", env);
40
41 void* handle = dlopen("/bogus/libfoo.dylib", RTLD_LAZY);
42 if ( handle == NULL ) {
43 printf("dlerror(): %s\n", dlerror());
44 printf("[FAIL] dlopen-DYLD_LIBRARY_PATH %s\n", env);
45 return 0;
46 }
47
48 typedef int (*FooProc)();
49
50 FooProc sym = (FooProc)dlsym(handle, "foo");
51 if ( sym == NULL ) {
52 printf("dlerror(): %s\n", dlerror());
53 printf("[FAIL] dlopen-DYLD_LIBRARY_PATH %s\n", env);
54 return 0;
55 }
56
57 int result = (*sym)();
58 if ( result != value ) {
59 printf("result=%d, expected %ld (str=%s)\n", result, value, valueStr);
60 printf("[FAIL] dlopen-DYLD_LIBRARY_PATH %s\n", env);
61 return 0;
62 }
63
64 int r = dlclose(handle);
65 if ( r != 0 ) {
66 printf("dlclose() returned %d\n", r);
67 printf("[FAIL] dlopen-DYLD_LIBRARY_PATH %s\n", env);
68 return 0;
69 }
70
71 void* handle2 = dlopen("/junk/libfoo.dylib", RTLD_LAZY);
72 if ( handle2 == NULL ) {
73 printf("dlerror(): %s\n", dlerror());
74 printf("[FAIL] dlopen-DYLD_LIBRARY_PATH %s\n", env);
75 return 0;
76 }
77
78
79
80 printf("[PASS] dlopen-DYLD_LIBRARY_PATH %s\n", env);
81
82 return 0;
83 }
84