From a5bcdeb1366b6e4db7f92e620f2ec42cd4f69386 Mon Sep 17 00:00:00 2001 From: unknown Date: Sat, 17 Jan 2026 16:38:14 +0800 Subject: [PATCH] add agent mgr Signed-off-by: unknown --- .../services/agentmgr/BUILD.gn | 3 + .../agentmgr/include/agent_card_db_mgr.h | 58 +++++ .../agentmgr/include/agent_card_mgr.h | 44 ++++ .../agentmgr/src/agent_card_db_mgr.cpp | 214 ++++++++++++++++++ .../services/agentmgr/src/agent_card_mgr.cpp | 93 ++++++++ .../unittest/agent_runtime_framework/BUILD.gn | 2 + .../agent_card_db_mgr_test/BUILD.gn | 66 ++++++ .../agent_card_db_mgr_test.cpp | 133 +++++++++++ .../agent_card_mgr_test/BUILD.gn | 68 ++++++ .../agent_card_mgr_test.cpp | 85 +++++++ .../mock/include/bundle_mgr_client.h | 38 ++++ .../mock/src/bundle_mgr_client.cpp | 41 ++++ 12 files changed, 845 insertions(+) create mode 100644 agent_runtime_framework/services/agentmgr/include/agent_card_db_mgr.h create mode 100644 agent_runtime_framework/services/agentmgr/include/agent_card_mgr.h create mode 100644 agent_runtime_framework/services/agentmgr/src/agent_card_db_mgr.cpp create mode 100644 agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp create mode 100644 test/unittest/agent_runtime_framework/agent_card_db_mgr_test/BUILD.gn create mode 100644 test/unittest/agent_runtime_framework/agent_card_db_mgr_test/agent_card_db_mgr_test.cpp create mode 100644 test/unittest/agent_runtime_framework/agent_card_mgr_test/BUILD.gn create mode 100644 test/unittest/agent_runtime_framework/agent_card_mgr_test/agent_card_mgr_test.cpp create mode 100644 test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/include/bundle_mgr_client.h create mode 100644 test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/src/bundle_mgr_client.cpp diff --git a/agent_runtime_framework/services/agentmgr/BUILD.gn b/agent_runtime_framework/services/agentmgr/BUILD.gn index 55682a4d86..04e0a0f7c5 100644 --- a/agent_runtime_framework/services/agentmgr/BUILD.gn +++ b/agent_runtime_framework/services/agentmgr/BUILD.gn @@ -28,6 +28,8 @@ ohos_shared_library("agentmgr") { ] sources = [ "src/agent_bundle_event_callback.cpp", + "src/agent_card_db_mgr.cpp", + "src/agent_card_mgr.cpp", "src/agent_event_handler.cpp", "src/agent_manager_service.cpp", ] @@ -51,6 +53,7 @@ ohos_shared_library("agentmgr") { "hilog:libhilog", "ipc:ipc_core", "json:nlohmann_json_static", + "kv_store:distributeddata_inner", "safwk:system_ability_fwk", "samgr:samgr_proxy", ] diff --git a/agent_runtime_framework/services/agentmgr/include/agent_card_db_mgr.h b/agent_runtime_framework/services/agentmgr/include/agent_card_db_mgr.h new file mode 100644 index 0000000000..d15214b26a --- /dev/null +++ b/agent_runtime_framework/services/agentmgr/include/agent_card_db_mgr.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2026 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 OHOS_AGENT_RUNTIME_AGENT_CARD_DB_MGR_H +#define OHOS_AGENT_RUNTIME_AGENT_CARD_DB_MGR_H + +#include +#include + +#include "agent_card.h" +#include "distributed_kv_data_manager.h" +#include "nlohmann/json.hpp" +#include "singleton.h" + +namespace OHOS { +namespace AgentRuntime { +class AgentCardDbMgr { +public: + static AgentCardDbMgr &GetInstance(); + + int32_t InsertData(const std::string &bundleName, int32_t userId, const std::vector &cards); + + int32_t DeleteData(const std::string &bundleName, int32_t userId); + + int32_t QueryData(const std::string &bundleName, int32_t userId, std::vector &cards); + +private: + AgentCardDbMgr(); + ~AgentCardDbMgr(); + DistributedKv::Options CreateKvStoreOptions(); + DistributedKv::Status RestoreCorruptedKvStore(const DistributedKv::Options& options); + DistributedKv::Status RestoreKvStore(DistributedKv::Status status); + DistributedKv::Status GetKvStore(); + bool CheckKvStore(); + DistributedKv::Value ConvertValue(const std::vector &cards); + DistributedKv::Key ConvertKey(const std::string &bundleName, int32_t userId); + + static const DistributedKv::AppId APP_ID; + static const DistributedKv::StoreId STORE_ID; + DistributedKv::DistributedKvDataManager dataManager_; + std::shared_ptr kvStorePtr_; + mutable std::mutex kvStorePtrMutex_; +}; +} // namespace AgentRuntime +} // namespace OHOS +#endif // OHOS_AGENT_RUNTIME_AGENT_CARD_DB_MGR_H \ No newline at end of file diff --git a/agent_runtime_framework/services/agentmgr/include/agent_card_mgr.h b/agent_runtime_framework/services/agentmgr/include/agent_card_mgr.h new file mode 100644 index 0000000000..355f9b5e91 --- /dev/null +++ b/agent_runtime_framework/services/agentmgr/include/agent_card_mgr.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2026 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 OHOS_AGENT_RUNTIME_AGENT_CARD_MGR_H +#define OHOS_AGENT_RUNTIME_AGENT_CARD_MGR_H + +#include +#include + +#include "agent_card.h" +#include "bundle_mgr_client.h" + +namespace OHOS { +namespace AgentRuntime { +class AgentCardMgr { +public: + static AgentCardMgr &GetInstance(); + + int32_t HandleBundleInstall(const std::string &bundleName, int32_t userId); + + int32_t HandleBundleUpdate(const std::string &bundleName, int32_t userId); + + int32_t HandleBundleRemove(const std::string &bundleName, int32_t userId); + +private: + OHOS::AppExecFwk::BundleMgrClient bundleMgrClient_; + AgentCardMgr(); + ~AgentCardMgr(); +}; +} // namespace AgentRuntime +} // namespace OHOS +#endif // OHOS_AGENT_RUNTIME_AGENT_CARD_MGR_H \ No newline at end of file diff --git a/agent_runtime_framework/services/agentmgr/src/agent_card_db_mgr.cpp b/agent_runtime_framework/services/agentmgr/src/agent_card_db_mgr.cpp new file mode 100644 index 0000000000..b2593b199d --- /dev/null +++ b/agent_runtime_framework/services/agentmgr/src/agent_card_db_mgr.cpp @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2026 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. + */ + +#include "agent_card_db_mgr.h" + +#include + +#include "ability_manager_errors.h" +#include "hilog_tag_wrapper.h" +#include "json_utils.h" + +namespace OHOS { +namespace AgentRuntime { +namespace { +constexpr int32_t CHECK_INTERVAL = 100000; // 100ms +constexpr int32_t MAX_TIMES = 5; // 5 * 100ms = 500ms +constexpr const char *KEEP_ALIVE_STORAGE_DIR = "/data/service/el1/public/database/ability_manager_service"; +const std::string JSON_KEY_BUNDLE_NAME = "bundleName"; +const std::string JSON_KEY_USER_ID = "userId"; +const std::string JSON_KEY_URL = "url"; +} // namespace + +const DistributedKv::AppId AgentCardDbMgr::APP_ID = { "agent_db" }; +const DistributedKv::StoreId AgentCardDbMgr::STORE_ID = { "agent_card_infos" }; + +AgentCardDbMgr &AgentCardDbMgr::GetInstance() +{ + static AgentCardDbMgr instance; + return instance; +} + +AgentCardDbMgr::AgentCardDbMgr() {} + +AgentCardDbMgr::~AgentCardDbMgr() +{ + std::lock_guard lock(kvStorePtrMutex_); + if (kvStorePtr_ != nullptr) { + dataManager_.CloseKvStore(APP_ID, kvStorePtr_); + } +} + +DistributedKv::Options AgentCardDbMgr::CreateKvStoreOptions() +{ + return { + .createIfMissing = true, + .encrypt = false, + .autoSync = false, + .syncable = false, + .securityLevel = DistributedKv::SecurityLevel::S2, + .area = DistributedKv::EL1, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = KEEP_ALIVE_STORAGE_DIR, + }; +} + +DistributedKv::Status AgentCardDbMgr::RestoreCorruptedKvStore(const DistributedKv::Options& options) +{ + TAG_LOGE(AAFwkTag::SER_ROUTER, "corrupted, deleting db"); + dataManager_.DeleteKvStore(APP_ID, STORE_ID, options.baseDir); + TAG_LOGE(AAFwkTag::SER_ROUTER, "deleted corrupted db, recreating db"); + DistributedKv::Status status = dataManager_.GetSingleKvStore(options, APP_ID, STORE_ID, kvStorePtr_); + TAG_LOGE(AAFwkTag::SER_ROUTER, "recreate db result:%{public}d", status); + return status; +} + +DistributedKv::Status AgentCardDbMgr::RestoreKvStore(DistributedKv::Status status) +{ + if (status == DistributedKv::Status::DATA_CORRUPTED) { + DistributedKv::Options options = CreateKvStoreOptions(); + status = RestoreCorruptedKvStore(options); + } + return status; +} + +DistributedKv::Status AgentCardDbMgr::GetKvStore() +{ + DistributedKv::Options options = { + .createIfMissing = true, + .encrypt = false, + .autoSync = false, + .syncable = false, + .securityLevel = DistributedKv::SecurityLevel::S2, + .area = DistributedKv::EL1, + .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION, + .baseDir = KEEP_ALIVE_STORAGE_DIR, + }; + + DistributedKv::Status status = dataManager_.GetSingleKvStore(options, APP_ID, STORE_ID, kvStorePtr_); + if (status != DistributedKv::Status::SUCCESS) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "Error: %{public}d", status); + status = RestoreKvStore(status); + return status; + } + + TAG_LOGD(AAFwkTag::SER_ROUTER, "Get kvStore success"); + return status; +} + +bool AgentCardDbMgr::CheckKvStore() +{ + if (kvStorePtr_ != nullptr) { + return true; + } + int32_t tryTimes = MAX_TIMES; + while (tryTimes > 0) { + DistributedKv::Status status = GetKvStore(); + if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) { + return true; + } + TAG_LOGE(AAFwkTag::SER_ROUTER, "Try times: %{public}d", tryTimes); + usleep(CHECK_INTERVAL); + tryTimes--; + } + return kvStorePtr_ != nullptr; +} + +int32_t AgentCardDbMgr::InsertData(const std::string &bundleName, int32_t userId, const std::vector &cards) +{ + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "null kvStore"); + return ERR_NO_INIT; + } + + DistributedKv::Key key = ConvertKey(bundleName, userId); + DistributedKv::Value value = ConvertValue(cards); + DistributedKv::Status status = kvStorePtr_->Put(key, value); + if (status != DistributedKv::Status::SUCCESS) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "kvStore insert error: %{public}d", status); + status = RestoreKvStore(status); + return ERR_INVALID_OPERATION; + } + return ERR_OK; +} + +int32_t AgentCardDbMgr::DeleteData(const std::string &bundleName, int32_t userId) +{ + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "null kvStore"); + return ERR_NO_INIT; + } + + DistributedKv::Key key = ConvertKey(bundleName, userId); + DistributedKv::Status status = kvStorePtr_->Delete(key); + if (status != DistributedKv::Status::SUCCESS) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "kvStore delete error: %{public}d", status); + status = RestoreKvStore(status); + return ERR_INVALID_OPERATION; + } + + return ERR_OK; +} + +int32_t AgentCardDbMgr::QueryData(const std::string &bundleName, int32_t userId, std::vector &cards) +{ + std::lock_guard lock(kvStorePtrMutex_); + if (!CheckKvStore()) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "null kvStore"); + return ERR_NO_INIT; + } + DistributedKv::Key key = ConvertKey(bundleName, userId); + DistributedKv::Value value; + DistributedKv::Status status = kvStorePtr_->Get(key, value); + if (status != DistributedKv::Status::SUCCESS) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "QueryData error: %{public}d", status); + RestoreKvStore(status); + return ERR_INVALID_OPERATION; + } + if (!nlohmann::json::accept(value.ToString())) { + return AAFwk::INNER_ERR; + } + nlohmann::json jsonArray = nlohmann::json::parse(value.ToString(), nullptr, false); + for (const auto &item : jsonArray) { + cards.push_back(AgentCard::FromJson(item)); + } + return ERR_OK; +} + +DistributedKv::Value AgentCardDbMgr::ConvertValue(const std::vector &cards) +{ + nlohmann::json jsonArray = nlohmann::json::array(); + for (const auto &item : cards) { + jsonArray.push_back(item.ToJson()); + } + DistributedKv::Value value(jsonArray.dump()); + TAG_LOGD(AAFwkTag::SER_ROUTER, "value: %{public}s", value.ToString().c_str()); + return value; +} + +DistributedKv::Key AgentCardDbMgr::ConvertKey(const std::string &bundleName, int32_t userId) +{ + nlohmann::json jsonObject = nlohmann::json { + { JSON_KEY_BUNDLE_NAME, bundleName }, + { JSON_KEY_USER_ID, userId }, + }; + DistributedKv::Key key(jsonObject.dump()); + TAG_LOGD(AAFwkTag::SER_ROUTER, "key: %{public}s", key.ToString().c_str()); + return key; +} +} // namespace AgentRuntime +} // namespace OHOS diff --git a/agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp b/agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp new file mode 100644 index 0000000000..907fbadd12 --- /dev/null +++ b/agent_runtime_framework/services/agentmgr/src/agent_card_mgr.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2026 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. + */ + +#include "agent_card_mgr.h" + +#include + +#include "agent_card.h" +#include "agent_card_db_mgr.h" +#include "hilog_tag_wrapper.h" +#include "json_utils.h" + +namespace OHOS { +namespace AgentRuntime { +using namespace OHOS::AppExecFwk; +using json = nlohmann::json; +namespace { +const std::string AGENT_CONFIG = "ohos.extension.agent"; +} // namespace +AgentCardMgr &AgentCardMgr::GetInstance() +{ + static AgentCardMgr instance; + return instance; +} + +AgentCardMgr::AgentCardMgr() {} + +AgentCardMgr::~AgentCardMgr() {} + +int32_t AgentCardMgr::HandleBundleInstall(const std::string &bundleName, int32_t userId) +{ + if (bundleName.empty()) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "invalid bundleName"); + return -1; + } + BundleInfo bundleInfo; + bool result = bundleMgrClient_.GetBundleInfo(bundleName, BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, + bundleInfo, userId); + if (!result) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "Get Bundle Info fail"); + return -1; + } + std::vector cards; + for (auto const &extensionInfo : bundleInfo.extensionInfos) { + if (extensionInfo.type != ExtensionAbilityType::BACKUP) { + continue; + } + std::vector profileInfos{}; + bundleMgrClient_.GetResConfigFile(extensionInfo, AGENT_CONFIG, profileInfos); + for (const std::string &profileInfo : profileInfos) { + if (!json::accept(profileInfo)) { + return -1; + } + json j = json::parse(profileInfo); + if (!j.contains("agent_cards")) { + return -1; + } + for (auto cardStr : j["agent_cards"]) { + AgentCard card = AgentCard::FromJson(cardStr); + cards.push_back(card); + } + } + } + return AgentCardDbMgr::GetInstance().InsertData(bundleName, userId, cards); +} + +int32_t AgentCardMgr::HandleBundleUpdate(const std::string &bundleName, int32_t userId) +{ + return HandleBundleInstall(bundleName, userId); +} + +int32_t AgentCardMgr::HandleBundleRemove(const std::string &bundleName, int32_t userId) +{ + if (bundleName.empty()) { + TAG_LOGE(AAFwkTag::SER_ROUTER, "invalid bundleName"); + return -1; + } + return AgentCardDbMgr::GetInstance().DeleteData(bundleName, userId); +} +} // namespace AgentRuntime +} // namespace OHOS diff --git a/test/unittest/agent_runtime_framework/BUILD.gn b/test/unittest/agent_runtime_framework/BUILD.gn index 81b7aa1551..00e2e408e4 100644 --- a/test/unittest/agent_runtime_framework/BUILD.gn +++ b/test/unittest/agent_runtime_framework/BUILD.gn @@ -18,6 +18,8 @@ group("unittest") { testonly = true deps = [ "agent_bundle_event_callback_test:agent_bundle_event_callback_test", + "agent_card_db_mgr_test:agent_card_db_mgr_test", + "agent_card_mgr_test:agent_card_mgr_test", "agent_card_test:agent_card_test", "agent_load_callback_test:agent_load_callback_test", "agent_manager_client_test:agent_manager_client_test", diff --git a/test/unittest/agent_runtime_framework/agent_card_db_mgr_test/BUILD.gn b/test/unittest/agent_runtime_framework/agent_card_db_mgr_test/BUILD.gn new file mode 100644 index 0000000000..ae5719425b --- /dev/null +++ b/test/unittest/agent_runtime_framework/agent_card_db_mgr_test/BUILD.gn @@ -0,0 +1,66 @@ +# Copyright (c) 2026 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. + +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_unittest("agent_card_db_mgr_test") { + module_out_path = "ability_runtime/ability_runtime/agent_card_db_mgr_test" + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "../../../cfi_blocklist.txt" + } + + sources = [ + "agent_card_db_mgr_test.cpp", + "${agent_runtime_framework_path}/services/agentmgr/src/agent_card_db_mgr.cpp", + ] + + include_dirs = [ + "mock/include", + "${ability_runtime_innerkits_path}/ability_manager/include", + "${ability_runtime_services_path}/common/include", + "${agent_runtime_framework_path}/interfaces/inner_api/include", + "${agent_runtime_framework_path}/services/agentmgr/include", + ] + + deps = [ + "${agent_runtime_framework_path}/interfaces/inner_api:agent_fwk", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "bundle_framework:libappexecfwk_common", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "json:nlohmann_json_static", + "kv_store:distributeddata_inner", + ] + + cflags_cc = [] + + configs = [] +} + +group("unittest") { + testonly = true + deps = [ ":agent_card_db_mgr_test" ] +} \ No newline at end of file diff --git a/test/unittest/agent_runtime_framework/agent_card_db_mgr_test/agent_card_db_mgr_test.cpp b/test/unittest/agent_runtime_framework/agent_card_db_mgr_test/agent_card_db_mgr_test.cpp new file mode 100644 index 0000000000..099c504390 --- /dev/null +++ b/test/unittest/agent_runtime_framework/agent_card_db_mgr_test/agent_card_db_mgr_test.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2026 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. + */ + +#include +#include +#define private public +#define protected public +#include "agent_card_db_mgr.h" +#undef private +#undef protected + +using namespace OHOS; +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AgentRuntime { +class AgentCardDbMgrTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() override; + void TearDown() override; +}; + +void AgentCardDbMgrTest::SetUpTestCase(void) +{} + +void AgentCardDbMgrTest::TearDownTestCase(void) +{} + +void AgentCardDbMgrTest::SetUp(void) +{} + +void AgentCardDbMgrTest::TearDown(void) +{} + +/** + * @tc.name: RestoreKvStoreTest_001 + * @tc.desc: RestoreKvStoreTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardDbMgrTest, RestoreKvStoreTest_001, TestSize.Level1) +{ + AgentCardDbMgr agentCardDbMgr; + DistributedKv::Status ret = agentCardDbMgr.RestoreKvStore(DistributedKv::Status::DATA_CORRUPTED); + EXPECT_TRUE(ret != DistributedKv::Status::DATA_CORRUPTED); + + ret = agentCardDbMgr.RestoreKvStore(DistributedKv::Status::SUCCESS); + EXPECT_TRUE(ret == DistributedKv::Status::SUCCESS); +} + +/** + * @tc.name: GetKvStoreTest_001 + * @tc.desc: GetKvStoreTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardDbMgrTest, GetKvStoreTest_001, TestSize.Level1) +{ + AgentCardDbMgr agentCardDbMgr; + DistributedKv::Status ret = agentCardDbMgr.GetKvStore(); + EXPECT_TRUE(ret == DistributedKv::Status::SUCCESS); +} + +/** + * @tc.name: CheckKvStoreTest_001 + * @tc.desc: CheckKvStoreTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardDbMgrTest, CheckKvStoreTest_001, TestSize.Level1) +{ + AgentCardDbMgr agentCardDbMgr; + bool ret = agentCardDbMgr.CheckKvStore(); + EXPECT_TRUE(ret); +} + +/** + * @tc.name: InsertDataTest_001 + * @tc.desc: InsertDataTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardDbMgrTest, InsertDataTest_001, TestSize.Level1) +{ + std::vector cards; + AgentCardDbMgr agentCardDbMgr; + int ret = agentCardDbMgr.InsertData("test", 100, cards); + EXPECT_TRUE(ret == ERR_OK); +} + +/** + * @tc.name: DeleteDataTest_001 + * @tc.desc: DeleteDataTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardDbMgrTest, DeleteDataTest_001, TestSize.Level1) +{ + AgentCardDbMgr agentCardDbMgr; + int ret = agentCardDbMgr.DeleteData("test", 100); + EXPECT_TRUE(ret == ERR_OK); +} + +/** + * @tc.name: QueryDataTest_001 + * @tc.desc: QueryDataTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardDbMgrTest, QueryDataTest_001, TestSize.Level1) +{ + std::vector cards; + AgentCardDbMgr agentCardDbMgr; + int ret = agentCardDbMgr.QueryData("test", 100, cards); + EXPECT_TRUE(ret != ERR_OK); +} +} // namespace AgentRuntime +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/agent_runtime_framework/agent_card_mgr_test/BUILD.gn b/test/unittest/agent_runtime_framework/agent_card_mgr_test/BUILD.gn new file mode 100644 index 0000000000..b5ae3dab65 --- /dev/null +++ b/test/unittest/agent_runtime_framework/agent_card_mgr_test/BUILD.gn @@ -0,0 +1,68 @@ +# Copyright (c) 2026 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. + +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/ability/ability_runtime/ability_runtime.gni") + +ohos_unittest("agent_card_mgr_test") { + module_out_path = "ability_runtime/ability_runtime/agent_card_mgr_test" + + sanitize = { + cfi = true + cfi_cross_dso = true + debug = false + blocklist = "../../../cfi_blocklist.txt" + } + + sources = [ + "agent_card_mgr_test.cpp", + "mock/src/bundle_mgr_client.cpp", + "${agent_runtime_framework_path}/services/agentmgr/src/agent_card_db_mgr.cpp", + "${agent_runtime_framework_path}/services/agentmgr/src/agent_card_mgr.cpp", + ] + + include_dirs = [ + "mock/include", + "${ability_runtime_innerkits_path}/ability_manager/include", + "${ability_runtime_services_path}/common/include", + "${agent_runtime_framework_path}/interfaces/inner_api/include", + "${agent_runtime_framework_path}/services/agentmgr/include", + ] + + deps = [ + "${agent_runtime_framework_path}/interfaces/inner_api:agent_fwk", + ] + + external_deps = [ + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "bundle_framework:libappexecfwk_common", + "c_utils:utils", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "ipc:ipc_core", + "json:nlohmann_json_static", + "kv_store:distributeddata_inner", + ] + + cflags_cc = [] + + configs = [] +} + +group("unittest") { + testonly = true + deps = [ ":agent_card_mgr_test" ] +} \ No newline at end of file diff --git a/test/unittest/agent_runtime_framework/agent_card_mgr_test/agent_card_mgr_test.cpp b/test/unittest/agent_runtime_framework/agent_card_mgr_test/agent_card_mgr_test.cpp new file mode 100644 index 0000000000..52d8db639c --- /dev/null +++ b/test/unittest/agent_runtime_framework/agent_card_mgr_test/agent_card_mgr_test.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2026 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. + */ + +#include +#include +#define private public +#define protected public +#include "agent_card_mgr.h" +#undef private +#undef protected + +using namespace OHOS; +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace AgentRuntime { +class AgentCardMgrTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() override; + void TearDown() override; +}; + +void AgentCardMgrTest::SetUpTestCase(void) +{} + +void AgentCardMgrTest::TearDownTestCase(void) +{} + +void AgentCardMgrTest::SetUp(void) +{} + +void AgentCardMgrTest::TearDown(void) +{} + +/** + * @tc.name: HandleBundleRemoveTest_001 + * @tc.desc: HandleBundleRemoveTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardMgrTest, HandleBundleRemoveTest_001, TestSize.Level1) +{ + AgentCardMgr agentCardMgr; + int ret = agentCardMgr.HandleBundleRemove("", 100); + EXPECT_TRUE(ret == -1); + + ret = agentCardMgr.HandleBundleRemove("test", 100); + EXPECT_TRUE(ret != -1); +} + +/** + * @tc.name: HandleBundleInstallTest_001 + * @tc.desc: HandleBundleInstallTest_001 + * @tc.type: FUNC + * @tc.require: AR000H1N32 + */ +HWTEST_F(AgentCardMgrTest, HandleBundleInstallTest_001, TestSize.Level1) +{ + AgentCardMgr agentCardMgr; + int ret = agentCardMgr.HandleBundleInstall("", 100); + EXPECT_TRUE(ret == -1); + + ret = agentCardMgr.HandleBundleInstall("test", 100); + EXPECT_TRUE(ret == -1); + + ret = agentCardMgr.HandleBundleInstall("test1", 100); + EXPECT_TRUE(ret != -1); +} +} // namespace AgentRuntime +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/include/bundle_mgr_client.h b/test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/include/bundle_mgr_client.h new file mode 100644 index 0000000000..d2dc78ec7b --- /dev/null +++ b/test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/include/bundle_mgr_client.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2026 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 FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_H +#define FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_H + +#include "bundle_info.h" +#include "extension_ability_info.h" + +namespace OHOS { +namespace AppExecFwk { +class BundleMgrClient { +public: + BundleMgrClient(); + virtual ~BundleMgrClient(); + + bool GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, + int32_t userId = Constants::UNSPECIFIED_USERID); + + bool GetResConfigFile(const ExtensionAbilityInfo &extensionInfo, const std::string &metadataName, + std::vector &profileInfos, bool includeSysRes = true) const; +}; +} // namespace AppExecFwk +} // namespace OHOS + +#endif // FOUNDATION_APPEXECFWK_INTERFACES_INNERKITS_APPEXECFWK_CORE_INCLUDE_BUNDLEMGR_BUNDLE_MGR_CLIENT_H \ No newline at end of file diff --git a/test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/src/bundle_mgr_client.cpp b/test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/src/bundle_mgr_client.cpp new file mode 100644 index 0000000000..1d53875635 --- /dev/null +++ b/test/unittest/agent_runtime_framework/agent_card_mgr_test/mock/src/bundle_mgr_client.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2026 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. + */ +#include "bundle_mgr_client.h" + +#include "ability_info.h" +#include "extension_ability_info.h" + +namespace OHOS { +namespace AppExecFwk { +BundleMgrClient::BundleMgrClient() {} + +BundleMgrClient::~BundleMgrClient() {} + +bool BundleMgrClient::GetBundleInfo(const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, + int32_t userId) +{ + if (bundleName == "test") { + return false; + } + return true; +} + +bool BundleMgrClient::GetResConfigFile(const ExtensionAbilityInfo &extensionInfo, const std::string &metadataName, + std::vector &profileInfos, bool includeSysRes) const +{ + return true; +} +} // namespace AppExecFwk +} // namespace OHOS