Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 3544a93 | 2017-07-19 10:26:05 -0700 | [diff] [blame] | 19 | #include <nativehelper/JniInvocation.h> |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | |
| 23 | #include "string.h" |
| 24 | |
Elliott Hughes | 6887571 | 2015-07-31 08:51:17 -0700 | [diff] [blame] | 25 | #if defined(__ANDROID__) && defined(__BIONIC__) |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 26 | #define HAVE_TEST_STUFF 1 |
| 27 | #else |
| 28 | #undef HAVE_TEST_STUFF |
| 29 | #endif |
| 30 | |
| 31 | #ifdef HAVE_TEST_STUFF |
| 32 | |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 33 | // PROPERTY_VALUE_MAX. |
| 34 | #include "cutils/properties.h" |
| 35 | |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 36 | // Ability to have fake local system properties. |
| 37 | #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_ |
| 38 | #include <sys/_system_properties.h> |
| 39 | |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 40 | struct 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 Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 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 | |
| 60 | ~LocalPropertyTestState() { |
| 61 | if (!valid) { |
| 62 | return; |
| 63 | } |
| 64 | |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 65 | __system_property_set_filename(PROP_FILENAME); |
Victor Khimenko | 5038189 | 2017-02-28 21:15:42 +0100 | [diff] [blame] | 66 | __system_properties_init(); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 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; |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 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 | |
| 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 | |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 101 | char buffer[PROPERTY_VALUE_MAX]; |
| 102 | const char* result = JniInvocation::GetLibrary(NULL, buffer); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 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 | |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 109 | result = JniInvocation::GetLibrary(kTestNonNull, buffer); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 110 | EXPECT_FALSE(result == NULL); |
| 111 | if (result != NULL) { |
| 112 | EXPECT_TRUE(strcmp(result, kTestNonNull) == 0); |
| 113 | EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0); |
| 114 | } |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 115 | #else |
| 116 | GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests."; |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 117 | #endif |
| 118 | } |
| 119 | |
| 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 | |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 126 | char buffer[PROPERTY_VALUE_MAX]; |
| 127 | const char* result = JniInvocation::GetLibrary(NULL, buffer); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 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 | |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 135 | result = JniInvocation::GetLibrary(kTestNonNull, buffer); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 136 | EXPECT_FALSE(result == NULL); |
| 137 | if (result != NULL) { |
| 138 | EXPECT_TRUE(strcmp(result, kExpected) == 0); |
| 139 | EXPECT_FALSE(strcmp(result, kTestNonNull) == 0); |
| 140 | } |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 141 | #else |
| 142 | GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests."; |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 143 | #endif |
| 144 | } |
| 145 | |
| 146 | } // namespace android |