diff --git a/services/storage_daemon/crypto/test/base_key_test/BUILD.gn b/services/storage_daemon/crypto/test/base_key_test/BUILD.gn index c24db3479ba1ea0ccaf0c008d010c39296045030..94441dd6ead68f87218b5868793952763f5c54a4 100644 --- a/services/storage_daemon/crypto/test/base_key_test/BUILD.gn +++ b/services/storage_daemon/crypto/test/base_key_test/BUILD.gn @@ -43,6 +43,7 @@ ohos_unittest("BaseKeyTest") { sources = [ "${storage_daemon_path}/crypto/src/base_key.cpp", "${storage_daemon_path}/crypto/src/fscrypt_key_v2.cpp", + "${storage_daemon_path}/crypto/src/iam_client.cpp", "${storage_daemon_path}/crypto/src/key_backup.cpp", "${storage_daemon_path}/crypto/test/mock/huks_master_mock.cpp", "${storage_daemon_path}/crypto/test/mock/openssl_crypto_mock.cpp", diff --git a/services/storage_daemon/crypto/test/crypto_key_test.cpp b/services/storage_daemon/crypto/test/crypto_key_test.cpp index ee6fa5d878ed0d9071aeb3919ec6b97919ce5561..3b4b165ece72e0bc70bca8429b713db1a2ec720e 100644 --- a/services/storage_daemon/crypto/test/crypto_key_test.cpp +++ b/services/storage_daemon/crypto/test/crypto_key_test.cpp @@ -35,6 +35,7 @@ #include "libfscrypt/fscrypt_utils.h" #include "libfscrypt/key_control.h" #include "securec.h" +#include "storage_service_errno.h" using namespace testing::ext; @@ -639,7 +640,11 @@ HWTEST_F(CryptoKeyTest, fscrypt_key_v1_load_and_set_policy_default, TestSize.Lev OHOS::ForceRemoveDirectory(TEST_DIR_LEGACY); OHOS::ForceCreateDirectory(TEST_DIR_LEGACY); - EXPECT_EQ(0, LoadAndSetPolicy(g_testKeyV1->GetDir().c_str(), TEST_DIR_LEGACY.c_str())); + if (KeyCtrlLoadVersion(TEST_DIR_LEGACY.c_str()) == FSCRYPT_V1) { + EXPECT_EQ(0, LoadAndSetPolicy(g_testKeyV1->GetDir().c_str(), TEST_DIR_LEGACY.c_str())); + } else { + EXPECT_EQ(-EFAULT, LoadAndSetPolicy(g_testKeyV1->GetDir().c_str(), TEST_DIR_LEGACY.c_str())); + } EXPECT_TRUE(OHOS::ForceCreateDirectory(TEST_DIR_LEGACY + "/test_dir")); EXPECT_TRUE(OHOS::SaveStringToFile(TEST_DIR_LEGACY + "/test_file1", "hello, world!\n")); @@ -953,8 +958,8 @@ HWTEST_F(CryptoKeyTest, key_manager_generate_delete_user_keys_001, TestSize.Leve EXPECT_EQ(0, KeyManager::GetInstance()->SetDirectoryElPolicy(userId, static_cast(0), {{userId, USER_EL2_DIR}})); // bad keytype EXPECT_EQ(-ENOENT, KeyManager::GetInstance()->UpdateUserAuth(userId, userTokenSecretNull)); - EXPECT_EQ(-ENOENT, KeyManager::GetInstance()->UpdateKeyContext(userId)); - EXPECT_EQ(-ENOENT, KeyManager::GetInstance()->InActiveUserKey(userId)); + EXPECT_EQ(E_PARAMS_INVAL, KeyManager::GetInstance()->UpdateKeyContext(userId)); + EXPECT_EQ(E_PARAMS_INVAL, KeyManager::GetInstance()->InActiveUserKey(userId)); EXPECT_EQ(-EFAULT, KeyManager::GetInstance()->ActiveUserKey(userId, {}, {})); EXPECT_EQ(0, KeyManager::GetInstance()->DeleteUserKeys(userId)); } @@ -1373,24 +1378,8 @@ HWTEST_F(CryptoKeyTest, fscrypt_libfscrypt_api, TestSize.Level1) EXPECT_NE(0, SetFscryptSysparam(NULL)); // test api in key_control.c - EXPECT_EQ(FSCRYPT_INVALID, KeyCtrlLoadVersion(NULL)); - EXPECT_EQ(FSCRYPT_INVALID, KeyCtrlLoadVersion("/nofile")); EXPECT_EQ(FSCRYPT_INVALID, KeyCtrlGetFscryptVersion(NULL)); - // version iss not digit - OHOS::ForceRemoveDirectory(TEST_DIR_LEGACY); - EXPECT_TRUE(OHOS::ForceCreateDirectory(TEST_DIR_LEGACY)); - std::string testVersionFile = TEST_DIR_LEGACY + "/fscrypt_version"; - EXPECT_TRUE(OHOS::SaveStringToFile(testVersionFile, "not-digit\n")); - EXPECT_EQ(FSCRYPT_V1, KeyCtrlLoadVersion(TEST_DIR_LEGACY.c_str())); - - // bad version - OHOS::ForceRemoveDirectory(TEST_DIR_LEGACY); - EXPECT_TRUE(OHOS::ForceCreateDirectory(TEST_DIR_LEGACY)); - testVersionFile = TEST_DIR_LEGACY + "/fscrypt_version"; - EXPECT_TRUE(OHOS::SaveStringToFile(testVersionFile, "10\n")); - EXPECT_EQ(FSCRYPT_V1, KeyCtrlLoadVersion(TEST_DIR_LEGACY.c_str())); - key_serial_t id = 1; EXPECT_NE(0, KeyCtrlGetKeyringId(id, 0)); diff --git a/services/storage_daemon/crypto/test/fscrypt_v2_test/fscrypt_key_v2_test.cpp b/services/storage_daemon/crypto/test/fscrypt_v2_test/fscrypt_key_v2_test.cpp index 7fb1d2a27a67c74de2508315b08006583d91dc7e..17fd90379244611d5489ba90e18b495f32df1c44 100644 --- a/services/storage_daemon/crypto/test/fscrypt_v2_test/fscrypt_key_v2_test.cpp +++ b/services/storage_daemon/crypto/test/fscrypt_v2_test/fscrypt_key_v2_test.cpp @@ -257,17 +257,19 @@ HWTEST_F(FscryptKeyV2Test, fscrypt_key_v2_LoadAndSetEceAndSecePolicy, TestSize.L EXPECT_EQ(LoadAndSetEceAndSecePolicy(TEST_DIR_LEGACY.c_str(), dir, type), 0); EXPECT_EQ(type, 1); - type = 3; - EXPECT_NE(LoadAndSetEceAndSecePolicy(TEST_DIR_LEGACY.c_str(), dir, type), 0); - - type = 4; - EXPECT_NE(LoadAndSetEceAndSecePolicy(TEST_DIR_LEGACY.c_str(), dir, type), 0); - - OHOS::ForceRemoveDirectory(TEST_DIR_LEGACY); - EXPECT_TRUE(OHOS::ForceCreateDirectory(TEST_DIR_LEGACY)); - testVersionFile = TEST_DIR_LEGACY + "/fscrypt_version"; - EXPECT_TRUE(OHOS::SaveStringToFile(testVersionFile, "2\n")); - EXPECT_EQ(LoadAndSetEceAndSecePolicy(TEST_DIR_LEGACY.c_str(), dir, type), 0); + if (KeyCtrlLoadVersion(TEST_DIR_LEGACY.c_str()) == FSCRYPT_V1) { + type = 3; + EXPECT_NE(LoadAndSetEceAndSecePolicy(TEST_DIR_LEGACY.c_str(), dir, type), 0); + + type = 4; + EXPECT_NE(LoadAndSetEceAndSecePolicy(TEST_DIR_LEGACY.c_str(), dir, type), 0); + + OHOS::ForceRemoveDirectory(TEST_DIR_LEGACY); + EXPECT_TRUE(OHOS::ForceCreateDirectory(TEST_DIR_LEGACY)); + testVersionFile = TEST_DIR_LEGACY + "/fscrypt_version"; + EXPECT_TRUE(OHOS::SaveStringToFile(testVersionFile, "2\n")); + EXPECT_NE(LoadAndSetEceAndSecePolicy(TEST_DIR_LEGACY.c_str(), dir, type), 0); + } GTEST_LOG_(INFO) << "fscrypt_key_v2_LoadAndSetEceAndSecePolicy end"; } diff --git a/services/storage_daemon/crypto/test/key_manager_test/key_manager_other_test.cpp b/services/storage_daemon/crypto/test/key_manager_test/key_manager_other_test.cpp index 651574f549c66654c5c9ffc661815484033faaf3..ba44944c87c4917ff000a4a4c3d2654602f20126 100644 --- a/services/storage_daemon/crypto/test/key_manager_test/key_manager_other_test.cpp +++ b/services/storage_daemon/crypto/test/key_manager_test/key_manager_other_test.cpp @@ -35,7 +35,11 @@ using namespace std; using namespace testing::ext; using namespace testing; - + +namespace { +constexpr const char *UECE_PATH = "/dev/fbex_uece"; +} + namespace OHOS::StorageDaemon { class KeyManagerOtherTest : public testing::Test { public: @@ -106,7 +110,6 @@ HWTEST_F(KeyManagerOtherTest, KeyManager_CheckAndClearTokenInfo_000, TestSize.Le EXPECT_CALL(*iamClientMoc_, HasFaceFinger(_, _)).WillOnce(DoAll(SetArgReferee<1>(false), Return(0))); KeyManager::GetInstance()->CheckAndClearTokenInfo(userId); - KeyManager::GetInstance()->userEl3Key_[userId] = nullptr; KeyManager::GetInstance()->userEl4Key_[userId] = nullptr; EXPECT_CALL(*iamClientMoc_, HasFaceFinger(_, _)).WillOnce(DoAll(SetArgReferee<1>(false), Return(0))); @@ -270,31 +273,38 @@ HWTEST_F(KeyManagerOtherTest, KeyManager_TryToFixUeceKey_000, TestSize.Level1) uint32_t user = 800; std::vector token; std::vector secret; + bool existUece = true; + if (access(UECE_PATH, F_OK) != 0) { + existUece = false; + std::ofstream file(UECE_PATH); + EXPECT_GT(open(UECE_PATH, O_RDWR), 0); + } EXPECT_CALL(*iamClientMoc_, HasPinProtect(_)).WillOnce(Return(false)); EXPECT_EQ(KeyManager::GetInstance()->TryToFixUeceKey(user, token, secret), E_OK); EXPECT_CALL(*iamClientMoc_, HasPinProtect(_)).WillOnce(Return(true)); + EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_INVALID)); + EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)); std::string keyDir = KeyManager::GetInstance()->GetKeyDirByUserAndType(user, EL5_KEY); ForceRemoveDirectory(keyDir); EXPECT_EQ(KeyManager::GetInstance()->TryToFixUeceKey(user, token, secret), -EFAULT); EXPECT_CALL(*iamClientMoc_, HasPinProtect(_)).WillOnce(Return(true)); - EXPECT_TRUE(OHOS::ForceCreateDirectory(keyDir)); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*fscryptKeyMock_, DeleteClassEPinCode(_)).WillOnce(Return(true)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(false)); - KeyManager::GetInstance()->userEl2Key_.erase(user); EXPECT_EQ(KeyManager::GetInstance()->TryToFixUeceKey(user, token, secret), E_OK); EXPECT_CALL(*iamClientMoc_, HasPinProtect(_)).WillOnce(Return(true)); - EXPECT_TRUE(OHOS::ForceCreateDirectory(keyDir)); EXPECT_CALL(*fscryptKeyMock_, DeleteClassEPinCode(_)).WillOnce(Return(true)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); - KeyManager::GetInstance()->userEl2Key_.erase(user); EXPECT_EQ(KeyManager::GetInstance()->TryToFixUeceKey(user, token, secret), -EFAULT); ForceRemoveDirectory(keyDir); KeyManager::GetInstance()->userEl5Key_.erase(user); + if (!existUece) { + OHOS::RemoveFile(UECE_PATH); + } GTEST_LOG_(INFO) << "KeyManager_TryToFixUeceKey_000 end"; } @@ -310,13 +320,28 @@ HWTEST_F(KeyManagerOtherTest, KeyManager_TryToFixUeceKey_001, TestSize.Level1) uint32_t user = 800; std::vector token{1, 2, 3, 4, 5}; std::vector secretEmpty; + bool existUece = true; + if (access(UECE_PATH, F_OK) != 0) { + existUece = false; + std::ofstream file(UECE_PATH); + EXPECT_GT(open(UECE_PATH, O_RDWR), 0); + } EXPECT_CALL(*iamClientMoc_, HasPinProtect(_)).WillOnce(Return(true)); + EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_INVALID)); + EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)); EXPECT_EQ(KeyManager::GetInstance()->TryToFixUeceKey(user, token, secretEmpty), -EFAULT); std::vector secret{1, 2, 3, 4, 5, 6}; EXPECT_CALL(*iamClientMoc_, HasPinProtect(_)).WillOnce(Return(true)); EXPECT_CALL(*iamClientMoc_, GetSecureUid(_, _)).WillOnce(Return(true)); + EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_INVALID)) + .WillOnce(Return(FSCRYPT_INVALID)); + EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)) + .WillOnce(Return(FSCRYPT_INVALID)); EXPECT_EQ(KeyManager::GetInstance()->TryToFixUeceKey(user, token, secret), -EFAULT); + if (!existUece) { + OHOS::RemoveFile(UECE_PATH); + } GTEST_LOG_(INFO) << "KeyManager_TryToFixUeceKey_001 end"; } } diff --git a/services/storage_daemon/crypto/test/key_manager_test/key_manager_test.cpp b/services/storage_daemon/crypto/test/key_manager_test/key_manager_test.cpp index be185300cbc3508ab3ced183dddd3e343dba0390..2c0f7a35b2e4e93b7818095897caa4d7393ce9e6 100644 --- a/services/storage_daemon/crypto/test/key_manager_test/key_manager_test.cpp +++ b/services/storage_daemon/crypto/test/key_manager_test/key_manager_test.cpp @@ -126,12 +126,12 @@ HWTEST_F(KeyManagerTest, KeyManager_GenerateAndInstallDeviceKey_001, TestSize.Le GTEST_LOG_(INFO) << "KeyManager_GenerateAndInstallDeviceKey_001 Start"; EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), -EOPNOTSUPP); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), E_GLOBAL_KEY_NULLPTR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), E_GLOBAL_KEY_INIT_ERROR); EXPECT_EQ(KeyManager::GetInstance()->globalEl1Key_, nullptr); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); @@ -143,7 +143,7 @@ HWTEST_F(KeyManagerTest, KeyManager_GenerateAndInstallDeviceKey_001, TestSize.Le EXPECT_CALL(*baseKeyMock_, StoreKey(_)).WillOnce(Return(false)); #endif EXPECT_CALL(*baseKeyMock_, ClearKey(_)).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), E_GLOBAL_KEY_STORE_ERROR); EXPECT_EQ(KeyManager::GetInstance()->globalEl1Key_, nullptr); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); @@ -156,7 +156,7 @@ HWTEST_F(KeyManagerTest, KeyManager_GenerateAndInstallDeviceKey_001, TestSize.Le #endif EXPECT_CALL(*baseKeyMock_, ClearKey(_)).WillOnce(Return(true)); EXPECT_CALL(*fscryptKeyMock_, ActiveKey(_, _)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallDeviceKey("/data/test"), E_GLOBAL_KEY_ACTIVE_ERROR); EXPECT_EQ(KeyManager::GetInstance()->globalEl1Key_, nullptr); GTEST_LOG_(INFO) << "KeyManager_GetBaseKey_001 end"; } @@ -198,19 +198,19 @@ HWTEST_F(KeyManagerTest, KeyManager_RestoreDeviceKey_001, TestSize.Level1) KeyManager::GetInstance()->globalEl1Key_ = nullptr; EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), -EOPNOTSUPP); + EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), E_GLOBAL_KEY_NULLPTR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), E_GLOBAL_KEY_INIT_ERROR); EXPECT_EQ(KeyManager::GetInstance()->globalEl1Key_, nullptr); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, RestoreKey(_)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), E_GLOBAL_KEY_STORE_ERROR); EXPECT_EQ(KeyManager::GetInstance()->globalEl1Key_, nullptr); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); @@ -218,7 +218,7 @@ HWTEST_F(KeyManagerTest, KeyManager_RestoreDeviceKey_001, TestSize.Level1) EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, RestoreKey(_)).WillOnce(Return(true)); EXPECT_CALL(*fscryptKeyMock_, ActiveKey(_, _)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->RestoreDeviceKey("/data/test"), E_GLOBAL_KEY_ACTIVE_ERROR); EXPECT_EQ(KeyManager::GetInstance()->globalEl1Key_, nullptr); GTEST_LOG_(INFO) << "KeyManager_RestoreDeviceKey_001 end"; } @@ -521,7 +521,7 @@ HWTEST_F(KeyManagerTest, KeyManager_UpdateCeEceSeceKeyContext, TestSize.Level1) KeyManager::GetInstance()->userEl1Key_.erase(userId); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->UpdateCeEceSeceKeyContext(userId, type), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->UpdateCeEceSeceKeyContext(userId, type), E_PARAMS_INVAL); std::shared_ptr tmpKey = std::dynamic_pointer_cast(std::make_shared("test")); KeyManager::GetInstance()->userEl1Key_[userId] = tmpKey; @@ -548,17 +548,17 @@ HWTEST_F(KeyManagerTest, KeyManager_UpdateKeyContext, TestSize.Level1) KeyManager::GetInstance()->userEl2Key_.erase(userId); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), E_PARAMS_INVAL); KeyManager::GetInstance()->userEl3Key_.erase(userId); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).Times(2).WillOnce(Return(false))\ .WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), E_PARAMS_INVAL); KeyManager::GetInstance()->userEl4Key_.erase(userId); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).Times(3).WillOnce(Return(false))\ .WillOnce(Return(false)).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), E_PARAMS_INVAL); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).Times(3).WillOnce(Return(false))\ .WillOnce(Return(false)).WillOnce(Return(false)); @@ -598,7 +598,7 @@ HWTEST_F(KeyManagerTest, KeyManager_UpdateKeyContext_001, TestSize.Level1) KeyManager::GetInstance()->saveESecretStatus[userId] = true; EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).Times(4).WillOnce(Return(false))\ .WillOnce(Return(false)).WillOnce(Return(false)).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->UpdateKeyContext(userId), E_PARAMS_INVAL); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).Times(4).WillOnce(Return(false))\ .WillOnce(Return(false)).WillOnce(Return(false)).WillOnce(Return(false)); @@ -648,7 +648,7 @@ HWTEST_F(KeyManagerTest, KeyManager_RestoreUserKey_000, TestSize.Level1) #ifdef USER_CRYPTO_MIGRATE_KEY int eL6Key = 6; type = static_cast(eL6Key); - EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(userId, type), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(userId, type), E_PARAMS_INVAL); type = EL1_KEY; EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(userId, type), -ENOENT); @@ -674,12 +674,14 @@ HWTEST_F(KeyManagerTest, KeyManager_GenerateAndInstallUserKey_001, TestSize.Leve KeyManager::GetInstance()->userEl1Key_.erase(800); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), -EOPNOTSUPP); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), + E_GLOBAL_KEY_NULLPTR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), + E_ELX_KEY_INIT_ERROR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); @@ -690,7 +692,8 @@ HWTEST_F(KeyManagerTest, KeyManager_GenerateAndInstallUserKey_001, TestSize.Leve EXPECT_CALL(*baseKeyMock_, StoreKey(_)).WillOnce(Return(false)); #endif EXPECT_CALL(*baseKeyMock_, ClearKey(_)).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), + E_ELX_KEY_STORE_ERROR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); @@ -702,7 +705,8 @@ HWTEST_F(KeyManagerTest, KeyManager_GenerateAndInstallUserKey_001, TestSize.Leve #endif EXPECT_CALL(*fscryptKeyMock_, ActiveKey(_, _)).WillOnce(Return(false)); EXPECT_CALL(*baseKeyMock_, ClearKey(_)).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->GenerateAndInstallUserKey(800, "/data/test", auth, EL1_KEY), + E_ELX_KEY_ACTIVE_ERROR); GTEST_LOG_(INFO) << "KeyManager_GenerateAndInstallUserKey_001 end"; } @@ -805,25 +809,25 @@ HWTEST_F(KeyManagerTest, KeyManager_RestoreUserKey_001, TestSize.Level1) KeyManager::GetInstance()->userEl1Key_.erase(800); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), -EOPNOTSUPP); + EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), E_GLOBAL_KEY_NULLPTR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), E_ELX_KEY_INIT_ERROR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, RestoreKey(_)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), E_ELX_KEY_STORE_ERROR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*baseKeyMock_, InitKey(_)).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, RestoreKey(_)).WillOnce(Return(true)); EXPECT_CALL(*fscryptKeyMock_, ActiveKey(_, _)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->RestoreUserKey(800, "/data/test", auth, EL1_KEY), E_ELX_KEY_ACTIVE_ERROR); GTEST_LOG_(INFO) << "KeyManager_GenerateAndInstallUserKey_001 end"; } @@ -1164,13 +1168,13 @@ HWTEST_F(KeyManagerTest, KeyManager_InactiveUserElKey_001, TestSize.Level1) unsigned int user = 800; string keyId; std::map> userElxKey_; - EXPECT_EQ(KeyManager::GetInstance()->InactiveUserElKey(user, userElxKey_), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->InactiveUserElKey(user, userElxKey_), E_PARAMS_INVAL); shared_ptr elKey = make_shared("/data/test"); userElxKey_[user] = elKey; EXPECT_CALL(*fscryptKeyMock_, InactiveKey(_, _)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->InactiveUserElKey(user, userElxKey_), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->InactiveUserElKey(user, userElxKey_), E_ELX_KEY_INACTIVE_ERROR); EXPECT_CALL(*fscryptKeyMock_, InactiveKey(_, _)).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, GetKeyDir()).WillOnce(Return("")); @@ -1211,34 +1215,34 @@ HWTEST_F(KeyManagerTest, KeyManager_InActiveUserKey_001, TestSize.Level1) EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), 0); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), E_PARAMS_INVAL); shared_ptr elKey = make_shared("/data/test"); KeyManager::GetInstance()->userEl2Key_[user] = elKey; EXPECT_CALL(*fscryptKeyMock_, InactiveKey(_, _)).WillOnce(Return(false)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); - EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), -EFAULT); + EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), E_ELX_KEY_INACTIVE_ERROR); EXPECT_FALSE(KeyManager::GetInstance()->userEl2Key_.find(user) == KeyManager::GetInstance()->userEl2Key_.end()); KeyManager::GetInstance()->userEl2Key_[user] = elKey; EXPECT_CALL(*fscryptKeyMock_, InactiveKey(_, _)).WillOnce(Return(true)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, GetKeyDir()).WillOnce(Return("")); - EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), E_PARAMS_INVAL); EXPECT_TRUE(KeyManager::GetInstance()->userEl2Key_.find(user) == KeyManager::GetInstance()->userEl2Key_.end()); KeyManager::GetInstance()->userEl2Key_[user] = elKey; EXPECT_CALL(*fscryptKeyMock_, InactiveKey(_, _)).WillOnce(Return(true)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, GetKeyDir()).WillOnce(Return("el1")); - EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), E_PARAMS_INVAL); EXPECT_TRUE(KeyManager::GetInstance()->userEl2Key_.find(user) == KeyManager::GetInstance()->userEl2Key_.end()); KeyManager::GetInstance()->userEl2Key_[user] = elKey; EXPECT_CALL(*fscryptKeyMock_, InactiveKey(_, _)).WillOnce(Return(true)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, GetKeyDir()).WillOnce(Return("el2")); - EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), E_PARAMS_INVAL); EXPECT_TRUE(KeyManager::GetInstance()->userEl2Key_.find(user) == KeyManager::GetInstance()->userEl2Key_.end()); GTEST_LOG_(INFO) << "KeyManager_InactiveUserElKey_001 end"; } @@ -1259,7 +1263,7 @@ HWTEST_F(KeyManagerTest, KeyManager_InActiveUserKey_002, TestSize.Level1) EXPECT_CALL(*fscryptKeyMock_, InactiveKey(_, _)).WillOnce(Return(true)).WillOnce(Return(true)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, GetKeyDir()).WillOnce(Return("")).WillOnce(Return("")); - EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), E_PARAMS_INVAL); EXPECT_TRUE(KeyManager::GetInstance()->userEl2Key_.find(user) == KeyManager::GetInstance()->userEl2Key_.end()); EXPECT_TRUE(KeyManager::GetInstance()->userEl3Key_.find(user) == KeyManager::GetInstance()->userEl3Key_.end()); @@ -1270,7 +1274,7 @@ HWTEST_F(KeyManagerTest, KeyManager_InActiveUserKey_002, TestSize.Level1) .WillOnce(Return(true)); EXPECT_CALL(*fscryptControlMock_, KeyCtrlHasFscryptSyspara()).WillOnce(Return(true)); EXPECT_CALL(*baseKeyMock_, GetKeyDir()).WillOnce(Return("")).WillOnce(Return("")).WillOnce(Return("")); - EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), -ENOENT); + EXPECT_EQ(KeyManager::GetInstance()->InActiveUserKey(user), E_PARAMS_INVAL); EXPECT_TRUE(KeyManager::GetInstance()->userEl2Key_.find(user) == KeyManager::GetInstance()->userEl2Key_.end()); EXPECT_TRUE(KeyManager::GetInstance()->userEl3Key_.find(user) == KeyManager::GetInstance()->userEl3Key_.end()); EXPECT_TRUE(KeyManager::GetInstance()->userEl4Key_.find(user) == KeyManager::GetInstance()->userEl4Key_.end()); @@ -1503,14 +1507,14 @@ HWTEST_F(KeyManagerTest, KeyManager_Generate_And_Install_El5_Key_001, TestSize.L EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_INVALID)); auto ret = KeyManager::GetInstance()->GenerateAndInstallEl5Key(userId, USER_EL5_DIR, badUserAuth); - EXPECT_EQ(ret, -EOPNOTSUPP); + EXPECT_EQ(ret, E_GLOBAL_KEY_NULLPTR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*fscryptKeyMock_, AddClassE(_, _, _)).WillOnce(Return(false)); EXPECT_CALL(*baseKeyMock_, ClearKey(_)).WillOnce(Return(true)); ret = KeyManager::GetInstance()->GenerateAndInstallEl5Key(userId, USER_EL5_DIR, badUserAuth); - EXPECT_EQ(ret, -EFAULT); + EXPECT_EQ(ret, E_EL5_ADD_CLASS_ERROR); GTEST_LOG_(INFO) << "KeyManager_GenerateAndInstallEl5Key_0100 end"; } @@ -1554,7 +1558,7 @@ HWTEST_F(KeyManagerTest, KeyManager_Generate_And_Install_El5_Key_002, TestSize.L EXPECT_CALL(*fscryptKeyMock_, EncryptClassE(_, _, _, _)).WillOnce(Return(false)); EXPECT_CALL(*baseKeyMock_, ClearKey(_)).WillOnce(Return(true)); ret = KeyManager::GetInstance()->GenerateAndInstallEl5Key(userId, TEST_DIR, badUserAuth); - EXPECT_EQ(ret, -EFAULT); + EXPECT_EQ(ret, E_EL5_ENCRYPT_CLASS_ERROR); EXPECT_CALL(*fscryptControlMock_, GetFscryptVersionFromPolicy()).WillOnce(Return(FSCRYPT_V2)); EXPECT_CALL(*keyControlMock_, KeyCtrlGetFscryptVersion(_)).WillOnce(Return(FSCRYPT_V2));