1 /*
2 * Copyright (C) 2022 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/traceconv/trace_to_text.h"
18
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/hash.h"
21 #include "test/gtest_and_gmock.h"
22
23 #include <fstream>
24
25 using std::string;
26
27 namespace perfetto {
28 namespace trace_to_text {
29
30 // Given a file, compute the checksum/hash of file.
31 // Learn more @ base::Hasher.
32 // Precondition: File should exist and be accessible.
FileHash(const string & filename)33 static uint64_t FileHash(const string& filename) {
34 base::Hasher hash;
35 std::ifstream input_f(filename, std::ios::binary);
36 PERFETTO_DCHECK(input_f.good());
37 char buffer[4096];
38 while (!input_f.eof()) {
39 input_f.read(buffer, sizeof(buffer));
40 if (input_f.gcount() > 0) {
41 hash.Update(buffer, size_t(input_f.gcount()));
42 }
43 }
44 return hash.digest();
45 }
46
TEST(TraceToText,DISABLED_Basic)47 TEST(TraceToText, DISABLED_Basic) {
48 auto tmp_file = "/tmp/trace_" + std::to_string(rand()) + ".txt";
49 auto input_file_names = {"test/data/example_android_trace_30s.pb.gz",
50 "test/data/example_android_trace_30s.pb"};
51 PERFETTO_LOG("tmp_file = %s.", tmp_file.c_str());
52 for (auto filename : input_file_names) {
53 {
54 std::ifstream input_f(filename, std::ios::binary);
55 std::ofstream output_f(tmp_file, std::ios::out | std::ios::binary);
56 EXPECT_TRUE(TraceToText(&input_f, &output_f));
57 PERFETTO_LOG("Processed %s", filename);
58 }
59 EXPECT_EQ(0xCD794377594BC7DCull, FileHash(tmp_file));
60 remove(tmp_file.c_str());
61 }
62 }
63
64 } // namespace trace_to_text
65 } // namespace perfetto
66