• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace DistributedHardware {
SetUpTestCase(void)32 void UtilsToolTest::SetUpTestCase(void)
33 {
34 }
35 
TearDownTestCase(void)36 void UtilsToolTest::TearDownTestCase(void)
37 {
38 }
39 
SetUp()40 void UtilsToolTest::SetUp()
41 {
42 }
43 
TearDown()44 void UtilsToolTest::TearDown()
45 {
46 }
47 
48 /**
49  * @tc.name: utils_tool_test_001
50  * @tc.desc: Verify the utils tool GetCurrentTime function
51  * @tc.type: FUNC
52  * @tc.require: AR000GHSK0
53  */
54 HWTEST_F(UtilsToolTest, utils_tool_test_001, TestSize.Level0)
55 {
56     int64_t time1 = GetCurrentTime();
57     using namespace std::chrono_literals;
58     std::this_thread::sleep_for(100ms);
59     int64_t time2 = GetCurrentTime();
60 
61     EXPECT_LT(time1, time2);
62 }
63 
64 /**
65  * @tc.name: utils_tool_test_002
66  * @tc.desc: Verify the utils tool GetRandomID function
67  * @tc.type: FUNC
68  * @tc.require: AR000GHSK0
69  */
70 HWTEST_F(UtilsToolTest, utils_tool_test_002, TestSize.Level0)
71 {
72     uint32_t roundCnt = 100;
73     std::set<std::string> idSet;
74 
75     for (uint32_t i = 0; i < roundCnt; i++) {
76         idSet.insert(GetRandomID());
77     }
78 
79     EXPECT_EQ(idSet.size(), roundCnt);
80 }
81 
82 /**
83  * @tc.name: utils_tool_test_003
84  * @tc.desc: Verify the GetAnonyString function
85  * @tc.type: FUNC
86  * @tc.require: AR000GHSK0
87  */
88 HWTEST_F(UtilsToolTest, utils_tool_test_003, TestSize.Level0)
89 {
90     std::string str1 = "a";
91     std::string str2 = "ab";
92     std::string str3 = "abc";
93     std::string str4 = "abcd";
94     std::string str5 = "9a40932aff004e209e93524c6e35706b";
95 
96     ASSERT_STRNE(str1.c_str(), GetAnonyString(str1).c_str());
97     ASSERT_STRNE(str2.c_str(), GetAnonyString(str2).c_str());
98     ASSERT_STRNE(str3.c_str(), GetAnonyString(str3).c_str());
99     ASSERT_STRNE(str4.c_str(), GetAnonyString(str4).c_str());
100     ASSERT_STRNE(str5.c_str(), GetAnonyString(str5).c_str());
101 }
102 
103 /**
104  * @tc.name: utils_tool_test_004
105  * @tc.desc: Verify the GetAnnoyInt32 function
106  * @tc.type: FUNC
107  * @tc.require: AR000GHSK0
108  */
109 HWTEST_F(UtilsToolTest, utils_tool_test_004, TestSize.Level0)
110 {
111     int32_t i1 = 0;
112     int32_t i2 = 10;
113     int32_t i3 = 555;
114     int32_t i4 = 6666;
115     int32_t i5 = 88888;
116 
117     ASSERT_STRNE(std::to_string(i1).c_str(), GetAnonyInt32(i1).c_str());
118     ASSERT_STRNE(std::to_string(i2).c_str(), GetAnonyInt32(i2).c_str());
119     ASSERT_STRNE(std::to_string(i3).c_str(), GetAnonyInt32(i3).c_str());
120     ASSERT_STRNE(std::to_string(i4).c_str(), GetAnonyInt32(i4).c_str());
121     ASSERT_STRNE(std::to_string(i5).c_str(), GetAnonyInt32(i5).c_str());
122 }
123 
124 /**
125  * @tc.name: utils_tool_test_005
126  * @tc.desc: Verify the GetUUIDBySoftBus function
127  * @tc.type: FUNC
128  * @tc.require: AR000GHSK0
129  */
130 HWTEST_F(UtilsToolTest, utils_tool_test_005, TestSize.Level0)
131 {
132     std::string networkId = "";
133     std::string ret = GetUUIDBySoftBus(networkId);
134     EXPECT_EQ(0, ret.size());
135 }
136 
137 /**
138  * @tc.name: utils_tool_test_006
139  * @tc.desc: Verify the GetDeviceIdByUUID function
140  * @tc.type: FUNC
141  * @tc.require: AR000GHSK0
142  */
143 HWTEST_F(UtilsToolTest, utils_tool_test_006, TestSize.Level0)
144 {
145     std::string uuidEmpty = "";
146     std::string ret = GetDeviceIdByUUID(uuidEmpty);
147     ASSERT_EQ(0, ret.size());
148 }
149 
150 /**
151  * @tc.name: utils_tool_test_007
152  * @tc.desc: Verify the GetDeviceIdByUUID function
153  * @tc.type: FUNC
154  * @tc.require: AR000GHSK0
155  */
156 HWTEST_F(UtilsToolTest, utils_tool_test_007, TestSize.Level0)
157 {
158     std::string uuid = "bb536a637105409e904d4da78290ab1";
159     std::string ret = GetDeviceIdByUUID(uuid);
160     ASSERT_NE(0, ret.size());
161 }
162 } // namespace DistributedHardware
163 } // namespace OHOS
164