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 19using namespace panda::ecmascript; 20using namespace panda::ecmascript::tooling; 21 22namespace panda::test { 23class DispatcherTest : public testing::Test { 24public: 25 static void SetUpTestCase() 26 { 27 GTEST_LOG_(INFO) << "SetUpTestCase"; 28 } 29 30 static void TearDownTestCase() 31 { 32 GTEST_LOG_(INFO) << "TearDownCase"; 33 } 34 35 void SetUp() override 36 { 37 TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope); 38 } 39 40 void TearDown() override 41 { 42 TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope); 43 } 44 45protected: 46 EcmaVM *ecmaVm {nullptr}; 47 EcmaHandleScope *scope {nullptr}; 48 JSThread *thread {nullptr}; 49}; 50 51HWTEST_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 94HWTEST_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 102HWTEST_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 110HWTEST_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 129HWTEST_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} // namespace panda::test