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_ALLOCATIONTRACK_TEST_H 17#define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_ALLOCATIONTRACK_TEST_H 18 19#include "tooling/test/client_utils/test_util.h" 20 21namespace panda::ecmascript::tooling::test { 22class JsAllocationtrackTest : public TestActions { 23public: 24 JsAllocationtrackTest() 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 sample.js 34 {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN}, 35 // break on start 36 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN}, 37 {SocketAction::SEND, "heapprofiler-enable"}, 38 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 39 {SocketAction::SEND, "allocationtrack"}, 40 {SocketAction::RECV, "", ActionRule::STRING_CONTAIN}, 41 42 {SocketAction::SEND, "b " DEBUGGER_JS_DIR "sample.js 22"}, 43 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 44 // hit breakpoint after resume first time 45 {SocketAction::SEND, "resume"}, 46 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 47 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 48 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN}, 49 50 {SocketAction::SEND, "resume"}, 51 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 52 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 53 {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN}, 54 {SocketAction::SEND, "allocationtrack-stop"}, 55 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, auto) -> bool { 56 return RecvReportProgress(recv); 57 }}, 58 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, bool& needMoreMsg) -> bool { 59 return RecvReportProgressFinished(recv, needMoreMsg); 60 }}, 61 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, [this] (auto recv, auto, bool& needMoreMsg) -> bool { 62 return RecvReportProgressChunk(recv, needMoreMsg); 63 }}, 64 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 65 {SocketAction::SEND, "heapprofiler-disable"}, 66 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 67 // reply success and run 68 {SocketAction::SEND, "success"}, 69 {SocketAction::SEND, "resume"}, 70 {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN}, 71 {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess}, 72 }; 73 } 74 75 bool RecvReportProgress(std::string recv) 76 { 77 std::unique_ptr<PtJson> json = PtJson::Parse(recv); 78 Result ret; 79 std::string method; 80 ret = json->GetString("method", &method); 81 if (ret != Result::SUCCESS) { 82 return false; 83 } 84 85 if (method != "HeapProfiler.reportHeapSnapshotProgress") { 86 return false; 87 } 88 std::unique_ptr<PtJson> params = nullptr; 89 ret = json->GetObject("params", ¶ms); 90 if (ret != Result::SUCCESS) { 91 return false; 92 } 93 int done; 94 ret = params->GetInt("done", &done); 95 if (ret != Result::SUCCESS) { 96 return false; 97 } 98 return true; 99 } 100 101 bool RecvReportProgressFinished(std::string recv, bool &needMoreMsg) 102 { 103 needMoreMsg = false; 104 std::unique_ptr<PtJson> json = PtJson::Parse(recv); 105 Result ret; 106 std::string method; 107 ret = json->GetString("method", &method); 108 if (ret != Result::SUCCESS) { 109 return false; 110 } 111 112 if (method != "HeapProfiler.reportHeapSnapshotProgress") { 113 return false; 114 } 115 116 std::unique_ptr<PtJson> params = nullptr; 117 ret = json->GetObject("params", ¶ms); 118 if (ret != Result::SUCCESS) { 119 return false; 120 } 121 122 int done; 123 ret = params->GetInt("done", &done); 124 if (ret != Result::SUCCESS) { 125 return false; 126 } 127 128 bool finished; 129 ret = params->GetBool("finished", &finished); 130 if (ret != Result::SUCCESS) { 131 needMoreMsg = true; 132 } 133 return true; 134 } 135 136 bool RecvReportProgressChunk(std::string recv, bool& needMoreMsg) 137 { 138 static std::string content; 139 needMoreMsg = false; 140 std::unique_ptr<PtJson> json = PtJson::Parse(recv); 141 Result ret; 142 std::string method; 143 ret = json->GetString("method", &method); 144 if (ret != Result::SUCCESS) { 145 return false; 146 } 147 148 if (method != "HeapProfiler.addHeapSnapshotChunk") { 149 return false; 150 } 151 152 std::unique_ptr<PtJson> params = nullptr; 153 ret = json->GetObject("params", ¶ms); 154 if (ret != Result::SUCCESS) { 155 return false; 156 } 157 158 std::string chunk; 159 ret = params->GetString("chunk", &chunk); 160 if (ret != Result::SUCCESS) { 161 return false; 162 } 163 164 content += chunk; 165 std::unique_ptr<PtJson> contentJson = PtJson::Parse(content); 166 if (contentJson == nullptr || contentJson->Stringify().empty()) { 167 needMoreMsg = true; 168 } 169 return true; 170 } 171 172 std::pair<std::string, std::string> GetEntryPoint() override 173 { 174 return {pandaFile_, entryPoint_}; 175 } 176 177 ~JsAllocationtrackTest() = default; 178 179private: 180 std::string pandaFile_ = DEBUGGER_ABC_DIR "sample.abc"; 181 std::string sourceFile_ = DEBUGGER_JS_DIR "sample.js"; 182 std::string entryPoint_ = "_GLOBAL::func_main_0"; 183}; 184 185std::unique_ptr<TestActions> GetJsAllocationtrackTest() 186{ 187 return std::make_unique<JsAllocationtrackTest>(); 188} 189} // namespace panda::ecmascript::tooling::test 190 191#endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_ALLOCATIONTRACK_TEST_H 192