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