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_HEAPUSAGE_RECURSION_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_HEAPUSAGE_RECURSION_TEST_H
18 
19 #include "tooling/test/client_utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsHeapusageRecursionTest : public TestActions {
23 public:
JsHeapusageRecursionTest()24     JsHeapusageRecursionTest()
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 
38             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 67"},
39             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
40             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 68"},
41             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
42 
43             {SocketAction::SEND, "resume"},
44             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
45             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
46             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
47             {SocketAction::SEND, "heapusage"},
48             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
49                 [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
50 
51             {SocketAction::SEND, "resume"},
52             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
53             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
54             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
55             {SocketAction::SEND, "heapusage"},
56             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
57                 [this](auto recv, auto, auto) -> bool { return RecvHeapusageInfo(recv); }},
58 
59             {SocketAction::SEND, "resume"},
60             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
61             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
62             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
63 
64             // reply success and run
65             {SocketAction::SEND, "success"},
66             {SocketAction::SEND, "resume"},
67             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
68             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
69         };
70     }
71 
RecvHeapusageInfo(std::string recv)72     bool RecvHeapusageInfo(std::string recv)
73     {
74         std::unique_ptr<PtJson> json = PtJson::Parse(recv);
75         Result ret;
76         int id = 0;
77         ret = json->GetInt("id", &id);
78         if (ret != Result::SUCCESS) {
79             return false;
80         }
81 
82         std::unique_ptr<PtJson> result = nullptr;
83         ret = json->GetObject("result", &result);
84         if (ret != Result::SUCCESS) {
85             return false;
86         }
87 
88         int usedSize = 0;
89         ret = result->GetInt("usedSize", &usedSize);
90         if (ret != Result::SUCCESS) {
91             return false;
92         }
93 
94         int totalSize = 0;
95         ret = result->GetInt("totalSize", &totalSize);
96         if (ret != Result::SUCCESS) {
97             return false;
98         }
99         return true;
100     }
101 
102     std::pair<std::string, std::string> GetEntryPoint() override
103     {
104         return {pandaFile_, entryPoint_};
105     }
106     ~JsHeapusageRecursionTest() = default;
107 
108 private:
109     std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
110     std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
111     std::string entryPoint_ = "_GLOBAL::func_main_0";
112 };
113 
GetJsHeapusageRecursionTest()114 std::unique_ptr<TestActions> GetJsHeapusageRecursionTest()
115 {
116     return std::make_unique<JsHeapusageRecursionTest>();
117 }
118 } // namespace panda::ecmascript::tooling::test
119 
120 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_RECURSION_TEST_H
121