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/heapprofiler_impl.h"
17 #include "ecmascript/tests/test_helper.h"
18
19 using namespace panda::ecmascript;
20 using namespace panda::ecmascript::tooling;
21
22 namespace panda::ecmascript::tooling {
23 class HeapProfilerImplFriendTest {
24 public:
HeapProfilerImplFriendTest(std::unique_ptr<HeapProfilerImpl> & heapprofilerImpl)25 explicit HeapProfilerImplFriendTest(std::unique_ptr<HeapProfilerImpl> &heapprofilerImpl)
26 {
27 heapprofilerImpl_ = std::move(heapprofilerImpl);
28 }
29
ResetProfiles()30 void ResetProfiles()
31 {
32 heapprofilerImpl_->frontend_.ResetProfiles();
33 }
34
LastSeenObjectId(int32_t lastSeenObjectId,int64_t timeStampUs)35 void LastSeenObjectId(int32_t lastSeenObjectId, int64_t timeStampUs)
36 {
37 heapprofilerImpl_->frontend_.LastSeenObjectId(lastSeenObjectId, timeStampUs);
38 }
39
HeapStatsUpdate(HeapStat * updateData,int32_t count)40 void HeapStatsUpdate(HeapStat* updateData, int32_t count)
41 {
42 heapprofilerImpl_->frontend_.HeapStatsUpdate(updateData, count);
43 }
44
AddHeapSnapshotChunk(char * data,int32_t size)45 void AddHeapSnapshotChunk(char *data, int32_t size)
46 {
47 heapprofilerImpl_->frontend_.AddHeapSnapshotChunk(data, size);
48 }
49
ReportHeapSnapshotProgress(int32_t done,int32_t total)50 void ReportHeapSnapshotProgress(int32_t done, int32_t total)
51 {
52 heapprofilerImpl_->frontend_.ReportHeapSnapshotProgress(done, total);
53 }
54 private:
55 std::unique_ptr<HeapProfilerImpl> heapprofilerImpl_;
56 };
57 }
58
59 namespace panda::test {
60 class HeapProfilerImplTest : public testing::Test {
61 public:
SetUpTestCase()62 static void SetUpTestCase()
63 {
64 GTEST_LOG_(INFO) << "SetUpTestCase";
65 }
66
TearDownTestCase()67 static void TearDownTestCase()
68 {
69 GTEST_LOG_(INFO) << "TearDownCase";
70 }
71
SetUp()72 void SetUp() override
73 {
74 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
75 }
76
TearDown()77 void TearDown() override
78 {
79 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
80 }
81
82 protected:
83 EcmaVM *ecmaVm {nullptr};
84 EcmaHandleScope *scope {nullptr};
85 JSThread *thread {nullptr};
86 };
87
HWTEST_F_L0(HeapProfilerImplTest,AddInspectedHeapObject)88 HWTEST_F_L0(HeapProfilerImplTest, AddInspectedHeapObject)
89 {
90 ProtocolChannel *channel = nullptr;
91 AddInspectedHeapObjectParams param;
92 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
93 DispatchResponse response = heapProfiler->AddInspectedHeapObject(param);
94 ASSERT_TRUE(response.GetMessage() == "AddInspectedHeapObject not support now");
95 ASSERT_TRUE(!response.IsOk());
96 }
97
HWTEST_F_L0(HeapProfilerImplTest,CollectGarbage)98 HWTEST_F_L0(HeapProfilerImplTest, CollectGarbage)
99 {
100 ProtocolChannel *channel = nullptr;
101 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
102 DispatchResponse response = heapProfiler->CollectGarbage();
103 ASSERT_TRUE(response.GetMessage() == "");
104 ASSERT_TRUE(response.IsOk());
105 }
106
HWTEST_F_L0(HeapProfilerImplTest,Enable)107 HWTEST_F_L0(HeapProfilerImplTest, Enable)
108 {
109 ProtocolChannel *channel = nullptr;
110 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
111 DispatchResponse response = heapProfiler->Enable();
112 ASSERT_TRUE(response.GetMessage() == "");
113 ASSERT_TRUE(response.IsOk());
114 }
115
HWTEST_F_L0(HeapProfilerImplTest,Disable)116 HWTEST_F_L0(HeapProfilerImplTest, Disable)
117 {
118 ProtocolChannel *channel = nullptr;
119 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
120 DispatchResponse response = heapProfiler->Disable();
121 ASSERT_TRUE(response.GetMessage() == "");
122 ASSERT_TRUE(response.IsOk());
123 }
124
HWTEST_F_L0(HeapProfilerImplTest,GetHeapObjectId)125 HWTEST_F_L0(HeapProfilerImplTest, GetHeapObjectId)
126 {
127 ProtocolChannel *channel = nullptr;
128 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
129 GetHeapObjectIdParams params;
130 HeapSnapshotObjectId objectId;
131 DispatchResponse response = heapProfiler->GetHeapObjectId(params, &objectId);
132 ASSERT_TRUE(response.GetMessage() == "GetHeapObjectId not support now");
133 ASSERT_TRUE(!response.IsOk());
134 }
135
HWTEST_F_L0(HeapProfilerImplTest,GetObjectByHeapObjectId)136 HWTEST_F_L0(HeapProfilerImplTest, GetObjectByHeapObjectId)
137 {
138 ProtocolChannel *channel = nullptr;
139 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
140 std::unique_ptr<GetObjectByHeapObjectIdParams> params;
141 std::unique_ptr<RemoteObject> remoteObjectResult;
142 DispatchResponse response = heapProfiler->GetObjectByHeapObjectId(*params, &remoteObjectResult);
143 ASSERT_TRUE(response.GetMessage() == "GetObjectByHeapObjectId not support now");
144 ASSERT_TRUE(!response.IsOk());
145 }
146
HWTEST_F_L0(HeapProfilerImplTest,GetSamplingProfile)147 HWTEST_F_L0(HeapProfilerImplTest, GetSamplingProfile)
148 {
149 ProtocolChannel *channel = nullptr;
150 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
151 std::unique_ptr<SamplingHeapProfile> profile;
152 DispatchResponse response = heapProfiler->GetSamplingProfile(&profile);
153 ASSERT_TRUE(response.GetMessage() == "GetSamplingProfile fail");
154 ASSERT_TRUE(!response.IsOk());
155 }
156
HWTEST_F_L0(HeapProfilerImplTest,StartSampling)157 HWTEST_F_L0(HeapProfilerImplTest, StartSampling)
158 {
159 ProtocolChannel *channel = nullptr;
160 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
161 StartSamplingParams params;
162 DispatchResponse response = heapProfiler->StartSampling(params);
163 ASSERT_TRUE(response.IsOk());
164 }
165
HWTEST_F_L0(HeapProfilerImplTest,StopSampling)166 HWTEST_F_L0(HeapProfilerImplTest, StopSampling)
167 {
168 ProtocolChannel *channel = nullptr;
169 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
170 std::unique_ptr<SamplingHeapProfile> profile;
171 DispatchResponse response = heapProfiler->StopSampling(&profile);
172 ASSERT_TRUE(response.GetMessage() == "StopSampling fail");
173 ASSERT_TRUE(!response.IsOk());
174 }
175
HWTEST_F_L0(HeapProfilerImplTest,TakeHeapSnapshot)176 HWTEST_F_L0(HeapProfilerImplTest, TakeHeapSnapshot)
177 {
178 ProtocolChannel *channel = nullptr;
179 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
180 StopTrackingHeapObjectsParams params;
181 DispatchResponse response = heapProfiler->TakeHeapSnapshot(params);
182 ASSERT_TRUE(response.GetMessage() == "");
183 ASSERT_TRUE(response.IsOk());
184 }
185
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplDispatch)186 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplDispatch)
187 {
188 std::string result = "";
189 std::function<void(const void*, const std::string &)> callback =
190 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
191 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
192 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
193 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
194 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
195 DispatchRequest request(msg);
196 dispatcherImpl->Dispatch(request);
197 ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
198 msg = std::string() + R"({"id":0,"method":"Debugger.disable","params":{}})";
199 DispatchRequest request1 = DispatchRequest(msg);
200 dispatcherImpl->Dispatch(request1);
201 if (channel) {
202 delete channel;
203 channel = nullptr;
204 }
205 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
206 }
207
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplAddInspectedHeapObject)208 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplAddInspectedHeapObject)
209 {
210 std::string result = "";
211 std::function<void(const void*, const std::string &)> callback =
212 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
213 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
214 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
215 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
216 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.addInspectedHeapObject","params":{}})";
217 DispatchRequest request(msg);
218 dispatcherImpl->Dispatch(request);
219 ASSERT_TRUE(result.find("wrong params") != std::string::npos);
220 msg = std::string() + R"({"id":0,"method":"HeapProfiler.addInspectedHeapObject","params":{"heapObjectId":"0"}})";
221 DispatchRequest request1 = DispatchRequest(msg);
222 dispatcherImpl->Dispatch(request1);
223 if (channel) {
224 delete channel;
225 channel = nullptr;
226 }
227 ASSERT_TRUE(result.find("AddInspectedHeapObject not support now") != std::string::npos);
228 }
229
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplCollectGarbage)230 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplCollectGarbage)
231 {
232 std::string result = "";
233 std::function<void(const void*, const std::string &)> callback =
234 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
235 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
236 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
237 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
238 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.collectGarbage","params":{}})";
239 DispatchRequest request(msg);
240 dispatcherImpl->Dispatch(request);
241 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
242 if (channel) {
243 delete channel;
244 channel = nullptr;
245 }
246 }
247
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplEnable)248 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplEnable)
249 {
250 std::string result = "";
251 std::function<void(const void*, const std::string &)> callback =
252 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
253 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
254 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
255 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
256 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.enable","params":{}})";
257 DispatchRequest request(msg);
258 dispatcherImpl->Dispatch(request);
259 ASSERT_TRUE(result.find("protocols") != std::string::npos);
260 if (channel) {
261 delete channel;
262 channel = nullptr;
263 }
264 }
265
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplDisable)266 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplDisable)
267 {
268 std::string result = "";
269 std::function<void(const void*, const std::string &)> callback =
270 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
271 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
272 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
273 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
274 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.disable","params":{}})";
275 DispatchRequest request(msg);
276 dispatcherImpl->Dispatch(request);
277 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
278 if (channel) {
279 delete channel;
280 channel = nullptr;
281 }
282 }
283
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplGetHeapObjectId)284 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetHeapObjectId)
285 {
286 std::string result = "";
287 std::function<void(const void*, const std::string &)> callback =
288 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
289 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
290 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
291 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
292 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.getHeapObjectId","params":{"objectId":true}})";
293 DispatchRequest request(msg);
294 dispatcherImpl->Dispatch(request);
295 ASSERT_TRUE(result.find("wrong params") != std::string::npos);
296 msg = std::string() + R"({"id":0,"method":"HeapProfiler.getHeapObjectId","params":{"objectId":"0"}})";
297 DispatchRequest request1(msg);
298 dispatcherImpl->Dispatch(request1);
299 ASSERT_TRUE(result.find("GetHeapObjectId not support now") != std::string::npos);
300 if (channel) {
301 delete channel;
302 channel = nullptr;
303 }
304 }
305
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplGetObjectByHeapObjectId)306 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetObjectByHeapObjectId)
307 {
308 std::string result = "";
309 std::function<void(const void*, const std::string &)> callback =
310 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
311 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
312 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
313 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
314 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.getObjectByHeapObjectId","params":{
315 "objectId":001}})";
316 DispatchRequest request(msg);
317 dispatcherImpl->Dispatch(request);
318 ASSERT_TRUE(result.find("wrong params") != std::string::npos);
319 msg = std::string() + R"({"id":0,"method":"HeapProfiler.getObjectByHeapObjectId","params":{"objectId":"001",
320 "objectGroup":"000"}})";
321 DispatchRequest request1(msg);
322 dispatcherImpl->Dispatch(request1);
323 ASSERT_TRUE(result.find("GetObjectByHeapObjectId not support now") != std::string::npos);
324 if (channel) {
325 delete channel;
326 channel = nullptr;
327 }
328 }
329
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplGetSamplingProfile)330 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetSamplingProfile)
331 {
332 std::string result = "";
333 std::function<void(const void*, const std::string &)> callback =
334 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
335 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
336 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
337 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
338 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.getSamplingProfile","params":{}})";
339 DispatchRequest request(msg);
340 dispatcherImpl->GetSamplingProfile(request);
341 ASSERT_TRUE(result.find("GetSamplingProfile fail") != std::string::npos);
342 if (channel) {
343 delete channel;
344 channel = nullptr;
345 }
346 }
347
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplStartSampling)348 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStartSampling)
349 {
350 std::string result = "";
351 std::function<void(const void*, const std::string &)> callback =
352 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
353 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
354 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
355 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
356 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.startSampling","params":{
357 "samplingInterval":"Test"}})";
358 DispatchRequest request(msg);
359 dispatcherImpl->StartSampling(request);
360 ASSERT_TRUE(result.find("wrong params") != std::string::npos);
361 msg = std::string() + R"({"id":0,"method":"HeapProfiler.startSampling","params":{"samplingInterval":1000}})";
362 DispatchRequest request1(msg);
363 dispatcherImpl->StartSampling(request1);
364 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
365 if (channel) {
366 delete channel;
367 channel = nullptr;
368 }
369 }
370
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplStopSampling)371 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStopSampling)
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<HeapProfilerImpl>(ecmaVm, channel);
378 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
379 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.stopSampling","params":{}})";
380 DispatchRequest request(msg);
381 dispatcherImpl->StopSampling(request);
382 ASSERT_TRUE(result.find("StopSampling fail") != std::string::npos);
383 if (channel) {
384 delete channel;
385 channel = nullptr;
386 }
387 }
388
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplTakeHeapSnapshot)389 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplTakeHeapSnapshot)
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<HeapProfilerImpl>(ecmaVm, channel);
396 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
397 std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.takeHeapSnapshot","params":{
398 "reportProgress":10,
399 "treatGlobalObjectsAsRoots":10,
400 "captureNumericValue":10}})";
401 DispatchRequest request(msg);
402 dispatcherImpl->TakeHeapSnapshot(request);
403 ASSERT_TRUE(result.find("wrong params") != std::string::npos);
404 msg = std::string() + R"({"id":0,"method":"HeapProfiler.takeHeapSnapshot","params":{
405 "reportProgress":true,
406 "treatGlobalObjectsAsRoots":true,
407 "captureNumericValue":true}})";
408 DispatchRequest request1(msg);
409 dispatcherImpl->TakeHeapSnapshot(request1);
410 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
411 if (channel) {
412 delete channel;
413 channel = nullptr;
414 }
415 }
416
HWTEST_F_L0(HeapProfilerImplTest,GetSamplingProfileSuccessful)417 HWTEST_F_L0(HeapProfilerImplTest, GetSamplingProfileSuccessful)
418 {
419 StartSamplingParams params;
420 ProtocolChannel *channel = nullptr;
421 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
422 DispatchResponse response = heapProfiler->StartSampling(params);
423 std::unique_ptr<SamplingHeapProfile> samplingHeapProfile = std::make_unique<SamplingHeapProfile>();
424 DispatchResponse result = heapProfiler->GetSamplingProfile(&samplingHeapProfile);
425 ASSERT_TRUE(result.IsOk());
426 }
427
HWTEST_F_L0(HeapProfilerImplTest,StartSamplingFail)428 HWTEST_F_L0(HeapProfilerImplTest, StartSamplingFail)
429 {
430 StartSamplingParams params;
431 ProtocolChannel *channel = nullptr;
432 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
433 DispatchResponse response = heapProfiler->StartSampling(params);
434 DispatchResponse result = heapProfiler->StartSampling(params);
435 ASSERT_TRUE(!result.IsOk());
436 }
437
HWTEST_F_L0(HeapProfilerImplTest,StopSamplingSuccessful)438 HWTEST_F_L0(HeapProfilerImplTest, StopSamplingSuccessful)
439 {
440 StartSamplingParams params;
441 ProtocolChannel *channel = nullptr;
442 std::unique_ptr<SamplingHeapProfile> samplingHeapProfile = std::make_unique<SamplingHeapProfile>();
443 auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
444 DispatchResponse response = heapProfiler->StartSampling(params);
445 DispatchResponse result = heapProfiler->StopSampling(&samplingHeapProfile);
446 ASSERT_TRUE(result.IsOk());
447 }
448
HWTEST_F_L0(HeapProfilerImplTest,StartTrackingHeapObjects)449 HWTEST_F_L0(HeapProfilerImplTest, StartTrackingHeapObjects)
450 {
451 std::string result = "";
452 std::function<void(const void*, const std::string &)> callback =
453 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
454 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
455 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
456 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
457 std::string msg = "";
458 msg += R"({"id":0,"method":"HeapProfiler.StartTrackingHeapObjects","params":{"trackAllocations":0}})";
459 DispatchRequest request1(msg);
460 dispatcherImpl->StartTrackingHeapObjects(request1);
461 ASSERT_TRUE(result == "{\"id\":0,\"result\":{\"code\":1,\"message\":\"wrong params\"}}");
462 if (channel) {
463 delete channel;
464 channel = nullptr;
465 }
466 }
467
HWTEST_F_L0(HeapProfilerImplTest,StopTrackingHeapObjects)468 HWTEST_F_L0(HeapProfilerImplTest, StopTrackingHeapObjects)
469 {
470 std::string result = "";
471 std::function<void(const void*, const std::string &)> callback =
472 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
473 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
474 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
475 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
476 std::string msg = "";
477 msg += R"({"id":0,"method":"HeapProfiler.StopTrackingHeapObjects","params":{"reportProgress":0}})";
478 DispatchRequest request1(msg);
479 dispatcherImpl->StopTrackingHeapObjects(request1);
480 ASSERT_TRUE(result == "{\"id\":0,\"result\":{\"code\":1,\"message\":\"wrong params\"}}");
481 if (channel) {
482 delete channel;
483 channel = nullptr;
484 }
485 }
486
HWTEST_F_L0(HeapProfilerImplTest,RepeateStartTrackingHeapObjects)487 HWTEST_F_L0(HeapProfilerImplTest, RepeateStartTrackingHeapObjects)
488 {
489 std::string result = "";
490 std::function<void(const void*, const std::string &)> callback =
491 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
492 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
493 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
494 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
495 ecmaVm->SetLoop(uv_default_loop());
496 std::string msg = "";
497 msg += R"({"id":0,"method":"HeapProfiler.StartTrackingHeapObjects","params":{"trackAllocations":false}})";
498 DispatchRequest request1(msg);
499 dispatcherImpl->StartTrackingHeapObjects(request1);
500 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
501 std::string msg1 = "";
502 msg1 += R"({"id":0,"method":"HeapProfiler.StartTrackingHeapObjects","params":{"trackAllocations":false}})";
503 DispatchRequest request2(msg1);
504 dispatcherImpl->StartTrackingHeapObjects(request2);
505 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
506 if (channel) {
507 delete channel;
508 channel = nullptr;
509 }
510 }
511
HWTEST_F_L0(HeapProfilerImplTest,StartAndStopTrackingHeapObjects)512 HWTEST_F_L0(HeapProfilerImplTest, StartAndStopTrackingHeapObjects)
513 {
514 std::string result = "";
515 std::function<void(const void*, const std::string &)> callback =
516 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
517 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
518 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
519 auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
520 ecmaVm->SetLoop(uv_default_loop());
521 std::string msg = "";
522 msg += R"({"id":0,"method":"HeapProfiler.StartTrackingHeapObjects","params":{"trackAllocations":false}})";
523 DispatchRequest request1(msg);
524 dispatcherImpl->StartTrackingHeapObjects(request1);
525 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
526 std::string msg1 = "";
527 msg1 += R"({"id":0,"method":"HeapProfiler.StopTrackingHeapObjects","params":{"reportProgress":false}})";
528 DispatchRequest request2(msg1);
529 dispatcherImpl->StopTrackingHeapObjects(request2);
530 ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
531 if (channel) {
532 delete channel;
533 channel = nullptr;
534 }
535 }
536
HWTEST_F_L0(HeapProfilerImplTest,ResetProfiles)537 HWTEST_F_L0(HeapProfilerImplTest, ResetProfiles)
538 {
539 std::string result = "";
540 std::function<void(const void*, const std::string &)> callback =
541 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
542 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
543 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
544 auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
545 heapprofiler->ResetProfiles();
546 ASSERT_TRUE(result == "");
547 tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
548 heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
549 heapprofiler->ResetProfiles();
550 ASSERT_TRUE(result == "");
551 if (channel) {
552 delete channel;
553 channel = nullptr;
554 }
555 }
556
HWTEST_F_L0(HeapProfilerImplTest,LastSeenObjectId)557 HWTEST_F_L0(HeapProfilerImplTest, LastSeenObjectId)
558 {
559 std::string result = "";
560 std::function<void(const void*, const std::string &)> callback =
561 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
562 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
563 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
564 auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
565 heapprofiler->LastSeenObjectId(0, 0);
566 ASSERT_TRUE(result == "");
567 tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
568 heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
569 heapprofiler->LastSeenObjectId(0, 0);
570 std::string msg = "{\"method\":\"HeapProfiler.lastSeenObjectId\",";
571 msg += "\"params\":{\"lastSeenObjectId\":0,";
572 msg += "\"timestamp\":0}}";
573 ASSERT_TRUE(result == msg);
574 if (channel) {
575 delete channel;
576 channel = nullptr;
577 }
578 }
579
HWTEST_F_L0(HeapProfilerImplTest,HeapStatsUpdate)580 HWTEST_F_L0(HeapProfilerImplTest, HeapStatsUpdate)
581 {
582 std::string result = "";
583 std::function<void(const void*, const std::string &)> callback =
584 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
585 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
586 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
587 auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
588 heapprofiler->HeapStatsUpdate(nullptr, 0);
589 ASSERT_TRUE(result == "");
590 tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
591 heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
592 heapprofiler->HeapStatsUpdate(nullptr, 0);
593 std::string msg = "{\"method\":\"HeapProfiler.heapStatsUpdate\",";
594 msg += "\"params\":{\"statsUpdate\":[]}}";
595 ASSERT_TRUE(result == msg);
596 if (channel) {
597 delete channel;
598 channel = nullptr;
599 }
600 }
601
HWTEST_F_L0(HeapProfilerImplTest,AddHeapSnapshotChunk)602 HWTEST_F_L0(HeapProfilerImplTest, AddHeapSnapshotChunk)
603 {
604 std::string result = "";
605 std::function<void(const void*, const std::string &)> callback =
606 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
607 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
608 auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
609 heapprofiler->AddHeapSnapshotChunk(nullptr, 0);
610 ASSERT_TRUE(result == "");
611 }
612
HWTEST_F_L0(HeapProfilerImplTest,ReportHeapSnapshotProgress)613 HWTEST_F_L0(HeapProfilerImplTest, ReportHeapSnapshotProgress)
614 {
615 std::string result = "";
616 std::function<void(const void*, const std::string &)> callback =
617 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
618 auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
619 auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
620 heapprofiler->ReportHeapSnapshotProgress(0, 0);
621 ASSERT_TRUE(result == "");
622 }
623 } // namespace panda::test
624