diff --git a/services/service/include/devicenamemgr/device_name_manager.h b/services/service/include/devicenamemgr/device_name_manager.h index ed68ca896c0116ba810c879ec5e77f2ceb7aed27..20aa157bcd851f7e44fc9471ff9439045b59f375 100644 --- a/services/service/include/devicenamemgr/device_name_manager.h +++ b/services/service/include/devicenamemgr/device_name_manager.h @@ -45,6 +45,7 @@ public: int32_t RestoreLocalDeviceName(); int32_t InitDeviceNameWhenLanguageOrRegionChanged(); std::string GetUserDefinedDeviceName(); + std::string GetLocalMarketName(); private: DeviceNameManager() = default; @@ -60,7 +61,6 @@ private: std::string GetSystemLanguage(); std::string GetSystemRegion(); - std::string GetLocalMarketName(); int32_t InitDisplayDeviceNameToSettingsData(const std::string &nickName, const std::string &deviceName, int32_t userId); diff --git a/services/service/include/permission/lite/permission_manager.h b/services/service/include/permission/lite/permission_manager.h index 0421f096a1ce9a2171760921b735db22bd6f0558..ff1e02302f2dd65e04663c37c4c05b55ee0500bf 100644 --- a/services/service/include/permission/lite/permission_manager.h +++ b/services/service/include/permission/lite/permission_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -41,6 +41,7 @@ public: bool CheckProcessNameValidModifyRemoteDeviceName(const std::string &processName); bool CheckProcessNameValidPutDeviceProfileInfoList(const std::string &processName); bool CheckProcessValidOnGetTrustedDeviceList(); + bool CheckReadLocalDeviceName(void); }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/include/permission/standard/permission_manager.h b/services/service/include/permission/standard/permission_manager.h index d5aab748c28af3a3fdf91c9fcad9076ebaa51c2b..21d057b9a027ee22cf8dcd427555dbc40335c7c3 100644 --- a/services/service/include/permission/standard/permission_manager.h +++ b/services/service/include/permission/standard/permission_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -40,6 +40,7 @@ public: bool CheckProcessNameValidModifyRemoteDeviceName(const std::string &processName); bool CheckProcessNameValidPutDeviceProfileInfoList(const std::string &processName); bool CheckProcessValidOnGetTrustedDeviceList(); + bool CheckReadLocalDeviceName(void); private: bool VerifyAccessTokenByPermissionName(const std::string& permissionName); diff --git a/services/service/src/device_manager_service.cpp b/services/service/src/device_manager_service.cpp index bf85b5fca313bd8ddf358eec90b32fb44a33ce4a..12022bb2026f2607669ddc012efc2dee3bdc0244 100644 --- a/services/service/src/device_manager_service.cpp +++ b/services/service/src/device_manager_service.cpp @@ -4473,7 +4473,12 @@ int32_t DeviceManagerService::UnRegisterPinHolderCallback(const std::string &pkg int32_t DeviceManagerService::GetLocalDeviceName(std::string &deviceName) { #if !(defined(__LITEOS_M__) || defined(LITE_DEVICE)) - return DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(0, deviceName); + if (PermissionManager::GetInstance().CheckReadLocalDeviceName()) { + return DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(0, deviceName); + } else { + deviceName = DeviceNameManager::GetInstance().GetLocalMarketName(); + return DM_OK; + } #endif (void) deviceName; return DM_OK; diff --git a/services/service/src/permission/lite/permission_manager.cpp b/services/service/src/permission/lite/permission_manager.cpp index dbfa6d7f3631c3c95e130bac52f4c2592e427342..bf6e407426040ae3f69bca6e2effcfc43f6e1076 100644 --- a/services/service/src/permission/lite/permission_manager.cpp +++ b/services/service/src/permission/lite/permission_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -105,5 +105,10 @@ bool PermissionManager::CheckProcessValidOnGetTrustedDeviceList() { return true; } + +bool PermissionManager::CheckReadLocalDeviceName(void) +{ + return true; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/service/src/permission/standard/permission_manager.cpp b/services/service/src/permission/standard/permission_manager.cpp index fbcd7077505b4f93b95c59b93b06ef7b46a29846..d7036d738c73225d768b1b6a1710840f54ca305d 100644 --- a/services/service/src/permission/standard/permission_manager.cpp +++ b/services/service/src/permission/standard/permission_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -98,6 +98,8 @@ constexpr const static char* GET_TRUSTED_DEVICE_LIST_WHITE_LIST[] = { "distributedsched", }; constexpr uint32_t GET_TRUSTED_DEVICE_LIST_WHITE_LIST_NUM = std::size(GET_TRUSTED_DEVICE_LIST_WHITE_LIST); + +constexpr const char* READ_LOCAL_DEVICE_NAME_PERMISSION = "ohos.permission.READ_LOCAL_DEVICE_NAME"; } bool PermissionManager::CheckPermission(void) @@ -366,5 +368,22 @@ bool PermissionManager::CheckProcessValidOnGetTrustedDeviceList() } return false; } + +bool PermissionManager::CheckReadLocalDeviceName(void) +{ + AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID(); + if (tokenCaller == 0) { + LOGE("CheckReadLocalDeviceName GetCallingTokenID error."); + return false; + } + ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller); + if ((tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) && + (AccessTokenKit::VerifyAccessToken(tokenCaller, READ_LOCAL_DEVICE_NAME_PERMISSION) == + PermissionState::PERMISSION_GRANTED)) { + return true; + } + LOGE("Read local device name permission is denied, please apply for corresponding permissions."); + return false; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/test/commonunittest/UTTest_permission_manager.cpp b/test/commonunittest/UTTest_permission_manager.cpp index 2ece3cf82720bafcbd8c136de865997b84f72d43..84522e02b8aa67faa40b77718f03928c4a174c5e 100644 --- a/test/commonunittest/UTTest_permission_manager.cpp +++ b/test/commonunittest/UTTest_permission_manager.cpp @@ -14,11 +14,10 @@ */ #include "UTTest_permission_manager.h" +#include "access_token.h" #include "accesstoken_kit.h" #include "nativetoken_kit.h" #include "token_setproc.h" -#include "access_token.h" - using namespace testing; using namespace testing::ext; namespace OHOS { @@ -271,6 +270,25 @@ HWTEST_F(PermissionManagerTest, CheckProcessNameValidOnGetDeviceInfo_001, testin ret = PermissionManager::GetInstance().CheckProcessNameValidOnGetDeviceInfo(processName); ASSERT_TRUE(ret); } + +HWTEST_F(PermissionManagerTest, CheckReadLocalDeviceName_001, testing::ext::TestSize.Level1) +{ + EXPECT_CALL(*ipcSkeletonMock_, GetCallingTokenID()).WillOnce(Return(0)); + bool ret = PermissionManager::GetInstance().CheckReadLocalDeviceName(); + ASSERT_FALSE(ret); + + EXPECT_CALL(*ipcSkeletonMock_, GetCallingTokenID()).WillOnce(Return(10)); + EXPECT_CALL(*accessTokenKitMock_, VerifyAccessToken(_, _)) + .WillOnce(Return(Security::AccessToken::PermissionState::PERMISSION_DENIED)); + ret = PermissionManager::GetInstance().CheckPermission(); + ASSERT_FALSE(ret); + + EXPECT_CALL(*ipcSkeletonMock_, GetCallingTokenID()).WillOnce(Return(10)); + EXPECT_CALL(*accessTokenKitMock_, VerifyAccessToken(_, _)) + .WillOnce(Return(Security::AccessToken::PermissionState::PERMISSION_GRANTED)); + ret = PermissionManager::GetInstance().CheckPermission(); + ASSERT_TRUE(ret); +} } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/commonunittest/UTTest_permission_manager.h b/test/commonunittest/UTTest_permission_manager.h index 20b409c342cc84ba9ae87f3d2f629f57a30125a5..459496bd3d40e5731e720ce1f73ce512880358b9 100644 --- a/test/commonunittest/UTTest_permission_manager.h +++ b/test/commonunittest/UTTest_permission_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -21,10 +21,10 @@ #include #include +#include "dm_accesstoken_kit_mock.h" #include "dm_constants.h" -#include "permission_manager.h" #include "dm_ipc_skeleton_mock.h" -#include "dm_accesstoken_kit_mock.h" +#include "permission_manager.h" namespace OHOS { namespace DistributedHardware { diff --git a/test/unittest/UTTest_device_manager_service.h b/test/unittest/UTTest_device_manager_service.h index 4b7b8e87eb914c989461fe8dc1284168ac56ea1b..853df5e233121011c78c291745e75c032c6173bf 100644 --- a/test/unittest/UTTest_device_manager_service.h +++ b/test/unittest/UTTest_device_manager_service.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 diff --git a/test/unittest/UTTest_device_manager_service_two.cpp b/test/unittest/UTTest_device_manager_service_two.cpp index 7c658a6ab6956673a24a6b5d73e4f0c3a28f2962..fa38acd04c02be1a82f4d94e50ad81df5009b263 100644 --- a/test/unittest/UTTest_device_manager_service_two.cpp +++ b/test/unittest/UTTest_device_manager_service_two.cpp @@ -1953,8 +1953,16 @@ HWTEST_F(DeviceManagerServiceTest, HandleShareUnbindBroadCast_003, testing::ext: HWTEST_F(DeviceManagerServiceTest, GetLocalDeviceName_201, testing::ext::TestSize.Level1) { std::string deviceName = ""; + EXPECT_CALL(*permissionManagerMock_, CheckReadLocalDeviceName()).WillOnce(Return(true)); int32_t ret = DeviceManagerService::GetInstance().GetLocalDeviceName(deviceName); EXPECT_EQ(ret, DM_OK); + EXPECT_FALSE(deviceName.empty()); + + deviceName = ""; + EXPECT_CALL(*permissionManagerMock_, CheckReadLocalDeviceName()).WillOnce(Return(false)); + ret = DeviceManagerService::GetInstance().GetLocalDeviceName(deviceName); + EXPECT_EQ(ret, DM_OK); + EXPECT_FALSE(deviceName.empty()); } HWTEST_F(DeviceManagerServiceTest, ValidateUnBindDeviceParams_201, testing::ext::TestSize.Level1) diff --git a/test/unittest/mock/permission_manager_mock.cpp b/test/unittest/mock/permission_manager_mock.cpp index becbab5e69719d172c38fd31877cfed11d31471e..675b86e1b782225d3b3bf819683e75311cd4e2a0 100644 --- a/test/unittest/mock/permission_manager_mock.cpp +++ b/test/unittest/mock/permission_manager_mock.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 @@ -69,5 +69,10 @@ bool PermissionManager::CheckProcessNameValidPutDeviceProfileInfoList(const std: { return DmPermissionManager::dmPermissionManager->CheckProcessNameValidPutDeviceProfileInfoList(processName); } + +bool PermissionManager::CheckReadLocalDeviceName() +{ + return DmPermissionManager::dmPermissionManager->CheckReadLocalDeviceName(); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/permission_manager_mock.h b/test/unittest/mock/permission_manager_mock.h index 29bb5ba5120897bf9fc648b218c69100de6b2d37..7cef7f02bfcf1398cc165b6aec82e38a10afd93f 100644 --- a/test/unittest/mock/permission_manager_mock.h +++ b/test/unittest/mock/permission_manager_mock.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Huawei Device Co., Ltd. + * Copyright (C) 2024-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 @@ -36,6 +36,7 @@ public: virtual bool CheckProcessNameValidModifyLocalDeviceName(const std::string &processName) = 0; virtual bool CheckProcessNameValidModifyRemoteDeviceName(const std::string &processName) = 0; virtual bool CheckProcessNameValidPutDeviceProfileInfoList(const std::string &processName) = 0; + virtual bool CheckReadLocalDeviceName() = 0; public: static inline std::shared_ptr dmPermissionManager = nullptr; }; @@ -52,6 +53,7 @@ public: MOCK_METHOD(bool, CheckProcessNameValidModifyLocalDeviceName, (const std::string &)); MOCK_METHOD(bool, CheckProcessNameValidModifyRemoteDeviceName, (const std::string &)); MOCK_METHOD(bool, CheckProcessNameValidPutDeviceProfileInfoList, (const std::string &)); + MOCK_METHOD(bool, CheckReadLocalDeviceName, ()); }; } }