1 /*
2 * Copyright (c) 2023 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 "netstack_log.h"
17 #include "gtest/gtest.h"
18 #include <csignal>
19 #include <cstring>
20 #include <functional>
21 #include <iostream>
22
23 #include "net_websocket.h"
24 #include "net_websocket_type.h"
25
26 class WebSocketTest : public testing::Test {
27 public:
SetUpTestCase()28 static void SetUpTestCase() {}
29
TearDownTestCase()30 static void TearDownTestCase() {}
31
SetUp()32 virtual void SetUp() {}
33
TearDown()34 virtual void TearDown() {}
35 };
36
37 namespace {
38 using namespace testing::ext;
39
OnOpen(struct WebSocket *client, WebSocket_OpenResult openResult)40 static void OnOpen(struct WebSocket *client, WebSocket_OpenResult openResult) {}
41
OnMessage(struct WebSocket *client, char *data, uint32_t length)42 static void OnMessage(struct WebSocket *client, char *data, uint32_t length) {}
43
OnError(struct WebSocket *client, WebSocket_ErrorResult error)44 static void OnError(struct WebSocket *client, WebSocket_ErrorResult error) {}
45
OnClose(struct WebSocket *client, WebSocket_CloseResult closeResult)46 static void OnClose(struct WebSocket *client, WebSocket_CloseResult closeResult) {}
47
HWTEST_F(WebSocketTest, WebSocketConstruct001, TestSize.Level1)48 HWTEST_F(WebSocketTest, WebSocketConstruct001, TestSize.Level1)
49 {
50 int32_t ret;
51 struct WebSocket *client = new WebSocket();
52 const char *url1 = "www.baidu.com";
53 struct WebSocket_Header header1;
54 header1.fieldName = "Content-Type";
55 header1.fieldValue = "application/json";
56 header1.next = nullptr;
57 client = OH_WebSocketClient_Constructor(OnOpen, OnMessage, OnError, OnClose);
58 ret = OH_WebSocketClient_AddHeader(client, header1);
59 OH_WebSocketClient_Connect(client, url1, client->requestOptions);
60 OH_WebSocketClient_Destroy(client);
61 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_OK);
62 }
63
HWTEST_F(WebSocketTest, WebSocketAddHeader002, TestSize.Level1)64 HWTEST_F(WebSocketTest, WebSocketAddHeader002, TestSize.Level1)
65 {
66 int32_t ret;
67 struct WebSocket *client = nullptr;
68 struct WebSocket_Header header1;
69 header1.fieldName = "Content-Type";
70 header1.fieldValue = "application/json";
71 header1.next = nullptr;
72 ret = OH_WebSocketClient_AddHeader(client, header1);
73 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_CLIENT_NULL);
74 }
75
HWTEST_F(WebSocketTest, WebSocketConnect003, TestSize.Level1)76 HWTEST_F(WebSocketTest, WebSocketConnect003, TestSize.Level1)
77 {
78 int32_t ret;
79 struct WebSocket *client = new WebSocket();
80 const char *url1 = "www.baidu.com";
81 ret = OH_WebSocketClient_Connect(client, url1, client->requestOptions);
82 OH_WebSocketClient_Destroy(client);
83 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_CLIENT_NOT_CREATED);
84 }
85
HWTEST_F(WebSocketTest, WebSocketSend004, TestSize.Level1)86 HWTEST_F(WebSocketTest, WebSocketSend004, TestSize.Level1)
87 {
88 int32_t ret;
89 struct WebSocket *client = nullptr;
90 const char *senddata = "Hello, I love China!";
91 int sendLength = std::strlen(senddata);
92 ret = OH_WebSocketClient_Send(client, const_cast<char *>(senddata), sendLength);
93 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_CLIENT_NULL);
94 }
95
HWTEST_F(WebSocketTest, WebSocketClose005, TestSize.Level1)96 HWTEST_F(WebSocketTest, WebSocketClose005, TestSize.Level1)
97 {
98 int32_t ret;
99 struct WebSocket *client = nullptr;
100 struct WebSocket_CloseOption CloseOption;
101 CloseOption.code = 1000;
102 CloseOption.reason = " ";
103 ret = OH_WebSocketClient_Close(client, CloseOption);
104 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_CLIENT_NULL);
105 }
106
HWTEST_F(WebSocketTest, WebSocketDestroy006, TestSize.Level1)107 HWTEST_F(WebSocketTest, WebSocketDestroy006, TestSize.Level1)
108 {
109 int32_t ret;
110 struct WebSocket *client = new WebSocket();
111 ret = OH_WebSocketClient_Destroy(client);
112 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_CLIENT_NOT_CREATED);
113 }
114
HWTEST_F(WebSocketTest, WebSocketSendTest007, TestSize.Level1)115 HWTEST_F(WebSocketTest, WebSocketSendTest007, TestSize.Level1)
116 {
117 int32_t ret;
118 const char *url1 = "www.baidu.com";
119 struct WebSocket *client = new WebSocket();
120 struct WebSocket_Header header1;
121 const char *senddata = "Hello, world!";
122 int sendLength = std::strlen(senddata);
123 const char senddata2[10] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x00, 0x17, 0x19, 0x19};
124
125 header1.fieldName = "Content-Type";
126 header1.fieldValue = "application/json";
127 header1.next = nullptr;
128
129 client = OH_WebSocketClient_Constructor(OnOpen, OnMessage, OnError, OnClose);
130 ret = OH_WebSocketClient_AddHeader(client, header1);
131 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_OK);
132 OH_WebSocketClient_Connect(client, url1, client->requestOptions);
133 ret = OH_WebSocketClient_Send(client, nullptr, 0);
134 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_SEND_DATA_NULL);
135 ret = OH_WebSocketClient_Send(client, const_cast<char *>(senddata), 0);
136 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_OK);
137 ret = OH_WebSocketClient_Send(client, const_cast<char *>(senddata), sendLength);
138 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_OK);
139 ret = OH_WebSocketClient_Send(client, const_cast<char *>(senddata2), sizeof(senddata2));
140 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_OK);
141 ret = OH_WebSocketClient_Destroy(client);
142 EXPECT_EQ(ret, WebSocket_ErrCode::WEBSOCKET_OK);
143 }
144
145 } // namespace