#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include <crazy_linker.h>
#include "test_util.h"
#define VARNAME "TEST_VAR"
static const char kJniLibName[] = "libcrazy_linker_tests_libjni_lib.so";
static void* kJavaVM = (void*)0xdeadcafe;
int main() {
crazy_context_t* context = crazy_context_create();
crazy_library_t* library;
crazy_add_search_path_for_address((void*)&main);
crazy_set_java_vm(kJavaVM, JNI_VERSION_1_2);
setenv(VARNAME, "INIT", 1);
if (!crazy_library_open(&library, kJniLibName, context))
Panic("Could not open library: %s\n", crazy_context_get_error(context));
const char* env = getenv(VARNAME);
if (strcmp(env, "LOADED"))
Panic("JNI_OnLoad() hook was not called! %s is %s\n", VARNAME, env);
crazy_library_close(library);
env = getenv(VARNAME);
if (strcmp(env, "UNLOADED"))
Panic("JNI_OnUnload() hook was not called! %s is %s\n", VARNAME, env);
crazy_set_java_vm(kJavaVM, JNI_VERSION_1_6);
setenv(VARNAME, "INIT", 1);
if (crazy_library_open(&library, kJniLibName, context))
Panic("Could load the library with JNI_VERSION_1_6 > JNI_VERSION_1_2.");
crazy_set_java_vm(nullptr, 0);
setenv(VARNAME, "INIT", 1);
if (!crazy_library_open(&library, kJniLibName, context))
Panic("Could not load the library without a JavaVM handle !?\n");
env = getenv(VARNAME);
if (strcmp(env, "INIT"))
Panic("JNI_OnLoad() was called, %s is %s (expected INIT)\n", VARNAME, env);
crazy_library_close(library);
env = getenv(VARNAME);
if (strcmp(env, "INIT"))
Panic(
"JNI_OnUnload() was called, %s is %s (expected INIT)\n", VARNAME, env);
crazy_context_destroy(context);
return 0;
}