1 // Copyright 2014 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.
5 // A crazy linker test to test crazy_context_set_java_vm().
10 #include <crazy_linker.h>
12 #include "test_util.h"
14 #define VARNAME "TEST_VAR"
16 static const char kJniLibName
[] = "libjni_lib.so";
17 static void* kJavaVM
= (void*)0xdeadcafe;
20 crazy_context_t
* context
= crazy_context_create();
21 crazy_library_t
* library
;
23 // Expect to find the library in the same directory than this executable.
24 crazy_context_add_search_path_for_address(context
, (void*)&main
);
26 crazy_context_set_java_vm(context
, kJavaVM
, JNI_VERSION_1_2
);
28 // Load libjni_lib.so, this should invoke its JNI_OnLoad() function
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
));
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
);
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
);
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
);
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.");
51 // Disable the feature, this shall load the library, but not call the
53 crazy_context_set_java_vm(context
, NULL
, 0);
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");
59 env
= getenv(VARNAME
);
60 if (strcmp(env
, "INIT"))
61 Panic("JNI_OnLoad() was called, %s is %s (expected INIT)\n", VARNAME
, env
);
63 crazy_library_close(library
);
64 env
= getenv(VARNAME
);
65 if (strcmp(env
, "INIT"))
67 "JNI_OnUnload() was called, %s is %s (expected INIT)\n", VARNAME
, env
);
69 crazy_context_destroy(context
);