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