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 <gtest/gtest.h>
17
18 #include "block_data.h"
19 #include "log_print.h"
20 #include "security_manager.h"
21 #include "store_util.h"
22
23 namespace OHOS::Test {
24 using namespace testing::ext;
25 using namespace OHOS::DistributedKv;
26 class SecurityManagerTest : public testing::Test {
27 public:
28 static void SetUpTestCase(void);
29 static void TearDownTestCase(void);
30 void SetUp();
31 void TearDown();
32 };
33
SetUpTestCase(void)34 void SecurityManagerTest::SetUpTestCase(void) { }
35
TearDownTestCase(void)36 void SecurityManagerTest::TearDownTestCase(void) { }
37
SetUp(void)38 void SecurityManagerTest::SetUp(void) { }
39
TearDown(void)40 void SecurityManagerTest::TearDown(void) { }
41
42 /**
43 * @tc.name: DBPasswordTest
44 * @tc.desc: Test DBPassword function
45 * @tc.type: FUNC
46 */
47 HWTEST_F(SecurityManagerTest, DBPasswordTest, TestSize.Level1)
48 {
49 SecurityManager::DBPassword passwd;
50 EXPECT_FALSE(passwd.IsValid());
51 std::vector<uint8_t> key = {
52 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
53 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
54 };
55 passwd.SetValue(key.data(), key.size());
56 EXPECT_TRUE(passwd.IsValid());
57 EXPECT_EQ(passwd.GetSize(), 32);
58 auto newKey = passwd.GetData();
59 EXPECT_EQ(newKey[0], 0x00);
60 passwd.Clear();
61 EXPECT_FALSE(passwd.IsValid());
62 }
63
64 /**
65 * @tc.name: KeyFilesMultiLockTest
66 * @tc.desc: Test KeyFiles function
67 * @tc.type: FUNC
68 */
69 HWTEST_F(SecurityManagerTest, KeyFilesMultiLockTest, TestSize.Level1)
70 {
71 std::string dbPath = "/data/service/el1/public/database/SecurityManagerTest";
72 std::string dbName = "test1";
73 StoreUtil::InitPath(dbPath);
74 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
75 auto keyPath = keyFiles.GetKeyFilePath();
76 EXPECT_EQ(keyPath, "/data/service/el1/public/database/SecurityManagerTest/key/test1.key");
77 auto ret = keyFiles.Lock();
78 EXPECT_EQ(ret, Status::SUCCESS);
79 ret = keyFiles.Lock();
80 EXPECT_EQ(ret, Status::SUCCESS);
81 ret = keyFiles.UnLock();
82 EXPECT_EQ(ret, Status::SUCCESS);
83 ret = keyFiles.UnLock();
84 EXPECT_EQ(ret, Status::SUCCESS);
85 }
86
87 /**
88 * @tc.name: KeyFilesTest
89 * @tc.desc: Test KeyFiles function
90 * @tc.type: FUNC
91 */
92 HWTEST_F(SecurityManagerTest, KeyFilesTest, TestSize.Level1)
93 {
94 std::string dbPath = "/data/service/el1/public/database/SecurityManagerTest";
95 std::string dbName = "test2";
96 StoreUtil::InitPath(dbPath);
97 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
98 auto keyPath = keyFiles.GetKeyFilePath();
99 EXPECT_EQ(keyPath, "/data/service/el1/public/database/SecurityManagerTest/key/test2.key");
100 keyFiles.Lock();
101 auto blockResult = std::make_shared<OHOS::BlockData<bool>>(1, false);
__anon080a378c0102() 102 std::thread thread([dbPath, dbName, blockResult]() {
103 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
104 keyFiles.Lock();
105 keyFiles.UnLock();
106 blockResult->SetValue(true);
107 });
108 auto beforeUnlock = blockResult->GetValue();
109 EXPECT_FALSE(beforeUnlock);
110 blockResult->Clear();
111 keyFiles.UnLock();
112 auto afterUnlock = blockResult->GetValue();
113 EXPECT_TRUE(afterUnlock);
114 thread.join();
115 }
116
117 /**
118 * @tc.name: KeyFilesAutoLockTest
119 * @tc.desc: Test KeyFilesAutoLock function
120 * @tc.type: FUNC
121 */
122 HWTEST_F(SecurityManagerTest, KeyFilesAutoLockTest, TestSize.Level1)
123 {
124 std::string dbPath = "/data/service/el1/public/database/SecurityManagerTest";
125 std::string dbName = "test3";
126 StoreUtil::InitPath(dbPath);
127 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
128 auto blockResult = std::make_shared<OHOS::BlockData<bool>>(1, false);
129 {
130 SecurityManager::KeyFilesAutoLock fileLock(keyFiles);
__anon080a378c0202() 131 std::thread thread([dbPath, dbName, blockResult]() {
132 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
133 SecurityManager::KeyFilesAutoLock fileLock(keyFiles);
134 blockResult->SetValue(true);
135 });
136 EXPECT_FALSE(blockResult->GetValue());
137 blockResult->Clear();
138 thread.detach();
139 }
140 EXPECT_TRUE(blockResult->GetValue());
141 }
142 } // namespace OHOS::Test