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 extern void *__system_property_area__; 41 42 struct 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 } 76 public: 77 bool valid; 78 private: 79 std::string pa_dirname; 80 std::string pa_filename; 81 void *old_pa; 82 }; 83 #endif 84 85 namespace android { 86 87 class JNIInvocationTest : public testing::Test { 88 }; 89 90 #ifdef HAVE_TEST_STUFF 91 static const char* kDebuggableSystemProperty = "ro.debuggable"; 92 static const char* kIsDebuggableValue = "1"; 93 static const char* kIsNotDebuggableValue = "0"; 94 95 static const char* kLibrarySystemProperty = "persist.sys.dalvik.vm.lib.2"; 96 static const char* kTestNonNull = "libartd.so"; 97 static const char* kTestNonNull2 = "libartd2.so"; 98 static const char* kExpected = "libart.so"; 99 #endif 100 101 TEST_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 108 char buffer[PROPERTY_VALUE_MAX]; 109 const char* result = JniInvocation::GetLibrary(NULL, buffer); 110 EXPECT_FALSE(result == NULL); 111 if (result != NULL) { 112 EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0); 113 EXPECT_FALSE(strcmp(result, kExpected) == 0); 114 } 115 116 result = JniInvocation::GetLibrary(kTestNonNull, buffer); 117 EXPECT_FALSE(result == NULL); 118 if (result != NULL) { 119 EXPECT_TRUE(strcmp(result, kTestNonNull) == 0); 120 EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0); 121 } 122 #else 123 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests."; 124 #endif 125 } 126 127 TEST_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 133 char buffer[PROPERTY_VALUE_MAX]; 134 const char* result = JniInvocation::GetLibrary(NULL, buffer); 135 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 142 result = JniInvocation::GetLibrary(kTestNonNull, buffer); 143 EXPECT_FALSE(result == NULL); 144 if (result != NULL) { 145 EXPECT_TRUE(strcmp(result, kExpected) == 0); 146 EXPECT_FALSE(strcmp(result, kTestNonNull) == 0); 147 } 148 #else 149 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests."; 150 #endif 151 } 152 153 } // namespace android 154