1/*
2 * Copyright (c) 2023 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_EXCEPTION_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_EXCEPTION_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsExceptionTest : public TestActions {
23public:
24    JsExceptionTest()
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 closure_scope.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 "exception.js 24"},
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, [this] (auto recv, auto, auto) -> bool {
46                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
47                Result ret;
48                std::string method;
49                ret = json->GetString("method", &method);
50                if (ret != Result::SUCCESS || method != "Debugger.paused") {
51                    return false;
52                }
53
54                std::unique_ptr<PtJson> params = nullptr;
55                ret = json->GetObject("params", &params);
56                if (ret != Result::SUCCESS) {
57                    return false;
58                }
59
60                std::unique_ptr<PtJson> hitBreakpoints = nullptr;
61                ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
62                if (ret != Result::SUCCESS) {
63                    return false;
64                }
65
66                std::string breakpoint;
67                breakpoint = hitBreakpoints->Get(0)->GetString();
68                if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
69                    breakpoint.find("23") == std::string::npos) {
70                    return false;
71                }
72
73                DebuggerClient debuggerClient(0);
74                debuggerClient.PausedReply(std::move(json));
75                return true;
76            }},
77
78            {SocketAction::SEND, "print"},
79            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
80
81            {SocketAction::SEND, "si"},
82            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
83            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
84            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [] (auto recv, auto, auto) -> bool {
85                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
86                Result ret;
87                std::string method;
88                ret = json->GetString("method", &method);
89                if (ret != Result::SUCCESS || method != "Debugger.paused") {
90                    return false;
91                }
92
93                std::unique_ptr<PtJson> params = nullptr;
94                ret = json->GetObject("params", &params);
95                if (ret != Result::SUCCESS) {
96                    return false;
97                }
98
99                std::unique_ptr<PtJson> callFrames = nullptr;
100                ret = params->GetArray("callFrames", &callFrames);
101                if (ret != Result::SUCCESS) {
102                    return false;
103                }
104
105                std::string functionName;
106                ret = callFrames->Get(0)->GetString("functionName", &functionName);
107                if (ret != Result::SUCCESS || functionName != "exception_func") {
108                    return false;
109                }
110
111                DebuggerClient debuggerClient(0);
112                debuggerClient.PausedReply(std::move(json));
113                return true;
114            }},
115
116            {SocketAction::SEND, "print"},
117            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
118
119            {SocketAction::SEND, "si"},
120            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
121            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
122            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [] (auto recv, auto, auto) -> bool {
123                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
124                Result ret;
125                std::string method;
126                ret = json->GetString("method", &method);
127                if (ret != Result::SUCCESS || method != "Debugger.paused") {
128                    return false;
129                }
130
131                std::unique_ptr<PtJson> params = nullptr;
132                ret = json->GetObject("params", &params);
133                if (ret != Result::SUCCESS) {
134                    return false;
135                }
136
137                std::unique_ptr<PtJson> callFrames = nullptr;
138                ret = params->GetArray("callFrames", &callFrames);
139                if (ret != Result::SUCCESS) {
140                    return false;
141                }
142
143                DebuggerClient debuggerClient(0);
144                debuggerClient.PausedReply(std::move(json));
145                return true;
146            }},
147
148            {SocketAction::SEND, "print"},
149            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
150
151            {SocketAction::SEND, "si"},
152            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
153            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
154            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
155
156            {SocketAction::SEND, "print"},
157            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
158
159            {SocketAction::SEND, "si"},
160            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
161            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
162            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE, [] (auto recv, auto, auto) -> bool {
163                std::unique_ptr<PtJson> json = PtJson::Parse(recv);
164                Result ret;
165                std::string method;
166                ret = json->GetString("method", &method);
167                if (ret != Result::SUCCESS || method != "Debugger.paused") {
168                    return false;
169                }
170
171                std::unique_ptr<PtJson> params = nullptr;
172                ret = json->GetObject("params", &params);
173                if (ret != Result::SUCCESS) {
174                    return false;
175                }
176
177                std::unique_ptr<PtJson> callFrames = nullptr;
178                ret = params->GetArray("callFrames", &callFrames);
179                if (ret != Result::SUCCESS) {
180                    return false;
181                }
182
183                DebuggerClient debuggerClient(0);
184                debuggerClient.PausedReply(std::move(json));
185                return true;
186            }},
187
188            // reply success and run
189            {SocketAction::SEND, "success"},
190            {SocketAction::SEND, "resume"},
191            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
192            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
193        };
194    }
195
196    std::pair<std::string, std::string> GetEntryPoint() override
197    {
198        return {pandaFile_, entryPoint_};
199    }
200    ~JsExceptionTest() = default;
201
202private:
203    std::string pandaFile_ = DEBUGGER_ABC_DIR "exception.abc";
204    std::string sourceFile_ = DEBUGGER_JS_DIR "exception.js";
205    std::string entryPoint_ = "_GLOBAL::func_main_0";
206};
207
208std::unique_ptr<TestActions> GetJsExceptionTest()
209{
210    return std::make_unique<JsExceptionTest>();
211}
212}  // namespace panda::ecmascript::tooling::test
213
214#endif  // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_TEST_H
215