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