• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "dispatcher.h"
16 #include "ecmascript/tests/test_helper.h"
17 #include "protocol_handler.h"
18 
19 using namespace panda::ecmascript;
20 using namespace panda::ecmascript::tooling;
21 
22 namespace panda::test {
23 class DispatcherTest : public testing::Test {
24 public:
SetUpTestCase()25     static void SetUpTestCase()
26     {
27         GTEST_LOG_(INFO) << "SetUpTestCase";
28     }
29 
TearDownTestCase()30     static void TearDownTestCase()
31     {
32         GTEST_LOG_(INFO) << "TearDownCase";
33     }
34 
SetUp()35     void SetUp() override
36     {
37         TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
38     }
39 
TearDown()40     void TearDown() override
41     {
42         TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
43     }
44 
45 protected:
46     EcmaVM *ecmaVm {nullptr};
47     EcmaHandleScope *scope {nullptr};
48     JSThread *thread {nullptr};
49 };
50 
HWTEST_F_L0(DispatcherTest,DispatchRequestTest)51 HWTEST_F_L0(DispatcherTest, DispatchRequestTest)
52 {
53     std::string msg = "";
54     auto dispatchRequest = std::make_unique<DispatchRequest>(msg);
55     bool result = dispatchRequest->IsValid();
56     ASSERT_TRUE(!result);
57     msg = std::string() + R"({"id":0,"method":"Tracing.xxx","params":{}})";
58     auto dispatchRequest1 = std::make_unique<DispatchRequest>(msg);
59     result  = dispatchRequest1->IsValid();
60     ASSERT_TRUE(result);
61     msg = std::string() + R"("":0)";
62     auto dispatchRequest2 = std::make_unique<DispatchRequest>(msg);
63     result = dispatchRequest2->IsValid();
64     ASSERT_TRUE(!result);
65     msg = std::string() + R"({"method":"Tracing","params":{}})";
66     auto dispatchRequest3 = std::make_unique<DispatchRequest>(msg);
67     result = dispatchRequest3->IsValid();
68     msg = std::string() + R"({"id":0,"params":{}})";
69     auto dispatchRequest9 = std::make_unique<DispatchRequest>(msg);
70     result = dispatchRequest9->IsValid();
71     ASSERT_TRUE(!result);
72     msg = std::string() + R"({"id":0,"method":"Tracing","params":{}})";
73     auto dispatchRequest4 = std::make_unique<DispatchRequest>(msg);
74     result = dispatchRequest4->IsValid();
75     ASSERT_TRUE(!result);
76     msg = std::string() + R"({"id":0,"method":".Tracing","params":{}})";
77     auto dispatchRequest5 = std::make_unique<DispatchRequest>(msg);
78     result = dispatchRequest5->IsValid();
79     ASSERT_TRUE(!result);
80     msg = std::string() + R"({"id":0,"method":"Tracing.","params":{}})";
81     auto dispatchRequest6 = std::make_unique<DispatchRequest>(msg);
82     result = dispatchRequest6->IsValid();
83     ASSERT_TRUE(!result);
84     msg = std::string() + R"({"id":0,"method":"Tracing.end", "params":1})";
85     auto dispatchRequest7 = std::make_unique<DispatchRequest>(msg);
86     result = dispatchRequest7->IsValid();
87     ASSERT_TRUE(!result);
88     msg = std::string() + R"({"id":0,"method":"Tracing.end"})";
89     auto dispatchRequest8 = std::make_unique<DispatchRequest>(msg);
90     result = dispatchRequest8->IsValid();
91     ASSERT_TRUE(result);
92 }
93 
HWTEST_F_L0(DispatcherTest,IsValidTest)94 HWTEST_F_L0(DispatcherTest, IsValidTest)
95 {
96     std::string msg = std::string() + R"({"id":0,"method":"Tracing.end","params":{}})";
97     auto dispatchRequest = std::make_unique<DispatchRequest>(msg);
98     bool result = dispatchRequest->IsValid();
99     ASSERT_TRUE(result);
100 }
101 
HWTEST_F_L0(DispatcherTest,GetDomainTest)102 HWTEST_F_L0(DispatcherTest, GetDomainTest)
103 {
104     std::string msg = std::string() + R"({"id":0,"method":"Tracing.end","params":{}})";
105     auto dispatchRequest = std::make_unique<DispatchRequest>(msg);
106     std::string result = dispatchRequest->GetDomain();
107     ASSERT_TRUE(result == "Tracing");
108 }
109 
HWTEST_F_L0(DispatcherTest,DispatchResponseCreateTest)110 HWTEST_F_L0(DispatcherTest, DispatchResponseCreateTest)
111 {
112     ResponseCode code = ResponseCode::OK;
113     std::string msg = "";
114     DispatchResponse response = DispatchResponse::Create(code, msg);
115     ASSERT_TRUE(response.IsOk());
116     ASSERT_TRUE(response.GetMessage() == "");
117 
118     msg = "msg";
119     response = DispatchResponse::Create(msg);
120     ASSERT_TRUE(!response.IsOk());
121     ASSERT_TRUE(response.GetMessage() == "msg");
122 
123     std::optional<std::string> error;
124     response = DispatchResponse::Create(error);
125     ASSERT_TRUE(response.IsOk());
126     ASSERT_TRUE(response.GetMessage() == "");
127 }
128 
HWTEST_F_L0(DispatcherTest,DispatcherDispatchTest)129 HWTEST_F_L0(DispatcherTest, DispatcherDispatchTest)
130 {
131     std::string msg = "";
132     DispatchRequest dispatchRequest(msg);
133     msg = std::string() + R"({"id":0,"method":"Tracing.end","params":{}})";
134     DispatchRequest dispatchRequest1(msg);
135     msg = std::string() + R"({"id":0,"method":"Test.end","params":{}})";
136     DispatchRequest dispatchRequest2(msg);
137     std::string result = "";
138     std::function<void(const void*, const std::string &)> callback =
139         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
140     ProtocolChannel *channel =  new ProtocolHandler(callback, ecmaVm);
141     auto dispatcher = std::make_unique<Dispatcher>(ecmaVm, channel);
142     dispatcher->Dispatch(dispatchRequest);
143     ASSERT_TRUE(result == "");
144     result = "";
145     dispatcher->Dispatch(dispatchRequest1);
146     ASSERT_TRUE(result.find("\"id\":0,") != std::string::npos);
147     result = "";
148     dispatcher->Dispatch(dispatchRequest2);
149     ASSERT_TRUE(result == "");
150 }
151 
HWTEST_F_L0(DispatcherTest,SaveAllBreakpointsTest)152 HWTEST_F_L0(DispatcherTest, SaveAllBreakpointsTest)
153 {
154     ProtocolChannel *channel =  new ProtocolHandler(nullptr, ecmaVm);
155     auto dispatcher = std::make_unique<Dispatcher>(ecmaVm, channel);
156 
157     std::string result = "";
158     std::string msg = std::string() +
159         R"({
160             "id": 0,
161             "method": "Debugger.saveAllPossibleBreakpoints",
162             "params": {
163                 "locations": {
164                     "entry|entry|1.0.0|src/main/ets/pages/Index.ts": [{
165                         "lineNumber": 59,
166                         "columnNumber": 16
167                     }]
168                 }
169             }
170         })";
171     result = dispatcher->OperateDebugMessage(msg.c_str());
172     EXPECT_STREQ(result.c_str(),
173         R"({"id":0,"result":{"code":1,"message":"SaveAllPossibleBreakpoints: debugger agent is not enabled"}})");
174     if (channel) {
175         delete channel;
176         channel = nullptr;
177     }
178 }
179 
HWTEST_F_L0(DispatcherTest,RemoveBreakpointTest)180 HWTEST_F_L0(DispatcherTest, RemoveBreakpointTest)
181 {
182     ProtocolChannel *channel =  new ProtocolHandler(nullptr, ecmaVm);
183     auto dispatcher = std::make_unique<Dispatcher>(ecmaVm, channel);
184 
185     std::string result = "";
186     std::string msg = std::string() +
187         R"({
188             "id": 0,
189             "method": "Debugger.removeBreakpointsByUrl",
190             "params": {
191                 "url": "entry|entry|1.0.0|src/main/ets/pages/Index.ts"
192             }
193         })";
194     result = dispatcher->OperateDebugMessage(msg.c_str());
195     EXPECT_STREQ(result.c_str(),
196         R"({"id":0,"result":{"code":1,"message":"Unknown url"}})");
197     if (channel) {
198         delete channel;
199         channel = nullptr;
200     }
201 }
202 
HWTEST_F_L0(DispatcherTest,SetBreakpointTest)203 HWTEST_F_L0(DispatcherTest, SetBreakpointTest)
204 {
205     ProtocolChannel *channel =  new ProtocolHandler(nullptr, ecmaVm);
206     auto dispatcher = std::make_unique<Dispatcher>(ecmaVm, channel);
207 
208     std::string result = "";
209     std::string msg = std::string() +
210         R"({
211             "id": 0,
212             "method": "Debugger.getPossibleAndSetBreakpointByUrl",
213             "params": {
214                 "locations": [{
215                     "url": "entry|entry|1.0.0|src/main/ets/pages/Index.ts",
216                     "lineNumber": 59,
217                     "columnNumber": 16
218                 }]
219             }
220         })";
221     result = dispatcher->OperateDebugMessage(msg.c_str());
222     EXPECT_STREQ(result.c_str(),
223         R"({"id":0,"result":{"code":1,"message":"GetPossibleAndSetBreakpointByUrl: debugger agent is not enabled"}})");
224     if (channel) {
225         delete channel;
226         channel = nullptr;
227     }
228 }
229 
HWTEST_F_L0(DispatcherTest,OperateDebugMessageTest)230 HWTEST_F_L0(DispatcherTest, OperateDebugMessageTest)
231 {
232     ProtocolChannel *channel = new ProtocolHandler(nullptr, ecmaVm);
233     auto dispatcher = std::make_unique<Dispatcher>(ecmaVm, channel);
234     std::string result = "";
235 
236     std::string msg = R"({"id":0,"method":"Runtime.getProperties"})";
237     result = dispatcher->OperateDebugMessage(msg.c_str());
238     ASSERT_TRUE(result != "");
239     msg = R"({"id":0,"method":"Runtime.getProperties","params":{"objectId":"1"}})";
240     result = dispatcher->OperateDebugMessage(msg.c_str());
241     ASSERT_TRUE(result != "");
242 
243     std::string msg2 = R"({"id":0,"method":"Debugger.callFunctionOn"})";
244     result = dispatcher->OperateDebugMessage(msg2.c_str());
245     ASSERT_TRUE(result != "");
246     msg2 = std::string() + R"({"id":0,"method":"Debugger.callFunctionOn","params":{
247         "callFrameId":"0", "functionDeclaration":"test"}})";
248     result = dispatcher->OperateDebugMessage(msg2.c_str());
249     EXPECT_STREQ(result.c_str(), R"({"id":0,"result":{"code":1,"message":"Invalid callFrameId."}})");
250 
251     std::string msg3 = R"({"id":0,"method":"Debugger.evaluateOnCallFrame"})";
252     result = dispatcher->OperateDebugMessage(msg3.c_str());
253     ASSERT_TRUE(result != "");
254     msg3 = std::string() +
255         R"({
256             "id":0,
257             "method":"Debugger.evaluateOnCallFrame",
258             "params":{
259                 "callFrameId":"-1",
260                 "expression":"the expression"
261             }
262         })";
263     result = dispatcher->OperateDebugMessage(msg3.c_str());
264     EXPECT_STREQ(result.c_str(), R"({"id":0,"result":{"code":1,"message":"Invalid callFrameId."}})");
265     if (channel) {
266         delete channel;
267         channel = nullptr;
268     }
269 }
270 }  // namespace panda::test