blob: b1d2b680f06488701cd4490dbcaadf720776b09b [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
19#include <JniInvocation.h>
20#include <gtest/gtest.h>
21
22
23#include "string.h"
24
25#if defined(HAVE_ANDROID_OS) && defined(__BIONIC__)
26#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
40extern void *__system_property_area__;
41
42struct LocalPropertyTestState {
43 LocalPropertyTestState() : valid(false) {
44 const char* ANDROID_DATA = getenv("ANDROID_DATA");
45 char dir_template[PATH_MAX];
46 snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", ANDROID_DATA);
47 char* dirname = mkdtemp(dir_template);
48 if (!dirname) {
49 fprintf(stderr, "making temp file for test state failed (is %s writable?): %s",
50 dir_template, strerror(errno));
51 return;
52 }
53
54 old_pa = __system_property_area__;
55 __system_property_area__ = NULL;
56
57 pa_dirname = dirname;
58 pa_filename = pa_dirname + "/__properties__";
59
60 __system_property_set_filename(pa_filename.c_str());
61 __system_property_area_init();
62 valid = true;
63 }
64
65 ~LocalPropertyTestState() {
66 if (!valid) {
67 return;
68 }
69
70 __system_property_area__ = old_pa;
71
72 __system_property_set_filename(PROP_FILENAME);
73 unlink(pa_filename.c_str());
74 rmdir(pa_dirname.c_str());
75 }
76public:
77 bool valid;
78private:
79 std::string pa_dirname;
80 std::string pa_filename;
81 void *old_pa;
82};
83#endif
84
85namespace android {
86
87class JNIInvocationTest : public testing::Test {
88};
89
90#ifdef HAVE_TEST_STUFF
91static const char* kDebuggableSystemProperty = "ro.debuggable";
92static const char* kIsDebuggableValue = "1";
93static const char* kIsNotDebuggableValue = "0";
94
95static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2";
96static const char* kTestNonNull = "libartd.so";
97static const char* kTestNonNull2 = "libartd2.so";
98static const char* kExpected = "libart.so";
99#endif
100
101TEST_F(JNIInvocationTest, Debuggable) {
102#ifdef HAVE_TEST_STUFF
103 LocalPropertyTestState pa;
104 ASSERT_TRUE(pa.valid);
105 ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsDebuggableValue, 1));
106 ASSERT_EQ(0, __system_property_add(kLibrarySystemProperty, 27, kTestNonNull2, 11));
107
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800108 char buffer[PROPERTY_VALUE_MAX];
109 const char* result = JniInvocation::GetLibrary(NULL, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700110 EXPECT_FALSE(result == NULL);
111 if (result != NULL) {
112 EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0);
113 EXPECT_FALSE(strcmp(result, kExpected) == 0);
114 }
115
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800116 result = JniInvocation::GetLibrary(kTestNonNull, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700117 EXPECT_FALSE(result == NULL);
118 if (result != NULL) {
119 EXPECT_TRUE(strcmp(result, kTestNonNull) == 0);
120 EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
121 }
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800122#else
123 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700124#endif
125}
126
127TEST_F(JNIInvocationTest, NonDebuggable) {
128#ifdef HAVE_TEST_STUFF
129 LocalPropertyTestState pa;
130 ASSERT_TRUE(pa.valid);
131 ASSERT_EQ(0, __system_property_add(kDebuggableSystemProperty, 13, kIsNotDebuggableValue, 1));
132
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800133 char buffer[PROPERTY_VALUE_MAX];
134 const char* result = JniInvocation::GetLibrary(NULL, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700135 EXPECT_FALSE(result == NULL);
136 if (result != NULL) {
137 EXPECT_TRUE(strcmp(result, kExpected) == 0);
138 EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
139 EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
140 }
141
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800142 result = JniInvocation::GetLibrary(kTestNonNull, buffer);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700143 EXPECT_FALSE(result == NULL);
144 if (result != NULL) {
145 EXPECT_TRUE(strcmp(result, kExpected) == 0);
146 EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
147 }
Ningsheng Jian88b84ec2014-09-17 13:34:09 +0800148#else
149 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -0700150#endif
151}
152
153} // namespace android