From 2abb686ddbf7c8fe54f2c1cb41ab90d7c8b04c7a Mon Sep 17 00:00:00 2001 From: Yanbing Jiang Date: Tue, 17 Jan 2017 13:23:43 +0800 Subject: Add test for getError() of NativeBridge. NativeBridge v3 introduced getError() of which the functionality is same as dlerror(). This patch forces the routine of ART to NativeBridge to execute getError() on ART host test by loading invalid library from Java. Test: make test-art-host-run-test-115-native-bridge Change-Id: Iba389c22b6587176cbfe57d42af49041560c5b00 Signed-off-by: Zhenhua WANG --- test/115-native-bridge/expected.txt | 5 +++++ test/115-native-bridge/nativebridge.cc | 10 +++++++--- test/115-native-bridge/run | 1 + test/115-native-bridge/src/NativeBridgeMain.java | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/test/115-native-bridge/expected.txt b/test/115-native-bridge/expected.txt index 852ec2e5e9..9c64111027 100644 --- a/test/115-native-bridge/expected.txt +++ b/test/115-native-bridge/expected.txt @@ -62,3 +62,8 @@ trampoline_Java_Main_testNewStringObject called! Getting trampoline for Java_Main_testSignal with shorty I. NB signal handler with signal 11. NB signal handler with signal 4. +Loading invalid library 'libinvalid.so' from Java, which will fail. +Checking for support. +Was to load 'libinvalid.so', force fail. +getError() in native bridge. +Catch UnsatisfiedLinkError exception as expected. diff --git a/test/115-native-bridge/nativebridge.cc b/test/115-native-bridge/nativebridge.cc index 87287f8acf..b3b89491bf 100644 --- a/test/115-native-bridge/nativebridge.cc +++ b/test/115-native-bridge/nativebridge.cc @@ -285,6 +285,10 @@ extern "C" bool native_bridge_initialize(const android::NativeBridgeRuntimeCallb } extern "C" void* native_bridge_loadLibrary(const char* libpath, int flag) { + if (strstr(libpath, "libinvalid.so") != nullptr) { + printf("Was to load 'libinvalid.so', force fail.\n"); + return nullptr; + } size_t len = strlen(libpath); char* tmp = new char[len + 10]; strncpy(tmp, libpath, len); @@ -300,7 +304,7 @@ extern "C" void* native_bridge_loadLibrary(const char* libpath, int flag) { printf("Handle = nullptr!\n"); printf("Was looking for %s.\n", libpath); printf("Error = %s.\n", dlerror()); - char cwd[1024]; + char cwd[1024] = {'\0'}; if (getcwd(cwd, sizeof(cwd)) != nullptr) { printf("Current working dir: %s\n", cwd); } @@ -437,8 +441,8 @@ extern "C" int native_bridge_unloadLibrary(void* handle ATTRIBUTE_UNUSED) { } extern "C" const char* native_bridge_getError() { - printf("dlerror() in native bridge.\n"); - return nullptr; + printf("getError() in native bridge.\n"); + return ""; } extern "C" bool native_bridge_isPathSupported(const char* library_path ATTRIBUTE_UNUSED) { diff --git a/test/115-native-bridge/run b/test/115-native-bridge/run index 9290dd3cf4..22f5c67ddc 100644 --- a/test/115-native-bridge/run +++ b/test/115-native-bridge/run @@ -23,6 +23,7 @@ LIBPATH=${LIBPATH##*:} ln -sf ${LIBPATH}/libnativebridgetest.so . touch libarttest.so touch libarttestd.so +touch libinvalid.so ln -sf ${LIBPATH}/libarttest.so libarttest2.so ln -sf ${LIBPATH}/libarttestd.so libarttestd2.so diff --git a/test/115-native-bridge/src/NativeBridgeMain.java b/test/115-native-bridge/src/NativeBridgeMain.java index c298b1b772..e8d1e4e326 100644 --- a/test/115-native-bridge/src/NativeBridgeMain.java +++ b/test/115-native-bridge/src/NativeBridgeMain.java @@ -16,6 +16,7 @@ import java.lang.reflect.Method; import java.lang.System; +import java.lang.Exception; // This is named Main as it is a copy of JniTest, so that we can re-use the native implementations // from libarttest. @@ -33,6 +34,7 @@ class Main { testEnvironment(); testNewStringObject(); testSignalHandler(); + testGetErrorByLoadInvalidLibrary(); } public static native void testFindClassOnAttachedNativeThread(); @@ -183,6 +185,20 @@ class Main { } private static native int testSignal(); + + // Test the path from Java to getError() of NativeBridge. + // + // Load invalid library 'libinvalid.so' from Java. Library loading will fail since it's + // invalid (empty file). ART, NativeLoader actually, calls getError() to dump error message. + // After that in Java, catch UnsatisfiedLinkError exception to confirm. + private static void testGetErrorByLoadInvalidLibrary() { + System.out.println("Loading invalid library 'libinvalid.so' from Java, which will fail."); + try { + System.loadLibrary("invalid"); + } catch (java.lang.UnsatisfiedLinkError e){ + System.out.println("Catch UnsatisfiedLinkError exception as expected."); + } + } } public class NativeBridgeMain { -- cgit v1.2.3-59-g8ed1b