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 #define LOG_TAG "NativeBridge_test"
18 
19 #include <nativehelper/JniInvocation.h>
20 #include <gtest/gtest.h>
21 
22 
23 #include "string.h"
24 
25 #if defined(__ANDROID__) && defined(__BIONIC__)
26 #define HAVE_TEST_STUFF 1
27 #else
28 #undef HAVE_TEST_STUFF
29 #endif
30 
31 #ifdef HAVE_TEST_STUFF
32 
33 // PROPERTY_VALUE_MAX.
34 #include "cutils/properties.h"
35 
36 #endif
37 
38 #ifdef HAVE_TEST_STUFF
39 static const char* kTestNonNull = "libartd.so";
40 static const char* kTestNonNull2 = "libartd2.so";
41 static const char* kExpected = "libart.so";
42 #endif
43 
TEST(JNIInvocation,Debuggable)44 TEST(JNIInvocation, Debuggable) {
45 #ifdef HAVE_TEST_STUFF
46     auto is_debuggable = []() { return true; };
47     auto get_library_system_property = [](char* buffer) -> int {
48         strcpy(buffer, kTestNonNull2);
49         return sizeof(kTestNonNull2);
50     };
51 
52     char buffer[PROPERTY_VALUE_MAX];
53     const char* result =
54         JniInvocation::GetLibrary(NULL, buffer, is_debuggable, get_library_system_property);
55     EXPECT_FALSE(result == NULL);
56     if (result != NULL) {
57         EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0);
58         EXPECT_FALSE(strcmp(result, kExpected) == 0);
59     }
60 
61     result =
62         JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, get_library_system_property);
63     EXPECT_FALSE(result == NULL);
64     if (result != NULL) {
65         EXPECT_TRUE(strcmp(result, kTestNonNull) == 0);
66         EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
67     }
68 #else
69     GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
70 #endif
71 }
72 
TEST(JNIInvocation,NonDebuggable)73 TEST(JNIInvocation, NonDebuggable) {
74 #ifdef HAVE_TEST_STUFF
75     auto is_debuggable = []() { return false; };
76 
77     char buffer[PROPERTY_VALUE_MAX];
78     const char* result = JniInvocation::GetLibrary(NULL, buffer, is_debuggable, nullptr);
79     EXPECT_FALSE(result == NULL);
80     if (result != NULL) {
81         EXPECT_TRUE(strcmp(result, kExpected) == 0);
82         EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
83         EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
84     }
85 
86     result = JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, nullptr);
87     EXPECT_FALSE(result == NULL);
88     if (result != NULL) {
89         EXPECT_TRUE(strcmp(result, kExpected) == 0);
90         EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
91     }
92 #else
93     GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
94 #endif
95 }
96