19be387c94ff8199f8031b7f11f06c52cce5ccf6djvr// Copyright 2014 The Chromium Authors. All rights reserved.
27842e56b97ce677b83bdab09cda48bc2d89ac75aJust// Use of this source code is governed by a BSD-style license that can be
3035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod// found in the LICENSE file.
47842e56b97ce677b83bdab09cda48bc2d89ac75aJust
57842e56b97ce677b83bdab09cda48bc2d89ac75aJust// A crazy linker test to test crazy_context_set_java_vm().
6d3d7250fc3147b6333e365e5be4ff29aa92ee506pabs
77842e56b97ce677b83bdab09cda48bc2d89ac75aJust#include <jni.h>
87842e56b97ce677b83bdab09cda48bc2d89ac75aJust#include <stdio.h>
97842e56b97ce677b83bdab09cda48bc2d89ac75aJust#include <stdlib.h>
107842e56b97ce677b83bdab09cda48bc2d89ac75aJust#include <crazy_linker.h>
117842e56b97ce677b83bdab09cda48bc2d89ac75aJust
127842e56b97ce677b83bdab09cda48bc2d89ac75aJust#include "test_util.h"
137842e56b97ce677b83bdab09cda48bc2d89ac75aJust
147842e56b97ce677b83bdab09cda48bc2d89ac75aJust#define VARNAME "TEST_VAR"
157842e56b97ce677b83bdab09cda48bc2d89ac75aJust
167842e56b97ce677b83bdab09cda48bc2d89ac75aJuststatic const char kJniLibName[] = "libjni_lib.so";
17035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbodstatic void* kJavaVM = (void*)0xdeadcafe;
187842e56b97ce677b83bdab09cda48bc2d89ac75aJust
19035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbodint main() {
20035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  crazy_context_t* context = crazy_context_create();
21035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  crazy_library_t* library;
22035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod
239be387c94ff8199f8031b7f11f06c52cce5ccf6djvr  // Expect to find the library in the same directory than this executable.
24035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  crazy_context_add_search_path_for_address(context, (void*)&main);
257842e56b97ce677b83bdab09cda48bc2d89ac75aJust
26035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  crazy_context_set_java_vm(context, kJavaVM, JNI_VERSION_1_2);
27035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod
28035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  // Load libjni_lib.so, this should invoke its JNI_OnLoad() function
29035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  // automatically.
30035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  setenv(VARNAME, "INIT", 1);
31035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  if (!crazy_library_open(&library, kJniLibName, context))
327842e56b97ce677b83bdab09cda48bc2d89ac75aJust    Panic("Could not open library: %s\n", crazy_context_get_error(context));
33731e4377e13b91d906d78f6e1e8c7ca2097c71e8Just
347842e56b97ce677b83bdab09cda48bc2d89ac75aJust  const char* env = getenv(VARNAME);
35035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  if (strcmp(env, "LOADED"))
367842e56b97ce677b83bdab09cda48bc2d89ac75aJust    Panic("JNI_OnLoad() hook was not called! %s is %s\n", VARNAME, env);
37035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod
38035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  crazy_library_close(library);
39035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  env = getenv(VARNAME);
40035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  if (strcmp(env, "UNLOADED"))
41035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod    Panic("JNI_OnUnload() hook was not called! %s is %s\n", VARNAME, env);
427842e56b97ce677b83bdab09cda48bc2d89ac75aJust
437842e56b97ce677b83bdab09cda48bc2d89ac75aJust  // Now, change the minimum JNI version to JNI_VERSION_1_6, which should
447842e56b97ce677b83bdab09cda48bc2d89ac75aJust  // prevent loading the library properly, since it only supports 1.2.
457842e56b97ce677b83bdab09cda48bc2d89ac75aJust  crazy_context_set_java_vm(context, kJavaVM, JNI_VERSION_1_6);
467842e56b97ce677b83bdab09cda48bc2d89ac75aJust
477842e56b97ce677b83bdab09cda48bc2d89ac75aJust  setenv(VARNAME, "INIT", 1);
487842e56b97ce677b83bdab09cda48bc2d89ac75aJust  if (crazy_library_open(&library, kJniLibName, context))
497842e56b97ce677b83bdab09cda48bc2d89ac75aJust    Panic("Could load the library with JNI_VERSION_1_6 > JNI_VERSION_1_2.");
50035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod
511288f8ad41b183ccdc3a8657b751ce53e03953a7jvr  // Disable the feature, this shall load the library, but not call the
52ca4c45681ef2ea9290c6845f8bf61dff281fc5c4jvr  // JNI_OnLoad() hook.
531288f8ad41b183ccdc3a8657b751ce53e03953a7jvr  crazy_context_set_java_vm(context, NULL, 0);
54ca4c45681ef2ea9290c6845f8bf61dff281fc5c4jvr
557842e56b97ce677b83bdab09cda48bc2d89ac75aJust  setenv(VARNAME, "INIT", 1);
56035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  if (!crazy_library_open(&library, kJniLibName, context))
577842e56b97ce677b83bdab09cda48bc2d89ac75aJust    Panic("Could not load the library without a JavaVM handle !?\n");
587842e56b97ce677b83bdab09cda48bc2d89ac75aJust
597842e56b97ce677b83bdab09cda48bc2d89ac75aJust  env = getenv(VARNAME);
60035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  if (strcmp(env, "INIT"))
61035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod    Panic("JNI_OnLoad() was called, %s is %s (expected INIT)\n", VARNAME, env);
629be387c94ff8199f8031b7f11f06c52cce5ccf6djvr
63035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod  crazy_library_close(library);
647842e56b97ce677b83bdab09cda48bc2d89ac75aJust  env = getenv(VARNAME);
657842e56b97ce677b83bdab09cda48bc2d89ac75aJust  if (strcmp(env, "INIT"))
66035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod    Panic(
679be387c94ff8199f8031b7f11f06c52cce5ccf6djvr        "JNI_OnUnload() was called, %s is %s (expected INIT)\n", VARNAME, env);
68035bc7da2318f0dcacecba2e1e24c0a4bf3cf80cBehdad Esfahbod
697842e56b97ce677b83bdab09cda48bc2d89ac75aJust  crazy_context_destroy(context);
707842e56b97ce677b83bdab09cda48bc2d89ac75aJust
717842e56b97ce677b83bdab09cda48bc2d89ac75aJust  return 0;
727842e56b97ce677b83bdab09cda48bc2d89ac75aJust}
737842e56b97ce677b83bdab09cda48bc2d89ac75aJust