1 /*
2 * Copyright (C) 2019 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 #include "palette/palette.h"
18
19 #include <jni.h>
20 #include <sys/mman.h>
21 #include <sys/syscall.h>
22 #include <unistd.h>
23
24 #include "gtest/gtest.h"
25
26 namespace {
27
GetTid()28 pid_t GetTid() {
29 #ifdef __BIONIC__
30 return gettid();
31 #else // __BIONIC__
32 return syscall(__NR_gettid);
33 #endif // __BIONIC__
34 }
35
36 } // namespace
37
38 class PaletteClientTest : public testing::Test {};
39
TEST_F(PaletteClientTest,SchedPriority)40 TEST_F(PaletteClientTest, SchedPriority) {
41 int32_t tid = GetTid();
42 int32_t saved_priority;
43 EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedGetPriority(tid, &saved_priority));
44
45 EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 0));
46 EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ -1));
47 EXPECT_EQ(PALETTE_STATUS_INVALID_ARGUMENT, PaletteSchedSetPriority(tid, /*java_priority=*/ 11));
48
49 EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, /*java_priority=*/ 1));
50 EXPECT_EQ(PALETTE_STATUS_OK, PaletteSchedSetPriority(tid, saved_priority));
51 }
52
TEST_F(PaletteClientTest,Trace)53 TEST_F(PaletteClientTest, Trace) {
54 bool enabled = false;
55 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnabled(&enabled));
56 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceBegin("Hello world!"));
57 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceEnd());
58 EXPECT_EQ(PALETTE_STATUS_OK, PaletteTraceIntegerValue("Beans", /*value=*/ 3));
59 }
60
TEST_F(PaletteClientTest,Ashmem)61 TEST_F(PaletteClientTest, Ashmem) {
62 #ifndef ART_TARGET_ANDROID
63 GTEST_SKIP() << "ashmem is only supported on Android";
64 #else
65 int fd;
66 EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemCreateRegion("ashmem-test", 4096, &fd));
67 EXPECT_EQ(PALETTE_STATUS_OK, PaletteAshmemSetProtRegion(fd, PROT_READ | PROT_EXEC));
68 EXPECT_EQ(0, close(fd));
69 #endif
70 }
71
TEST_F(PaletteClientTest,JniInvocation)72 TEST_F(PaletteClientTest, JniInvocation) {
73 #ifndef ART_TARGET_ANDROID
74 // On host we need to set up a boot classpath and pass it in here. Let's not
75 // bother since this test is only for native API coverage on target.
76 GTEST_SKIP() << "Will only spin up a VM on Android";
77 #else
78 bool enabled;
79 EXPECT_EQ(PALETTE_STATUS_OK, PaletteShouldReportJniInvocations(&enabled));
80
81 JavaVMInitArgs vm_args;
82 JavaVMOption options[] = {
83 {.optionString = "-verbose:jni", .extraInfo = nullptr},
84 };
85 vm_args.version = JNI_VERSION_1_6;
86 vm_args.nOptions = std::size(options);
87 vm_args.options = options;
88 vm_args.ignoreUnrecognized = JNI_TRUE;
89
90 JavaVM* jvm = nullptr;
91 JNIEnv* env = nullptr;
92 EXPECT_EQ(JNI_OK, JNI_CreateJavaVM(&jvm, &env, &vm_args));
93 ASSERT_NE(nullptr, env);
94
95 PaletteNotifyBeginJniInvocation(env);
96 PaletteNotifyEndJniInvocation(env);
97
98 EXPECT_EQ(JNI_OK, jvm->DestroyJavaVM());
99 #endif
100 }
101