1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #if defined(ART_TARGET_ANDROID)
18 
19 #include <gtest/gtest.h>
20 
21 #include "native_loader_test.h"
22 #include "nativehelper/scoped_utf_chars.h"
23 #include "nativeloader/native_loader.h"
24 
25 namespace android {
26 namespace nativeloader {
27 
28 using ::testing::StrEq;
29 
30 // Only need to test that the trivial lazy lib wrappers call through to the real
31 // functions, but still have to mock things well enough to avoid null pointer
32 // dereferences.
33 
34 class NativeLoaderLazyTest : public ::testing::Test {
35  protected:
SetUp()36   void SetUp() override {
37     mock = std::make_unique<testing::NiceMock<MockPlatform>>(false);
38     env = std::make_unique<JNIEnv>();
39     env->functions = CreateJNINativeInterface();
40   }
41 
TearDown()42   void TearDown() override {
43     // ResetNativeLoader isn't accessible through the lazy library, so we cannot
44     // reset libnativeloader internal state. Hence be sure to not reuse the same
45     // class loader/namespace names.
46     delete env->functions;
47     mock.reset();
48   }
49 
CallCreateClassLoaderNamespace(const char * class_loader)50   void CallCreateClassLoaderNamespace(const char* class_loader) {
51     ON_CALL(*mock, JniObject_getParent(StrEq(class_loader))).WillByDefault(Return(nullptr));
52     EXPECT_CALL(*mock, mock_create_namespace)
53         .WillOnce(Return(TO_MOCK_NAMESPACE(TO_ANDROID_NAMESPACE(class_loader))));
54     ON_CALL(*mock, mock_link_namespaces).WillByDefault(Return(true));
55 
56     jstring err = CreateClassLoaderNamespace(env.get(),
57                                              17,
58                                              env.get()->NewStringUTF(class_loader),
59                                              false,
60                                              env.get()->NewStringUTF("/data/app/foo/classes.dex"),
61                                              env.get()->NewStringUTF("/data/app/foo"),
62                                              /*permitted_path=*/nullptr,
63                                              /*uses_library_list=*/nullptr);
64     EXPECT_EQ(err, nullptr) << "Error is: " << std::string(ScopedUtfChars(env.get(), err).c_str());
65   }
66 
67   std::unique_ptr<JNIEnv> env;
68 };
69 
TEST_F(NativeLoaderLazyTest,CreateClassLoaderNamespace)70 TEST_F(NativeLoaderLazyTest, CreateClassLoaderNamespace) {
71   CallCreateClassLoaderNamespace("my_classloader_1");
72 }
73 
TEST_F(NativeLoaderLazyTest,OpenNativeLibrary)74 TEST_F(NativeLoaderLazyTest, OpenNativeLibrary) {
75   bool needs_native_bridge;
76   char* errmsg = nullptr;
77   EXPECT_EQ(nullptr,
78             OpenNativeLibrary(env.get(),
79                               17,
80                               "libnotfound.so",
81                               env.get()->NewStringUTF("my_classloader"),
82                               /*caller_location=*/nullptr,
83                               /*library_path=*/nullptr,
84                               &needs_native_bridge,
85                               &errmsg));
86   EXPECT_NE(nullptr, errmsg);
87   NativeLoaderFreeErrorMessage(errmsg);
88 }
89 
TEST_F(NativeLoaderLazyTest,CloseNativeLibrary)90 TEST_F(NativeLoaderLazyTest, CloseNativeLibrary) {
91   char* errmsg = nullptr;
92   EXPECT_FALSE(CloseNativeLibrary(nullptr, false, &errmsg));
93   EXPECT_NE(nullptr, errmsg);
94   NativeLoaderFreeErrorMessage(errmsg);
95 }
96 
TEST_F(NativeLoaderLazyTest,OpenNativeLibraryInNamespace)97 TEST_F(NativeLoaderLazyTest, OpenNativeLibraryInNamespace) {
98   CallCreateClassLoaderNamespace("my_classloader_2");
99   struct NativeLoaderNamespace* ns = FindNativeLoaderNamespaceByClassLoader(
100       env.get(), env.get()->NewStringUTF("my_classloader_2"));
101   ASSERT_NE(nullptr, ns);
102 
103   bool needs_native_bridge;
104   char* errmsg = nullptr;
105   EXPECT_FALSE(OpenNativeLibraryInNamespace(ns, "libnotfound.so", &needs_native_bridge, &errmsg));
106   EXPECT_NE(nullptr, errmsg);
107   NativeLoaderFreeErrorMessage(errmsg);
108 }
109 
TEST_F(NativeLoaderLazyTest,FindNamespaceByClassLoader)110 TEST_F(NativeLoaderLazyTest, FindNamespaceByClassLoader) {
111   EXPECT_EQ(nullptr, FindNamespaceByClassLoader(env.get(), env.get()->NewStringUTF("namespace")));
112 }
113 
TEST_F(NativeLoaderLazyTest,FindNativeLoaderNamespaceByClassLoader)114 TEST_F(NativeLoaderLazyTest, FindNativeLoaderNamespaceByClassLoader) {
115   EXPECT_EQ(
116       nullptr,
117       FindNativeLoaderNamespaceByClassLoader(env.get(), env.get()->NewStringUTF("namespace")));
118 }
119 
TEST_F(NativeLoaderLazyTest,NativeLoaderFreeErrorMessage)120 TEST_F(NativeLoaderLazyTest, NativeLoaderFreeErrorMessage) {
121   NativeLoaderFreeErrorMessage(nullptr);
122 }
123 
124 }  // namespace nativeloader
125 }  // namespace android
126 
127 #endif  // defined(ART_TARGET_ANDROID)
128