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_SMART_STEPINTO_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_SMART_STEPINTO_TEST_H
18
19 #include "tooling/test/client_utils/test_util.h"
20
21 namespace panda::ecmascript::tooling::test {
22 class JsSmartStepoutTest : public TestActions {
23 public:
JsSmartStepoutTest()24 JsSmartStepoutTest()
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 smart_stepInto.js
34 {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35 // break on start
36 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37 // set first breakpoint
38 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "smart_stepInto.js 31"},
39 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40
41 // hit breakpoint after resume first time
42 {SocketAction::SEND, "resume"},
43 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
44 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
45 {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
46 [this](auto recv, auto, auto) -> bool { return RecvBreakInfo(recv, "30"); }},
47
48 {SocketAction::SEND, "si"},
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 RecvStepintoInfo(recv, "smart_func1", 16); }},
53
54 {SocketAction::SEND, "resume"},
55 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
56 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
57 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
58
59 // reply success and run
60 {SocketAction::SEND, "success"},
61 {SocketAction::SEND, "resume"},
62 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
63 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
64 };
65 }
66
RecvBreakInfo(std::string recv,std::string lineNumber)67 bool RecvBreakInfo(std::string recv, std::string lineNumber)
68 {
69 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
70 Result ret;
71 std::string method = "";
72 ret = json->GetString("method", &method);
73 if (ret != Result::SUCCESS || method != "Debugger.paused") {
74 return false;
75 }
76
77 std::unique_ptr<PtJson> params = nullptr;
78 ret = json->GetObject("params", ¶ms);
79 if (ret != Result::SUCCESS) {
80 return false;
81 }
82
83 std::unique_ptr<PtJson> hitBreakpoints = nullptr;
84 ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
85 if (ret != Result::SUCCESS) {
86 return false;
87 }
88
89 std::string breakpoint = "";
90 breakpoint = hitBreakpoints->Get(0)->GetString();
91 if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
92 breakpoint.find(lineNumber) == std::string::npos) {
93 return false;
94 }
95
96 DebuggerClient debuggerClient(0);
97 debuggerClient.PausedReply(std::move(json));
98 return true;
99 }
100
RecvStepintoInfo(std::string recv,std::string funcName,int lineNumber)101 bool RecvStepintoInfo(std::string recv, std::string funcName, int lineNumber)
102 {
103 std::unique_ptr<PtJson> json = PtJson::Parse(recv);
104 Result ret;
105 std::string method = "";
106 ret = json->GetString("method", &method);
107 if (ret != Result::SUCCESS || method != "Debugger.paused") {
108 return false;
109 }
110
111 std::unique_ptr<PtJson> params = nullptr;
112 ret = json->GetObject("params", ¶ms);
113 if (ret != Result::SUCCESS) {
114 return false;
115 }
116
117 std::unique_ptr<PtJson> callFrames = nullptr;
118 ret = params->GetArray("callFrames", &callFrames);
119 if (ret != Result::SUCCESS) {
120 return false;
121 }
122
123 std::string functionName = "";
124 ret = callFrames->Get(0)->GetString("functionName", &functionName);
125 if (ret != Result::SUCCESS || functionName != funcName) {
126 return false;
127 }
128
129 std::unique_ptr<PtJson> location = nullptr;
130 ret = callFrames->Get(0)->GetObject("location", &location);
131 if (ret != Result::SUCCESS) {
132 return false;
133 }
134
135 int lineNum = 0;
136 ret = location->GetInt("lineNumber", &lineNum);
137 if (ret != Result::SUCCESS || lineNum != lineNumber) {
138 return false;
139 }
140
141 DebuggerClient debuggerClient(0);
142 debuggerClient.PausedReply(std::move(json));
143 return true;
144 }
145
GetEntryPoint()146 std::pair<std::string, std::string> GetEntryPoint() override { return {pandaFile_, entryPoint_}; }
147 ~JsSmartStepoutTest() = default;
148
149 private:
150 std::string pandaFile_ = DEBUGGER_ABC_DIR "smart_stepInto.abc";
151 std::string sourceFile_ = DEBUGGER_JS_DIR "smart_stepInto.js";
152 std::string entryPoint_ = "_GLOBAL::func_main_0";
153 };
154
GetJsSmartStepoutTest()155 std::unique_ptr<TestActions> GetJsSmartStepoutTest() { return std::make_unique<JsSmartStepoutTest>(); }
156 } // namespace panda::ecmascript::tooling::test
157
158 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_SMART_STEPINTO_TEST_H
159