blob: 1467cb3d98c1acade10387beb78203ddea935e2a [file] [log] [blame]
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -07001/*
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
Steven Moreland3544a932017-07-19 10:26:05 -070019#include <nativehelper/JniInvocation.h>
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070020#include <gtest/gtest.h>
21
22
23#include "string.h"
24
Elliott Hughes68875712015-07-31 08:51:17 -070025#if defined(__ANDROID__) && defined(__BIONIC__)
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070026#define HAVE_TEST_STUFF 1
27#else
28#undef HAVE_TEST_STUFF
29#endif
30
31#ifdef HAVE_TEST_STUFF
32
Ningsheng Jian88b84ec2014-09-17 13:34:09 +080033// PROPERTY_VALUE_MAX.
34#include "cutils/properties.h"
35
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070036// Ability to have fake local system properties.
37#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
38#include <sys/_system_properties.h>
39
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070040struct LocalPropertyTestState {
41 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
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070052 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
60 ~LocalPropertyTestState() {
61 if (!valid) {
62 return;
63 }
64
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070065 __system_property_set_filename(PROP_FILENAME);
Victor Khimenko50381892017-02-28 21:15:42 +010066 __system_properties_init();
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070067 unlink(pa_filename.c_str());
68 rmdir(pa_dirname.c_str());
69 }
70public:
71 bool valid;
72private:
73 std::string pa_dirname;
74 std::string pa_filename;
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070075};
76#endif
77
78namespace android {
79
80class JNIInvocationTest : public testing::Test {
81};
82
83#ifdef HAVE_TEST_STUFF
84static const char* kDebuggableSystemProperty = "ro.debuggable";
85static const char* kIsDebuggableValue = "1";
86static const char* kIsNotDebuggableValue = "0";
87
88static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2";
89static const char* kTestNonNull = "libartd.so";
90static const char* kTestNonNull2 = "libartd2.so";
91static const char* kExpected = "libart.so";
92#endif
93
94TEST_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
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800101 char buffer[PROPERTY_VALUE_MAX];
102 const char* result = JniInvocation::GetLibrary(NULL, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700103 EXPECT_FALSE(result == NULL);
104 if (result != NULL) {
105 EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0);
106 EXPECT_FALSE(strcmp(result, kExpected) == 0);
107 }
108
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800109 result = JniInvocation::GetLibrary(kTestNonNull, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700110 EXPECT_FALSE(result == NULL);
111 if (result != NULL) {
112 EXPECT_TRUE(strcmp(result, kTestNonNull) == 0);
113 EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
114 }
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800115#else
116 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700117#endif
118}
119
120TEST_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
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800126 char buffer[PROPERTY_VALUE_MAX];
127 const char* result = JniInvocation::GetLibrary(NULL, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700128 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
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800135 result = JniInvocation::GetLibrary(kTestNonNull, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700136 EXPECT_FALSE(result == NULL);
137 if (result != NULL) {
138 EXPECT_TRUE(strcmp(result, kExpected) == 0);
139 EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
140 }
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800141#else
142 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700143#endif
144}
145
146} // namespace android