xref: /aosp_15_r20/external/perfetto/src/trace_redaction/trace_redaction_integration_fixture.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 "src/trace_redaction/trace_redaction_integration_fixture.h"
18 
19 #include "perfetto/ext/base/file_utils.h"
20 #include "src/base/test/utils.h"
21 
22 namespace perfetto::trace_redaction {
23 
TraceRedactionIntegrationFixure()24 TraceRedactionIntegrationFixure::TraceRedactionIntegrationFixure() {
25   dest_trace_ = tmp_dir_.AbsolutePath("dst.pftrace");
26 
27   // TODO: Most of the tests were written usng this trace. Those tests make a
28   // lot of assumption around using this trace. Those tests should be
29   // transitioned to the other constructor to remove this assumption.
30   SetSourceTrace("test/data/trace-redaction-general.pftrace");
31 }
32 
SetSourceTrace(std::string_view source_file)33 void TraceRedactionIntegrationFixure::SetSourceTrace(
34     std::string_view source_file) {
35   src_trace_ = base::GetTestDataPath(std::string(source_file));
36 }
37 
Redact(const TraceRedactor & redactor,Context * context)38 base::Status TraceRedactionIntegrationFixure::Redact(
39     const TraceRedactor& redactor,
40     Context* context) {
41   auto status = redactor.Redact(src_trace_, dest_trace_, context);
42 
43   if (status.ok()) {
44     tmp_dir_.TrackFile("dst.pftrace");
45   }
46 
47   return status;
48 }
49 
LoadOriginal() const50 base::StatusOr<std::string> TraceRedactionIntegrationFixure::LoadOriginal()
51     const {
52   return ReadRawTrace(src_trace_);
53 }
54 
LoadRedacted() const55 base::StatusOr<std::string> TraceRedactionIntegrationFixure::LoadRedacted()
56     const {
57   return ReadRawTrace(dest_trace_);
58 }
59 
ReadRawTrace(const std::string & path) const60 base::StatusOr<std::string> TraceRedactionIntegrationFixure::ReadRawTrace(
61     const std::string& path) const {
62   std::string redacted_buffer;
63 
64   if (base::ReadFile(path, &redacted_buffer)) {
65     return redacted_buffer;
66   }
67 
68   return base::ErrStatus("Failed to read %s", path.c_str());
69 }
70 
71 }  // namespace perfetto::trace_redaction
72