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_BREAKPOINT_RECURSION_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_RECURSION_TEST_H
18 
19 #include "tooling/test/client_utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsBreakpointRecursionTest : public TestActions {
23 public:
JsBreakpointRecursionTest()24     JsBreakpointRecursionTest()
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::STRING_CONTAIN},
37             // set first breakpoint
38             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 61"},
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::STRING_CONTAIN},
46 
47             {SocketAction::SEND, "resume"},
48             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
49             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
50             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
51                 [this](auto recv, auto, auto) -> bool { return RecvBreakInfo(recv, "factorial", 60); }},
52             {SocketAction::SEND, "resume"},
53             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
54             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
55             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
56                 [this](auto recv, auto, auto) -> bool { return RecvBreakInfo(recv, "factorial", 60); }},
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 RecvBreakInfo(recv, "factorial", 60); }},
62             {SocketAction::SEND, "resume"},
63             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
64             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
65             {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
66                 [this](auto recv, auto, auto) -> bool { return RecvBreakInfo(recv, "factorial", 60); }},
67 
68             {SocketAction::SEND, "resume"},
69             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
70             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
71             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
72 
73             // reply success and run
74             {SocketAction::SEND, "success"},
75             {SocketAction::SEND, "resume"},
76             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
77             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
78         };
79     }
80 
RecvBreakInfo(std::string recv, std::string funcName, int lineNumber)81     bool RecvBreakInfo(std::string recv, std::string funcName, int lineNumber)
82     {
83         std::unique_ptr<PtJson> json = PtJson::Parse(recv);
84         Result ret;
85         std::string method = "";
86         ret = json->GetString("method", &method);
87         if (ret != Result::SUCCESS || method != "Debugger.paused") {
88             return false;
89         }
90 
91         std::unique_ptr<PtJson> params = nullptr;
92         ret = json->GetObject("params", &params);
93         if (ret != Result::SUCCESS) {
94             return false;
95         }
96 
97         std::unique_ptr<PtJson> callFrames = nullptr;
98         ret = params->GetArray("callFrames", &callFrames);
99         if (ret != Result::SUCCESS) {
100             return false;
101         }
102 
103         std::string functionName = "";
104         ret = callFrames->Get(0)->GetString("functionName", &functionName);
105         if (ret != Result::SUCCESS || functionName != funcName) {
106             return false;
107         }
108 
109         std::unique_ptr<PtJson> location = nullptr;
110         ret = callFrames->Get(0)->GetObject("location", &location);
111         if (ret != Result::SUCCESS) {
112             return false;
113         }
114 
115         int lineNum = 0;
116         ret = location->GetInt("lineNumber", &lineNum);
117         if (ret != Result::SUCCESS || lineNum != lineNumber) {
118             return false;
119         }
120 
121         DebuggerClient debuggerClient(0);
122         debuggerClient.PausedReply(std::move(json));
123         return true;
124     }
125 
126     std::pair<std::string, std::string> GetEntryPoint() override
127     {
128         return {pandaFile_, entryPoint_};
129     }
130     ~JsBreakpointRecursionTest() = default;
131 
132 private:
133     std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
134     std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
135     std::string entryPoint_ = "_GLOBAL::func_main_0";
136 };
137 
GetJsBreakpointRecursionTest()138 std::unique_ptr<TestActions> GetJsBreakpointRecursionTest()
139 {
140     return std::make_unique<JsBreakpointRecursionTest>();
141 }
142 } // namespace panda::ecmascript::tooling::test
143 
144 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_RECURSION_TEST_H
145