1 /*
2 * Copyright (c) 2022 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 #include "agent/tracing_impl.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "protocol_handler.h"
19
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::tooling;
22
23 namespace panda::test {
24 class TracingImplTest : public testing::Test {
25 public:
SetUpTestCase()26 static void SetUpTestCase()
27 {
28 GTEST_LOG_(INFO) << "SetUpTestCase";
29 }
30
TearDownTestCase()31 static void TearDownTestCase()
32 {
33 GTEST_LOG_(INFO) << "TearDownCase";
34 }
35
SetUp()36 void SetUp() override
37 {
38 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
39 }
40
TearDown()41 void TearDown() override
42 {
43 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
44 }
45
46 protected:
47 EcmaVM *ecmaVm {nullptr};
48 EcmaHandleScope *scope {nullptr};
49 JSThread *thread {nullptr};
50 };
51
HWTEST_F_L0(TracingImplTest,EndTest)52 HWTEST_F_L0(TracingImplTest, EndTest)
53 {
54 ProtocolChannel *channel = nullptr;
55 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
56 DispatchResponse response = tracing->End();
57 ASSERT_TRUE(response.GetMessage() == "End not support now.");
58 }
59
HWTEST_F_L0(TracingImplTest,GetCategoriesTest)60 HWTEST_F_L0(TracingImplTest, GetCategoriesTest)
61 {
62 ProtocolChannel *channel = nullptr;
63 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
64 std::vector<std::string> categories = {};
65 DispatchResponse response = tracing->GetCategories(categories);
66 ASSERT_TRUE(response.GetMessage() == "GetCategories not support now.");
67 }
68
HWTEST_F_L0(TracingImplTest,RecordClockSyncMarkerTest)69 HWTEST_F_L0(TracingImplTest, RecordClockSyncMarkerTest)
70 {
71 ProtocolChannel *channel = nullptr;
72 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
73 std::string syncId;
74 DispatchResponse response = tracing->RecordClockSyncMarker(syncId);
75 ASSERT_TRUE(response.GetMessage() == "RecordClockSyncMarker not support now.");
76 }
77
HWTEST_F_L0(TracingImplTest,RequestMemoryDumpTest)78 HWTEST_F_L0(TracingImplTest, RequestMemoryDumpTest)
79 {
80 ProtocolChannel *channel = nullptr;
81 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
82 auto params = std::make_unique<RequestMemoryDumpParams>();
83 std::string dumpGuid;
84 bool success = false;
85 DispatchResponse response = tracing->RequestMemoryDump(std::move(params), dumpGuid, success);
86 ASSERT_TRUE(response.GetMessage() == "RequestMemoryDump not support now.");
87 }
88
HWTEST_F_L0(TracingImplTest,StartTest)89 HWTEST_F_L0(TracingImplTest, StartTest)
90 {
91 ProtocolChannel *channel = nullptr;
92 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
93 auto params = std::make_unique<StartParams>();
94 DispatchResponse response = tracing->Start(std::move(params));
95 ASSERT_TRUE(response.GetMessage() == "Start not support now.");
96 }
97
HWTEST_F_L0(TracingImplTest,DispatcherImplDispatchTest)98 HWTEST_F_L0(TracingImplTest, DispatcherImplDispatchTest)
99 {
100 std::string result = "";
101 std::function<void(const void*, const std::string &)> callback =
102 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
103 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
104 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
105 auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
106 std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";;
107 DispatchRequest request(msg);
108 dispatcherImpl->Dispatch(request);
109 ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
110 msg = std::string() + R"({"id":0,"method":"Debugger.end","params":{}})";
111 DispatchRequest request1 = DispatchRequest(msg);
112 dispatcherImpl->Dispatch(request1);
113 if (channel) {
114 delete channel;
115 channel = nullptr;
116 }
117 ASSERT_TRUE(result.find("End not support now.") != std::string::npos);
118 }
119
HWTEST_F_L0(TracingImplTest,DispatcherImplEndTest)120 HWTEST_F_L0(TracingImplTest, DispatcherImplEndTest)
121 {
122 std::string result = "";
123 std::function<void(const void*, const std::string &)> callback =
124 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
125 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
126 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
127 auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
128 std::string msg = std::string() + R"({"id":0,"method":"Debugger.end","params":{}})";
129 DispatchRequest request = DispatchRequest(msg);
130 dispatcherImpl->Dispatch(request);
131 if (channel) {
132 delete channel;
133 channel = nullptr;
134 }
135 ASSERT_TRUE(result.find("End not support now.") != std::string::npos);
136 }
137
HWTEST_F_L0(TracingImplTest,DispatcherImplGetCategoriesTest)138 HWTEST_F_L0(TracingImplTest, DispatcherImplGetCategoriesTest)
139 {
140 std::string result = "";
141 std::function<void(const void*, const std::string &)> callback =
142 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
143 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
144 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
145 auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
146 std::string msg = std::string() + R"({"id":0,"method":"Debugger.getCategories","params":{}})";
147 DispatchRequest request = DispatchRequest(msg);
148 dispatcherImpl->Dispatch(request);
149 if (channel) {
150 delete channel;
151 channel = nullptr;
152 }
153 ASSERT_TRUE(result.find("GetCategories not support now") != std::string::npos);
154 }
155
HWTEST_F_L0(TracingImplTest,DispatcherImplRecordClockSyncMarkerTest)156 HWTEST_F_L0(TracingImplTest, DispatcherImplRecordClockSyncMarkerTest)
157 {
158 std::string result = "";
159 std::function<void(const void*, const std::string &)> callback =
160 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
161 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
162 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
163 auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
164 std::string msg = std::string() + R"({"id":0,"method":"Debugger.recordClockSyncMarker","params":{}})";
165 DispatchRequest request = DispatchRequest(msg);
166 dispatcherImpl->Dispatch(request);
167 if (channel) {
168 delete channel;
169 channel = nullptr;
170 }
171 ASSERT_TRUE(result.find("RecordClockSyncMarker not support now.") != std::string::npos);
172 }
173
HWTEST_F_L0(TracingImplTest,DispatcherImplRequestMemoryDumpTest)174 HWTEST_F_L0(TracingImplTest, DispatcherImplRequestMemoryDumpTest)
175 {
176 std::string result = "";
177 std::function<void(const void*, const std::string &)> callback =
178 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
179 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
180 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
181 auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
182 std::string msg = std::string() + R"({"id":0,"method":"Debugger.requestMemoryDump","params":{}})";
183 DispatchRequest request = DispatchRequest(msg);
184 dispatcherImpl->Dispatch(request);
185 if (channel) {
186 delete channel;
187 channel = nullptr;
188 }
189 ASSERT_TRUE(result.find("RequestMemoryDump not support now.") != std::string::npos);
190 }
191
HWTEST_F_L0(TracingImplTest,DispatcherImplStartDumpTest)192 HWTEST_F_L0(TracingImplTest, DispatcherImplStartDumpTest)
193 {
194 std::string result = "";
195 std::function<void(const void*, const std::string &)> callback =
196 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
197 ProtocolChannel *channel = new ProtocolHandler(callback, ecmaVm);
198 auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
199 auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
200 std::string msg = std::string() + R"({"id":0,"method":"Debugger.start","params":{}})";
201 DispatchRequest request = DispatchRequest(msg);
202 dispatcherImpl->Dispatch(request);
203 if (channel) {
204 delete channel;
205 channel = nullptr;
206 }
207 ASSERT_TRUE(result.find("Start not support now.") != std::string::npos);
208 }
209
HWTEST_F_L0(TracingImplTest,FrontendBufferUsageTest)210 HWTEST_F_L0(TracingImplTest, FrontendBufferUsageTest)
211 {
212 std::string result = "";
213 std::function<void(const void*, const std::string &)> callback =
214 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
215 ProtocolChannel *channel = nullptr;
216 auto frontend = std::make_unique<TracingImpl::Frontend>(channel);
217 frontend->BufferUsage();
218 ASSERT_TRUE(result == "");
219 if (!channel) {
220 channel = new ProtocolHandler(callback, ecmaVm);
221 }
222 auto frontend1 = std::make_unique<TracingImpl::Frontend>(channel);
223 frontend1->BufferUsage();
224 if (channel) {
225 delete channel;
226 channel = nullptr;
227 }
228 ASSERT_TRUE(result.find("Tracing.BufferUsage") != std::string::npos);
229 }
230
231
HWTEST_F_L0(TracingImplTest,FrontendDataCollectedTest)232 HWTEST_F_L0(TracingImplTest, FrontendDataCollectedTest)
233 {
234 std::string result = "";
235 std::function<void(const void*, const std::string &)> callback =
236 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
237 ProtocolChannel *channel = nullptr;
238 auto frontend = std::make_unique<TracingImpl::Frontend>(channel);
239 frontend->DataCollected();
240 ASSERT_TRUE(result == "");
241 if (!channel) {
242 channel = new ProtocolHandler(callback, ecmaVm);
243 }
244 auto frontend1 = std::make_unique<TracingImpl::Frontend>(channel);
245 frontend1->DataCollected();
246 if (channel) {
247 delete channel;
248 channel = nullptr;
249 }
250 ASSERT_TRUE(result.find("Tracing.DataCollected") != std::string::npos);
251 }
252
HWTEST_F_L0(TracingImplTest,FrontendTracingCompleteTest)253 HWTEST_F_L0(TracingImplTest, FrontendTracingCompleteTest)
254 {
255 std::string result = "";
256 std::function<void(const void*, const std::string &)> callback =
257 [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
258 ProtocolChannel *channel = nullptr;
259 auto frontend = std::make_unique<TracingImpl::Frontend>(channel);
260 frontend->TracingComplete();
261 ASSERT_TRUE(result == "");
262 if (!channel) {
263 channel = new ProtocolHandler(callback, ecmaVm);
264 }
265 auto frontend1 = std::make_unique<TracingImpl::Frontend>(channel);
266 frontend1->TracingComplete();
267 if (channel) {
268 delete channel;
269 channel = nullptr;
270 }
271 ASSERT_TRUE(result.find("Tracing.TracingComplete") != std::string::npos);
272 }
273 } // namespace panda::test