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 "security_manager.h"
17
18 #include <gtest/gtest.h>
19
20 #include <chrono>
21
22 #include "block_data.h"
23 #include "file_ex.h"
24 #include "hks_api.h"
25 #include "hks_param.h"
26 #include "store_util.h"
27
28 namespace OHOS::Test {
29 using namespace testing::ext;
30 using namespace OHOS::DistributedKv;
31
32 static constexpr int32_t KEY_SIZE = 32;
33 static constexpr int32_t NONCE_SIZE = 12;
34 static constexpr int32_t LOOP_NUM = 2;
35 static constexpr const char *STORE_NAME = "test_store";
36 static constexpr const char *BASE_DIR = "/data/service/el1/public/database/SecurityManagerTest";
37 static constexpr const char *KEY_DIR = "/data/service/el1/public/database/SecurityManagerTest/key";
38 static constexpr const char *KEY_FULL_PATH = "/data/service/el1/public/database/SecurityManagerTest/key/test_store.key";
39 static constexpr const char *KEY_FULL_PATH_V1 =
40 "/data/service/el1/public/database/SecurityManagerTest/key/test_store.key_v1";
41 static constexpr const char *LOCK_FULL_PATH =
42 "/data/service/el1/public/database/SecurityManagerTest/key/test_store.key_lock";
43 static constexpr const char *ROOT_KEY_ALIAS = "distributeddb_client_root_key";
44 static constexpr const char *HKS_BLOB_TYPE_NONCE = "Z5s0Bo571KoqwIi6";
45 static constexpr const char *HKS_BLOB_TYPE_AAD = "distributeddata_client";
46
47 class SecurityManagerTest : public testing::Test {
48 public:
49 static void SetUpTestCase(void);
50 static void TearDownTestCase(void);
51 void SetUp();
52 void TearDown();
53
54 static std::vector<uint8_t> Random(int32_t length);
55 static std::vector<uint8_t> Encrypt(const std::vector<uint8_t> &key, const std::vector<uint8_t> &nonce);
56 static bool SaveKeyToOldFile(const std::vector<uint8_t> &key);
57 static void GenerateRootKey();
58 static void DeleteRootKey();
59
60 static std::vector<uint8_t> vecRootKeyAlias_;
61 static std::vector<uint8_t> vecNonce_;
62 static std::vector<uint8_t> vecAad_;
63 };
64
65 std::vector<uint8_t> SecurityManagerTest::vecRootKeyAlias_ =
66 std::vector<uint8_t>(ROOT_KEY_ALIAS, ROOT_KEY_ALIAS + strlen(ROOT_KEY_ALIAS));
67 std::vector<uint8_t> SecurityManagerTest::vecNonce_ =
68 std::vector<uint8_t>(HKS_BLOB_TYPE_NONCE, HKS_BLOB_TYPE_NONCE + strlen(HKS_BLOB_TYPE_NONCE));
69 std::vector<uint8_t> SecurityManagerTest::vecAad_ =
70 std::vector<uint8_t>(HKS_BLOB_TYPE_AAD, HKS_BLOB_TYPE_AAD + strlen(HKS_BLOB_TYPE_AAD));
71
SetUpTestCase(void)72 void SecurityManagerTest::SetUpTestCase(void)
73 {
74 mkdir(BASE_DIR, (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
75 GenerateRootKey();
76 }
77
TearDownTestCase(void)78 void SecurityManagerTest::TearDownTestCase(void)
79 {
80 DeleteRootKey();
81 (void)remove(BASE_DIR);
82 }
83
SetUp()84 void SecurityManagerTest::SetUp()
85 {
86 mkdir(KEY_DIR, (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH));
87 }
88
TearDown()89 void SecurityManagerTest::TearDown()
90 {
91 (void)remove(LOCK_FULL_PATH);
92 (void)remove(KEY_FULL_PATH);
93 (void)remove(KEY_FULL_PATH_V1);
94 (void)remove(KEY_DIR);
95 }
96
Random(int32_t length)97 std::vector<uint8_t> SecurityManagerTest::Random(int32_t length)
98 {
99 std::vector<uint8_t> value(length, 0);
100 struct HksBlob blobValue = { .size = length, .data = &(value[0]) };
101 auto ret = HksGenerateRandom(nullptr, &blobValue);
102 if (ret != HKS_SUCCESS) {
103 return {};
104 }
105 return value;
106 }
107
Encrypt(const std::vector<uint8_t> & key,const std::vector<uint8_t> & nonce)108 std::vector<uint8_t> SecurityManagerTest::Encrypt(const std::vector<uint8_t> &key, const std::vector<uint8_t> &nonce)
109 {
110 struct HksBlob blobAad = { uint32_t(vecAad_.size()), vecAad_.data() };
111 struct HksBlob blobNonce = { uint32_t(nonce.size()), const_cast<uint8_t *>(nonce.data()) };
112 struct HksBlob rootKeyName = { uint32_t(vecRootKeyAlias_.size()), vecRootKeyAlias_.data() };
113 struct HksBlob plainKey = { uint32_t(key.size()), const_cast<uint8_t *>(key.data()) };
114 struct HksParamSet *params = nullptr;
115 int32_t ret = HksInitParamSet(¶ms);
116 if (ret != HKS_SUCCESS) {
117 return {};
118 }
119 struct HksParam hksParam[] = {
120 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
121 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT },
122 { .tag = HKS_TAG_DIGEST, .uint32Param = 0 },
123 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_GCM },
124 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE },
125 { .tag = HKS_TAG_NONCE, .blob = blobNonce },
126 { .tag = HKS_TAG_ASSOCIATED_DATA, .blob = blobAad },
127 { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE },
128 };
129 ret = HksAddParams(params, hksParam, sizeof(hksParam) / sizeof(hksParam[0]));
130 if (ret != HKS_SUCCESS) {
131 HksFreeParamSet(¶ms);
132 return {};
133 }
134 ret = HksBuildParamSet(¶ms);
135 if (ret != HKS_SUCCESS) {
136 HksFreeParamSet(¶ms);
137 return {};
138 }
139 uint8_t cipherBuf[256] = { 0 };
140 struct HksBlob cipherText = { sizeof(cipherBuf), cipherBuf };
141 ret = HksEncrypt(&rootKeyName, params, &plainKey, &cipherText);
142 (void)HksFreeParamSet(¶ms);
143 if (ret != HKS_SUCCESS) {
144 return {};
145 }
146 std::vector<uint8_t> encryptedKey(cipherText.data, cipherText.data + cipherText.size);
147 std::fill(cipherBuf, cipherBuf + sizeof(cipherBuf), 0);
148 return encryptedKey;
149 }
150
SaveKeyToOldFile(const std::vector<uint8_t> & key)151 bool SecurityManagerTest::SaveKeyToOldFile(const std::vector<uint8_t> &key)
152 {
153 auto encryptKey = Encrypt(key, vecNonce_);
154 if (encryptKey.empty()) {
155 return false;
156 }
157 std::vector<char> content;
158 auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::system_clock::now());
159 std::vector<uint8_t> date(reinterpret_cast<uint8_t *>(&time), reinterpret_cast<uint8_t *>(&time) + sizeof(time));
160 content.push_back(char((sizeof(time_t) / sizeof(uint8_t)) + KEY_SIZE));
161 content.insert(content.end(), date.begin(), date.end());
162 content.insert(content.end(), encryptKey.begin(), encryptKey.end());
163 return SaveBufferToFile(KEY_FULL_PATH, content);
164 }
165
GenerateRootKey()166 void SecurityManagerTest::GenerateRootKey()
167 {
168 struct HksBlob rootKeyName = { uint32_t(vecRootKeyAlias_.size()), vecRootKeyAlias_.data() };
169 struct HksParamSet *params = nullptr;
170 int32_t ret = HksInitParamSet(¶ms);
171 if (ret != HKS_SUCCESS) {
172 return;
173 }
174 struct HksParam hksParam[] = {
175 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
176 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_256 },
177 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
178 { .tag = HKS_TAG_DIGEST, .uint32Param = 0 },
179 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE },
180 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_GCM },
181 { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE },
182 };
183
184 ret = HksAddParams(params, hksParam, sizeof(hksParam) / sizeof(hksParam[0]));
185 if (ret != HKS_SUCCESS) {
186 HksFreeParamSet(¶ms);
187 return;
188 }
189
190 ret = HksBuildParamSet(¶ms);
191 if (ret != HKS_SUCCESS) {
192 HksFreeParamSet(¶ms);
193 return;
194 }
195 ret = HksGenerateKey(&rootKeyName, params, nullptr);
196 HksFreeParamSet(¶ms);
197 }
198
DeleteRootKey()199 void SecurityManagerTest::DeleteRootKey()
200 {
201 struct HksBlob rootKeyName = { uint32_t(vecRootKeyAlias_.size()), vecRootKeyAlias_.data() };
202 struct HksParamSet *params = nullptr;
203 int32_t ret = HksInitParamSet(¶ms);
204 if (ret != HKS_SUCCESS) {
205 return;
206 }
207 struct HksParam hksParam[] = {
208 { .tag = HKS_TAG_ALGORITHM, .uint32Param = HKS_ALG_AES },
209 { .tag = HKS_TAG_KEY_SIZE, .uint32Param = HKS_AES_KEY_SIZE_256 },
210 { .tag = HKS_TAG_PURPOSE, .uint32Param = HKS_KEY_PURPOSE_ENCRYPT | HKS_KEY_PURPOSE_DECRYPT },
211 { .tag = HKS_TAG_DIGEST, .uint32Param = 0 },
212 { .tag = HKS_TAG_PADDING, .uint32Param = HKS_PADDING_NONE },
213 { .tag = HKS_TAG_BLOCK_MODE, .uint32Param = HKS_MODE_GCM },
214 { .tag = HKS_TAG_AUTH_STORAGE_LEVEL, .uint32Param = HKS_AUTH_STORAGE_LEVEL_DE },
215 };
216 ret = HksAddParams(params, hksParam, sizeof(hksParam) / sizeof(hksParam[0]));
217 if (ret != HKS_SUCCESS) {
218 HksFreeParamSet(¶ms);
219 return;
220 }
221 ret = HksBuildParamSet(¶ms);
222 if (ret != HKS_SUCCESS) {
223 HksFreeParamSet(¶ms);
224 return;
225 }
226 ret = HksDeleteKey(&rootKeyName, params);
227 HksFreeParamSet(¶ms);
228 }
229
230 /**
231 * @tc.name: GetDBPasswordTest001
232 * @tc.desc: get password with first create and needCreate is false
233 * @tc.type: FUNC
234 */
235 HWTEST_F(SecurityManagerTest, GetDBPasswordTest001, TestSize.Level0)
236 {
237 auto dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
238 ASSERT_FALSE(dbPassword.IsValid());
239 }
240
241 /**
242 * @tc.name: GetDBPasswordTest002
243 * @tc.desc: get password with key file exist and the key file is empty
244 * @tc.type: FUNC
245 */
246 HWTEST_F(SecurityManagerTest, GetDBPasswordTest002, TestSize.Level0)
247 {
248 std::vector<char> content;
249 auto result = SaveBufferToFile(KEY_FULL_PATH, content);
250 ASSERT_TRUE(result);
251
252 auto dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
253 ASSERT_FALSE(dbPassword.IsValid());
254
255 result = SaveBufferToFile(KEY_FULL_PATH_V1, content);
256 ASSERT_TRUE(result);
257
258 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
259 ASSERT_FALSE(dbPassword.IsValid());
260
261 SecurityManager::GetInstance().DelDBPassword(STORE_NAME, BASE_DIR);
262 }
263
264 /**
265 * @tc.name: GetDBPasswordTest003
266 * @tc.desc: get password with old key file exist and the old key file is invalid
267 * @tc.type: FUNC
268 */
269 HWTEST_F(SecurityManagerTest, GetDBPasswordTest003, TestSize.Level0)
270 {
271 auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::system_clock::now());
272 std::vector<uint8_t> date(reinterpret_cast<uint8_t *>(&time), reinterpret_cast<uint8_t *>(&time) + sizeof(time));
273 // 1.the size of content is invalid
274 std::vector<char> invalidContent1;
275 invalidContent1.push_back(char(sizeof(time_t) / sizeof(uint8_t)));
276 invalidContent1.insert(invalidContent1.end(), date.begin(), date.end());
277 auto result = SaveBufferToFile(KEY_FULL_PATH, invalidContent1);
278 ASSERT_TRUE(result);
279 auto dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
280 ASSERT_FALSE(dbPassword.IsValid());
281
282 auto invalidKey = Random(KEY_SIZE);
283 ASSERT_FALSE(invalidKey.empty());
284 // 2.the pos 0 of content is invalid
285 std::vector<char> invalidContent2;
286 invalidContent2.push_back(char((sizeof(time_t) / sizeof(uint8_t))));
287 invalidContent2.insert(invalidContent2.end(), date.begin(), date.end());
288 invalidContent2.insert(invalidContent2.end(), invalidKey.begin(), invalidKey.end());
289 result = SaveBufferToFile(KEY_FULL_PATH, invalidContent2);
290 ASSERT_TRUE(result);
291 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
292 ASSERT_FALSE(dbPassword.IsValid());
293
294 // 3.the key of content decrypt fail
295 std::vector<char> invalidContent3;
296 invalidContent3.push_back(char((sizeof(time_t) / sizeof(uint8_t)) + KEY_SIZE));
297 invalidContent3.insert(invalidContent3.end(), date.begin(), date.end());
298 invalidContent3.insert(invalidContent3.end(), invalidKey.begin(), invalidKey.end());
299 result = SaveBufferToFile(KEY_FULL_PATH, invalidContent3);
300 ASSERT_TRUE(result);
301 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
302 ASSERT_FALSE(dbPassword.IsValid());
303
304 SecurityManager::GetInstance().DelDBPassword(STORE_NAME, BASE_DIR);
305 }
306
307 /**
308 * @tc.name: GetDBPasswordTest004
309 * @tc.desc: get password with new key file exist and the new key file is invalid
310 * @tc.type: FUNC
311 */
312 HWTEST_F(SecurityManagerTest, GetDBPasswordTest004, TestSize.Level0)
313 {
314 std::vector<char> content;
315
316 // 1.the size of content is invalid
317 content.push_back(char(SecurityManager::SecurityContent::MAGIC_CHAR));
318 auto result = SaveBufferToFile(KEY_FULL_PATH_V1, content);
319 ASSERT_TRUE(result);
320 auto dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
321 ASSERT_FALSE(dbPassword.IsValid());
322
323 // 2.the size of content is invalid
324 for (size_t index = 0; index < SecurityManager::SecurityContent::MAGIC_NUM - 1; ++index) {
325 content.push_back(char(SecurityManager::SecurityContent::MAGIC_CHAR));
326 }
327 result = SaveBufferToFile(KEY_FULL_PATH_V1, content);
328 ASSERT_TRUE(result);
329 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
330 ASSERT_FALSE(dbPassword.IsValid());
331
332 // 3.the size of content is invalid
333 auto nonce = Random(NONCE_SIZE);
334 ASSERT_FALSE(nonce.empty());
335 content.insert(content.end(), nonce.begin(), nonce.end());
336 result = SaveBufferToFile(KEY_FULL_PATH_V1, content);
337 ASSERT_TRUE(result);
338 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
339 ASSERT_FALSE(dbPassword.IsValid());
340
341 // 4.the content decrypt fail
342 auto time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::system_clock::now());
343 std::vector<uint8_t> date(reinterpret_cast<uint8_t *>(&time), reinterpret_cast<uint8_t *>(&time) + sizeof(time));
344 std::vector<char> invalidContent = content;
345 invalidContent.insert(invalidContent.end(), date.begin(), date.end());
346 result = SaveBufferToFile(KEY_FULL_PATH_V1, invalidContent);
347 ASSERT_TRUE(result);
348 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
349 ASSERT_FALSE(dbPassword.IsValid());
350
351 // 5.the content decrypt success and key is empty
352 std::vector<uint8_t> keyContent;
353 keyContent.push_back(SecurityManager::SecurityContent::CURRENT_VERSION);
354 keyContent.insert(keyContent.end(), date.begin(), date.end());
355 auto encryptValue = Encrypt(keyContent, nonce);
356 ASSERT_FALSE(encryptValue.empty());
357 invalidContent = content;
358 invalidContent.insert(invalidContent.end(), encryptValue.begin(), encryptValue.end());
359 result = SaveBufferToFile(KEY_FULL_PATH_V1, invalidContent);
360 ASSERT_TRUE(result);
361 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
362 ASSERT_FALSE(dbPassword.IsValid());
363
364 SecurityManager::GetInstance().DelDBPassword(STORE_NAME, BASE_DIR);
365 }
366
367 /**
368 * @tc.name: GetDBPasswordTest005
369 * @tc.desc: get password with first create and needCreate is true
370 * @tc.type: FUNC
371 */
372 HWTEST_F(SecurityManagerTest, GetDBPasswordTest005, TestSize.Level0)
373 {
374 auto dbPassword1 = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, true);
375 ASSERT_TRUE(dbPassword1.IsValid());
376
377 auto dbPassword2 = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
378 ASSERT_TRUE(dbPassword2.IsValid());
379
380 ASSERT_EQ(dbPassword2.GetSize(), dbPassword1.GetSize());
381
382 std::vector<uint8_t> key1(dbPassword1.GetData(), dbPassword1.GetData() + dbPassword1.GetSize());
383 std::vector<uint8_t> key2(dbPassword2.GetData(), dbPassword2.GetData() + dbPassword2.GetSize());
384 ASSERT_EQ(key1.size(), key2.size());
385
386 for (auto index = 0; index < key1.size(); ++index) {
387 ASSERT_EQ(key1[index], key2[index]);
388 }
389
390 key1.assign(key1.size(), 0);
391 key2.assign(key2.size(), 0);
392 dbPassword1.Clear();
393 dbPassword2.Clear();
394 SecurityManager::GetInstance().DelDBPassword(STORE_NAME, BASE_DIR);
395 }
396
397 /**
398 * @tc.name: GetDBPasswordTest006
399 * @tc.desc: get password with old key file exit and update
400 * @tc.type: FUNC
401 */
402 HWTEST_F(SecurityManagerTest, GetDBPasswordTest006, TestSize.Level0)
403 {
404 auto key = Random(KEY_SIZE);
405 ASSERT_FALSE(key.empty());
406 auto result = SaveKeyToOldFile(key);
407 ASSERT_TRUE(result);
408
409 for (auto loop = 0; loop < LOOP_NUM; ++loop) {
410 auto dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, true);
411 ASSERT_TRUE(dbPassword.IsValid());
412 ASSERT_EQ(dbPassword.GetSize(), key.size());
413 std::vector<uint8_t> password(dbPassword.GetData(), dbPassword.GetData() + dbPassword.GetSize());
414 ASSERT_EQ(password.size(), key.size());
415 for (auto index = 0; index < key.size(); ++index) {
416 ASSERT_EQ(password[index], key[index]);
417 }
418 password.assign(password.size(), 0);
419 dbPassword.Clear();
420 }
421
422 key.assign(key.size(), 0);
423 SecurityManager::GetInstance().DelDBPassword(STORE_NAME, BASE_DIR);
424 }
425
426 /**
427 * @tc.name: SaveDBPasswordTest001
428 * @tc.desc: save password
429 * @tc.type: FUNC
430 */
431 HWTEST_F(SecurityManagerTest, SaveDBPasswordTest001, TestSize.Level0)
432 {
433 auto key = Random(KEY_SIZE);
434 ASSERT_FALSE(key.empty());
435
436 DistributedDB::CipherPassword dbPassword1;
437 dbPassword1.SetValue(key.data(), key.size());
438 ASSERT_EQ(dbPassword1.GetSize(), key.size());
439 auto result = SecurityManager::GetInstance().SaveDBPassword(STORE_NAME, BASE_DIR, dbPassword1);
440 ASSERT_TRUE(result);
441 dbPassword1.Clear();
442
443 auto dbPassword2 = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, true);
444 ASSERT_TRUE(dbPassword2.IsValid());
445 ASSERT_EQ(dbPassword2.GetSize(), key.size());
446 std::vector<uint8_t> password(dbPassword2.GetData(), dbPassword2.GetData() + dbPassword2.GetSize());
447 ASSERT_EQ(password.size(), key.size());
448 for (auto index = 0; index < key.size(); ++index) {
449 ASSERT_EQ(password[index], key[index]);
450 }
451 password.assign(password.size(), 0);
452 dbPassword2.Clear();
453 key.assign(key.size(), 0);
454 SecurityManager::GetInstance().DelDBPassword(STORE_NAME, BASE_DIR);
455 }
456
457 /**
458 * @tc.name: DelDBPasswordTest001
459 * @tc.desc: delete password
460 * @tc.type: FUNC
461 */
462 HWTEST_F(SecurityManagerTest, DelDBPasswordTest001, TestSize.Level0)
463 {
464 auto dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, true);
465 ASSERT_TRUE(dbPassword.IsValid());
466 dbPassword.Clear();
467
468 SecurityManager::GetInstance().DelDBPassword(STORE_NAME, BASE_DIR);
469
470 dbPassword = SecurityManager::GetInstance().GetDBPassword(STORE_NAME, BASE_DIR, false);
471 ASSERT_FALSE(dbPassword.IsValid());
472 }
473
474 /**
475 * @tc.name: KeyFilesMultiLockTest
476 * @tc.desc: Test KeyFiles function
477 * @tc.type: FUNC
478 */
479 HWTEST_F(SecurityManagerTest, KeyFilesMultiLockTest, TestSize.Level1)
480 {
481 std::string dbPath = "/data/service/el1/public/database/SecurityManagerTest";
482 std::string dbName = "test1";
483 StoreUtil::InitPath(dbPath);
484 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
485 auto ret = keyFiles.Lock();
486 EXPECT_EQ(ret, Status::SUCCESS);
487 ret = keyFiles.Lock();
488 EXPECT_EQ(ret, Status::SUCCESS);
489 ret = keyFiles.UnLock();
490 EXPECT_EQ(ret, Status::SUCCESS);
491 ret = keyFiles.UnLock();
492 EXPECT_EQ(ret, Status::SUCCESS);
493 }
494
495 /**
496 * @tc.name: KeyFilesTest
497 * @tc.desc: Test KeyFiles function
498 * @tc.type: FUNC
499 */
500 HWTEST_F(SecurityManagerTest, KeyFilesTest, TestSize.Level1)
501 {
502 std::string dbPath = "/data/service/el1/public/database/SecurityManagerTest";
503 std::string dbName = "test2";
504 StoreUtil::InitPath(dbPath);
505 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
506 keyFiles.Lock();
507 auto blockResult = std::make_shared<OHOS::BlockData<bool>>(1, false);
__anon6bef1d2e0102() 508 std::thread thread([dbPath, dbName, blockResult]() {
509 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
510 keyFiles.Lock();
511 keyFiles.UnLock();
512 blockResult->SetValue(true);
513 });
514 auto beforeUnlock = blockResult->GetValue();
515 EXPECT_FALSE(beforeUnlock);
516 blockResult->Clear();
517 keyFiles.UnLock();
518 auto afterUnlock = blockResult->GetValue();
519 EXPECT_TRUE(afterUnlock);
520 thread.join();
521 }
522
523 /**
524 * @tc.name: KeyFilesAutoLockTest
525 * @tc.desc: Test KeyFilesAutoLock function
526 * @tc.type: FUNC
527 */
528 HWTEST_F(SecurityManagerTest, KeyFilesAutoLockTest, TestSize.Level1)
529 {
530 std::string dbPath = "/data/service/el1/public/database/SecurityManagerTest";
531 std::string dbName = "test3";
532 StoreUtil::InitPath(dbPath);
533 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
534 auto blockResult = std::make_shared<OHOS::BlockData<bool>>(1, false);
535 {
536 SecurityManager::KeyFilesAutoLock fileLock(keyFiles);
__anon6bef1d2e0202() 537 std::thread thread([dbPath, dbName, blockResult]() {
538 SecurityManager::KeyFiles keyFiles(dbName, dbPath);
539 SecurityManager::KeyFilesAutoLock fileLock(keyFiles);
540 blockResult->SetValue(true);
541 });
542 EXPECT_FALSE(blockResult->GetValue());
543 blockResult->Clear();
544 thread.detach();
545 }
546 EXPECT_TRUE(blockResult->GetValue());
547 }
548 } // namespace OHOS::Test