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_STEPINTO_ARROW_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_ARROW_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsStepintoArrowTest : public TestActions {
23 public:
JsStepintoArrowTest()24 JsStepintoArrowTest()
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 arrow_func.js
34 {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35 // break on start
36 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
37 [](auto recv, auto, auto) -> bool {
38 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
39 DebuggerClient debuggerClient(0);
40 debuggerClient.RecvReply(std::move(json));
41 return true;
42 }},
43 // set first breakpoint
44 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "arrow_func.js 23"},
45 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46
47 // hit breakpoint after resume first time
48 {SocketAction::SEND, "resume"},
49 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
50 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
51 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
52 [this](auto recv, auto, auto) -> bool { return RecvBreakInfo(recv); }},
53
54 {SocketAction::SEND, "si"},
55 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
56 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
57 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
58 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "sum", 16); }},
59
60 {SocketAction::SEND, "si"},
61 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
62 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
63 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
64 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "sum", 17); }},
65
66 {SocketAction::SEND, "si"},
67 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
68 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
69 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
70 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "sum", 18); }},
71
72 {SocketAction::SEND, "si"},
73 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
74 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
75 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
76 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "sum", 19); }},
77
78 {SocketAction::SEND, "si"},
79 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
80 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
81 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
82 [this](auto recv, auto, auto) -> bool { return RecvStepintoInfo(recv, "sum", 20); }},
83
84 {SocketAction::SEND, "watch num1"},
85 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
86 [this](auto recv, auto, auto) -> bool { return RecvWatchInfo(recv, "number", "2"); }},
87 {SocketAction::SEND, "watch num2"},
88 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
89 [this](auto recv, auto, auto) -> bool { return RecvWatchInfo(recv, "number", "3"); }},
90
91 {SocketAction::SEND, "resume"},
92 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
93 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
94 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
95
96 // reply success and run
97 {SocketAction::SEND, "success"},
98 {SocketAction::SEND, "resume"},
99 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
100 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
101 };
102 }
103
RecvBreakInfo(std::string recv)104 bool RecvBreakInfo(std::string recv)
105 {
106 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
107 Result ret;
108 std::string method = "";
109 ret = json->GetString("method", &method);
110 if (ret != Result::SUCCESS || method != "Debugger.paused") {
111 return false;
112 }
113
114 std::unique_ptr<PtJson> params = nullptr;
115 ret = json->GetObject("params", ¶ms);
116 if (ret != Result::SUCCESS) {
117 return false;
118 }
119
120 std::unique_ptr<PtJson> hitBreakpoints = nullptr;
121 ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
122 if (ret != Result::SUCCESS) {
123 return false;
124 }
125
126 std::string breakpoint = "";
127 breakpoint = hitBreakpoints->Get(0)->GetString();
128 if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
129 breakpoint.find("22") == std::string::npos) {
130 return false;
131 }
132
133 DebuggerClient debuggerClient(0);
134 debuggerClient.PausedReply(std::move(json));
135 return true;
136 }
137
RecvStepintoInfo(std::string recv,std::string funcName,int lineNumber)138 bool RecvStepintoInfo(std::string recv, std::string funcName, int lineNumber)
139 {
140 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
141 Result ret;
142 std::string method = "";
143 ret = json->GetString("method", &method);
144 if (ret != Result::SUCCESS || method != "Debugger.paused") {
145 return false;
146 }
147
148 std::unique_ptr<PtJson> params = nullptr;
149 ret = json->GetObject("params", ¶ms);
150 if (ret != Result::SUCCESS) {
151 return false;
152 }
153
154 std::unique_ptr<PtJson> callFrames = nullptr;
155 ret = params->GetArray("callFrames", &callFrames);
156 if (ret != Result::SUCCESS) {
157 return false;
158 }
159
160 std::string functionName = "";
161 ret = callFrames->Get(0)->GetString("functionName", &functionName);
162 if (ret != Result::SUCCESS || functionName != funcName) {
163 return false;
164 }
165
166 std::unique_ptr<PtJson> location = nullptr;
167 ret = callFrames->Get(0)->GetObject("location", &location);
168 if (ret != Result::SUCCESS) {
169 return false;
170 }
171
172 int lineNum = 0;
173 ret = location->GetInt("lineNumber", &lineNum);
174 if (ret != Result::SUCCESS || lineNum != lineNumber) {
175 return false;
176 }
177
178 DebuggerClient debuggerClient(0);
179 debuggerClient.PausedReply(std::move(json));
180 return true;
181 }
182
RecvWatchInfo(std::string recv,std::string var_type,std::string var_value)183 bool RecvWatchInfo(std::string recv, std::string var_type, std::string var_value)
184 {
185 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
186 Result ret;
187 int id = 0;
188 ret = json->GetInt("id", &id);
189 if (ret != Result::SUCCESS) {
190 return false;
191 }
192
193 std::unique_ptr<PtJson> result = nullptr;
194 ret = json->GetObject("result", &result);
195 if (ret != Result::SUCCESS) {
196 return false;
197 }
198
199 std::unique_ptr<PtJson> watchResult = nullptr;
200 ret = result->GetObject("result", &watchResult);
201 if (ret != Result::SUCCESS) {
202 return false;
203 }
204
205 std::string type = "";
206 ret = watchResult->GetString("type", &type);
207 if (ret != Result::SUCCESS || type != var_type) {
208 return false;
209 }
210
211 std::string value = "";
212 ret = watchResult->GetString("unserializableValue", &value);
213 if (ret != Result::SUCCESS || value != var_value) {
214 return false;
215 }
216
217 std::string description = "";
218 ret = watchResult->GetString("description", &description);
219 if (ret != Result::SUCCESS || description != var_value) {
220 return false;
221 }
222
223 DebuggerClient debuggerClient(0);
224 debuggerClient.PausedReply(std::move(json));
225 return true;
226 }
227
GetEntryPoint()228 std::pair<std::string, std::string> GetEntryPoint() override
229 {
230 return {pandaFile_, entryPoint_};
231 }
232 ~JsStepintoArrowTest() = default;
233
234 private:
235 std::string pandaFile_ = DEBUGGER_ABC_DIR "arrow_func.abc";
236 std::string sourceFile_ = DEBUGGER_JS_DIR "arrow_func.js";
237 std::string entryPoint_ = "_GLOBAL::func_main_0";
238 };
239
GetJsStepintoArrowTest()240 std::unique_ptr<TestActions> GetJsStepintoArrowTest()
241 {
242 return std::make_unique<JsStepintoArrowTest>();
243 }
244 } // namespace panda::ecmascript::tooling::test
245
246 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_ARROW_TEST_H
247