diff --git a/services/distributeddataservice/framework/directory/directory_manager.cpp b/services/distributeddataservice/framework/directory/directory_manager.cpp index 5b7b5251a6ffe10a9191389e97fbc395430a084c..1586511ce2cf210fad1741ba01b844ef82c1dc2e 100644 --- a/services/distributeddataservice/framework/directory/directory_manager.cpp +++ b/services/distributeddataservice/framework/directory/directory_manager.cpp @@ -289,44 +289,4 @@ bool DirectoryManager::CreateDirectory(const std::string &path) const return access(path.c_str(), F_OK) == 0; } - -bool DirectoryManager::DeleteDirectory(const char* path) -{ - if (path == nullptr) { - return false; - } - DIR* dir; - struct dirent* dirEntry; - struct stat buf; - char* curWorkDir = getcwd(nullptr, 0); - if ((dir = opendir(path)) == nullptr) { - return true; - } - if (chdir(path) == -1) { - closedir(dir); - return false; - } - while ((dirEntry = readdir(dir))) { - if ((strcmp(dirEntry->d_name, ".") == 0) || (strcmp(dirEntry->d_name, "..") == 0)) { - continue; - } - if (stat(dirEntry->d_name, &buf) == -1) { - closedir(dir); - return false; - } - if (S_ISDIR(buf.st_mode)) { - DeleteDirectory(dirEntry->d_name); - continue; - } - if (remove(dirEntry->d_name) == -1) { - closedir(dir); - return false; - } - } - closedir(dir); - if (curWorkDir == nullptr || chdir(curWorkDir) == -1 || rmdir(path) == -1) { - return false; - } - return true; -} } // namespace OHOS::DistributedData diff --git a/services/distributeddataservice/framework/include/directory/directory_manager.h b/services/distributeddataservice/framework/include/directory/directory_manager.h index 5ec7eb4410f06110aab79e0b13b6326a76f10816..c7cc4253aa4a00568bc842f13ee55503a6e647c8 100644 --- a/services/distributeddataservice/framework/include/directory/directory_manager.h +++ b/services/distributeddataservice/framework/include/directory/directory_manager.h @@ -46,7 +46,6 @@ public: API_EXPORT std::vector GetVersions(); API_EXPORT void Initialize(const std::vector &strategies, const std::vector &storeTypes); API_EXPORT bool CreateDirectory(const std::string &path) const; - API_EXPORT bool DeleteDirectory(const char* path); private: using Action = std::string (DirectoryManager::*)(const StoreMetaData &) const; diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index ce2389d90ac17d7be75de2ff055afc7906bf3b4f..791ef3b46891d47777529f224239d00394de3aff 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1591,6 +1591,7 @@ ohos_unittest("UdmfServiceImplTest") { "${data_service_path}/adapter/include/account", "${data_service_path}/adapter/include/communicator", "${data_service_path}/app/src", + "${data_service_path}/framework/include/account", "${data_service_path}/service/kvdb", "${data_service_path}/service/matrix/include", "${data_service_path}/service/udmf", @@ -1774,7 +1775,6 @@ ohos_unittest("UdmfServiceStubMockTest") { "${data_service_path}/service/udmf/preprocess/preprocess_utils.cpp", "${data_service_path}/service/udmf/preprocess/udmf_notifier_proxy.cpp", "${data_service_path}/service/udmf/store/runtime_store.cpp", - "${data_service_path}/service/udmf/store/store_account_observer.cpp", "${data_service_path}/service/udmf/store/store_cache.cpp", "${data_service_path}/service/udmf/udmf_service_impl.cpp", "${data_service_path}/service/udmf/udmf_service_stub.cpp", @@ -2401,7 +2401,6 @@ ohos_unittest("UdmfServiceImplDbCorruptionMockTest") { "${data_service_path}/service/udmf/preprocess/data_handler.cpp", "${data_service_path}/service/udmf/preprocess/udmf_notifier_proxy.cpp", "${data_service_path}/service/udmf/store/runtime_store.cpp", - "${data_service_path}/service/udmf/store/store_account_observer.cpp", "${data_service_path}/service/udmf/store/store_cache.cpp", "${data_service_path}/service/udmf/udmf_service_impl.cpp", "${data_service_path}/service/udmf/udmf_service_stub.cpp", diff --git a/services/distributeddataservice/service/test/directory_manager_test.cpp b/services/distributeddataservice/service/test/directory_manager_test.cpp index 85e3db8e1efda0db6e3cf20f176fb8f9f61a6944..96a433b954248e3b64d9369a51462bb77df1fafd 100644 --- a/services/distributeddataservice/service/test/directory_manager_test.cpp +++ b/services/distributeddataservice/service/test/directory_manager_test.cpp @@ -255,35 +255,6 @@ HWTEST_F(DirectoryManagerTest, GetSecretKeyPath, TestSize.Level0) EXPECT_EQ(path, "/data/service/el1/public/database/bundle_manager_service/kvdb/secret"); } -/** -* @tc.name: DeleteDirectory -* @tc.desc: test delete dir -* @tc.type: FUNC -* @tc.require: -* @tc.author: -*/ -HWTEST_F(DirectoryManagerTest, DeleteDirectory, TestSize.Level0) -{ - std::string path = "/data/service/el1/public/database/bundle_manager_service/kvdb/100/data"; - bool ret = DirectoryManager::GetInstance().CreateDirectory(path); - EXPECT_TRUE(ret); - std::ofstream file( - "/data/service/el1/public/database/bundle_manager_service/kvdb/100/data/test.txt", std::ios::out); - if (file.is_open()) { - file << "test content" << std::endl; - file.close(); - } - std::ofstream file1("/data/service/el1/public/database/bundle_manager_service/kvdb/100/test.txt", std::ios::out); - if (file1.is_open()) { - file1 << "test content" << std::endl; - file1.close(); - } - std::string deleteDir = "/data/service/el1/public/database/bundle_manager_service/kvdb/100"; - bool ret1 = DirectoryManager::GetInstance().DeleteDirectory(deleteDir.c_str()); - EXPECT_TRUE(ret1); - EXPECT_EQ(access(deleteDir.c_str(), F_OK), -1); -} - /** * @tc.name: GetStoreTypePath * @tc.desc: test get db dir 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 b73675f5ae53f50ded6456ee3a94f0e08681c44a..e188e2284905b55f080bee4052fe2ff12f071590 100644 --- a/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp +++ b/services/distributeddataservice/service/test/udmf_run_time_store_test.cpp @@ -29,7 +29,6 @@ #include "runtime_store.h" #include "text.h" #include "token_setproc.h" -#include "store_account_observer.h" #include "directory_manager.h" using namespace testing::ext; @@ -713,35 +712,6 @@ HWTEST_F(UdmfRunTimeStoreTest, GetRuntime002, TestSize.Level1) 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); -} - /** * @tc.name: MarkWhenCorrupted001 * @tc.desc: Normal testcase of MarkWhenCorrupted diff --git a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp index bc39e7fc2d59b8fcf13a8f389b721ef6068eda76..dde6410469aecbb9e50d93fb691e8acf251fe8bb 100644 --- a/services/distributeddataservice/service/test/udmf_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/udmf_service_impl_test.cpp @@ -16,6 +16,7 @@ #define LOG_TAG "UdmfServiceImplTest" #include "udmf_service_impl.h" #include "accesstoken_kit.h" +#include "account_delegate.h" #include "bootstrap.h" #include "device_manager_adapter.h" #include "executor_pool.h" @@ -281,14 +282,104 @@ HWTEST_F(UdmfServiceImplTest, SetAppShareOption004, TestSize.Level1) */ HWTEST_F(UdmfServiceImplTest, OnUserChangeTest001, TestSize.Level1) { - uint32_t code = 4; + // Clear store + StoreCache::GetInstance().CloseStores(); + auto stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 0); + // Init store + StoreCache::GetInstance().GetStore("SystemShare"); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 1); + + uint32_t code = static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_STOPPING); std::string user = "OH_USER_test"; std::string account = "OH_ACCOUNT_test"; UdmfServiceImpl udmfServiceImpl; auto status = udmfServiceImpl.OnUserChange(code, user, account); ASSERT_EQ(status, UDMF::E_OK); - auto sizeAfter = StoreCache::GetInstance().stores_.Size(); - ASSERT_EQ(sizeAfter, 0); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 0); +} + +/** +* @tc.name: OnUserChangeTest002 +* @tc.desc: OnUserChange test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfServiceImplTest, OnUserChangeTest002, TestSize.Level1) +{ + // Clear store + StoreCache::GetInstance().CloseStores(); + auto stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 0); + // Init store + StoreCache::GetInstance().GetStore(STORE_ID); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 1); + + uint32_t code = static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_STOPPED); + std::string user = "OH_USER_test"; + std::string account = "OH_ACCOUNT_test"; + UdmfServiceImpl udmfServiceImpl; + auto status = udmfServiceImpl.OnUserChange(code, user, account); + ASSERT_EQ(status, UDMF::E_OK); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 0); +} + +/** +* @tc.name: OnUserChangeTest003 +* @tc.desc: OnUserChange test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfServiceImplTest, OnUserChangeTest003, TestSize.Level1) +{ + // Clear store + StoreCache::GetInstance().CloseStores(); + auto stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 0); + // Init store + StoreCache::GetInstance().GetStore(STORE_ID); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 1); + + uint32_t code = static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_SWITCHED); + std::string user = "OH_USER_test"; + std::string account = "OH_ACCOUNT_test"; + UdmfServiceImpl udmfServiceImpl; + auto status = udmfServiceImpl.OnUserChange(code, user, account); + ASSERT_EQ(status, UDMF::E_OK); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 0); +} + +/** +* @tc.name: OnUserChangeTest004 +* @tc.desc: OnUserChange test +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(UdmfServiceImplTest, OnUserChangeTest004, TestSize.Level1) +{ + // Clear store + StoreCache::GetInstance().CloseStores(); + auto stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 0); + // Init store + StoreCache::GetInstance().GetStore(STORE_ID); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 1); + + uint32_t code = static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_UNLOCKED); + std::string user = "OH_USER_test"; + std::string account = "OH_ACCOUNT_test"; + UdmfServiceImpl udmfServiceImpl; + auto status = udmfServiceImpl.OnUserChange(code, user, account); + ASSERT_EQ(status, UDMF::E_OK); + stores = StoreCache::GetInstance().stores_.Size(); + ASSERT_EQ(stores, 1); } /** diff --git a/services/distributeddataservice/service/udmf/BUILD.gn b/services/distributeddataservice/service/udmf/BUILD.gn index 6ab6b40031a197f2ef98e7f6617813c40e8b62f8..d1d7d3889b20f83f2aade1ca3ce2b8352b5c837b 100644 --- a/services/distributeddataservice/service/udmf/BUILD.gn +++ b/services/distributeddataservice/service/udmf/BUILD.gn @@ -52,7 +52,6 @@ ohos_source_set("udmf_server") { "preprocess/preprocess_utils.cpp", "preprocess/udmf_notifier_proxy.cpp", "store/runtime_store.cpp", - "store/store_account_observer.cpp", "store/store_cache.cpp", "udmf_service_impl.cpp", "udmf_service_stub.cpp", diff --git a/services/distributeddataservice/service/udmf/store/store_account_observer.cpp b/services/distributeddataservice/service/udmf/store/store_account_observer.cpp deleted file mode 100644 index dd3369a9912f95eae925838a780752664eedec60..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/service/udmf/store/store_account_observer.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#define LOG_TAG "RuntimeStoreAccountObserver" - -#include "store_account_observer.h" -#include "log_print.h" -#include "directory/directory_manager.h" -#include "bootstrap.h" -#include "ipc_skeleton.h" - -namespace OHOS { -namespace UDMF { -using namespace DistributedKv; -using namespace DistributedData; -void RuntimeStoreAccountObserver::OnAccountChanged(const AccountEventInfo &eventInfo, int32_t timeout) -{ - ZLOGI("account event begin. status is %{public}d.", eventInfo.status); - 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; - 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); - DistributedData::DirectoryManager::GetInstance().DeleteDirectory(userPath.c_str()); - } -} - -} // namespace UDMF -} // namespace OHOS \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/store/store_account_observer.h b/services/distributeddataservice/service/udmf/store/store_account_observer.h deleted file mode 100644 index 78876da4b57613adcca2df030a5e07c5224748d0..0000000000000000000000000000000000000000 --- a/services/distributeddataservice/service/udmf/store/store_account_observer.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef STORE_ACCOUNT_OBSERVER_H -#define STORE_ACCOUNT_OBSERVER_H - -#include "account/account_delegate.h" -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 - { - return "UdmfRuntimeStore"; - } - - LevelType GetLevel() override - { - return LevelType::LOW; - } -}; - -} // namespace UDMF -} // namespace OHOS - -#endif // STORE_ACCOUNT_OBSERVER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp index dc285565af4718efc4cb14f0a349a60e891e336b..4341d38e9039955c071f0f430f1d40487a19b16e 100644 --- a/services/distributeddataservice/service/udmf/udmf_service_impl.cpp +++ b/services/distributeddataservice/service/udmf/udmf_service_impl.cpp @@ -21,6 +21,7 @@ #include "tokenid_kit.h" #include "accesstoken_kit.h" +#include "account/account_delegate.h" #include "bootstrap.h" #include "bundle_info.h" #include "bundlemgr/bundle_mgr_proxy.h" @@ -35,7 +36,6 @@ #include "metadata/meta_data_manager.h" #include "preprocess_utils.h" #include "dfx/reporter.h" -#include "store_account_observer.h" #include "system_ability_definition.h" #include "uri_permission_manager.h" #include "udmf_radar_reporter.h" @@ -77,8 +77,6 @@ UdmfServiceImpl::Factory::Factory() } return product_; }, FeatureSystem::BIND_NOW); - auto observer = std::make_shared(); - DistributedData::AccountDelegate::GetInstance()->Subscribe(observer); } UdmfServiceImpl::Factory::~Factory() @@ -996,7 +994,9 @@ void UdmfServiceImpl::RegisterAsyncProcessInfo(const std::string &businessUdKey) int32_t UdmfServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account) { ZLOGI("user change, code:%{public}u, user:%{public}s", code, user.c_str()); - if (code == static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_SWITCHED)) { + if (code == static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_STOPPING) + || code == static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_STOPPED) + || code == static_cast(DistributedData::AccountStatus::DEVICE_ACCOUNT_SWITCHED)) { StoreCache::GetInstance().CloseStores(); } return Feature::OnUserChange(code, user, account);