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 <sys/system_properties.h>
18 #include <random>
19 #include "test/gtest_and_gmock.h"
20
21 #include "perfetto/ext/base/android_utils.h"
22 #include "perfetto/ext/base/uuid.h"
23 #include "perfetto/tracing/core/data_source_config.h"
24 #include "src/base/test/test_task_runner.h"
25 #include "test/android_test_utils.h"
26 #include "test/test_helper.h"
27
28 #include "protos/perfetto/config/test_config.gen.h"
29 #include "protos/perfetto/trace/test_event.gen.h"
30 #include "protos/perfetto/trace/trace.gen.h"
31 #include "protos/perfetto/trace/trace_packet.gen.h"
32
33 namespace perfetto {
34 namespace {
35
TEST(PerfettoReporterTest,TestEndToEndReport)36 TEST(PerfettoReporterTest, TestEndToEndReport) {
37 base::TestTaskRunner task_runner;
38 TestHelper helper(&task_runner);
39 helper.ConnectFakeProducer();
40
41 TraceConfig trace_config;
42 trace_config.add_buffers()->set_size_kb(1024);
43 trace_config.set_duration_ms(200);
44 trace_config.set_allow_user_build_tracing(true);
45
46 auto* ds_config = trace_config.add_data_sources()->mutable_config();
47 ds_config->set_name("android.perfetto.FakeProducer");
48 ds_config->set_target_buffer(0);
49
50 base::Uuid uuid = base::Uuidv4();
51 trace_config.set_trace_uuid_lsb(uuid.lsb());
52 trace_config.set_trace_uuid_msb(uuid.msb());
53
54 static constexpr uint32_t kRandomSeed = 42;
55 static constexpr uint32_t kEventCount = 1;
56 static constexpr uint32_t kMessageSizeBytes = 2;
57 ds_config->mutable_for_testing()->set_seed(kRandomSeed);
58 ds_config->mutable_for_testing()->set_message_count(kEventCount);
59 ds_config->mutable_for_testing()->set_message_size(kMessageSizeBytes);
60 ds_config->mutable_for_testing()->set_send_batch_on_register(true);
61
62 auto* report_config = trace_config.mutable_android_report_config();
63 report_config->set_reporter_service_package("android.perfetto.cts.reporter");
64 report_config->set_reporter_service_class(
65 "android.perfetto.cts.reporter.PerfettoReportService");
66 report_config->set_use_pipe_in_framework_for_testing(true);
67
68 // We have to construct all the processes we want to fork before we start the
69 // service with |StartServiceIfRequired()|. this is because it is unsafe
70 // (could deadlock) to fork after we've spawned some threads which might
71 // printf (and thus hold locks).
72 auto perfetto_proc = Exec("perfetto",
73 {
74 "--upload",
75 "-c",
76 "-",
77 },
78 trace_config.SerializeAsString());
79
80 std::string stderr_str;
81 EXPECT_EQ(0, perfetto_proc.Run(&stderr_str)) << stderr_str;
82
83 static constexpr char kPath[] =
84 "/sdcard/Android/data/android.perfetto.cts.reporter/files/";
85 std::string path = kPath + uuid.ToPrettyString();
86 static constexpr uint32_t kIterationSleepMs = 500;
87 static constexpr uint32_t kIterationCount =
88 kDefaultTestTimeoutMs / kIterationSleepMs;
89 for (size_t i = 0; i < kIterationCount; ++i) {
90 if (!base::FileExists(path)) {
91 base::SleepMicroseconds(kIterationSleepMs * 1000);
92 continue;
93 }
94
95 std::string trace_str;
96 ASSERT_TRUE(base::ReadFile(path, &trace_str));
97
98 protos::gen::Trace trace;
99 ASSERT_TRUE(trace.ParseFromString(trace_str));
100 int for_testing = 0;
101 for (const auto& packet : trace.packet()) {
102 for_testing += packet.has_for_testing();
103 }
104 ASSERT_EQ(for_testing, kEventCount);
105 return;
106 }
107 FAIL() << "Timed out waiting for trace file";
108 }
109
110 } // namespace
111 } // namespace perfetto
112