• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 <cinttypes>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include <sys/time.h>
20 
21 #include "auth_log.h"
22 #include "auth_tcp_connection.c"
23 #include "auth_tcp_connection.h"
24 #include "softbus_error_code.h"
25 #include "softbus_socket.h"
26 
27 namespace OHOS {
28 using namespace testing::ext;
29 constexpr uint32_t TEST_DATA_LEN = 30;
30 
31 class AuthTcpConnectionTest : public testing::Test {
32 public:
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp();
36     void TearDown();
37 };
38 
SetUpTestCase()39 void AuthTcpConnectionTest::SetUpTestCase() { }
40 
TearDownTestCase()41 void AuthTcpConnectionTest::TearDownTestCase() { }
42 
SetUp()43 void AuthTcpConnectionTest::SetUp()
44 {
45     AUTH_LOGI(AUTH_TEST, "AuthTcpConnectionTest start.");
46 }
47 
TearDown()48 void AuthTcpConnectionTest::TearDown() { }
49 
50 /*
51  * @tc.name: UNPACK_SOCKET_PKT_TEST_001
52  * @tc.desc: unpack socket pkt test
53  * @tc.type: FUNC
54  * @tc.require:
55  */
56 HWTEST_F(AuthTcpConnectionTest, UNPACK_SOCKET_PKT_TEST_001, TestSize.Level1)
57 {
58     const uint8_t data[AUTH_PKT_HEAD_LEN] = { 0 };
59     uint32_t len = 1;
60     SocketPktHead head;
61     (void)memset_s(&head, sizeof(head), 0, sizeof(head));
62     int32_t ret = UnpackSocketPkt(data, len, &head);
63     EXPECT_TRUE(ret == SOFTBUS_NO_ENOUGH_DATA);
64     len = AUTH_PKT_HEAD_LEN;
65     ret = UnpackSocketPkt(data, len, &head);
66     EXPECT_TRUE(ret == SOFTBUS_OK);
67 }
68 
69 /*
70  * @tc.name: MODULE_TO_DATA_TYPE_TEST_001
71  * @tc.desc: module to data type test
72  * @tc.type: FUNC
73  * @tc.require:
74  */
75 HWTEST_F(AuthTcpConnectionTest, MODULE_TO_DATA_TYPE_TEST_001, TestSize.Level1)
76 {
77     int32_t module = MODULE_TRUST_ENGINE;
78     uint32_t ret = ModuleToDataType(module);
79     EXPECT_TRUE(ret == DATA_TYPE_DEVICE_ID);
80     module = MODULE_AUTH_SDK;
81     ret = ModuleToDataType(module);
82     EXPECT_TRUE(ret == DATA_TYPE_AUTH);
83     module = MODULE_AUTH_CONNECTION;
84     ret = ModuleToDataType(module);
85     EXPECT_TRUE(ret == DATA_TYPE_DEVICE_INFO);
86     module = MODULE_MESSAGE_SERVICE;
87     ret = ModuleToDataType(module);
88     EXPECT_TRUE(ret == DATA_TYPE_CONNECTION);
89 }
90 
91 /*
92  * @tc.name: RECV_PACKET_HEAD_TEST_001
93  * @tc.desc: recv packet head test
94  * @tc.type: FUNC
95  * @tc.require:
96  */
97 HWTEST_F(AuthTcpConnectionTest, RECV_PACKET_HEAD_TEST_001, TestSize.Level1)
98 {
99     int32_t fd = 0;
100     SocketPktHead pktHead;
101     (void)memset_s(&pktHead, sizeof(SocketPktHead), 0, sizeof(SocketPktHead));
102     int32_t ret = RecvPacketHead(AUTH, fd, &pktHead);
103     EXPECT_NE(ret, SOFTBUS_OK);
104 }
105 
106 /*
107  * @tc.name: RECV_PACKET_DATA_TEST_001
108  * @tc.desc: recv packet head test
109  * @tc.type: FUNC
110  * @tc.require:
111  */
112 HWTEST_F(AuthTcpConnectionTest, RECV_PACKET_DATA_TEST_001, TestSize.Level1)
113 {
114     int32_t fd = 0;
115     SocketPktHead pktHead;
116     uint8_t data[TEST_DATA_LEN] = { 0 };
117     (void)memset_s(&pktHead, sizeof(SocketPktHead), 0, sizeof(SocketPktHead));
118     pktHead.module = MODULE_AUTH_CHANNEL;
119     NotifyDataReceived(AUTH, fd, &pktHead, data);
120     pktHead.module = MODULE_AUTH_MSG;
121     NotifyDataReceived(AUTH, fd, &pktHead, data);
122     pktHead.module = MODULE_CONNECTION;
123     NotifyDataReceived(AUTH, fd, &pktHead, data);
124 
125     uint32_t len = TEST_DATA_LEN;
126     uint8_t *packetData = RecvPacketData(fd, len);
127     EXPECT_TRUE(packetData == nullptr);
128 }
129 
130 /*
131  * @tc.name: PROCESS_SOCKET_OUT_EVENT_TEST_001
132  * @tc.desc: process socket out event test
133  * @tc.type: FUNC
134  * @tc.require:
135  */
136 HWTEST_F(AuthTcpConnectionTest, PROCESS_SOCKET_OUT_EVENT_TEST_001, TestSize.Level1)
137 {
138     int32_t fd = 0;
139     bool isClient = true;
140     NotifyConnected(AUTH, fd, isClient);
141     NotifyDisconnected(fd);
142     StopSocketListening(AUTH);
143 
144     int32_t ret = ProcessSocketOutEvent(AUTH, fd);
145     EXPECT_NE(ret, SOFTBUS_OK);
146 }
147 
148 /*
149  * @tc.name: PROCESS_SOCKET_IN_EVENT_TEST_001
150  * @tc.desc: process socket in event test
151  * @tc.type: FUNC
152  * @tc.require:
153  */
154 HWTEST_F(AuthTcpConnectionTest, PROCESS_SOCKET_IN_EVENT_TEST_001, TestSize.Level1)
155 {
156     int32_t fd = 0;
157     int32_t channelId = 0;
158     SocketPktHead head;
159     const uint8_t data[TEST_DATA_LEN] = { 0 };
160     (void)memset_s(&head, sizeof(SocketPktHead), 0, sizeof(SocketPktHead));
161     NotifyChannelDataReceived(channelId, &head, data);
162     NotifyChannelDisconnected(channelId);
163 
164     int32_t ret = ProcessSocketInEvent(AUTH, fd);
165     EXPECT_NE(ret, SOFTBUS_OK);
166 }
167 
168 /*
169  * @tc.name: ON_CONNECT_EVENT_TEST_001
170  * @tc.desc: on connect event test
171  * @tc.type: FUNC
172  * @tc.require:
173  */
174 HWTEST_F(AuthTcpConnectionTest, ON_CONNECT_EVENT_TEST_001, TestSize.Level1)
175 {
176     ListenerModule module = AUTH_P2P;
177     int32_t cfd = -1;
178     ConnectOption clientAddr;
179     (void)memset_s(&clientAddr, sizeof(ConnectOption), 0, sizeof(ConnectOption));
180     int32_t ret = OnConnectEvent(module, cfd, &clientAddr);
181     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
182     cfd = 0;
183     ret = OnConnectEvent(module, cfd, &clientAddr);
184     EXPECT_NE(ret, SOFTBUS_OK);
185 }
186 
187 /*
188  * @tc.name: ON_DATA_EVENT_TEST_001
189  * @tc.desc: on data event test
190  * @tc.type: FUNC
191  * @tc.require:
192  */
193 HWTEST_F(AuthTcpConnectionTest, ON_DATA_EVENT_TEST_001, TestSize.Level1)
194 {
195     ListenerModule module = AUTH_P2P;
196     int32_t events = SOFTBUS_SOCKET_OUT;
197     int32_t fd = 0;
198     int32_t ret = OnDataEvent(module, events, fd);
199     EXPECT_NE(ret, SOFTBUS_OK);
200     events = SOFTBUS_SOCKET_IN;
201     ret = OnDataEvent(module, events, fd);
202     EXPECT_NE(ret, SOFTBUS_OK);
203     events = SOFTBUS_SOCKET_EXCEPTION;
204     ret = OnDataEvent(module, events, fd);
205     EXPECT_NE(ret, SOFTBUS_OK);
206 }
207 
208 /*
209  * @tc.name: START_SOCKET_LISTENING_TEST_001
210  * @tc.desc: start socket listening test
211  * @tc.type: FUNC
212  * @tc.require:
213  */
214 HWTEST_F(AuthTcpConnectionTest, START_SOCKET_LISTENING_TEST_001, TestSize.Level1)
215 {
216     LocalListenerInfo info = {
217         .type = CONNECT_TCP,
218         .socketOption = {
219             .addr = "192.168.12.1",
220             .port = 22,
221             .moduleId = AUTH,
222             .protocol = LNN_PROTOCOL_IP,
223         },
224     };
225     int32_t ret = StartSocketListening(AUTH, &info);
226     EXPECT_NE(ret, SOFTBUS_OK);
227 }
228 
229 /*
230  * @tc.name: SOCKET_GET_CONN_INFO_TEST_001
231  * @tc.desc: socket get conn info test
232  * @tc.type: FUNC
233  * @tc.require:
234  */
235 HWTEST_F(AuthTcpConnectionTest, SOCKET_GET_CONN_INFO_TEST_001, TestSize.Level1)
236 {
237     int32_t fd = 0;
238     AuthConnInfo connInfo;
239     bool isServer;
240     (void)memset_s(&connInfo, sizeof(AuthConnInfo), 0, sizeof(AuthConnInfo));
241     int32_t ret = SocketGetConnInfo(fd, nullptr, &isServer);
242     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
243     ret = SocketGetConnInfo(fd, &connInfo, nullptr);
244     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
245     ret = SocketGetConnInfo(fd, &connInfo, &isServer);
246     EXPECT_NE(ret, SOFTBUS_OK);
247 }
248 
249 /*
250  * @tc.name: SOCKET_CONNECT_INNER_TEST_001
251  * @tc.desc: SocketConnectInner test
252  * @tc.type: FUNC
253  * @tc.require:
254  */
255 HWTEST_F(AuthTcpConnectionTest, SOCKET_CONNECT_INNER_TEST_001, TestSize.Level1)
256 {
257     const char *localIp = "192.168.11.22";
258     const char *peerIp = "192.168.11.33";
259     int32_t ret = SocketConnectInner(nullptr, peerIp, 37025, AUTH, true);
260     EXPECT_TRUE(ret == AUTH_INVALID_FD);
261     ret = SocketConnectInner(localIp, nullptr, 37025, AUTH, true);
262     EXPECT_TRUE(ret == AUTH_INVALID_FD);
263     ret = SocketConnectInner(localIp, peerIp, 37025, AUTH, true);
264     EXPECT_TRUE(ret == SOFTBUS_CONN_SOCKET_GET_INTERFACE_ERR);
265 }
266 
267 /*
268  * @tc.name: NIP_SOCKET_CONNECT_DEVICE_TEST_001
269  * @tc.desc: NipSocketConnectDevice test
270  * @tc.type: FUNC
271  * @tc.require:
272  */
273 HWTEST_F(AuthTcpConnectionTest, NIP_SOCKET_CONNECT_DEVICE_TEST_001, TestSize.Level1)
274 {
275     const char *addr = "192.168.11.44";
276     int32_t ret = NipSocketConnectDevice(AUTH, addr, 37025, true);
277     EXPECT_TRUE(ret == AUTH_INVALID_FD);
278     ret = NipSocketConnectDevice(AUTH, nullptr, 37025, true);
279     EXPECT_TRUE(ret == AUTH_INVALID_FD);
280 }
281 
282 /*
283  * @tc.name: AUTH_OPEN_CHANNEL_WITH_ALL_IP_TEST_001
284  * @tc.desc: AuthOpenChannelWithAllIp test
285  * @tc.type: FUNC
286  * @tc.require:
287  */
288 HWTEST_F(AuthTcpConnectionTest, AUTH_OPEN_CHANNEL_WITH_ALL_IP_TEST_001, TestSize.Level1)
289 {
290     const char *localIp = "192.168.11.22";
291     const char *remoteIp = "192.168.11.33";
292     int32_t ret = AuthOpenChannelWithAllIp(localIp, remoteIp, 37025);
293     EXPECT_TRUE(ret == INVALID_CHANNEL_ID);
294     ret = AuthOpenChannelWithAllIp(nullptr, remoteIp, 37025);
295     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
296     ret = AuthOpenChannelWithAllIp(localIp, nullptr, 37025);
297     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
298     ret = AuthOpenChannelWithAllIp(localIp, remoteIp, 0);
299     EXPECT_TRUE(ret == SOFTBUS_INVALID_PARAM);
300 }
301 } // namespace OHOS
302