xref: /aosp_15_r20/external/perfetto/src/trace_redaction/process_thread_timeline_integrationtest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
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 <cstdint>
18 #include <string>
19 
20 #include "src/base/test/status_matchers.h"
21 #include "src/trace_redaction/collect_timeline_events.h"
22 #include "src/trace_redaction/find_package_uid.h"
23 #include "src/trace_redaction/trace_redaction_framework.h"
24 #include "src/trace_redaction/trace_redaction_integration_fixture.h"
25 #include "src/trace_redaction/trace_redactor.h"
26 #include "test/gtest_and_gmock.h"
27 
28 namespace perfetto::trace_redaction {
29 namespace {
30 // Every thread in the package stars before the trace and ends after the
31 // trace, allowing any time to be used for the query. This time is the
32 // start time of a slice in the trace.
33 constexpr uint64_t kTime = 6702094223167642;
34 }  // namespace
35 
36 class ProcessThreadTimelineIntegrationTest
37     : public testing::Test,
38       protected TraceRedactionIntegrationFixure {
39  protected:
SetUp()40   void SetUp() override {
41     context_.package_name = "com.Unity.com.unity.multiplayer.samples.coop";
42 
43     trace_redactor_.emplace_collect<FindPackageUid>();
44     trace_redactor_.emplace_collect<CollectTimelineEvents>();
45 
46     ASSERT_OK(Redact(trace_redactor_, &context_));
47   }
48 
49   Context context_;
50   TraceRedactor trace_redactor_;
51 };
52 
TEST_F(ProcessThreadTimelineIntegrationTest,PackageThreadsAreConnected)53 TEST_F(ProcessThreadTimelineIntegrationTest, PackageThreadsAreConnected) {
54   // select * from thread where upid in (
55   //   select upid from process where uid in (
56   //     select uid from package_list where
57   //     package_name='com.Unity.com.unity.multiplayer.samples.coop'))
58 
59   auto threads = {
60       7105, 7111, 7112, 7113, 7114, 7115, 7116, 7117, 7118, 7119, 7120,
61       7124, 7125, 7127, 7129, 7130, 7131, 7132, 7133, 7134, 7135, 7136,
62       7137, 7139, 7141, 7142, 7143, 7144, 7145, 7146, 7147, 7148, 7149,
63       7150, 7151, 7152, 7153, 7154, 7155, 7156, 7157, 7158, 7159, 7160,
64       7161, 7162, 7163, 7164, 7165, 7166, 7167, 7171, 7172, 7174, 7178,
65       7180, 7184, 7200, 7945, 7946, 7947, 7948, 7950, 7969,
66   };
67 
68   for (auto pid : threads) {
69     // Use EXPECT instead of ASSERT to test all values.
70     EXPECT_TRUE(
71         context_.timeline->PidConnectsToUid(kTime, pid, *context_.package_uid));
72   }
73 }
74 
TEST_F(ProcessThreadTimelineIntegrationTest,MainThreadIsConnected)75 TEST_F(ProcessThreadTimelineIntegrationTest, MainThreadIsConnected) {
76   // select * from process where uid in (
77   //   select uid from package_list where
78   //   package_name='com.Unity.com.unity.multiplayer.samples.coop')
79 
80   ASSERT_TRUE(
81       context_.timeline->PidConnectsToUid(kTime, 7105, *context_.package_uid));
82 }
83 
TEST_F(ProcessThreadTimelineIntegrationTest,DoesNotConnectDisconnectedMainThread)84 TEST_F(ProcessThreadTimelineIntegrationTest,
85        DoesNotConnectDisconnectedMainThread) {
86   // /vendor/bin/hw/android.hardware.audio.service
87   //
88   // select * from thread where upid in (
89   //   select upid from process where pid=1104)
90   //
91   // The audio server, like the targe threads, have no start or end time, so
92   // using the "whatever" time is okay. Because the audio service is not
93   // directly or indirectly connected to the target package, no thread should
94   // test as connected.
95 
96   auto threads = {
97       1104, 1135, 1142, 1169, 1176, 1602,  1609,  1610,
98       1617, 1689, 1690, 1692, 2190, 29650, 23020,
99   };
100 
101   for (auto pid : threads) {
102     // Use EXPECT instead of ASSERT to test all values.
103     EXPECT_FALSE(
104         context_.timeline->PidConnectsToUid(kTime, pid, *context_.package_uid));
105   }
106 }
107 
108 }  // namespace perfetto::trace_redaction
109