aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJiakai Zhang <jiakaiz@google.com>2024-03-13 10:51:03 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2024-03-13 10:51:03 +0000
commit8a1436725a9f662dcb4dd674bda27af5c4bfc03b (patch)
tree15b0900857bbc871a506782d0c1e1f1fa9125ca1
parentf75aeea03586279434cae7cca514aa01f2bf376f (diff)
parent4c6039710998ceaec303f360a0f15930ef320111 (diff)
downloadlibnativehelper-8a1436725a9f662dcb4dd674bda27af5c4bfc03b.tar.gz
Merge "Move tests in libnativehelper_test.cpp out of tests_mts." into main
-rw-r--r--TEST_MAPPING5
-rw-r--r--tests/Android.bp55
-rw-r--r--tests/AndroidManifest.xml28
-rw-r--r--tests/libnativehelper_test.cpp191
-rw-r--r--tests/src/com/android/art/libnativehelper/LibnativehelperInternalTests.java26
-rw-r--r--tests_mts/jni/libnativehelper_test.cpp158
6 files changed, 300 insertions, 163 deletions
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 251ee58..87c2922 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -15,5 +15,10 @@
{
"name": "CtsLibnativehelperTestCases"
}
+ ],
+ "postsubmit": [
+ {
+ "name": "LibnativehelperInternalTestCases"
+ }
]
}
diff --git a/tests/Android.bp b/tests/Android.bp
index ca9442d..fe46344 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -10,7 +10,7 @@ package {
}
cc_defaults {
- name: "libnativehelper_test_defaults",
+ name: "libnativehelper_common_test_defaults",
cflags: [
// Base set of cflags used by all things ART.
"-fno-rtti",
@@ -52,16 +52,21 @@ cc_defaults {
// Enable thread annotations for std::mutex, etc.
"-D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS",
],
+ tidy: true,
+}
+
+cc_defaults {
+ name: "libnativehelper_unit_test_defaults",
+ defaults: ["libnativehelper_common_test_defaults"],
host_supported: true,
test_options: {
unit_test: true,
},
- tidy: true,
}
cc_test {
name: "libnativehelper_tests",
- defaults: ["libnativehelper_test_defaults"],
+ defaults: ["libnativehelper_unit_test_defaults"],
test_suites: ["general-tests"],
srcs: [
"scoped_local_frame_test.cpp",
@@ -75,7 +80,7 @@ cc_test {
cc_test {
name: "libnativehelper_lazy_tests",
- defaults: ["libnativehelper_test_defaults"],
+ defaults: ["libnativehelper_unit_test_defaults"],
test_suites: ["general-tests"],
srcs: ["libnativehelper_lazy_test.cpp"],
shared_libs: ["liblog"],
@@ -93,7 +98,7 @@ cc_test {
name: "libnativehelper_internal_tests",
defaults: [
"art_module_source_build_defaults",
- "libnativehelper_test_defaults",
+ "libnativehelper_unit_test_defaults",
],
srcs: [
"ExpandableString_test.cpp",
@@ -102,3 +107,43 @@ cc_test {
bootstrap: true,
shared_libs: ["libnativehelper"],
}
+
+cc_library {
+ name: "libnativehelper_internal_tests_jni",
+ defaults: [
+ "libnativehelper_common_test_defaults",
+ "art_module_source_build_defaults",
+ ],
+ host_supported: false,
+ srcs: [
+ "libnativehelper_test.cpp",
+ ],
+ shared_libs: [
+ "libnativehelper",
+ "liblog",
+ ],
+ static_libs: [
+ "libbase",
+ "libgmock",
+ "libnativetesthelper_jni",
+ ],
+}
+
+android_test {
+ name: "LibnativehelperInternalTestCases",
+ defaults: [
+ "art_module_source_build_java_defaults",
+ ],
+ srcs: [
+ "src/**/*.java",
+ ],
+ jni_libs: [
+ "libnativehelper_internal_tests_jni",
+ ],
+ static_libs: [
+ "ctstestrunner-axt",
+ "nativetesthelper",
+ ],
+ manifest: "AndroidManifest.xml",
+ test_suites: ["general-tests"],
+}
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
new file mode 100644
index 0000000..c0dabb4
--- /dev/null
+++ b/tests/AndroidManifest.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2024 The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+ package="com.android.art.libnativehelper"
+ android:targetSandboxVersion="2">
+ <application android:usesCleartextTraffic="true">
+ <uses-library android:name="android.test.runner" />
+ </application>
+
+ <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+ android:targetPackage="com.android.art.libnativehelper"
+ android:label="Internal tests for libnativehelper">
+ </instrumentation>
+</manifest>
diff --git a/tests/libnativehelper_test.cpp b/tests/libnativehelper_test.cpp
new file mode 100644
index 0000000..e8cd77f
--- /dev/null
+++ b/tests/libnativehelper_test.cpp
@@ -0,0 +1,191 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <jni.h>
+
+#include <memory>
+
+#include "android-base/logging.h"
+#include "gtest/gtest.h"
+#include "nativehelper/scoped_local_ref.h"
+#include "nativehelper/scoped_utf_chars.h"
+#include "nativehelper/utils.h"
+#include "nativetesthelper_jni/utils.h"
+
+class LibnativehelperTest : public ::testing::Test {
+ protected:
+ virtual void SetUp() override {
+ int result = GetJavaVM()->GetEnv(reinterpret_cast<void**>(&mEnv), JNI_VERSION_1_6);
+ CHECK_EQ(JNI_OK, result);
+ CHECK_NE(nullptr, mEnv);
+ }
+
+ virtual void TearDown() override { mEnv = nullptr; }
+
+ JNIEnv* mEnv;
+};
+
+TEST_F(LibnativehelperTest, GetUtfOrReturn) {
+ ScopedLocalRef<jstring> j_str(mEnv, mEnv->NewStringUTF("foo"));
+ std::unique_ptr<ScopedUtfChars> result;
+
+ jint ret = [&](JNIEnv* env) -> jint {
+ ScopedUtfChars str = GET_UTF_OR_RETURN(env, j_str.get());
+ result.reset(new ScopedUtfChars(std::move(str)));
+ return 1;
+ }(mEnv);
+
+ EXPECT_EQ(result->c_str(), std::string_view("foo"));
+ EXPECT_FALSE(mEnv->ExceptionCheck());
+ EXPECT_EQ(ret, 1);
+}
+
+TEST_F(LibnativehelperTest, GetUtfOrReturnVoid) {
+ ScopedLocalRef<jstring> j_str(mEnv, mEnv->NewStringUTF("foo"));
+ std::unique_ptr<ScopedUtfChars> result;
+
+ [&](JNIEnv* env) -> void {
+ ScopedUtfChars str = GET_UTF_OR_RETURN_VOID(env, j_str.get());
+ result.reset(new ScopedUtfChars(std::move(str)));
+ }(mEnv);
+
+ EXPECT_EQ(result->c_str(), std::string_view("foo"));
+ EXPECT_FALSE(mEnv->ExceptionCheck());
+}
+
+TEST_F(LibnativehelperTest, GetUtfOrReturnFailed) {
+ jint ret = [&](JNIEnv* env) -> jint {
+ ScopedUtfChars str = GET_UTF_OR_RETURN(env, nullptr);
+ return 1;
+ }(mEnv);
+
+ EXPECT_TRUE(mEnv->ExceptionCheck());
+ EXPECT_EQ(ret, 0);
+
+ mEnv->ExceptionClear();
+}
+
+TEST_F(LibnativehelperTest, GetUtfOrReturnVoidFailed) {
+ bool execution_completed = false;
+
+ [&](JNIEnv* env) -> void {
+ ScopedUtfChars str = GET_UTF_OR_RETURN_VOID(env, nullptr);
+ execution_completed = true;
+ }(mEnv);
+
+ EXPECT_TRUE(mEnv->ExceptionCheck());
+ EXPECT_FALSE(execution_completed);
+
+ mEnv->ExceptionClear();
+}
+
+TEST_F(LibnativehelperTest, CreateUtfOrReturn) {
+ std::unique_ptr<ScopedLocalRef<jstring>> result;
+
+ jint ret = [&](JNIEnv* env) -> jint {
+ ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, "foo");
+ result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
+ return 1;
+ }(mEnv);
+
+ ScopedUtfChars str(mEnv, result->get());
+ EXPECT_EQ(str.c_str(), std::string_view("foo"));
+ EXPECT_FALSE(mEnv->ExceptionCheck());
+ EXPECT_EQ(ret, 1);
+}
+
+class MyString : public std::string {
+ public:
+ explicit MyString(const char* c_str) : std::string(c_str) {}
+
+ // Force clear the string to catch use-after-free issues.
+ ~MyString() { clear(); }
+};
+
+// `expr` creates a temporary object and evaluates to it.
+TEST_F(LibnativehelperTest, CreateUtfOrReturnExprEvaluatesToTemporary) {
+ std::unique_ptr<ScopedLocalRef<jstring>> result;
+
+ jint ret = [&](JNIEnv* env) -> jint {
+ ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, MyString("foo"));
+ result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
+ return 1;
+ }(mEnv);
+
+ ScopedUtfChars str(mEnv, result->get());
+ EXPECT_EQ(str.c_str(), std::string_view("foo"));
+ EXPECT_FALSE(mEnv->ExceptionCheck());
+ EXPECT_EQ(ret, 1);
+}
+
+// `expr` creates a temporary object and evaluates to something else backed by it.
+TEST_F(LibnativehelperTest, CreateUtfOrReturnExprEvaluatesToValueBackedByTemporary) {
+ std::unique_ptr<ScopedLocalRef<jstring>> result;
+
+ jint ret = [&](JNIEnv* env) -> jint {
+ ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, MyString("foo").c_str());
+ result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
+ return 1;
+ }(mEnv);
+
+ ScopedUtfChars str(mEnv, result->get());
+ EXPECT_EQ(str.c_str(), std::string_view("foo"));
+ EXPECT_FALSE(mEnv->ExceptionCheck());
+ EXPECT_EQ(ret, 1);
+}
+
+TEST_F(LibnativehelperTest, CreateUtfOrReturnVoid) {
+ std::unique_ptr<ScopedLocalRef<jstring>> result;
+
+ [&](JNIEnv* env) -> void {
+ ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN_VOID(env, "foo");
+ result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
+ }(mEnv);
+
+ ScopedUtfChars str(mEnv, result->get());
+ EXPECT_EQ(str.c_str(), std::string_view("foo"));
+ EXPECT_FALSE(mEnv->ExceptionCheck());
+}
+
+TEST_F(LibnativehelperTest, CreateUtfOrReturnFailed) {
+ JNINativeInterface interface;
+ interface.NewStringUTF = [](JNIEnv*, const char*) -> jstring { return nullptr; };
+ JNIEnv fake_env;
+ fake_env.functions = &interface;
+
+ jint ret = [&](JNIEnv* env) -> jint {
+ ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, "foo");
+ return 1;
+ }(&fake_env);
+
+ EXPECT_EQ(ret, 0);
+}
+
+TEST_F(LibnativehelperTest, CreateUtfOrReturnVoidFailed) {
+ JNINativeInterface interface;
+ interface.NewStringUTF = [](JNIEnv*, const char*) -> jstring { return nullptr; };
+ JNIEnv fake_env;
+ fake_env.functions = &interface;
+
+ bool execution_completed = false;
+
+ [&](JNIEnv* env) -> void {
+ ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN_VOID(env, "foo");
+ execution_completed = true;
+ }(&fake_env);
+
+ EXPECT_FALSE(execution_completed);
+}
diff --git a/tests/src/com/android/art/libnativehelper/LibnativehelperInternalTests.java b/tests/src/com/android/art/libnativehelper/LibnativehelperInternalTests.java
new file mode 100644
index 0000000..207b2c5
--- /dev/null
+++ b/tests/src/com/android/art/libnativehelper/LibnativehelperInternalTests.java
@@ -0,0 +1,26 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.art.libnativehelper;
+
+import com.android.gtestrunner.GtestRunner;
+import com.android.gtestrunner.TargetLibrary;
+
+import org.junit.runner.RunWith;
+
+@RunWith(GtestRunner.class)
+@TargetLibrary("nativehelper_internal_tests_jni")
+public class LibnativehelperInternalTests {}
diff --git a/tests_mts/jni/libnativehelper_test.cpp b/tests_mts/jni/libnativehelper_test.cpp
index bd448f9..c3c06fe 100644
--- a/tests_mts/jni/libnativehelper_test.cpp
+++ b/tests_mts/jni/libnativehelper_test.cpp
@@ -16,12 +16,6 @@
#include "libnativehelper_test.h"
-#include <memory>
-
-#include "jni.h"
-#include "nativehelper/scoped_local_ref.h"
-#include "nativehelper/scoped_utf_chars.h"
-#include "nativehelper/utils.h"
#include "nativetesthelper_jni/utils.h"
void LibnativehelperTest::SetUp() {
@@ -33,155 +27,3 @@ void LibnativehelperTest::SetUp() {
void LibnativehelperTest::TearDown() {
mEnv = nullptr;
}
-
-TEST_F(LibnativehelperTest, GetUtfOrReturn) {
- ScopedLocalRef<jstring> j_str(mEnv, mEnv->NewStringUTF("foo"));
- std::unique_ptr<ScopedUtfChars> result;
-
- jint ret = [&](JNIEnv* env) -> jint {
- ScopedUtfChars str = GET_UTF_OR_RETURN(env, j_str.get());
- result.reset(new ScopedUtfChars(std::move(str)));
- return 1;
- }(mEnv);
-
- EXPECT_EQ(result->c_str(), std::string_view("foo"));
- EXPECT_FALSE(mEnv->ExceptionCheck());
- EXPECT_EQ(ret, 1);
-}
-
-TEST_F(LibnativehelperTest, GetUtfOrReturnVoid) {
- ScopedLocalRef<jstring> j_str(mEnv, mEnv->NewStringUTF("foo"));
- std::unique_ptr<ScopedUtfChars> result;
-
- [&](JNIEnv* env) -> void {
- ScopedUtfChars str = GET_UTF_OR_RETURN_VOID(env, j_str.get());
- result.reset(new ScopedUtfChars(std::move(str)));
- }(mEnv);
-
- EXPECT_EQ(result->c_str(), std::string_view("foo"));
- EXPECT_FALSE(mEnv->ExceptionCheck());
-}
-
-TEST_F(LibnativehelperTest, GetUtfOrReturnFailed) {
- jint ret = [&](JNIEnv* env) -> jint {
- ScopedUtfChars str = GET_UTF_OR_RETURN(env, nullptr);
- return 1;
- }(mEnv);
-
- EXPECT_TRUE(mEnv->ExceptionCheck());
- EXPECT_EQ(ret, 0);
-
- mEnv->ExceptionClear();
-}
-
-TEST_F(LibnativehelperTest, GetUtfOrReturnVoidFailed) {
- bool execution_completed = false;
-
- [&](JNIEnv* env) -> void {
- ScopedUtfChars str = GET_UTF_OR_RETURN_VOID(env, nullptr);
- execution_completed = true;
- }(mEnv);
-
- EXPECT_TRUE(mEnv->ExceptionCheck());
- EXPECT_FALSE(execution_completed);
-
- mEnv->ExceptionClear();
-}
-
-TEST_F(LibnativehelperTest, CreateUtfOrReturn) {
- std::unique_ptr<ScopedLocalRef<jstring>> result;
-
- jint ret = [&](JNIEnv* env) -> jint {
- ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, "foo");
- result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
- return 1;
- }(mEnv);
-
- ScopedUtfChars str(mEnv, result->get());
- EXPECT_EQ(str.c_str(), std::string_view("foo"));
- EXPECT_FALSE(mEnv->ExceptionCheck());
- EXPECT_EQ(ret, 1);
-}
-
-class MyString : public std::string {
- public:
- explicit MyString(const char* c_str) : std::string(c_str) {}
-
- // Force clear the string to catch use-after-free issues.
- ~MyString() { clear(); }
-};
-
-// `expr` creates a temporary object and evaluates to it.
-TEST_F(LibnativehelperTest, CreateUtfOrReturnExprEvaluatesToTemporary) {
- std::unique_ptr<ScopedLocalRef<jstring>> result;
-
- jint ret = [&](JNIEnv* env) -> jint {
- ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, MyString("foo"));
- result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
- return 1;
- }(mEnv);
-
- ScopedUtfChars str(mEnv, result->get());
- EXPECT_EQ(str.c_str(), std::string_view("foo"));
- EXPECT_FALSE(mEnv->ExceptionCheck());
- EXPECT_EQ(ret, 1);
-}
-
-// `expr` creates a temporary object and evaluates to something else backed by it.
-TEST_F(LibnativehelperTest, CreateUtfOrReturnExprEvaluatesToValueBackedByTemporary) {
- std::unique_ptr<ScopedLocalRef<jstring>> result;
-
- jint ret = [&](JNIEnv* env) -> jint {
- ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, MyString("foo").c_str());
- result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
- return 1;
- }(mEnv);
-
- ScopedUtfChars str(mEnv, result->get());
- EXPECT_EQ(str.c_str(), std::string_view("foo"));
- EXPECT_FALSE(mEnv->ExceptionCheck());
- EXPECT_EQ(ret, 1);
-}
-
-TEST_F(LibnativehelperTest, CreateUtfOrReturnVoid) {
- std::unique_ptr<ScopedLocalRef<jstring>> result;
-
- [&](JNIEnv* env) -> void {
- ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN_VOID(env, "foo");
- result.reset(new ScopedLocalRef<jstring>(std::move(j_str)));
- }(mEnv);
-
- ScopedUtfChars str(mEnv, result->get());
- EXPECT_EQ(str.c_str(), std::string_view("foo"));
- EXPECT_FALSE(mEnv->ExceptionCheck());
-}
-
-TEST_F(LibnativehelperTest, CreateUtfOrReturnFailed) {
- JNINativeInterface interface;
- interface.NewStringUTF = [](JNIEnv*, const char*) -> jstring { return nullptr; };
- JNIEnv fake_env;
- fake_env.functions = &interface;
-
- jint ret = [&](JNIEnv* env) -> jint {
- ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN(env, "foo");
- return 1;
- }(&fake_env);
-
- EXPECT_EQ(ret, 0);
-}
-
-TEST_F(LibnativehelperTest, CreateUtfOrReturnVoidFailed) {
- JNINativeInterface interface;
- interface.NewStringUTF = [](JNIEnv*, const char*) -> jstring { return nullptr; };
- JNIEnv fake_env;
- fake_env.functions = &interface;
-
- bool execution_completed = false;
-
- [&](JNIEnv* env) -> void {
- ScopedLocalRef<jstring> j_str = CREATE_UTF_OR_RETURN_VOID(env, "foo");
- execution_completed = true;
- }(&fake_env);
-
- EXPECT_FALSE(execution_completed);
-}