1 /*
2  * Copyright (C) 2014 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 "../JniInvocation-priv.h"
18 
19 #include <gtest/gtest.h>
20 #include <jni.h>
21 
22 
23 static const char* kDefaultJniInvocationLibrary = "libart.so";
24 static const char* kTestNonNull = "libartd.so";
25 static const char* kTestNonNull2 = "libartd2.so";
26 
27 TEST(JNIInvocation, Debuggable) {
28     const char* result = JniInvocationGetLibraryWith(nullptr, true, kTestNonNull2);
29     EXPECT_STREQ(result, kTestNonNull2);
30 
31     result = JniInvocationGetLibraryWith(kTestNonNull, true, kTestNonNull2);
32     EXPECT_STREQ(result, kTestNonNull);
33 
34     result = JniInvocationGetLibraryWith(kTestNonNull, true, nullptr);
35     EXPECT_STREQ(result, kTestNonNull);
36 
37     result = JniInvocationGetLibraryWith(nullptr, true, nullptr);
38     EXPECT_STREQ(result, kDefaultJniInvocationLibrary);
39 }
40 
41 TEST(JNIInvocation, NonDebuggable) {
42     const char* result = JniInvocationGetLibraryWith(nullptr, false, kTestNonNull2);
43     EXPECT_STREQ(result, kDefaultJniInvocationLibrary);
44 
45     result = JniInvocationGetLibraryWith(kTestNonNull, false, kTestNonNull2);
46     EXPECT_STREQ(result, kDefaultJniInvocationLibrary);
47 
48     result = JniInvocationGetLibraryWith(kTestNonNull, false, nullptr);
49     EXPECT_STREQ(result, kDefaultJniInvocationLibrary);
50 
51     result = JniInvocationGetLibraryWith(nullptr, false, nullptr);
52     EXPECT_STREQ(result, kDefaultJniInvocationLibrary);
53 }
54 
55 TEST(JNIInvocation, GetDefaultJavaVMInitArgsBeforeInit) {
56     EXPECT_DEATH(JNI_GetDefaultJavaVMInitArgs(nullptr), "Runtime library not loaded.");
57 }
58 
59 TEST(JNIInvocation, CreateJavaVMBeforeInit) {
60     JavaVM *vm;
61     JNIEnv *env;
62     EXPECT_DEATH(JNI_CreateJavaVM(&vm, &env, nullptr), "Runtime library not loaded.");
63 }
64 
65 TEST(JNIInvocation, GetCreatedJavaVMsBeforeInit) {
66     jsize vm_count;
67     JavaVM *vm;
68     int status = JNI_GetCreatedJavaVMs(&vm, 1, &vm_count);
69     EXPECT_EQ(status, JNI_OK);
70     EXPECT_EQ(vm_count, 0);
71 }
72