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