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_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsStepintoTest : public TestActions {
23 public:
JsStepintoTest()24 JsStepintoTest()
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 common_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 "common_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, "common_func", 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, "common_func", 17); }},
65 {SocketAction::SEND, "watch a"},
66 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
67 [this](auto recv, auto, auto) -> bool { return RecvWatchInfo(recv); }},
68
69 {SocketAction::SEND, "si"},
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 RecvStepintoInfo(recv, "common_func", 18); }},
74 {SocketAction::SEND, "si"},
75 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
76 [this](auto recv, auto, auto) -> bool { return RecvWatchInfo(recv); }},
77 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
78 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
79 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
80
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::STRING_CONTAIN},
85
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
RecvBreakInfo(std::string recv)94 bool RecvBreakInfo(std::string recv)
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", ¶ms);
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("22") == std::string::npos) {
120 return false;
121 }
122
123 DebuggerClient debuggerClient(0);
124 debuggerClient.PausedReply(std::move(json));
125 return true;
126 }
127
RecvStepintoInfo(std::string recv, std::string funcName, int lineNumber)128 bool RecvStepintoInfo(std::string recv, std::string funcName, int lineNumber)
129 {
130 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
131 Result ret;
132 std::string method = "";
133 ret = json->GetString("method", &method);
134 if (ret != Result::SUCCESS || method != "Debugger.paused") {
135 return false;
136 }
137
138 std::unique_ptr<PtJson> params = nullptr;
139 ret = json->GetObject("params", ¶ms);
140 if (ret != Result::SUCCESS) {
141 return false;
142 }
143
144 std::unique_ptr<PtJson> callFrames = nullptr;
145 ret = params->GetArray("callFrames", &callFrames);
146 if (ret != Result::SUCCESS) {
147 return false;
148 }
149
150 std::string functionName = "";
151 ret = callFrames->Get(0)->GetString("functionName", &functionName);
152 if (ret != Result::SUCCESS || functionName != funcName) {
153 return false;
154 }
155
156 std::unique_ptr<PtJson> location = nullptr;
157 ret = callFrames->Get(0)->GetObject("location", &location);
158 if (ret != Result::SUCCESS) {
159 return false;
160 }
161
162 int lineNum = 0;
163 ret = location->GetInt("lineNumber", &lineNum);
164 if (ret != Result::SUCCESS || lineNum != lineNumber) {
165 return false;
166 }
167
168 DebuggerClient debuggerClient(0);
169 debuggerClient.PausedReply(std::move(json));
170 return true;
171 }
172
RecvWatchInfo(std::string recv)173 bool RecvWatchInfo(std::string recv)
174 {
175 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
176 Result ret;
177 int id = 0;
178 ret = json->GetInt("id", &id);
179 if (ret != Result::SUCCESS) {
180 return false;
181 }
182
183 std::unique_ptr<PtJson> result = nullptr;
184 ret = json->GetObject("result", &result);
185 if (ret != Result::SUCCESS) {
186 return false;
187 }
188
189 std::unique_ptr<PtJson> watchResult = nullptr;
190 ret = result->GetObject("result", &watchResult);
191 if (ret != Result::SUCCESS) {
192 return false;
193 }
194
195 std::string type = "";
196 ret = watchResult->GetString("type", &type);
197 if (ret != Result::SUCCESS || type != "string") {
198 return false;
199 }
200
201 std::string value = "";
202 ret = watchResult->GetString("unserializableValue", &value);
203 if (ret != Result::SUCCESS || value != "js_test") {
204 return false;
205 }
206
207 std::string description = "";
208 ret = watchResult->GetString("description", &description);
209 if (ret != Result::SUCCESS || description != "js_test") {
210 return false;
211 }
212 return true;
213 }
214
215 std::pair<std::string, std::string> GetEntryPoint() override
216 {
217 return {pandaFile_, entryPoint_};
218 }
219 ~JsStepintoTest() = default;
220
221 private:
222 std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
223 std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
224 std::string entryPoint_ = "_GLOBAL::func_main_0";
225 };
226
GetJsStepintoTest()227 std::unique_ptr<TestActions> GetJsStepintoTest()
228 {
229 return std::make_unique<JsStepintoTest>();
230 }
231 } // namespace panda::ecmascript::tooling::test
232
233 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_TEST_H
234