#include <stdio.h> // fprintf(), NULL
#include <stdlib.h> // exit(), EXIT_SUCCESS
#include <dlfcn.h>
#include "test.h" // PASS(), FAIL(), XPASS(), XFAIL()
int main()
{
Dl_info info;
void* handleFoo = dlopen("libfoo.dylib", RTLD_LAZY);
if ( handleFoo == NULL ) {
FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libfoo.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* handleBar = dlopen("libbar.dylib", RTLD_LAZY);
if ( handleBar == NULL ) {
FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libbar.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* handleBaz = dlopen("libbaz.dylib", RTLD_LAZY);
if ( handleBaz == NULL ) {
FAIL("dlclose-dylib-dynamic-ref: dlopen(\"libbaz.dylib\", RTLD_LAZY) failed with dlerror()=%s", dlerror());
exit(0);
}
void* sym_foo = dlsym(handleFoo, "foo");
if ( sym_foo == NULL ) {
FAIL("dlclose-dylib-dynamic-ref: dlsym(handleFoo, \"foo\") failed");
exit(0);
}
void* sym_bar = dlsym(handleBar, "bar");
if ( sym_bar == NULL ) {
FAIL("dlclose-dylib-dynamic-ref: dlsym(handleBar, \"bar\") failed");
exit(0);
}
void* sym_baz = dlsym(handleBaz, "baz");
if ( sym_baz == NULL ) {
FAIL("dlclose-dylib-dynamic-ref: dlsym(handleBaz, \"baz\") failed");
exit(0);
}
if ( dlclose(handleFoo) != 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
exit(0);
}
if ( dladdr(sym_foo, &info) == 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_base) == 0, but should have succeeded");
exit(0);
}
if ( dlclose(handleBar) != 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
exit(0);
}
if ( dladdr(sym_bar, &info) != 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_bar) != 0, but should have failed");
exit(0);
}
if ( dladdr(sym_foo, &info) == 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_base) == 0, but should have succeeded");
exit(0);
}
if ( dlclose(handleBaz) != 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dlclose(handleBar) != 0, dlerrr()=%s", dlerror());
exit(0);
}
if ( dladdr(sym_baz, &info) != 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_baz) != 0, but should have failed");
exit(0);
}
if ( dladdr(sym_foo, &info) != 0 ) {
FAIL("dlclose-dylib-dynamic-ref: dladdr(sym_foo) == 0, but should have succeeded");
exit(0);
}
PASS("dlclose-dylib-dynamic-ref");
return EXIT_SUCCESS;
}