• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "utils_tool_test.h"
17 
18 #include <chrono>
19 #include <cstdint>
20 #include <gtest/gtest.h>
21 #include <set>
22 #include <string>
23 #include <thread>
24 
25 #include "anonymous_string.h"
26 #include "dh_utils_tool.h"
27 #include "dh_utils_hitrace.h"
28 #include "distributed_hardware_errno.h"
29 
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 namespace {
35     constexpr int32_t UUID_LENGTH = 257;
36     constexpr uint32_t JSON_SIZE = 40 * 1024 * 1024 + 1;
37     constexpr uint32_t KEY_SIZE = 257;
38     constexpr int32_t OS_TYPE = 10;
39 }
SetUpTestCase(void)40 void UtilsToolTest::SetUpTestCase(void)
41 {
42 }
43 
TearDownTestCase(void)44 void UtilsToolTest::TearDownTestCase(void)
45 {
46 }
47 
SetUp()48 void UtilsToolTest::SetUp()
49 {
50 }
51 
TearDown()52 void UtilsToolTest::TearDown()
53 {
54 }
55 
56 /**
57  * @tc.name: GetAnonyString_001
58  * @tc.desc: Verify the GetAnonyString function
59  * @tc.type: FUNC
60  * @tc.require: AR000GHSK0
61  */
62 HWTEST_F(UtilsToolTest, GetAnonyString_001, TestSize.Level0)
63 {
64     std::string value = "";
65     auto ret = GetAnonyString(value);
66     EXPECT_EQ("", ret);
67 
68     value = "11";
69     ret = GetAnonyString(value);
70     EXPECT_EQ("******", ret);
71 
72     value = "123456789";
73     ret = GetAnonyString(value);
74     EXPECT_EQ("1******9", ret);
75 
76     value = "111222333444555666777888999";
77     ret = GetAnonyString(value);
78     EXPECT_EQ("1112******8999", ret);
79 }
80 
81 /**
82  * @tc.name: GetAnonyInt32_001
83  * @tc.desc: Verify the GetAnnoyInt32 function
84  * @tc.type: FUNC
85  * @tc.require: AR000GHSK0
86  */
87 HWTEST_F(UtilsToolTest, GetAnonyInt32_001, TestSize.Level0)
88 {
89     int32_t value = 123456;
90     auto ret = GetAnonyInt32(value);
91     EXPECT_EQ("1*****", ret);
92 }
93 
94 /**
95  * @tc.name: GetUUIDByDm_001
96  * @tc.desc: Verify the GetUUIDBySoftBus function
97  * @tc.type: FUNC
98  * @tc.require: AR000GHSK0
99  */
100 HWTEST_F(UtilsToolTest, GetUUIDByDm_001, TestSize.Level0)
101 {
102     std::string networkId = "";
103     std::string ret = GetUUIDByDm(networkId);
104     EXPECT_EQ(0, ret.size());
105 }
106 
107 HWTEST_F(UtilsToolTest, GetUDIDByDm_001, TestSize.Level0)
108 {
109     std::string networkId = "";
110     std::string ret = GetUDIDByDm(networkId);
111     EXPECT_EQ(0, ret.size());
112 }
113 
114 /**
115  * @tc.name: GetDeviceIdByUUID_001
116  * @tc.desc: Verify the GetDeviceIdByUUID function
117  * @tc.type: FUNC
118  * @tc.require: AR000GHSK0
119  */
120 HWTEST_F(UtilsToolTest, GetDeviceIdByUUID_001, TestSize.Level0)
121 {
122     std::string uuidEmpty = "";
123     std::string ret = GetDeviceIdByUUID(uuidEmpty);
124     EXPECT_EQ(0, ret.size());
125 
126     std::string uuid(UUID_LENGTH, '1');
127     ret = GetDeviceIdByUUID(uuid);
128     EXPECT_EQ(0, ret.size());
129 }
130 
131 /**
132  * @tc.name: GetDeviceIdByUUID_002
133  * @tc.desc: Verify the GetDeviceIdByUUID function
134  * @tc.type: FUNC
135  * @tc.require: AR000GHSK0
136  */
137 HWTEST_F(UtilsToolTest, GetDeviceIdByUUID_002, TestSize.Level0)
138 {
139     DHType dhType = DHType::CAMERA;
140     DHQueryTraceStart(dhType);
141 
142     dhType = DHType::MAX_DH;
143     DHQueryTraceStart(dhType);
144 
145     std::string uuid = "bb536a637105409e904d4da78290ab1";
146     std::string ret = GetDeviceIdByUUID(uuid);
147     EXPECT_NE(0, ret.size());
148 }
149 
150 HWTEST_F(UtilsToolTest, IsUInt8_001, TestSize.Level0)
151 {
152     cJSON* jsonObj1 = cJSON_CreateObject();
153     ASSERT_TRUE(jsonObj1 != nullptr);
154     const std::string key = "int8_key";
155     cJSON_AddStringToObject(jsonObj1, key.c_str(), "int8_key_test");
156     cJSON *keyJson1 = cJSON_GetObjectItem(jsonObj1, key.c_str());
157     auto ret = IsUInt8(keyJson1);
158     EXPECT_EQ(false, ret);
159     cJSON_Delete(jsonObj1);
160 
161     cJSON* jsonObj2 = cJSON_CreateObject();
162     ASSERT_TRUE(jsonObj2 != nullptr);
163     cJSON_AddNumberToObject(jsonObj2, key.c_str(), 1);
164     cJSON *keyJson2 = cJSON_GetObjectItem(jsonObj2, key.c_str());
165     ret = IsUInt8(keyJson2);
166     EXPECT_EQ(true, ret);
167     cJSON_Delete(jsonObj2);
168 }
169 
170 HWTEST_F(UtilsToolTest, IsUInt16_001, TestSize.Level0)
171 {
172     cJSON* jsonObj1 = cJSON_CreateObject();
173     ASSERT_TRUE(jsonObj1 != nullptr);
174     const std::string key = "uint16_key";
175     cJSON_AddStringToObject(jsonObj1, key.c_str(), "uint16_key_test");
176     cJSON *keyJson1 = cJSON_GetObjectItem(jsonObj1, key.c_str());
177     auto ret = IsUInt16(keyJson1);
178     cJSON_Delete(jsonObj1);
179     EXPECT_EQ(false, ret);
180 
181     cJSON* jsonObj2 = cJSON_CreateObject();
182     ASSERT_TRUE(jsonObj2 != nullptr);
183     cJSON_AddNumberToObject(jsonObj2, key.c_str(), 1);
184     cJSON *keyJson2 = cJSON_GetObjectItem(jsonObj2, key.c_str());
185     ret = IsUInt16(keyJson2);
186     EXPECT_EQ(true, ret);
187     cJSON_Delete(jsonObj2);
188 }
189 
190 HWTEST_F(UtilsToolTest, IsUInt32_001, TestSize.Level0)
191 {
192     cJSON* jsonObj1 = cJSON_CreateObject();
193     ASSERT_TRUE(jsonObj1 != nullptr);
194     const std::string key = "uint32_key";
195     cJSON_AddStringToObject(jsonObj1, key.c_str(), "uint32_key_test");
196     cJSON *keyJson1 = cJSON_GetObjectItem(jsonObj1, key.c_str());
197     auto ret = IsUInt32(keyJson1);
198     EXPECT_EQ(false, ret);
199     cJSON_Delete(jsonObj1);
200 
201     cJSON* jsonObj2 = cJSON_CreateObject();
202     ASSERT_TRUE(jsonObj2 != nullptr);
203     cJSON_AddNumberToObject(jsonObj2, key.c_str(), 1);
204     cJSON *keyJson2 = cJSON_GetObjectItem(jsonObj2, key.c_str());
205     ret = IsUInt32(keyJson2);
206     EXPECT_EQ(true, ret);
207     cJSON_Delete(jsonObj2);
208 }
209 
210 HWTEST_F(UtilsToolTest, GetSysPara_001, TestSize.Level0)
211 {
212     char *key = nullptr;
213     bool value = false;
214     auto ret = GetSysPara(key, value);
215     EXPECT_EQ(false, ret);
216 }
217 
218 HWTEST_F(UtilsToolTest, IsJsonLengthValid_001, TestSize.Level0)
219 {
220     std::string inputJsonStr = "";
221     auto ret = IsJsonLengthValid(inputJsonStr);
222     EXPECT_EQ(false, ret);
223 
224     std::string jsonStr(JSON_SIZE, 'a');
225     ret = IsJsonLengthValid(jsonStr);
226     EXPECT_EQ(false, ret);
227 }
228 
229 HWTEST_F(UtilsToolTest, IsKeySizeValid_001, TestSize.Level0)
230 {
231     std::string inputKey = "";
232     auto ret = IsKeySizeValid(inputKey);
233     EXPECT_EQ(false, ret);
234 
235     std::string key(KEY_SIZE, 'a');
236     ret = IsKeySizeValid(key);
237     EXPECT_EQ(false, ret);
238 }
239 
240 HWTEST_F(UtilsToolTest, GetDeviceSystemType_001, TestSize.Level0)
241 {
242     std::string extraData = "";
243     auto ret = GetDeviceSystemType(extraData);
244     EXPECT_EQ(ERR_DH_FWK_INVALID_OSTYPE, ret);
245 }
246 
247 HWTEST_F(UtilsToolTest, GetDeviceSystemType_002, TestSize.Level0)
248 {
249     cJSON* jsonObj = cJSON_CreateObject();
250     ASSERT_TRUE(jsonObj != nullptr);
251     std::string key = "key";
252     cJSON_AddStringToObject(jsonObj, key.c_str(), "key_test");
253     char* cjson = cJSON_PrintUnformatted(jsonObj);
254     if (cjson == nullptr) {
255         cJSON_Delete(jsonObj);
256         return;
257     }
258     std::string extraData(cjson);
259     auto ret = GetDeviceSystemType(extraData);
260     cJSON_free(cjson);
261     cJSON_Delete(jsonObj);
262     EXPECT_EQ(ERR_DH_FWK_INVALID_OSTYPE, ret);
263 }
264 
265 HWTEST_F(UtilsToolTest, GetDeviceSystemType_003, TestSize.Level0)
266 {
267     cJSON* jsonObj = cJSON_CreateObject();
268     ASSERT_TRUE(jsonObj != nullptr);
269     const std::string key = "OS_TYPE";
270     cJSON_AddStringToObject(jsonObj, key.c_str(), "key_test");
271     char* cjson = cJSON_PrintUnformatted(jsonObj);
272     if (cjson == nullptr) {
273         cJSON_Delete(jsonObj);
274         return;
275     }
276     std::string extraData(cjson);
277     auto ret = GetDeviceSystemType(extraData);
278     cJSON_free(cjson);
279     cJSON_Delete(jsonObj);
280     EXPECT_EQ(ERR_DH_FWK_INVALID_OSTYPE, ret);
281 }
282 
283 HWTEST_F(UtilsToolTest, GetDeviceSystemType_004, TestSize.Level0)
284 {
285     cJSON* jsonObj = cJSON_CreateObject();
286     ASSERT_TRUE(jsonObj != nullptr);
287     const std::string key = "OS_TYPE";
288     cJSON_AddNumberToObject(jsonObj, key.c_str(), OS_TYPE);
289     char* cjson = cJSON_PrintUnformatted(jsonObj);
290     if (cjson == nullptr) {
291         cJSON_Delete(jsonObj);
292         return;
293     }
294     std::string extraData(cjson);
295     auto ret = GetDeviceSystemType(extraData);
296     cJSON_free(cjson);
297     cJSON_Delete(jsonObj);
298     EXPECT_EQ(OS_TYPE, ret);
299 }
300 } // namespace DistributedHardware
301 } // namespace OHOS
302