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_CANNOT_HIT_TEST_H
17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_CANNOT_HIT_TEST_H
18
19#include "tooling/test/client_utils/test_util.h"
20
21namespace panda::ecmascript::tooling::test {
22class JsBreakpointCannotHitTest : public TestActions {
23public:
24    JsBreakpointCannotHitTest()
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 branch.js
34            {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35            // break on start
36            {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37            // set breakpoint
38            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "branch.js 19"},
39            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "branch.js 22"},
41            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
42            {SocketAction::SEND, "b " DEBUGGER_JS_DIR "branch.js 24"},
43            // hit breakpoint after resume first time
44            {SocketAction::SEND, "resume"},
45            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
47            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
48            {SocketAction::RECV, "Debugger.paused", ActionRule::CUSTOM_RULE,
49                [this](auto recv, auto, auto) -> bool { return RecvHitBreakInfo(recv, 18); }},
50
51            // hit breakpoint after resume second time
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 RecvHitBreakInfo(recv, 23); }},
57
58            // reply success and run
59            {SocketAction::SEND, "success"},
60            {SocketAction::SEND, "resume"},
61            {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
62            {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
63        };
64    }
65
66    bool RecvHitBreakInfo(std::string recv, int line)
67    {
68        std::unique_ptr<PtJson> json = PtJson::Parse(recv);
69        Result ret;
70        std::string method = "";
71        ret = json->GetString("method", &method);
72        if (ret != Result::SUCCESS || method != "Debugger.paused") {
73            return false;
74        }
75
76        std::unique_ptr<PtJson> params = nullptr;
77        ret = json->GetObject("params", &params);
78        if (ret != Result::SUCCESS) {
79            return false;
80        }
81
82        std::unique_ptr<PtJson> hitBreakpoints = nullptr;
83        ret = params->GetArray("hitBreakpoints", &hitBreakpoints);
84        if (ret != Result::SUCCESS) {
85            return false;
86        }
87
88        std::string breakpoint = "";
89        breakpoint = hitBreakpoints->Get(0)->GetString();
90        if (ret != Result::SUCCESS || breakpoint.find(sourceFile_) == std::string::npos ||
91            breakpoint.find(std::to_string(line)) == std::string::npos) {
92            return false;
93        }
94        return true;
95    }
96
97    std::pair<std::string, std::string> GetEntryPoint() override
98    {
99        return {pandaFile_, entryPoint_};
100    }
101    ~JsBreakpointCannotHitTest() = default;
102
103private:
104    std::string pandaFile_ = DEBUGGER_ABC_DIR "branch.abc";
105    std::string sourceFile_ = DEBUGGER_JS_DIR "branch.js";
106    std::string entryPoint_ = "_GLOBAL::func_main_0";
107};
108
109std::unique_ptr<TestActions> GetJsBreakpointCannotHitTest()
110{
111    return std::make_unique<JsBreakpointCannotHitTest>();
112}
113} // namespace panda::ecmascript::tooling::test
114
115#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_BREAKPOINT_CANNOT_HIT_TEST_H
116