From 7486c3c2b2652e321233eb0095739b6d13cf4a3b Mon Sep 17 00:00:00 2001 From: l30054665 Date: Thu, 5 Jun 2025 16:43:08 +0800 Subject: [PATCH 1/4] ut Signed-off-by: l30054665 --- test/unittest/BUILD.gn | 1 + test/unittest/UTTest_oh_device_manager.cpp | 358 +++++++++++++++++++++ test/unittest/UTTest_oh_device_manager.h | 42 +++ 3 files changed, 401 insertions(+) create mode 100644 test/unittest/UTTest_oh_device_manager.cpp create mode 100644 test/unittest/UTTest_oh_device_manager.h diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index f0af8d78f..1bee300ac 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -89,6 +89,7 @@ group("unittest") { ":UTTest_mine_softbus_listener", ":UTTest_mini_tools_kit", ":UTTest_multiple_user_connector", + ":UTTest_permission_manager", ":UTTest_pin_auth", ":UTTest_pin_auth_ui", diff --git a/test/unittest/UTTest_oh_device_manager.cpp b/test/unittest/UTTest_oh_device_manager.cpp new file mode 100644 index 000000000..0c1d7c4d3 --- /dev/null +++ b/test/unittest/UTTest_oh_device_manager.cpp @@ -0,0 +1,358 @@ +/* + * Copyright (c) 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 + * + * 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 "UTTest_app_manager.h" +#include "bundle_mgr_mock.h" +#include "dm_constants.h" +#include "dm_system_ability_manager_mock.h" + +using namespace OHOS::AppExecFwk; +using namespace testing; + +namespace OHOS { +namespace DistributedHardware { +namespace { +constexpr size_t ARG_THIRD = 2; +constexpr size_t INVOKE_COUNT = 1; +constexpr size_t ERR_FAILED_VALUE = 11600101; +constexpr uint32_t VALUABLE_TOKEN_ID = 153; +constexpr uint32_t UNVALUABLE_TOKEN_ID = 0; +} // namespace +void AppManagerTest::SetUp() +{ + auto skeleton = IPCSkeletonInterface::GetOrCreateIPCSkeleton(); + skeleton_ = std::static_pointer_cast(skeleton); + auto token = AccessTokenKitInterface::GetOrCreateAccessTokenKit(); + token_ = std::static_pointer_cast(token); + auto client = ISystemAbilityManagerClient::GetOrCreateSAMgrClient(); + client_ = std::static_pointer_cast(client); + auto accountManager = IOsAccountManager::GetOrCreateOsAccountManager(); + accountManager_ = std::static_pointer_cast(accountManager); +} + +void AppManagerTest::TearDown() +{ + IPCSkeletonInterface::ReleaseIPCSkeleton(); + AccessTokenKitInterface::ReleaseAccessTokenKit(); + ISystemAbilityManagerClient::ReleaseSAMgrClient(); + IOsAccountManager::ReleaseAccountManager(); + skeleton_ = nullptr; + token_ = nullptr; + client_ = nullptr; + accountManager_ = nullptr; +} + +void AppManagerTest::SetUpTestCase() +{} + +void AppManagerTest::TearDownTestCase() +{} + +HWTEST_F(AppManagerTest, RegisterCallerAppId_success_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + ASSERT_TRUE(client_ != nullptr); + ASSERT_TRUE(accountManager_ != nullptr); + + EXPECT_CALL(*skeleton_, GetCallingTokenID()) + .Times(INVOKE_COUNT) + .WillOnce(Return(VALUABLE_TOKEN_ID)); + EXPECT_CALL(*skeleton_, GetCallingUid()) + .Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)) + .Times(INVOKE_COUNT) + .WillOnce(Return(ATokenTypeEnum::TOKEN_HAP)); + auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); + auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); + EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)) + .Times(INVOKE_COUNT) + .WillOnce(Return(bundleMgr)); + EXPECT_CALL(*client_, GetSystemAbilityManager()) + .Times(INVOKE_COUNT) + .WillOnce(Return(systemAbilityManager)); + EXPECT_CALL(*accountManager_, GetForegroundOsAccountLocalId(_)) + .Times(INVOKE_COUNT) + .WillOnce(Return(ERR_OK)); + EXPECT_CALL(*bundleMgr, GetNameForUid(_, _)) + .Times(INVOKE_COUNT) + .WillOnce(Return(ERR_OK)); + BundleInfo bundleInfo; + bundleInfo.appId = "mock_appId"; + EXPECT_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _)) + .Times(INVOKE_COUNT) + .WillOnce(DoAll(SetArgReferee(bundleInfo), Return(ERR_OK))); + std::string packageName = "packageName"; + AppManager::GetInstance().RegisterCallerAppId(packageName); + + std::string appId; + auto result = AppManager::GetInstance().GetAppIdByPkgName(packageName, appId); + EXPECT_EQ(result, DM_OK); + EXPECT_STREQ(bundleInfo.appId.c_str(), appId.c_str()); + AppManager::GetInstance().UnRegisterCallerAppId(packageName); +} + +HWTEST_F(AppManagerTest, RegisterCallerAppId_failed_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + + std::string emptyPackageName; + std::string appId; + AppManager::GetInstance().RegisterCallerAppId(emptyPackageName); + AppManager::GetInstance().GetAppIdByPkgName(emptyPackageName, appId); + AppManager::GetInstance().UnRegisterCallerAppId(emptyPackageName); + + EXPECT_CALL(*skeleton_, GetCallingTokenID()) + .Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)) + .Times(INVOKE_COUNT) + .WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE)); + std::string packageName = "packageName"; + AppManager::GetInstance().RegisterCallerAppId(packageName); + auto result = AppManager::GetInstance().GetAppIdByPkgName(packageName, appId); + EXPECT_EQ(result, ERR_DM_FAILED); + AppManager::GetInstance().UnRegisterCallerAppId(packageName); +} + +HWTEST_F(AppManagerTest, GetAppId_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + ASSERT_TRUE(client_ != nullptr); + ASSERT_TRUE(accountManager_ != nullptr); + + EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*skeleton_, GetCallingUid()).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); + + auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); + auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); + size_t getSAMgrcallCount = 0; + size_t getSACallCount = 0; + size_t getForegroundOsAccountLocalIdCallCount = 0; + size_t getBInfoCallCount = 0; + ON_CALL(*client_, GetSystemAbilityManager()) + .WillByDefault(Invoke([&getSAMgrcallCount, systemAbilityManager]() -> sptr { + return (getSAMgrcallCount++ == 0) ? nullptr : systemAbilityManager; + })); + ON_CALL(*systemAbilityManager, GetSystemAbility(_)) + .WillByDefault(Invoke([&getSACallCount, bundleMgr](int32_t) -> sptr { + return (getSACallCount++ == 0) ? nullptr : bundleMgr; + })); + ON_CALL(*accountManager_, GetForegroundOsAccountLocalId(_)) + .WillByDefault(Invoke([&getForegroundOsAccountLocalIdCallCount](int32_t &) -> ErrCode { + return (getForegroundOsAccountLocalIdCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; + })); + ON_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _)) + .WillByDefault(Invoke([&getBInfoCallCount](const std::string &, int32_t, BundleInfo &, int32_t) -> ErrCode { + return (getBInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; + })); + EXPECT_CALL(*client_, GetSystemAbilityManager()).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*accountManager_, GetForegroundOsAccountLocalId(_)).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*bundleMgr, GetNameForUid(_, _)).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _)).Times(AtLeast(INVOKE_COUNT)); + for (size_t i = 0; i < 5; ++i) { + auto appId = AppManager::GetInstance().GetAppId(); + EXPECT_TRUE(appId.empty()); + } +} + +HWTEST_F(AppManagerTest, IsSystemSA_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + size_t getCallingTokenIDCallCount = 0; + size_t getTokenTypeFlagCallCount = 0; + + ON_CALL(*skeleton_, GetCallingTokenID()) + .WillByDefault(Invoke([&getCallingTokenIDCallCount]() { + return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID; + })); + ON_CALL(*token_, GetTokenTypeFlag(_)) + .WillByDefault(Invoke([&getTokenTypeFlagCallCount](AccessTokenID) -> ATokenTypeEnum { + return (getTokenTypeFlagCallCount++ == 0) ? ATokenTypeEnum::TOKEN_HAP : ATokenTypeEnum::TOKEN_NATIVE; + })); + EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).Times(AtLeast(INVOKE_COUNT)); + for (size_t i = 0; i < 3; ++i) { + auto ret = AppManager::GetInstance().IsSystemSA(); + if (getCallingTokenIDCallCount > 1 && getTokenTypeFlagCallCount > 1) { + EXPECT_TRUE(ret); + } else { + EXPECT_FALSE(ret); + } + } +} + +HWTEST_F(AppManagerTest, GetCallerName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + size_t getCallingTokenIDCallCount = 0; + size_t getHapTokenInfoCallCount = 0; + + ON_CALL(*skeleton_, GetCallingTokenID()) + .WillByDefault(Invoke([&getCallingTokenIDCallCount]() { + return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID; + })); + ON_CALL(*token_, GetHapTokenInfo(_, _)) + .WillByDefault(Invoke([&getHapTokenInfoCallCount](AccessTokenID, HapTokenInfo &) { + return (getHapTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; + })); + EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); + EXPECT_CALL(*token_, GetHapTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT)); + for (size_t i = 0; i < 3; ++i) { + bool isSystemSA = false; + std::string output; + auto ret = AppManager::GetInstance().GetCallerName(isSystemSA, output); + if (getCallingTokenIDCallCount > 1 && getHapTokenInfoCallCount > 1) { + EXPECT_EQ(ret, DM_OK); + } else { + EXPECT_EQ(ret, ERR_DM_FAILED); + } + } +} + +HWTEST_F(AppManagerTest, GetCallerName_002, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + + EXPECT_CALL(*skeleton_, GetCallingTokenID()).WillRepeatedly(Return(VALUABLE_TOKEN_ID)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE)); + EXPECT_CALL(*token_, GetNativeTokenInfo(_, _)).WillOnce(Return(ERR_FAILED_VALUE)); + bool isSystemSA = true; + std::string output; + auto ret = AppManager::GetInstance().GetCallerName(isSystemSA, output); + EXPECT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_INVALID)); + ret = AppManager::GetInstance().GetCallerName(isSystemSA, output); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(AppManagerTest, GetNativeTokenIdByName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(token_ != nullptr); + + EXPECT_CALL(*token_, GetNativeTokenId(_)).WillOnce(Return(UNVALUABLE_TOKEN_ID)); + std::string processName; + int64_t tokenId = 0; + auto ret = AppManager::GetInstance().GetNativeTokenIdByName(processName, tokenId); + EXPECT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*token_, GetNativeTokenId(_)).WillOnce(Return(VALUABLE_TOKEN_ID)); + ret = AppManager::GetInstance().GetNativeTokenIdByName(processName, tokenId); + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(tokenId, VALUABLE_TOKEN_ID); +} + +HWTEST_F(AppManagerTest, GetHapTokenIdByName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(token_ != nullptr); + EXPECT_CALL(*token_, GetHapTokenID(_, _, _)).WillOnce(Return(UNVALUABLE_TOKEN_ID)); + int32_t userId = 0; + std::string bundleName; + int32_t instIndex = 0; + int64_t tokenId = 0; + auto ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, instIndex, tokenId); + EXPECT_EQ(ret, ERR_DM_FAILED); + + EXPECT_CALL(*token_, GetHapTokenID(_, _, _)).WillOnce(Return(VALUABLE_TOKEN_ID)); + ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, instIndex, tokenId); + EXPECT_EQ(ret, DM_OK); + EXPECT_EQ(tokenId, VALUABLE_TOKEN_ID); +} + +HWTEST_F(AppManagerTest, GetCallerProcessName_001, testing::ext::TestSize.Level1) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + size_t getCallingTokenIDCallCount = 0; + size_t getNativeTokenInfoCallCount = 0; + + ON_CALL(*skeleton_, GetCallingTokenID()) + .WillByDefault(Invoke([&getCallingTokenIDCallCount]() { + return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID; + })); + ON_CALL(*token_, GetNativeTokenInfo(_, _)) + .WillByDefault(Invoke([&getNativeTokenInfoCallCount](AccessTokenID, NativeTokenInfo &) { + return (getNativeTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; + })); + EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_NATIVE)); + EXPECT_CALL(*token_, GetNativeTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT)); + for (size_t i = 0; i < 3; ++i) { + std::string output; + auto ret = AppManager::GetInstance().GetCallerProcessName(output); + if (getCallingTokenIDCallCount > 1 && getNativeTokenInfoCallCount > 1) { + EXPECT_EQ(ret, DM_OK); + } else { + EXPECT_EQ(ret, ERR_DM_FAILED); + } + } +} + +HWTEST_F(AppManagerTest, GetCallerProcessName_002, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(skeleton_ != nullptr); + ASSERT_TRUE(token_ != nullptr); + size_t GetHapTokenInfoCallCount = 0; + + ON_CALL(*token_, GetHapTokenInfo(_, _)) + .WillByDefault(Invoke([&GetHapTokenInfoCallCount](AccessTokenID, HapTokenInfo &) { + return (GetHapTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; + })); + + EXPECT_CALL(*skeleton_, GetCallingTokenID()).WillRepeatedly(Return(VALUABLE_TOKEN_ID)); + EXPECT_CALL(*skeleton_, GetCallingFullTokenID()).Times(AtLeast(INVOKE_COUNT)); + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); + EXPECT_CALL(*token_, GetHapTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT)); + std::string output; + for (size_t i = 0; i < 2; ++i) { + auto ret = AppManager::GetInstance().GetCallerProcessName(output); + EXPECT_EQ(ret, ERR_DM_FAILED); + } + + EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_INVALID)); + auto ret = AppManager::GetInstance().GetCallerProcessName(output); + EXPECT_EQ(ret, ERR_DM_FAILED); +} + +HWTEST_F(AppManagerTest, GetBundleNameForSelf_001, testing::ext::TestSize.Level2) +{ + ASSERT_TRUE(client_ != nullptr); + auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); + auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); + EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)) + .Times(INVOKE_COUNT) + .WillOnce(Return(bundleMgr)); + EXPECT_CALL(*client_, GetSystemAbilityManager()) + .Times(INVOKE_COUNT) + .WillOnce(Return(systemAbilityManager)); + EXPECT_CALL(*bundleMgr, GetBundleInfoForSelf(_, _)) + .Times(INVOKE_COUNT) + .WillOnce(Return(ERR_OK)); + + std::string bundleName = ""; + auto result = AppManager::GetInstance().GetBundleNameForSelf(bundleName); + EXPECT_EQ(result, DM_OK); + EXPECT_EQ(bundleName, ""); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_oh_device_manager.h b/test/unittest/UTTest_oh_device_manager.h new file mode 100644 index 000000000..15a31e706 --- /dev/null +++ b/test/unittest/UTTest_oh_device_manager.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 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 + * + * 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_DM_APP_MANAGER_TEST_H +#define OHOS_DM_APP_MANAGER_TEST_H + +#include +#include "app_manager.h" +#include "ipc_skeleton_mock.h" +#include "accesstoken_kit_mock.h" +#include "system_ability_manager_client_mock.h" +#include "os_account_manager_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class AppManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +private: + std::shared_ptr skeleton_; + std::shared_ptr token_; + std::shared_ptr client_; + std::shared_ptr accountManager_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif -- Gitee From a3b714e3dcc2c775f62caef83eebdd8245c8a9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B0=9A?= Date: Thu, 5 Jun 2025 08:47:59 +0000 Subject: [PATCH 2/4] update test/unittest/BUILD.gn. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李尚 --- test/unittest/BUILD.gn | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 1bee300ac..8e572f9a3 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -89,7 +89,7 @@ group("unittest") { ":UTTest_mine_softbus_listener", ":UTTest_mini_tools_kit", ":UTTest_multiple_user_connector", - + ":UTTest_oh_device_manager", ":UTTest_permission_manager", ":UTTest_pin_auth", ":UTTest_pin_auth_ui", @@ -1110,6 +1110,43 @@ ohos_unittest("UTTest_permission_manager") { ## UnitTest UTTest_permission_manager }}} +## UnitTest UTTest_oh_device_manager {{{ +ohos_unittest("UTTest_oh_device_manager") { + module_out_path = module_out_path + + include_dirs = [ + "${devicemanager_path}/interfaces/kits/ndk/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${innerkits_path}/native_cpp/include", + ] + sources = [ + "${devicemanager_path}/interfaces/kits/ndk/src/dm_client.cpp", + "${devicemanager_path}/interfaces/kits/ndk/src/oh_device_manager.cpp", + "${devicemanager_path}/test/unittest/UTTest_oh_device_manager.cpp", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"devicemanager_ndk\"", + "LOG_DOMAIN=0xD004110", + ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + ] + + external_deps = [ + "c_utils:utils", + "eventhandler:libeventhandler", + "hilog:libhilog", + "ipc:ipc_core", + ] +} + +## UnitTest UTTest_oh_device_manager }}} + ## UnitTest UTTest_device_manager_notify {{{ ohos_unittest("UTTest_device_manager_notify") { module_out_path = module_out_path -- Gitee From c36415f05612331c3e86215b3fc1dd7c87eba7f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B0=9A?= Date: Thu, 5 Jun 2025 08:49:03 +0000 Subject: [PATCH 3/4] update test/unittest/UTTest_oh_device_manager.cpp. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李尚 --- test/unittest/UTTest_oh_device_manager.cpp | 351 ++------------------- 1 file changed, 20 insertions(+), 331 deletions(-) diff --git a/test/unittest/UTTest_oh_device_manager.cpp b/test/unittest/UTTest_oh_device_manager.cpp index 0c1d7c4d3..03012d452 100644 --- a/test/unittest/UTTest_oh_device_manager.cpp +++ b/test/unittest/UTTest_oh_device_manager.cpp @@ -4,7 +4,7 @@ * 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 + * 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, @@ -12,347 +12,36 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "UTTest_oh_device_manager.h" -#include "UTTest_app_manager.h" -#include "bundle_mgr_mock.h" -#include "dm_constants.h" -#include "dm_system_ability_manager_mock.h" +#include -using namespace OHOS::AppExecFwk; -using namespace testing; +#include "dm_client.h" +#include "dm_log.h" +#include "oh_device_manager_err_code.h" +using namespace testing; +using namespace testing::ext; namespace OHOS { namespace DistributedHardware { -namespace { -constexpr size_t ARG_THIRD = 2; -constexpr size_t INVOKE_COUNT = 1; -constexpr size_t ERR_FAILED_VALUE = 11600101; -constexpr uint32_t VALUABLE_TOKEN_ID = 153; -constexpr uint32_t UNVALUABLE_TOKEN_ID = 0; -} // namespace -void AppManagerTest::SetUp() -{ - auto skeleton = IPCSkeletonInterface::GetOrCreateIPCSkeleton(); - skeleton_ = std::static_pointer_cast(skeleton); - auto token = AccessTokenKitInterface::GetOrCreateAccessTokenKit(); - token_ = std::static_pointer_cast(token); - auto client = ISystemAbilityManagerClient::GetOrCreateSAMgrClient(); - client_ = std::static_pointer_cast(client); - auto accountManager = IOsAccountManager::GetOrCreateOsAccountManager(); - accountManager_ = std::static_pointer_cast(accountManager); -} +void OhDeviceManagerTest::SetUp() {} -void AppManagerTest::TearDown() -{ - IPCSkeletonInterface::ReleaseIPCSkeleton(); - AccessTokenKitInterface::ReleaseAccessTokenKit(); - ISystemAbilityManagerClient::ReleaseSAMgrClient(); - IOsAccountManager::ReleaseAccountManager(); - skeleton_ = nullptr; - token_ = nullptr; - client_ = nullptr; - accountManager_ = nullptr; -} +void OhDeviceManagerTest::TearDown() {} -void AppManagerTest::SetUpTestCase() -{} +void OhDeviceManagerTest::SetUpTestCase() {} -void AppManagerTest::TearDownTestCase() -{} - -HWTEST_F(AppManagerTest, RegisterCallerAppId_success_001, testing::ext::TestSize.Level1) -{ - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - ASSERT_TRUE(client_ != nullptr); - ASSERT_TRUE(accountManager_ != nullptr); - - EXPECT_CALL(*skeleton_, GetCallingTokenID()) - .Times(INVOKE_COUNT) - .WillOnce(Return(VALUABLE_TOKEN_ID)); - EXPECT_CALL(*skeleton_, GetCallingUid()) - .Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)) - .Times(INVOKE_COUNT) - .WillOnce(Return(ATokenTypeEnum::TOKEN_HAP)); - auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); - auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); - EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)) - .Times(INVOKE_COUNT) - .WillOnce(Return(bundleMgr)); - EXPECT_CALL(*client_, GetSystemAbilityManager()) - .Times(INVOKE_COUNT) - .WillOnce(Return(systemAbilityManager)); - EXPECT_CALL(*accountManager_, GetForegroundOsAccountLocalId(_)) - .Times(INVOKE_COUNT) - .WillOnce(Return(ERR_OK)); - EXPECT_CALL(*bundleMgr, GetNameForUid(_, _)) - .Times(INVOKE_COUNT) - .WillOnce(Return(ERR_OK)); - BundleInfo bundleInfo; - bundleInfo.appId = "mock_appId"; - EXPECT_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _)) - .Times(INVOKE_COUNT) - .WillOnce(DoAll(SetArgReferee(bundleInfo), Return(ERR_OK))); - std::string packageName = "packageName"; - AppManager::GetInstance().RegisterCallerAppId(packageName); - - std::string appId; - auto result = AppManager::GetInstance().GetAppIdByPkgName(packageName, appId); - EXPECT_EQ(result, DM_OK); - EXPECT_STREQ(bundleInfo.appId.c_str(), appId.c_str()); - AppManager::GetInstance().UnRegisterCallerAppId(packageName); -} - -HWTEST_F(AppManagerTest, RegisterCallerAppId_failed_001, testing::ext::TestSize.Level2) -{ - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - - std::string emptyPackageName; - std::string appId; - AppManager::GetInstance().RegisterCallerAppId(emptyPackageName); - AppManager::GetInstance().GetAppIdByPkgName(emptyPackageName, appId); - AppManager::GetInstance().UnRegisterCallerAppId(emptyPackageName); - - EXPECT_CALL(*skeleton_, GetCallingTokenID()) - .Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)) - .Times(INVOKE_COUNT) - .WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE)); - std::string packageName = "packageName"; - AppManager::GetInstance().RegisterCallerAppId(packageName); - auto result = AppManager::GetInstance().GetAppIdByPkgName(packageName, appId); - EXPECT_EQ(result, ERR_DM_FAILED); - AppManager::GetInstance().UnRegisterCallerAppId(packageName); -} - -HWTEST_F(AppManagerTest, GetAppId_001, testing::ext::TestSize.Level2) -{ - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - ASSERT_TRUE(client_ != nullptr); - ASSERT_TRUE(accountManager_ != nullptr); - - EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*skeleton_, GetCallingUid()).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); - - auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); - auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); - size_t getSAMgrcallCount = 0; - size_t getSACallCount = 0; - size_t getForegroundOsAccountLocalIdCallCount = 0; - size_t getBInfoCallCount = 0; - ON_CALL(*client_, GetSystemAbilityManager()) - .WillByDefault(Invoke([&getSAMgrcallCount, systemAbilityManager]() -> sptr { - return (getSAMgrcallCount++ == 0) ? nullptr : systemAbilityManager; - })); - ON_CALL(*systemAbilityManager, GetSystemAbility(_)) - .WillByDefault(Invoke([&getSACallCount, bundleMgr](int32_t) -> sptr { - return (getSACallCount++ == 0) ? nullptr : bundleMgr; - })); - ON_CALL(*accountManager_, GetForegroundOsAccountLocalId(_)) - .WillByDefault(Invoke([&getForegroundOsAccountLocalIdCallCount](int32_t &) -> ErrCode { - return (getForegroundOsAccountLocalIdCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; - })); - ON_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _)) - .WillByDefault(Invoke([&getBInfoCallCount](const std::string &, int32_t, BundleInfo &, int32_t) -> ErrCode { - return (getBInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; - })); - EXPECT_CALL(*client_, GetSystemAbilityManager()).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*accountManager_, GetForegroundOsAccountLocalId(_)).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*bundleMgr, GetNameForUid(_, _)).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*bundleMgr, GetBundleInfoV9(_, _, _, _)).Times(AtLeast(INVOKE_COUNT)); - for (size_t i = 0; i < 5; ++i) { - auto appId = AppManager::GetInstance().GetAppId(); - EXPECT_TRUE(appId.empty()); - } -} - -HWTEST_F(AppManagerTest, IsSystemSA_001, testing::ext::TestSize.Level1) -{ - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - size_t getCallingTokenIDCallCount = 0; - size_t getTokenTypeFlagCallCount = 0; - - ON_CALL(*skeleton_, GetCallingTokenID()) - .WillByDefault(Invoke([&getCallingTokenIDCallCount]() { - return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID; - })); - ON_CALL(*token_, GetTokenTypeFlag(_)) - .WillByDefault(Invoke([&getTokenTypeFlagCallCount](AccessTokenID) -> ATokenTypeEnum { - return (getTokenTypeFlagCallCount++ == 0) ? ATokenTypeEnum::TOKEN_HAP : ATokenTypeEnum::TOKEN_NATIVE; - })); - EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).Times(AtLeast(INVOKE_COUNT)); - for (size_t i = 0; i < 3; ++i) { - auto ret = AppManager::GetInstance().IsSystemSA(); - if (getCallingTokenIDCallCount > 1 && getTokenTypeFlagCallCount > 1) { - EXPECT_TRUE(ret); - } else { - EXPECT_FALSE(ret); - } - } -} - -HWTEST_F(AppManagerTest, GetCallerName_001, testing::ext::TestSize.Level1) -{ - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - size_t getCallingTokenIDCallCount = 0; - size_t getHapTokenInfoCallCount = 0; - - ON_CALL(*skeleton_, GetCallingTokenID()) - .WillByDefault(Invoke([&getCallingTokenIDCallCount]() { - return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID; - })); - ON_CALL(*token_, GetHapTokenInfo(_, _)) - .WillByDefault(Invoke([&getHapTokenInfoCallCount](AccessTokenID, HapTokenInfo &) { - return (getHapTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; - })); - EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); - EXPECT_CALL(*token_, GetHapTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT)); - for (size_t i = 0; i < 3; ++i) { - bool isSystemSA = false; - std::string output; - auto ret = AppManager::GetInstance().GetCallerName(isSystemSA, output); - if (getCallingTokenIDCallCount > 1 && getHapTokenInfoCallCount > 1) { - EXPECT_EQ(ret, DM_OK); - } else { - EXPECT_EQ(ret, ERR_DM_FAILED); - } - } -} - -HWTEST_F(AppManagerTest, GetCallerName_002, testing::ext::TestSize.Level2) -{ - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - - EXPECT_CALL(*skeleton_, GetCallingTokenID()).WillRepeatedly(Return(VALUABLE_TOKEN_ID)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_NATIVE)); - EXPECT_CALL(*token_, GetNativeTokenInfo(_, _)).WillOnce(Return(ERR_FAILED_VALUE)); - bool isSystemSA = true; - std::string output; - auto ret = AppManager::GetInstance().GetCallerName(isSystemSA, output); - EXPECT_EQ(ret, ERR_DM_FAILED); - - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_INVALID)); - ret = AppManager::GetInstance().GetCallerName(isSystemSA, output); - EXPECT_EQ(ret, ERR_DM_FAILED); -} - -HWTEST_F(AppManagerTest, GetNativeTokenIdByName_001, testing::ext::TestSize.Level1) -{ - ASSERT_TRUE(token_ != nullptr); - - EXPECT_CALL(*token_, GetNativeTokenId(_)).WillOnce(Return(UNVALUABLE_TOKEN_ID)); - std::string processName; - int64_t tokenId = 0; - auto ret = AppManager::GetInstance().GetNativeTokenIdByName(processName, tokenId); - EXPECT_EQ(ret, ERR_DM_FAILED); - - EXPECT_CALL(*token_, GetNativeTokenId(_)).WillOnce(Return(VALUABLE_TOKEN_ID)); - ret = AppManager::GetInstance().GetNativeTokenIdByName(processName, tokenId); - EXPECT_EQ(ret, DM_OK); - EXPECT_EQ(tokenId, VALUABLE_TOKEN_ID); -} - -HWTEST_F(AppManagerTest, GetHapTokenIdByName_001, testing::ext::TestSize.Level1) -{ - ASSERT_TRUE(token_ != nullptr); - EXPECT_CALL(*token_, GetHapTokenID(_, _, _)).WillOnce(Return(UNVALUABLE_TOKEN_ID)); - int32_t userId = 0; - std::string bundleName; - int32_t instIndex = 0; - int64_t tokenId = 0; - auto ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, instIndex, tokenId); - EXPECT_EQ(ret, ERR_DM_FAILED); - - EXPECT_CALL(*token_, GetHapTokenID(_, _, _)).WillOnce(Return(VALUABLE_TOKEN_ID)); - ret = AppManager::GetInstance().GetHapTokenIdByName(userId, bundleName, instIndex, tokenId); - EXPECT_EQ(ret, DM_OK); - EXPECT_EQ(tokenId, VALUABLE_TOKEN_ID); -} - -HWTEST_F(AppManagerTest, GetCallerProcessName_001, testing::ext::TestSize.Level1) -{ - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - size_t getCallingTokenIDCallCount = 0; - size_t getNativeTokenInfoCallCount = 0; - - ON_CALL(*skeleton_, GetCallingTokenID()) - .WillByDefault(Invoke([&getCallingTokenIDCallCount]() { - return (getCallingTokenIDCallCount++ == 0) ? UNVALUABLE_TOKEN_ID : VALUABLE_TOKEN_ID; - })); - ON_CALL(*token_, GetNativeTokenInfo(_, _)) - .WillByDefault(Invoke([&getNativeTokenInfoCallCount](AccessTokenID, NativeTokenInfo &) { - return (getNativeTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; - })); - EXPECT_CALL(*skeleton_, GetCallingTokenID()).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_NATIVE)); - EXPECT_CALL(*token_, GetNativeTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT)); - for (size_t i = 0; i < 3; ++i) { - std::string output; - auto ret = AppManager::GetInstance().GetCallerProcessName(output); - if (getCallingTokenIDCallCount > 1 && getNativeTokenInfoCallCount > 1) { - EXPECT_EQ(ret, DM_OK); - } else { - EXPECT_EQ(ret, ERR_DM_FAILED); - } - } -} - -HWTEST_F(AppManagerTest, GetCallerProcessName_002, testing::ext::TestSize.Level2) +void OhDeviceManagerTest::TearDownTestCase() {} +namespace { +HWTEST_F(OhDeviceManagerTest, OH_DeviceManager_GetLocalDeviceName_001, testing::ext::TestSize.Level0) { - ASSERT_TRUE(skeleton_ != nullptr); - ASSERT_TRUE(token_ != nullptr); - size_t GetHapTokenInfoCallCount = 0; - - ON_CALL(*token_, GetHapTokenInfo(_, _)) - .WillByDefault(Invoke([&GetHapTokenInfoCallCount](AccessTokenID, HapTokenInfo &) { - return (GetHapTokenInfoCallCount++ == 0) ? ERR_FAILED_VALUE : ERR_OK; - })); + char *localDeviceName = nullptr; + unsigned int len = 0; + int32_t result = OH_DeviceManager_GetLocalDeviceName(&localDeviceName, len); - EXPECT_CALL(*skeleton_, GetCallingTokenID()).WillRepeatedly(Return(VALUABLE_TOKEN_ID)); - EXPECT_CALL(*skeleton_, GetCallingFullTokenID()).Times(AtLeast(INVOKE_COUNT)); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillRepeatedly(Return(ATokenTypeEnum::TOKEN_HAP)); - EXPECT_CALL(*token_, GetHapTokenInfo(_, _)).Times(AtLeast(INVOKE_COUNT)); - std::string output; - for (size_t i = 0; i < 2; ++i) { - auto ret = AppManager::GetInstance().GetCallerProcessName(output); - EXPECT_EQ(ret, ERR_DM_FAILED); - } + EXPECT_EQ(result, DM_ERR_OBTAIN_BUNDLE_NAME); - EXPECT_CALL(*token_, GetTokenTypeFlag(_)).WillOnce(Return(ATokenTypeEnum::TOKEN_INVALID)); - auto ret = AppManager::GetInstance().GetCallerProcessName(output); - EXPECT_EQ(ret, ERR_DM_FAILED); -} - -HWTEST_F(AppManagerTest, GetBundleNameForSelf_001, testing::ext::TestSize.Level2) -{ - ASSERT_TRUE(client_ != nullptr); - auto bundleMgr = sptr(new (std::nothrow) BundleMgrMock()); - auto systemAbilityManager = sptr(new (std::nothrow) SystemAbilityManagerMock()); - EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)) - .Times(INVOKE_COUNT) - .WillOnce(Return(bundleMgr)); - EXPECT_CALL(*client_, GetSystemAbilityManager()) - .Times(INVOKE_COUNT) - .WillOnce(Return(systemAbilityManager)); - EXPECT_CALL(*bundleMgr, GetBundleInfoForSelf(_, _)) - .Times(INVOKE_COUNT) - .WillOnce(Return(ERR_OK)); - - std::string bundleName = ""; - auto result = AppManager::GetInstance().GetBundleNameForSelf(bundleName); - EXPECT_EQ(result, DM_OK); - EXPECT_EQ(bundleName, ""); + delete[] localDeviceName; } +} // namespace } // namespace DistributedHardware } // namespace OHOS -- Gitee From 46d62f46847f7efaf25bc214684695c65dfb0228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=B0=9A?= Date: Thu, 5 Jun 2025 08:49:32 +0000 Subject: [PATCH 4/4] update test/unittest/UTTest_oh_device_manager.h. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 李尚 --- test/unittest/UTTest_oh_device_manager.h | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/test/unittest/UTTest_oh_device_manager.h b/test/unittest/UTTest_oh_device_manager.h index 15a31e706..a4456279e 100644 --- a/test/unittest/UTTest_oh_device_manager.h +++ b/test/unittest/UTTest_oh_device_manager.h @@ -4,7 +4,7 @@ * 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 + * 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, @@ -13,30 +13,22 @@ * limitations under the License. */ -#ifndef OHOS_DM_APP_MANAGER_TEST_H -#define OHOS_DM_APP_MANAGER_TEST_H +#ifndef OHOS_OH_DEVICE_MANAGER_TEST_H +#define OHOS_OH_DEVICE_MANAGER_TEST_H #include -#include "app_manager.h" -#include "ipc_skeleton_mock.h" -#include "accesstoken_kit_mock.h" -#include "system_ability_manager_client_mock.h" -#include "os_account_manager_mock.h" +#include "oh_device_manager.h" namespace OHOS { namespace DistributedHardware { -class AppManagerTest : public testing::Test { +class OhDeviceManagerTest : public testing::Test { public: static void SetUpTestCase(); static void TearDownTestCase(); void SetUp(); void TearDown(); -private: - std::shared_ptr skeleton_; - std::shared_ptr token_; - std::shared_ptr client_; - std::shared_ptr accountManager_; }; } // namespace DistributedHardware } // namespace OHOS -#endif + +#endif \ No newline at end of file -- Gitee