1 /*
2 * Copyright (c) 2022 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 "MetaDataTest"
16 #include <gtest/gtest.h>
17
18 #include "accesstoken_kit.h"
19 #include "bootstrap.h"
20 #include "device_manager_adapter.h"
21 #include "device_matrix.h"
22 #include "executor_pool.h"
23 #include "ipc_skeleton.h"
24 #include "kvdb_service_impl.h"
25 #include "kvstore_meta_manager.h"
26 #include "log_print.h"
27 #include "metadata/meta_data_manager.h"
28 #include "metadata/store_meta_data.h"
29 #include "metadata/store_meta_data_local.h"
30 #include "nativetoken_kit.h"
31 #include "token_setproc.h"
32 #include "directory/directory_manager.h"
33 using namespace testing::ext;
34 using namespace OHOS::DistributedData;
35 using namespace OHOS::Security::AccessToken;
36 using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter;
37 using Status = OHOS::DistributedKv::Status;
38 using Options = OHOS::DistributedKv::Options;
39 static OHOS::DistributedKv::StoreId storeId = { "meta_test_storeid" };
40 static OHOS::DistributedKv::AppId appId = { "ohos.test.metadata" };
41 static constexpr const char *TEST_USER = "0";
42 namespace OHOS::Test {
43 namespace DistributedDataTest {
44 class MetaDataTest : public testing::Test {
45 public:
46 static void SetUpTestCase(void);
47 static void TearDownTestCase(void);
48 void SetUp();
49 void TearDown();
50
51 protected:
52 StoreMetaData metaData_;
53 Options options_;
54 std::shared_ptr<DistributedKv::KVDBServiceImpl> kvdbServiceImpl_;
55 int32_t GetInstIndex(uint32_t tokenId, const DistributedKv::AppId &appId);
56 };
57
GetInstIndex(uint32_t tokenId,const DistributedKv::AppId & appId)58 int32_t MetaDataTest::GetInstIndex(uint32_t tokenId, const DistributedKv::AppId &appId)
59 {
60 if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
61 return 0;
62 }
63
64 HapTokenInfo tokenInfo;
65 tokenInfo.instIndex = -1;
66 int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo);
67 if (errCode != RET_SUCCESS) {
68 return -1;
69 }
70 return tokenInfo.instIndex;
71 }
72
GrantPermissionNative()73 void GrantPermissionNative()
74 {
75 const char **perms = new const char *[2];
76 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
77 perms[1] = "ohos.permission.ACCESS_SERVICE_DM";
78 TokenInfoParams infoInstance = {
79 .dcapsNum = 0,
80 .permsNum = 2,
81 .aclsNum = 0,
82 .dcaps = nullptr,
83 .perms = perms,
84 .acls = nullptr,
85 .processName = "distributed_data_test",
86 .aplStr = "system_basic",
87 };
88 uint64_t tokenId = GetAccessTokenId(&infoInstance);
89 SetSelfTokenID(tokenId);
90 AccessTokenKit::ReloadNativeTokenInfo();
91 delete []perms;
92 }
93
SetUpTestCase(void)94 void MetaDataTest::SetUpTestCase(void)
95 {
96 DistributedData::Bootstrap::GetInstance().LoadComponents();
97 DistributedData::Bootstrap::GetInstance().LoadDirectory();
98 DistributedData::Bootstrap::GetInstance().LoadCheckers();
99 GrantPermissionNative();
100
101 size_t max = 12;
102 size_t min = 5;
103 auto executors = std::make_shared<OHOS::ExecutorPool>(max, min);
104 DmAdapter::GetInstance().Init(executors);
105 DistributedKv::KvStoreMetaManager::GetInstance().BindExecutor(executors);
106 DistributedKv::KvStoreMetaManager::GetInstance().InitMetaParameter();
107 DistributedKv::KvStoreMetaManager::GetInstance().InitMetaListener();
108 }
109
TearDownTestCase()110 void MetaDataTest::TearDownTestCase() {}
111
SetUp()112 void MetaDataTest::SetUp()
113 {
114 options_.isNeedCompress = true;
115 kvdbServiceImpl_ = std::make_shared<DistributedKv::KVDBServiceImpl>();
116 metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
117 metaData_.bundleName = appId.appId;
118 metaData_.storeId = storeId.storeId;
119 metaData_.user = TEST_USER;
120 metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
121 metaData_.instanceId = GetInstIndex(metaData_.tokenId, appId);
122 metaData_.version = 1;
123 MetaDataManager::GetInstance().DelMeta(metaData_.GetKey());
124 metaData_.area = options_.area;
125 metaData_.dataDir = DirectoryManager::GetInstance().GetStorePath(metaData_);
126 }
127
TearDown()128 void MetaDataTest::TearDown() {}
129
130 /**
131 * @tc.name: SaveLoadMateData
132 * @tc.desc: save meta data
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author: yl
136 */
137 HWTEST_F(MetaDataTest, SaveLoadMateData, TestSize.Level0)
138 {
139 ZLOGI("SaveLoadMateData start");
140 StoreMetaData metaData;
141 std::vector<uint8_t> password {};
142 auto status = kvdbServiceImpl_->AfterCreate(appId, storeId, options_, password);
143 ASSERT_EQ(status, Status::SUCCESS);
144 ASSERT_TRUE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyWithoutPath(), metaData));
145 ASSERT_TRUE(metaData.isNeedCompress);
146 }
147
148 /**
149 * @tc.name: MetaDataChanged
150 * @tc.desc: meta data changed
151 * @tc.type: FUNC
152 * @tc.require:
153 * @tc.author: yl
154 */
155 HWTEST_F(MetaDataTest, MateDataChanged, TestSize.Level0)
156 {
157 ZLOGI("MateDataChangeed start");
158 options_.isNeedCompress = false;
159 StoreMetaData metaData;
160 std::vector<uint8_t> password {};
161 auto status = kvdbServiceImpl_->AfterCreate(appId, storeId, options_, password);
162 ASSERT_EQ(status, Status::SUCCESS);
163 ASSERT_TRUE(MetaDataManager::GetInstance().LoadMeta(metaData_.GetKeyWithoutPath(), metaData));
164 ASSERT_FALSE(metaData.isNeedCompress);
165 ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(metaData_.GetKeyWithoutPath()));
166 }
167 } // namespace DistributedDataTest
168 } // namespace OHOS::Test
169