xref: /aosp_15_r20/external/perfetto/src/trace_redaction/collect_frame_cookies_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 "perfetto/trace_processor/trace_processor.h"
18 #include "src/base/test/status_matchers.h"
19 #include "src/trace_redaction/trace_redaction_integration_fixture.h"
20 #include "test/gtest_and_gmock.h"
21 
22 namespace perfetto::trace_redaction {
23 namespace {
24 constexpr auto kTrace = "test/data/trace-redaction-api-capture.pftrace";
25 
26 constexpr auto kPackageName = "com.prefabulated.touchlatency";
27 constexpr auto kPid = 4524;
28 }  // namespace
29 
30 class CollectFrameCookiesIntegrationTest
31     : public testing::Test,
32       protected TraceRedactionIntegrationFixure {
33  protected:
SetUp()34   void SetUp() override {
35     SetSourceTrace(kTrace);
36 
37     trace_processor::Config tp_config;
38     trace_processor_ =
39         trace_processor::TraceProcessor::CreateInstance(tp_config);
40 
41     TraceRedactor::Config tr_config;
42     auto trace_redactor = TraceRedactor::CreateInstance(tr_config);
43 
44     Context context;
45     context.package_name = kPackageName;
46 
47     ASSERT_OK(Redact(*trace_redactor, &context));
48 
49     auto raw = LoadRedacted();
50     ASSERT_OK(raw);
51 
52     auto read_buffer = std::make_unique<uint8_t[]>(raw->size());
53     memcpy(read_buffer.get(), raw->data(), raw->size());
54 
55     ASSERT_OK(trace_processor_->Parse(std::move(read_buffer), raw->size()));
56     ASSERT_OK(trace_processor_->NotifyEndOfFile());
57   }
58 
59   std::unique_ptr<trace_processor::TraceProcessor> trace_processor_;
60 };
61 
TEST_F(CollectFrameCookiesIntegrationTest,OnlyRetainsTargetActualFrames)62 TEST_F(CollectFrameCookiesIntegrationTest, OnlyRetainsTargetActualFrames) {
63   auto query =
64       " SELECT pid"
65       "   FROM process"
66       "   WHERE upid IN ("
67       "     SELECT DISTINCT upid FROM actual_frame_timeline_slice)";
68 
69   auto rows = trace_processor_->ExecuteQuery(query);
70 
71   ASSERT_TRUE(rows.Next());
72   ASSERT_EQ(rows.Get(0).AsLong(), kPid);
73 
74   ASSERT_FALSE(rows.Next());
75   ASSERT_OK(rows.Status());
76 }
77 
TEST_F(CollectFrameCookiesIntegrationTest,OnlyRetainsTargetExpectedFrames)78 TEST_F(CollectFrameCookiesIntegrationTest, OnlyRetainsTargetExpectedFrames) {
79   auto query =
80       " SELECT pid"
81       "   FROM process"
82       "   WHERE upid IN ("
83       "     SELECT DISTINCT upid FROM expected_frame_timeline_slice)";
84 
85   auto row = trace_processor_->ExecuteQuery(query);
86 
87   ASSERT_TRUE(row.Next());
88   ASSERT_EQ(row.Get(0).AsLong(), kPid);
89 
90   ASSERT_FALSE(row.Next());
91   ASSERT_OK(row.Status());
92 }
93 
94 // The target package has two overlapping timelines. So both tracks should exist
95 // under one pid.
TEST_F(CollectFrameCookiesIntegrationTest,RetainsOverlappingExpectedFrameEvents)96 TEST_F(CollectFrameCookiesIntegrationTest,
97        RetainsOverlappingExpectedFrameEvents) {
98   auto query =
99       " SELECT DISTINCT track_id, pid"
100       "   FROM expected_frame_timeline_slice"
101       "     JOIN process USING (upid)";
102 
103   auto rows = trace_processor_->ExecuteQuery(query);
104 
105   ASSERT_TRUE(rows.Next());
106   ASSERT_EQ(rows.Get(1).AsLong(), kPid);
107 
108   ASSERT_TRUE(rows.Next());
109   ASSERT_EQ(rows.Get(1).AsLong(), kPid);
110 
111   ASSERT_FALSE(rows.Next());
112   ASSERT_OK(rows.Status());
113 }
114 
115 }  // namespace perfetto::trace_redaction
116