• 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 
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 ProtocolHandlerTest : 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(ProtocolHandlerTest,DispatchCommandTest)51 HWTEST_F_L0(ProtocolHandlerTest, DispatchCommandTest)
52 {
53     std::string result = "";
54     std::function<void(const void*, const std::string &)> callback =
55         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
56     auto protocol = std::make_unique<ProtocolHandler>(callback, ecmaVm);
57     std::string msg = std::string() + R"({"id":0,"method":"Tracing.Test","params":{}})";
58     protocol->DispatchCommand(msg + "");
59     protocol->ProcessCommand();
60     ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
61 }
62 
HWTEST_F_L0(ProtocolHandlerTest,GetDispatchStatusTest)63 HWTEST_F_L0(ProtocolHandlerTest, GetDispatchStatusTest)
64 {
65     std::string result = "";
66     std::function<void(const void*, const std::string &)> callback =
67         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
68     auto protocol = std::make_unique<ProtocolHandler>(callback, ecmaVm);
69     std::string msg = std::string() + R"({"id":0,"method":"Tracing.Test","params":{}})";
70     ProtocolHandler::DispatchStatus status = ProtocolHandler::DispatchStatus(protocol->GetDispatchStatus());
71     ASSERT_TRUE(status == ProtocolHandler::DispatchStatus::DISPATCHED);
72     protocol->DispatchCommand(msg + "");
73     status = ProtocolHandler::DispatchStatus(protocol->GetDispatchStatus());
74     ASSERT_TRUE(status == ProtocolHandler::DispatchStatus::UNKNOWN);
75 }
76 
HWTEST_F_L0(ProtocolHandlerTest,ProcessCommandTest)77 HWTEST_F_L0(ProtocolHandlerTest, ProcessCommandTest)
78 {
79     std::string result = "";
80     std::function<void(const void*, const std::string &)> callback =
81         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
82     auto protocol = std::make_unique<ProtocolHandler>(callback, ecmaVm);
83     std::string msg = std::string() + R"({"id":0,"method":"Tracing.Test","params":{}})";
84     protocol->ProcessCommand();
85     ASSERT_TRUE(result == "");
86     protocol->DispatchCommand(msg + "");
87     protocol->ProcessCommand();
88     ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
89 }
90 
HWTEST_F_L0(ProtocolHandlerTest,SendResponseTest)91 HWTEST_F_L0(ProtocolHandlerTest, SendResponseTest)
92 {
93     std::string result = "";
94     std::function<void(const void*, const std::string &)> callback =
95         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
96     auto protocol = std::make_unique<ProtocolHandler>(callback, ecmaVm);
97     std::string msg = std::string() + R"({"id":0,"method":"Tracing.Test","params":{}})";
98     DispatchRequest request(msg);
99     ResponseCode code = ResponseCode::NOK;
100     msg = "error";
101     DispatchResponse response = DispatchResponse::Create(code, msg);
102     PtBaseReturns returns;
103     protocol->SendResponse(request, response, returns);
104     ASSERT_TRUE(result == "{\"id\":0,\"result\":{\"code\":1,\"message\":\"error\"}}");
105     code = ResponseCode::OK;
106     DispatchResponse response1 = DispatchResponse::Create(code, msg);
107     protocol->SendResponse(request, response1, returns);
108     ASSERT_TRUE(result == "{\"id\":0,\"result\":{}}");
109 }
110 }  // namespace panda::test