• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/corrupt_reporter.h"
27 #include "utils/ref_count.h"
28 #include "utils/endian_converter.h"
29 using namespace testing::ext;
30 using namespace OHOS::DistributedData;
31 namespace OHOS::Test {
32 static constexpr const char *TEST_CORRUPT_PATH = "/data/service/el1/public/database/utils_test/";
33 static constexpr const char *TEST_CORRUPT_STOREID = "utils_test_store";
34 class ServiceUtilsTest : public testing::Test {
35 public:
36     static constexpr uint16_t HOST_VALUE16 = 0x1234;
37     static constexpr uint16_t NET_VALUE16 = 0x3412;
38     static constexpr uint32_t HOST_VALUE32 = 0x12345678;
39     static constexpr uint32_t NET_VALUE32 = 0x78563412;
40     static constexpr uint64_t HOST_VALUE64 = 0x1234567890ABCDEF;
41     static constexpr uint64_t NET_VALUE64 = 0xEFCDAB8967452301;
SetUpTestCase(void)42     static void SetUpTestCase(void){};
TearDownTestCase(void)43     static void TearDownTestCase(void){};
SetUp()44     void SetUp(){};
TearDown()45     void TearDown(){};
46 };
47 
48 class BlockIntegerTest : public testing::Test {
49 public:
SetUpTestCase(void)50     static void SetUpTestCase(void){};
TearDownTestCase(void)51     static void TearDownTestCase(void){};
SetUp()52     void SetUp(){};
TearDown()53     void TearDown(){};
GetCurrentTime()54     static uint64_t GetCurrentTime()
55     {
56         return static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::microseconds>
57                 (std::chrono::system_clock::now().time_since_epoch()).count());
58     }
59     int intervalTime = 100 * 1000;
60     int testNum = 10;
61 };
62 
63 class RefCountTest : public testing::Test {
64 public:
SetUpTestCase(void)65     static void SetUpTestCase(void){};
TearDownTestCase(void)66     static void TearDownTestCase(void){};
SetUp()67     void SetUp(){};
TearDown()68     void TearDown(){};
69 };
70 
71 /**
72  * @tc.name: StoreMetaDataConvertToStoreInfo
73  * @tc.desc: Storemeta data convert to storeinfo.
74  * @tc.type: FUNC
75  */
76 HWTEST_F(ServiceUtilsTest, StoreMetaDataConvertToStoreInfo, TestSize.Level2)
77 {
78     ZLOGI("ServiceUtilsTest StoreMetaDataConvertToStoreInfo begin.");
79     StoreMetaData metaData;
80     metaData.bundleName = "ohos.test.demo1";
81     metaData.storeId = "test_storeId";
82     metaData.uid = OHOS::IPCSkeleton::GetCallingUid();
83     metaData.tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
84     CheckerManager::StoreInfo info = Converter::ConvertToStoreInfo(metaData);
85     EXPECT_EQ(info.uid, metaData.uid);
86     EXPECT_EQ(info.tokenId, metaData.tokenId);
87     EXPECT_EQ(info.bundleName, metaData.bundleName);
88     EXPECT_EQ(info.storeId, metaData.storeId);
89 }
90 
91 
92 /**
93 * @tc.name: SymbolOverloadingTest
94 * @tc.desc: Symbol Overloading
95 * @tc.type: FUNC
96 * @tc.require:
97 * @tc.author:
98 */
99 HWTEST_F(BlockIntegerTest, SymbolOverloadingTest, TestSize.Level2)
100 {
101     ZLOGI("BlockIntegerTest SymbolOverloading begin.");
102     int interval = intervalTime;
103     BlockInteger blockInteger(interval);
104     blockInteger = testNum;
105     ASSERT_EQ(blockInteger, testNum);
106 
107     auto now1 = GetCurrentTime();
108     int val = blockInteger++;
109     auto now2 = GetCurrentTime();
110     ASSERT_EQ(val, 10);
111     ASSERT_EQ(blockInteger, 11);
112     ASSERT_TRUE(now2 - now1 >= interval);
113 
114     now1 = GetCurrentTime();
115     val = ++blockInteger;
116     now2 = GetCurrentTime();
117     ASSERT_EQ(val, 12);
118     ASSERT_EQ(blockInteger, 12);
119     ASSERT_TRUE(now2 - now1 >= interval);
120     ASSERT_TRUE(blockInteger < 20);
121     int result = static_cast<int>(blockInteger);
122     EXPECT_EQ(result, 12);
123 }
124 
125 /**
126 * @tc.name: ConstrucTortest
127 * @tc.desc:  Create Object.
128 * @tc.type: FUNC
129 * @tc.require:
130 * @tc.author:
131 */
132 HWTEST_F(RefCountTest, Constructortest, TestSize.Level2)
133 {
__anoneafa731d0102() 134     std::function<void()> action = []() { };
135     RefCount refCountWithAction(action);
136     EXPECT_TRUE(refCountWithAction);
137 
138     std::function<void()> actions;
139     RefCount refCountWithActions(actions);
140     EXPECT_TRUE(refCountWithActions);
141     int num = 0;
142     {
__anoneafa731d0202() 143         RefCount refCount([&num]() {
144             num += 10;
145         });
146         ASSERT_TRUE(refCount);
147 
148         RefCount refCount1(refCount);
149         ASSERT_TRUE(refCount1);
150 
151         RefCount refCount2(std::move(refCount));
152         ASSERT_TRUE(refCount2);
153 
154         RefCount refCount3 = refCount1;
155         ASSERT_TRUE(refCount3);
156 
157         RefCount refCount4 = std::move(refCount2);
158         ASSERT_TRUE(refCount4);
159         ASSERT_EQ(num, 0);
160         EXPECT_TRUE(static_cast<bool>(refCount4));
161 
162         RefCount refCount5 = refCount1;
163         refCount5 = refCount3;
164         ASSERT_TRUE(refCount5);
165 
166         RefCount refCount6 = std::move(refCount2);
167         refCount6 = std::move(refCount4);
168         ASSERT_TRUE(refCount6);
169 
170         EXPECT_TRUE(refCount5 = refCount5);
171     }
172     ASSERT_EQ(num, 10);
173 }
174 
175 /**
176 * @tc.name: HostToNet
177 * @tc.desc: test endian_converter HostToNet function.
178 * @tc.type: FUNC
179 * @tc.require:
180 * @tc.author: SQL
181 */
182 HWTEST_F(ServiceUtilsTest, HostToNet, TestSize.Level1)
183 {
184     uint16_t netValue16 = HostToNet(HOST_VALUE16);
185     EXPECT_EQ(netValue16, htole16(HOST_VALUE16));
186 
187     uint16_t hostValue16 = NetToHost(NET_VALUE16);
188     EXPECT_EQ(hostValue16, le16toh(NET_VALUE16));
189 
190     uint32_t netValue32 = HostToNet(HOST_VALUE32);
191     EXPECT_EQ(netValue32, htole32(HOST_VALUE32));
192 
193     uint32_t hostValue32 = NetToHost(NET_VALUE32);
194     EXPECT_EQ(hostValue32, le32toh(NET_VALUE32));
195 
196     uint64_t netValue64 = HostToNet(HOST_VALUE64);
197     EXPECT_EQ(netValue64, htole64(HOST_VALUE64));
198 
199     uint64_t hostValue64 = NetToHost(NET_VALUE64);
200     EXPECT_EQ(hostValue64, le64toh(NET_VALUE64));
201 }
202 
203 /**
204 * @tc.name: DCopy
205 * @tc.desc: test Constant::DCopy function.
206 * @tc.type: FUNC
207 * @tc.require:
208 * @tc.author: SQL
209 */
210 HWTEST_F(ServiceUtilsTest, DCopy, TestSize.Level1)
211 {
212     Constant constant;
213     uint8_t *tag = nullptr;
214     size_t tagLen = 1;
215     uint8_t *src = nullptr;
216     size_t srcLen = 0;
217 
218     uint8_t tags[10];
219     size_t tagsLen = 10;
220     uint8_t srcs[10];
221     size_t srcsLen = 10;
222 
223     EXPECT_FALSE(constant.DCopy(tag, tagLen, src, srcLen));
224     EXPECT_FALSE(constant.DCopy(tag, tagsLen, src, srcsLen));
225     EXPECT_FALSE(constant.DCopy(tags, tagLen, src, srcLen));
226     EXPECT_FALSE(constant.DCopy(tag, tagLen, srcs, srcLen));
227     EXPECT_FALSE(constant.DCopy(tags, tagsLen, src, srcsLen));
228     EXPECT_FALSE(constant.DCopy(tag, tagsLen, srcs, srcsLen));
229     EXPECT_FALSE(constant.DCopy(tags, tagLen, srcs, srcLen));
230     EXPECT_TRUE(constant.DCopy(tags, tagsLen, srcs, srcsLen));
231 }
232 
233 /**
234 * @tc.name: CorruptTest001
235 * @tc.desc: test CorruptReporter function with invalid parameter.
236 * @tc.type: FUNC
237 * @tc.require:
238 * @tc.author: yanhui
239 */
240 HWTEST_F(ServiceUtilsTest, CorruptTest001, TestSize.Level1)
241 {
242     ASSERT_EQ(false, CorruptReporter::CreateCorruptedFlag(TEST_CORRUPT_PATH, ""));
243     ASSERT_EQ(false, CorruptReporter::CreateCorruptedFlag("", TEST_CORRUPT_STOREID));
244     ASSERT_EQ(false, CorruptReporter::CreateCorruptedFlag(TEST_CORRUPT_PATH, TEST_CORRUPT_STOREID));
245     ASSERT_EQ(false, CorruptReporter::HasCorruptedFlag(TEST_CORRUPT_PATH, ""));
246     ASSERT_EQ(false, CorruptReporter::HasCorruptedFlag("", TEST_CORRUPT_STOREID));
247     ASSERT_EQ(false, CorruptReporter::DeleteCorruptedFlag(TEST_CORRUPT_PATH, ""));
248     ASSERT_EQ(false, CorruptReporter::DeleteCorruptedFlag("", TEST_CORRUPT_STOREID));
249     ASSERT_EQ(false, CorruptReporter::DeleteCorruptedFlag(TEST_CORRUPT_PATH, TEST_CORRUPT_STOREID));
250 }
251 
252 /**
253 * @tc.name: CorruptTest002
254 * @tc.desc: test CorruptReporter function with normal parameter.
255 * @tc.type: FUNC
256 * @tc.require:
257 * @tc.author: yanhui
258 */
259 HWTEST_F(ServiceUtilsTest, CorruptTest002, TestSize.Level1)
260 {
261     mode_t mode = S_IRWXU | S_IRWXG | S_IXOTH; // 0771
262     auto ret = mkdir(TEST_CORRUPT_PATH, mode);
263     ASSERT_EQ(0, ret);
264     ASSERT_EQ(true, CorruptReporter::CreateCorruptedFlag(TEST_CORRUPT_PATH, TEST_CORRUPT_STOREID));
265     ASSERT_EQ(true, CorruptReporter::CreateCorruptedFlag(TEST_CORRUPT_PATH, TEST_CORRUPT_STOREID));
266     ASSERT_EQ(true, CorruptReporter::HasCorruptedFlag(TEST_CORRUPT_PATH, TEST_CORRUPT_STOREID));
267     ASSERT_EQ(true, CorruptReporter::DeleteCorruptedFlag(TEST_CORRUPT_PATH, TEST_CORRUPT_STOREID));
268     ret = rmdir(TEST_CORRUPT_PATH);
269     ASSERT_EQ(0, ret);
270 }
271 } // namespace OHOS::Test
272