1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // A crazy linker test to test crazy_context_set_java_vm(). 6 7 #include <jni.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <crazy_linker.h> 11 12 #include "test_util.h" 13 14 #define VARNAME "TEST_VAR" 15 16 static const char kJniLibName[] = "libjni_lib.so"; 17 static void* kJavaVM = (void*)0xdeadcafe; 18 19 int main() { 20 crazy_context_t* context = crazy_context_create(); 21 crazy_library_t* library; 22 23 // Expect to find the library in the same directory than this executable. 24 crazy_context_add_search_path_for_address(context, (void*)&main); 25 26 crazy_context_set_java_vm(context, kJavaVM, JNI_VERSION_1_2); 27 28 // Load libjni_lib.so, this should invoke its JNI_OnLoad() function 29 // automatically. 30 setenv(VARNAME, "INIT", 1); 31 if (!crazy_library_open(&library, kJniLibName, context)) 32 Panic("Could not open library: %s\n", crazy_context_get_error(context)); 33 34 const char* env = getenv(VARNAME); 35 if (strcmp(env, "LOADED")) 36 Panic("JNI_OnLoad() hook was not called! %s is %s\n", VARNAME, env); 37 38 crazy_library_close(library); 39 env = getenv(VARNAME); 40 if (strcmp(env, "UNLOADED")) 41 Panic("JNI_OnUnload() hook was not called! %s is %s\n", VARNAME, env); 42 43 // Now, change the minimum JNI version to JNI_VERSION_1_6, which should 44 // prevent loading the library properly, since it only supports 1.2. 45 crazy_context_set_java_vm(context, kJavaVM, JNI_VERSION_1_6); 46 47 setenv(VARNAME, "INIT", 1); 48 if (crazy_library_open(&library, kJniLibName, context)) 49 Panic("Could load the library with JNI_VERSION_1_6 > JNI_VERSION_1_2."); 50 51 // Disable the feature, this shall load the library, but not call the 52 // JNI_OnLoad() hook. 53 crazy_context_set_java_vm(context, NULL, 0); 54 55 setenv(VARNAME, "INIT", 1); 56 if (!crazy_library_open(&library, kJniLibName, context)) 57 Panic("Could not load the library without a JavaVM handle !?\n"); 58 59 env = getenv(VARNAME); 60 if (strcmp(env, "INIT")) 61 Panic("JNI_OnLoad() was called, %s is %s (expected INIT)\n", VARNAME, env); 62 63 crazy_library_close(library); 64 env = getenv(VARNAME); 65 if (strcmp(env, "INIT")) 66 Panic( 67 "JNI_OnUnload() was called, %s is %s (expected INIT)\n", VARNAME, env); 68 69 crazy_context_destroy(context); 70 71 return 0; 72 } 73