blob: bb62e3933a19905577cd1b444ffd7f94aef6c506 [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
Steven Moreland3544a932017-07-19 10:26:05 -070019#include <nativehelper/JniInvocation.h>
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070020#include <gtest/gtest.h>
21
22
23#include "string.h"
24
Elliott Hughes68875712015-07-31 08:51:17 -070025#if defined(__ANDROID__) && defined(__BIONIC__)
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070026#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#endif
37
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070038#ifdef HAVE_TEST_STUFF
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070039static const char* kTestNonNull = "libartd.so";
40static const char* kTestNonNull2 = "libartd2.so";
41static const char* kExpected = "libart.so";
42#endif
43
Tom Cherry3c461712017-12-14 00:00:15 -080044TEST(JNIInvocation, Debuggable) {
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070045#ifdef HAVE_TEST_STUFF
Tom Cherry3c461712017-12-14 00:00:15 -080046 auto is_debuggable = []() { return true; };
47 auto get_library_system_property = [](char* buffer) -> int {
48 strcpy(buffer, kTestNonNull2);
49 return sizeof(kTestNonNull2);
50 };
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070051
Ningsheng Jian88b84ec2014-09-17 13:34:09 +080052 char buffer[PROPERTY_VALUE_MAX];
Tom Cherry3c461712017-12-14 00:00:15 -080053 const char* result =
54 JniInvocation::GetLibrary(NULL, buffer, is_debuggable, get_library_system_property);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070055 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 Cherry3c461712017-12-14 00:00:15 -080061 result =
62 JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, get_library_system_property);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070063 EXPECT_FALSE(result == NULL);
64 if (result != NULL) {
65 EXPECT_TRUE(strcmp(result, kTestNonNull) == 0);
66 EXPECT_FALSE(strcmp(result, kTestNonNull2) == 0);
67 }
Ningsheng Jian88b84ec2014-09-17 13:34:09 +080068#else
69 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070070#endif
71}
72
Tom Cherry3c461712017-12-14 00:00:15 -080073TEST(JNIInvocation, NonDebuggable) {
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070074#ifdef HAVE_TEST_STUFF
Tom Cherry3c461712017-12-14 00:00:15 -080075 auto is_debuggable = []() { return false; };
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070076
Ningsheng Jian88b84ec2014-09-17 13:34:09 +080077 char buffer[PROPERTY_VALUE_MAX];
Tom Cherry3c461712017-12-14 00:00:15 -080078 const char* result = JniInvocation::GetLibrary(NULL, buffer, is_debuggable, nullptr);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070079 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 Cherry3c461712017-12-14 00:00:15 -080086 result = JniInvocation::GetLibrary(kTestNonNull, buffer, is_debuggable, nullptr);
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070087 EXPECT_FALSE(result == NULL);
88 if (result != NULL) {
89 EXPECT_TRUE(strcmp(result, kExpected) == 0);
90 EXPECT_FALSE(strcmp(result, kTestNonNull) == 0);
91 }
Ningsheng Jian88b84ec2014-09-17 13:34:09 +080092#else
93 GTEST_LOG_(WARNING) << "Host testing unsupported. Please run target tests.";
Andreas Gampe5f4f4aa2014-08-19 16:57:38 -070094#endif
95}