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,DisallowOomAdjusterWithSuffix)71 TEST_F(GuardrailTest, DisallowOomAdjusterWithSuffix) {
72   ::uprobestats::protos::UprobestatsConfig config;
73   config.add_tasks()->add_probe_configs()->set_method_signature(
74       "void com.android.server.am.OomAdjusterWithSomeSuffix.doWork()");
75   EXPECT_FALSE(guardrail::isAllowed(config, "user", false));
76 
77   ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfig;
78   probeConfig.set_fully_qualified_class_name(
79       "com.android.server.am.OomAdjusterWithSomeSuffix");
80   probeConfig.set_method_name("doWork");
81   ::uprobestats::protos::UprobestatsConfig newConfig;
82   newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfig);
83   EXPECT_FALSE(guardrail::isAllowed(newConfig, "user", true));
84 }
85 
TEST_F(GuardrailTest,DisallowedMethodInSecondTask)86 TEST_F(GuardrailTest, DisallowedMethodInSecondTask) {
87   ::uprobestats::protos::UprobestatsConfig config;
88   config.add_tasks()->add_probe_configs()->set_method_signature(
89       "void com.android.server.am.OomAdjuster.setUidTempAllowlistStateLSP(int, "
90       "boolean)");
91   config.add_tasks()->add_probe_configs()->set_method_signature(
92       "void com.android.server.am.disallowedClass.doWork()");
93   EXPECT_FALSE(guardrail::isAllowed(config, "user", false));
94 
95   ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfig;
96   probeConfig.set_fully_qualified_class_name(
97       "com.android.server.am.OomAdjuster");
98   probeConfig.set_method_name("setUidTempAllowlistStateLSP");
99   ::uprobestats::protos::UprobestatsConfig::Task::ProbeConfig probeConfigTwo;
100   probeConfigTwo.set_fully_qualified_class_name(
101       "com.android.server.am.disallowedClass");
102   probeConfigTwo.set_method_name("doWork");
103   ::uprobestats::protos::UprobestatsConfig newConfig;
104   newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfig);
105   newConfig.add_tasks()->add_probe_configs()->CopyFrom(probeConfigTwo);
106   EXPECT_FALSE(guardrail::isAllowed(newConfig, "user", true));
107 }
108 
109 } // namespace uprobestats
110 } // namespace android
111