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
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
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
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
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
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
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 } // namespace