Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Unified Diff: third_party/android_crazy_linker/src/tests/test_jni_hooks.cpp

Issue 322433006: Fork of the Android NDK crazy linker. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a required license header to a cpp module, missing in the original. Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/android_crazy_linker/src/tests/test_jni_hooks.cpp
diff --git a/third_party/android_crazy_linker/src/tests/test_jni_hooks.cpp b/third_party/android_crazy_linker/src/tests/test_jni_hooks.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..be3f245fa9f65e8496b44685c135e1acd5f6241a
--- /dev/null
+++ b/third_party/android_crazy_linker/src/tests/test_jni_hooks.cpp
@@ -0,0 +1,72 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// A crazy linker test to test crazy_context_set_java_vm().
+
+#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[] = "libjni_lib.so";
+static void* kJavaVM = (void*)0xdeadcafe;
+
+int main() {
+ crazy_context_t* context = crazy_context_create();
+ crazy_library_t* library;
+
+ // Expect to find the library in the same directory than this executable.
+ crazy_context_add_search_path_for_address(context, (void*)&main);
+
+ crazy_context_set_java_vm(context, kJavaVM, JNI_VERSION_1_2);
+
+ // Load libjni_lib.so, this should invoke its JNI_OnLoad() function
+ // automatically.
+ 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);
+
+ // Now, change the minimum JNI version to JNI_VERSION_1_6, which should
+ // prevent loading the library properly, since it only supports 1.2.
+ crazy_context_set_java_vm(context, 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.");
+
+ // Disable the feature, this shall load the library, but not call the
+ // JNI_OnLoad() hook.
+ crazy_context_set_java_vm(context, NULL, 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;
+}

Powered by Google App Engine
This is Rietveld 408576698