• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "RdbSecurityManagerTest"
16 #include "rdb_security_manager.h"
17 
18 #include <block_data.h>
19 #include <gtest/gtest.h>
20 
21 #include <thread>
22 
23 #include "common.h"
24 #include "file_ex.h"
25 #include "rdb_errno.h"
26 #include "rdb_helper.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::NativeRdb;
30 namespace Test {
31 using KeyType = RdbSecurityManager::KeyFileType;
32 class RdbSecurityManagerTest : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38 
39 protected:
40     static constexpr const char *BUNDLE_NAME = "rdb.security.manager.test";
41     static constexpr const char *DB_FILE = "security_manager_test.db";
42     const std::string dbFile_ = RDB_TEST_PATH + DB_FILE;
43 };
44 
SetUpTestCase(void)45 void RdbSecurityManagerTest::SetUpTestCase(void)
46 {
47     RdbSecurityManager::GetInstance().Init(BUNDLE_NAME);
48 }
49 
TearDownTestCase(void)50 void RdbSecurityManagerTest::TearDownTestCase(void)
51 {
52 }
53 
SetUp(void)54 void RdbSecurityManagerTest::SetUp(void)
55 {
56 }
57 
TearDown(void)58 void RdbSecurityManagerTest::TearDown(void)
59 {
60 }
61 
62 class RdbStoreSecurityManagerTestOpenCallback : public RdbOpenCallback {
63 public:
OnCreate(RdbStore & store)64     int OnCreate(RdbStore &store) override
65     {
66         return E_OK;
67     }
OnUpgrade(RdbStore & store,int oldVersion,int newVersion)68     int OnUpgrade(RdbStore &store, int oldVersion, int newVersion) override
69     {
70         return E_OK;
71     }
72 };
73 
74 /**
75  * @tc.name: Insert_BigInt_INT64
76  * @tc.desc: test insert bigint to rdb store
77  * @tc.type: FUNC
78  */
79 HWTEST_F(RdbSecurityManagerTest, RestoreKeyFile, TestSize.Level1)
80 {
81     std::vector<uint8_t> key = {
82         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
83         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
84     };
85     auto errCode = RdbSecurityManager::GetInstance().RestoreKeyFile(dbFile_, key);
86     ASSERT_EQ(errCode, E_OK);
87     auto password = RdbSecurityManager::GetInstance().GetRdbPassword(dbFile_, KeyType::PUB_KEY_FILE);
88     ASSERT_EQ(password.GetSize(), key.size());
89     auto ret = memcmp(password.GetData(), key.data(), password.GetSize());
90     ASSERT_EQ(ret, 0);
91 }
92 
93 /**
94  * @tc.name: Insert_BigInt_INT64
95  * @tc.desc: test insert bigint to rdb store
96  * @tc.type: FUNC
97  */
98 HWTEST_F(RdbSecurityManagerTest, LockUnlock, TestSize.Level1)
99 {
100     auto blockResult = std::make_shared<OHOS::BlockData<bool>>(1, false);
101     RdbSecurityManager::KeyFiles keyFiles(dbFile_);
102     keyFiles.Lock();
__anon7c0d2f3b0102() 103     std::thread thread([dbFile = dbFile_, blockResult]() {
104         RdbSecurityManager::KeyFiles keyFiles(dbFile);
105         keyFiles.Lock();
106         keyFiles.Unlock();
107         blockResult->SetValue(true);
108     });
109     auto beforeUnlock = blockResult->GetValue();
110     blockResult->Clear(false);
111     keyFiles.Unlock();
112     auto afterUnlock = blockResult->GetValue();
113     ASSERT_FALSE(beforeUnlock);
114     ASSERT_TRUE(afterUnlock);
115     thread.join();
116 }
117 
118 /**
119  * @tc.name: LoadSecretKeyFromDiskTest
120  * @tc.desc: test load secret key from disk test
121  * @tc.type: FUNC
122  */
123 HWTEST_F(RdbSecurityManagerTest, LoadSecretKeyFromDiskTest, TestSize.Level1)
124 {
125     int errCode = E_OK;
126     std::string dbPath = RDB_TEST_PATH + "secret_key_load_test.db";
127     RdbStoreConfig config(dbPath);
128     config.SetEncryptStatus(true);
129     config.SetBundleName(BUNDLE_NAME);
130     RdbStoreSecurityManagerTestOpenCallback helper;
131 
132     auto store = RdbHelper::GetRdbStore(config, 1, helper, errCode);
133     EXPECT_EQ(errCode, E_OK);
134     EXPECT_NE(store, nullptr);
135     store = nullptr;
136 
137     RdbSecurityManager::KeyFiles keyFile(dbPath);
138     const std::string file = keyFile.GetKeyFile(RdbSecurityManager::KeyFileType::PUB_KEY_FILE);
139     std::vector<char> keyfile1;
140     ASSERT_TRUE(OHOS::LoadBufferFromFile(file, keyfile1));
141 
142     std::vector<char> content = { 'a' };
143     bool ret = OHOS::SaveBufferToFile(file, content);
144     ASSERT_TRUE(ret);
145 
146     std::vector<char> keyfile2;
147     ASSERT_TRUE(OHOS::LoadBufferFromFile(file, keyfile2));
148     ASSERT_NE(keyfile1.size(), keyfile2.size());
149 
150     RdbStoreConfig config1(dbPath);
151     config1.SetEncryptStatus(true);
152     config1.SetBundleName(BUNDLE_NAME);
153     RdbStoreSecurityManagerTestOpenCallback helper1;
154     auto store1 = RdbHelper::GetRdbStore(config1, 1, helper1, errCode);
155     EXPECT_EQ(errCode, E_OK);
156     EXPECT_NE(store1, nullptr);
157 
158     RdbHelper::DeleteRdbStore(config);
159 }
160 } // namespace Test