From 8e43726b6b9d97d1dc355ba91c14ccaa8697f0e2 Mon Sep 17 00:00:00 2001 From: caiminggang Date: Tue, 16 May 2023 14:12:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9EDO=E5=9C=A8=E5=AD=90?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=B8=8B=E5=8F=AF=E4=BB=A5=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E6=8E=A5=E5=8F=A3=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: caiminggang --- services/edm/include/admin_manager.h | 4 +-- services/edm/src/admin_manager.cpp | 29 ++++++++++++++---- .../edm/src/enterprise_device_mgr_ability.cpp | 18 +++++------ .../enterprise_device_mgr_ability_test.cpp | 3 +- test/unittest/src/admin_manager_test.cpp | 30 ++++++++++--------- 5 files changed, 50 insertions(+), 34 deletions(-) diff --git a/services/edm/include/admin_manager.h b/services/edm/include/admin_manager.h index 68a1cd5f8..6fd0f8f99 100644 --- a/services/edm/include/admin_manager.h +++ b/services/edm/include/admin_manager.h @@ -50,8 +50,8 @@ public: std::vector &permissions, int32_t userId); ErrCode GetEntInfo(const std::string &packageName, EntInfo &entInfo, int32_t userId); ErrCode SetEntInfo(const std::string &packageName, EntInfo &entInfo, int32_t userId); - void SaveSubscribeEvents(const std::vector &events, std::shared_ptr &admin, int32_t userId); - void RemoveSubscribeEvents(const std::vector &events, std::shared_ptr &admin, int32_t userId); + void SaveSubscribeEvents(const std::vector &events, const std::string &bundleName, int32_t userId); + void RemoveSubscribeEvents(const std::vector &events, const std::string &bundleName, int32_t userId); ErrCode GetSuperAdmin(std::shared_ptr &admin); virtual ~AdminManager(); diff --git a/services/edm/src/admin_manager.cpp b/services/edm/src/admin_manager.cpp index dc9196443..a8180f23f 100644 --- a/services/edm/src/admin_manager.cpp +++ b/services/edm/src/admin_manager.cpp @@ -133,9 +133,14 @@ ErrCode AdminManager::SetAdminValue(AppExecFwk::ExtensionAbilityInfo &abilityInf std::shared_ptr AdminManager::GetAdminByPkgName(const std::string &packageName, int32_t userId) { + std::shared_ptr superAdmin; + if (IsSuperAdminExist() && SUCCEEDED(GetSuperAdmin(superAdmin)) && + superAdmin->adminInfo_.packageName_ == packageName) { + EDMLOGD("GetAdminByPkgName::get super admin %{public}s userId = %{public}d", packageName.c_str(), userId); + return superAdmin; + } std::vector> userAdmin; - bool ret = GetAdminByUserId(userId, userAdmin); - if (!ret) { + if (!GetAdminByUserId(userId, userAdmin)) { EDMLOGW("GetAdminByPkgName::get userId Admin failed. userId = %{public}d", userId); return nullptr; } @@ -293,6 +298,10 @@ ErrCode AdminManager::GetSuperAdmin(std::shared_ptr &admin) } auto adminItem = std::find_if(userAdmin.begin(), userAdmin.end(), [](const std::shared_ptr &admin) { return admin->adminInfo_.adminType_ == AdminType::ENT; }); + if (adminItem == userAdmin.end()) { + EDMLOGW("IsSuperAdminExist::not find super Admin"); + return ERR_EDM_SUPER_ADMIN_NOT_FOUND; + } admin = *adminItem; return ERR_OK; } @@ -332,9 +341,13 @@ ErrCode AdminManager::SetEntInfo(const std::string &packageName, EntInfo &entInf return ERR_EDM_UNKNOWN_ADMIN; } -void AdminManager::SaveSubscribeEvents(const std::vector &events, - std::shared_ptr &admin, int32_t userId) +void AdminManager::SaveSubscribeEvents(const std::vector &events, const std::string &bundleName, + int32_t userId) { + std::shared_ptr admin = GetAdminByPkgName(bundleName, userId); + if (admin == nullptr) { + return; + } size_t eventsNumber = admin->adminInfo_.managedEvents_.size(); for (auto &event : events) { std::vector managedEvents = admin->adminInfo_.managedEvents_; @@ -348,9 +361,13 @@ void AdminManager::SaveSubscribeEvents(const std::vector &events, } } -void AdminManager::RemoveSubscribeEvents(const std::vector &events, - std::shared_ptr &admin, int32_t userId) +void AdminManager::RemoveSubscribeEvents(const std::vector &events, const std::string &bundleName, + int32_t userId) { + std::shared_ptr admin = GetAdminByPkgName(bundleName, userId); + if (admin == nullptr) { + return; + } size_t eventsNumber = admin->adminInfo_.managedEvents_.size(); for (auto &event : events) { auto iter = admin->adminInfo_.managedEvents_.begin(); diff --git a/services/edm/src/enterprise_device_mgr_ability.cpp b/services/edm/src/enterprise_device_mgr_ability.cpp index df70894ab..9106c772d 100644 --- a/services/edm/src/enterprise_device_mgr_ability.cpp +++ b/services/edm/src/enterprise_device_mgr_ability.cpp @@ -899,7 +899,8 @@ ErrCode EnterpriseDeviceMgrAbility::GetEnterpriseInfo(AppExecFwk::ElementName &a { std::lock_guard autoLock(mutexLock_); EntInfo entInfo; - ErrCode code = adminMgr_->GetEntInfo(admin.GetBundleName(), entInfo, GetCurrentUserId()); + int32_t userId = adminMgr_->IsSuperAdmin(admin.GetBundleName()) ? DEFAULT_USER_ID : GetCurrentUserId(); + ErrCode code = adminMgr_->GetEntInfo(admin.GetBundleName(), entInfo, userId); if (code != ERR_OK) { reply.WriteInt32(EdmReturnErrCode::ADMIN_INACTIVE); return EdmReturnErrCode::ADMIN_INACTIVE; @@ -919,7 +920,7 @@ ErrCode EnterpriseDeviceMgrAbility::SetEnterpriseInfo(AppExecFwk::ElementName &a EDMLOGW("EnterpriseDeviceMgrAbility::SetEnterpriseInfo: check permission failed"); return EdmReturnErrCode::PERMISSION_DENIED; } - int32_t userId = GetCurrentUserId(); + int32_t userId = adminMgr_->IsSuperAdmin(admin.GetBundleName()) ? DEFAULT_USER_ID : GetCurrentUserId(); std::shared_ptr adminItem = adminMgr_->GetAdminByPkgName(admin.GetBundleName(), userId); if (adminItem == nullptr) { return EdmReturnErrCode::ADMIN_INACTIVE; @@ -955,9 +956,8 @@ ErrCode EnterpriseDeviceMgrAbility::SubscribeManagedEvent(const AppExecFwk::Elem std::lock_guard autoLock(mutexLock_); RETURN_IF_FAILED(VerifyManagedEvent(admin, events)); RETURN_IF_FAILED(HandleApplicationEvent(events, true)); - int32_t userId = GetCurrentUserId(); - std::shared_ptr adminItem = adminMgr_->GetAdminByPkgName(admin.GetBundleName(), userId); - adminMgr_->SaveSubscribeEvents(events, adminItem, userId); + int32_t userId = adminMgr_->IsSuperAdmin(admin.GetBundleName()) ? DEFAULT_USER_ID : GetCurrentUserId(); + adminMgr_->SaveSubscribeEvents(events, admin.GetBundleName(), userId); return ERR_OK; } @@ -966,9 +966,8 @@ ErrCode EnterpriseDeviceMgrAbility::UnsubscribeManagedEvent(const AppExecFwk::El { std::lock_guard autoLock(mutexLock_); RETURN_IF_FAILED(VerifyManagedEvent(admin, events)); - int32_t userId = GetCurrentUserId(); - std::shared_ptr adminItem = adminMgr_->GetAdminByPkgName(admin.GetBundleName(), userId); - adminMgr_->RemoveSubscribeEvents(events, adminItem, userId); + int32_t userId = adminMgr_->IsSuperAdmin(admin.GetBundleName()) ? DEFAULT_USER_ID : GetCurrentUserId(); + adminMgr_->RemoveSubscribeEvents(events, admin.GetBundleName(), userId); return HandleApplicationEvent(events, false); } @@ -979,8 +978,7 @@ ErrCode EnterpriseDeviceMgrAbility::VerifyManagedEvent(const AppExecFwk::Element EDMLOGW("EnterpriseDeviceMgrAbility::VerifyManagedEvent: check permission failed"); return EdmReturnErrCode::PERMISSION_DENIED; } - int32_t userId = GetCurrentUserId(); - std::shared_ptr adminItem = adminMgr_->GetAdminByPkgName(admin.GetBundleName(), userId); + std::shared_ptr adminItem = adminMgr_->GetAdminByPkgName(admin.GetBundleName(), GetCurrentUserId()); if (adminItem == nullptr) { return EdmReturnErrCode::ADMIN_INACTIVE; } diff --git a/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp b/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp index 0893256ab..7e0c3e0f0 100644 --- a/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp +++ b/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp @@ -686,9 +686,8 @@ HWTEST_F(EnterpriseDeviceMgrAbilityTest, TestOnCommonEventPackageAdded, TestSize entInfo.enterpriseName = "company1"; entInfo.description = "technology company in wuhan1"; edmMgr_->adminMgr_->SetAdminValue(abilityInfo, entInfo, AdminType::NORMAL, permissions, DEFAULT_USER_ID); - std::shared_ptr adminItem = edmMgr_->adminMgr_->GetAdminByPkgName("com.edm.test.demo", DEFAULT_USER_ID); const std::vector events = {BUNDLE_ADDED_EVENT, BUNDLE_REMOVED_EVENT}; - edmMgr_->adminMgr_->SaveSubscribeEvents(events, adminItem, DEFAULT_USER_ID); + edmMgr_->adminMgr_->SaveSubscribeEvents(events, "com.edm.test.demo", DEFAULT_USER_ID); std::string action = "usual.event.PACKAGE_ADDED"; want.SetAction(action); diff --git a/test/unittest/src/admin_manager_test.cpp b/test/unittest/src/admin_manager_test.cpp index 09b0bccb8..ee5f26e68 100644 --- a/test/unittest/src/admin_manager_test.cpp +++ b/test/unittest/src/admin_manager_test.cpp @@ -460,12 +460,10 @@ HWTEST_F(AdminManagerTest, TestGetAdminBySubscribeEvent, TestSize.Level1) entInfo.description = "technology company in wuhan2"; adminMgr_->SetAdminValue(abilityInfo, entInfo, AdminType::NORMAL, permissions, TEST_USER_ID); - std::shared_ptr adminItem = adminMgr_->GetAdminByPkgName("com.edm.test.demo", DEFAULT_USER_ID); const std::vector events = {0, 1}; - adminMgr_->SaveSubscribeEvents(events, adminItem, DEFAULT_USER_ID); + adminMgr_->SaveSubscribeEvents(events, "com.edm.test.demo", DEFAULT_USER_ID); const std::vector events1 = {1}; - std::shared_ptr adminItem1 = adminMgr_->GetAdminByPkgName("com.edm.test.demo2", TEST_USER_ID); - adminMgr_->SaveSubscribeEvents(events1, adminItem1, TEST_USER_ID); + adminMgr_->SaveSubscribeEvents(events1, "com.edm.test.demo2", TEST_USER_ID); std::unordered_map>> subscribeAdmins; adminMgr_->GetAdminBySubscribeEvent(ManagedEvent::BUNDLE_ADDED, subscribeAdmins); @@ -480,9 +478,6 @@ HWTEST_F(AdminManagerTest, TestGetAdminBySubscribeEvent, TestSize.Level1) */ HWTEST_F(AdminManagerTest, TestSaveSubscribeEvents, TestSize.Level1) { - std::shared_ptr admin = std::make_shared(); - admin->adminInfo_.managedEvents_.push_back(ManagedEvent::BUNDLE_ADDED); - std::vector events = {0}; AppExecFwk::ExtensionAbilityInfo abilityInfo; abilityInfo.bundleName = "com.edm.test.demo"; abilityInfo.name = "testDemo"; @@ -491,10 +486,14 @@ HWTEST_F(AdminManagerTest, TestSaveSubscribeEvents, TestSize.Level1) entInfo.description = "technology company in wuhan"; std::vector permissions = { "ohos.permission.EDM_TEST_PERMISSION" }; adminMgr_->SetAdminValue(abilityInfo, entInfo, AdminType::NORMAL, permissions, DEFAULT_USER_ID); - adminMgr_->SaveSubscribeEvents(events, admin, DEFAULT_USER_ID); + + std::shared_ptr admin = adminMgr_->GetAdminByPkgName(abilityInfo.bundleName, DEFAULT_USER_ID); + ASSERT_TRUE(admin != nullptr); + std::vector events = {0}; + adminMgr_->SaveSubscribeEvents(events, abilityInfo.bundleName, DEFAULT_USER_ID); ASSERT_TRUE(admin->adminInfo_.managedEvents_.size() == 1); events.push_back(1); - adminMgr_->SaveSubscribeEvents(events, admin, DEFAULT_USER_ID); + adminMgr_->SaveSubscribeEvents(events, abilityInfo.bundleName, DEFAULT_USER_ID); ASSERT_TRUE(admin->adminInfo_.managedEvents_.size() > 1); } @@ -505,9 +504,6 @@ HWTEST_F(AdminManagerTest, TestSaveSubscribeEvents, TestSize.Level1) */ HWTEST_F(AdminManagerTest, TestRemoveSubscribeEvents, TestSize.Level1) { - std::shared_ptr admin = std::make_shared(); - admin->adminInfo_.managedEvents_.push_back(ManagedEvent::BUNDLE_ADDED); - std::vector events = {1}; AppExecFwk::ExtensionAbilityInfo abilityInfo; abilityInfo.bundleName = "com.edm.test.demo"; abilityInfo.name = "testDemo"; @@ -516,10 +512,16 @@ HWTEST_F(AdminManagerTest, TestRemoveSubscribeEvents, TestSize.Level1) entInfo.description = "technology company in wuhan"; std::vector permissions = { "ohos.permission.EDM_TEST_PERMISSION" }; adminMgr_->SetAdminValue(abilityInfo, entInfo, AdminType::NORMAL, permissions, DEFAULT_USER_ID); - adminMgr_->RemoveSubscribeEvents(events, admin, DEFAULT_USER_ID); + + std::shared_ptr admin = adminMgr_->GetAdminByPkgName(abilityInfo.bundleName, DEFAULT_USER_ID); + ASSERT_TRUE(admin != nullptr); + admin->adminInfo_.managedEvents_.push_back(ManagedEvent::BUNDLE_ADDED); + + std::vector events = {1}; + adminMgr_->RemoveSubscribeEvents(events, abilityInfo.bundleName, DEFAULT_USER_ID); ASSERT_TRUE(admin->adminInfo_.managedEvents_.size() == 1); events.push_back(0); - adminMgr_->RemoveSubscribeEvents(events, admin, DEFAULT_USER_ID); + adminMgr_->RemoveSubscribeEvents(events, abilityInfo.bundleName, DEFAULT_USER_ID); ASSERT_TRUE(admin->adminInfo_.managedEvents_.empty()); } } // namespace TEST -- Gitee From bf13e0546a8ad816c4c26a164fc4f13c7a4dc1ee Mon Sep 17 00:00:00 2001 From: fangyun Date: Tue, 16 May 2023 16:00:38 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9getDeviceInfoProxy?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fangyun --- .../include/application_manager_proxy.h | 1 - .../include/datetime_manager_proxy.h | 1 - .../include/device_control_proxy.h | 1 - .../device_info/include/device_info_proxy.h | 6 ++++-- .../device_info/src/device_info_proxy.cpp | 16 +++++++++++++++ .../include/network_manager_proxy.h | 1 - .../wifi_manager/include/wifi_manager_proxy.h | 1 - .../device_info/src/device_info_addon.cpp | 20 +++++++++++++++++-- .../include/enterprise_device_mgr_ability.h | 2 +- .../edm/src/enterprise_device_mgr_ability.cpp | 8 +++++++- .../device_info_proxy_test.cpp | 5 +++-- 11 files changed, 49 insertions(+), 13 deletions(-) diff --git a/interfaces/inner_api/application_manager/include/application_manager_proxy.h b/interfaces/inner_api/application_manager/include/application_manager_proxy.h index 5d6008440..02a680bcd 100644 --- a/interfaces/inner_api/application_manager/include/application_manager_proxy.h +++ b/interfaces/inner_api/application_manager/include/application_manager_proxy.h @@ -29,7 +29,6 @@ public: int32_t GetDisallowedRunningBundles(AppExecFwk::ElementName &admin, int32_t userId, std::vector &bundles); private: - static std::shared_ptr proxy_; static std::shared_ptr instance_; static std::mutex mutexLock_; }; diff --git a/interfaces/inner_api/datetime_manager/include/datetime_manager_proxy.h b/interfaces/inner_api/datetime_manager/include/datetime_manager_proxy.h index 06553a43d..e4d220dee 100644 --- a/interfaces/inner_api/datetime_manager/include/datetime_manager_proxy.h +++ b/interfaces/inner_api/datetime_manager/include/datetime_manager_proxy.h @@ -29,7 +29,6 @@ public: int32_t IsModifyDateTimeDisallowed(AppExecFwk::ElementName &admin, bool hasAdmin, bool &result); private: - static std::shared_ptr proxy_; static std::shared_ptr instance_; static std::mutex mutexLock_; }; diff --git a/interfaces/inner_api/device_control/include/device_control_proxy.h b/interfaces/inner_api/device_control/include/device_control_proxy.h index 059db1ba6..e759d6173 100644 --- a/interfaces/inner_api/device_control/include/device_control_proxy.h +++ b/interfaces/inner_api/device_control/include/device_control_proxy.h @@ -27,7 +27,6 @@ public: static std::shared_ptr GetDeviceControlProxy(); int32_t ResetFactory(AppExecFwk::ElementName &admin); private: - static std::shared_ptr proxy_; static std::shared_ptr instance_; static std::mutex mutexLock_; }; diff --git a/interfaces/inner_api/device_info/include/device_info_proxy.h b/interfaces/inner_api/device_info/include/device_info_proxy.h index 9e667f96b..8bf7ff804 100644 --- a/interfaces/inner_api/device_info/include/device_info_proxy.h +++ b/interfaces/inner_api/device_info/include/device_info_proxy.h @@ -24,9 +24,11 @@ public: DeviceInfoProxy(); ~DeviceInfoProxy(); static std::shared_ptr GetDeviceInfoProxy(); - int32_t GetDeviceInfo(AppExecFwk::ElementName &admin, std::string &info, int policyCode); + int32_t GetDeviceSerial(AppExecFwk::ElementName &admin, std::string &info); + int32_t GetDisplayVersion(AppExecFwk::ElementName &admin, std::string &info); + int32_t GetDeviceName(AppExecFwk::ElementName &admin, std::string &info); private: - static std::shared_ptr proxy_; + int32_t GetDeviceInfo(AppExecFwk::ElementName &admin, std::string &info, int policyCode); static std::shared_ptr instance_; static std::mutex mutexLock_; }; diff --git a/interfaces/inner_api/device_info/src/device_info_proxy.cpp b/interfaces/inner_api/device_info/src/device_info_proxy.cpp index 26cae9b17..b49a03ca0 100644 --- a/interfaces/inner_api/device_info/src/device_info_proxy.cpp +++ b/interfaces/inner_api/device_info/src/device_info_proxy.cpp @@ -16,6 +16,7 @@ #include "device_info_proxy.h" #include "edm_log.h" #include "func_code.h" +#include "policy_info.h" namespace OHOS { namespace EDM { @@ -39,6 +40,21 @@ std::shared_ptr DeviceInfoProxy::GetDeviceInfoProxy() return instance_; } +int32_t DeviceInfoProxy::GetDeviceSerial(AppExecFwk::ElementName &admin, std::string &info) +{ + return GetDeviceInfo(admin, info, GET_DEVICE_SERIAL); +} + +int32_t DeviceInfoProxy::GetDisplayVersion(AppExecFwk::ElementName &admin, std::string &info) +{ + return GetDeviceInfo(admin, info, GET_DISPLAY_VERSION); +} + +int32_t DeviceInfoProxy::GetDeviceName(AppExecFwk::ElementName &admin, std::string &info) +{ + return GetDeviceInfo(admin, info, GET_DEVICE_NAME); +} + int32_t DeviceInfoProxy::GetDeviceInfo(AppExecFwk::ElementName &admin, std::string &info, int policyCode) { EDMLOGD("DeviceInfoProxy::GetDeviceInfo %{pubilc}d", policyCode); diff --git a/interfaces/inner_api/network_manager/include/network_manager_proxy.h b/interfaces/inner_api/network_manager/include/network_manager_proxy.h index f755a29ed..b46bf218a 100644 --- a/interfaces/inner_api/network_manager/include/network_manager_proxy.h +++ b/interfaces/inner_api/network_manager/include/network_manager_proxy.h @@ -33,7 +33,6 @@ public: int32_t IsNetworkInterfaceDisabled(const AppExecFwk::ElementName &admin, const std::string &networkInterface, bool &status); private: - static std::shared_ptr proxy_; static std::shared_ptr instance_; static std::mutex mutexLock_; }; diff --git a/interfaces/inner_api/wifi_manager/include/wifi_manager_proxy.h b/interfaces/inner_api/wifi_manager/include/wifi_manager_proxy.h index dd45c5ed9..e8ddec616 100644 --- a/interfaces/inner_api/wifi_manager/include/wifi_manager_proxy.h +++ b/interfaces/inner_api/wifi_manager/include/wifi_manager_proxy.h @@ -29,7 +29,6 @@ public: int32_t IsWifiActive(const AppExecFwk::ElementName &admin, bool &result); int32_t SetWifiProfile(const AppExecFwk::ElementName &admin, const Wifi::WifiDeviceConfig &config); private: - static std::shared_ptr proxy_; static std::shared_ptr instance_; static std::mutex mutexLock_; }; diff --git a/interfaces/kits/device_info/src/device_info_addon.cpp b/interfaces/kits/device_info/src/device_info_addon.cpp index 3767b0be8..a25a1be24 100644 --- a/interfaces/kits/device_info/src/device_info_addon.cpp +++ b/interfaces/kits/device_info/src/device_info_addon.cpp @@ -95,8 +95,24 @@ void DeviceInfoAddon::NativeGetDeviceInfo(napi_env env, void *data) EDMLOGE("can not get DeviceInfoProxy"); return; } - asyncCallbackInfo->ret = deviceInfoProxy->GetDeviceInfo(asyncCallbackInfo->elementName, - asyncCallbackInfo->stringRet, asyncCallbackInfo->policyCode); + switch (asyncCallbackInfo->policyCode) { + case static_cast(GET_DEVICE_SERIAL): + asyncCallbackInfo->ret = deviceInfoProxy->GetDeviceSerial(asyncCallbackInfo->elementName, + asyncCallbackInfo->stringRet); + break; + case static_cast(GET_DISPLAY_VERSION): + asyncCallbackInfo->ret = deviceInfoProxy->GetDisplayVersion(asyncCallbackInfo->elementName, + asyncCallbackInfo->stringRet); + break; + case static_cast(GET_DEVICE_NAME): + asyncCallbackInfo->ret = deviceInfoProxy->GetDeviceName(asyncCallbackInfo->elementName, + asyncCallbackInfo->stringRet); + break; + default: + asyncCallbackInfo->ret = EdmReturnErrCode::INTERFACE_UNSUPPORTED; + EDMLOGE("NAPI_GetDeviceInfo failed"); + return; + } } static napi_module g_deviceInfoModule = { diff --git a/services/edm/include/enterprise_device_mgr_ability.h b/services/edm/include/enterprise_device_mgr_ability.h index e6e7b7744..9dfd96cbb 100644 --- a/services/edm/include/enterprise_device_mgr_ability.h +++ b/services/edm/include/enterprise_device_mgr_ability.h @@ -103,7 +103,7 @@ private: static std::mutex mutexLock_; static sptr instance_; std::shared_ptr policyMgr_; - std::map> policyMgrMap_; + std::map> policyMgrMap_; std::shared_ptr adminMgr_; std::shared_ptr pluginMgr_; bool registerToService_ = false; diff --git a/services/edm/src/enterprise_device_mgr_ability.cpp b/services/edm/src/enterprise_device_mgr_ability.cpp index df70894ab..6aae91d39 100644 --- a/services/edm/src/enterprise_device_mgr_ability.cpp +++ b/services/edm/src/enterprise_device_mgr_ability.cpp @@ -747,7 +747,7 @@ std::shared_ptr EnterpriseDeviceMgrAbility::GetAndSwitchPolicyMan EDMLOGI("get policyMgr failed create success userId : %{public}d", userId); policyMgr->Init(); } else { - policyMgr = policyMgrMap_.find(userId)->second; + policyMgr = iter->second; } IPolicyManager::policyManagerInstance_ = policyMgr.get(); return policyMgr; @@ -825,6 +825,11 @@ ErrCode EnterpriseDeviceMgrAbility::GetDevicePolicy(uint32_t code, MessageParcel int32_t userId) { std::lock_guard autoLock(mutexLock_); + bool isUserExist = false; + AccountSA::OsAccountManager::IsOsAccountExists(userId, isUserExist); + if (!isUserExist) { + return EdmReturnErrCode::PARAM_ERROR; + } std::shared_ptr plugin = pluginMgr_->GetPluginByFuncCode(code); if (plugin == nullptr) { EDMLOGW("GetDevicePolicy: get plugin failed"); @@ -832,6 +837,7 @@ ErrCode EnterpriseDeviceMgrAbility::GetDevicePolicy(uint32_t code, MessageParcel return EdmReturnErrCode::INTERFACE_UNSUPPORTED; } std::string adminName; + // has admin if (data.ReadInt32() == 0) { std::unique_ptr admin(data.ReadParcelable()); std::shared_ptr deviceAdmin = adminMgr_->GetAdminByPkgName(admin->GetBundleName(), GetCurrentUserId()); diff --git a/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp b/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp index c8aee8364..712f83f25 100644 --- a/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp +++ b/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp @@ -73,8 +73,9 @@ HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoSuc, TestSize.Level1) .Times(1) .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy)); std::string info; - int32_t ret = deviceInfoProxy->GetDeviceInfo(admin, info, GET_DEVICE_SERIAL); + int32_t ret = deviceInfoProxy->GetDeviceSerial(admin, info); ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(info == RETURN_STRING); } /** @@ -87,7 +88,7 @@ HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoFail, TestSize.Level1) AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); std::string info; - int32_t ret = deviceInfoProxy->GetDeviceInfo(admin, info, GET_DEVICE_SERIAL); + int32_t ret = deviceInfoProxy->GetDeviceSerial(admin, info); ASSERT_TRUE(ret != ERR_OK); } } // namespace TEST -- Gitee From b0838fd24f2ec5d94c9f39c7ff45d4a135ecea0b Mon Sep 17 00:00:00 2001 From: liwuli Date: Tue, 16 May 2023 21:10:20 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8D=95=E7=8B=AC?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=A5=97=E8=BF=90=E8=A1=8C=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liwuli --- services/edm/include/admin_manager.h | 3 +- services/edm/src/admin_manager.cpp | 5 +- .../edm/src/enterprise_device_mgr_ability.cpp | 2 +- test/unittest/account_manager_proxy/BUILD.gn | 11 +- .../account_manager_proxy_test.cpp | 44 ++++- .../application_manager_proxy/BUILD.gn | 14 +- .../application_manager_proxy_test.cpp | 87 ++++++++- test/unittest/bundle_manager_proxy/BUILD.gn | 18 +- .../bundle_manager_proxy_test.cpp | 180 +++++++++++++++--- test/unittest/datetime_manager_proxy/BUILD.gn | 9 +- .../datetime_manager_proxy_test.cpp | 113 +++++++++-- test/unittest/device_control_proxy/BUILD.gn | 23 ++- .../device_control_proxy_test.cpp | 42 +++- .../device_info_proxy_test.cpp | 11 +- .../allowed_install_bundles_plugin_test.cpp | 6 +- .../allowed_install_bundles_plugin_test.h | 4 +- .../edm_plugin/device_info_plugin_test.cpp | 6 +- .../edm_plugin/device_info_plugin_test.h | 4 +- ...disallow_add_local_account_plugin_test.cpp | 6 +- .../disallow_add_local_account_plugin_test.h | 4 +- .../disallow_modify_datetime_plugin_test.cpp | 6 +- .../disallow_modify_datetime_plugin_test.h | 4 +- ...disallowed_install_bundles_plugin_test.cpp | 6 +- .../disallowed_install_bundles_plugin_test.h | 4 +- ...disallowed_running_bundles_plugin_test.cpp | 6 +- .../disallowed_running_bundles_plugin_test.h | 4 +- .../get_device_name_plugin_test.cpp | 10 +- .../edm_plugin/is_wifi_active_plugin_test.cpp | 10 +- .../network_manager_plugin_test.cpp | 10 +- .../edm_plugin/reset_factory_plugin_test.cpp | 6 +- .../edm_plugin/reset_factory_plugin_test.h | 4 +- .../set_wifi_profile_plugin_test.cpp | 10 +- .../enterprise_device_mgr_ability_test.cpp | 12 ++ .../enterprise_device_mgr_proxy/BUILD.gn | 1 + .../enterprise_device_mgr_proxy_test.cpp | 9 + test/unittest/include/utils.h | 2 + .../network_manager_proxy_test.cpp | 19 +- test/unittest/src/admin_manager_test.cpp | 21 +- .../src/edm_data_ability_utils_test.cpp | 10 + test/unittest/src/edm_sys_manager_test.cpp | 25 ++- test/unittest/src/utils.cpp | 11 ++ .../wifi_manager_proxy_test.cpp | 35 +++- 42 files changed, 680 insertions(+), 137 deletions(-) diff --git a/services/edm/include/admin_manager.h b/services/edm/include/admin_manager.h index 68a1cd5f8..7c2a3a5e0 100644 --- a/services/edm/include/admin_manager.h +++ b/services/edm/include/admin_manager.h @@ -38,8 +38,7 @@ public: ErrCode DeleteAdmin(const std::string &packageName, int32_t userId); ErrCode UpdateAdmin(AppExecFwk::ExtensionAbilityInfo &abilityInfo, const std::vector &permissions, int32_t userId); - ErrCode GetGrantedPermission(AppExecFwk::ExtensionAbilityInfo &abilityInfo, std::vector &permissions, - AdminType type); + ErrCode GetGrantedPermission(std::vector &permissions, AdminType type); bool IsSuperAdminExist(); bool IsAdminExist(); bool IsSuperAdmin(const std::string &bundleName); diff --git a/services/edm/src/admin_manager.cpp b/services/edm/src/admin_manager.cpp index dc9196443..c82ca616e 100644 --- a/services/edm/src/admin_manager.cpp +++ b/services/edm/src/admin_manager.cpp @@ -177,8 +177,7 @@ ErrCode AdminManager::DeleteAdmin(const std::string &packageName, int32_t userId return ERR_EDM_UNKNOWN_ADMIN; } -ErrCode AdminManager::GetGrantedPermission(AppExecFwk::ExtensionAbilityInfo& abilityInfo, - std::vector& permissions, AdminType type) +ErrCode AdminManager::GetGrantedPermission(std::vector& permissions, AdminType type) { if (permissions.empty()) { EDMLOGW("GetGrantedPermission::permissions is empty"); @@ -214,7 +213,7 @@ ErrCode AdminManager::UpdateAdmin(AppExecFwk::ExtensionAbilityInfo &abilityInfo, } std::vector combinePermission = permissions; - ErrCode ret = GetGrantedPermission(abilityInfo, combinePermission, adminItem->adminInfo_.adminType_); + ErrCode ret = GetGrantedPermission(combinePermission, adminItem->adminInfo_.adminType_); if (ret != ERR_OK) { EDMLOGW("UpdateAdmin::GetGrantedPermission failed"); return ret; diff --git a/services/edm/src/enterprise_device_mgr_ability.cpp b/services/edm/src/enterprise_device_mgr_ability.cpp index df70894ab..d1fb15e8b 100644 --- a/services/edm/src/enterprise_device_mgr_ability.cpp +++ b/services/edm/src/enterprise_device_mgr_ability.cpp @@ -500,7 +500,7 @@ ErrCode EnterpriseDeviceMgrAbility::EnableAdmin(AppExecFwk::ElementName &admin, return EdmReturnErrCode::COMPONENT_INVALID; } /* Filter permissions with AdminType, such as NORMAL can't request super permission */ - if (FAILED(adminMgr_->GetGrantedPermission(abilityInfo.at(0), permissionList, type))) { + if (FAILED(adminMgr_->GetGrantedPermission(permissionList, type))) { EDMLOGW("EnableAdmin: GetGrantedPermission failed"); // permission verify, should throw exception if failed return EdmReturnErrCode::ENABLE_ADMIN_FAILED; diff --git a/test/unittest/account_manager_proxy/BUILD.gn b/test/unittest/account_manager_proxy/BUILD.gn index 2c4a536ee..b2c5668ee 100644 --- a/test/unittest/account_manager_proxy/BUILD.gn +++ b/test/unittest/account_manager_proxy/BUILD.gn @@ -24,7 +24,12 @@ ohos_unittest("EdmAccountManagerProxyUnitTest") { "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test", ] - sources = [ "./account_manager_proxy_test.cpp" ] + sources = [ + "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_stub_mock.cpp", + "../mock/src/edm_sys_manager_mock.cpp", + "../src/utils.cpp", + "./account_manager_proxy_test.cpp", + ] configs = [ "../../../common/config:coverage_flags" ] @@ -37,6 +42,10 @@ ohos_unittest("EdmAccountManagerProxyUnitTest") { external_deps = [ "ability_base:want", "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", ] diff --git a/test/unittest/account_manager_proxy/account_manager_proxy_test.cpp b/test/unittest/account_manager_proxy/account_manager_proxy_test.cpp index 497b6dd49..709937d07 100644 --- a/test/unittest/account_manager_proxy/account_manager_proxy_test.cpp +++ b/test/unittest/account_manager_proxy/account_manager_proxy_test.cpp @@ -15,9 +15,13 @@ #include #include +#include #include #include "account_manager_proxy.h" +#include "edm_sys_manager_mock.h" +#include "enterprise_device_mgr_stub_mock.h" +#include "utils.h" using namespace testing::ext; using namespace testing; @@ -32,29 +36,61 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr accountManagerProxy = nullptr; + std::shared_ptr edmSysManager_ = nullptr; + sptr object_ = nullptr; }; void AccountManagerProxyTest::SetUp() { accountManagerProxy = AccountManagerProxy::GetAccountManagerProxy(); + edmSysManager_ = std::make_shared(); + object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock(); + edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_); + Utils::SetEdmServiceEnable(); } void AccountManagerProxyTest::TearDown() { accountManagerProxy.reset(); + edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID); + object_ = nullptr; + Utils::SetEdmServiceDisable(); +} + +void AccountManagerProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + +/** + * @tc.name: TestDisallowAddLocalAccountSuc + * @tc.desc: Test DisallowAddLocalAccount success func. + * @tc.type: FUNC + */ +HWTEST_F(AccountManagerProxyTest, TestDisallowAddLocalAccountSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + ErrCode ret = accountManagerProxy->DisallowAddLocalAccount(admin, true); + ASSERT_TRUE(ret == ERR_OK); } /** - * @tc.name: TestDisallowAddLocalAccount - * @tc.desc: Test DisallowAddLocalAccount func. + * @tc.name: TestDisallowAddLocalAccountFail + * @tc.desc: Test DisallowAddLocalAccount without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(AccountManagerProxyTest, TestDisallowAddLocalAccount, TestSize.Level1) +HWTEST_F(AccountManagerProxyTest, TestDisallowAddLocalAccountFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; ErrCode ret = accountManagerProxy->DisallowAddLocalAccount(admin, true); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } } // namespace TEST } // namespace EDM diff --git a/test/unittest/application_manager_proxy/BUILD.gn b/test/unittest/application_manager_proxy/BUILD.gn index 64bf5f2cc..b9395c0af 100644 --- a/test/unittest/application_manager_proxy/BUILD.gn +++ b/test/unittest/application_manager_proxy/BUILD.gn @@ -18,16 +18,26 @@ module_output_path = "enterprise_device_management/services" ohos_unittest("EdmApplicationManagerProxyUnitTest") { module_out_path = module_output_path - include_dirs = [ "../include" ] + include_dirs = [ + "../include", + "../mock/include", + "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test", + ] sources = [ + "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_stub_mock.cpp", + "../mock/src/edm_sys_manager_mock.cpp", "../src/utils.cpp", "./application_manager_proxy_test.cpp", ] configs = [ "../../../common/config:coverage_flags" ] - deps = [ "../../../interfaces/inner_api:edmservice_kits" ] + deps = [ + "../../../interfaces/inner_api:edmservice_kits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] external_deps = [ "ability_base:want", diff --git a/test/unittest/application_manager_proxy/application_manager_proxy_test.cpp b/test/unittest/application_manager_proxy/application_manager_proxy_test.cpp index d06efc314..8db5526e4 100644 --- a/test/unittest/application_manager_proxy/application_manager_proxy_test.cpp +++ b/test/unittest/application_manager_proxy/application_manager_proxy_test.cpp @@ -15,8 +15,12 @@ #include #include +#include #include + #include "application_manager_proxy.h" +#include "edm_sys_manager_mock.h" +#include "enterprise_device_mgr_stub_mock.h" #include "utils.h" using namespace testing::ext; @@ -32,28 +36,59 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr applicationManagerProxy_ = nullptr; + std::shared_ptr edmSysManager_ = nullptr; + sptr object_ = nullptr; }; void ApplicationManagerProxyTest::SetUp() { applicationManagerProxy_ = ApplicationManagerProxy::GetApplicationManagerProxy(); + edmSysManager_ = std::make_shared(); + object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock(); + edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_); Utils::SetEdmServiceEnable(); } void ApplicationManagerProxyTest::TearDown() { applicationManagerProxy_.reset(); + edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID); + object_ = nullptr; Utils::SetEdmServiceDisable(); } +void ApplicationManagerProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + /** - * @tc.name: TestAddDisallowedRunningBundles - * @tc.desc: Test AddDisallowedRunningBundles func. + * @tc.name: TestAddDisallowedRunningBundlesSuc + * @tc.desc: Test AddDisallowedRunningBundles success func. + * @tc.type: FUNC + */ +HWTEST_F(ApplicationManagerProxyTest, AddDisallowedRunningBundlesSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + ErrCode ret = applicationManagerProxy_->AddDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID); + ASSERT_TRUE(ret == ERR_OK); +} + +/** + * @tc.name: TestAddDisallowedRunningBundlesFail + * @tc.desc: Test AddDisallowedRunningBundles without enable edm service func. * @tc.type: FUNC */ HWTEST_F(ApplicationManagerProxyTest, AddDisallowedRunningBundles, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; ErrCode ret = applicationManagerProxy_->AddDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID); @@ -61,12 +96,29 @@ HWTEST_F(ApplicationManagerProxyTest, AddDisallowedRunningBundles, TestSize.Leve } /** - * @tc.name: TestRemoveDisallowedRunningBundles - * @tc.desc: Test RemoveDisallowedRunningBundles func. + * @tc.name: TestRemoveDisallowedRunningBundlesSuc + * @tc.desc: Test RemoveDisallowedRunningBundles success func. + * @tc.type: FUNC + */ +HWTEST_F(ApplicationManagerProxyTest, RemoveDisallowedRunningBundlesSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + ErrCode ret = applicationManagerProxy_->RemoveDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID); + ASSERT_TRUE(ret == ERR_OK); +} + +/** + * @tc.name: TestRemoveDisallowedRunningBundlesFail + * @tc.desc: Test RemoveDisallowedRunningBundles without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(ApplicationManagerProxyTest, RemoveDisallowedRunningBundles, TestSize.Level1) +HWTEST_F(ApplicationManagerProxyTest, RemoveDisallowedRunningBundlesFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; ErrCode ret = applicationManagerProxy_->RemoveDisallowedRunningBundles(admin, bundles, DEFAULT_USER_ID); @@ -74,12 +126,31 @@ HWTEST_F(ApplicationManagerProxyTest, RemoveDisallowedRunningBundles, TestSize.L } /** - * @tc.name: TestGetDisallowedRunningBundles - * @tc.desc: Test GetDisallowedRunningBundles func. + * @tc.name: TestGetDisallowedRunningBundlesSuc + * @tc.desc: Test GetDisallowedRunningBundles success func. * @tc.type: FUNC */ -HWTEST_F(ApplicationManagerProxyTest, GetDisallowedRunningBundles, TestSize.Level1) +HWTEST_F(ApplicationManagerProxyTest, GetDisallowedRunningBundlesSuc, TestSize.Level1) { + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy)); + ErrCode ret = applicationManagerProxy_->GetDisallowedRunningBundles(admin, DEFAULT_USER_ID, bundles); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(bundles.size() == 1); + ASSERT_TRUE(bundles[0] == RETURN_STRING); +} + +/** + * @tc.name: TestGetDisallowedRunningBundlesFail + * @tc.desc: Test GetDisallowedRunningBundles without enable edm service func. + * @tc.type: FUNC + */ +HWTEST_F(ApplicationManagerProxyTest, GetDisallowedRunningBundlesFail, TestSize.Level1) +{ + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; ErrCode ret = applicationManagerProxy_->GetDisallowedRunningBundles(admin, DEFAULT_USER_ID, bundles); diff --git a/test/unittest/bundle_manager_proxy/BUILD.gn b/test/unittest/bundle_manager_proxy/BUILD.gn index f1b2450d3..97090f5a4 100644 --- a/test/unittest/bundle_manager_proxy/BUILD.gn +++ b/test/unittest/bundle_manager_proxy/BUILD.gn @@ -20,18 +20,32 @@ ohos_unittest("EdmBundleManagerProxyUnitTest") { include_dirs = [ "../include", + "../mock/include", "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test", ] - sources = [ "./bundle_manager_proxy_test.cpp" ] + sources = [ + "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_stub_mock.cpp", + "../mock/src/edm_sys_manager_mock.cpp", + "../src/utils.cpp", + "./bundle_manager_proxy_test.cpp", + ] configs = [ "../../../common/config:coverage_flags" ] - deps = [ "../../../interfaces/inner_api:edmservice_kits" ] + deps = [ + "../../../interfaces/inner_api:edmservice_kits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] external_deps = [ "ability_base:want", "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", ] diff --git a/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp b/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp index 4529ab884..7a698de8e 100644 --- a/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp +++ b/test/unittest/bundle_manager_proxy/bundle_manager_proxy_test.cpp @@ -15,10 +15,14 @@ #include #include +#include #include #include "bundle_manager_proxy.h" +#include "edm_sys_manager_mock.h" +#include "enterprise_device_mgr_stub_mock.h" #include "policy_type.h" +#include "utils.h" using namespace testing::ext; using namespace testing; @@ -33,101 +37,229 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr bundleManagerProxy = nullptr; + std::shared_ptr edmSysManager_ = nullptr; + sptr object_ = nullptr; }; void BundleManagerProxyTest::SetUp() { bundleManagerProxy = BundleManagerProxy::GetBundleManagerProxy(); + edmSysManager_ = std::make_shared(); + object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock(); + edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_); + Utils::SetEdmServiceEnable(); } void BundleManagerProxyTest::TearDown() { bundleManagerProxy.reset(); + edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID); + object_ = nullptr; + Utils::SetEdmServiceDisable(); +} + +void BundleManagerProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + +/** + * @tc.name: TestAddAllowedInstallBundlesSuc + * @tc.desc: Test AddAllowedInstallBundles success func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, AddAllowedInstallBundlesSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + int32_t policyType = static_cast(PolicyType::ALLOW_INSTALL); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + ErrCode ret = bundleManagerProxy->AddBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); + ASSERT_TRUE(ret == ERR_OK); } /** - * @tc.name: TestAddAllowedInstallBundles - * @tc.desc: Test AddAllowedInstallBundles func. + * @tc.name: TestAddAllowedInstallBundlesFail + * @tc.desc: Test AddAllowedInstallBundles without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(BundleManagerProxyTest, AddAllowedInstallBundles, TestSize.Level1) +HWTEST_F(BundleManagerProxyTest, AddAllowedInstallBundlesFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; int32_t policyType = static_cast(PolicyType::ALLOW_INSTALL); ErrCode ret = bundleManagerProxy->AddBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); +} + +/** + * @tc.name: TestRemoveAllowedInstallBundlesSuc + * @tc.desc: Test RemoveAllowedInstallBundles success func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, RemoveAllowedInstallBundlesSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + int32_t policyType = static_cast(PolicyType::ALLOW_INSTALL); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + ErrCode ret = bundleManagerProxy->RemoveBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); + ASSERT_TRUE(ret == ERR_OK); } /** - * @tc.name: TestRemoveAllowedInstallBundles - * @tc.desc: Test RemoveAllowedInstallBundles func. + * @tc.name: TestRemoveAllowedInstallBundlesFail + * @tc.desc: Test RemoveAllowedInstallBundles without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(BundleManagerProxyTest, RemoveAllowedInstallBundles, TestSize.Level1) +HWTEST_F(BundleManagerProxyTest, RemoveAllowedInstallBundlesFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; int32_t policyType = static_cast(PolicyType::ALLOW_INSTALL); ErrCode ret = bundleManagerProxy->RemoveBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } /** - * @tc.name: TestGetAllowedInstallBundles - * @tc.desc: Test GetAllowedInstallBundles func. + * @tc.name: TestGetAllowedInstallBundlesSuc + * @tc.desc: Test GetAllowedInstallBundles success func. * @tc.type: FUNC */ -HWTEST_F(BundleManagerProxyTest, GetAllowedInstallBundles, TestSize.Level1) +HWTEST_F(BundleManagerProxyTest, GetAllowedInstallBundlesSuc, TestSize.Level1) { OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; int32_t policyType = static_cast(PolicyType::ALLOW_INSTALL); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy)); ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(bundles.size() == 1); + ASSERT_TRUE(bundles[0] == RETURN_STRING); } /** - * @tc.name: TestAddDisallowedInstallBundles - * @tc.desc: Test AddDisallowedInstallBundles func. + * @tc.name: TestGetAllowedInstallBundlesFail + * @tc.desc: Test GetAllowedInstallBundles without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(BundleManagerProxyTest, AddDisallowedInstallBundles, TestSize.Level1) +HWTEST_F(BundleManagerProxyTest, GetAllowedInstallBundlesFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + int32_t policyType = static_cast(PolicyType::ALLOW_INSTALL); + ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); +} + +/** + * @tc.name: TestAddDisallowedInstallBundlesSuc + * @tc.desc: Test AddDisallowedInstallBundles success func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, AddDisallowedInstallBundlesSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + int32_t policyType = static_cast(PolicyType::DISALLOW_INSTALL); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + ErrCode ret = bundleManagerProxy->AddBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); + ASSERT_TRUE(ret == ERR_OK); +} + +/** + * @tc.name: TestAddDisallowedInstallBundlesFail + * @tc.desc: Test AddDisallowedInstallBundles without enable edm service func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, AddDisallowedInstallBundlesFail, TestSize.Level1) +{ + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; int32_t policyType = static_cast(PolicyType::DISALLOW_INSTALL); ErrCode ret = bundleManagerProxy->AddBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } /** - * @tc.name: TestRemoveDisallowedInstallBundles - * @tc.desc: Test RemoveDisallowedInstallBundles func. + * @tc.name: TestRemoveDisallowedInstallBundlesSuc + * @tc.desc: Test RemoveDisallowedInstallBundles success func. * @tc.type: FUNC */ -HWTEST_F(BundleManagerProxyTest, RemoveDisallowedInstallBundles, TestSize.Level1) +HWTEST_F(BundleManagerProxyTest, RemoveDisallowedInstallBundlesSuc, TestSize.Level1) { OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; int32_t policyType = static_cast(PolicyType::DISALLOW_INSTALL); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); ErrCode ret = bundleManagerProxy->RemoveBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == ERR_OK); +} + +/** + * @tc.name: TestRemoveDisallowedInstallBundlesFail + * @tc.desc: Test RemoveDisallowedInstallBundles without enable edm service func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, RemoveDisallowedInstallBundlesFail, TestSize.Level1) +{ + Utils::SetEdmServiceDisable(); + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + int32_t policyType = static_cast(PolicyType::DISALLOW_INSTALL); + ErrCode ret = bundleManagerProxy->RemoveBundlesByPolicyType(admin, bundles, DEFAULT_USER_ID, policyType); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); +} + +/** + * @tc.name: TestGetDisallowedInstallBundlesSuc + * @tc.desc: Test GetDisallowedInstallBundles success func. + * @tc.type: FUNC + */ +HWTEST_F(BundleManagerProxyTest, GetDisallowedInstallBundlesSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + std::vector bundles = {ADMIN_PACKAGENAME}; + int32_t policyType = static_cast(PolicyType::DISALLOW_INSTALL); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeArrayStringSendRequestGetPolicy)); + ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(bundles.size() == 1); + ASSERT_TRUE(bundles[0] == RETURN_STRING); } /** - * @tc.name: TestGetDisallowedInstallBundles - * @tc.desc: Test GetDisallowedInstallBundles func. + * @tc.name: TestGetDisallowedInstallBundlesFail + * @tc.desc: Test GetDisallowedInstallBundles without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(BundleManagerProxyTest, GetDisallowedInstallBundles, TestSize.Level1) +HWTEST_F(BundleManagerProxyTest, GetDisallowedInstallBundlesFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; std::vector bundles = {ADMIN_PACKAGENAME}; int32_t policyType = static_cast(PolicyType::DISALLOW_INSTALL); ErrCode ret = bundleManagerProxy->GetBundlesByPolicyType(admin, DEFAULT_USER_ID, bundles, policyType); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } } // namespace TEST } // namespace EDM diff --git a/test/unittest/datetime_manager_proxy/BUILD.gn b/test/unittest/datetime_manager_proxy/BUILD.gn index 515be78ee..c3d6ef646 100644 --- a/test/unittest/datetime_manager_proxy/BUILD.gn +++ b/test/unittest/datetime_manager_proxy/BUILD.gn @@ -20,17 +20,24 @@ ohos_unittest("EdmDateTimeManagerProxyUnitTest") { include_dirs = [ "../include", + "../mock/include", "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test", ] sources = [ + "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_stub_mock.cpp", + "../mock/src/edm_sys_manager_mock.cpp", "../src/utils.cpp", "./datetime_manager_proxy_test.cpp", ] configs = [ "../../../common/config:coverage_flags" ] - deps = [ "../../../interfaces/inner_api:edmservice_kits" ] + deps = [ + "../../../interfaces/inner_api:edmservice_kits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] external_deps = [ "ability_base:want", diff --git a/test/unittest/datetime_manager_proxy/datetime_manager_proxy_test.cpp b/test/unittest/datetime_manager_proxy/datetime_manager_proxy_test.cpp index 868c6d174..b618a91fc 100644 --- a/test/unittest/datetime_manager_proxy/datetime_manager_proxy_test.cpp +++ b/test/unittest/datetime_manager_proxy/datetime_manager_proxy_test.cpp @@ -15,11 +15,15 @@ #include #include +#include #include #include "datetime_manager_proxy.h" +#include "edm_sys_manager_mock.h" +#include "enterprise_device_mgr_stub_mock.h" #include "utils.h" using namespace testing::ext; +using namespace testing; namespace OHOS { namespace EDM { @@ -30,12 +34,18 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr dateTimeManagerProxy; + std::shared_ptr edmSysManager_ = nullptr; + sptr object_ = nullptr; }; void DatetimeManagerProxyTest::SetUp() { dateTimeManagerProxy = DatetimeManagerProxy::GetDatetimeManagerProxy(); + edmSysManager_ = std::make_shared(); + object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock(); + edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_); Utils::SetEdmServiceEnable(); } @@ -44,60 +54,139 @@ void DatetimeManagerProxyTest::TearDown() if (dateTimeManagerProxy) { dateTimeManagerProxy.reset(); } + edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID); + object_ = nullptr; Utils::SetEdmServiceDisable(); } +void DatetimeManagerProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + +/** + * @tc.name: TestSetDateTimeSuc + * @tc.desc: Test SetDateTime success func. + * @tc.type: FUNC + */ +HWTEST_F(DatetimeManagerProxyTest, TestSetDateTimeSuc, TestSize.Level1) +{ + AppExecFwk::ElementName admin; + admin.SetBundleName("com.edm.test.demo"); + admin.SetAbilityName("com.edm.test.demo.Ability"); + int64_t time = 1674365400000; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + int32_t ret = dateTimeManagerProxy->SetDateTime(admin, time); + EXPECT_TRUE(ret == ERR_OK); +} + /** - * @tc.name: TestSetDateTime - * @tc.desc: Test SetDateTime func. + * @tc.name: TestSetDateTimeFail + * @tc.desc: Test SetDateTime without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(DatetimeManagerProxyTest, TestSetDateTime, TestSize.Level1) +HWTEST_F(DatetimeManagerProxyTest, TestSetDateTimeFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName("com.edm.test.demo"); admin.SetAbilityName("com.edm.test.demo.Ability"); int64_t time = 1674365400000; int32_t ret = dateTimeManagerProxy->SetDateTime(admin, time); - EXPECT_TRUE(ret != ERR_OK); + EXPECT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); +} + +/** + * @tc.name: TestDisallowModifyDateTimeSuc + * @tc.desc: Test DisallowModifyDateTime success func. + * @tc.type: FUNC + */ +HWTEST_F(DatetimeManagerProxyTest, TestDisallowModifyDateTimeSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + ErrCode ret = dateTimeManagerProxy->DisallowModifyDateTime(admin, true); + ASSERT_TRUE(ret == ERR_OK); } /** - * @tc.name: TestDisallowModifyDateTime - * @tc.desc: Test DisallowModifyDateTime func. + * @tc.name: TestDisallowModifyDateTimeFail + * @tc.desc: Test DisallowModifyDateTime without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(DatetimeManagerProxyTest, TestDisallowModifyDateTime, TestSize.Level1) +HWTEST_F(DatetimeManagerProxyTest, TestDisallowModifyDateTimeFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; ErrCode ret = dateTimeManagerProxy->DisallowModifyDateTime(admin, true); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } /** * @tc.name: TestIsModifyDateTimeDisallowed - * @tc.desc: Test IsModifyDateTimeDisallowed func if have Admin. + * @tc.desc: Test IsModifyDateTimeDisallowed success func if have Admin parameter. * @tc.type: FUNC */ HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed001, TestSize.Level1) { OHOS::AppExecFwk::ElementName admin; bool result = true; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy)); ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(admin, true, result); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == ERR_OK); } /** * @tc.name: TestIsModifyDateTimeDisallowed - * @tc.desc: Test IsModifyDateTimeDisallowed func if does not have Admin. + * @tc.desc: Test IsModifyDateTimeDisallowed func if have Admin parameter without enable edm service. * @tc.type: FUNC */ HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed002, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; bool result = true; + ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(admin, true, result); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); +} + +/** + * @tc.name: TestIsModifyDateTimeDisallowed + * @tc.desc: Test IsModifyDateTimeDisallowed success func if does not have Admin parameter. + * @tc.type: FUNC + */ +HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed003, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + bool result = false; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeBoolSendRequestGetPolicy)); + ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(admin, false, result); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(result); +} + +/** + * @tc.name: TestIsModifyDateTimeDisallowed + * @tc.desc: Test IsModifyDateTimeDisallowed func if does not have Admin parameter without enable edm service. + * @tc.type: FUNC + */ +HWTEST_F(DatetimeManagerProxyTest, TestIsModifyDateTimeDisallowed004, TestSize.Level1) +{ + Utils::SetEdmServiceDisable(); + OHOS::AppExecFwk::ElementName admin; + bool result = false; ErrCode ret = dateTimeManagerProxy->IsModifyDateTimeDisallowed(admin, false, result); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_FALSE(result); } } // namespace TEST } // namespace EDM diff --git a/test/unittest/device_control_proxy/BUILD.gn b/test/unittest/device_control_proxy/BUILD.gn index 0a1d18f24..b16533703 100644 --- a/test/unittest/device_control_proxy/BUILD.gn +++ b/test/unittest/device_control_proxy/BUILD.gn @@ -18,21 +18,38 @@ module_output_path = "enterprise_device_management/services" ohos_unittest("EdmDeviceControlProxyUnitTest") { module_out_path = module_output_path - include_dirs = [ "../include" ] + include_dirs = [ + "../include", + "../mock/include", + "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test", + ] - sources = [ "./device_control_proxy_test.cpp" ] + sources = [ + "../enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_stub_mock.cpp", + "../mock/src/edm_sys_manager_mock.cpp", + "../src/utils.cpp", + "./device_control_proxy_test.cpp", + ] configs = [ "../../../common/config:coverage_flags" ] - deps = [ "../../../interfaces/inner_api:edmservice_kits" ] + deps = [ + "../../../interfaces/inner_api:edmservice_kits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] external_deps = [ "ability_base:want", "ability_runtime:ability_manager", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "init:libbegetutil", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/unittest/device_control_proxy/device_control_proxy_test.cpp b/test/unittest/device_control_proxy/device_control_proxy_test.cpp index 11fd35568..9ffc67772 100644 --- a/test/unittest/device_control_proxy/device_control_proxy_test.cpp +++ b/test/unittest/device_control_proxy/device_control_proxy_test.cpp @@ -19,7 +19,10 @@ #include #include "device_control_proxy.h" +#include "edm_sys_manager_mock.h" +#include "enterprise_device_mgr_stub_mock.h" #include "policy_info.h" +#include "utils.h" using namespace testing::ext; using namespace testing; @@ -34,30 +37,63 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr deviceControlProxy = nullptr; + std::shared_ptr edmSysManager_ = nullptr; + sptr object_ = nullptr; }; void DeviceControlProxyTest::SetUp() { deviceControlProxy = DeviceControlProxy::GetDeviceControlProxy(); + edmSysManager_ = std::make_shared(); + object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock(); + edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_); + Utils::SetEdmServiceEnable(); } void DeviceControlProxyTest::TearDown() { deviceControlProxy.reset(); + edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID); + object_ = nullptr; + Utils::SetEdmServiceDisable(); +} + +void DeviceControlProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + +/** + * @tc.name: TestResetFactorySuc + * @tc.desc: Test ResetFactory success func. + * @tc.type: FUNC + */ +HWTEST_F(DeviceControlProxyTest, TestGetDeviceControlSuc, TestSize.Level1) +{ + OHOS::AppExecFwk::ElementName admin; + admin.SetBundleName(ADMIN_PACKAGENAME); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + int32_t ret = deviceControlProxy->ResetFactory(admin); + ASSERT_TRUE(ret == ERR_OK); } /** * @tc.name: TestResetFactoryFail - * @tc.desc: Test ResetFactory func. + * @tc.desc: Test ResetFactory without enable edm service func. * @tc.type: FUNC */ -HWTEST_F(DeviceControlProxyTest, TestGetDeviceInfoFail, TestSize.Level1) +HWTEST_F(DeviceControlProxyTest, TestGetDeviceControlFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); OHOS::AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); int32_t ret = deviceControlProxy->ResetFactory(admin); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } } // namespace TEST } // namespace EDM diff --git a/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp b/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp index c8aee8364..b8a10ba5e 100644 --- a/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp +++ b/test/unittest/device_info_proxy/device_info_proxy_test/device_info_proxy_test.cpp @@ -38,6 +38,7 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr deviceInfoProxy = nullptr; std::shared_ptr edmSysManager_ = nullptr; sptr object_ = nullptr; @@ -60,6 +61,12 @@ void DeviceInfoProxyTest::TearDown() Utils::SetEdmServiceDisable(); } +void DeviceInfoProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + /** * @tc.name: TestGetDeviceInfoSuc * @tc.desc: Test GetDeviceInfo func. @@ -75,6 +82,7 @@ HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoSuc, TestSize.Level1) std::string info; int32_t ret = deviceInfoProxy->GetDeviceInfo(admin, info, GET_DEVICE_SERIAL); ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(info == RETURN_STRING); } /** @@ -84,11 +92,12 @@ HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoSuc, TestSize.Level1) */ HWTEST_F(DeviceInfoProxyTest, TestGetDeviceInfoFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); std::string info; int32_t ret = deviceInfoProxy->GetDeviceInfo(admin, info, GET_DEVICE_SERIAL); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } } // namespace TEST } // namespace EDM diff --git a/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.cpp b/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.cpp index 600b350d8..f8cfd3ca8 100644 --- a/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.cpp +++ b/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.cpp @@ -27,14 +27,16 @@ using namespace testing::ext; namespace OHOS { namespace EDM { namespace TEST { -void AllowedInstallBundlesPluginTest::SetUpTestCase(void) +void AllowedInstallBundlesPluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void AllowedInstallBundlesPluginTest::TearDownTestCase(void) +void AllowedInstallBundlesPluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.h b/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.h index a96a7442f..9dcecdc13 100644 --- a/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.h +++ b/test/unittest/edm_plugin/allowed_install_bundles_plugin_test.h @@ -24,9 +24,9 @@ namespace EDM { namespace TEST { class AllowedInstallBundlesPluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; } // namespace TEST } // namespace EDM diff --git a/test/unittest/edm_plugin/device_info_plugin_test.cpp b/test/unittest/edm_plugin/device_info_plugin_test.cpp index 0021a291f..70a5ff5ec 100644 --- a/test/unittest/edm_plugin/device_info_plugin_test.cpp +++ b/test/unittest/edm_plugin/device_info_plugin_test.cpp @@ -22,14 +22,16 @@ using namespace testing::ext; namespace OHOS { namespace EDM { namespace TEST { -void DeviceInfoPluginTest::SetUpTestCase(void) +void DeviceInfoPluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void DeviceInfoPluginTest::TearDownTestCase(void) +void DeviceInfoPluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/device_info_plugin_test.h b/test/unittest/edm_plugin/device_info_plugin_test.h index 2aa367f5c..c32a47595 100644 --- a/test/unittest/edm_plugin/device_info_plugin_test.h +++ b/test/unittest/edm_plugin/device_info_plugin_test.h @@ -27,9 +27,9 @@ namespace EDM { namespace TEST { class DeviceInfoPluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); std::shared_ptr plugin_; }; diff --git a/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.cpp b/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.cpp index b5c933ef9..b1733f7af 100644 --- a/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.cpp +++ b/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.cpp @@ -22,14 +22,16 @@ using namespace testing::ext; namespace OHOS { namespace EDM { namespace TEST { -void DisallowAddLocalAccountPluginTest::SetUpTestCase(void) +void DisallowAddLocalAccountPluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void DisallowAddLocalAccountPluginTest::TearDownTestCase(void) +void DisallowAddLocalAccountPluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.h b/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.h index d53040cb3..fbe31e3a9 100644 --- a/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.h +++ b/test/unittest/edm_plugin/disallow_add_local_account_plugin_test.h @@ -23,9 +23,9 @@ namespace EDM { namespace TEST { class DisallowAddLocalAccountPluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; } // namespace TEST } // namespace EDM diff --git a/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.cpp b/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.cpp index 27f23dd93..fba6495c5 100644 --- a/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.cpp +++ b/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.cpp @@ -24,14 +24,16 @@ using namespace testing::ext; namespace OHOS { namespace EDM { namespace TEST { -void DisallowModifyDateTimePluginTest::SetUpTestCase(void) +void DisallowModifyDateTimePluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void DisallowModifyDateTimePluginTest::TearDownTestCase(void) +void DisallowModifyDateTimePluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.h b/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.h index 5482cf6e5..b3878cb94 100644 --- a/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.h +++ b/test/unittest/edm_plugin/disallow_modify_datetime_plugin_test.h @@ -23,9 +23,9 @@ namespace EDM { namespace TEST { class DisallowModifyDateTimePluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; } // namespace TEST } // namespace EDM diff --git a/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.cpp b/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.cpp index c2caf75a3..ce204f0fc 100644 --- a/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.cpp +++ b/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.cpp @@ -29,14 +29,16 @@ namespace EDM { namespace TEST { constexpr int32_t OVER_MAX_SIZE = 201; -void DisallowedInstallBundlesPluginTest::SetUpTestCase(void) +void DisallowedInstallBundlesPluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void DisallowedInstallBundlesPluginTest::TearDownTestCase(void) +void DisallowedInstallBundlesPluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.h b/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.h index a9476dd86..153f922aa 100644 --- a/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.h +++ b/test/unittest/edm_plugin/disallowed_install_bundles_plugin_test.h @@ -24,9 +24,9 @@ namespace EDM { namespace TEST { class DisallowedInstallBundlesPluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; } // namespace TEST } // namespace EDM diff --git a/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.cpp b/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.cpp index b6ca59313..856d489ed 100644 --- a/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.cpp +++ b/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.cpp @@ -29,14 +29,16 @@ namespace EDM { namespace TEST { constexpr int OVER_MAX_SIZE = 201; -void DisallowedRunningBundlesPluginTest::SetUpTestCase(void) +void DisallowedRunningBundlesPluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void DisallowedRunningBundlesPluginTest::TearDownTestCase(void) +void DisallowedRunningBundlesPluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.h b/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.h index 6663237e1..e81f4e8a5 100644 --- a/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.h +++ b/test/unittest/edm_plugin/disallowed_running_bundles_plugin_test.h @@ -24,9 +24,9 @@ namespace EDM { namespace TEST { class DisallowedRunningBundlesPluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; } // namespace TEST } // namespace EDM diff --git a/test/unittest/edm_plugin/get_device_name_plugin_test.cpp b/test/unittest/edm_plugin/get_device_name_plugin_test.cpp index 2e45f4a6d..7ee067cca 100644 --- a/test/unittest/edm_plugin/get_device_name_plugin_test.cpp +++ b/test/unittest/edm_plugin/get_device_name_plugin_test.cpp @@ -28,19 +28,21 @@ namespace EDM { namespace TEST { class GetDeviceNamePluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; -void GetDeviceNamePluginTest::SetUpTestCase(void) +void GetDeviceNamePluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void GetDeviceNamePluginTest::TearDownTestCase(void) +void GetDeviceNamePluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/is_wifi_active_plugin_test.cpp b/test/unittest/edm_plugin/is_wifi_active_plugin_test.cpp index a7e8b827c..3ee5a1ee3 100644 --- a/test/unittest/edm_plugin/is_wifi_active_plugin_test.cpp +++ b/test/unittest/edm_plugin/is_wifi_active_plugin_test.cpp @@ -27,19 +27,21 @@ namespace EDM { namespace TEST { class IsWifiActivePluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; -void IsWifiActivePluginTest::SetUpTestCase(void) +void IsWifiActivePluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void IsWifiActivePluginTest::TearDownTestCase(void) +void IsWifiActivePluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/network_manager_plugin_test.cpp b/test/unittest/edm_plugin/network_manager_plugin_test.cpp index def2acf97..a5b442fc9 100644 --- a/test/unittest/edm_plugin/network_manager_plugin_test.cpp +++ b/test/unittest/edm_plugin/network_manager_plugin_test.cpp @@ -35,19 +35,21 @@ const std::string INVALID_NETWORK_INTERFACE = "fail"; class NetworkManagerPluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; -void NetworkManagerPluginTest::SetUpTestCase(void) +void NetworkManagerPluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void NetworkManagerPluginTest::TearDownTestCase(void) +void NetworkManagerPluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/reset_factory_plugin_test.cpp b/test/unittest/edm_plugin/reset_factory_plugin_test.cpp index 3dc9a0652..f610b7895 100644 --- a/test/unittest/edm_plugin/reset_factory_plugin_test.cpp +++ b/test/unittest/edm_plugin/reset_factory_plugin_test.cpp @@ -23,14 +23,16 @@ using namespace testing::ext; namespace OHOS { namespace EDM { namespace TEST { -void DeviceControlPluginTest::SetUpTestCase(void) +void DeviceControlPluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void DeviceControlPluginTest::TearDownTestCase(void) +void DeviceControlPluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/edm_plugin/reset_factory_plugin_test.h b/test/unittest/edm_plugin/reset_factory_plugin_test.h index 551aed241..af08a7063 100644 --- a/test/unittest/edm_plugin/reset_factory_plugin_test.h +++ b/test/unittest/edm_plugin/reset_factory_plugin_test.h @@ -25,9 +25,9 @@ namespace EDM { namespace TEST { class DeviceControlPluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; } // namespace TEST } // namespace EDM diff --git a/test/unittest/edm_plugin/set_wifi_profile_plugin_test.cpp b/test/unittest/edm_plugin/set_wifi_profile_plugin_test.cpp index af5797fa0..2224bef5d 100644 --- a/test/unittest/edm_plugin/set_wifi_profile_plugin_test.cpp +++ b/test/unittest/edm_plugin/set_wifi_profile_plugin_test.cpp @@ -27,19 +27,21 @@ namespace EDM { namespace TEST { class SetWifiProfilePluginTest : public testing::Test { protected: - static void SetUpTestCase(void); + static void SetUpTestSuite(void); - static void TearDownTestCase(void); + static void TearDownTestSuite(void); }; -void SetWifiProfilePluginTest::SetUpTestCase(void) +void SetWifiProfilePluginTest::SetUpTestSuite(void) { Utils::SetEdmInitialEnv(); } -void SetWifiProfilePluginTest::TearDownTestCase(void) +void SetWifiProfilePluginTest::TearDownTestSuite(void) { Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; } /** diff --git a/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp b/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp index 0893256ab..7b1cd69b9 100644 --- a/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp +++ b/test/unittest/enterprise_device_mgr_ability/enterprise_device_mgr_ability_test/enterprise_device_mgr_ability_test.cpp @@ -62,6 +62,8 @@ protected: // Tears down the test fixture. void TearDown() override; + + static void TearDownTestSuite(void); sptr edmMgr_; std::shared_ptr edmSysManager_ = nullptr; int32_t TestDump(); @@ -104,6 +106,12 @@ void EnterpriseDeviceMgrAbilityTest::TearDown() edmSysManager_->UnregisterSystemAbilityOfRemoteObject(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); } +void EnterpriseDeviceMgrAbilityTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout<< "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + /** * @tc.name: TestGetAllPermissionsByAdminFail * @tc.desc: Test GetAllPermissionsByAdminFail func. @@ -455,6 +463,7 @@ HWTEST_F(EnterpriseDeviceMgrAbilityTest, TestDisableSuperAdminIpcFail, TestSize. // the same edmMgr_->adminMgr_ two super admin res = edmMgr_->adminMgr_->DeleteAdmin(bundleName, DEFAULT_USER_ID); + Utils::SetEdmServiceDisable(); EXPECT_TRUE(res == ERR_OK); } @@ -570,6 +579,7 @@ HWTEST_F(EnterpriseDeviceMgrAbilityTest, TestSetEnterpriseInfoNoSame, TestSize.L // the same edmMgr_->adminMgr_ two super admin res = edmMgr_->adminMgr_->DeleteAdmin(bundleName, DEFAULT_USER_ID); + Utils::SetEdmServiceDisable(); EXPECT_TRUE(res == ERR_OK); } @@ -837,6 +847,8 @@ HWTEST_F(EnterpriseDeviceMgrAbilityTest, TestSubscribeManagedEventIpcFail, TestS EXPECT_TRUE(res == ERR_OK); res = edmMgr_->SubscribeManagedEvent(admin, event); EXPECT_TRUE(res == EdmReturnErrCode::PERMISSION_DENIED); + res = edmMgr_->DisableAdmin(admin, DEFAULT_USER_ID); + EXPECT_TRUE(res == ERR_OK); Utils::ResetTokenTypeAndUid(); } diff --git a/test/unittest/enterprise_device_mgr_proxy/BUILD.gn b/test/unittest/enterprise_device_mgr_proxy/BUILD.gn index 1a7d911cc..e783e5caa 100644 --- a/test/unittest/enterprise_device_mgr_proxy/BUILD.gn +++ b/test/unittest/enterprise_device_mgr_proxy/BUILD.gn @@ -57,6 +57,7 @@ ohos_unittest("EdmDeviceMgrProxyUnitTest") { "bundle_framework:appexecfwk_core", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "init:libbegetutil", "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", diff --git a/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp b/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp index 0eb8f87f3..4a9284a12 100644 --- a/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp +++ b/test/unittest/enterprise_device_mgr_proxy/enterprise_device_mgr_proxy_test/enterprise_device_mgr_proxy_test.cpp @@ -43,6 +43,7 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr enterpriseDeviceMgrProxyTest = nullptr; std::shared_ptr edmSysManager_ = nullptr; sptr object_ = nullptr; @@ -54,6 +55,7 @@ void EnterpriseDeviceMgrProxyTest::SetUp() edmSysManager_ = std::make_shared(); object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock(); edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_); + Utils::SetEdmServiceEnable(); } void EnterpriseDeviceMgrProxyTest::TearDown() @@ -61,6 +63,13 @@ void EnterpriseDeviceMgrProxyTest::TearDown() EnterpriseDeviceMgrProxy::DestroyInstance(); edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID); object_ = nullptr; + Utils::SetEdmServiceDisable(); +} + +void EnterpriseDeviceMgrProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; } /** diff --git a/test/unittest/include/utils.h b/test/unittest/include/utils.h index 2f010de3e..c6c601d72 100644 --- a/test/unittest/include/utils.h +++ b/test/unittest/include/utils.h @@ -33,6 +33,8 @@ public: static void SetNativeTokenTypeAndPermissions(const char* permissions[], int size); static void SetEdmInitialEnv(); static void ResetTokenTypeAndUid(); + static bool GetEdmServiceState(); + static bool IsOriginalUTEnv(); static void SetEdmServiceEnable(); static void SetEdmServiceDisable(); diff --git a/test/unittest/network_manager_proxy/network_manager_proxy_test/network_manager_proxy_test.cpp b/test/unittest/network_manager_proxy/network_manager_proxy_test/network_manager_proxy_test.cpp index 7020eee68..aee301d86 100644 --- a/test/unittest/network_manager_proxy/network_manager_proxy_test/network_manager_proxy_test.cpp +++ b/test/unittest/network_manager_proxy/network_manager_proxy_test/network_manager_proxy_test.cpp @@ -37,6 +37,7 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr networkManagerProxy = nullptr; std::shared_ptr edmSysManager_ = nullptr; sptr object_ = nullptr; @@ -59,6 +60,12 @@ void NetworkManagerProxyTest::TearDown() Utils::SetEdmServiceDisable(); } +void NetworkManagerProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + /** * @tc.name: TestGetAllNetworkInterfacesSuc * @tc.desc: Test GetAllNetworkInterfaces func. @@ -85,11 +92,12 @@ HWTEST_F(NetworkManagerProxyTest, TestGetAllNetworkInterfacesSuc, TestSize.Level */ HWTEST_F(NetworkManagerProxyTest, TestGetAllNetworkInterfacesFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); std::vector networkInterfaces; int32_t ret = networkManagerProxy->GetAllNetworkInterfaces(admin, networkInterfaces); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } /** @@ -117,11 +125,12 @@ HWTEST_F(NetworkManagerProxyTest, TestGetIpOrMacAddressSuc, TestSize.Level1) */ HWTEST_F(NetworkManagerProxyTest, TestGetIpOrMacAddressFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); std::string info; int32_t ret = networkManagerProxy->GetIpOrMacAddress(admin, "eth0", GET_MAC, info); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } /** @@ -149,11 +158,12 @@ HWTEST_F(NetworkManagerProxyTest, TestIsNetworkInterfaceDisabledSuc, TestSize.Le */ HWTEST_F(NetworkManagerProxyTest, TestIsNetworkInterfaceDisabledFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); bool status = false; int32_t ret = networkManagerProxy->IsNetworkInterfaceDisabled(admin, "eth0", status); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); ASSERT_FALSE(status); } @@ -180,10 +190,11 @@ HWTEST_F(NetworkManagerProxyTest, TestSetNetworkInterfaceDisabledSuc, TestSize.L */ HWTEST_F(NetworkManagerProxyTest, TestSetNetworkInterfaceDisabledFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); int32_t ret = networkManagerProxy->SetNetworkInterfaceDisabled(admin, "eth0", true); - ASSERT_TRUE(ret == ERR_INVALID_VALUE); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } } // namespace TEST } // namespace EDM diff --git a/test/unittest/src/admin_manager_test.cpp b/test/unittest/src/admin_manager_test.cpp index 09b0bccb8..a19f46217 100644 --- a/test/unittest/src/admin_manager_test.cpp +++ b/test/unittest/src/admin_manager_test.cpp @@ -84,44 +84,35 @@ HWTEST_F(AdminManagerTest, TestGetReqPermission, TestSize.Level1) */ HWTEST_F(AdminManagerTest, TestGetGrantedPermission, TestSize.Level1) { - ErrCode res; std::vector permissions; - AppExecFwk::ExtensionAbilityInfo abilityInfo; - abilityInfo.bundleName = "com.edm.test.demo"; - abilityInfo.name = "testDemo"; - EntInfo entInfo; - entInfo.enterpriseName = "company"; - entInfo.description = "technology company in china"; - - permissions = {}; - res = adminMgr_->GetGrantedPermission(abilityInfo, permissions, AdminType::NORMAL); + ErrCode res = adminMgr_->GetGrantedPermission(permissions, AdminType::NORMAL); ASSERT_TRUE(res == ERR_OK); permissions = { "ohos.permission.EDM_TEST_PERMISSION_FAIL", "ohos.permission.EDM_TEST_PERMISSION" }; - res = adminMgr_->GetGrantedPermission(abilityInfo, permissions, AdminType::NORMAL); + res = adminMgr_->GetGrantedPermission(permissions, AdminType::NORMAL); ASSERT_TRUE(res == ERR_OK); ASSERT_TRUE(permissions.size() == 1); permissions = { "ohos.permission.EDM_TEST_PERMISSION_FAIL", "ohos.permission.EDM_TEST_PERMISSION" }; - res = adminMgr_->GetGrantedPermission(abilityInfo, permissions, AdminType::ENT); + res = adminMgr_->GetGrantedPermission(permissions, AdminType::ENT); ASSERT_TRUE(res == ERR_OK); ASSERT_TRUE(permissions.size() == 1); permissions = { "ohos.permission.EDM_TEST_ENT_PERMISSION", "ohos.permission.EDM_TEST_PERMISSION" }; - res = adminMgr_->GetGrantedPermission(abilityInfo, permissions, AdminType::NORMAL); + res = adminMgr_->GetGrantedPermission(permissions, AdminType::NORMAL); ASSERT_TRUE(res == ERR_OK); ASSERT_TRUE(permissions.size() == 1); permissions = { "ohos.permission.EDM_TEST_ENT_PERMISSION", "ohos.permission.EDM_TEST_PERMISSION" }; - res = adminMgr_->GetGrantedPermission(abilityInfo, permissions, AdminType::ENT); + res = adminMgr_->GetGrantedPermission(permissions, AdminType::ENT); ASSERT_TRUE(res == ERR_OK); ASSERT_TRUE(permissions.size() == 2); } @@ -351,7 +342,7 @@ HWTEST_F(AdminManagerTest, TestUpdateAdmin, TestSize.Level1) std::vector permissions = { "ohos.permission.EDM_TEST_PERMISSION_FAIL", "ohos.permission.EDM_TEST_ENT_PERMISSION" }; - res = adminMgr_->GetGrantedPermission(abilityInfo, permissions, AdminType::ENT); + res = adminMgr_->GetGrantedPermission(permissions, AdminType::ENT); ASSERT_TRUE(res == ERR_OK); adminMgr_->SetAdminValue(abilityInfo, entInfo, AdminType::ENT, permissions, DEFAULT_USER_ID); std::vector> userAdmin; diff --git a/test/unittest/src/edm_data_ability_utils_test.cpp b/test/unittest/src/edm_data_ability_utils_test.cpp index f035a64b0..034ad1c96 100644 --- a/test/unittest/src/edm_data_ability_utils_test.cpp +++ b/test/unittest/src/edm_data_ability_utils_test.cpp @@ -35,16 +35,26 @@ protected: void SetUp() override; void TearDown() override; + + static void TearDownTestSuite(void); }; void EdmDataAbilityUtilsTest::SetUp() { Utils::SetEdmInitialEnv(); + Utils::SetEdmServiceEnable(); } void EdmDataAbilityUtilsTest::TearDown() { Utils::ResetTokenTypeAndUid(); + Utils::SetEdmServiceDisable(); +} + +void EdmDataAbilityUtilsTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; } /** diff --git a/test/unittest/src/edm_sys_manager_test.cpp b/test/unittest/src/edm_sys_manager_test.cpp index 817cb0cf4..de8792c1d 100644 --- a/test/unittest/src/edm_sys_manager_test.cpp +++ b/test/unittest/src/edm_sys_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -16,6 +16,7 @@ #include #include "edm_sys_manager.h" #include "system_ability_definition.h" +#include "utils.h" using namespace testing::ext; @@ -24,11 +25,29 @@ namespace EDM { namespace TEST { class EdmSysManagerTest : public testing::Test { protected: - void SetUp() override {} + void SetUp() override; - void TearDown() override {} + void TearDown() override; + + static void TearDownTestSuite(void); }; +void EdmSysManagerTest::SetUp() +{ + Utils::SetEdmServiceEnable(); +} + +void EdmSysManagerTest::TearDown() +{ + Utils::SetEdmServiceDisable(); +} + +void EdmSysManagerTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + /** * @tc.name: TestGetRemoteObjectOfSystemAbility * @tc.desc: Test GetRemoteObjectOfSystemAbility function. diff --git a/test/unittest/src/utils.cpp b/test/unittest/src/utils.cpp index cfc5135c0..ef3d8c3de 100644 --- a/test/unittest/src/utils.cpp +++ b/test/unittest/src/utils.cpp @@ -71,6 +71,17 @@ void Utils::ResetTokenTypeAndUid() SetSelfTokenID(selfTokenId_); } +bool Utils::IsOriginalUTEnv() +{ + return Utils::ROOT_UID == geteuid() && selfTokenId_ == GetSelfTokenID(); +} + +bool Utils::GetEdmServiceState() +{ + std::string edmParaValue = system::GetParameter("persist.edm.edm_enable", "false"); + return edmParaValue == "true"; +} + void Utils::SetEdmServiceEnable() { system::SetParameter(SET_EDM_SERVICE, "true"); diff --git a/test/unittest/wifi_manager_proxy/wifi_manager_proxy_test/wifi_manager_proxy_test.cpp b/test/unittest/wifi_manager_proxy/wifi_manager_proxy_test/wifi_manager_proxy_test.cpp index d2bcf84e6..920d0468d 100644 --- a/test/unittest/wifi_manager_proxy/wifi_manager_proxy_test/wifi_manager_proxy_test.cpp +++ b/test/unittest/wifi_manager_proxy/wifi_manager_proxy_test/wifi_manager_proxy_test.cpp @@ -36,6 +36,7 @@ protected: void TearDown() override; + static void TearDownTestSuite(void); std::shared_ptr wifiManagerProxy = nullptr; std::shared_ptr edmSysManager_ = nullptr; sptr object_ = nullptr; @@ -58,6 +59,12 @@ void WifiManagerProxyTest::TearDown() Utils::SetEdmServiceDisable(); } +void WifiManagerProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); + std::cout << "EdmServiceState : " << Utils::GetEdmServiceState() << std::endl; +} + /** * @tc.name: TestIsWifiActiveSuc * @tc.desc: Test IsWifiActive func. @@ -78,32 +85,52 @@ HWTEST_F(WifiManagerProxyTest, TestIsWifiActiveSuc, TestSize.Level1) /** * @tc.name: TestIsWifiActiveFail - * @tc.desc: Test IsWifiActive func. + * @tc.desc: Test IsWifiActive func without enable edm service. * @tc.type: FUNC */ HWTEST_F(WifiManagerProxyTest, TestIsWifiActiveFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); bool isActive = false; int32_t ret = wifiManagerProxy->IsWifiActive(admin, isActive); - ASSERT_TRUE(ret != ERR_OK); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); ASSERT_FALSE(isActive); } +/** + * @tc.name: TestSetWifiProfileSuc + * @tc.desc: Test SetWifiProfile success func. + * @tc.type: FUNC + */ +HWTEST_F(WifiManagerProxyTest, TestSetWifiProfileSuc, TestSize.Level1) +{ + AppExecFwk::ElementName admin; + admin.SetBundleName(ADMIN_PACKAGENAME); + Wifi::WifiDeviceConfig config; + config.wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv6 = { 0x01 }; + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + int32_t ret = wifiManagerProxy->SetWifiProfile(admin, config); + ASSERT_TRUE(ret == ERR_OK); +} + /** * @tc.name: TestSetWifiProfileFail - * @tc.desc: Test SetWifiProfile func. + * @tc.desc: Test SetWifiProfile func without enable edm service. * @tc.type: FUNC */ HWTEST_F(WifiManagerProxyTest, TestSetWifiProfileFail, TestSize.Level1) { + Utils::SetEdmServiceDisable(); AppExecFwk::ElementName admin; admin.SetBundleName(ADMIN_PACKAGENAME); Wifi::WifiDeviceConfig config; config.wifiIpConfig.staticIpAddress.ipAddress.address.addressIpv6 = { 0x01 }; int32_t ret = wifiManagerProxy->SetWifiProfile(admin, config); - ASSERT_TRUE(ret == ERR_INVALID_VALUE); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); } } // namespace TEST } // namespace EDM -- Gitee