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 
72     void SetUp() override
73     {
74         TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
75     }
76 
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, ResetProfiles)487 HWTEST_F_L0(HeapProfilerImplTest, ResetProfiles)
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, nullptr);
494     auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
495     heapprofiler->ResetProfiles();
496     ASSERT_TRUE(result == "");
497     tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
498     heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
499     heapprofiler->ResetProfiles();
500     ASSERT_TRUE(result == "");
501     if (channel) {
502         delete channel;
503         channel = nullptr;
504     }
505 }
506 
HWTEST_F_L0(HeapProfilerImplTest, LastSeenObjectId)507 HWTEST_F_L0(HeapProfilerImplTest, LastSeenObjectId)
508 {
509     std::string result = "";
510     std::function<void(const void*, const std::string &)> callback =
511         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
512     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
513     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
514     auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
515     heapprofiler->LastSeenObjectId(0, 0);
516     ASSERT_TRUE(result == "");
517     tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
518     heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
519     heapprofiler->LastSeenObjectId(0, 0);
520     std::string msg = "{\"method\":\"HeapProfiler.lastSeenObjectId\",";
521     msg += "\"params\":{\"lastSeenObjectId\":0,";
522     msg += "\"timestamp\":0}}";
523     ASSERT_TRUE(result == msg);
524     if (channel) {
525         delete channel;
526         channel = nullptr;
527     }
528 }
529 
HWTEST_F_L0(HeapProfilerImplTest, HeapStatsUpdate)530 HWTEST_F_L0(HeapProfilerImplTest, HeapStatsUpdate)
531 {
532     std::string result = "";
533     std::function<void(const void*, const std::string &)> callback =
534         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
535     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
536     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
537     auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
538     heapprofiler->HeapStatsUpdate(nullptr, 0);
539     ASSERT_TRUE(result == "");
540     tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
541     heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
542     heapprofiler->HeapStatsUpdate(nullptr, 0);
543     std::string msg = "{\"method\":\"HeapProfiler.heapStatsUpdate\",";
544     msg += "\"params\":{\"statsUpdate\":[]}}";
545     ASSERT_TRUE(result == msg);
546     if (channel) {
547         delete channel;
548         channel = nullptr;
549     }
550 }
551 
HWTEST_F_L0(HeapProfilerImplTest, AddHeapSnapshotChunk)552 HWTEST_F_L0(HeapProfilerImplTest, AddHeapSnapshotChunk)
553 {
554     std::string result = "";
555     std::function<void(const void*, const std::string &)> callback =
556         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
557     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
558     auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
559     heapprofiler->AddHeapSnapshotChunk(nullptr, 0);
560     ASSERT_TRUE(result == "");
561 }
562 
HWTEST_F_L0(HeapProfilerImplTest, ReportHeapSnapshotProgress)563 HWTEST_F_L0(HeapProfilerImplTest, ReportHeapSnapshotProgress)
564 {
565     std::string result = "";
566     std::function<void(const void*, const std::string &)> callback =
567         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) {result = temp;};
568     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, nullptr);
569     auto heapprofiler = std::make_unique<HeapProfilerImplFriendTest>(tracing);
570     heapprofiler->ReportHeapSnapshotProgress(0, 0);
571     ASSERT_TRUE(result == "");
572 }
573 }  // namespace panda::test
574