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 <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 // Ability to have fake local system properties.
37 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
38 #include <sys/_system_properties.h>
39 
40 struct LocalPropertyTestState {
LocalPropertyTestStateLocalPropertyTestState41     LocalPropertyTestState() : valid(false) {
42         const char* ANDROID_DATA = getenv("ANDROID_DATA");
43         char dir_template[PATH_MAX];
44         snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", ANDROID_DATA);
45         char* dirname = mkdtemp(dir_template);
46         if (!dirname) {
47             fprintf(stderr, "making temp file for test state failed (is %s writable?): %s",
48                     dir_template, strerror(errno));
49             return;
50         }
51 
52         pa_dirname = dirname;
53         pa_filename = pa_dirname + "/__properties__";
54 
55         __system_property_set_filename(pa_filename.c_str());
56         __system_property_area_init();
57         valid = true;
58     }
59 
~LocalPropertyTestStateLocalPropertyTestState60     ~LocalPropertyTestState() {
61         if (!valid) {
62             return;
63         }
64 
65         __system_property_set_filename(PROP_FILENAME);
66         __system_properties_init();
67         unlink(pa_filename.c_str());
68         rmdir(pa_dirname.c_str());
69     }
70 public:
71     bool valid;
72 private:
73     std::string pa_dirname;
74     std::string pa_filename;
75 };
76 #endif
77 
78 namespace android {
79 
80 class JNIInvocationTest : public testing::Test {
81 };
82 
83 #ifdef HAVE_TEST_STUFF
84 static const char* kDebuggableSystemProperty = "ro.debuggable";
85 static const char* kIsDebuggableValue = "1";
86 static const char* kIsNotDebuggableValue = "0";
87 
88 static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2";
89 static const char* kTestNonNull = "libartd.so";
90 static const char* kTestNonNull2 = "libartd2.so";
91 static const char* kExpected = "libart.so";
92 #endif
93 
TEST_F(JNIInvocationTest,Debuggable)94 TEST_F(JNIInvocationTest, Debuggable) {
95 #ifdef HAVE_TEST_STUFF
96     LocalPropertyTestState pa;
97     ASSERT_TRUE(pa.valid);
98     ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsDebuggableValue, 1));
99     ASSERT_EQ(0, __system_property_add(kLibrarySystemProperty, 27, kTestNonNull2, 11));
100 
101     char buffer[PROPERTY_VALUE_MAX];
102     const char* result = JniInvocation::GetLibrary(NULL, buffer);
103     EXPECT_FALSE(result == NULL);
104     if (result != NULL) {
105         EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0);
106         EXPECT_FALSE(strcmp(result, kExpected) == 0);
107     }
108 
109     result = JniInvocation::GetLibrary(kTestNonNull, buffer);
110     EXPECT_FALSE(result == NULL);
111     if (result != NULL) {
112         EXPECT_TRUE(strcmp(result, kTestNonNull) == 0);
113         EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
114     }
115 #else
116     GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
117 #endif
118 }
119 
TEST_F(JNIInvocationTest,NonDebuggable)120 TEST_F(JNIInvocationTest, NonDebuggable) {
121 #ifdef HAVE_TEST_STUFF
122     LocalPropertyTestState pa;
123     ASSERT_TRUE(pa.valid);
124     ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsNotDebuggableValue, 1));
125 
126     char buffer[PROPERTY_VALUE_MAX];
127     const char* result = JniInvocation::GetLibrary(NULL, buffer);
128     EXPECT_FALSE(result == NULL);
129     if (result != NULL) {
130         EXPECT_TRUE(strcmp(result, kExpected) == 0);
131         EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
132         EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
133     }
134 
135     result = JniInvocation::GetLibrary(kTestNonNull, buffer);
136     EXPECT_FALSE(result == NULL);
137     if (result != NULL) {
138         EXPECT_TRUE(strcmp(result, kExpected) == 0);
139         EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
140     }
141 #else
142     GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
143 #endif
144 }
145 
146 }  // namespace android
147