• 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/debugger_impl.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "protocol_channel.h"
19 #include "protocol_handler.h"
20 
21 using namespace panda::ecmascript;
22 using namespace panda::ecmascript::tooling;
23 
24 namespace panda::test {
25 class DebuggerImplTest : public testing::Test {
26 public:
SetUpTestCase()27     static void SetUpTestCase()
28     {
29         GTEST_LOG_(INFO) << "SetUpTestCase";
30     }
31 
TearDownTestCase()32     static void TearDownTestCase()
33     {
34         GTEST_LOG_(INFO) << "TearDownCase";
35     }
36 
SetUp()37     void SetUp() override
38     {
39         TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
40     }
41 
TearDown()42     void TearDown() override
43     {
44         TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
45     }
46 
47 protected:
48     EcmaVM *ecmaVm {nullptr};
49     EcmaHandleScope *scope {nullptr};
50     JSThread *thread {nullptr};
51 };
52 
HWTEST_F_L0(DebuggerImplTest,NotifyScriptParsed__001)53 HWTEST_F_L0(DebuggerImplTest, NotifyScriptParsed__001)
54 {
55     std::string outStrForCallbackCheck = "";
56     std::function<void(const void*, const std::string &)> callback =
57         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
58             outStrForCallbackCheck = inStrOfReply;};
59     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
60     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
61     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
62 
63     // DebuggerImpl::NotifyScriptParsed -- fileName.substr(0, DATA_APP_PATH.length()) != DATA_APP_PATH
64     std::string strFilename = "filename";
65     EXPECT_FALSE(debuggerImpl->NotifyScriptParsed(0, strFilename, ""));
66 
67     // DebuggerImpl::NotifyScriptParsed -- fileName.substr(0, DATA_APP_PATH.length()) != DATA_APP_PATH
68     strFilename = "/filename";
69     EXPECT_FALSE(debuggerImpl->NotifyScriptParsed(0, strFilename, ""));
70 
71     if (protocolChannel) {
72         delete protocolChannel;
73         protocolChannel = nullptr;
74     }
75 }
76 
HWTEST_F_L0(DebuggerImplTest,GenerateCallFrames__001)77 HWTEST_F_L0(DebuggerImplTest, GenerateCallFrames__001)
78 {
79 }
80 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_Enable__001)81 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_Enable__001)
82 {
83     std::string outStrForCallbackCheck = "";
84     std::function<void(const void*, const std::string &)> callback =
85         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
86             outStrForCallbackCheck = inStrOfReply;};
87     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
88     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
89     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
90     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
91 
92     // DebuggerImpl::DispatcherImpl::Enable -- params == nullptr
93     std::string msg = std::string() +
94         R"({
95             "id":0,
96             "method":"Debugger.enable",
97             "params":{
98                 "maxScriptsCacheSize":"NotIntValue"
99             }
100         })";
101     DispatchRequest request(msg);
102     dispatcherImpl->Dispatch(request);
103 
104     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
105     if (protocolChannel) {
106         delete protocolChannel;
107         protocolChannel = nullptr;
108     }
109 }
110 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_Enable__002)111 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_Enable__002)
112 {
113     std::string outStrForCallbackCheck = "";
114     std::function<void(const void*, const std::string &)> callback =
115         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
116             outStrForCallbackCheck = inStrOfReply;};
117     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
118     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
119     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
120     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
121     EXPECT_FALSE(ecmaVm->GetJsDebuggerManager()->IsDebugMode());
122 
123     std::string msg = std::string() +
124         R"({
125             "id":0,
126             "method":"Debugger.enable",
127             "params":{
128                 "maxScriptsCacheSize":1024
129             }
130         })";
131     DispatchRequest request(msg);
132     dispatcherImpl->Dispatch(request);
133 
134     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"debuggerId":"0"}})");
135     EXPECT_TRUE(ecmaVm->GetJsDebuggerManager()->IsDebugMode());
136     if (protocolChannel) {
137         delete protocolChannel;
138         protocolChannel = nullptr;
139     }
140 }
141 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_Disable__001)142 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_Disable__001)
143 {
144     std::string outStrForCallbackCheck = "";
145     std::function<void(const void*, const std::string &)> callback =
146         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
147             outStrForCallbackCheck = inStrOfReply;};
148     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
149     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
150     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
151     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
152     ecmaVm->GetJsDebuggerManager()->SetDebugMode(true);
153 
154     std::string msg = std::string() +
155         R"({
156             "id":0,
157             "method":"Debugger.disable"
158         })";
159     DispatchRequest request(msg);
160 
161     dispatcherImpl->Dispatch(request);
162     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
163     EXPECT_FALSE(ecmaVm->GetJsDebuggerManager()->IsDebugMode());
164     if (protocolChannel) {
165         delete protocolChannel;
166         protocolChannel = nullptr;
167     }
168 }
169 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_EvaluateOnCallFrame__001)170 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_EvaluateOnCallFrame__001)
171 {
172     std::string outStrForCallbackCheck = "";
173     std::function<void(const void*, const std::string &)> callback =
174         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
175             outStrForCallbackCheck = inStrOfReply;};
176     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
177     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
178     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
179     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
180 
181     // DebuggerImpl::DispatcherImpl::EvaluateOnCallFrame -- params == nullptr
182     std::string msg = std::string() +
183         R"({
184             "id":0,
185             "method":"Debugger.evaluateOnCallFrame",
186             "params":{}
187         })";
188     DispatchRequest request(msg);
189 
190     dispatcherImpl->Dispatch(request);
191     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
192     if (protocolChannel) {
193         delete protocolChannel;
194         protocolChannel = nullptr;
195     }
196 }
197 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_EvaluateOnCallFrame__002)198 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_EvaluateOnCallFrame__002)
199 {
200     std::string outStrForCallbackCheck = "";
201     std::function<void(const void*, const std::string &)> callback =
202         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
203             outStrForCallbackCheck = inStrOfReply;};
204     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
205     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
206     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
207     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
208 
209     // DebuggerImpl::EvaluateOnCallFrame -- callFrameId < 0
210     std::string msg = std::string() +
211         R"({
212             "id":0,
213             "method":"Debugger.evaluateOnCallFrame",
214             "params":{
215                 "callFrameId":"-1",
216                 "expression":"the expression"
217             }
218         })";
219     DispatchRequest request(msg);
220 
221     dispatcherImpl->Dispatch(request);
222     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"Invalid callFrameId."}})");
223     if (protocolChannel) {
224         delete protocolChannel;
225         protocolChannel = nullptr;
226     }
227 }
228 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_EvaluateOnCallFrame__003)229 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_EvaluateOnCallFrame__003)
230 {
231     std::string outStrForCallbackCheck = "";
232     std::function<void(const void*, const std::string &)> callback =
233         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
234             outStrForCallbackCheck = inStrOfReply;};
235     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
236     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
237     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
238     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
239 
240     // DebuggerImpl::EvaluateOnCallFrame -- callFrameId >= static_cast<CallFrameId>(callFrameHandlers_.size())
241     std::string msg = std::string() +
242         R"({
243             "id":0,
244             "method":"Debugger.evaluateOnCallFrame",
245             "params":{
246                 "callFrameId":"0",
247                 "expression":"the expression"
248             }
249         })";
250     DispatchRequest request(msg);
251 
252     dispatcherImpl->Dispatch(request);
253     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"Invalid callFrameId."}})");
254     if (protocolChannel) {
255         delete protocolChannel;
256         protocolChannel = nullptr;
257     }
258 }
259 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_GetPossibleBreakpoints__001)260 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_GetPossibleBreakpoints__001)
261 {
262     std::string outStrForCallbackCheck = "";
263     std::function<void(const void*, const std::string &)> callback =
264         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
265             outStrForCallbackCheck = inStrOfReply;};
266     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
267     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
268     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
269     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
270 
271     // DebuggerImpl::DispatcherImpl::GetPossibleBreakpoints -- params == nullptr
272     std::string msg = std::string() +
273         R"({
274             "id":0,
275             "method":"Debugger.getPossibleBreakpoints",
276             "params":{}
277         })";
278     DispatchRequest request(msg);
279 
280     dispatcherImpl->Dispatch(request);
281     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
282     if (protocolChannel) {
283         delete protocolChannel;
284         protocolChannel = nullptr;
285     }
286 }
287 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_GetPossibleBreakpoints__002)288 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_GetPossibleBreakpoints__002)
289 {
290     std::string outStrForCallbackCheck = "";
291     std::function<void(const void*, const std::string &)> callback =
292         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
293             outStrForCallbackCheck = inStrOfReply;};
294     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
295     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
296     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
297     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
298 
299     // DebuggerImpl::GetPossibleBreakpoints -- iter == scripts_.end()
300     std::string msg = std::string() +
301         R"({
302             "id":0,
303             "method":"Debugger.getPossibleBreakpoints",
304             "params":{
305                 "start":{
306                     "scriptId":"0",
307                     "lineNumber":1,
308                     "columnNumber":0
309                 },
310                 "end":{
311                     "scriptId":"0",
312                     "lineNumber":1,
313                     "columnNumber":0
314                 },
315                 "restrictToFunction":true
316             }
317         })";
318     DispatchRequest request(msg);
319 
320     dispatcherImpl->Dispatch(request);
321     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"Unknown file name."}})");
322     if (protocolChannel) {
323         delete protocolChannel;
324         protocolChannel = nullptr;
325     }
326 }
327 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_GetScriptSource__001)328 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_GetScriptSource__001)
329 {
330     std::string outStrForCallbackCheck = "";
331     std::function<void(const void*, const std::string &)> callback =
332         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
333             outStrForCallbackCheck = inStrOfReply;};
334     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
335     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
336     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
337     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
338 
339     // DebuggerImpl::DispatcherImpl::GetScriptSource -- params == nullptr
340     std::string msg = std::string() +
341         R"({
342             "id":0,
343             "method":"Debugger.getScriptSource",
344             "params":{}
345         })";
346     DispatchRequest request(msg);
347 
348     dispatcherImpl->Dispatch(request);
349     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
350     if (protocolChannel) {
351         delete protocolChannel;
352         protocolChannel = nullptr;
353     }
354 }
355 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_GetScriptSource__002)356 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_GetScriptSource__002)
357 {
358     std::string outStrForCallbackCheck = "";
359     std::function<void(const void*, const std::string &)> callback =
360         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
361             outStrForCallbackCheck = inStrOfReply;};
362     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
363     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
364     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
365     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
366 
367     // DebuggerImpl::GetScriptSource -- iter == scripts_.end()
368     std::string msg = std::string() +
369         R"({
370             "id":0,
371             "method":"Debugger.getScriptSource",
372             "params":{
373                 "scriptId":"0"
374             }
375         })";
376     DispatchRequest request(msg);
377 
378     dispatcherImpl->Dispatch(request);
379     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"unknown script id: 0"}})");
380     if (protocolChannel) {
381         delete protocolChannel;
382         protocolChannel = nullptr;
383     }
384 }
385 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_Pause__001)386 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_Pause__001)
387 {
388     std::string outStrForCallbackCheck = "";
389     std::function<void(const void*, const std::string &)> callback =
390         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
391             outStrForCallbackCheck = inStrOfReply;};
392     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
393     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
394     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
395     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
396 
397     // DDebuggerImpl::DispatcherImpl::GetScriptSource -- params == nullptr
398     std::string msg = std::string() +
399         R"({
400             "id":0,
401             "method":"Debugger.pause"
402         })";
403     DispatchRequest request(msg);
404 
405     dispatcherImpl->Dispatch(request);
406     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
407     if (protocolChannel) {
408         delete protocolChannel;
409         protocolChannel = nullptr;
410     }
411 }
412 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_RemoveBreakpoint__001)413 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_RemoveBreakpoint__001)
414 {
415     std::string outStrForCallbackCheck = "";
416     std::function<void(const void*, const std::string &)> callback =
417         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
418             outStrForCallbackCheck = inStrOfReply;};
419     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
420     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
421     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
422     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
423 
424     // DDebuggerImpl::DispatcherImpl::RemoveBreakpoint -- params == nullptr
425     std::string msg = std::string() +
426         R"({
427             "id":0,
428             "method":"Debugger.removeBreakpoint",
429             "params":{}
430         })";
431     DispatchRequest request(msg);
432 
433     dispatcherImpl->Dispatch(request);
434     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
435     if (protocolChannel) {
436         delete protocolChannel;
437         protocolChannel = nullptr;
438     }
439 }
440 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_RemoveBreakpoint__002)441 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_RemoveBreakpoint__002)
442 {
443     std::string outStrForCallbackCheck = "";
444     std::function<void(const void*, const std::string &)> callback =
445         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
446             outStrForCallbackCheck = inStrOfReply;};
447     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
448     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
449     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
450     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
451 
452     // DDebuggerImpl::RemoveBreakpoint -- !BreakpointDetails::ParseBreakpointId(id, &metaData)
453     std::string msg = std::string() +
454         R"({
455             "id":0,
456             "method":"Debugger.removeBreakpoint",
457             "params":{
458                 "breakpointId":"id:0:0"
459             }
460         })";
461     DispatchRequest request(msg);
462 
463     dispatcherImpl->Dispatch(request);
464     EXPECT_STREQ(outStrForCallbackCheck.c_str(),
465         R"({"id":0,"result":{"code":1,"message":"Parse breakpoint id failed"}})");
466     if (protocolChannel) {
467         delete protocolChannel;
468         protocolChannel = nullptr;
469     }
470 }
471 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_RemoveBreakpoint__003)472 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_RemoveBreakpoint__003)
473 {
474     std::string outStrForCallbackCheck = "";
475     std::function<void(const void*, const std::string &)> callback =
476         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
477             outStrForCallbackCheck = inStrOfReply;};
478     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
479     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
480     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
481     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
482 
483     // DDebuggerImpl::RemoveBreakpoint -- !MatchScripts(scriptFunc, metaData.url_, ScriptMatchType::URL)
484     std::string msg = std::string() +
485         R"({
486             "id":0,
487             "method":"Debugger.removeBreakpoint",
488             "params":{
489                 "breakpointId":"id:0:0:url"
490             }
491         })";
492     DispatchRequest request(msg);
493 
494     dispatcherImpl->Dispatch(request);
495     EXPECT_STREQ(outStrForCallbackCheck.c_str(),
496         R"({"id":0,"result":{"code":1,"message":"Unknown file name."}})");
497     if (protocolChannel) {
498         delete protocolChannel;
499         protocolChannel = nullptr;
500     }
501 }
502 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_Resume__001)503 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_Resume__001)
504 {
505     std::string outStrForCallbackCheck = "";
506     std::function<void(const void*, const std::string &)> callback =
507         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
508             outStrForCallbackCheck = inStrOfReply;};
509     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
510     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
511     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
512     debuggerImpl->SetDebuggerState(DebuggerState::PAUSED);
513     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
514 
515     // DebuggerImpl::DispatcherImpl::Resume -- params == nullptr
516     std::string msg = std::string() +
517         R"({
518             "id":0,
519             "method":"Debugger.resume",
520             "params":{
521                 "terminateOnResume":"NotBool"
522             }
523         })";
524     DispatchRequest request(msg);
525 
526     dispatcherImpl->Dispatch(request);
527     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
528     if (protocolChannel) {
529         delete protocolChannel;
530         protocolChannel = nullptr;
531     }
532 }
533 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_Resume__002)534 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_Resume__002)
535 {
536     std::string outStrForCallbackCheck = "";
537     std::function<void(const void*, const std::string &)> callback =
538         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
539             outStrForCallbackCheck = inStrOfReply;};
540     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
541     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
542     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
543     debuggerImpl->SetDebuggerState(DebuggerState::PAUSED);
544     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
545 
546     std::string msg = std::string() +
547         R"({
548             "id":0,
549             "method":"Debugger.resume",
550             "params":{
551                 "terminateOnResume":true
552             }
553         })";
554     DispatchRequest request(msg);
555 
556     dispatcherImpl->Dispatch(request);
557     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
558     if (protocolChannel) {
559         delete protocolChannel;
560         protocolChannel = nullptr;
561     }
562 }
563 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetAsyncCallStackDepth__001)564 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetAsyncCallStackDepth__001)
565 {
566     std::string outStrForCallbackCheck = "";
567     std::function<void(const void*, const std::string &)> callback =
568         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
569             outStrForCallbackCheck = inStrOfReply;};
570     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
571     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
572     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
573     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
574 
575     std::string msg = std::string() +
576         R"({
577             "id":0,
578             "method":"Debugger.setAsyncCallStackDepth"
579         })";
580     DispatchRequest request(msg);
581 
582     dispatcherImpl->Dispatch(request);
583     EXPECT_STREQ(outStrForCallbackCheck.c_str(),
584         R"({"id":0,"result":{"code":1,"message":"SetAsyncCallStackDepth not support now"}})");
585     if (protocolChannel) {
586         delete protocolChannel;
587         protocolChannel = nullptr;
588     }
589 }
590 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetBreakpointByUrl__001)591 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetBreakpointByUrl__001)
592 {
593     std::string outStrForCallbackCheck = "";
594     std::function<void(const void*, const std::string &)> callback =
595         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
596             outStrForCallbackCheck = inStrOfReply;};
597     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
598     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
599     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
600     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
601 
602     // DebuggerImpl::DispatcherImpl::SetBreakpointByUrl -- params == nullptr
603     std::string msg = std::string() +
604         R"({
605             "id":0,
606             "method":"Debugger.setBreakpointByUrl",
607             "params":{}
608         })";
609     DispatchRequest request(msg);
610 
611     dispatcherImpl->Dispatch(request);
612     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
613     if (protocolChannel) {
614         delete protocolChannel;
615         protocolChannel = nullptr;
616     }
617 }
618 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetBreakpointByUrl__002)619 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetBreakpointByUrl__002)
620 {
621     std::string outStrForCallbackCheck = "";
622     std::function<void(const void*, const std::string &)> callback =
623         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
624             outStrForCallbackCheck = inStrOfReply;};
625     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
626     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
627     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
628     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
629     ecmaVm->GetJsDebuggerManager()->SetDebugMode(true);
630 
631     // DebuggerImpl::SetBreakpointByUrl -- extractor == nullptr
632     std::string msg = std::string() +
633         R"({
634             "id":0,
635             "method":"Debugger.setBreakpointByUrl",
636             "params":{
637                 "lineNumber":0,
638                 "url":"url_str",
639                 "urlRegex":"urlRegex_str",
640                 "scriptHash":"scriptHash_str",
641                 "columnNumber":0,
642                 "condition":"condition_str"
643             }
644         })";
645     DispatchRequest request(msg);
646 
647     dispatcherImpl->Dispatch(request);
648     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"Unknown file name."}})");
649     if (protocolChannel) {
650         delete protocolChannel;
651         protocolChannel = nullptr;
652     }
653 }
654 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetPauseOnExceptions__001)655 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetPauseOnExceptions__001)
656 {
657     std::string outStrForCallbackCheck = "";
658     std::function<void(const void*, const std::string &)> callback =
659         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
660             outStrForCallbackCheck = inStrOfReply;};
661     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
662     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
663     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
664     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
665 
666     // DebuggerImpl::DispatcherImpl::SetPauseOnExceptions -- params == nullptr
667     std::string msg = std::string() +
668         R"({
669             "id":0,
670             "method":"Debugger.setPauseOnExceptions",
671             "params":{}
672         })";
673     DispatchRequest request(msg);
674 
675     dispatcherImpl->Dispatch(request);
676     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
677     if (protocolChannel) {
678         delete protocolChannel;
679         protocolChannel = nullptr;
680     }
681 }
682 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetPauseOnExceptions__002)683 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetPauseOnExceptions__002)
684 {
685     std::string outStrForCallbackCheck = "";
686     std::function<void(const void*, const std::string &)> callback =
687         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
688             outStrForCallbackCheck = inStrOfReply;};
689     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
690     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
691     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
692     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
693 
694     std::string msg = std::string() +
695         R"({
696             "id":0,
697             "method":"Debugger.setPauseOnExceptions",
698             "params":{
699                 "state":"all"
700             }
701         })";
702     DispatchRequest request(msg);
703 
704     dispatcherImpl->Dispatch(request);
705     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
706     if (protocolChannel) {
707         delete protocolChannel;
708         protocolChannel = nullptr;
709     }
710 }
711 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_StepInto__001)712 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_StepInto__001)
713 {
714     std::string outStrForCallbackCheck = "";
715     std::function<void(const void*, const std::string &)> callback =
716         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
717             outStrForCallbackCheck = inStrOfReply;};
718     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
719     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
720     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
721     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
722 
723     // DebuggerImpl::DispatcherImpl::StepInto -- params == nullptr
724     std::string msg = std::string() +
725         R"({
726             "id":0,
727             "method":"Debugger.stepInto",
728             "params":{
729                 "breakOnAsyncCall":"Str"
730             }
731         })";
732     DispatchRequest request(msg);
733 
734     dispatcherImpl->Dispatch(request);
735     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
736     if (protocolChannel) {
737         delete protocolChannel;
738         protocolChannel = nullptr;
739     }
740 }
741 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetMixedDebugEnabled__001)742 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetMixedDebugEnabled__001)
743 {
744     std::string outStrForCallbackCheck = "";
745     std::function<void(const void*, const std::string &)> callback =
746         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
747             outStrForCallbackCheck = inStrOfReply;};
748     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
749     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
750     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
751     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
752 
753     // DebuggerImpl::DispatcherImpl::SetMixedDebugEnabled -- Params == nullptr
754     std::string msg = std::string() +
755         R"({
756             "id":0,
757             "method":"Debugger.setMixedDebugEnabled",
758             "params":{
759                 "enabled":"NotBoolValue"
760             }
761         })";
762     DispatchRequest request(msg);
763 
764     dispatcherImpl->Dispatch(request);
765     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
766     if (protocolChannel) {
767         delete protocolChannel;
768         protocolChannel = nullptr;
769     }
770 }
771 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetMixedDebugEnabled__002)772 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetMixedDebugEnabled__002)
773 {
774     std::string outStrForCallbackCheck = "";
775     std::function<void(const void*, const std::string &)> callback =
776         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
777             outStrForCallbackCheck = inStrOfReply;};
778     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
779     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
780     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
781     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
782     EXPECT_FALSE(ecmaVm->GetJsDebuggerManager()->IsMixedDebugEnabled());
783 
784     std::string msg = std::string() +
785         R"({
786             "id":0,
787             "method":"Debugger.setMixedDebugEnabled",
788             "params":{
789                 "enabled":true
790             }
791         })";
792     DispatchRequest request(msg);
793 
794     dispatcherImpl->Dispatch(request);
795     EXPECT_TRUE(ecmaVm->GetJsDebuggerManager()->IsMixedDebugEnabled());
796     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
797     if (protocolChannel) {
798         delete protocolChannel;
799         protocolChannel = nullptr;
800     }
801 }
802 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_SetBlackboxPatterns__001)803 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_SetBlackboxPatterns__001)
804 {
805     std::string outStrForCallbackCheck = "";
806     std::function<void(const void*, const std::string &)> callback =
807         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
808             outStrForCallbackCheck = inStrOfReply;};
809     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
810     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
811     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
812     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
813 
814     std::string msg = std::string() +
815         R"({
816             "id":0,
817             "method":"Debugger.setBlackboxPatterns"
818         })";
819     DispatchRequest request(msg);
820 
821     dispatcherImpl->Dispatch(request);
822     EXPECT_STREQ(outStrForCallbackCheck.c_str(),
823         R"({"id":0,"result":{"code":1,"message":"SetBlackboxPatterns not support now"}})");
824     if (protocolChannel) {
825         delete protocolChannel;
826         protocolChannel = nullptr;
827     }
828 }
829 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_ReplyNativeCalling__001)830 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_ReplyNativeCalling__001)
831 {
832     std::string outStrForCallbackCheck = "";
833     std::function<void(const void*, const std::string &)> callback =
834         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
835             outStrForCallbackCheck = inStrOfReply;};
836     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
837     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
838     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
839     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
840 
841     // DebuggerImpl::DispatcherImpl::ReplyNativeCalling -- params == nullptr
842     std::string msg = std::string() +
843         R"({
844             "id":0,
845             "method":"Debugger.replyNativeCalling",
846             "params":{
847                 "userCode":"NotBoolValue"
848             }
849         })";
850     DispatchRequest request(msg);
851 
852     dispatcherImpl->Dispatch(request);
853     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{"code":1,"message":"wrong params"}})");
854     if (protocolChannel) {
855         delete protocolChannel;
856         protocolChannel = nullptr;
857     }
858 }
859 
HWTEST_F_L0(DebuggerImplTest,Dispatcher_Dispatch_ReplyNativeCalling__002)860 HWTEST_F_L0(DebuggerImplTest, Dispatcher_Dispatch_ReplyNativeCalling__002)
861 {
862     std::string outStrForCallbackCheck = "";
863     std::function<void(const void*, const std::string &)> callback =
864         [&outStrForCallbackCheck]([[maybe_unused]] const void *ptr, const std::string &inStrOfReply) {
865             outStrForCallbackCheck = inStrOfReply;};
866     ProtocolChannel *protocolChannel = new ProtocolHandler(callback, ecmaVm);
867     auto runtimeImpl = std::make_unique<RuntimeImpl>(ecmaVm, protocolChannel);
868     auto debuggerImpl = std::make_unique<DebuggerImpl>(ecmaVm, protocolChannel, runtimeImpl.get());
869     auto dispatcherImpl = std::make_unique<DebuggerImpl::DispatcherImpl>(protocolChannel, std::move(debuggerImpl));
870 
871     std::string msg = std::string() +
872         R"({
873             "id":0,
874             "method":"Debugger.replyNativeCalling",
875             "params":{
876                 "userCode":true
877             }
878         })";
879     DispatchRequest request(msg);
880 
881     dispatcherImpl->Dispatch(request);
882     EXPECT_STREQ(outStrForCallbackCheck.c_str(), R"({"id":0,"result":{}})");
883     if (protocolChannel) {
884         delete protocolChannel;
885         protocolChannel = nullptr;
886     }
887 }
888 }  // namespace panda::test
889