1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "agent/profiler_impl.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "protocol_handler.h"
19
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::tooling;
22
23 namespace panda::test {
24 class ProfilerImplTest : public testing::Test {
25 public:
SetUpTestCase()26 static void SetUpTestCase()
27 {
28 GTEST_LOG_(INFO) << "SetUpTestCase";
29 }
30
TearDownTestCase()31 static void TearDownTestCase()
32 {
33 GTEST_LOG_(INFO) << "TearDownCase";
34 }
35
SetUp()36 void SetUp() override
37 {
38 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
39 }
40
TearDown()41 void TearDown() override
42 {
43 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
44 }
45
46 protected:
47 EcmaVM *ecmaVm {nullptr};
48 EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
HWTEST_F_L0(ProfilerImplTest,Disable)52 HWTEST_F_L0(ProfilerImplTest, Disable)
53 {
54 ProtocolChannel *channel = nullptr;
55 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
56 DispatchResponse response = profiler->Disable();
57 ASSERT_TRUE(response.GetMessage() == "");
58 ASSERT_TRUE(response.IsOk());
59 }
60
HWTEST_F_L0(ProfilerImplTest,Enable)61 HWTEST_F_L0(ProfilerImplTest, Enable)
62 {
63 ProtocolChannel *channel = nullptr;
64 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
65 DispatchResponse response = profiler->Enable();
66 ASSERT_TRUE(response.GetMessage() == "");
67 ASSERT_TRUE(response.IsOk());
68 }
69
70
HWTEST_F_L0(ProfilerImplTest,Start)71 HWTEST_F_L0(ProfilerImplTest, Start)
72 {
73 ProtocolChannel *channel = nullptr;
74 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
75 DispatchResponse response = profiler->Start();
76 std::unique_ptr<Profile> profile;
77 DispatchResponse response1 = profiler->Stop(&profile);
78 ASSERT_TRUE(response.IsOk());
79 ASSERT_TRUE(response1.IsOk());
80 ASSERT_TRUE(profile);
81 }
82
HWTEST_F_L0(ProfilerImplTest,Stop)83 HWTEST_F_L0(ProfilerImplTest, Stop)
84 {
85 ProtocolChannel *channel = nullptr;
86 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
87 std::unique_ptr<Profile> profile;
88 DispatchResponse response = profiler->Stop(&profile);
89 ASSERT_TRUE(response.GetMessage().find("Stop is failure") != std::string::npos);
90 ASSERT_TRUE(!response.IsOk());
91 }
92
HWTEST_F_L0(ProfilerImplTest,SetSamplingInterval)93 HWTEST_F_L0(ProfilerImplTest, SetSamplingInterval)
94 {
95 ProtocolChannel *channel = nullptr;
96 SetSamplingIntervalParams params;
97 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
98 DispatchResponse response = profiler->SetSamplingInterval(params);
99 ASSERT_TRUE(response.GetMessage() == "");
100 ASSERT_TRUE(response.IsOk());
101 }
102
HWTEST_F_L0(ProfilerImplTest,GetBestEffortCoverage)103 HWTEST_F_L0(ProfilerImplTest, GetBestEffortCoverage)
104 {
105 ProtocolChannel *channel = nullptr;
106 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
107 DispatchResponse response = profiler->GetBestEffortCoverage();
108 ASSERT_TRUE(response.GetMessage() == "GetBestEffortCoverage not support now");
109 }
110
HWTEST_F_L0(ProfilerImplTest,StopPreciseCoverage)111 HWTEST_F_L0(ProfilerImplTest, StopPreciseCoverage)
112 {
113 ProtocolChannel *channel = nullptr;
114 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
115 DispatchResponse response = profiler->StopPreciseCoverage();
116 ASSERT_TRUE(response.GetMessage() == "StopPreciseCoverage not support now");
117 }
118
HWTEST_F_L0(ProfilerImplTest,TakePreciseCoverage)119 HWTEST_F_L0(ProfilerImplTest, TakePreciseCoverage)
120 {
121 ProtocolChannel *channel = nullptr;
122 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
123 DispatchResponse response = profiler->TakePreciseCoverage();
124 ASSERT_TRUE(response.GetMessage() == "TakePreciseCoverage not support now");
125 }
126
HWTEST_F_L0(ProfilerImplTest,StartPreciseCoverage)127 HWTEST_F_L0(ProfilerImplTest, StartPreciseCoverage)
128 {
129 ProtocolChannel *channel = nullptr;
130 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
131 std::string msg = std::string() + R"({"id":0,"method":"Debugger.requestMemoryDump","params":{}})";
132 DispatchRequest request = DispatchRequest(msg);
133 std::unique_ptr<StartPreciseCoverageParams> params = StartPreciseCoverageParams::Create(request.GetParams());
134 DispatchResponse response = profiler->StartPreciseCoverage(*params);
135 ASSERT_TRUE(response.GetMessage() == "StartPreciseCoverage not support now");
136 }
137
HWTEST_F_L0(ProfilerImplTest,StartTypeProfile)138 HWTEST_F_L0(ProfilerImplTest, StartTypeProfile)
139 {
140 ProtocolChannel *channel = nullptr;
141 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
142 DispatchResponse response = profiler->StartTypeProfile();
143 ASSERT_TRUE(response.GetMessage() == "StartTypeProfile not support now");
144 }
145
HWTEST_F_L0(ProfilerImplTest,StopTypeProfile)146 HWTEST_F_L0(ProfilerImplTest, StopTypeProfile)
147 {
148 ProtocolChannel *channel = nullptr;
149 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
150 DispatchResponse response = profiler->StopTypeProfile();
151 ASSERT_TRUE(response.GetMessage() == "StopTypeProfile not support now");
152 }
153
HWTEST_F_L0(ProfilerImplTest,TakeTypeProfile)154 HWTEST_F_L0(ProfilerImplTest, TakeTypeProfile)
155 {
156 ProtocolChannel *channel = nullptr;
157 auto profiler = std::make_unique<ProfilerImpl>(ecmaVm, channel);
158 DispatchResponse response = profiler->TakeTypeProfile();
159 ASSERT_TRUE(response.GetMessage() == "TakeTypeProfile not support now");
160 }
161
HWTEST_F_L0(ProfilerImplTest,DispatcherImplDispatch)162 HWTEST_F_L0(ProfilerImplTest, DispatcherImplDispatch)
163 {
164 std::string result = "";
165 std::function<void(const void*, const std::string &)> callback =
166 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
167 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
168 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
169 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
170 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
171 DispatchRequest request(msg);
172 dispatcherImpl->Dispatch(request);
173 ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
174 msg = std::string() + R"({"id":0,"method":"Debugger.disable","params":{}})";
175 DispatchRequest request1 = DispatchRequest(msg);
176 dispatcherImpl->Dispatch(request1);
177 if (channel) {
178 delete channel;
179 channel = nullptr;
180 }
181 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
182 }
183
HWTEST_F_L0(ProfilerImplTest,DispatcherImplEnable)184 HWTEST_F_L0(ProfilerImplTest, DispatcherImplEnable)
185 {
186 std::string result = "";
187 std::function<void(const void*, const std::string &)> callback =
188 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
189 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
190 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
191 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
192 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
193 DispatchRequest request(msg);
194 dispatcherImpl->Enable(request);
195 if (channel) {
196 delete channel;
197 channel = nullptr;
198 }
199 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
200 }
201
HWTEST_F_L0(ProfilerImplTest,DispatcherImplDisable)202 HWTEST_F_L0(ProfilerImplTest, DispatcherImplDisable)
203 {
204 std::string result = "";
205 std::function<void(const void*, const std::string &)> callback =
206 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
207 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
208 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
209 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
210 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
211 DispatchRequest request(msg);
212 dispatcherImpl->Disable(request);
213 if (channel) {
214 delete channel;
215 channel = nullptr;
216 }
217 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
218 }
219
HWTEST_F_L0(ProfilerImplTest,DispatcherImplStart)220 HWTEST_F_L0(ProfilerImplTest, DispatcherImplStart)
221 {
222 std::string result = "";
223 std::function<void(const void*, const std::string &)> callback =
224 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
225 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
226 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
227 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
228 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
229 DispatchRequest request(msg);
230 dispatcherImpl->Start(request);
231 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
232 dispatcherImpl->Stop(request);
233 if (channel) {
234 delete channel;
235 channel = nullptr;
236 }
237 ASSERT_TRUE(result.find("\"id\":0") != std::string::npos);
238 ASSERT_TRUE(result.find("\"profile\"") != std::string::npos);
239 }
240
HWTEST_F_L0(ProfilerImplTest,DispatcherImplStop)241 HWTEST_F_L0(ProfilerImplTest, DispatcherImplStop)
242 {
243 std::string result = "";
244 std::function<void(const void*, const std::string &)> callback =
245 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
246 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
247 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
248 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
249 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
250 DispatchRequest request(msg);
251 dispatcherImpl->Stop(request);
252 if (channel) {
253 delete channel;
254 channel = nullptr;
255 }
256 ASSERT_TRUE(result.find("Stop is failure") != std::string::npos);
257 }
258
HWTEST_F_L0(ProfilerImplTest,DispatcherImplSetSamplingInterval)259 HWTEST_F_L0(ProfilerImplTest, DispatcherImplSetSamplingInterval)
260 {
261 std::string result = "";
262 std::function<void(const void*, const std::string &)> callback =
263 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
264 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
265 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
266 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
267 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
268 DispatchRequest request(msg);
269 dispatcherImpl->SetSamplingInterval(request);
270 ASSERT_TRUE(result.find("wrong params") != std::string::npos);
271 msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{"interval":24}})";
272 DispatchRequest request1(msg);
273 dispatcherImpl->SetSamplingInterval(request1);
274 if (channel) {
275 delete channel;
276 channel = nullptr;
277 }
278 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
279 }
280
HWTEST_F_L0(ProfilerImplTest,DispatcherImplGetBestEffortCoverage)281 HWTEST_F_L0(ProfilerImplTest, DispatcherImplGetBestEffortCoverage)
282 {
283 std::string result = "";
284 std::function<void(const void*, const std::string &)> callback =
285 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
286 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
287 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
288 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
289 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
290 DispatchRequest request(msg);
291 dispatcherImpl->GetBestEffortCoverage(request);
292 if (channel) {
293 delete channel;
294 channel = nullptr;
295 }
296 ASSERT_TRUE(result.find("GetBestEffortCoverage not support now") != std::string::npos);
297 }
298
HWTEST_F_L0(ProfilerImplTest,DispatcherImplStopPreciseCoverage)299 HWTEST_F_L0(ProfilerImplTest, DispatcherImplStopPreciseCoverage)
300 {
301 std::string result = "";
302 std::function<void(const void*, const std::string &)> callback =
303 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
304 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
305 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
306 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
307 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
308 DispatchRequest request(msg);
309 dispatcherImpl->StopPreciseCoverage(request);
310 if (channel) {
311 delete channel;
312 channel = nullptr;
313 }
314 ASSERT_TRUE(result.find("StopPreciseCoverage not support now") != std::string::npos);
315 }
316
HWTEST_F_L0(ProfilerImplTest,DispatcherImplTakePreciseCoverage)317 HWTEST_F_L0(ProfilerImplTest, DispatcherImplTakePreciseCoverage)
318 {
319 std::string result = "";
320 std::function<void(const void*, const std::string &)> callback =
321 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
322 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
323 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
324 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
325 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
326 DispatchRequest request(msg);
327 dispatcherImpl->TakePreciseCoverage(request);
328 if (channel) {
329 delete channel;
330 channel = nullptr;
331 }
332 ASSERT_TRUE(result.find("TakePreciseCoverage not support now") != std::string::npos);
333 }
334
HWTEST_F_L0(ProfilerImplTest,DispatcherImplStartPreciseCoverage)335 HWTEST_F_L0(ProfilerImplTest, DispatcherImplStartPreciseCoverage)
336 {
337 std::string result = "";
338 std::function<void(const void*, const std::string &)> callback =
339 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
340 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
341 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
342 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
343 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
344 DispatchRequest request(msg);
345 dispatcherImpl->StartPreciseCoverage(request);
346 if (channel) {
347 delete channel;
348 channel = nullptr;
349 }
350 ASSERT_TRUE(result.find("StartPreciseCoverage not support now") != std::string::npos);
351 }
352
HWTEST_F_L0(ProfilerImplTest,DispatcherImplStartTypeProfile)353 HWTEST_F_L0(ProfilerImplTest, DispatcherImplStartTypeProfile)
354 {
355 std::string result = "";
356 std::function<void(const void*, const std::string &)> callback =
357 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
358 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
359 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
360 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
361 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
362 DispatchRequest request(msg);
363 dispatcherImpl->StartTypeProfile(request);
364 if (channel) {
365 delete channel;
366 channel = nullptr;
367 }
368 ASSERT_TRUE(result.find("StartTypeProfile not support now") != std::string::npos);
369 }
370
HWTEST_F_L0(ProfilerImplTest,DispatcherImplStopTypeProfile)371 HWTEST_F_L0(ProfilerImplTest, DispatcherImplStopTypeProfile)
372 {
373 std::string result = "";
374 std::function<void(const void*, const std::string &)> callback =
375 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
376 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
377 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
378 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
379 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
380 DispatchRequest request(msg);
381 dispatcherImpl->StopTypeProfile(request);
382 if (channel) {
383 delete channel;
384 channel = nullptr;
385 }
386 ASSERT_TRUE(result.find("StopTypeProfile not support now") != std::string::npos);
387 }
388
HWTEST_F_L0(ProfilerImplTest,DispatcherImplTakeTypeProfile)389 HWTEST_F_L0(ProfilerImplTest, DispatcherImplTakeTypeProfile)
390 {
391 std::string result = "";
392 std::function<void(const void*, const std::string &)> callback =
393 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
394 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
395 auto tracing = std::make_unique<ProfilerImpl>(ecmaVm, channel);
396 auto dispatcherImpl = std::make_unique<ProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
397 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
398 DispatchRequest request(msg);
399 dispatcherImpl->TakeTypeProfile(request);
400 if (channel) {
401 delete channel;
402 channel = nullptr;
403 }
404 ASSERT_TRUE(result.find("TakeTypeProfile not support now") != std::string::npos);
405 }
406
HWTEST_F_L0(ProfilerImplTest,FrontendPreciseCoverageDeltaUpdate)407 HWTEST_F_L0(ProfilerImplTest, FrontendPreciseCoverageDeltaUpdate)
408 {
409 std::string result = "";
410 std::function<void(const void*, const std::string &)> callback =
411 [&result]([[maybe_unused]]const void* ptr, const std::string &temp) { result = temp; };
412 ProtocolChannel *channel = nullptr;
413 auto frontend = std::make_unique<ProfilerImpl::Frontend>(channel);
414 frontend->PreciseCoverageDeltaUpdate();
415 ASSERT_TRUE(result == "");
416 if (!channel) {
417 channel = new ProtocolHandler(callback, ecmaVm);
418 }
419 auto frontend1 = std::make_unique<ProfilerImpl::Frontend>(channel);
420 frontend1->PreciseCoverageDeltaUpdate();
421 if (channel) {
422 delete channel;
423 channel = nullptr;
424 }
425 ASSERT_TRUE(result.find("Profile.PreciseCoverageDeltaUpdate") != std::string::npos);
426 }
427 } // namespace panda::test
428