dyld-750.5.tar.gz
[apple/dyld.git] / testing / test-cases / dlopen-RTLD_NOLOAD.dtest / main.c
1
2 // BUILD: $CC foo.c -dynamiclib -install_name $RUN_DIR/libfoo.dylib -o $BUILD_DIR/libfoo.dylib
3 // BUILD: $CC main.c -DRUN_DIR="$RUN_DIR" $BUILD_DIR/libfoo.dylib -o $BUILD_DIR/dlopen-RTLD_NOLOAD-basic.exe
4 // BUILD: $SYMLINK libfoo.dylib $BUILD_DIR/libfoo-sym.dylib
5
6 // RUN: ./dlopen-RTLD_NOLOAD-basic.exe
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <dlfcn.h>
12
13 #include "test_support.h"
14
15 int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
16 ///
17 /// This tests that RTLD_NOLOAD finds existing dylib statically linked
18 ///
19 void* handle = dlopen(RUN_DIR "/libfoo.dylib", RTLD_NOLOAD);
20 if ( handle == NULL ) {
21 FAIL("dlopen(\"libfoo.dylib\", RTLD_NOLOAD) failed but it should have worked: %s", dlerror());
22 }
23 void* sym = dlsym(handle, "foo");
24 if ( sym == NULL ) {
25 FAIL("dlsym(handle, \"foo\") failed but it should have worked: %s", dlerror());
26 }
27
28 ///
29 /// This tests that RTLD_NOLOAD verifies that non-existant dylib returns NULL
30 ///
31 void* handle2 = dlopen(RUN_DIR "/libfobbulate.dylib", RTLD_NOLOAD);
32 if ( handle2 != NULL ) {
33 FAIL("dlopen(\"libfobbulate.dylib\", RTLD_NOLOAD) succeeded but it should have failed");
34 }
35
36
37 ///
38 /// This tests that RTLD_NOLOAD finds symlink to existing dylib
39 ///
40 void* handle3 = dlopen(RUN_DIR "/libfoo-sym.dylib", RTLD_NOLOAD);
41 if ( handle3 == NULL ) {
42 FAIL("dlopen(\"libfoo-sym.dylib\", RTLD_NOLOAD) failed but it should have worked: %s", dlerror());
43 }
44
45
46 ///
47 /// This tests that RTLD_NOLOAD of something in the dyld cache that is not yet loaded returns NULL
48 ///
49 void* handle4 = dlopen("/usr/lib/libz.1.dylib", RTLD_NOLOAD);
50 if ( handle4 != NULL ) {
51 FAIL("dlopen(\"libz.dylib\", RTLD_NOLOAD) worked but it should have failed");
52 }
53
54 PASS("Success");
55 }