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
16 #include <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include <memory>
19 #include <type_traits>
20
21 #include "include/security_manager_mock.h"
22 #include "include/store_util_mock.h"
23 #include "store_factory.h"
24
25 namespace OHOS::DistributedKv {
26 using namespace std;
27 using namespace testing;
28
29 static StoreId storeId = { "single_test" };
30 static AppId appId = { "rekey" };
31 static Options options = {
32 .encrypt = false,
33 .securityLevel = S1,
34 .area = EL1,
35 .kvStoreType = SINGLE_VERSION,
36 .baseDir = "/data/service/el1/public/database/rekey",
37 };
38
39 class StoreFactoryMockTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase();
43 void SetUp() override;
44 void TearDown() override;
45
46 public:
47 using DBManager = DistributedDB::KvStoreDelegateManager;
48 using DBPassword = SecurityManager::DBPassword;
49 static inline shared_ptr<StoreUtilMock> storeUtilMock = nullptr;
50 static inline shared_ptr<SecurityManagerMock> securityManagerMock = nullptr;
51 };
52
SetUp()53 void StoreFactoryMockTest::SetUp()
54 {
55 std::string baseDir = "/data/service/el1/public/database/rekey";
56 mkdir(baseDir.c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
57 }
58
TearDown()59 void StoreFactoryMockTest::TearDown()
60 {
61 (void)remove("/data/service/el1/public/database/rekey");
62 }
63
SetUpTestCase()64 void StoreFactoryMockTest::SetUpTestCase()
65 {
66 GTEST_LOG_(INFO) << "SetUpTestCase enter";
67 storeUtilMock = make_shared<StoreUtilMock>();
68 StoreUtilMock::storeUtil = storeUtilMock;
69 securityManagerMock = make_shared<SecurityManagerMock>();
70 BSecurityManager::securityManager = securityManagerMock;
71 }
72
TearDownTestCase()73 void StoreFactoryMockTest::TearDownTestCase()
74 {
75 GTEST_LOG_(INFO) << "TearDownTestCase enter";
76 StoreUtilMock::storeUtil = nullptr;
77 storeUtilMock = nullptr;
78 BSecurityManager::securityManager = nullptr;
79 securityManagerMock = nullptr;
80 }
81
82 /**
83 * @tc.name: RekeyRecover_001
84 * @tc.desc: Rekey recover test.
85 * @tc.type: FUNC
86 * @tc.require:
87 * @tc.author: cao zhijun
88 */
89 HWTEST_F(StoreFactoryMockTest, RekeyRecover_001, testing::ext::TestSize.Level1)
90 {
91 GTEST_LOG_(INFO) << "StoreFactoryMockTest-begin RekeyRecover_001";
92 try {
93 std::string path = options.GetDatabaseDir();
94 std::shared_ptr<DBManager> dbManager = std::make_shared<DBManager>(appId.appId, "default");
95 DBPassword dbPassword;
96
97 EXPECT_CALL(*storeUtilMock, IsFileExist(_)).WillOnce(Return(true));
98 EXPECT_CALL(*securityManagerMock, GetDBPassword(_, _, _)).WillOnce(Return(SecurityManager::DBPassword()));
99 EXPECT_CALL(*storeUtilMock, GetDBSecurity(_)).Times(1);
100 EXPECT_CALL(*storeUtilMock, GetDBIndexType(_)).Times(1);
101 EXPECT_CALL(*storeUtilMock, ConvertStatus(_)).WillOnce(Return(SUCCESS));
102 EXPECT_CALL(*storeUtilMock, Remove(_)).WillOnce(Return(true));
103 auto status = StoreFactory::GetInstance().RekeyRecover(storeId, path, dbPassword, dbManager, options);
104 EXPECT_TRUE(status == SUCCESS);
105
106 EXPECT_CALL(*storeUtilMock, IsFileExist(_)).WillOnce(Return(true)).WillOnce(Return(true));
107 EXPECT_CALL(*securityManagerMock, GetDBPassword(_, _, _)).Times(2);
108 EXPECT_CALL(*storeUtilMock, GetDBSecurity(_)).Times(2);
109 EXPECT_CALL(*storeUtilMock, GetDBIndexType(_)).Times(2);
110 EXPECT_CALL(*storeUtilMock, ConvertStatus(_)).WillOnce(Return(ERROR)).WillOnce(Return(ERROR));
111 status = StoreFactory::GetInstance().RekeyRecover(storeId, path, dbPassword, dbManager, options);
112 EXPECT_TRUE(status != SUCCESS);
113
114 EXPECT_CALL(*storeUtilMock, IsFileExist(_)).WillOnce(Return(true)).WillOnce(Return(false));
115 EXPECT_CALL(*securityManagerMock, GetDBPassword(_, _, _)).Times(1);
116 EXPECT_CALL(*storeUtilMock, GetDBSecurity(_)).Times(1);
117 EXPECT_CALL(*storeUtilMock, GetDBIndexType(_)).Times(1);
118 EXPECT_CALL(*storeUtilMock, ConvertStatus(_)).WillOnce(Return(ERROR));
119 status = StoreFactory::GetInstance().RekeyRecover(storeId, path, dbPassword, dbManager, options);
120 EXPECT_TRUE(status != SUCCESS);
121
122 EXPECT_CALL(*storeUtilMock, IsFileExist(_)).WillOnce(Return(false)).WillOnce(Return(true));
123 EXPECT_CALL(*securityManagerMock, GetDBPassword(_, _, _)).Times(1);
124 EXPECT_CALL(*storeUtilMock, GetDBSecurity(_)).Times(1);
125 EXPECT_CALL(*storeUtilMock, GetDBIndexType(_)).Times(1);
126 EXPECT_CALL(*storeUtilMock, ConvertStatus(_)).WillOnce(Return(SUCCESS));
127 EXPECT_CALL(*securityManagerMock, SaveDBPassword(_, _, _)).Times(1);
128 status = StoreFactory::GetInstance().RekeyRecover(storeId, path, dbPassword, dbManager, options);
129 EXPECT_TRUE(status == SUCCESS);
130 } catch (...) {
131 EXPECT_TRUE(false);
132 GTEST_LOG_(INFO) << "StoreFactoryMockTest-an exception occurred by GetSrcPath.";
133 }
134 GTEST_LOG_(INFO) << "StoreFactoryMockTest-end RekeyRecover_001";
135 }
136 } // namespace OHOS::DistributedKv