1 /*
2 * Copyright (C) 2024 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 <gtest/gtest.h>
18
19 #include "Guardrail.h"
20
21 namespace android {
22 namespace uprobestats {
23
24 class GuardrailTest : public ::testing::Test {};
25
TEST_F(GuardrailTest,EverythingAllowedOnUserDebugAndEng)26 TEST_F(GuardrailTest, EverythingAllowedOnUserDebugAndEng) {
27 ::uprobestats::protos::UprobestatsConfig config;
28 config.add_tasks()->add_probe_configs()->set_method_signature(
29 "void com.android.server.am.SomeClass.doWork()");
30 EXPECT_TRUE(guardrail::isAllowed(config, "userdebug", false));
31 EXPECT_TRUE(guardrail::isAllowed(config, "eng", false));
32
33 ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfig;
34 probeConfig.set_fully_qualified_class_name("com.android.server.am.SomeClass");
35 probeConfig.set_method_name("doWork");
36 ::uprobestats::protos::UprobestatsConfig newConfig;
37 newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfig);
38 EXPECT_TRUE(guardrail::isAllowed(newConfig, "userdebug", true));
39 EXPECT_TRUE(guardrail::isAllowed(newConfig, "eng", true));
40 }
41
TEST_F(GuardrailTest,OomAdjusterAllowed)42 TEST_F(GuardrailTest, OomAdjusterAllowed) {
43 ::uprobestats::protos::UprobestatsConfig config;
44 config.add_tasks()->add_probe_configs()->set_method_signature(
45 "void com.android.server.am.OomAdjuster.setUidTempAllowlistStateLSP(int, "
46 "boolean)");
47 config.add_tasks()->add_probe_configs()->set_method_signature(
48 "void "
49 "com.android.server.am.OomAdjuster$$ExternalSyntheticLambda0.accept(java."
50 "lang.Object)");
51 EXPECT_TRUE(guardrail::isAllowed(config, "user", false));
52 EXPECT_TRUE(guardrail::isAllowed(config, "userdebug", false));
53 EXPECT_TRUE(guardrail::isAllowed(config, "eng", false));
54
55 ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfig;
56 probeConfig.set_fully_qualified_class_name(
57 "com.android.server.am.OomAdjuster");
58 probeConfig.set_method_name("setUidTempAllowlistStateLSP");
59 ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfigTwo;
60 probeConfigTwo.set_fully_qualified_class_name(
61 "com.android.server.am.OomAdjuster$$ExternalSyntheticLambda0");
62 probeConfigTwo.set_method_name("accept");
63 ::uprobestats::protos::UprobestatsConfig newConfig;
64 newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfig);
65 newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfigTwo);
66 EXPECT_TRUE(guardrail::isAllowed(newConfig, "user", true));
67 EXPECT_TRUE(guardrail::isAllowed(newConfig, "userdebug", true));
68 EXPECT_TRUE(guardrail::isAllowed(newConfig, "eng", true));
69 }
70
TEST_F(GuardrailTest,UpdateDeviceIdleTempAllowlistAllowed)71 TEST_F(GuardrailTest, UpdateDeviceIdleTempAllowlistAllowed) {
72 ::uprobestats::protos::UprobestatsConfig config;
73 ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig *probeConfig =
74 config.add_tasks()->add_probe_configs();
75 probeConfig->set_fully_qualified_class_name(
76 "com.android.server.am.ActivityManagerService$LocalService");
77 probeConfig->set_method_name("updateDeviceIdleTempAllowlist");
78 EXPECT_TRUE(guardrail::isAllowed(config, "user", true));
79 EXPECT_TRUE(guardrail::isAllowed(config, "userdebug", true));
80 EXPECT_TRUE(guardrail::isAllowed(config, "eng", true));
81
82 ::uprobestats::protos::UprobestatsConfig oldConfig;
83 oldConfig.add_tasks()->add_probe_configs()->set_method_signature(
84 "void com.android.server.am.ActivityManagerService$LocalService.updateDeviceIdleTempAllowlist()");
85
86 EXPECT_TRUE(guardrail::isAllowed(oldConfig, "user", false));
87 EXPECT_TRUE(guardrail::isAllowed(oldConfig, "userdebug", false));
88 EXPECT_TRUE(guardrail::isAllowed(oldConfig, "eng", false));
89 }
90
TEST_F(GuardrailTest,DisallowOomAdjusterWithSuffix)91 TEST_F(GuardrailTest, DisallowOomAdjusterWithSuffix) {
92 ::uprobestats::protos::UprobestatsConfig config;
93 config.add_tasks()->add_probe_configs()->set_method_signature(
94 "void com.android.server.am.OomAdjusterWithSomeSuffix.doWork()");
95 EXPECT_FALSE(guardrail::isAllowed(config, "user", false));
96
97 ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfig;
98 probeConfig.set_fully_qualified_class_name(
99 "com.android.server.am.OomAdjusterWithSomeSuffix");
100 probeConfig.set_method_name("doWork");
101 ::uprobestats::protos::UprobestatsConfig newConfig;
102 newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfig);
103 EXPECT_FALSE(guardrail::isAllowed(newConfig, "user", true));
104 }
105
TEST_F(GuardrailTest,DisallowedMethodInSecondTask)106 TEST_F(GuardrailTest, DisallowedMethodInSecondTask) {
107 ::uprobestats::protos::UprobestatsConfig config;
108 config.add_tasks()->add_probe_configs()->set_method_signature(
109 "void com.android.server.am.OomAdjuster.setUidTempAllowlistStateLSP(int, "
110 "boolean)");
111 config.add_tasks()->add_probe_configs()->set_method_signature(
112 "void com.android.server.am.disallowedClass.doWork()");
113 EXPECT_FALSE(guardrail::isAllowed(config, "user", false));
114
115 ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfig;
116 probeConfig.set_fully_qualified_class_name(
117 "com.android.server.am.OomAdjuster");
118 probeConfig.set_method_name("setUidTempAllowlistStateLSP");
119 ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfigTwo;
120 probeConfigTwo.set_fully_qualified_class_name(
121 "com.android.server.am.disallowedClass");
122 probeConfigTwo.set_method_name("doWork");
123 ::uprobestats::protos::UprobestatsConfig newConfig;
124 newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfig);
125 newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfigTwo);
126 EXPECT_FALSE(guardrail::isAllowed(newConfig, "user", true));
127 }
128
129 } // namespace uprobestats
130 } // namespace android
131