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_STEPOVER_RECURSION_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPOVER_RECURSION_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsStepoverRecursionTest : public TestActions {
23public:
24    JsStepoverRecursionTest()
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,
40                [](auto recv, auto, auto) -> bool {
41                    std::unique_ptr<PtJson> json = PtJson::Parse(recv);
42                    DebuggerClient debuggerClient(0);
43                    debuggerClient.RecvReply(std::move(json));
44                    return true;
45                }},
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::STRING_CONTAIN},
52
53            {SocketAction::SEND, "sov"},
54            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
55            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
56            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
57                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "factorial", 60); }},
58            {SocketAction::SEND, "sov"},
59            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
60            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
61            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
62                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "factorial", 60); }},
63            {SocketAction::SEND, "sov"},
64            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
65            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
66            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
67                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "factorial", 61); }},
68            {SocketAction::SEND, "sov"},
69            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
70            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
71            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
72                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "factorial", 60); }},
73            {SocketAction::SEND, "sov"},
74            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
75            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
76            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
77                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "factorial", 61); }},
78            {SocketAction::SEND, "sov"},
79            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
80            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
81            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
82                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "factorial", 60); }},
83            {SocketAction::SEND, "sov"},
84            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
85            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
86            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
87                [this](auto recv, auto, auto) -> bool { return RecvStepoverInfo(recv, "factorial", 61); }},
88
89            {SocketAction::SEND, "delete 1"},
90            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
91            {SocketAction::SEND, "resume"},
92            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
93            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
94            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
95
96            // reply success and run
97            {SocketAction::SEND, "success"},
98            {SocketAction::SEND, "resume"},
99            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
100            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
101        };
102    }
103
104    bool RecvStepoverInfo(std::string recv, std::string funcName, int lineNumber)
105    {
106        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
107        Result ret;
108        std::string method = "";
109        ret = json->GetString("method", &method);
110        if (ret != Result::SUCCESS || method != "Debugger.paused") {
111            return false;
112        }
113
114        std::unique_ptr<PtJson> params = nullptr;
115        ret = json->GetObject("params", &params);
116        if (ret != Result::SUCCESS) {
117            return false;
118        }
119
120        std::unique_ptr<PtJson> callFrames = nullptr;
121        ret = params->GetArray("callFrames", &callFrames);
122        if (ret != Result::SUCCESS) {
123            return false;
124        }
125
126        std::string functionName = "";
127        ret = callFrames->Get(0)->GetString("functionName", &functionName);
128        if (ret != Result::SUCCESS || functionName != funcName) {
129            return false;
130        }
131
132        std::unique_ptr<PtJson> location = nullptr;
133        ret = callFrames->Get(0)->GetObject("location", &location);
134        if (ret != Result::SUCCESS) {
135            return false;
136        }
137
138        int lineNum = 0;
139        ret = location->GetInt("lineNumber", &lineNum);
140        if (ret != Result::SUCCESS || lineNum != lineNumber) {
141            return false;
142        }
143
144        DebuggerClient debuggerClient(0);
145        debuggerClient.PausedReply(std::move(json));
146        return true;
147    }
148
149    std::pair<std::string, std::string> GetEntryPoint() override
150    {
151        return {pandaFile_, entryPoint_};
152    }
153    ~JsStepoverRecursionTest() = default;
154
155private:
156    std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
157    std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
158    std::string entryPoint_ = "_GLOBAL::func_main_0";
159};
160
161std::unique_ptr<TestActions> GetJsStepoverRecursionTest()
162{
163    return std::make_unique<JsStepoverRecursionTest>();
164}
165} // namespace panda::ecmascript::tooling::test
166
167#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_STEPINTO_RECURSION_TEST_H
168