• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 
16 #include <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 #include "upgrade.h"
19 #include "access_token_mock.h"
20 #include "kv_store_delegate_manager.h"
21 #include "kv_store_nb_delegate_mock.h"
22 
23 using namespace OHOS::DistributedKv;
24 using namespace testing::ext;
25 using namespace testing;
26 using namespace std;
27 using namespace OHOS::Security::AccessToken;
28 using namespace DistributedDB;
29 
30 KvStoreNbDelegateMock g_nbDelegateMock;
31 
CloseKvStore(KvStoreDelegate * kvStore)32 DBStatus KvStoreDelegateManager::CloseKvStore(KvStoreDelegate *kvStore)
33 {
34     return DBStatus::NOT_SUPPORT;
35 }
36 
GetKvStore(const std::string & storeId,const KvStoreNbDelegate::Option & option,const std::function<void (DBStatus,KvStoreNbDelegate *)> & callback)37 void KvStoreDelegateManager::GetKvStore(const std::string &storeId, const KvStoreNbDelegate::Option &option,
38     const std::function<void(DBStatus, KvStoreNbDelegate *)> &callback)
39 {
40     DBStatus status = DBStatus::OK;
41     if (callback) {
42         callback(status, &g_nbDelegateMock);
43     }
44     return;
45 }
46 
47 namespace OHOS::Test {
48 namespace DistributedDataTest {
49 using DBStatus = DistributedDB::DBStatus;
50 namespace {
ExporterPwd(const DistributedKv::Upgrade::StoreMeta & storeMeta,DistributedKv::Upgrade::DBPassword & pwd)51 std::string ExporterPwd(const DistributedKv::Upgrade::StoreMeta &storeMeta,
52     DistributedKv::Upgrade::DBPassword &pwd)
53 {
54     if (storeMeta.user.empty()) {
55         return "";
56     }
57     return "testIt";
58 }
59 
cleanData(const DistributedKv::Upgrade::StoreMeta &)60 Status cleanData(const DistributedKv::Upgrade::StoreMeta &)
61 {
62     return Status::SUCCESS;
63 }
64 }
65 
66 class UpgradeMockTest : public testing::Test {
67 public:
68     using StoreMeta = OHOS::DistributedData::StoreMetaData;
69     static void SetUpTestCase(void);
70     static void TearDownTestCase(void);
SetUp()71     void SetUp() {}
TearDown()72     void TearDown() {}
73     static inline shared_ptr<AccessTokenKitMock> accessTokenKitMock = nullptr;
74 };
75 
SetUpTestCase(void)76 void UpgradeMockTest::SetUpTestCase(void)
77 {
78     accessTokenKitMock = make_shared<AccessTokenKitMock>();
79     BAccessTokenKit::accessTokenkit = accessTokenKitMock;
80 }
81 
TearDownTestCase(void)82 void UpgradeMockTest::TearDownTestCase(void)
83 {
84     BAccessTokenKit::accessTokenkit = nullptr;
85     accessTokenKitMock = nullptr;
86 }
87 
88 /**
89 * @tc.name: UpgradeMockTest001
90 * @tc.desc: Get encrypted uuid by Meta.
91 * @tc.type: FUNC
92 * @tc.require:
93 */
94 HWTEST_F(UpgradeMockTest, UpgradeMockTest001, TestSize.Level0)
95 {
96     StoreMeta meta;
97     meta.appId = "UpgradeMockTest";
98     meta.deviceId = "";
99     EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillOnce(Return(TOKEN_INVALID));
100     std::string uuid = Upgrade::GetInstance().GetEncryptedUuidByMeta(meta);
101     EXPECT_TRUE(uuid.empty());
102 
103     meta.appId = "UpgradeTest";
104     meta.deviceId = "";
105     EXPECT_CALL(*accessTokenKitMock, GetTokenTypeFlag(_)).WillRepeatedly(Return(TOKEN_HAP));
106     uuid = Upgrade::GetInstance().GetEncryptedUuidByMeta(meta);
107     EXPECT_TRUE(uuid.empty());
108 }
109 
110 /**
111 * @tc.name: UpgradeMockTest002
112 * @tc.desc: Get encrypted uuid by Meta.
113 * @tc.type: FUNC
114 * @tc.require:
115 */
116 HWTEST_F(UpgradeMockTest, UpgradeMockTest002, TestSize.Level0)
117 {
118     DistributedKv::Upgrade::StoreMeta old;
119     old.version = StoreMeta::UUID_CHANGED_TAG + 1,
120     old.dataDir = "/data/user/test";
121     old.user = "";
122     DistributedKv::Upgrade::StoreMeta meta;
123     meta.dataDir = "/data/user";
124     std::vector<uint8_t> pwd;
125     bool ret = Upgrade::GetInstance().RegisterExporter(0, ExporterPwd);
126     ret = Upgrade::GetInstance().RegisterCleaner(0, cleanData) && ret;
127     EXPECT_TRUE(ret);
128     Upgrade::DBStatus status = Upgrade::GetInstance().UpdateStore(old, meta, pwd);
129     EXPECT_TRUE(status == Upgrade::DBStatus::NOT_FOUND);
130 }
131 }
132 }