1 /*
2 * Copyright (C) 2018 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 "test/test_helper.h"
18
19 #include "gtest/gtest.h"
20 #include "perfetto/traced/traced.h"
21 #include "perfetto/tracing/core/trace_packet.h"
22 #include "test/task_runner_thread_delegates.h"
23
24 #include "src/tracing/ipc/default_socket.h"
25
26 #include "perfetto/trace/trace_packet.pb.h"
27 #include "perfetto/trace/trace_packet.pbzero.h"
28
29 namespace perfetto {
30
31 uint64_t TestHelper::next_instance_num_ = 0;
32
33 // If we're building on Android and starting the daemons ourselves,
34 // create the sockets in a world-writable location.
35 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) && \
36 PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
37 #define TEST_PRODUCER_SOCK_NAME "/data/local/tmp/traced_producer"
38 #define TEST_CONSUMER_SOCK_NAME "/data/local/tmp/traced_consumer"
39 #else
40 #define TEST_PRODUCER_SOCK_NAME ::perfetto::GetProducerSocket()
41 #define TEST_CONSUMER_SOCK_NAME ::perfetto::GetConsumerSocket()
42 #endif
43
TestHelper(base::TestTaskRunner * task_runner)44 TestHelper::TestHelper(base::TestTaskRunner* task_runner)
45 : instance_num_(next_instance_num_++),
46 task_runner_(task_runner),
47 service_thread_("perfetto.svc"),
48 producer_thread_("perfetto.prd") {}
49
OnConnect()50 void TestHelper::OnConnect() {
51 std::move(on_connect_callback_)();
52 }
53
OnDisconnect()54 void TestHelper::OnDisconnect() {
55 FAIL() << "Consumer unexpectedly disconnected from the service";
56 }
57
OnTracingDisabled()58 void TestHelper::OnTracingDisabled() {
59 std::move(on_stop_tracing_callback_)();
60 }
61
OnTraceData(std::vector<TracePacket> packets,bool has_more)62 void TestHelper::OnTraceData(std::vector<TracePacket> packets, bool has_more) {
63 for (auto& encoded_packet : packets) {
64 protos::TracePacket packet;
65 ASSERT_TRUE(encoded_packet.Decode(&packet));
66 if (packet.has_clock_snapshot() || packet.has_trace_config() ||
67 packet.has_trace_stats() || !packet.synchronization_marker().empty() ||
68 packet.has_system_info()) {
69 continue;
70 }
71 ASSERT_EQ(protos::TracePacket::kTrustedUid,
72 packet.optional_trusted_uid_case());
73 trace_.push_back(std::move(packet));
74 }
75
76 if (!has_more) {
77 std::move(on_packets_finished_callback_)();
78 }
79 }
80
StartServiceIfRequired()81 void TestHelper::StartServiceIfRequired() {
82 #if PERFETTO_BUILDFLAG(PERFETTO_START_DAEMONS)
83 service_thread_.Start(std::unique_ptr<ServiceDelegate>(
84 new ServiceDelegate(TEST_PRODUCER_SOCK_NAME, TEST_CONSUMER_SOCK_NAME)));
85 #endif
86 }
87
ConnectFakeProducer()88 FakeProducer* TestHelper::ConnectFakeProducer() {
89 std::unique_ptr<FakeProducerDelegate> producer_delegate(
90 new FakeProducerDelegate(TEST_PRODUCER_SOCK_NAME,
91 WrapTask(CreateCheckpoint("producer.setup")),
92 WrapTask(CreateCheckpoint("producer.enabled"))));
93 FakeProducerDelegate* producer_delegate_cached = producer_delegate.get();
94 producer_thread_.Start(std::move(producer_delegate));
95 return producer_delegate_cached->producer();
96 }
97
ConnectConsumer()98 void TestHelper::ConnectConsumer() {
99 cur_consumer_num_++;
100 on_connect_callback_ = CreateCheckpoint("consumer.connected." +
101 std::to_string(cur_consumer_num_));
102 endpoint_ =
103 ConsumerIPCClient::Connect(TEST_CONSUMER_SOCK_NAME, this, task_runner_);
104 }
105
DetachConsumer(const std::string & key)106 void TestHelper::DetachConsumer(const std::string& key) {
107 on_detach_callback_ = CreateCheckpoint("detach." + key);
108 endpoint_->Detach(key);
109 RunUntilCheckpoint("detach." + key);
110 endpoint_.reset();
111 }
112
AttachConsumer(const std::string & key)113 bool TestHelper::AttachConsumer(const std::string& key) {
114 bool success = false;
115 auto checkpoint = CreateCheckpoint("attach." + key);
116 on_attach_callback_ = [&success, checkpoint](bool s) {
117 success = s;
118 checkpoint();
119 };
120 endpoint_->Attach(key);
121 RunUntilCheckpoint("attach." + key);
122 return success;
123 }
124
StartTracing(const TraceConfig & config,base::ScopedFile file)125 void TestHelper::StartTracing(const TraceConfig& config,
126 base::ScopedFile file) {
127 trace_.clear();
128 on_stop_tracing_callback_ = CreateCheckpoint("stop.tracing");
129 endpoint_->EnableTracing(config, std::move(file));
130 }
131
DisableTracing()132 void TestHelper::DisableTracing() {
133 endpoint_->DisableTracing();
134 }
135
FlushAndWait(uint32_t timeout_ms)136 void TestHelper::FlushAndWait(uint32_t timeout_ms) {
137 static int flush_num = 0;
138 std::string checkpoint_name = "flush." + std::to_string(flush_num++);
139 auto checkpoint = CreateCheckpoint(checkpoint_name);
140 endpoint_->Flush(timeout_ms, [checkpoint](bool) { checkpoint(); });
141 RunUntilCheckpoint(checkpoint_name, timeout_ms + 1000);
142 }
143
ReadData(uint32_t read_count)144 void TestHelper::ReadData(uint32_t read_count) {
145 on_packets_finished_callback_ =
146 CreateCheckpoint("readback.complete." + std::to_string(read_count));
147 endpoint_->ReadBuffers();
148 }
149
WaitForConsumerConnect()150 void TestHelper::WaitForConsumerConnect() {
151 RunUntilCheckpoint("consumer.connected." + std::to_string(cur_consumer_num_));
152 }
153
WaitForProducerSetup()154 void TestHelper::WaitForProducerSetup() {
155 RunUntilCheckpoint("producer.setup");
156 }
157
WaitForProducerEnabled()158 void TestHelper::WaitForProducerEnabled() {
159 RunUntilCheckpoint("producer.enabled");
160 }
161
WaitForTracingDisabled(uint32_t timeout_ms)162 void TestHelper::WaitForTracingDisabled(uint32_t timeout_ms) {
163 RunUntilCheckpoint("stop.tracing", timeout_ms);
164 }
165
WaitForReadData(uint32_t read_count)166 void TestHelper::WaitForReadData(uint32_t read_count) {
167 RunUntilCheckpoint("readback.complete." + std::to_string(read_count));
168 }
169
WrapTask(const std::function<void ()> & function)170 std::function<void()> TestHelper::WrapTask(
171 const std::function<void()>& function) {
172 return [this, function] { task_runner_->PostTask(function); };
173 }
174
OnDetach(bool)175 void TestHelper::OnDetach(bool) {
176 if (on_detach_callback_)
177 std::move(on_detach_callback_)();
178 }
179
OnAttach(bool success,const TraceConfig &)180 void TestHelper::OnAttach(bool success, const TraceConfig&) {
181 if (on_attach_callback_)
182 std::move(on_attach_callback_)(success);
183 }
184
OnTraceStats(bool,const TraceStats &)185 void TestHelper::OnTraceStats(bool, const TraceStats&) {}
186
OnObservableEvents(const ObservableEvents &)187 void TestHelper::OnObservableEvents(const ObservableEvents&) {}
188
189 // static
GetConsumerSocketName()190 const char* TestHelper::GetConsumerSocketName() {
191 return TEST_CONSUMER_SOCK_NAME;
192 }
193
194 // static
GetProducerSocketName()195 const char* TestHelper::GetProducerSocketName() {
196 return TEST_PRODUCER_SOCK_NAME;
197 }
198
199 } // namespace perfetto
200