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