• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 namespace panda::test {
24 class HeapProfilerImplTest : 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(HeapProfilerImplTest,AddInspectedHeapObject)52 HWTEST_F_L0(HeapProfilerImplTest, AddInspectedHeapObject)
53 {
54     ProtocolChannel *channel = nullptr;
55     AddInspectedHeapObjectParams param;
56     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
57     DispatchResponse response = heapProfiler->AddInspectedHeapObject(param);
58     ASSERT_TRUE(response.GetMessage() == "AddInspectedHeapObject not support now");
59     ASSERT_TRUE(!response.IsOk());
60 }
61 
HWTEST_F_L0(HeapProfilerImplTest,CollectGarbage)62 HWTEST_F_L0(HeapProfilerImplTest, CollectGarbage)
63 {
64     ProtocolChannel *channel = nullptr;
65     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
66     DispatchResponse response = heapProfiler->CollectGarbage();
67     ASSERT_TRUE(response.GetMessage() == "");
68     ASSERT_TRUE(response.IsOk());
69 }
70 
HWTEST_F_L0(HeapProfilerImplTest,Enable)71 HWTEST_F_L0(HeapProfilerImplTest, Enable)
72 {
73     ProtocolChannel *channel = nullptr;
74     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
75     DispatchResponse response = heapProfiler->Enable();
76     ASSERT_TRUE(response.GetMessage() == "");
77     ASSERT_TRUE(response.IsOk());
78 }
79 
HWTEST_F_L0(HeapProfilerImplTest,Disable)80 HWTEST_F_L0(HeapProfilerImplTest, Disable)
81 {
82     ProtocolChannel *channel = nullptr;
83     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
84     DispatchResponse response = heapProfiler->Disable();
85     ASSERT_TRUE(response.GetMessage() == "");
86     ASSERT_TRUE(response.IsOk());
87 }
88 
HWTEST_F_L0(HeapProfilerImplTest,GetHeapObjectId)89 HWTEST_F_L0(HeapProfilerImplTest, GetHeapObjectId)
90 {
91     ProtocolChannel *channel = nullptr;
92     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
93     GetHeapObjectIdParams params;
94     HeapSnapshotObjectId objectId;
95     DispatchResponse response = heapProfiler->GetHeapObjectId(params, &objectId);
96     ASSERT_TRUE(response.GetMessage() == "GetHeapObjectId not support now");
97     ASSERT_TRUE(!response.IsOk());
98 }
99 
HWTEST_F_L0(HeapProfilerImplTest,GetObjectByHeapObjectId)100 HWTEST_F_L0(HeapProfilerImplTest, GetObjectByHeapObjectId)
101 {
102     ProtocolChannel *channel = nullptr;
103     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
104     std::unique_ptr<GetObjectByHeapObjectIdParams> params;
105     std::unique_ptr<RemoteObject> remoteObjectResult;
106     DispatchResponse response = heapProfiler->GetObjectByHeapObjectId(*params, &remoteObjectResult);
107     ASSERT_TRUE(response.GetMessage() == "GetObjectByHeapObjectId not support now");
108     ASSERT_TRUE(!response.IsOk());
109 }
110 
HWTEST_F_L0(HeapProfilerImplTest,GetSamplingProfile)111 HWTEST_F_L0(HeapProfilerImplTest, GetSamplingProfile)
112 {
113     ProtocolChannel *channel = nullptr;
114     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
115     std::unique_ptr<SamplingHeapProfile> profile;
116     DispatchResponse response = heapProfiler->GetSamplingProfile(&profile);
117     ASSERT_TRUE(response.GetMessage() == "GetSamplingProfile not support now");
118     ASSERT_TRUE(!response.IsOk());
119 }
120 
HWTEST_F_L0(HeapProfilerImplTest,StartSampling)121 HWTEST_F_L0(HeapProfilerImplTest, StartSampling)
122 {
123     ProtocolChannel *channel = nullptr;
124     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
125     StartSamplingParams params;
126     DispatchResponse response = heapProfiler->StartSampling(params);
127     ASSERT_TRUE(response.GetMessage() == "StartSampling not support now");
128     ASSERT_TRUE(!response.IsOk());
129 }
130 
HWTEST_F_L0(HeapProfilerImplTest,StopSampling)131 HWTEST_F_L0(HeapProfilerImplTest, StopSampling)
132 {
133     ProtocolChannel *channel = nullptr;
134     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
135     std::unique_ptr<SamplingHeapProfile> profile;
136     DispatchResponse response = heapProfiler->StopSampling(&profile);
137     ASSERT_TRUE(response.GetMessage() == "StopSampling not support now.");
138     ASSERT_TRUE(!response.IsOk());
139 }
140 
HWTEST_F_L0(HeapProfilerImplTest,TakeHeapSnapshot)141 HWTEST_F_L0(HeapProfilerImplTest, TakeHeapSnapshot)
142 {
143     ProtocolChannel *channel = nullptr;
144     auto heapProfiler = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
145     StopTrackingHeapObjectsParams params;
146     DispatchResponse response = heapProfiler->TakeHeapSnapshot(params);
147     ASSERT_TRUE(response.GetMessage() == "");
148     ASSERT_TRUE(response.IsOk());
149 }
150 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplDispatch)151 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplDispatch)
152 {
153     std::string result = "";
154     std::function<void(const void*, const std::string &)> callback =
155         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
156     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
157     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
158     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
159     std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
160     DispatchRequest request(msg);
161     dispatcherImpl->Dispatch(request);
162     ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
163     msg = std::string() + R"({"id":0,"method":"Debugger.disable","params":{}})";
164     DispatchRequest request1 = DispatchRequest(msg);
165     dispatcherImpl->Dispatch(request1);
166     if (channel) {
167         delete channel;
168         channel = nullptr;
169     }
170     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
171 }
172 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplAddInspectedHeapObject)173 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplAddInspectedHeapObject)
174 {
175     std::string result = "";
176     std::function<void(const void*, const std::string &)> callback =
177         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
178     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
179     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
180     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
181     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.disable","params":{}})";
182     DispatchRequest request(msg);
183     dispatcherImpl->AddInspectedHeapObject(request);
184     ASSERT_TRUE(result.find("wrong params") != std::string::npos);
185     msg = std::string() + R"({"id":0,"method":"HeapProfiler.disable","params":{"heapObjectId":"0"}})";
186     DispatchRequest request1 = DispatchRequest(msg);
187     dispatcherImpl->AddInspectedHeapObject(request1);
188     if (channel) {
189         delete channel;
190         channel = nullptr;
191     }
192     ASSERT_TRUE(result.find("AddInspectedHeapObject not support now") != std::string::npos);
193 }
194 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplCollectGarbage)195 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplCollectGarbage)
196 {
197     std::string result = "";
198     std::function<void(const void*, const std::string &)> callback =
199         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
200     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
201     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
202     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
203     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.collectGarbage","params":{}})";
204     DispatchRequest request(msg);
205     dispatcherImpl->CollectGarbage(request);
206     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
207     if (channel) {
208         delete channel;
209         channel = nullptr;
210     }
211 }
212 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplEnable)213 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplEnable)
214 {
215     std::string result = "";
216     std::function<void(const void*, const std::string &)> callback =
217         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
218     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
219     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
220     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
221     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.enable","params":{}})";
222     DispatchRequest request(msg);
223     dispatcherImpl->Enable(request);
224     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
225     if (channel) {
226         delete channel;
227         channel = nullptr;
228     }
229 }
230 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplDisable)231 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplDisable)
232 {
233     std::string result = "";
234     std::function<void(const void*, const std::string &)> callback =
235         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
236     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
237     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
238     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
239     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.disable","params":{}})";
240     DispatchRequest request(msg);
241     dispatcherImpl->Disable(request);
242     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
243     if (channel) {
244         delete channel;
245         channel = nullptr;
246     }
247 }
248 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplGetHeapObjectId)249 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetHeapObjectId)
250 {
251     std::string result = "";
252     std::function<void(const void*, const std::string &)> callback =
253         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
254     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
255     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
256     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
257     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.getHeapObjectId","params":{"objectId":true}})";
258     DispatchRequest request(msg);
259     dispatcherImpl->GetHeapObjectId(request);
260     ASSERT_TRUE(result.find("wrong params") != std::string::npos);
261     msg = std::string() + R"({"id":0,"method":"HeapProfiler.getHeapObjectId","params":{"objectId":"0"}})";
262     DispatchRequest request1(msg);
263     dispatcherImpl->GetHeapObjectId(request1);
264     ASSERT_TRUE(result.find("GetHeapObjectId not support now") != std::string::npos);
265     if (channel) {
266         delete channel;
267         channel = nullptr;
268     }
269 }
270 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplGetObjectByHeapObjectId)271 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetObjectByHeapObjectId)
272 {
273     std::string result = "";
274     std::function<void(const void*, const std::string &)> callback =
275         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
276     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
277     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
278     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
279     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.getObjectByHeapObjectId","params":{
280         "objectId":001}})";
281     DispatchRequest request(msg);
282     dispatcherImpl->GetObjectByHeapObjectId(request);
283     ASSERT_TRUE(result.find("wrong params") != std::string::npos);
284     msg = std::string() + R"({"id":0,"method":"HeapProfiler.getObjectByHeapObjectId","params":{"objectId":"001",
285         "objectGroup":"000"}})";
286     DispatchRequest request1(msg);
287     dispatcherImpl->GetObjectByHeapObjectId(request1);
288     ASSERT_TRUE(result.find("GetObjectByHeapObjectId not support now") != std::string::npos);
289     if (channel) {
290         delete channel;
291         channel = nullptr;
292     }
293 }
294 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplGetSamplingProfile)295 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplGetSamplingProfile)
296 {
297     std::string result = "";
298     std::function<void(const void*, const std::string &)> callback =
299         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
300     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
301     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
302     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
303     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.getSamplingProfile","params":{}})";
304     DispatchRequest request(msg);
305     dispatcherImpl->GetSamplingProfile(request);
306     ASSERT_TRUE(result.find("GetSamplingProfile not support now") != std::string::npos);
307     if (channel) {
308         delete channel;
309         channel = nullptr;
310     }
311 }
312 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplStartSampling)313 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStartSampling)
314 {
315     std::string result = "";
316     std::function<void(const void*, const std::string &)> callback =
317         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
318     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
319     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
320     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
321     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.startSampling","params":{
322         "samplingInterval":"Test"}})";
323     DispatchRequest request(msg);
324     dispatcherImpl->StartSampling(request);
325     ASSERT_TRUE(result.find("wrong params") != std::string::npos);
326     msg = std::string() + R"({"id":0,"method":"HeapProfiler.startSampling","params":{"samplingInterval":1000}})";
327     DispatchRequest request1(msg);
328     dispatcherImpl->StartSampling(request1);
329     ASSERT_TRUE(result.find("StartSampling not support now") != std::string::npos);
330     if (channel) {
331         delete channel;
332         channel = nullptr;
333     }
334 }
335 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplStopSampling)336 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplStopSampling)
337 {
338     std::string result = "";
339     std::function<void(const void*, const std::string &)> callback =
340         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
341     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
342     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
343     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
344     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.stopSampling","params":{}})";
345     DispatchRequest request(msg);
346     dispatcherImpl->StopSampling(request);
347     ASSERT_TRUE(result.find("StopSampling not support now") != std::string::npos);
348     if (channel) {
349         delete channel;
350         channel = nullptr;
351     }
352 }
353 
HWTEST_F_L0(HeapProfilerImplTest,DispatcherImplTakeHeapSnapshot)354 HWTEST_F_L0(HeapProfilerImplTest, DispatcherImplTakeHeapSnapshot)
355 {
356     std::string result = "";
357     std::function<void(const void*, const std::string &)> callback =
358         [&result]([[maybe_unused]]const void* ptr, const std::string &temp) {result = temp;};
359     ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
360     auto tracing = std::make_unique<HeapProfilerImpl>(ecmaVm, channel);
361     auto dispatcherImpl = std::make_unique<HeapProfilerImpl::DispatcherImpl>(channel, std::move(tracing));
362     std::string msg = std::string() + R"({"id":0,"method":"HeapProfiler.takeHeapSnapshot","params":{
363         "reportProgress":10,
364         "treatGlobalObjectsAsRoots":10,
365         "captureNumericValue":10}})";
366     DispatchRequest request(msg);
367     dispatcherImpl->TakeHeapSnapshot(request);
368     ASSERT_TRUE(result.find("wrong params") != std::string::npos);
369     msg = std::string() + R"({"id":0,"method":"HeapProfiler.takeHeapSnapshot","params":{
370         "reportProgress":true,
371         "treatGlobalObjectsAsRoots":true,
372         "captureNumericValue":true}})";
373     DispatchRequest request1(msg);
374     dispatcherImpl->TakeHeapSnapshot(request1);
375     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
376     if (channel) {
377         delete channel;
378         channel = nullptr;
379     }
380 }
381 }  // namespace panda::test
382