From 76c8d191b8db964d8c3d44d42d2e04a2a383bd80 Mon Sep 17 00:00:00 2001 From: Martin Stjernholm Date: Tue, 16 Apr 2024 18:33:11 +0100 Subject: Repurpose libnativeloader_lazy tests as shallow tests for API coverage in CTS. Most APIs are exercised extensively in any managed process, but use this to cover the fringes. This drops the mocks that used dynamic library symbol overrides (android_create_namespace and android_link_namespaces) from the former libnativeloader_lazy tests, because we cannot inject them in CTS tests. However it turns out it works just fine to call into the real code in the linker. As a result libnativeloader_lazy_test no longer needs to be built with exported symbols necessary to make the symbol override mocks to work. Also strengthen the CreateClassLoaderNamespace test slightly by verifying that it really did create a classloader namespace. Test: atest libnativeloader_test \ art_libnativeloader_cts_test \ libnativeloader_lazy_test Bug: 335224367 Change-Id: Idd15d5a79f4f54c68304a4b758c7a46ba9b96c86 (cherry picked from commit 1dc111804f9ea778bce6258ffd10fed80fb02d0c) Merged-In: Idd15d5a79f4f54c68304a4b758c7a46ba9b96c86 --- libnativeloader/Android.bp | 55 ++++++++---- libnativeloader/native_loader_api_test.cpp | 127 ++++++++++++++++++++++++++++ libnativeloader/native_loader_lazy_test.cpp | 127 ---------------------------- 3 files changed, 164 insertions(+), 145 deletions(-) create mode 100644 libnativeloader/native_loader_api_test.cpp delete mode 100644 libnativeloader/native_loader_lazy_test.cpp diff --git a/libnativeloader/Android.bp b/libnativeloader/Android.bp index 2db1f76ead..114af7020b 100644 --- a/libnativeloader/Android.bp +++ b/libnativeloader/Android.bp @@ -120,8 +120,8 @@ cc_library { shared_libs: ["liblog"], } -cc_defaults { - name: "libnativeloader-test-defaults", +art_cc_test { + name: "libnativeloader_test", defaults: [ "art_module_source_build_defaults", // Cannot use art_standalone_gtest_defaults because it makes us link @@ -149,42 +149,61 @@ cc_defaults { ], shared_libs: [ "libbase", + "libnativeloader", + ], + + tidy_timeout_srcs: [ + "native_loader_test.cpp", + ], + srcs: [ + "native_loader_api_test.c", + "native_loader_test.cpp", ], test_for: [ "com.android.art", "com.android.art.debug", ], - test_suites: ["device-tests"], + test_suites: [ + "device-tests", + ], } -art_cc_test { - name: "libnativeloader_test", - defaults: [ - "libnativeloader-test-defaults", - ], - tidy_timeout_srcs: [ - "native_loader_test.cpp", +cc_defaults { + name: "libnativeloader_api_test_defaults", + defaults: ["art_standalone_test_defaults"], + + srcs: ["native_loader_api_test.cpp"], + header_libs: [ + "libnativebridge-headers", + "libnativehelper_header_only", ], - srcs: [ - "native_loader_api_test.c", - "native_loader_test.cpp", + static_libs: [ + "libbase", + "libgmock", ], +} + +art_cc_test { + name: "art_libnativeloader_cts_test", + defaults: ["libnativeloader_api_test_defaults"], shared_libs: [ "libnativeloader", ], + test_config_template: ":art-gtests-target-standalone-cts-template", test_suites: [ - "mts-art", + "cts", + "mcts-art", ], } art_cc_test { name: "libnativeloader_lazy_test", - defaults: ["libnativeloader-test-defaults"], - srcs: [ - "native_loader_lazy_test.cpp", - ], + defaults: ["libnativeloader_api_test_defaults"], static_libs: [ "libnativeloader_lazy", ], + test_suites: [ + "device-tests", + ], } diff --git a/libnativeloader/native_loader_api_test.cpp b/libnativeloader/native_loader_api_test.cpp new file mode 100644 index 0000000000..78fb29f91d --- /dev/null +++ b/libnativeloader/native_loader_api_test.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2021 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. + */ + +#if defined(ART_TARGET_ANDROID) + +#include "gtest/gtest.h" +#include "native_loader_test.h" +#include "nativehelper/scoped_utf_chars.h" +#include "nativeloader/native_loader.h" + +namespace android { +namespace nativeloader { + +using ::testing::Return; +using ::testing::StrEq; + +// Test the exported API in libnativeloader and libnativeloader_lazy. The +// testing we can do here outside a full VM is limited, but this is only to +// complement other tests and ensure coverage of the APIs that aren't in the +// common call paths. + +class NativeLoaderLazyTest : public ::testing::Test { + protected: + void SetUp() override { + mock = std::make_unique>(false); + env = std::make_unique(); + env->functions = CreateJNINativeInterface(); + } + + void TearDown() override { + // ResetNativeLoader isn't accessible through the lazy library, so we cannot + // reset libnativeloader internal state. Hence be sure to not reuse the same + // class loader/namespace names. + delete env->functions; + mock.reset(); + } + + void CallCreateClassLoaderNamespace(const char* class_loader) { + ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr)); + + jstring err = CreateClassLoaderNamespace(env.get(), + 17, + env.get()->NewStringUTF(class_loader), + false, + env.get()->NewStringUTF("/data/app/foo/classes.dex"), + env.get()->NewStringUTF("/data/app/foo"), + /*permitted_path=*/nullptr, + /*uses_library_list=*/nullptr); + EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env.get(), err).c_str()); + } + + std::unique_ptr env; +}; + +TEST_F(NativeLoaderLazyTest, CreateClassLoaderNamespace) { + CallCreateClassLoaderNamespace("my_classloader_1"); + EXPECT_NE(FindNamespaceByClassLoader(env.get(), env.get()->NewStringUTF("my_classloader_1")), + nullptr); +} + +TEST_F(NativeLoaderLazyTest, OpenNativeLibrary) { + bool needs_native_bridge; + char* errmsg = nullptr; + EXPECT_EQ(nullptr, + OpenNativeLibrary(env.get(), + 17, + "libnotfound.so", + env.get()->NewStringUTF("my_classloader"), + /*caller_location=*/nullptr, + /*library_path=*/nullptr, + &needs_native_bridge, + &errmsg)); + EXPECT_NE(nullptr, errmsg); + NativeLoaderFreeErrorMessage(errmsg); +} + +TEST_F(NativeLoaderLazyTest, CloseNativeLibrary) { + char* errmsg = nullptr; + EXPECT_FALSE(CloseNativeLibrary(nullptr, false, &errmsg)); + EXPECT_NE(nullptr, errmsg); + NativeLoaderFreeErrorMessage(errmsg); +} + +TEST_F(NativeLoaderLazyTest, OpenNativeLibraryInNamespace) { + CallCreateClassLoaderNamespace("my_classloader_2"); + struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader( + env.get(), env.get()->NewStringUTF("my_classloader_2")); + ASSERT_NE(nullptr, ns); + + bool needs_native_bridge; + char* errmsg = nullptr; + EXPECT_FALSE(OpenNativeLibraryInNamespace(ns, "libnotfound.so", &needs_native_bridge, &errmsg)); + EXPECT_NE(nullptr, errmsg); + NativeLoaderFreeErrorMessage(errmsg); +} + +TEST_F(NativeLoaderLazyTest, FindNamespaceByClassLoader) { + EXPECT_EQ(nullptr, FindNamespaceByClassLoader(env.get(), env.get()->NewStringUTF("namespace"))); +} + +TEST_F(NativeLoaderLazyTest, FindNativeLoaderNamespaceByClassLoader) { + EXPECT_EQ( + nullptr, + FindNativeLoaderNamespaceByClassLoader(env.get(), env.get()->NewStringUTF("namespace"))); +} + +TEST_F(NativeLoaderLazyTest, NativeLoaderFreeErrorMessage) { + NativeLoaderFreeErrorMessage(nullptr); +} + +} // namespace nativeloader +} // namespace android + +#endif // defined(ART_TARGET_ANDROID) diff --git a/libnativeloader/native_loader_lazy_test.cpp b/libnativeloader/native_loader_lazy_test.cpp deleted file mode 100644 index b863c85100..0000000000 --- a/libnativeloader/native_loader_lazy_test.cpp +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (C) 2021 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. - */ - -#if defined(ART_TARGET_ANDROID) - -#include - -#include "native_loader_test.h" -#include "nativehelper/scoped_utf_chars.h" -#include "nativeloader/native_loader.h" - -namespace android { -namespace nativeloader { - -using ::testing::StrEq; - -// Only need to test that the trivial lazy lib wrappers call through to the real -// functions, but still have to mock things well enough to avoid null pointer -// dereferences. - -class NativeLoaderLazyTest : public ::testing::Test { - protected: - void SetUp() override { - mock = std::make_unique>(false); - env = std::make_unique(); - env->functions = CreateJNINativeInterface(); - } - - void TearDown() override { - // ResetNativeLoader isn't accessible through the lazy library, so we cannot - // reset libnativeloader internal state. Hence be sure to not reuse the same - // class loader/namespace names. - delete env->functions; - mock.reset(); - } - - void CallCreateClassLoaderNamespace(const char* class_loader) { - ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr)); - EXPECT_CALL(*mock, mock_create_namespace) - .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(class_loader)))); - ON_CALL(*mock, mock_link_namespaces).WillByDefault(Return(true)); - - jstring err = CreateClassLoaderNamespace(env.get(), - 17, - env.get()->NewStringUTF(class_loader), - false, - env.get()->NewStringUTF("/data/app/foo/classes.dex"), - env.get()->NewStringUTF("/data/app/foo"), - /*permitted_path=*/nullptr, - /*uses_library_list=*/nullptr); - EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env.get(), err).c_str()); - } - - std::unique_ptr env; -}; - -TEST_F(NativeLoaderLazyTest, CreateClassLoaderNamespace) { - CallCreateClassLoaderNamespace("my_classloader_1"); -} - -TEST_F(NativeLoaderLazyTest, OpenNativeLibrary) { - bool needs_native_bridge; - char* errmsg = nullptr; - EXPECT_EQ(nullptr, - OpenNativeLibrary(env.get(), - 17, - "libnotfound.so", - env.get()->NewStringUTF("my_classloader"), - /*caller_location=*/nullptr, - /*library_path=*/nullptr, - &needs_native_bridge, - &errmsg)); - EXPECT_NE(nullptr, errmsg); - NativeLoaderFreeErrorMessage(errmsg); -} - -TEST_F(NativeLoaderLazyTest, CloseNativeLibrary) { - char* errmsg = nullptr; - EXPECT_FALSE(CloseNativeLibrary(nullptr, false, &errmsg)); - EXPECT_NE(nullptr, errmsg); - NativeLoaderFreeErrorMessage(errmsg); -} - -TEST_F(NativeLoaderLazyTest, OpenNativeLibraryInNamespace) { - CallCreateClassLoaderNamespace("my_classloader_2"); - struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader( - env.get(), env.get()->NewStringUTF("my_classloader_2")); - ASSERT_NE(nullptr, ns); - - bool needs_native_bridge; - char* errmsg = nullptr; - EXPECT_FALSE(OpenNativeLibraryInNamespace(ns, "libnotfound.so", &needs_native_bridge, &errmsg)); - EXPECT_NE(nullptr, errmsg); - NativeLoaderFreeErrorMessage(errmsg); -} - -TEST_F(NativeLoaderLazyTest, FindNamespaceByClassLoader) { - EXPECT_EQ(nullptr, FindNamespaceByClassLoader(env.get(), env.get()->NewStringUTF("namespace"))); -} - -TEST_F(NativeLoaderLazyTest, FindNativeLoaderNamespaceByClassLoader) { - EXPECT_EQ( - nullptr, - FindNativeLoaderNamespaceByClassLoader(env.get(), env.get()->NewStringUTF("namespace"))); -} - -TEST_F(NativeLoaderLazyTest, NativeLoaderFreeErrorMessage) { - NativeLoaderFreeErrorMessage(nullptr); -} - -} // namespace nativeloader -} // namespace android - -#endif // defined(ART_TARGET_ANDROID) -- cgit v1.2.3