From 6e76f21b6696763c8d49bd3a7d87bdd18180b5c1 Mon Sep 17 00:00:00 2001 From: fundavid Date: Mon, 14 Apr 2025 11:09:10 +0800 Subject: [PATCH] db remove in one transaction Signed-off-by: fundavid --- .../unittest/src/sandbox_manager_kit_test.cpp | 94 +++++++++---------- .../include/database/sandbox_manager_rdb.h | 6 +- .../cpp/src/database/sandbox_manager_rdb.cpp | 46 ++++++++- .../cpp/src/service/policy_info_manager.cpp | 18 ++-- .../unittest/policy_info_manager_test.cpp | 28 +++--- 5 files changed, 116 insertions(+), 76 deletions(-) diff --git a/frameworks/inner_api/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp b/frameworks/inner_api/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp index b893f71..ec1e5c2 100644 --- a/frameworks/inner_api/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp +++ b/frameworks/inner_api/sandbox_manager/test/unittest/src/sandbox_manager_kit_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -168,7 +168,7 @@ void SandboxManagerKitTest::TearDown() * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy003, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy003, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -211,7 +211,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy003, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicyByTokenID001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicyByTokenID001, TestSize.Level0) { PolicyInfo info = { .path = "/A/B", @@ -251,7 +251,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicyByTokenID001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy004, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy004, TestSize.Level0) { PolicyInfo info = { .path = "", @@ -282,7 +282,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy004, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy005, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy005, TestSize.Level0) { PolicyInfo infoParent = { .path = "/A/B", @@ -329,7 +329,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy005, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy006, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy006, TestSize.Level0) { PolicyInfo infoSub = { .path = "/A/B/C", @@ -380,7 +380,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy006, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy007, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy007, TestSize.Level0) { std::vector policy; PolicyInfo infoSub = { @@ -430,7 +430,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy007, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy008, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy008, TestSize.Level0) { std::vector policyParent; std::vector policySub; @@ -473,7 +473,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy008, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy009, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy009, TestSize.Level0) { std::vector policySub; std::vector policyParent; @@ -522,7 +522,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy009, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy010, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy010, TestSize.Level0) { std::vector policy; std::vector policyParent; @@ -569,7 +569,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy010, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy011, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy011, TestSize.Level0) { std::vector policy; std::vector policySub; @@ -616,7 +616,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy011, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy012, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy012, TestSize.Level0) { std::vector policy; std::vector persistResult; @@ -664,7 +664,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy012, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy013, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy013, TestSize.Level0) { std::vector policy; std::vector policySub; @@ -791,7 +791,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy015, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PersistPolicy016, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PersistPolicy016, TestSize.Level0) { std::vector policy; std::vector searchPolicy; @@ -843,7 +843,7 @@ HWTEST_F(SandboxManagerKitTest, PersistPolicy016, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, MassiveIPCTest001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, MassiveIPCTest001, TestSize.Level0) { std::vector policy; uint64_t policySize = 90000; @@ -895,7 +895,7 @@ HWTEST_F(SandboxManagerKitTest, MassiveIPCTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, MassiveIPCTest002, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, MassiveIPCTest002, TestSize.Level0) { std::vector policy; uint64_t policySize = 90000; @@ -961,7 +961,7 @@ HWTEST_F(SandboxManagerKitTest, MassiveIPCTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, MassiveIPCTest003, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, MassiveIPCTest003, TestSize.Level0) { std::vector policy; uint64_t policySize = 90000; @@ -1024,7 +1024,7 @@ HWTEST_F(SandboxManagerKitTest, MassiveIPCTest003, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, MassiveIPCTest004, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, MassiveIPCTest004, TestSize.Level0) { std::vector policy; uint64_t policySize = 90000; @@ -1088,7 +1088,7 @@ HWTEST_F(SandboxManagerKitTest, MassiveIPCTest004, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, MassiveIPCTest005, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, MassiveIPCTest005, TestSize.Level0) { std::vector policy; uint64_t policySize = 90000; @@ -1150,7 +1150,7 @@ HWTEST_F(SandboxManagerKitTest, MassiveIPCTest005, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, MassiveIPCTest006, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, MassiveIPCTest006, TestSize.Level0) { std::vector policy; uint64_t policySize = 90000; @@ -1214,7 +1214,7 @@ HWTEST_F(SandboxManagerKitTest, MassiveIPCTest006, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, MassiveIPCTest007, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, MassiveIPCTest007, TestSize.Level0) { std::vector policy; uint64_t policySize = 90000; @@ -1275,7 +1275,7 @@ HWTEST_F(SandboxManagerKitTest, MassiveIPCTest007, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest001, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 0; @@ -1322,7 +1322,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest002, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest002, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 0; @@ -1370,7 +1370,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest003, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest003, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -1418,7 +1418,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest003, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest004, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest004, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -1473,7 +1473,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest004, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest005, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest005, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -1528,7 +1528,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest005, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest006, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest006, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -1572,7 +1572,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest006, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest007, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest007, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -1605,7 +1605,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest007, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest008, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest008, TestSize.Level0) { std::vector policyA; std::vector result; @@ -1658,7 +1658,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest008, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CheckPolicyTest009, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CheckPolicyTest009, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -1700,7 +1700,7 @@ HWTEST_F(SandboxManagerKitTest, CheckPolicyTest009, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest001, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -1727,7 +1727,7 @@ HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest002, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest002, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -1776,7 +1776,7 @@ HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest003, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest003, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -1810,7 +1810,7 @@ HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest003, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest004, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, UnSetPolicyTest004, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -1868,7 +1868,7 @@ HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest002, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest002, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -1914,7 +1914,7 @@ HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest003, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest003, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -1944,7 +1944,7 @@ HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest003, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest004, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest004, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -1975,7 +1975,7 @@ HWTEST_F(SandboxManagerKitTest, PolicyAsyncTest004, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest001, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -2026,7 +2026,7 @@ HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest001, TestSize.Level1 * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest002, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest002, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -2087,7 +2087,7 @@ HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest002, TestSize.Level1 * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest003, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest003, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -2159,7 +2159,7 @@ HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest004, TestSize.Level1 * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest005, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest005, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -2198,7 +2198,7 @@ HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest005, TestSize.Level1 * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest006, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, CleanPersistPolicyByPathTest006, TestSize.Level0) { std::vector policy; uint64_t policyFlag = 1; @@ -2281,7 +2281,7 @@ HWTEST_F(SandboxManagerKitTest, StartAccessingByTokenIdTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, StartAccessingByTokenIdTest003, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, StartAccessingByTokenIdTest003, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -2337,7 +2337,7 @@ HWTEST_F(SandboxManagerKitTest, StartAccessingByTokenIdTest003, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(SandboxManagerKitTest, UnSetAllPolicyByTokenTest001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, UnSetAllPolicyByTokenTest001, TestSize.Level0) { std::vector policyA; uint64_t policyFlag = 1; @@ -2363,7 +2363,7 @@ HWTEST_F(SandboxManagerKitTest, UnSetAllPolicyByTokenTest001, TestSize.Level1) EXPECT_FALSE(result[0]); } -HWTEST_F(SandboxManagerKitTest, TimestampTest001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, TimestampTest001, TestSize.Level0) { SetDeny("/a/b"); std::vector policyA; @@ -2397,7 +2397,7 @@ HWTEST_F(SandboxManagerKitTest, TimestampTest001, TestSize.Level1) EXPECT_FALSE(result[0]); } -HWTEST_F(SandboxManagerKitTest, TimestampTest002, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, TimestampTest002, TestSize.Level0) { SetDeny("/a/b"); std::vector policyA; @@ -2436,7 +2436,7 @@ HWTEST_F(SandboxManagerKitTest, TimestampTest002, TestSize.Level1) EXPECT_FALSE(result[0]); } -HWTEST_F(SandboxManagerKitTest, UserIdTest001, TestSize.Level1) +HWTEST_F(SandboxManagerKitTest, UserIdTest001, TestSize.Level0) { MacAdapter macAdapter; macAdapter.Init(); diff --git a/services/sandbox_manager/main/cpp/include/database/sandbox_manager_rdb.h b/services/sandbox_manager/main/cpp/include/database/sandbox_manager_rdb.h index 354e1bf..bee46d6 100644 --- a/services/sandbox_manager/main/cpp/include/database/sandbox_manager_rdb.h +++ b/services/sandbox_manager/main/cpp/include/database/sandbox_manager_rdb.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -44,6 +44,8 @@ public: int32_t Remove(const DataType type, const GenericValues &conditions); + int32_t Remove(const DataType type, const std::vector &conditions); + int32_t Modify(const DataType type, const GenericValues &modifyValues, const GenericValues &conditions); int32_t FindSubPath(const DataType type, const std::string& filePath, std::vector& results); @@ -54,7 +56,7 @@ private: SandboxManagerRdb(); int32_t OpenDataBase(); std::shared_ptr GetRdb(); - void DbInsertFailure(const std::string& tableName, int32_t res); + void DbOperateFailure(const std::string& tableName, int32_t res); inline static int32_t GetConflictResolution(const std::string &duplicateMode, NativeRdb::ConflictResolution &solution); DISALLOW_COPY_AND_MOVE(SandboxManagerRdb); diff --git a/services/sandbox_manager/main/cpp/src/database/sandbox_manager_rdb.cpp b/services/sandbox_manager/main/cpp/src/database/sandbox_manager_rdb.cpp index e252a52..da8cd67 100644 --- a/services/sandbox_manager/main/cpp/src/database/sandbox_manager_rdb.cpp +++ b/services/sandbox_manager/main/cpp/src/database/sandbox_manager_rdb.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -123,7 +123,7 @@ int32_t SandboxManagerRdb::GetConflictResolution(const std::string &duplicateMod return FAILURE; } -void SandboxManagerRdb::DbInsertFailure(const std::string& tableName, int32_t res) +void SandboxManagerRdb::DbOperateFailure(const std::string& tableName, int32_t res) { SANDBOXMANAGER_LOG_ERROR(LABEL, "Failed to batch insert into table %{public}s, res is %{public}d.", tableName.c_str(), res); @@ -172,13 +172,15 @@ int32_t SandboxManagerRdb::Add(const DataType type, const std::vectorInsertWithConflictResolution(rowId, tableName, bucket, solution); if (res != NativeRdb::E_OK) { - DbInsertFailure(tableName, res); + SANDBOXMANAGER_LOG_ERROR(LABEL, "Failed to batch insert into table %{public}s, res is %{public}d.", + tableName.c_str(), res); + DbOperateFailure(tableName, res); return FAILURE; } } res = db_->Commit(); if (res != NativeRdb::E_OK) { - DbInsertFailure(tableName, res); + DbOperateFailure(tableName, res); return FAILURE; } return SUCCESS; @@ -219,6 +221,42 @@ int32_t SandboxManagerRdb::Remove(const DataType type, const GenericValues& cond return SUCCESS; } +int32_t SandboxManagerRdb::Remove(const DataType type, const std::vector &conditions) +{ + std::string tableName; + GetTableNameByType(type, tableName); + if (tableName.empty()) { + return FAILURE; + } + SANDBOXMANAGER_LOG_DEBUG(LABEL, "Remove tableName: %{public}s", tableName.c_str()); + + OHOS::Utils::UniqueWriteGuard lock(this->rwLock_); + std::shared_ptr db = GetRdb(); + if (db == nullptr) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Db is null, open db first"); + return FAILURE; + } + db_->BeginTransaction(); + for (const auto& condition : conditions) { + NativeRdb::RdbPredicates predicates(tableName); + ToRdbPredicates(condition, predicates); + int32_t deletedRows = 0; + int32_t res = db_->Delete(deletedRows, predicates); + if (res != NativeRdb::E_OK) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Failed to delete record from table %{public}s, res is %{public}d.", + tableName.c_str(), res); + DbOperateFailure(tableName, res); + return FAILURE; + } + } + int32_t res = db_->Commit(); + if (res != NativeRdb::E_OK) { + DbOperateFailure(tableName, res); + return FAILURE; + } + return SUCCESS; +} + int32_t SandboxManagerRdb::Modify(const DataType type, const GenericValues& modifyValues, const GenericValues& conditions) { diff --git a/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp b/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp index 76e40e3..966a21e 100644 --- a/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp +++ b/services/sandbox_manager/main/cpp/src/service/policy_info_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023-2024 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -406,6 +406,7 @@ int32_t PolicyInfoManager::RemovePolicy( uint32_t invalidNum = 0; uint32_t failNum = 0; uint32_t successNum = 0; + std::vector conditions; for (size_t i = 0; i < policySize; ++i) { int32_t checkPolicyRet = CheckPolicyValidity(policy[i]); if (checkPolicyRet != SANDBOX_MANAGER_OK) { @@ -420,7 +421,7 @@ int32_t PolicyInfoManager::RemovePolicy( ++successNum; continue; } - + ret = macAdapter_.UnSetSandboxPolicy(tokenId, policy[i]); if (ret != SANDBOX_MANAGER_OK) { SANDBOXMANAGER_LOG_ERROR(LABEL, "MacAdapter unset error, err code = %{public}d.", ret); @@ -432,14 +433,14 @@ int32_t PolicyInfoManager::RemovePolicy( GenericValues condition; TransferPolicyToGeneric(tokenId, policy[i], condition); - ret = SandboxManagerRdb::GetInstance().Remove( - SANDBOX_MANAGER_PERSISTED_POLICY, condition); - if (ret != SandboxManagerRdb::SUCCESS) { - SANDBOXMANAGER_LOG_ERROR(LABEL, "Database operate error"); - return SANDBOX_MANAGER_DB_ERR; - } + conditions.emplace_back(condition); ++successNum; } + int32_t ret = SandboxManagerRdb::GetInstance().Remove(SANDBOX_MANAGER_PERSISTED_POLICY, conditions); + if (ret != SandboxManagerRdb::SUCCESS) { + SANDBOXMANAGER_LOG_ERROR(LABEL, "Database operate error"); + return SANDBOX_MANAGER_DB_ERR; + } PolicyOperateInfo info(result.size(), successNum, failNum, invalidNum); SandboxManagerDfxHelper::WritePersistPolicyOperateSucc(OperateTypeEnum::UNPERSIST_POLICY, info); return SANDBOX_MANAGER_OK; @@ -881,7 +882,6 @@ int32_t PolicyInfoManager::CheckPolicyValidity(const PolicyInfo &policy) SANDBOXMANAGER_LOG_ERROR(LABEL, "Policy path check fail, length = %{public}zu", policy.path.length()); return SandboxRetType::INVALID_PATH; } - std::string path = AdjustPath(policy.path); // mode between 0 and 0b11(READ_MODE+WRITE_MODE) if (policy.mode < OperateMode::READ_MODE || diff --git a/services/sandbox_manager/test/unittest/policy_info_manager_test.cpp b/services/sandbox_manager/test/unittest/policy_info_manager_test.cpp index 77ce987..afc7dcc 100644 --- a/services/sandbox_manager/test/unittest/policy_info_manager_test.cpp +++ b/services/sandbox_manager/test/unittest/policy_info_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -109,7 +109,7 @@ void PrintDbRecords() * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, PolicyInfoManagerTest002, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, PolicyInfoManagerTest002, TestSize.Level0) { PolicyInfo info; uint64_t sizeLimit = 1; @@ -161,7 +161,7 @@ HWTEST_F(PolicyInfoManagerTest, PolicyInfoManagerTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, PolicyInfoManagerTest004, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, PolicyInfoManagerTest004, TestSize.Level0) { PolicyInfo info; uint64_t sizeLimit = 1; @@ -313,7 +313,7 @@ HWTEST_F(PolicyInfoManagerTest, GenericValuesTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, CleanPersistPolicyByPathTest001, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, CleanPersistPolicyByPathTest001, TestSize.Level0) { PolicyInfo info; uint64_t sizeLimit = 1; @@ -377,7 +377,7 @@ HWTEST_F(PolicyInfoManagerTest, MacAdapterTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest001, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest001, TestSize.Level0) { std::string stringJson1 = R"([{"path":"/data/test", "rename":1, "delete":1, "inherit":1}])"; EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().macAdapter_.SetDenyCfg(stringJson1)); @@ -389,7 +389,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest002, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest002, TestSize.Level0) { std::string stringJson1 = R"([ {"path":"/etc/test", "rename":1, "delete":"test", "inherit":1}])"; @@ -402,7 +402,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest003, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest003, TestSize.Level0) { std::string stringJson1 = R"([ {"path":"/etc/test", "rename":"test", "delete":1, "inherit":1}])"; @@ -415,7 +415,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest003, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest004, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest004, TestSize.Level0) { std::string stringJson1 = R"([ {"path":"/etc/test", "rename":1, "delete":1, "inherit":"test"}])"; @@ -428,7 +428,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest004, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest005, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest005, TestSize.Level0) { std::string stringJson1 = R"([ {"path":1, "rename":1, "delete":1, "inherit":1}])"; @@ -441,7 +441,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest005, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest006, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest006, TestSize.Level0) { std::string stringJson1 = R"([])"; EXPECT_EQ(SANDBOX_MANAGER_OK, PolicyInfoManager::GetInstance().macAdapter_.SetDenyCfg(stringJson1)); @@ -453,7 +453,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest006, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest007, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest007, TestSize.Level0) { std::string stringJson1 = R"([ {"path":"/etc/test", "rename":1, "delete":1, "inherit":1}, @@ -475,7 +475,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest007, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest008, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest008, TestSize.Level0) { std::string stringJson1 = R"([ {"path":"/etc/test", "delete":1, "inherit":1}])"; @@ -488,7 +488,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest008, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest009, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest009, TestSize.Level0) { const char *jsonPath = "etc/sandbox_manager_service/test"; std::string inputString; @@ -502,7 +502,7 @@ HWTEST_F(PolicyInfoManagerTest, DenyTest009, TestSize.Level1) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(PolicyInfoManagerTest, DenyTest010, TestSize.Level1) +HWTEST_F(PolicyInfoManagerTest, DenyTest010, TestSize.Level0) { const char *jsonPath = "etc/sandbox/appdata-sandbox.json"; std::string inputString; -- Gitee