1 /*
2 * Copyright (c) 2021-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
16 #include <gtest/gtest.h>
17
18 #include <memory>
19
20 // redefine private and protected since testcase need to invoke and test private function
21 #define private public
22 #define protected public
23 #include "client_socket.h"
24 #undef private
25 #undef protected
26
27 #include "securec.h"
28
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS::AppSpawn;
32
33 class ClientSocketTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase()41 void ClientSocketTest::SetUpTestCase()
42 {}
43
TearDownTestCase()44 void ClientSocketTest::TearDownTestCase()
45 {}
46
SetUp()47 void ClientSocketTest::SetUp()
48 {}
49
TearDown()50 void ClientSocketTest::TearDown()
51 {}
52
53 /*
54 * Feature: AppSpawn
55 * Function: ClientSocket
56 * SubFunction: CreateClient & ConnectSocket
57 * FunctionPoints: create client socket
58 * EnvConditions: mobile that can run ohos test framework
59 * CaseDescription: Verify although the client socket created success but don't create the server socket, the connect
60 * socket still fail.
61 */
62 HWTEST(ClientSocketTest, Client_Socket_001, TestSize.Level0)
63 {
64 GTEST_LOG_(INFO) << "Client_Socket_001 start";
65
66 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
67
68 EXPECT_EQ(-1, clientSocket->GetSocketFd());
69 EXPECT_EQ(0, clientSocket->CreateClient());
70 int32_t socketFd = clientSocket->GetSocketFd();
71 EXPECT_EQ(0, clientSocket->CreateClient());
72 EXPECT_EQ(socketFd, clientSocket->GetSocketFd());
73 EXPECT_EQ(-1, clientSocket->ConnectSocket());
74
75 GTEST_LOG_(INFO) << "Client_Socket_001 end";
76 }
77
78 /*
79 * Feature: AppSpawn
80 * Function: ClientSocket
81 * SubFunction: ConnectSocket
82 * FunctionPoints: connect socket
83 * EnvConditions: mobile that can run ohos test framework
84 * CaseDescription: Verify connect socket fail when don't create client socket.
85 */
86 HWTEST(ClientSocketTest, Client_Socket_002, TestSize.Level0)
87 {
88 GTEST_LOG_(INFO) << "Client_Socket_002 start";
89
90 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
91
92 EXPECT_EQ(-1, clientSocket->GetSocketFd());
93 EXPECT_EQ(-1, clientSocket->ConnectSocket());
94
95 GTEST_LOG_(INFO) << "Client_Socket_002 end";
96 }
97
98 /*
99 * Feature: AppSpawn
100 * Function: ClientSocket
101 * SubFunction: WriteSocketMessage
102 * FunctionPoints: write message
103 * EnvConditions: mobile that can run ohos test framework
104 * CaseDescription: Verify write message fail when don't create client socket.
105 */
106 HWTEST(ClientSocketTest, Client_Socket_003, TestSize.Level0)
107 {
108 GTEST_LOG_(INFO) << "Client_Socket_003 start";
109
110 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
111 std::string buff = "hiworld";
112
113 EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), buff.length()));
114
115 GTEST_LOG_(INFO) << "Client_Socket_003 end";
116 }
117
118 /*
119 * Feature: AppSpawn
120 * Function: ClientSocket
121 * SubFunction: WriteSocketMessage
122 * FunctionPoints: check params
123 * EnvConditions: mobile that can run ohos test framework
124 * CaseDescription: Verify the function WriteSocketMessage can check the invalid buffer pointer.
125 */
126 HWTEST(ClientSocketTest, Client_Socket_004, TestSize.Level0)
127 {
128 GTEST_LOG_(INFO) << "Client_Socket_004 start";
129
130 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
131 std::unique_ptr<uint8_t[]> buff = nullptr;
132 uint32_t len = 10;
133
134 EXPECT_EQ(0, clientSocket->CreateClient());
135 EXPECT_LE(0, clientSocket->GetSocketFd());
136 EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.get(), len));
137
138 GTEST_LOG_(INFO) << "Client_Socket_004 end";
139 }
140
141 /*
142 * Feature: AppSpawn
143 * Function: ClientSocket
144 * SubFunction: WriteSocketMessage
145 * FunctionPoints: check params
146 * EnvConditions: mobile that can run ohos test framework
147 * CaseDescription: Verify the function WriteSocketMessage can check the buffer length is 0.
148 */
149 HWTEST(ClientSocketTest, Client_Socket_005, TestSize.Level0)
150 {
151 GTEST_LOG_(INFO) << "Client_Socket_005 start";
152
153 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
154 std::string buff = "hiworld";
155 uint32_t len = 0;
156
157 EXPECT_EQ(0, clientSocket->CreateClient());
158 EXPECT_LE(0, clientSocket->GetSocketFd());
159 EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), len));
160
161 GTEST_LOG_(INFO) << "Client_Socket_005 end";
162 }
163
164 /*
165 * Feature: AppSpawn
166 * Function: ClientSocket
167 * SubFunction: WriteSocketMessage
168 * FunctionPoints: check params
169 * EnvConditions: mobile that can run ohos test framework
170 * CaseDescription: Verify the function WriteSocketMessage can check the buffer length < 0.
171 */
172 HWTEST(ClientSocketTest, Client_Socket_006, TestSize.Level0)
173 {
174 GTEST_LOG_(INFO) << "Client_Socket_006 start";
175
176 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
177 std::string buff = "hiworld";
178 uint32_t len = -1;
179
180 EXPECT_EQ(0, clientSocket->CreateClient());
181 EXPECT_LE(0, clientSocket->GetSocketFd());
182 EXPECT_EQ(-1, clientSocket->WriteSocketMessage(buff.c_str(), len));
183
184 GTEST_LOG_(INFO) << "Client_Socket_006 end";
185 }
186
187 /*
188 * Feature: AppSpawn
189 * Function: ClientSocket
190 * SubFunction: ReadSocketMessage
191 * FunctionPoints: read message
192 * EnvConditions: mobile that can run ohos test framework
193 * CaseDescription: Verify read message fail when don't create client socket.
194 */
195 HWTEST(ClientSocketTest, Client_Socket_007, TestSize.Level0)
196 {
197 GTEST_LOG_(INFO) << "Client_Socket_007 start";
198
199 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
200 int32_t len = 10;
201 std::unique_ptr<uint8_t[]> buff = std::make_unique<uint8_t[]>(len);
202
203 EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len));
204
205 GTEST_LOG_(INFO) << "Client_Socket_007 end";
206 }
207
208 /*
209 * Feature: AppSpawn
210 * Function: ClientSocket
211 * SubFunction: ReadSocketMessage
212 * FunctionPoints: check params
213 * EnvConditions: mobile that can run ohos test framework
214 * CaseDescription: Verify the function WriteSocketMessage can check the invalid buffer pointer.
215 */
216 HWTEST(ClientSocketTest, Client_Socket_008, TestSize.Level0)
217 {
218 GTEST_LOG_(INFO) << "Client_Socket_008 start";
219
220 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
221 int32_t len = 10;
222 std::unique_ptr<uint8_t[]> buff = nullptr;
223
224 EXPECT_EQ(0, clientSocket->CreateClient());
225 EXPECT_LE(0, clientSocket->GetSocketFd());
226 EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len));
227
228 GTEST_LOG_(INFO) << "Client_Socket_008 end";
229 }
230
231 /*
232 * Feature: AppSpawn
233 * Function: ClientSocket
234 * SubFunction: ReadSocketMessage
235 * FunctionPoints: check params
236 * EnvConditions: mobile that can run ohos test framework
237 * CaseDescription: Verify the function WriteSocketMessage can check the buffer length is 0.
238 */
239 HWTEST(ClientSocketTest, Client_Socket_009, TestSize.Level0)
240 {
241 GTEST_LOG_(INFO) << "Client_Socket_009 start";
242
243 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
244 int32_t len = 0;
245 std::unique_ptr<uint8_t[]> buff = std::make_unique<uint8_t[]>(10);
246
247 EXPECT_EQ(0, clientSocket->CreateClient());
248 EXPECT_LE(0, clientSocket->GetSocketFd());
249 EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len));
250
251 GTEST_LOG_(INFO) << "Client_Socket_009 end";
252 }
253
254 /*
255 * Feature: AppSpawn
256 * Function: ClientSocket
257 * SubFunction: ReadSocketMessage
258 * FunctionPoints: check params
259 * EnvConditions: mobile that can run ohos test framework
260 * CaseDescription: Verify the function WriteSocketMessage can check the buffer length < 0.
261 */
262 HWTEST(ClientSocketTest, Client_Socket_010, TestSize.Level0)
263 {
264 GTEST_LOG_(INFO) << "Client_Socket_010 start";
265
266 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
267 int32_t len = -1;
268 std::unique_ptr<uint8_t[]> buff = std::make_unique<uint8_t[]>(10);
269
270 EXPECT_EQ(0, clientSocket->CreateClient());
271 EXPECT_LE(0, clientSocket->GetSocketFd());
272 EXPECT_EQ(-1, clientSocket->ReadSocketMessage(buff.get(), len));
273
274 GTEST_LOG_(INFO) << "Client_Socket_010 end";
275 }
276
277 /*
278 * Feature: AppSpawn
279 * Function: ClientSocket
280 * SubFunction: GetSocketFd
281 * FunctionPoints: close the socket
282 * EnvConditions: mobile that can run ohos test framework
283 * CaseDescription: Verify the function CloseClient can close the socket which socket fd has created.
284 */
285 HWTEST(ClientSocketTest, Client_Socket_011, TestSize.Level0)
286 {
287 GTEST_LOG_(INFO) << "Client_Socket_011 start";
288
289 std::unique_ptr<ClientSocket> clientSocket = std::make_unique<ClientSocket>("ClientSocketTest");
290
291 EXPECT_EQ(-1, clientSocket->GetSocketFd());
292 EXPECT_EQ(0, clientSocket->CreateClient());
293 EXPECT_LE(0, clientSocket->GetSocketFd());
294 clientSocket->CloseClient();
295 EXPECT_EQ(-1, clientSocket->GetSocketFd());
296
297 GTEST_LOG_(INFO) << "Client_Socket_011 end";
298 }
299