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