1 /*
2 * Copyright (c) 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 #define LOG_TAG "ServiceUtilsTest"
16 #include <chrono>
17 
18 #include "access_token.h"
19 #include <endian.h>
20 #include "gtest/gtest.h"
21 #include "ipc_skeleton.h"
22 #include "log_print.h"
23 #include "utils/block_integer.h"
24 #include "utils/constant.h"
25 #include "utils/converter.h"
26 #include "utils/ref_count.h"
27 #include "utils/endian_converter.h"
28 using namespace testing::ext;
29 using namespace OHOS::DistributedData;
30 namespace OHOS::Test {
31 class ServiceUtilsTest : public testing::Test {
32 public:
33     static constexpr uint16_t HOST_VALUE16 = 0x1234;
34     static constexpr uint16_t NET_VALUE16 = 0x3412;
35     static constexpr uint32_t HOST_VALUE32 = 0x12345678;
36     static constexpr uint32_t NET_VALUE32 = 0x78563412;
37     static constexpr uint64_t HOST_VALUE64 = 0x1234567890ABCDEF;
38     static constexpr uint64_t NET_VALUE64 = 0xEFCDAB8967452301;
SetUpTestCase(void)39     static void SetUpTestCase(void){};
TearDownTestCase(void)40     static void TearDownTestCase(void){};
SetUp()41     void SetUp(){};
TearDown()42     void TearDown(){};
43 };
44 
45 class BlockIntegerTest : public testing::Test {
46 public:
SetUpTestCase(void)47     static void SetUpTestCase(void){};
TearDownTestCase(void)48     static void TearDownTestCase(void){};
SetUp()49     void SetUp(){};
TearDown()50     void TearDown(){};
GetCurrentTime()51     static uint64_t GetCurrentTime()
52     {
53         return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>
54                 (std::chrono::system_clock::now().time_since_epoch()).count());
55     }
56     int intervalTime = 100 * 1000;
57     int testNum = 10;
58 };
59 
60 class RefCountTest : public testing::Test {
61 public:
SetUpTestCase(void)62     static void SetUpTestCase(void){};
TearDownTestCase(void)63     static void TearDownTestCase(void){};
SetUp()64     void SetUp(){};
TearDown()65     void TearDown(){};
66 };
67 
68 /**
69  * @tc.name: StoreMetaDataConvertToStoreInfo
70  * @tc.desc: Storemeta data convert to storeinfo.
71  * @tc.type: FUNC
72  */
73 HWTEST_F(ServiceUtilsTest, StoreMetaDataConvertToStoreInfo, TestSize.Level2)
74 {
75     ZLOGI("ServiceUtilsTest StoreMetaDataConvertToStoreInfo begin.");
76     StoreMetaData metaData;
77     metaData.bundleName = "ohos.test.demo1";
78     metaData.storeId = "test_storeId";
79     metaData.uid = OHOS::IPCSkeleton::GetCallingUid();
80     metaData.tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
81     CheckerManager::StoreInfo info = Converter::ConvertToStoreInfo(metaData);
82     EXPECT_EQ(info.uid, metaData.uid);
83     EXPECT_EQ(info.tokenId, metaData.tokenId);
84     EXPECT_EQ(info.bundleName, metaData.bundleName);
85     EXPECT_EQ(info.storeId, metaData.storeId);
86 }
87 
88 
89 /**
90 * @tc.name: SymbolOverloadingTest
91 * @tc.desc: Symbol Overloading
92 * @tc.type: FUNC
93 * @tc.require:
94 * @tc.author:
95 */
96 HWTEST_F(BlockIntegerTest, SymbolOverloadingTest, TestSize.Level2)
97 {
98     ZLOGI("BlockIntegerTest SymbolOverloading begin.");
99     int interval = intervalTime;
100     BlockInteger blockInteger(interval);
101     blockInteger = testNum;
102     ASSERT_EQ(blockInteger, testNum);
103 
104     auto now1 = GetCurrentTime();
105     int val = blockInteger++;
106     auto now2 = GetCurrentTime();
107     ASSERT_EQ(val, 10);
108     ASSERT_EQ(blockInteger, 11);
109     ASSERT_TRUE(now2 - now1 >= interval);
110 
111     now1 = GetCurrentTime();
112     val = ++blockInteger;
113     now2 = GetCurrentTime();
114     ASSERT_EQ(val, 12);
115     ASSERT_EQ(blockInteger, 12);
116     ASSERT_TRUE(now2 - now1 >= interval);
117     ASSERT_TRUE(blockInteger < 20);
118     int result = static_cast<int>(blockInteger);
119     EXPECT_EQ(result, 12);
120 }
121 
122 /**
123 * @tc.name: ConstrucTortest
124 * @tc.desc:  Create Object.
125 * @tc.type: FUNC
126 * @tc.require:
127 * @tc.author:
128 */
129 HWTEST_F(RefCountTest, Constructortest, TestSize.Level2)
130 {
__anon20e15c150102() 131     std::function<void()> action = []() { };
132     RefCount refCountWithAction(action);
133     EXPECT_TRUE(refCountWithAction);
134 
135     std::function<void()> actions;
136     RefCount refCountWithActions(actions);
137     EXPECT_TRUE(refCountWithActions);
138     int num = 0;
139     {
__anon20e15c150202() 140         RefCount refCount([&num]() {
141             num += 10;
142         });
143         ASSERT_TRUE(refCount);
144 
145         RefCount refCount1(refCount);
146         ASSERT_TRUE(refCount1);
147 
148         RefCount refCount2(std::move(refCount));
149         ASSERT_TRUE(refCount2);
150 
151         RefCount refCount3 = refCount1;
152         ASSERT_TRUE(refCount3);
153 
154         RefCount refCount4 = std::move(refCount2);
155         ASSERT_TRUE(refCount4);
156         ASSERT_EQ(num, 0);
157         EXPECT_TRUE(static_cast<bool>(refCount4));
158 
159         RefCount refCount5 = refCount1;
160         refCount5 = refCount3;
161         ASSERT_TRUE(refCount5);
162 
163         RefCount refCount6 = std::move(refCount2);
164         refCount6 = std::move(refCount4);
165         ASSERT_TRUE(refCount6);
166 
167         EXPECT_TRUE(refCount5 = refCount5);
168     }
169     ASSERT_EQ(num, 10);
170 }
171 
172 /**
173 * @tc.name: HostToNet
174 * @tc.desc: test endian_converter HostToNet function.
175 * @tc.type: FUNC
176 * @tc.require:
177 * @tc.author: SQL
178 */
179 HWTEST_F(ServiceUtilsTest, HostToNet, TestSize.Level1)
180 {
181     uint16_t netValue16 = HostToNet(HOST_VALUE16);
182     EXPECT_EQ(netValue16, htole16(HOST_VALUE16));
183 
184     uint16_t hostValue16 = NetToHost(NET_VALUE16);
185     EXPECT_EQ(hostValue16, le16toh(NET_VALUE16));
186 
187     uint32_t netValue32 = HostToNet(HOST_VALUE32);
188     EXPECT_EQ(netValue32, htole32(HOST_VALUE32));
189 
190     uint32_t hostValue32 = NetToHost(NET_VALUE32);
191     EXPECT_EQ(hostValue32, le32toh(NET_VALUE32));
192 
193     uint64_t netValue64 = HostToNet(HOST_VALUE64);
194     EXPECT_EQ(netValue64, htole64(HOST_VALUE64));
195 
196     uint64_t hostValue64 = NetToHost(NET_VALUE64);
197     EXPECT_EQ(hostValue64, le64toh(NET_VALUE64));
198 }
199 
200 /**
201 * @tc.name: DCopy
202 * @tc.desc: test Constant::DCopy function.
203 * @tc.type: FUNC
204 * @tc.require:
205 * @tc.author: SQL
206 */
207 HWTEST_F(ServiceUtilsTest, DCopy, TestSize.Level1)
208 {
209     Constant constant;
210     uint8_t *tag = nullptr;
211     size_t tagLen = 1;
212     uint8_t *src = nullptr;
213     size_t srcLen = 0;
214 
215     uint8_t tags[10];
216     size_t tagsLen = 10;
217     uint8_t srcs[10];
218     size_t srcsLen = 10;
219 
220     EXPECT_FALSE(constant.DCopy(tag, tagLen, src, srcLen));
221     EXPECT_FALSE(constant.DCopy(tag, tagsLen, src, srcsLen));
222     EXPECT_FALSE(constant.DCopy(tags, tagLen, src, srcLen));
223     EXPECT_FALSE(constant.DCopy(tag, tagLen, srcs, srcLen));
224     EXPECT_FALSE(constant.DCopy(tags, tagsLen, src, srcsLen));
225     EXPECT_FALSE(constant.DCopy(tag, tagsLen, srcs, srcsLen));
226     EXPECT_FALSE(constant.DCopy(tags, tagLen, srcs, srcLen));
227     EXPECT_TRUE(constant.DCopy(tags, tagsLen, srcs, srcsLen));
228 }
229 } // namespace OHOS::Test