1 /*
2 * Copyright (C) 2020 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 "src/profiling/common/producer_support.h"
18
19 #include "perfetto/ext/base/file_utils.h"
20 #include "perfetto/ext/base/temp_file.h"
21 #include "perfetto/ext/base/utils.h"
22 #include "perfetto/tracing/core/data_source_config.h"
23 #include "test/gtest_and_gmock.h"
24
25 namespace perfetto {
26 namespace profiling {
27 namespace {
28
TEST(CanProfileAndroidTest,NonUserSystemExtraGuardrails)29 TEST(CanProfileAndroidTest, NonUserSystemExtraGuardrails) {
30 DataSourceConfig ds_config;
31 ds_config.set_enable_extra_guardrails(true);
32 EXPECT_TRUE(CanProfileAndroid(ds_config, 1, {}, "userdebug", "/dev/null"));
33 }
34
TEST(CanProfileAndroidTest,NonUserNonProfileableApp)35 TEST(CanProfileAndroidTest, NonUserNonProfileableApp) {
36 DataSourceConfig ds_config;
37 ds_config.set_enable_extra_guardrails(false);
38 auto tmp = base::TempFile::Create();
39 constexpr char content[] =
40 "invalid.example.profileable 10001 0 "
41 "/data/user/0/invalid.example.profileable default:targetSdkVersion=10000 "
42 "none 0 1\n";
43 base::WriteAll(tmp.fd(), content, sizeof(content));
44 EXPECT_TRUE(CanProfileAndroid(ds_config, 10001, {}, "userdebug", tmp.path()));
45 }
46
TEST(CanProfileAndroidTest,NonUserNonProfileableAppExtraGuardrails)47 TEST(CanProfileAndroidTest, NonUserNonProfileableAppExtraGuardrails) {
48 DataSourceConfig ds_config;
49 ds_config.set_enable_extra_guardrails(true);
50 auto tmp = base::TempFile::Create();
51 constexpr char content[] =
52 "invalid.example.profileable 10001 0 "
53 "/data/user/0/invalid.example.profileable default:targetSdkVersion=10000 "
54 "none 0 1\n";
55 base::WriteAll(tmp.fd(), content, sizeof(content));
56 EXPECT_TRUE(CanProfileAndroid(ds_config, 10001, {}, "userdebug", tmp.path()));
57 }
58
TEST(CanProfileAndroidTest,UserProfileableApp)59 TEST(CanProfileAndroidTest, UserProfileableApp) {
60 DataSourceConfig ds_config;
61 ds_config.set_enable_extra_guardrails(false);
62 auto tmp = base::TempFile::Create();
63 constexpr char content[] =
64 "invalid.example.profileable 10001 0 "
65 "/data/user/0/invalid.example.profileable default:targetSdkVersion=10000 "
66 "none 1 1\n";
67 base::WriteAll(tmp.fd(), content, sizeof(content));
68 EXPECT_TRUE(CanProfileAndroid(ds_config, 10001, {}, "user", tmp.path()));
69 }
70
TEST(CanProfileAndroidTest,UserProfileableAppMultiuser)71 TEST(CanProfileAndroidTest, UserProfileableAppMultiuser) {
72 DataSourceConfig ds_config;
73 ds_config.set_enable_extra_guardrails(false);
74 auto tmp = base::TempFile::Create();
75 constexpr char content[] =
76 "invalid.example.profileable 10001 0 "
77 "/data/user/0/invalid.example.profileable default:targetSdkVersion=10000 "
78 "none 1 1\n";
79 base::WriteAll(tmp.fd(), content, sizeof(content));
80 EXPECT_TRUE(CanProfileAndroid(ds_config, 210001, {}, "user", tmp.path()));
81 }
82
TEST(CanProfileAndroidTest,UserNonProfileableApp)83 TEST(CanProfileAndroidTest, UserNonProfileableApp) {
84 DataSourceConfig ds_config;
85 ds_config.set_enable_extra_guardrails(false);
86 auto tmp = base::TempFile::Create();
87 constexpr char content[] =
88 "invalid.example.profileable 10001 0 "
89 "/data/user/0/invalid.example.profileable default:targetSdkVersion=10000 "
90 "none 0 1\n";
91 base::WriteAll(tmp.fd(), content, sizeof(content));
92 EXPECT_FALSE(CanProfileAndroid(ds_config, 10001, {}, "user", tmp.path()));
93 }
94
TEST(CanProfileAndroidTest,UserDebuggableApp)95 TEST(CanProfileAndroidTest, UserDebuggableApp) {
96 DataSourceConfig ds_config;
97 ds_config.set_enable_extra_guardrails(false);
98 auto tmp = base::TempFile::Create();
99 constexpr char content[] =
100 "invalid.example.profileable 10001 1 "
101 "/data/user/0/invalid.example.profileable default:targetSdkVersion=10000 "
102 "none 0 1\n";
103 base::WriteAll(tmp.fd(), content, sizeof(content));
104 EXPECT_TRUE(CanProfileAndroid(ds_config, 10001, {}, "user", tmp.path()));
105 }
106
TEST(CanProfileAndroidTest,UserProfileableMatchingInstallerStatsd)107 TEST(CanProfileAndroidTest, UserProfileableMatchingInstallerStatsd) {
108 DataSourceConfig ds_config;
109 ds_config.set_session_initiator(
110 DataSourceConfig::SESSION_INITIATOR_TRUSTED_SYSTEM);
111 auto tmp = base::TempFile::Create();
112 constexpr char content[] =
113 "invalid.example 10001 0 /data/user/0/invalid.example "
114 "default:targetSdkVersion=29 3002,3003 0 13030407 1 invalid.store";
115 base::WriteAll(tmp.fd(), content, sizeof(content));
116 EXPECT_TRUE(CanProfileAndroid(ds_config, 10001, {"invalid.store"}, "user",
117 tmp.path()));
118 }
119
TEST(CanProfileAndroidTest,UserProfileableMatchingInstallerShell)120 TEST(CanProfileAndroidTest, UserProfileableMatchingInstallerShell) {
121 DataSourceConfig ds_config;
122 ds_config.set_session_initiator(
123 DataSourceConfig::SESSION_INITIATOR_UNSPECIFIED);
124 auto tmp = base::TempFile::Create();
125 constexpr char content[] =
126 "invalid.example 10001 0 /data/user/0/invalid.example "
127 "default:targetSdkVersion=29 3002,3003 0 13030407 1 invalid.store";
128 base::WriteAll(tmp.fd(), content, sizeof(content));
129 EXPECT_FALSE(CanProfileAndroid(ds_config, 10001, {"invalid.store"}, "user",
130 tmp.path()));
131 }
132
TEST(CanProfileAndroidTest,UserProfileableNonMatchingInstallerStatsd)133 TEST(CanProfileAndroidTest, UserProfileableNonMatchingInstallerStatsd) {
134 DataSourceConfig ds_config;
135 ds_config.set_session_initiator(
136 DataSourceConfig::SESSION_INITIATOR_TRUSTED_SYSTEM);
137 auto tmp = base::TempFile::Create();
138 constexpr char content[] =
139 "invalid.example 10001 0 /data/user/0/invalid.example "
140 "default:targetSdkVersion=29 3002,3003 0 13030407 1 invalid.store";
141 base::WriteAll(tmp.fd(), content, sizeof(content));
142 EXPECT_FALSE(CanProfileAndroid(ds_config, 10001, {"invalid.otherstore"},
143 "user", tmp.path()));
144 }
145
TEST(CanProfileAndroidTest,UserProfileableNonMatchingInstallerShell)146 TEST(CanProfileAndroidTest, UserProfileableNonMatchingInstallerShell) {
147 DataSourceConfig ds_config;
148 ds_config.set_session_initiator(
149 DataSourceConfig::SESSION_INITIATOR_UNSPECIFIED);
150 auto tmp = base::TempFile::Create();
151 constexpr char content[] =
152 "invalid.example 10001 0 /data/user/0/invalid.example "
153 "default:targetSdkVersion=29 3002,3003 0 13030407 1 invalid.store";
154 base::WriteAll(tmp.fd(), content, sizeof(content));
155 EXPECT_FALSE(CanProfileAndroid(ds_config, 10001, {"invalid.otherstore"},
156 "user", tmp.path()));
157 }
158
TEST(CanProfileAndroidTest,UserProfileableFromShellWithInstallerOldPackages)159 TEST(CanProfileAndroidTest, UserProfileableFromShellWithInstallerOldPackages) {
160 DataSourceConfig ds_config;
161 ds_config.set_session_initiator(
162 DataSourceConfig::SESSION_INITIATOR_UNSPECIFIED);
163 auto tmp = base::TempFile::Create();
164 constexpr char content[] =
165 "invalid.example 10001 0 /data/user/0/invalid.example "
166 "default:targetSdkVersion=29 3002,3003 1 13030407";
167 base::WriteAll(tmp.fd(), content, sizeof(content));
168 EXPECT_FALSE(CanProfileAndroid(ds_config, 10001, {"invalid.otherstore"},
169 "user", tmp.path()));
170 }
171
172 } // namespace
173 } // namespace profiling
174 } // namespace perfetto
175