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_AND_STEPOUT_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_AND_STEPOUT_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsStepintoAndStepoutTest : public TestActions {
23 public:
JsStepintoAndStepoutTest()24 JsStepintoAndStepoutTest()
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 RecvStepInfo(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 RecvStepInfo(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, "so"},
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 RecvStepInfo(recv, "func_main_0", 23); }},
74 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
75
76 {SocketAction::SEND, "so"},
77 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
78 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
79 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
80 [this](auto recv, auto, auto) -> bool { return RecvStepInfo(recv, "func_main_0", 24); }},
81 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
82
83 {SocketAction::SEND, "resume"},
84 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
85 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
86 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
87
88 // reply success and run
89 {SocketAction::SEND, "success"},
90 {SocketAction::SEND, "resume"},
91 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
92 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
93 };
94 }
95
RecvBreakInfo(std::string recv)96 bool RecvBreakInfo(std::string recv)
97 {
98 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
99 Result ret;
100 std::string method = "";
101 ret = json->GetString("method", &method);
102 if (ret != Result::SUCCESS || method != "Debugger.paused") {
103 return false;
104 }
105
106 std::unique_ptr<PtJson> params = nullptr;
107 ret = json->GetObject("params", ¶ms);
108 if (ret != Result::SUCCESS) {
109 return false;
110 }
111
112 std::unique_ptr<PtJson> hitBreakpoints = nullptr;
113 ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
114 if (ret != Result::SUCCESS) {
115 return false;
116 }
117
118 std::string breakpoint = "";
119 breakpoint = hitBreakpoints->Get(0)->GetString();
120 if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
121 breakpoint.find("22") == std::string::npos) {
122 return false;
123 }
124
125 DebuggerClient debuggerClient(0);
126 debuggerClient.PausedReply(std::move(json));
127 return true;
128 }
129
RecvStepInfo(std::string recv,std::string funcName,int lineNumber)130 bool RecvStepInfo(std::string recv, std::string funcName, int lineNumber)
131 {
132 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
133 Result ret;
134 std::string method = "";
135 ret = json->GetString("method", &method);
136 if (ret != Result::SUCCESS || method != "Debugger.paused") {
137 return false;
138 }
139
140 std::unique_ptr<PtJson> params = nullptr;
141 ret = json->GetObject("params", ¶ms);
142 if (ret != Result::SUCCESS) {
143 return false;
144 }
145
146 std::unique_ptr<PtJson> callFrames = nullptr;
147 ret = params->GetArray("callFrames", &callFrames);
148 if (ret != Result::SUCCESS) {
149 return false;
150 }
151
152 std::string functionName = "";
153 ret = callFrames->Get(0)->GetString("functionName", &functionName);
154 if (ret != Result::SUCCESS || functionName != funcName) {
155 return false;
156 }
157
158 std::unique_ptr<PtJson> location = nullptr;
159 ret = callFrames->Get(0)->GetObject("location", &location);
160 if (ret != Result::SUCCESS) {
161 return false;
162 }
163
164 int lineNum = 0;
165 ret = location->GetInt("lineNumber", &lineNum);
166 if (ret != Result::SUCCESS || lineNum != lineNumber) {
167 return false;
168 }
169
170 DebuggerClient debuggerClient(0);
171 debuggerClient.PausedReply(std::move(json));
172 return true;
173 }
174
RecvWatchInfo(std::string recv)175 bool RecvWatchInfo(std::string recv)
176 {
177 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
178 Result ret;
179 int id;
180 ret = json->GetInt("id", &id);
181 if (ret != Result::SUCCESS) {
182 return false;
183 }
184
185 std::unique_ptr<PtJson> result = nullptr;
186 ret = json->GetObject("result", &result);
187 if (ret != Result::SUCCESS) {
188 return false;
189 }
190
191 std::unique_ptr<PtJson> watchResult = nullptr;
192 ret = result->GetObject("result", &watchResult);
193 if (ret != Result::SUCCESS) {
194 return false;
195 }
196
197 std::string type = "";
198 ret = watchResult->GetString("type", &type);
199 if (ret != Result::SUCCESS || type != "string") {
200 return false;
201 }
202
203 std::string value = "";
204 ret = watchResult->GetString("unserializableValue", &value);
205 if (ret != Result::SUCCESS || value != "js_test") {
206 return false;
207 }
208
209 std::string description = "";
210 ret = watchResult->GetString("description", &description);
211 if (ret != Result::SUCCESS || description != "js_test") {
212 return false;
213 }
214 return true;
215 }
216
GetEntryPoint()217 std::pair<std::string, std::string> GetEntryPoint() override
218 {
219 return {pandaFile_, entryPoint_};
220 }
221 ~JsStepintoAndStepoutTest() = default;
222
223 private:
224 std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
225 std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
226 std::string entryPoint_ = "_GLOBAL::func_main_0";
227 };
228
GetJsStepintoAndStepoutTest()229 std::unique_ptr<TestActions> GetJsStepintoAndStepoutTest()
230 {
231 return std::make_unique<JsStepintoAndStepoutTest>();
232 }
233 } // namespace panda::ecmascript::tooling::test
234
235 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_AND_STEPOUT_TEST_H
236