From 5c57ff807fcdde387e6d3a188b73387bc479e8a8 Mon Sep 17 00:00:00 2001 From: dengrenqi <1069093439@qq.com> Date: Fri, 8 Nov 2024 11:38:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=8A=B6=E6=80=81=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dengrenqi <1069093439@qq.com> --- .../storage_manager/native/istorage_manager.h | 1 + .../storage_manager_ipc_interface_code.h | 1 + .../native/storage_manager_proxy.h | 1 + .../client/include/storage_daemon_client.h | 1 + .../client/storage_daemon_client.cpp | 16 ++++++++ .../storage_daemon/crypto/src/key_manager.cpp | 9 +++-- .../crypto/test/key_manager_mock.cpp | 5 +++ .../key_manager_sup_test.cpp.rej | 31 ++++++++++++++ .../include/crypto/key_manager.h | 1 + .../include/ipc/istorage_daemon.h | 1 + .../include/ipc/storage_daemon.h | 1 + .../ipc/storage_daemon_ipc_interface_code.h | 3 +- .../include/ipc/storage_daemon_proxy.h | 1 + .../include/ipc/storage_daemon_stub.h | 1 + .../include/user/mount_manager.h | 1 + .../storage_daemon/ipc/src/storage_daemon.cpp | 26 ++++++++++++ .../ipc/src/storage_daemon_proxy.cpp | 25 ++++++++++++ .../ipc/src/storage_daemon_stub.cpp | 21 ++++++++++ .../ipc/test/storage_daemon_proxy_test.cpp | 23 +++++++++++ .../ipc/test/storage_daemon_service_mock.h | 9 +++++ .../ipc/test/storage_daemon_stub_mock.h | 1 + .../ipc/test/storage_daemon_stub_test.cpp | 2 + services/storage_daemon/sdc.cpp | 23 +++++++++++ .../storage_daemon/user/src/mount_manager.cpp | 19 +++++++++ .../client/storage_manager_client.cpp | 11 +++++ .../test/storage_manager_client_test.cpp | 21 ++++++++++ .../crypto/filesystem_crypto.cpp | 13 ++++++ .../crypto/test/filesystem_crypto_test.cpp | 21 ++++++++++ .../include/client/storage_manager_client.h | 1 + .../include/crypto/filesystem_crypto.h | 1 + .../include/ipc/storage_manager.h | 1 + .../include/ipc/storage_manager_stub.h | 1 + .../storage_daemon_communication.h | 1 + .../src/storage_manager_proxy.cpp | 28 +++++++++++++ .../ipc/src/storage_manager.cpp | 15 ++++++- .../ipc/src/storage_manager_stub.cpp | 40 +++++++++++++++++++ .../ipc/src/storage_manager_stub.cpp.rej | 19 +++++++++ .../ipc/test/storage_manager_service_mock.h | 9 +++++ .../ipc/test/storage_manager_stub_mock.h | 1 + .../ipc/test/storage_manager_stub_mock.h.rej | 9 +++++ .../src/storage_daemon_communication.cpp | 15 +++++++ .../storagemanagerproxymock.h | 4 ++ 42 files changed, 427 insertions(+), 7 deletions(-) create mode 100644 services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej create mode 100644 services/storage_manager/ipc/src/storage_manager_stub.cpp.rej create mode 100644 services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej diff --git a/interfaces/innerkits/storage_manager/native/istorage_manager.h b/interfaces/innerkits/storage_manager/native/istorage_manager.h index 06146e99..d161f329 100644 --- a/interfaces/innerkits/storage_manager/native/istorage_manager.h +++ b/interfaces/innerkits/storage_manager/native/istorage_manager.h @@ -86,6 +86,7 @@ public: virtual int32_t GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus) = 0; virtual int32_t GenerateAppkey(uint32_t hashId, uint32_t userId, std::string &keyId) = 0; virtual int32_t DeleteAppkey(const std::string keyId) = 0; + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) = 0; // app file share api virtual std::vector CreateShareFile(const std::vector &uriList, diff --git a/interfaces/innerkits/storage_manager/native/storage_manager_ipc_interface_code.h b/interfaces/innerkits/storage_manager/native/storage_manager_ipc_interface_code.h index 4f60599f..99fdadc3 100644 --- a/interfaces/innerkits/storage_manager/native/storage_manager_ipc_interface_code.h +++ b/interfaces/innerkits/storage_manager/native/storage_manager_ipc_interface_code.h @@ -83,6 +83,7 @@ namespace StorageManager { GET_BUNDLE_STATS_INCREASE, GENERATE_APP_KEY, DELETE_APP_KEY, + GET_FILE_ENCRYPT_STATUS, }; } // namespace StorageManager } // namespace OHOS diff --git a/interfaces/innerkits/storage_manager/native/storage_manager_proxy.h b/interfaces/innerkits/storage_manager/native/storage_manager_proxy.h index 8e43db3e..2c49aed0 100644 --- a/interfaces/innerkits/storage_manager/native/storage_manager_proxy.h +++ b/interfaces/innerkits/storage_manager/native/storage_manager_proxy.h @@ -78,6 +78,7 @@ public: int32_t UpdateKeyContext(uint32_t userId) override; int32_t GenerateAppkey(uint32_t hashId, uint32_t userId, std::string &keyId) override; int32_t DeleteAppkey(const std::string keyId) override; + int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override; // app file share api std::vector CreateShareFile(const std::vector &uriList, diff --git a/services/storage_daemon/client/include/storage_daemon_client.h b/services/storage_daemon/client/include/storage_daemon_client.h index 7f0d0bda..34ecd34e 100644 --- a/services/storage_daemon/client/include/storage_daemon_client.h +++ b/services/storage_daemon/client/include/storage_daemon_client.h @@ -61,6 +61,7 @@ public: const std::string &networkId, const std::string &deviceId); static int32_t UMountDfsDocs(int32_t userId, const std::string &relativePath, const std::string &networkId, const std::string &deviceId); + static int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false); private: static sptr GetStorageDaemonProxy(void); diff --git a/services/storage_daemon/client/storage_daemon_client.cpp b/services/storage_daemon/client/storage_daemon_client.cpp index da5e1b4e..561411d8 100644 --- a/services/storage_daemon/client/storage_daemon_client.cpp +++ b/services/storage_daemon/client/storage_daemon_client.cpp @@ -448,5 +448,21 @@ int32_t StorageDaemonClient::FscryptEnable(const std::string &fscryptOptions) return 0; } + +int32_t StorageDaemonClient::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ + if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) { + LOGE("Storage service flag check failed!"); + return -EAGAIN; + } + + sptr client = GetStorageDaemonProxy(); + if (client == nullptr) { + LOGE("Get StorageDaemon service failed!"); + return -EAGAIN; + } + + return client->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); +} } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_daemon/crypto/src/key_manager.cpp b/services/storage_daemon/crypto/src/key_manager.cpp index ee35f4a7..05d9225b 100644 --- a/services/storage_daemon/crypto/src/key_manager.cpp +++ b/services/storage_daemon/crypto/src/key_manager.cpp @@ -1568,9 +1568,10 @@ int KeyManager::UpgradeKeys(const std::vector &dirInfo) return 0; } -int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) +int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) { - LOGI("Begin check encrypted status."); + LOGI("Begin check encrypted status, userId is %{public}d, needCheckDirMount is %{public}d", + userId, needCheckDirMount); isEncrypted = true; const char rootPath[] = "/data/app/el2/"; const char basePath[] = "/base"; @@ -1582,8 +1583,8 @@ int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) } int len = sprintf_s(path, allPathSize, "%s%u%s", rootPath, userId, basePath); if (len <= 0 || (size_t)len >= allPathSize) { - LOGE("Failed to get base path"); free(path); + LOGE("Failed to get base path"); return -ENOENT; } if (access(path, F_OK) != 0) { @@ -1592,7 +1593,7 @@ int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) return E_OK; } free(path); - if (!MountManager::GetInstance()->CheckMountFileByUser(userId)) { + if (needCheckDirMount && !MountManager::GetInstance()->CheckMountFileByUser(userId)) { LOGI("The virturalDir is not exists."); return E_OK; } diff --git a/services/storage_daemon/crypto/test/key_manager_mock.cpp b/services/storage_daemon/crypto/test/key_manager_mock.cpp index 2b8d95cc..2537a829 100644 --- a/services/storage_daemon/crypto/test/key_manager_mock.cpp +++ b/services/storage_daemon/crypto/test/key_manager_mock.cpp @@ -110,6 +110,11 @@ int KeyManager::ActiveCeSceSeceUserKey(unsigned int user, KeyType type, return E_OK; } +int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ + return E_OK; +} + int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) { return E_OK; diff --git a/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej b/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej new file mode 100644 index 00000000..7d168565 --- /dev/null +++ b/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej @@ -0,0 +1,31 @@ +diff a/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp b/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp (rejected hunks) +@@ -104,21 +104,24 @@ void KeyManagerSupTest::TearDown(void) + HWTEST_F(KeyManagerSupTest, KeyManager_GetFileEncryptStatus_000, TestSize.Level1) + { + GTEST_LOG_(INFO) << "KeyManager_GetFileEncryptStatus_000 Start"; +- unsigned int userId = 1; +- bool isEncrypted; ++ unsigned int userId = 1000; ++ bool isEncrypted = true; + EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted), E_OK); + EXPECT_EQ(isEncrypted, true); + + string basePath = "/data/app/el2/" + to_string(userId); + string path = basePath + "/base"; + EXPECT_TRUE(OHOS::ForceCreateDirectory(path)); +- EXPECT_CALL(*mountManagerMoc_, CheckMountFileByUser(_)).WillOnce(Return(false)); + EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted), E_OK); +- EXPECT_EQ(isEncrypted, true); ++ EXPECT_EQ(isEncrypted, false); + + EXPECT_CALL(*mountManagerMoc_, CheckMountFileByUser(_)).WillOnce(Return(true)); +- EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted), E_OK); ++ EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted, true), E_OK); + EXPECT_EQ(isEncrypted, false); ++ ++ EXPECT_CALL(*mountManagerMoc_, CheckMountFileByUser(_)).WillOnce(Return(false)); ++ EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted, true), E_OK); ++ EXPECT_EQ(isEncrypted, true); + EXPECT_TRUE(OHOS::ForceRemoveDirectory(basePath)); + GTEST_LOG_(INFO) << "KeyManager_GetFileEncryptStatus_000 end"; + } diff --git a/services/storage_daemon/include/crypto/key_manager.h b/services/storage_daemon/include/crypto/key_manager.h index 54bb2fb7..bb52904d 100644 --- a/services/storage_daemon/include/crypto/key_manager.h +++ b/services/storage_daemon/include/crypto/key_manager.h @@ -137,6 +137,7 @@ private: bool UnlockUece(uint32_t user, const std::vector &token, const std::vector &secret, int &ret); void CheckAndClearTokenInfo(uint32_t user); int CheckUserPinProtect(unsigned int userId, const std::vector &token, const std::vector &secret); + int GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false); std::map> userEl1Key_; std::map> userEl2Key_; diff --git a/services/storage_daemon/include/ipc/istorage_daemon.h b/services/storage_daemon/include/ipc/istorage_daemon.h index e1587c4b..14c974de 100644 --- a/services/storage_daemon/include/ipc/istorage_daemon.h +++ b/services/storage_daemon/include/ipc/istorage_daemon.h @@ -90,6 +90,7 @@ public: const std::string &networkId, const std::string &deviceId) = 0; virtual int32_t UMountDfsDocs(int32_t userId, const std::string &relativePath, const std::string &networkId, const std::string &deviceId) = 0; + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) = 0; DECLARE_INTERFACE_DESCRIPTOR(u"ohos.StorageDaemon"); }; diff --git a/services/storage_daemon/include/ipc/storage_daemon.h b/services/storage_daemon/include/ipc/storage_daemon.h index 14d5bb7d..704cf666 100644 --- a/services/storage_daemon/include/ipc/storage_daemon.h +++ b/services/storage_daemon/include/ipc/storage_daemon.h @@ -89,6 +89,7 @@ public: const std::string &networkId, const std::string &deviceId) override; virtual int32_t UMountDfsDocs(int32_t userId, const std::string &relativePath, const std::string &networkId, const std::string &deviceId) override; + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override; class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { public: SystemAbilityStatusChangeListener() = default; diff --git a/services/storage_daemon/include/ipc/storage_daemon_ipc_interface_code.h b/services/storage_daemon/include/ipc/storage_daemon_ipc_interface_code.h index 89764da2..ca8cf74c 100644 --- a/services/storage_daemon/include/ipc/storage_daemon_ipc_interface_code.h +++ b/services/storage_daemon/include/ipc/storage_daemon_ipc_interface_code.h @@ -56,7 +56,8 @@ namespace StorageDaemon { GENERATE_APP_KEY, DELETE_APP_KEY, MOUNT_DFS_DOCS, - UMOUNT_DFS_DOCS + UMOUNT_DFS_DOCS, + GET_FILE_ENCRYPT_STATUS, }; } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_daemon/include/ipc/storage_daemon_proxy.h b/services/storage_daemon/include/ipc/storage_daemon_proxy.h index 7d567f6a..cfe4bc31 100644 --- a/services/storage_daemon/include/ipc/storage_daemon_proxy.h +++ b/services/storage_daemon/include/ipc/storage_daemon_proxy.h @@ -78,6 +78,7 @@ public: const std::string &networkId, const std::string &deviceId) override; virtual int32_t UMountDfsDocs(int32_t userId, const std::string &relativePath, const std::string &networkId, const std::string &deviceId) override; + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override; private: static inline BrokerDelegator delegator_; int32_t SendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/services/storage_daemon/include/ipc/storage_daemon_stub.h b/services/storage_daemon/include/ipc/storage_daemon_stub.h index 046b5f83..2c8a8767 100644 --- a/services/storage_daemon/include/ipc/storage_daemon_stub.h +++ b/services/storage_daemon/include/ipc/storage_daemon_stub.h @@ -67,6 +67,7 @@ private: int32_t HandleGetLockScreenStatus(MessageParcel &data, MessageParcel &reply); int32_t HandleGenerateAppkey(MessageParcel &data, MessageParcel &reply); int32_t HandleDeleteAppkey(MessageParcel &data, MessageParcel &reply); + int32_t HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply); // app file share api int32_t HandleCreateShareFile(MessageParcel &data, MessageParcel &reply); diff --git a/services/storage_daemon/include/user/mount_manager.h b/services/storage_daemon/include/user/mount_manager.h index d19fc2e3..d54e890c 100644 --- a/services/storage_daemon/include/user/mount_manager.h +++ b/services/storage_daemon/include/user/mount_manager.h @@ -95,6 +95,7 @@ public: void MountSandboxPath(const std::vector &srcPaths, const std::vector &dstPaths, const std::string &bundleName, const std::string &userId); bool CheckMountFileByUser(int32_t userId); + bool CloudDirFlag(const std::string &path); private: bool SupportHmdfs(); diff --git a/services/storage_daemon/ipc/src/storage_daemon.cpp b/services/storage_daemon/ipc/src/storage_daemon.cpp index cf40ebe8..bffb5412 100644 --- a/services/storage_daemon/ipc/src/storage_daemon.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon.cpp @@ -889,6 +889,32 @@ int32_t StorageDaemon::UMountDfsDocs(int32_t userId, const std::string &relative return MountManager::GetInstance()->UMountDfsDocs(userId, relativePath, networkId, deviceId); } +int32_t StorageDaemon::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ +#ifdef USER_CRYPTO_MANAGER + int32_t ret = KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); + if (ret != E_OK) { + LOGE("GetFileEncryptStatus failed, please check"); + RadarParameter parameterRes = { + .orgPkg = DEFAULT_ORGPKGNAME, + .userId = userId, + .funcName = "GetFileEncryptStatus", + .bizScene = BizScene::USER_KEY_ENCRYPTION, + .bizStage = BizStage::BIZ_STAGE_GET_FILE_ENCRYPT_STATUS, + .keyElxLevel = "EL1", + .errorCode = ret + }; + StorageService::StorageRadar::GetInstance().RecordFuctionResult(parameterRes); + AuditLog storageAuditLog = { false, "FAILED TO GetFileEncryptStatus", "SELECT", "GetFileEncryptStatus", 1, + "FAILED" }; + HiAudit::GetInstance().Write(storageAuditLog); + } + return ret; +#else + return E_OK; +#endif +} + static bool ReadFileToString(const std::string& pathInst, std::string& oldContent) { std::fstream fd; diff --git a/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp b/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp index c9c1b944..78d474ee 100644 --- a/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon_proxy.cpp @@ -889,5 +889,30 @@ int32_t StorageDaemonProxy::SendRequest(uint32_t code, MessageParcel &data, Mess return E_OK; } + +int32_t StorageDaemonProxy::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + + if (!data.WriteInterfaceToken(StorageDaemonProxy::GetDescriptor())) { + return E_WRITE_DESCRIPTOR_ERR; + } + + if (!data.WriteUint32(userId)) { + return E_WRITE_PARCEL_ERR; + } + if (!data.WriteBool(needCheckDirMount)) { + return E_WRITE_PARCEL_ERR; + } + int32_t err = + SendRequest(static_cast(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS), data, reply, option); + if (err != E_OK) { + return err; + } + isEncrypted = reply.ReadBool(); + return reply.ReadInt32(); +} } // StorageDaemon } // OHOS diff --git a/services/storage_daemon/ipc/src/storage_daemon_stub.cpp b/services/storage_daemon/ipc/src/storage_daemon_stub.cpp index 84a2f90e..5925c313 100644 --- a/services/storage_daemon/ipc/src/storage_daemon_stub.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon_stub.cpp @@ -92,6 +92,8 @@ StorageDaemonStub::StorageDaemonStub() &StorageDaemonStub::HandleMountDfsDocs; opToInterfaceMap_[static_cast(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS)] = &StorageDaemonStub::HandleUMountDfsDocs; + opToInterfaceMap_[static_cast(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS)] = + &StorageDaemonStub::HandleGetFileEncryptStatus; } int32_t StorageDaemonStub::OnRemoteRequest(uint32_t code, @@ -139,6 +141,7 @@ int32_t StorageDaemonStub::OnRemoteRequest(uint32_t code, case static_cast(StorageDaemonInterfaceCode::UMOUNT_DFS_DOCS): case static_cast(StorageDaemonInterfaceCode::GENERATE_APP_KEY): case static_cast(StorageDaemonInterfaceCode::DELETE_APP_KEY): + case static_cast(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS): return OnRemoteRequestForApp(code, data, reply); default: LOGE("Cannot response request %d: unknown tranction", code); @@ -233,6 +236,8 @@ int32_t StorageDaemonStub::OnRemoteRequestForApp(uint32_t code, MessageParcel &d return HandleGenerateAppkey(data, reply); case static_cast(StorageDaemonInterfaceCode::DELETE_APP_KEY): return HandleDeleteAppkey(data, reply); + case static_cast(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS): + return HandleGetFileEncryptStatus(data, reply); default: LOGE("Cannot response request %d: unknown tranction", code); return E_SYS_ERR; @@ -701,5 +706,21 @@ int32_t StorageDaemonStub::HandleUMountDfsDocs(MessageParcel &data, MessageParce return E_OK; } +int32_t StorageDaemonStub::HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply) +{ + uint32_t userId = data.ReadUint32(); + bool needCheckDirMount = data.ReadBool(); + bool isEncrypted = true; + int timerId = StorageXCollie::SetTimer("storage:GetFileEncryptStatus", LOCAL_TIME_OUT_SECONDS); + int err = GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); + StorageXCollie::CancelTimer(timerId); + if (!reply.WriteInt32(err)) { + return E_WRITE_REPLY_ERR; + } + if (!reply.WriteBool(isEncrypted)) { + return E_WRITE_REPLY_ERR; + } + return E_OK; +} } // StorageDaemon } // OHOS diff --git a/services/storage_daemon/ipc/test/storage_daemon_proxy_test.cpp b/services/storage_daemon/ipc/test/storage_daemon_proxy_test.cpp index 690d6f35..94c44922 100644 --- a/services/storage_daemon/ipc/test/storage_daemon_proxy_test.cpp +++ b/services/storage_daemon/ipc/test/storage_daemon_proxy_test.cpp @@ -20,6 +20,7 @@ #include "ipc/storage_daemon_proxy.h" #include "storage_daemon_service_mock.h" #include "storage_service_errno.h" +#include "storage_service_log.h" namespace OHOS { namespace StorageDaemon { @@ -696,5 +697,27 @@ HWTEST_F(StorageDaemonProxyTest, StorageDaemonProxyTest_UpdateMemoryPara_001, Te GTEST_LOG_(INFO) << "StorageDaemonProxyTest_UpdateMemoryPara_001 end"; } +/** + * @tc.name: StorageDaemonProxyTest_GetFileEncryptStatus_001 + * @tc.desc: Verify the UpdateMemoryPara function. + * @tc.type: FUNC + * @tc.require: I8ZBB3 + */ +HWTEST_F(StorageDaemonProxyTest, StorageDaemonProxyTest_GetFileEncryptStatus_001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StorageDaemonProxyTest_GetFileEncryptStatus_001 start"; + EXPECT_CALL(*mock_, SendRequest(testing::_, testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Invoke(mock_.GetRefPtr(), &StorageDaemonServiceMock::InvokeSendRequest)); + ASSERT_TRUE(proxy_ != nullptr); + bool isEncrypted = true; + int32_t ret = proxy_->GetFileEncryptStatus(USER_ID1, isEncrypted); + ASSERT_TRUE(ret == E_OK); + ASSERT_TRUE(mock_ != nullptr); + int m=static_cast(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS); + ASSERT_TRUE(m == mock_->code_); + GTEST_LOG_(INFO) << "StorageDaemonProxyTest_GetFileEncryptStatus_001 end"; +} + } // STORAGE_DAEMON } // OHOS \ No newline at end of file diff --git a/services/storage_daemon/ipc/test/storage_daemon_service_mock.h b/services/storage_daemon/ipc/test/storage_daemon_service_mock.h index 0ca9674b..545532aa 100644 --- a/services/storage_daemon/ipc/test/storage_daemon_service_mock.h +++ b/services/storage_daemon/ipc/test/storage_daemon_service_mock.h @@ -207,6 +207,15 @@ public: { return E_OK; } + + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override + { + return E_OK; + } + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) override + { + return E_OK; + } }; } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h b/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h index 45dad9ab..851201a6 100644 --- a/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h +++ b/services/storage_daemon/ipc/test/storage_daemon_stub_mock.h @@ -61,6 +61,7 @@ public: const std::vector &, std::vector &, std::vector &)); MOCK_METHOD3(GenerateAppkey, int32_t (uint32_t, uint32_t, std::string &)); MOCK_METHOD2(DeleteAppkey, int32_t (uint32_t, const std::string &)); + MOCK_METHOD3(GetFileEncryptStatus, int32_t (uint32_t, bool &, bool)); }; } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_daemon/ipc/test/storage_daemon_stub_test.cpp b/services/storage_daemon/ipc/test/storage_daemon_stub_test.cpp index d5a80c40..8a75b187 100644 --- a/services/storage_daemon/ipc/test/storage_daemon_stub_test.cpp +++ b/services/storage_daemon/ipc/test/storage_daemon_stub_test.cpp @@ -62,6 +62,7 @@ namespace { static_cast(StorageDaemonInterfaceCode::UPDATE_MEM_PARA), static_cast(StorageDaemonInterfaceCode::GENERATE_APP_KEY), static_cast(StorageDaemonInterfaceCode::DELETE_APP_KEY), + static_cast(StorageDaemonInterfaceCode::GET_FILE_ENCRYPT_STATUS), }; } @@ -165,6 +166,7 @@ HWTEST_F(StorageDaemonStubTest, Storage_Manager_StorageDaemonStubTest_OnRemoteRe EXPECT_CALL(mock, GetOccupiedSpace(testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, MountCryptoPathAgain(testing::_)).WillOnce(testing::Return(E_OK)); EXPECT_CALL(mock, UpdateMemoryPara(testing::_, testing::_)).WillOnce(testing::Return(E_OK)); + EXPECT_CALL(mock, GetFileEncryptStatus(testing::_, testing::_, testing::_)).WillOnce(testing::Return(E_OK)); for (auto c : g_code) { MessageParcel data; diff --git a/services/storage_daemon/sdc.cpp b/services/storage_daemon/sdc.cpp index 4abd09e3..99c5d93c 100644 --- a/services/storage_daemon/sdc.cpp +++ b/services/storage_daemon/sdc.cpp @@ -209,6 +209,28 @@ static int32_t UnlockUserScreen(const std::vector &args) return OHOS::StorageDaemon::StorageDaemonClient::UnlockUserScreen(userId, {}, {}); } +int32_t GetFileEncryptStatus(const std::vector &args) +{ + if (args.size() < ARG_CNT_5) { + LOGE("Parameter nums is less than 4, please retry"); + return -EINVAL; + } + uint32_t userId; + // 3 means take the fourth argument of args + if (OHOS::StorageDaemon::StringToUint32(args[INDEX_3], userId) == false) { + LOGE("Parameter input error, please retry"); + return -EINVAL; + } + uint32_t judge; + if (OHOS::StorageDaemon::StringToUint32(args[INDEX_4], judge) == false) { + LOGE("Parameter input error, please retry"); + return -EINVAL; + } + bool isEncrypted = true; + bool judgeFlag = judge != 0; + return OHOS::StorageDaemon::StorageDaemonClient::GetFileEncryptStatus(userId, isEncrypted, judgeFlag); +} + static int32_t EnableFscrypt(const std::vector &args) { if (args.size() < ARG_CNT_4) { @@ -286,6 +308,7 @@ static const auto g_fscryptCmdHandler = std::mapUnlockUserScreen(userId, token, secret); } +int32_t StorageManagerClient::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ + sptr client = GetStorageManagerProxy(); + if (client == nullptr) { + LOGE("get storage manager service failed"); + return -EFAULT; + } + + return client->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); +} + int32_t StorageManagerClient::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus) { sptr client = GetStorageManagerProxy(); diff --git a/services/storage_manager/client/test/storage_manager_client_test.cpp b/services/storage_manager/client/test/storage_manager_client_test.cpp index aeed984e..61fe06aa 100644 --- a/services/storage_manager/client/test/storage_manager_client_test.cpp +++ b/services/storage_manager/client/test/storage_manager_client_test.cpp @@ -460,5 +460,26 @@ HWTEST_F(StorageManagerClientTest, Client_manager_service_GetLockScreenStatus_00 GTEST_LOG_(INFO) << "Client_manager_service_GetLockScreenStatus_0000 end"; } + +/** + * @tc.number: SUB_STORAGE_Client_manager_service_GetFileEncryptStatus_0000 + * @tc.name: Client_manager_service_GetFileEncryptStatus_0000 + * @tc.desc: Test function of GetLockScreenStatus interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000GGUPF + */ +HWTEST_F(StorageManagerClientTest, Client_manager_service_GetFileEncryptStatus_0000, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StorageManagerClientTest-begin Client_manager_service_GetFileEncryptStatus_0000"; + + ASSERT_TRUE(storageManagerClient_ != nullptr); + bool isEncrypted = true; + uint32_t userId = 104; + int32_t ret = storageManagerClient_->GetFileEncryptStatus(userId, isEncrypted); + EXPECT_TRUE(ret == 0); + GTEST_LOG_(INFO) << "Client_manager_service_GetFileEncryptStatus_0000 end"; +} } } diff --git a/services/storage_manager/crypto/filesystem_crypto.cpp b/services/storage_manager/crypto/filesystem_crypto.cpp index 60591b8f..5ec570b7 100644 --- a/services/storage_manager/crypto/filesystem_crypto.cpp +++ b/services/storage_manager/crypto/filesystem_crypto.cpp @@ -147,6 +147,19 @@ int32_t FileSystemCrypto::UnlockUserScreen(uint32_t userId, return sdCommunication->UnlockUserScreen(userId, token, secret); } +int32_t FileSystemCrypto::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ + LOGI("UserId: %{public}u", userId); + int32_t err = CheckUserIdRange(userId); + if (err != E_OK) { + LOGE("User ID out of range"); + return err; + } + std::shared_ptr sdCommunication; + sdCommunication = DelayedSingleton::GetInstance(); + return sdCommunication->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); +} + int32_t FileSystemCrypto::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus) { LOGI("UserId: %{public}u", userId); diff --git a/services/storage_manager/crypto/test/filesystem_crypto_test.cpp b/services/storage_manager/crypto/test/filesystem_crypto_test.cpp index f3ad24f0..5a3640de 100644 --- a/services/storage_manager/crypto/test/filesystem_crypto_test.cpp +++ b/services/storage_manager/crypto/test/filesystem_crypto_test.cpp @@ -376,5 +376,26 @@ HWTEST_F(FileSystemCryptoTest, Storage_manager_crypto_UpdateKeyContext_0001, tes GTEST_LOG_(INFO) << "FileSystemCryptoTest-end Storage_manager_crypto_UpdateKeyContext_0001"; } + ++/** ++ * @tc.number: SUB_Storage_manager_crypto_GetFileEncryptStatus_0000 ++ * @tc.name: Storage_manager_crypto_GetFileEncryptStatus_0000 ++ * @tc.desc: Test function of GetFileEncryptStatus interface for Parameters ERROR which userId not in [101, 1099]. ++ * @tc.size: MEDIUM ++ * @tc.type: FUNC ++ * @tc.level Level 1 ++ * @tc.require: AR000H0F7I ++ */ ++HWTEST_F(FileSystemCryptoTest, Storage_manager_crypto_GetFileEncryptStatus_0000, testing::ext::TestSize.Level1) ++{ ++ GTEST_LOG_(INFO) << "FileSystemCryptoTest-start Storage_manager_crypto_UpdateKeyContext_0001"; ++ std::shared_ptr fileSystemCrypto_ = ++ DelayedSingleton::GetInstance(); ++ uint32_t userId = 99999; ++ bool isEncrypted = true; ++ uint32_t ret = fileSystemCrypto_->GetFileEncryptStatus(userId, isEncrypted); ++ EXPECT_EQ(ret, E_USERID_RANGE); ++ GTEST_LOG_(INFO) << "FileSystemCryptoTest-end Storage_manager_crypto_GetFileEncryptStatus_0000"; ++} } } \ No newline at end of file diff --git a/services/storage_manager/include/client/storage_manager_client.h b/services/storage_manager/include/client/storage_manager_client.h index 1e32c863..d7edc3f3 100644 --- a/services/storage_manager/include/client/storage_manager_client.h +++ b/services/storage_manager/include/client/storage_manager_client.h @@ -46,6 +46,7 @@ public: const std::string &networkId, const std::string &deviceId); static int32_t UMountDfsDocs(int32_t userId, const std::string &relativePath, const std::string &networkId, const std::string &deviceId); + static int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false); private: static sptr GetStorageManagerProxy(void); }; diff --git a/services/storage_manager/include/crypto/filesystem_crypto.h b/services/storage_manager/include/crypto/filesystem_crypto.h index 0e838da6..b71d9f3b 100644 --- a/services/storage_manager/include/crypto/filesystem_crypto.h +++ b/services/storage_manager/include/crypto/filesystem_crypto.h @@ -43,6 +43,7 @@ public: int32_t GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus); int32_t GenerateAppkey(uint32_t hashId, uint32_t userId, std::string &keyId); int32_t DeleteAppkey(const std::string keyId); + int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false); private: int32_t CheckUserIdRange(int32_t userId); }; diff --git a/services/storage_manager/include/ipc/storage_manager.h b/services/storage_manager/include/ipc/storage_manager.h index 7a753c40..d7872ec9 100644 --- a/services/storage_manager/include/ipc/storage_manager.h +++ b/services/storage_manager/include/ipc/storage_manager.h @@ -88,6 +88,7 @@ public: int32_t GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus) override; int32_t GenerateAppkey(uint32_t hashId, uint32_t userId, std::string &keyId) override; int32_t DeleteAppkey(const std::string keyId) override; + int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override; // app file share api std::vector CreateShareFile(const std::vector &uriList, diff --git a/services/storage_manager/include/ipc/storage_manager_stub.h b/services/storage_manager/include/ipc/storage_manager_stub.h index 630de994..f3e71bd1 100644 --- a/services/storage_manager/include/ipc/storage_manager_stub.h +++ b/services/storage_manager/include/ipc/storage_manager_stub.h @@ -75,6 +75,7 @@ private: int32_t HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply); int32_t HandleGenerateAppkey(MessageParcel &data, MessageParcel &reply); int32_t HandleDeleteAppkey(MessageParcel &data, MessageParcel &reply); + int32_t HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply); // app file share api int32_t HandleCreateShareFile(MessageParcel &data, MessageParcel &reply); diff --git a/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h b/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h index cc6465f3..7d7b1c3d 100644 --- a/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h +++ b/services/storage_manager/include/storage_daemon_communication/storage_daemon_communication.h @@ -59,6 +59,7 @@ public: int32_t UnlockUserScreen(uint32_t userId, const std::vector &token, const std::vector &secret); + int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false); int32_t GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus); int32_t GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId); int32_t DeleteAppkey(uint32_t userId, const std::string keyId); diff --git a/services/storage_manager/innerkits_impl/src/storage_manager_proxy.cpp b/services/storage_manager/innerkits_impl/src/storage_manager_proxy.cpp index 44f819d9..58198aa3 100644 --- a/services/storage_manager/innerkits_impl/src/storage_manager_proxy.cpp +++ b/services/storage_manager/innerkits_impl/src/storage_manager_proxy.cpp @@ -1371,5 +1371,33 @@ int32_t StorageManagerProxy::UMountDfsDocs(int32_t userId, const std::string &re return reply.ReadInt32(); } + +int32_t StorageManagerProxy::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ + LOGI("user ID: %{public}u", userId); + HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__); + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_SYNC); + if (!data.WriteInterfaceToken(StorageManagerProxy::GetDescriptor())) { + LOGE("WriteInterfaceToken failed"); + return E_WRITE_DESCRIPTOR_ERR; + } + if (!data.WriteUint32(userId)) { + LOGE("Write user ID failed"); + return E_WRITE_PARCEL_ERR; + } + if (!data.WriteBool(needCheckDirMount)) { + LOGE("Write user ID failed"); + return E_WRITE_PARCEL_ERR; + } + int32_t err = SendRequest( + static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS), data, reply, option); + if (err != E_OK) { + return err; + } + isEncrypted = reply.ReadBool(); + return reply.ReadInt32(); +} } // StorageManager } // OHOS diff --git a/services/storage_manager/ipc/src/storage_manager.cpp b/services/storage_manager/ipc/src/storage_manager.cpp index 32af3828..410475b3 100644 --- a/services/storage_manager/ipc/src/storage_manager.cpp +++ b/services/storage_manager/ipc/src/storage_manager.cpp @@ -450,12 +450,23 @@ int32_t StorageManager::InactiveUserKey(uint32_t userId) #endif } -int32_t StorageManager::LockUserScreen(uint32_t userId) +int32_t StorageManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) { #ifdef USER_CRYPTO_MANAGER LOGI("UserId: %{public}u", userId); std::shared_ptr fsCrypto = DelayedSingleton::GetInstance(); - return fsCrypto->LockUserScreen(userId); + return fsCrypto->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); +#else + return E_OK; +#endif +} + +int32_t StorageManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) +{ +#ifdef USER_CRYPTO_MANAGER + LOGI("UserId: %{public}u", userId); + std::shared_ptr fsCrypto = DelayedSingleton::GetInstance(); + return fsCrypto->GetFileEncryptStatus(userId, isEncrypted); #else return E_OK; #endif diff --git a/services/storage_manager/ipc/src/storage_manager_stub.cpp b/services/storage_manager/ipc/src/storage_manager_stub.cpp index 1495198a..b0bb73b9 100644 --- a/services/storage_manager/ipc/src/storage_manager_stub.cpp +++ b/services/storage_manager/ipc/src/storage_manager_stub.cpp @@ -178,6 +178,8 @@ StorageManagerStub::StorageManagerStub() &StorageManagerStub::HandleMountDfsDocs; opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::UMOUNT_DFS_DOCS)] = &StorageManagerStub::HandleUMountDfsDocs; + opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS)] = + &StorageManagerStub::HandleGetFileEncryptStatus; } int32_t StorageManagerStub::OnRemoteRequest(uint32_t code, @@ -283,6 +285,8 @@ int32_t StorageManagerStub::OnRemoteRequest(uint32_t code, return HandleGenerateAppkey(data, reply); case static_cast(StorageManagerInterfaceCode::DELETE_APP_KEY): return HandleDeleteAppkey(data, reply); + case static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS): + return HandleGetFileEncryptStatus(data, reply); default: LOGE("Cannot response request %d: unknown tranction", code); return IPCObjectStub::OnRemoteRequest(code, data, reply, option); @@ -864,6 +868,42 @@ int32_t StorageManagerStub::HandleLockUserScreen(MessageParcel &data, MessagePar return E_OK; } +int32_t StorageManagerStub::HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply) +{ + if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) { + return E_PERMISSION_DENIED; + } + bool isEncrypted = true; + uint32_t userId = data.ReadUint32(); + bool needCheckDirMount = data.ReadBool(); + int32_t err = GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); + if (!reply.WriteInt32(err)) { + LOGE("Write reply error code failed"); + return E_WRITE_REPLY_ERR; + } + if (!reply.WriteBool(isEncrypted)) { + LOGE("Write reply isEncrypted failed"); + return E_WRITE_REPLY_ERR; + } + return E_OK; +} + +int32_t StorageManagerStub::HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply) +{ + if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) { + return E_PERMISSION_DENIED; + } + bool isEncrypted = true; + uint32_t userId = data.ReadUint32(); + int32_t err = GetFileEncryptStatus(userId, isEncrypted); + if (!reply.WriteInt32(err)) { + LOGE("Write reply error code failed"); + return E_WRITE_REPLY_ERR; + } + + return E_OK; +} + int32_t StorageManagerStub::HandleUnlockUserScreen(MessageParcel &data, MessageParcel &reply) { if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) { diff --git a/services/storage_manager/ipc/src/storage_manager_stub.cpp.rej b/services/storage_manager/ipc/src/storage_manager_stub.cpp.rej new file mode 100644 index 00000000..67e5ef29 --- /dev/null +++ b/services/storage_manager/ipc/src/storage_manager_stub.cpp.rej @@ -0,0 +1,19 @@ +diff a/services/storage_manager/ipc/src/storage_manager_stub.cpp b/services/storage_manager/ipc/src/storage_manager_stub.cpp (rejected hunks) +@@ -178,6 +178,8 @@ StorageManagerStub::StorageManagerStub() + &StorageManagerStub::HandleMountDfsDocs; + opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::UMOUNT_DFS_DOCS)] = + &StorageManagerStub::HandleUMountDfsDocs; ++ opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS)] = ++ &StorageManagerStub::HandleGetFileEncryptStatus; + opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::CREATE_RECOVER_KEY)] = + &StorageManagerStub::HandleCreateRecoverKey; + opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::SET_RECOVER_KEY)] = +@@ -285,6 +287,8 @@ int32_t StorageManagerStub::OnRemoteRequest(uint32_t code, + return HandleGenerateAppkey(data, reply); + case static_cast(StorageManagerInterfaceCode::DELETE_APP_KEY): + return HandleDeleteAppkey(data, reply); ++ case static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS): ++ return HandleGetFileEncryptStatus(data, reply); + default: + LOGE("Cannot response request %d: unknown tranction", code); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); diff --git a/services/storage_manager/ipc/test/storage_manager_service_mock.h b/services/storage_manager/ipc/test/storage_manager_service_mock.h index c9ad1160..009e5232 100644 --- a/services/storage_manager/ipc/test/storage_manager_service_mock.h +++ b/services/storage_manager/ipc/test/storage_manager_service_mock.h @@ -280,6 +280,15 @@ public: { return E_OK; } + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override + { + return E_OK; + } + + virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) override + { + return E_OK; + } }; } // namespace StorageManager } // namespace OHOS diff --git a/services/storage_manager/ipc/test/storage_manager_stub_mock.h b/services/storage_manager/ipc/test/storage_manager_stub_mock.h index a164ce2d..9907460a 100644 --- a/services/storage_manager/ipc/test/storage_manager_stub_mock.h +++ b/services/storage_manager/ipc/test/storage_manager_stub_mock.h @@ -76,6 +76,7 @@ public: MOCK_METHOD3(GenerateAppkey, int32_t(uint32_t, uint32_t, std::string &)); MOCK_METHOD4(SetBundleQuota, int32_t(const std::string &, int32_t, const std::string &, int32_t)); MOCK_METHOD1(DeleteAppkey, int32_t(const std::string keyId)); + MOCK_METHOD3(GetFileEncryptStatus, int32_t(uint32_t, bool &, bool)); }; } // namespace StorageManager } // namespace OHOS diff --git a/services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej b/services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej new file mode 100644 index 00000000..56b48672 --- /dev/null +++ b/services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej @@ -0,0 +1,9 @@ +diff a/services/storage_manager/ipc/test/storage_manager_stub_mock.h b/services/storage_manager/ipc/test/storage_manager_stub_mock.h (rejected hunks) +@@ -75,6 +75,7 @@ public: + MOCK_METHOD2(GenerateAppkey, int32_t(uint32_t, std::string &)); + MOCK_METHOD4(SetBundleQuota, int32_t(const std::string &, int32_t, const std::string &, int32_t)); + MOCK_METHOD1(DeleteAppkey, int32_t(const std::string keyId)); ++ MOCK_METHOD2(GetFileEncryptStatus, int32_t(uint32_t, bool &)); + MOCK_METHOD4(CreateRecoverKey, int32_t(uint32_t, uint32_t, const std::vector &, + const std::vector &)); + MOCK_METHOD1(SetRecoverKey, int32_t(const std::vector &)); diff --git a/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp b/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp index b6454f1a..95750b55 100644 --- a/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp +++ b/services/storage_manager/storage_daemon_communication/src/storage_daemon_communication.cpp @@ -333,6 +333,21 @@ int32_t StorageDaemonCommunication::LockUserScreen(uint32_t userId) return storageDaemon_->LockUserScreen(userId); } +int32_t StorageDaemonCommunication::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +{ + LOGD("enter"); + int32_t err = Connect(); + if (err != E_OK) { + LOGE("Connect failed"); + return err; + } + if (storageDaemon_ == nullptr) { + LOGE("StorageDaemonCommunication::Connect service nullptr"); + return E_SERVICE_IS_NULLPTR; + } + return storageDaemon_->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); +} + int32_t StorageDaemonCommunication::UnlockUserScreen(uint32_t userId, const std::vector &token, const std::vector &secret) diff --git a/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxymock.h b/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxymock.h index 9d193fe7..a31b65c5 100644 --- a/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxymock.h +++ b/test/fuzztest/storagemanagerproxy_fuzzer/storagemanagerproxymock.h @@ -279,6 +279,10 @@ public: { return E_OK; } + int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false) override + { + return E_OK; + } }; } // namespace StorageManager } // namespace OHOS -- Gitee From 3822db0a8b79e2a3c20f5fc5aba024bf029eb77e Mon Sep 17 00:00:00 2001 From: dengrenqi <1069093439@qq.com> Date: Fri, 8 Nov 2024 16:40:15 +0800 Subject: [PATCH 2/3] qqq Signed-off-by: dengrenqi <1069093439@qq.com> --- .../key_manager_sup_test.cpp.rej | 31 ------------------- .../ipc/src/storage_manager_stub.cpp.rej | 19 ------------ .../ipc/test/storage_manager_stub_mock.h.rej | 9 ------ 3 files changed, 59 deletions(-) delete mode 100644 services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej delete mode 100644 services/storage_manager/ipc/src/storage_manager_stub.cpp.rej delete mode 100644 services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej diff --git a/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej b/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej deleted file mode 100644 index 7d168565..00000000 --- a/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp.rej +++ /dev/null @@ -1,31 +0,0 @@ -diff a/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp b/services/storage_daemon/crypto/test/key_manager_test/key_manager_sup_test.cpp (rejected hunks) -@@ -104,21 +104,24 @@ void KeyManagerSupTest::TearDown(void) - HWTEST_F(KeyManagerSupTest, KeyManager_GetFileEncryptStatus_000, TestSize.Level1) - { - GTEST_LOG_(INFO) << "KeyManager_GetFileEncryptStatus_000 Start"; -- unsigned int userId = 1; -- bool isEncrypted; -+ unsigned int userId = 1000; -+ bool isEncrypted = true; - EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted), E_OK); - EXPECT_EQ(isEncrypted, true); - - string basePath = "/data/app/el2/" + to_string(userId); - string path = basePath + "/base"; - EXPECT_TRUE(OHOS::ForceCreateDirectory(path)); -- EXPECT_CALL(*mountManagerMoc_, CheckMountFileByUser(_)).WillOnce(Return(false)); - EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted), E_OK); -- EXPECT_EQ(isEncrypted, true); -+ EXPECT_EQ(isEncrypted, false); - - EXPECT_CALL(*mountManagerMoc_, CheckMountFileByUser(_)).WillOnce(Return(true)); -- EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted), E_OK); -+ EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted, true), E_OK); - EXPECT_EQ(isEncrypted, false); -+ -+ EXPECT_CALL(*mountManagerMoc_, CheckMountFileByUser(_)).WillOnce(Return(false)); -+ EXPECT_EQ(KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted, true), E_OK); -+ EXPECT_EQ(isEncrypted, true); - EXPECT_TRUE(OHOS::ForceRemoveDirectory(basePath)); - GTEST_LOG_(INFO) << "KeyManager_GetFileEncryptStatus_000 end"; - } diff --git a/services/storage_manager/ipc/src/storage_manager_stub.cpp.rej b/services/storage_manager/ipc/src/storage_manager_stub.cpp.rej deleted file mode 100644 index 67e5ef29..00000000 --- a/services/storage_manager/ipc/src/storage_manager_stub.cpp.rej +++ /dev/null @@ -1,19 +0,0 @@ -diff a/services/storage_manager/ipc/src/storage_manager_stub.cpp b/services/storage_manager/ipc/src/storage_manager_stub.cpp (rejected hunks) -@@ -178,6 +178,8 @@ StorageManagerStub::StorageManagerStub() - &StorageManagerStub::HandleMountDfsDocs; - opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::UMOUNT_DFS_DOCS)] = - &StorageManagerStub::HandleUMountDfsDocs; -+ opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS)] = -+ &StorageManagerStub::HandleGetFileEncryptStatus; - opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::CREATE_RECOVER_KEY)] = - &StorageManagerStub::HandleCreateRecoverKey; - opToInterfaceMap_[static_cast(StorageManagerInterfaceCode::SET_RECOVER_KEY)] = -@@ -285,6 +287,8 @@ int32_t StorageManagerStub::OnRemoteRequest(uint32_t code, - return HandleGenerateAppkey(data, reply); - case static_cast(StorageManagerInterfaceCode::DELETE_APP_KEY): - return HandleDeleteAppkey(data, reply); -+ case static_cast(StorageManagerInterfaceCode::GET_FILE_ENCRYPT_STATUS): -+ return HandleGetFileEncryptStatus(data, reply); - default: - LOGE("Cannot response request %d: unknown tranction", code); - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); diff --git a/services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej b/services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej deleted file mode 100644 index 56b48672..00000000 --- a/services/storage_manager/ipc/test/storage_manager_stub_mock.h.rej +++ /dev/null @@ -1,9 +0,0 @@ -diff a/services/storage_manager/ipc/test/storage_manager_stub_mock.h b/services/storage_manager/ipc/test/storage_manager_stub_mock.h (rejected hunks) -@@ -75,6 +75,7 @@ public: - MOCK_METHOD2(GenerateAppkey, int32_t(uint32_t, std::string &)); - MOCK_METHOD4(SetBundleQuota, int32_t(const std::string &, int32_t, const std::string &, int32_t)); - MOCK_METHOD1(DeleteAppkey, int32_t(const std::string keyId)); -+ MOCK_METHOD2(GetFileEncryptStatus, int32_t(uint32_t, bool &)); - MOCK_METHOD4(CreateRecoverKey, int32_t(uint32_t, uint32_t, const std::vector &, - const std::vector &)); - MOCK_METHOD1(SetRecoverKey, int32_t(const std::vector &)); -- Gitee From b5d899060d4db8e14c2fd1b9af83b59fbcfbcdf3 Mon Sep 17 00:00:00 2001 From: dengrenqi <1069093439@qq.com> Date: Tue, 12 Nov 2024 15:36:18 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dengrenqi <1069093439@qq.com> --- .../crypto/test/key_manager_mock.cpp | 5 --- .../include/crypto/key_manager.h | 3 +- .../storage_daemon/ipc/src/storage_daemon.cpp | 19 +-------- .../ipc/src/storage_daemon_stub.cpp | 2 - .../ipc/test/storage_daemon_service_mock.h | 4 -- .../crypto/test/filesystem_crypto_test.cpp | 40 +++++++++---------- .../ipc/src/storage_manager.cpp | 8 ++-- .../ipc/src/storage_manager_stub.cpp | 16 -------- .../ipc/test/storage_manager_service_mock.h | 5 --- 9 files changed, 26 insertions(+), 76 deletions(-) diff --git a/services/storage_daemon/crypto/test/key_manager_mock.cpp b/services/storage_daemon/crypto/test/key_manager_mock.cpp index 2537a829..6fbb10b4 100644 --- a/services/storage_daemon/crypto/test/key_manager_mock.cpp +++ b/services/storage_daemon/crypto/test/key_manager_mock.cpp @@ -114,10 +114,5 @@ int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool ne { return E_OK; } - -int KeyManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) -{ - return E_OK; -} } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_daemon/include/crypto/key_manager.h b/services/storage_daemon/include/crypto/key_manager.h index fee74f2d..658a222b 100644 --- a/services/storage_daemon/include/crypto/key_manager.h +++ b/services/storage_daemon/include/crypto/key_manager.h @@ -87,7 +87,7 @@ public: int GenerateAppkey(uint32_t user, uint32_t hashId, std::string &keyId); int DeleteAppkey(uint32_t user, const std::string keyId); int UnlockUserAppKeys(uint32_t userId, bool needGetAllAppKey); - int GetFileEncryptStatus(uint32_t userId, bool &isEncrypted); + int GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false); #ifdef USER_CRYPTO_MIGRATE_KEY int RestoreUserKey(uint32_t userId, KeyType type); #endif @@ -144,7 +144,6 @@ private: bool UnlockUece(uint32_t user, const std::vector &token, const std::vector &secret, int &ret); void CheckAndClearTokenInfo(uint32_t user); int CheckUserPinProtect(unsigned int userId, const std::vector &token, const std::vector &secret); - int GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount = false); bool HashElxActived(unsigned int user, KeyType type); bool HasElxDesc(std::map> &userElKey_, KeyType type, unsigned int user); bool IsWorkDirExist(std::string type, int32_t userId); diff --git a/services/storage_daemon/ipc/src/storage_daemon.cpp b/services/storage_daemon/ipc/src/storage_daemon.cpp index bffb5412..b33dad9b 100644 --- a/services/storage_daemon/ipc/src/storage_daemon.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon.cpp @@ -892,24 +892,7 @@ int32_t StorageDaemon::UMountDfsDocs(int32_t userId, const std::string &relative int32_t StorageDaemon::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) { #ifdef USER_CRYPTO_MANAGER - int32_t ret = KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); - if (ret != E_OK) { - LOGE("GetFileEncryptStatus failed, please check"); - RadarParameter parameterRes = { - .orgPkg = DEFAULT_ORGPKGNAME, - .userId = userId, - .funcName = "GetFileEncryptStatus", - .bizScene = BizScene::USER_KEY_ENCRYPTION, - .bizStage = BizStage::BIZ_STAGE_GET_FILE_ENCRYPT_STATUS, - .keyElxLevel = "EL1", - .errorCode = ret - }; - StorageService::StorageRadar::GetInstance().RecordFuctionResult(parameterRes); - AuditLog storageAuditLog = { false, "FAILED TO GetFileEncryptStatus", "SELECT", "GetFileEncryptStatus", 1, - "FAILED" }; - HiAudit::GetInstance().Write(storageAuditLog); - } - return ret; + return KeyManager::GetInstance()->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); #else return E_OK; #endif diff --git a/services/storage_daemon/ipc/src/storage_daemon_stub.cpp b/services/storage_daemon/ipc/src/storage_daemon_stub.cpp index 5925c313..1bc252a0 100644 --- a/services/storage_daemon/ipc/src/storage_daemon_stub.cpp +++ b/services/storage_daemon/ipc/src/storage_daemon_stub.cpp @@ -711,9 +711,7 @@ int32_t StorageDaemonStub::HandleGetFileEncryptStatus(MessageParcel &data, Messa uint32_t userId = data.ReadUint32(); bool needCheckDirMount = data.ReadBool(); bool isEncrypted = true; - int timerId = StorageXCollie::SetTimer("storage:GetFileEncryptStatus", LOCAL_TIME_OUT_SECONDS); int err = GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); - StorageXCollie::CancelTimer(timerId); if (!reply.WriteInt32(err)) { return E_WRITE_REPLY_ERR; } diff --git a/services/storage_daemon/ipc/test/storage_daemon_service_mock.h b/services/storage_daemon/ipc/test/storage_daemon_service_mock.h index 545532aa..a8fab033 100644 --- a/services/storage_daemon/ipc/test/storage_daemon_service_mock.h +++ b/services/storage_daemon/ipc/test/storage_daemon_service_mock.h @@ -212,10 +212,6 @@ public: { return E_OK; } - virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) override - { - return E_OK; - } }; } // namespace StorageDaemon } // namespace OHOS diff --git a/services/storage_manager/crypto/test/filesystem_crypto_test.cpp b/services/storage_manager/crypto/test/filesystem_crypto_test.cpp index 5a3640de..dbc9d2bd 100644 --- a/services/storage_manager/crypto/test/filesystem_crypto_test.cpp +++ b/services/storage_manager/crypto/test/filesystem_crypto_test.cpp @@ -377,25 +377,25 @@ HWTEST_F(FileSystemCryptoTest, Storage_manager_crypto_UpdateKeyContext_0001, tes GTEST_LOG_(INFO) << "FileSystemCryptoTest-end Storage_manager_crypto_UpdateKeyContext_0001"; } -+/** -+ * @tc.number: SUB_Storage_manager_crypto_GetFileEncryptStatus_0000 -+ * @tc.name: Storage_manager_crypto_GetFileEncryptStatus_0000 -+ * @tc.desc: Test function of GetFileEncryptStatus interface for Parameters ERROR which userId not in [101, 1099]. -+ * @tc.size: MEDIUM -+ * @tc.type: FUNC -+ * @tc.level Level 1 -+ * @tc.require: AR000H0F7I -+ */ -+HWTEST_F(FileSystemCryptoTest, Storage_manager_crypto_GetFileEncryptStatus_0000, testing::ext::TestSize.Level1) -+{ -+ GTEST_LOG_(INFO) << "FileSystemCryptoTest-start Storage_manager_crypto_UpdateKeyContext_0001"; -+ std::shared_ptr fileSystemCrypto_ = -+ DelayedSingleton::GetInstance(); -+ uint32_t userId = 99999; -+ bool isEncrypted = true; -+ uint32_t ret = fileSystemCrypto_->GetFileEncryptStatus(userId, isEncrypted); -+ EXPECT_EQ(ret, E_USERID_RANGE); -+ GTEST_LOG_(INFO) << "FileSystemCryptoTest-end Storage_manager_crypto_GetFileEncryptStatus_0000"; -+} +/** + * @tc.number: SUB_Storage_manager_crypto_GetFileEncryptStatus_0000 + * @tc.name: Storage_manager_crypto_GetFileEncryptStatus_0000 + * @tc.desc: Test function of GetFileEncryptStatus interface for Parameters ERROR which userId not in [101, 1099]. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: AR000H0F7I + */ +HWTEST_F(FileSystemCryptoTest, Storage_manager_crypto_GetFileEncryptStatus_0000, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileSystemCryptoTest-start Storage_manager_crypto_UpdateKeyContext_0001"; + std::shared_ptr fileSystemCrypto_ = + DelayedSingleton::GetInstance(); + uint32_t userId = 99999; + bool isEncrypted = true; + uint32_t ret = fileSystemCrypto_->GetFileEncryptStatus(userId, isEncrypted); + EXPECT_EQ(ret, E_USERID_RANGE); + GTEST_LOG_(INFO) << "FileSystemCryptoTest-end Storage_manager_crypto_GetFileEncryptStatus_0000"; +} } } \ No newline at end of file diff --git a/services/storage_manager/ipc/src/storage_manager.cpp b/services/storage_manager/ipc/src/storage_manager.cpp index 410475b3..54cdf1cc 100644 --- a/services/storage_manager/ipc/src/storage_manager.cpp +++ b/services/storage_manager/ipc/src/storage_manager.cpp @@ -450,23 +450,23 @@ int32_t StorageManager::InactiveUserKey(uint32_t userId) #endif } -int32_t StorageManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) +int32_t StorageManager::LockUserScreen(uint32_t userId) { #ifdef USER_CRYPTO_MANAGER LOGI("UserId: %{public}u", userId); std::shared_ptr fsCrypto = DelayedSingleton::GetInstance(); - return fsCrypto->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); + return fsCrypto->LockUserScreen(userId); #else return E_OK; #endif } -int32_t StorageManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) +int32_t StorageManager::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount) { #ifdef USER_CRYPTO_MANAGER LOGI("UserId: %{public}u", userId); std::shared_ptr fsCrypto = DelayedSingleton::GetInstance(); - return fsCrypto->GetFileEncryptStatus(userId, isEncrypted); + return fsCrypto->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount); #else return E_OK; #endif diff --git a/services/storage_manager/ipc/src/storage_manager_stub.cpp b/services/storage_manager/ipc/src/storage_manager_stub.cpp index b0bb73b9..a833e175 100644 --- a/services/storage_manager/ipc/src/storage_manager_stub.cpp +++ b/services/storage_manager/ipc/src/storage_manager_stub.cpp @@ -888,22 +888,6 @@ int32_t StorageManagerStub::HandleGetFileEncryptStatus(MessageParcel &data, Mess return E_OK; } -int32_t StorageManagerStub::HandleGetFileEncryptStatus(MessageParcel &data, MessageParcel &reply) -{ - if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) { - return E_PERMISSION_DENIED; - } - bool isEncrypted = true; - uint32_t userId = data.ReadUint32(); - int32_t err = GetFileEncryptStatus(userId, isEncrypted); - if (!reply.WriteInt32(err)) { - LOGE("Write reply error code failed"); - return E_WRITE_REPLY_ERR; - } - - return E_OK; -} - int32_t StorageManagerStub::HandleUnlockUserScreen(MessageParcel &data, MessageParcel &reply) { if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) { diff --git a/services/storage_manager/ipc/test/storage_manager_service_mock.h b/services/storage_manager/ipc/test/storage_manager_service_mock.h index 009e5232..048f4dc5 100644 --- a/services/storage_manager/ipc/test/storage_manager_service_mock.h +++ b/services/storage_manager/ipc/test/storage_manager_service_mock.h @@ -284,11 +284,6 @@ public: { return E_OK; } - - virtual int32_t GetFileEncryptStatus(uint32_t userId, bool &isEncrypted) override - { - return E_OK; - } }; } // namespace StorageManager } // namespace OHOS -- Gitee