#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include "test_support.h"
extern int interposableFoo();
extern int interposableBar();
extern int callFunc();
static void tryImage(const char* path, int expectedFoo, int expectedBar)
{
void* handle = dlopen(path, RTLD_LAZY);
if ( handle == NULL ) {
FAIL("dlopen(\"%s\") error: %s", path, dlerror());
}
__typeof(&callFunc) callFooSym = (__typeof(&callFunc))dlsym(handle, "callFoo");
if ( callFooSym == NULL ) {
FAIL("dlsym(\"callFoo\") error: %s", dlerror());
}
int fooResult = callFooSym();
if ( fooResult != expectedFoo ) {
FAIL("callFoo() from %s not interposed as it returned %d", path, fooResult);
}
__typeof(&callFunc) callBarSym = (__typeof(&callFunc))dlsym(handle, "callBar");
if ( callBarSym == NULL ) {
FAIL("dlsym(\"callBar\") error: %s", dlerror());
}
int barResult = callBarSym();
if ( barResult != expectedBar ) {
FAIL("callBar() from %s not interposed as it returned %d", path, barResult);
}
}
int main(int argc, const char* argv[], const char* envp[], const char* apple[]) {
tryImage(RUN_DIR "/libfoo.dylib", 4, 2);
tryImage(RUN_DIR "/libbar.bundle", 4, 100);
PASS("Success");
}