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 #include "../libnativehelper_lazy.h"
18
19 #include <gtest/gtest.h>
20 #include "jni.h"
21
22 #include "nativehelper/JniInvocation.h"
23 #include "nativehelper/JNIHelp.h"
24 #include "nativehelper/JNIPlatformHelp.h"
25
26 // The tests here are just for the case when libnativehelper.so cannot be loaded by
27 // libnativehelper_lazy.
28 class LibnativehelperLazyTest : public ::testing::Test {
29 protected:
SetUp()30 virtual void SetUp() {
31 ::testing::Test::SetUp();
32 PreventLibnativehelperLazyLoadingForTests();
33 }
34 };
35
36 static const char* kLoadFailed = "Failed to load libnativehelper.so";
37
TEST_F(LibnativehelperLazyTest,NoLibnativehelperIsForJNIPlatformHelp)38 TEST_F(LibnativehelperLazyTest, NoLibnativehelperIsForJNIPlatformHelp) {
39 C_JNIEnv* env = NULL;
40 EXPECT_DEATH(jniGetNioBufferBaseArray(env, NULL), kLoadFailed);
41 EXPECT_DEATH(jniGetNioBufferBaseArrayOffset(env, NULL), kLoadFailed);
42 EXPECT_DEATH(jniGetNioBufferFields(env, NULL, NULL, NULL, NULL), kLoadFailed);
43 EXPECT_DEATH(jniGetNioBufferPointer(env, NULL), kLoadFailed);
44 EXPECT_DEATH(jniUninitializeConstants(), kLoadFailed);
45 }
46
TEST_F(LibnativehelperLazyTest,NoLibnativehelperIsForJniInvocation)47 TEST_F(LibnativehelperLazyTest, NoLibnativehelperIsForJniInvocation) {
48 EXPECT_DEATH(JniInvocationCreate(), kLoadFailed);
49 EXPECT_DEATH(JniInvocationDestroy(NULL), kLoadFailed);
50 EXPECT_DEATH(JniInvocationGetLibrary("a", NULL), kLoadFailed);
51 EXPECT_DEATH(JniInvocationInit(NULL, "a"), kLoadFailed);
52 }
53
TEST_F(LibnativehelperLazyTest,NoLibnativehelperIsForJniApi)54 TEST_F(LibnativehelperLazyTest, NoLibnativehelperIsForJniApi) {
55 PreventLibnativehelperLazyLoadingForTests();
56
57 JavaVM* vm = NULL;
58 JNIEnv* env = NULL;
59 jsize count = 0;
60
61 EXPECT_DEATH(JNI_GetDefaultJavaVMInitArgs(NULL), kLoadFailed);
62 EXPECT_DEATH(JNI_CreateJavaVM(&vm, &env, NULL), kLoadFailed);
63 EXPECT_EQ(JNI_OK, JNI_GetCreatedJavaVMs(&vm, 1, &count));
64 EXPECT_EQ(0, count);
65 }
66