1/* 2 * Copyright (c) 2023-2024 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 <csignal> 17#include <cstring> 18#include <functional> 19#include <iostream> 20 21#include "netstack_log.h" 22#include "gtest/gtest.h" 23#ifdef GTEST_API_ 24#define private public 25#endif 26#include "websocket_client_innerapi.h" 27 28class WebSocketTest : public testing::Test { 29public: 30 static void SetUpTestCase() {} 31 32 static void TearDownTestCase() {} 33 34 virtual void SetUp() {} 35 36 virtual void TearDown() {} 37}; 38 39namespace { 40using namespace testing::ext; 41using namespace OHOS::NetStack::WebSocketClient; 42static constexpr const size_t TEST_MAX_DATA_LENGTH = 5 * 1024 * 1024; 43static constexpr const size_t TEST_LENGTH = 1; 44 45OpenOptions openOptions; 46 47CloseOption closeOptions; 48 49static void OnMessage(WebSocketClient *client, const std::string &data, size_t length) {} 50 51static void OnOpen(WebSocketClient *client, OpenResult openResult) {} 52 53static void OnError(WebSocketClient *client, ErrorResult error) {} 54 55static void OnClose(WebSocketClient *client, CloseResult closeResult) {} 56 57WebSocketClient *client = new WebSocketClient(); 58 59HWTEST_F(WebSocketTest, WebSocketRegistcallback001, TestSize.Level1) 60{ 61 openOptions.headers["Content-Type"] = "application/json"; 62 openOptions.headers["Authorization"] = "Bearer your_token_here"; 63 closeOptions.code = LWS_CLOSE_STATUS_NORMAL; 64 closeOptions.reason = ""; 65 client->Registcallback(OnOpen, OnMessage, OnError, OnClose); 66 int32_t ret = client->Connect("www.baidu.com", openOptions); 67 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_CONNECTION_TO_SERVER_FAIL); 68} 69 70HWTEST_F(WebSocketTest, WebSocketConnect002, TestSize.Level1) 71{ 72 int32_t ret = 0; 73 openOptions.headers["Content-Type"] = "application/json"; 74 openOptions.headers["Authorization"] = "Bearer your_token_here"; 75 client->Registcallback(OnOpen, OnMessage, OnError, OnClose); 76 ret = client->Connect("www.baidu.com", openOptions); 77 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_CONNECTION_TO_SERVER_FAIL); 78} 79 80HWTEST_F(WebSocketTest, WebSocketSend003, TestSize.Level1) 81{ 82 int32_t ret; 83 const char *data = "Hello, world!"; 84 int32_t length = std::strlen(data); 85 client->Connect("www.baidu.com", openOptions); 86 ret = client->Send(const_cast<char *>(data), length); 87 EXPECT_EQ(ret, 0); 88} 89 90HWTEST_F(WebSocketTest, WebSocketClose004, TestSize.Level1) 91{ 92 const int32_t WEBSOCKET_NO_CONNECTION = 1017; 93 CloseOption CloseOptions; 94 CloseOptions.code = LWS_CLOSE_STATUS_NORMAL; 95 CloseOptions.reason = ""; 96 int32_t ret = client->Close(CloseOptions); 97 EXPECT_EQ(ret, WEBSOCKET_NO_CONNECTION); 98} 99 100HWTEST_F(WebSocketTest, WebSocketDestroy005, TestSize.Level1) 101{ 102 int32_t ret; 103 ret = client->Destroy(); 104 delete client; 105 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_ERROR_HAVE_NO_CONNECT_CONTEXT); 106} 107 108HWTEST_F(WebSocketTest, WebSocketBranchTest001, TestSize.Level1) 109{ 110 const char *data = "test data"; 111 char *testData = nullptr; 112 size_t length = 0; 113 int32_t ret = client->Send(testData, length); 114 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_SEND_DATA_NULL); 115 116 ret = client->Send(const_cast<char *>(data), TEST_MAX_DATA_LENGTH); 117 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_DATA_LENGTH_EXCEEDS); 118 119 CloseOption options; 120 options.reason = ""; 121 options.code = 0; 122 WebSocketClient *client = new WebSocketClient(); 123 EXPECT_TRUE(client->GetClientContext() != nullptr); 124 client->GetClientContext()->openStatus = TEST_LENGTH; 125 ret = client->Close(options); 126 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_NONE_ERR); 127 client->GetClientContext()->openStatus = 0; 128 ret = client->Close(options); 129 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_ERROR_HAVE_NO_CONNECT); 130} 131 132HWTEST_F(WebSocketTest, WebSocketBranchTest002, TestSize.Level1) 133{ 134 client->clientContext = nullptr; 135 const char *data = "test data"; 136 size_t length = 0; 137 int32_t ret = client->Send(const_cast<char *>(data), length); 138 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_ERROR_NO_CLIENTCONTEX); 139 140 CloseOption options; 141 ret = client->Close(options); 142 EXPECT_EQ(ret, WebSocketErrorCode::WEBSOCKET_ERROR_NO_CLIENTCONTEX); 143} 144} // namespace