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 <set>
20 #include <thread>
21
22 #include "anonymous_string.h"
23 #include "dh_utils_tool.h"
24
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace DistributedHardware {
SetUpTestCase(void)29 void UtilsToolTest::SetUpTestCase(void)
30 {
31 }
32
TearDownTestCase(void)33 void UtilsToolTest::TearDownTestCase(void)
34 {
35 }
36
SetUp()37 void UtilsToolTest::SetUp()
38 {
39 }
40
TearDown()41 void UtilsToolTest::TearDown()
42 {
43 }
44
45 /**
46 * @tc.name: utils_tool_test_001
47 * @tc.desc: Verify the utils tool GetCurrentTime function
48 * @tc.type: FUNC
49 * @tc.require: AR000GHSK0
50 */
51 HWTEST_F(UtilsToolTest, utils_tool_test_001, TestSize.Level0)
52 {
53 int64_t time1 = GetCurrentTime();
54 using namespace std::chrono_literals;
55 std::this_thread::sleep_for(100ms);
56 int64_t time2 = GetCurrentTime();
57
58 EXPECT_LT(time1, time2);
59 }
60
61 /**
62 * @tc.name: utils_tool_test_002
63 * @tc.desc: Verify the utils tool GetRandomID function
64 * @tc.type: FUNC
65 * @tc.require: AR000GHSK0
66 */
67 HWTEST_F(UtilsToolTest, utils_tool_test_002, TestSize.Level0)
68 {
69 uint32_t roundCnt = 100;
70 std::set<std::string> idSet;
71
72 for (uint32_t i = 0; i < roundCnt; i++) {
73 idSet.insert(GetRandomID());
74 }
75
76 EXPECT_EQ(idSet.size(), roundCnt);
77 }
78
79 /**
80 * @tc.name: utils_tool_test_003
81 * @tc.desc: Verify the GetAnonyString function
82 * @tc.type: FUNC
83 * @tc.require: AR000GHSK0
84 */
85 HWTEST_F(UtilsToolTest, utils_tool_test_003, TestSize.Level0)
86 {
87 std::string str1 = "a";
88 std::string str2 = "ab";
89 std::string str3 = "abc";
90 std::string str4 = "abcd";
91 std::string str5 = "9a40932aff004e209e93524c6e35706b";
92
93 ASSERT_STRNE(str1.c_str(), GetAnonyString(str1).c_str());
94 ASSERT_STRNE(str2.c_str(), GetAnonyString(str2).c_str());
95 ASSERT_STRNE(str3.c_str(), GetAnonyString(str3).c_str());
96 ASSERT_STRNE(str4.c_str(), GetAnonyString(str4).c_str());
97 ASSERT_STRNE(str5.c_str(), GetAnonyString(str5).c_str());
98 }
99
100 /**
101 * @tc.name: utils_tool_test_004
102 * @tc.desc: Verify the GetAnnoyInt32 function
103 * @tc.type: FUNC
104 * @tc.require: AR000GHSK0
105 */
106 HWTEST_F(UtilsToolTest, utils_tool_test_004, TestSize.Level0)
107 {
108 int32_t i1 = 0;
109 int32_t i2 = 10;
110 int32_t i3 = 555;
111 int32_t i4 = 6666;
112 int32_t i5 = 88888;
113
114 ASSERT_STRNE(std::to_string(i1).c_str(), GetAnonyInt32(i1).c_str());
115 ASSERT_STRNE(std::to_string(i2).c_str(), GetAnonyInt32(i2).c_str());
116 ASSERT_STRNE(std::to_string(i3).c_str(), GetAnonyInt32(i3).c_str());
117 ASSERT_STRNE(std::to_string(i4).c_str(), GetAnonyInt32(i4).c_str());
118 ASSERT_STRNE(std::to_string(i5).c_str(), GetAnonyInt32(i5).c_str());
119 }
120 }
121 }