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 | #endif |
| 37 | |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 38 | #ifdef HAVE_TEST_STUFF |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 39 | static const char* kTestNonNull = "libartd.so"; |
| 40 | static const char* kTestNonNull2 = "libartd2.so"; |
| 41 | static const char* kExpected = "libart.so"; |
| 42 | #endif |
| 43 | |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 44 | TEST(JNIInvocation, Debuggable) { |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 45 | #ifdef HAVE_TEST_STUFF |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 46 | auto is_debuggable = []() { return true; }; |
| 47 | auto get_library_system_property = [](char* buffer) -> int { |
| 48 | strcpy(buffer, kTestNonNull2); |
| 49 | return sizeof(kTestNonNull2); |
| 50 | }; |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 51 | |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 52 | char buffer[PROPERTY_VALUE_MAX]; |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 53 | const char* result = |
| 54 | JniInvocation::GetLibrary(NULL, buffer, is_debuggable, get_library_system_property); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 55 | EXPECT_FALSE(result == NULL); |
| 56 | if (result != NULL) { |
| 57 | EXPECT_TRUE(strcmp(result, kTestNonNull2) == 0); |
| 58 | EXPECT_FALSE(strcmp(result, kExpected) == 0); |
| 59 | } |
| 60 | |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 61 | result = |
| 62 | JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, get_library_system_property); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 63 | EXPECT_FALSE(result == NULL); |
| 64 | if (result != NULL) { |
| 65 | EXPECT_TRUE(strcmp(result, kTestNonNull) == 0); |
| 66 | EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0); |
| 67 | } |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 68 | #else |
| 69 | GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests."; |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 70 | #endif |
| 71 | } |
| 72 | |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 73 | TEST(JNIInvocation, NonDebuggable) { |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 74 | #ifdef HAVE_TEST_STUFF |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 75 | auto is_debuggable = []() { return false; }; |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 76 | |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 77 | char buffer[PROPERTY_VALUE_MAX]; |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 78 | const char* result = JniInvocation::GetLibrary(NULL, buffer, is_debuggable, nullptr); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 79 | EXPECT_FALSE(result == NULL); |
| 80 | if (result != NULL) { |
| 81 | EXPECT_TRUE(strcmp(result, kExpected) == 0); |
| 82 | EXPECT_FALSE(strcmp(result, kTestNonNull) == 0); |
| 83 | EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0); |
| 84 | } |
| 85 | |
Tom Cherry | 3c46171 | 2017-12-14 00:00:15 -0800 | [diff] [blame^] | 86 | result = JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, nullptr); |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 87 | EXPECT_FALSE(result == NULL); |
| 88 | if (result != NULL) { |
| 89 | EXPECT_TRUE(strcmp(result, kExpected) == 0); |
| 90 | EXPECT_FALSE(strcmp(result, kTestNonNull) == 0); |
| 91 | } |
Ningsheng Jian | 88b84ec | 2014-09-17 13:34:09 +0800 | [diff] [blame] | 92 | #else |
| 93 | GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests."; |
Andreas Gampe | 5f4f4aa | 2014-08-19 16:57:38 -0700 | [diff] [blame] | 94 | #endif |
| 95 | } |