1 /*
2  * Copyright (c) 2024 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 #ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_MULTIPLE_BREAKPOINT_IN_FUNCTION_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_MULTIPLE_BREAKPOINT_IN_FUNCTION_TEST_H
18 
19 #include "tooling/test/client_utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsMultipleBreakpointInFunctionTest : public TestActions {
23 public:
JsMultipleBreakpointInFunctionTest()24     JsMultipleBreakpointInFunctionTest()
25     {
26         testAction = {
27             {SocketAction::SEND, "enable"},
28             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29             {SocketAction::SEND, "runtime-enable"},
30             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31             {SocketAction::SEND, "run"},
32             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33             // load step.js
34             {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35             // break on start
36             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37             // set breakpoint
38             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 19"},
39             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 27"},
41             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
42             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 38"},
43             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
44             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 52"},
45             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 62"},
47             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
48             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "step.js 77"},
49             // hit breakpoint
50             {SocketAction::SEND, "resume"},
51             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
52             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
53             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
54             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
55                 [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 26); }},
56             // hit breakpoint
57             {SocketAction::SEND, "resume"},
58             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
59             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
60             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
61                 [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 76); }},
62             // hit breakpoint
63             {SocketAction::SEND, "resume"},
64             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
65             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
66             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
67                 [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 37); }},
68             // hit breakpoint
69             {SocketAction::SEND, "resume"},
70             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
71             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
72             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
73                 [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 51); }},
74             // hit breakpoint
75             {SocketAction::SEND, "resume"},
76             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
77             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
78             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
79                 [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 18); }},
80             // hit breakpoint
81             {SocketAction::SEND, "resume"},
82             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
83             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
84             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
85                 [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 61); }},
86             // reply success and run
87             {SocketAction::SEND, "success"},
88             {SocketAction::SEND, "resume"},
89             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
90             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
91         };
92     }
93 
RecvHitBreakInfo(std::string recv, int line)94     bool RecvHitBreakInfo(std::string recv, int line)
95     {
96         std::unique_ptr<PtJson> json = PtJson::Parse(recv);
97         Result ret;
98         std::string method = "";
99         ret = json->GetString("method", &method);
100         if (ret != Result::SUCCESS || method != "Debugger.paused") {
101             return false;
102         }
103 
104         std::unique_ptr<PtJson> params = nullptr;
105         ret = json->GetObject("params", &params);
106         if (ret != Result::SUCCESS) {
107             return false;
108         }
109 
110         std::unique_ptr<PtJson> hitBreakpoints = nullptr;
111         ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
112         if (ret != Result::SUCCESS) {
113             return false;
114         }
115 
116         std::string breakpoint = "";
117         breakpoint = hitBreakpoints->Get(0)->GetString();
118         if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
119             breakpoint.find(std::to_string(line)) == std::string::npos) {
120             return false;
121         }
122         return true;
123     }
124 
125     std::pair<std::string, std::string> GetEntryPoint() override
126     {
127         return {pandaFile_, entryPoint_};
128     }
129     ~JsMultipleBreakpointInFunctionTest() = default;
130 
131 private:
132     std::string pandaFile_ = DEBUGGER_ABC_DIR "step.abc";
133     std::string sourceFile_ = DEBUGGER_JS_DIR "step.js";
134     std::string entryPoint_ = "_GLOBAL::func_main_0";
135 };
136 
GetJsMultipleBreakpointInFunctionTest()137 std::unique_ptr<TestActions> GetJsMultipleBreakpointInFunctionTest()
138 {
139     return std::make_unique<JsMultipleBreakpointInFunctionTest>();
140 }
141 } // namespace panda::ecmascript::tooling::test
142 
143 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_MULTIPLE_BREAKPOINT_IN_FUNCTION_TEST_H
144