1 /*
2 * Copyright (c) 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 <fcntl.h>
17 #include <cstring>
18
19 #include "gtest/gtest.h"
20 #include "lnn_ip_utils_adapter.h"
21 #include "bus_center_adapter.h"
22 #include "softbus_adapter_file.h"
23 #include "softbus_adapter_mem.h"
24 #include "softbus_errcode.h"
25 #include "softbus_adapter_log.h"
26
27 using namespace std;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 const char *g_FileName = "example.txt";
32
33 class DsoftbusOtherTest : public testing::Test {
34 protected:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39 };
SetUpTestCase(void)40 void DsoftbusOtherTest::SetUpTestCase(void)
41 {
42 }
TearDownTestCase(void)43 void DsoftbusOtherTest::TearDownTestCase(void)
44 {
45 int32_t ret = remove(g_FileName);
46 if (ret == 0) {
47 return;
48 }
49 }
SetUp(void)50 void DsoftbusOtherTest::SetUp(void)
51 {
52 }
TearDown(void)53 void DsoftbusOtherTest::TearDown(void)
54 {
55 }
56
57 /*
58 * @tc.name: GetNetworkIpByIfName001
59 * @tc.desc: ifName is illegal
60 * @tc.type: FUNC
61 * @tc.require: 1
62 */
63 HWTEST_F(DsoftbusOtherTest, GetNetworkIpByIfName001, TestSize.Level0)
64 {
65 const char *ifName = "abcdefgh";
66 char netmask[] = "abcdefd";
67 char ip[32] = "0";
68 int32_t len = 10;
69 int32_t ret = GetNetworkIpByIfName(ifName, ip, netmask, len);
70 EXPECT_EQ(SOFTBUS_ERR, ret);
71 }
72
73 /*
74 * @tc.name: GetNetworkIpByIfName002
75 * @tc.desc: ifName is nullptr
76 * @tc.type: FUNC
77 * @tc.require: 1
78 */
79 HWTEST_F(DsoftbusOtherTest, GetNetworkIpByIfName002, TestSize.Level0)
80 {
81 const char *ifName = "abcdefgh";
82 char netmask[] = "abcdefd";
83 char ip[32] = "0";
84 int32_t len = 10;
85 int32_t ret = GetNetworkIpByIfName(NULL, ip, netmask, len);
86 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
87
88 ret = GetNetworkIpByIfName(ifName, NULL, netmask, len);
89 EXPECT_EQ(SOFTBUS_INVALID_PARAM, ret);
90 }
91
92 /*
93 * @tc.name: GetNetworkIpByIfName003
94 * @tc.desc: netmask is nullptr
95 * @tc.type: FUNC
96 * @tc.require: 1
97 */
98 HWTEST_F(DsoftbusOtherTest, GetNetworkIpByIfName003, TestSize.Level0)
99 {
100 const char *ifName = "abcdefgh";
101 char ip[32] = "0";
102 int32_t len = 10;
103 int32_t ret = GetNetworkIpByIfName(ifName, ip, NULL, len);
104 EXPECT_EQ(SOFTBUS_ERR, ret);
105 }
106
107 /**
108 * @tc.name: SoftBusAdapter_ReadFullFileTest_001
109 * @tc.desc: Read File
110 * @tc.type: FUNC
111 * @tc.require: 1
112 */
113 HWTEST_F(DsoftbusOtherTest, SoftBusReadFullFileTest001, TestSize.Level0)
114 {
115 const char *writeBuf = "abcdef";
116 char readbuf[1024] = {"\0"};
117 int32_t maxLen = 100;
118 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, strlen(writeBuf));
119 EXPECT_EQ(SOFTBUS_OK, ret);
120 ret = SoftBusReadFullFile(g_FileName, readbuf, maxLen);
121 EXPECT_EQ(SOFTBUS_OK, ret);
122 }
123
124 /**
125 * @tc.name: SoftBusAdapter_ReadFullFileTest_002
126 * @tc.desc: g_FileName is null
127 * @tc.type: FUNC
128 * @tc.require: 1
129 */
130 HWTEST_F(DsoftbusOtherTest, SoftBusReadFullFileTest002, TestSize.Level0)
131 {
132 char readbuf[1024] = {"\0"};
133 int32_t maxLen = 100;
134 int32_t ret = SoftBusReadFullFile(nullptr, readbuf, maxLen);
135 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
136
137 ret = SoftBusReadFullFile(g_FileName, nullptr, maxLen);
138 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
139 }
140
141 /**
142 * @tc.name: SoftBusAdapter_ReadFullFileTest_003
143 * @tc.desc: maxLen is ivaild param
144 * @tc.type: FUNC
145 * @tc.require: 1
146 */
147 HWTEST_F(DsoftbusOtherTest, SoftBusReadFullFileTest003, TestSize.Level0)
148 {
149 char readbuf[1024] = {"\0"};
150 int32_t maxLen = 0;
151 int32_t ret = SoftBusReadFullFile(g_FileName, readbuf, maxLen);
152 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
153 }
154
155 /**
156 * @tc.name: SoftBusAdapter_WriterFileTest_001
157 * @tc.desc: writeBuf isn't nullptr
158 * @tc.type: FUNC
159 * @tc.require: 1
160 */
161 HWTEST_F(DsoftbusOtherTest, SoftBusWriterFileTest001, TestSize.Level0)
162 {
163 const char *writeBuf = "abcdef";
164 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, strlen(writeBuf));
165 EXPECT_EQ(SOFTBUS_OK, ret);
166 }
167
168 /**
169 * @tc.name: SoftBusAdapter_WriterFileTest_002
170 * @tc.desc: g_FileName and writeBuf is null
171 * @tc.type: FUNC
172 * @tc.require: 1
173 */
174 HWTEST_F(DsoftbusOtherTest, SoftBusWriterFileTest002, TestSize.Level0)
175 {
176 const char *writeBuf = "abcdef";
177 int32_t ret = SoftBusWriteFile(nullptr, writeBuf, strlen(writeBuf));
178 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
179
180 ret = SoftBusWriteFile(g_FileName, nullptr, strlen(writeBuf));
181 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
182 }
183
184 /**
185 * @tc.name: SoftBusAdapter_WriterFileTest_003
186 * @tc.desc: len is illegal
187 * @tc.type: FUNC
188 * @tc.require: 1
189 */
190 HWTEST_F(DsoftbusOtherTest, SoftBusWriterFileTest003, TestSize.Level0)
191 {
192 const char *writeBuf = "abcdef";
193 int32_t len = 0;
194 int32_t ret = SoftBusWriteFile(g_FileName, writeBuf, len);
195 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
196
197 int32_t len1 = -10;
198 ret = SoftBusWriteFile(g_FileName, writeBuf, len1);
199 EXPECT_EQ(SOFTBUS_FILE_ERR, ret);
200 }
201
202 /**
203 * @tc.name: SoftBusAdapter_MallocTest_001
204 * @tc.desc: size is zero
205 * @tc.type: FUNC
206 * @tc.require: 1
207 */
208 HWTEST_F(DsoftbusOtherTest, SoftBusMallocTest001, TestSize.Level0)
209 {
210 void *ret = SoftBusMalloc(0);
211 EXPECT_TRUE(ret != NULL);
212 SoftBusFree(ret);
213 }
214
215 /**
216 * @tc.name: SoftBusAdapter_MallocTest_002
217 * @tc.desc: size is MAX_MALLOC_SIZE+1
218 * @tc.type: FUNC
219 * @tc.require: 1
220 */
221 HWTEST_F(DsoftbusOtherTest, SoftBusMallocTest002, TestSize.Level0)
222 {
223 void *ret = SoftBusMalloc(MAX_MALLOC_SIZE + 1);
224 EXPECT_EQ(NULL, ret);
225 }
226
227 /**
228 * @tc.name: SoftBusAdapter_MallocTest_003
229 * @tc.desc: size is -1
230 * @tc.type: FUNC
231 * @tc.require: 1
232 */
233 HWTEST_F(DsoftbusOtherTest, SoftBusMallocTest003, TestSize.Level0)
234 {
235 void *ret = SoftBusMalloc(-1);
236 EXPECT_EQ(NULL, ret);
237 }
238
239 /**
240 * @tc.name: SoftBusAdapter_MallocTest_004
241 * @tc.desc: size is MAX_MALLOC_SIZE
242 * @tc.type: FUNC
243 * @tc.require: 1
244 */
245 HWTEST_F(DsoftbusOtherTest, SoftBusMallocTest004, TestSize.Level0)
246 {
247 void *ret = SoftBusMalloc(12);
248 EXPECT_TRUE(ret != NULL);
249 SoftBusFree(ret);
250 }
251
252 /**
253 * @tc.name: SoftBusAdapter_FreeTest_001
254 * @tc.desc: malloc size is 256
255 * @tc.type: FUNC
256 * @tc.require: 1
257 */
258 HWTEST_F(DsoftbusOtherTest, SoftBusFreeTest001, TestSize.Level0)
259 {
260 void *ret = SoftBusMalloc(256);
261 EXPECT_TRUE(ret != NULL);
262 SoftBusFree(ret);
263 }
264
265 /**
266 * @tc.name: SoftBusAdapter_CallocTest_001
267 * @tc.desc: calloc size is zero
268 * @tc.type: FUNC
269 * @tc.require: 1
270 */
271 HWTEST_F(DsoftbusOtherTest, SoftBusCallocTest001, TestSize.Level0)
272 {
273 void *ret = SoftBusCalloc(0);
274 EXPECT_TRUE(ret != NULL);
275 SoftBusFree(ret);
276 }
277
278 /**
279 * @tc.name: SoftBusAdapter_CallocTest_002
280 * @tc.desc: calloc size is 22
281 * @tc.type: FUNC
282 * @tc.require: 1
283 */
284 HWTEST_F(DsoftbusOtherTest, SoftBusCallocTest002, TestSize.Level0)
285 {
286 void *ret = SoftBusCalloc(22);
287 EXPECT_TRUE(ret != NULL);
288 SoftBusFree(ret);
289 }
290
291 /**
292 * @tc.name: SoftBusAdapter_CallocTest_003
293 * @tc.desc: calloc size is 256
294 * @tc.type: FUNC
295 * @tc.require: 1
296 */
297 HWTEST_F(DsoftbusOtherTest, SoftBusCallocTest003, TestSize.Level0)
298 {
299 void *ret = SoftBusCalloc(-1);
300 EXPECT_EQ(NULL, ret);
301 }
302
303 /**
304 * @tc.name: SoftBusAdapter_CallocTest_004
305 * @tc.desc: calloc size is 256
306 * @tc.type: FUNC
307 * @tc.require: 1
308 */
309 HWTEST_F(DsoftbusOtherTest, SoftBusCallocTest004, TestSize.Level0)
310 {
311 void *ret = SoftBusCalloc(MAX_MALLOC_SIZE + 1);
312 EXPECT_EQ(NULL, ret);
313 }
314
315 /**
316 * @tc.name: SoftBusAdapter_OutPrintTest_001
317 * @tc.desc: OutPrintLog is SOFTBUS_LOG_DBG
318 * @tc.type: FUNC
319 * @tc.require: 1
320 */
321 HWTEST_F(DsoftbusOtherTest, SoftBusOutPrintTest001, TestSize.Level0)
322 {
323 const char buf[20] = "123";
324 SoftBusOutPrint(buf, SOFTBUS_LOG_DBG);
325 }
326
327 /**
328 * @tc.name: SoftBusAdapter_OutPrintTest_002
329 * @tc.desc: OutPrintLog is SOFTBUS_LOG_INFO
330 * @tc.type: FUNC
331 * @tc.require: 1
332 */
333 HWTEST_F(DsoftbusOtherTest, SoftBusOutPrintTest002, TestSize.Level0)
334 {
335 const char buf[20] = "123";
336 SoftBusOutPrint(buf, SOFTBUS_LOG_INFO);
337 }
338
339 /**
340 * @tc.name: SoftBusAdapter_OutPrintTest_003
341 * @tc.desc: OutPrintLog is SOFTBUS_LOG_WARN
342 * @tc.type: FUNC
343 * @tc.require: 1
344 */
345 HWTEST_F(DsoftbusOtherTest, SoftBusOutPrintTest003, TestSize.Level0)
346 {
347 const char buf[20] = "123";
348 SoftBusOutPrint(buf, SOFTBUS_LOG_WARN);
349 }
350
351 /**
352 * @tc.name: SoftBusAdapter_OutPrintTest_004
353 * @tc.desc: OutPrintLog is SOFTBUS_LOG_ERROR
354 * @tc.type: FUNC
355 * @tc.require: 1
356 */
357 HWTEST_F(DsoftbusOtherTest, SoftBusOutPrintTest004, TestSize.Level0)
358 {
359 const char buf[20] = "123";
360 SoftBusOutPrint(buf, SOFTBUS_LOG_ERROR);
361 }
362
363 /**
364 * @tc.name: SoftBusAdapter_OutPrintTest_005
365 * @tc.desc: OutPrintLog is SOFTBUS_LOG_LEVEL_MAX
366 * @tc.type: FUNC
367 * @tc.require: 1
368 */
369 HWTEST_F(DsoftbusOtherTest, SoftBusOutPrintTest005, TestSize.Level0)
370 {
371 const char buf[20] = "123";
372 SoftBusOutPrint(buf, SOFTBUS_LOG_LEVEL_MAX);
373 }
374 }