diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index e7d9784768c74503239b9072a1d9d0d8f023a150..6eef966b743749282b4cc014e15ac39ec40d0821 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -960,10 +960,16 @@ ohos_unittest("UdmfRunTimeStoreTest") { "${data_service_path}/service/udmf", "${data_service_path}/service/udmf/store", "${data_service_path}/service/udmf/preprocess", + "${data_service_path}/framework/include/directory", ] configs = [ ":module_private_config" ] + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + external_deps = [ "ability_base:base", "ability_base:want", diff --git a/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp b/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp index 71b8d5c259543b82a29364178657520fa92e4f8d..01795f4b167166759a80628c7b63c2ca4c14f8ee 100644 --- a/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp +++ b/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp @@ -29,6 +29,8 @@ #include "runtime_store.h" #include "text.h" #include "token_setproc.h" +#include "store_account_observer.h" +#include "directory_manager.h" using namespace testing::ext; using namespace OHOS::DistributedData; @@ -706,5 +708,34 @@ HWTEST_F(UdmfRunTimeStoreTest, GetRuntime002, TestSize.Level1) auto status = store->GetRuntime(key, outRuntime); EXPECT_EQ(status, E_NOT_FOUND); } + +/** +* @tc.name: OnAccountChanged001 +* @tc.desc: Abnormal testcase of OnAccountChanged +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfRunTimeStoreTest, OnAccountChanged001, TestSize.Level1) +{ + RuntimeStoreAccountObserver observer; + const AccountEventInfo eventInfo = { + .status = AccountStatus::DEVICE_ACCOUNT_DELETE + }; + DistributedData::StoreMetaData metaData; + uint32_t token = IPCSkeleton::GetSelfTokenID(); + metaData.bundleName = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + metaData.appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); + metaData.user = eventInfo.userId; + metaData.tokenId = token; + metaData.securityLevel = DistributedKv::SecurityLevel::S1; + metaData.area = DistributedKv::Area::EL1; + metaData.storeType = DistributedKv::KvStoreType::SINGLE_VERSION; + metaData.dataDir = DistributedData::DirectoryManager::GetInstance().GetStorePath(metaData); + std::string userPath = metaData.dataDir.append("/").append(eventInfo.userId); + observer.OnAccountChanged(eventInfo, 0); + EXPECT_EQ(access(userPath.c_str(), F_OK), -1); + SetSelfTokenID(0); + observer.OnAccountChanged(eventInfo, 0); +} }; // namespace DistributedDataTest }; // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/store/store_account_observer.cpp b/services/distributeddataservice/service/udmf/store/store_account_observer.cpp index 9085a9b5aee06f58ef809b6a6068f4457a4a36bb..dd3369a9912f95eae925838a780752664eedec60 100644 --- a/services/distributeddataservice/service/udmf/store/store_account_observer.cpp +++ b/services/distributeddataservice/service/udmf/store/store_account_observer.cpp @@ -30,6 +30,10 @@ void RuntimeStoreAccountObserver::OnAccountChanged(const AccountEventInfo &event if (eventInfo.status == AccountStatus::DEVICE_ACCOUNT_DELETE) { DistributedData::StoreMetaData metaData; uint32_t token = IPCSkeleton::GetSelfTokenID(); + if (token == 0) { + ZLOGW("invalid token."); + return; + } metaData.bundleName = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); metaData.appId = DistributedData::Bootstrap::GetInstance().GetProcessLabel(); metaData.user = eventInfo.userId; diff --git a/services/distributeddataservice/service/udmf/store/store_account_observer.h b/services/distributeddataservice/service/udmf/store/store_account_observer.h index 3d50570b9a24bef13b8256dd0c0d90ef5b5ae4ec..78876da4b57613adcca2df030a5e07c5224748d0 100644 --- a/services/distributeddataservice/service/udmf/store/store_account_observer.h +++ b/services/distributeddataservice/service/udmf/store/store_account_observer.h @@ -19,6 +19,7 @@ namespace OHOS { namespace UDMF { class RuntimeStoreAccountObserver : public DistributedData::AccountDelegate::Observer { +private: void OnAccountChanged(const DistributedData::AccountEventInfo &eventInfo, int32_t timeout) override; // must specify unique name for observer std::string Name() override