diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index e75363defb6af89b182684670ecd7a782755b5ef..3342e5478916e2a00348cd67b13cb5bb811cd4fb 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -61,6 +61,9 @@ ohos_unittest("EdmServicesUnitTest") { external_deps = [ "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "c_utils:utils", diff --git a/test/unittest/include/enterprise_device_mgr_ability_test.h b/test/unittest/include/enterprise_device_mgr_ability_test.h new file mode 100644 index 0000000000000000000000000000000000000000..de096a2917e69a14706abaa250851373dc740457 --- /dev/null +++ b/test/unittest/include/enterprise_device_mgr_ability_test.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef EDM_UNIT_TEST_ENTERPRISE_DEVICE_MGR_ABILITY_TEST_H +#define EDM_UNIT_TEST_ENTERPRISE_DEVICE_MGR_ABILITY_TEST_H + +#include +#include "enterprise_device_mgr_ability.h" + +namespace OHOS { +namespace EDM { +namespace TEST { +class EnterpriseDeviceMgrAbilityTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void PrepareBeforeHandleDevicePolicy(); + int HandleDevicePolicyFuncTest001(void); + int HandleDevicePolicyFuncTest002(void); + int HandleDevicePolicyFuncTest003(void); + int HandleDevicePolicyFuncTest004(void); + int HandleDevicePolicyFuncTest005(void); + int HandleDevicePolicyFuncTest006(void); + int HandleDevicePolicyFuncTest007(void); +protected: + virtual void SetUp(); + virtual void TearDown(); + sptr edmMgr_; + std::shared_ptr plugin_; +}; +} // namespace TEST +} // namespace EDM +} // namespace OHOS +#endif // EDM_UNIT_TEST_ENTERPRISE_DEVICE_MGR_ABILITY_TEST_H \ No newline at end of file diff --git a/test/unittest/include/iplugin_template_test.h b/test/unittest/include/iplugin_template_test.h index 1adba2e92b64e9a609caca69253b6166913644d6..b1356913fe3289ca39fabcc5b41069475f63c274 100644 --- a/test/unittest/include/iplugin_template_test.h +++ b/test/unittest/include/iplugin_template_test.h @@ -36,7 +36,6 @@ namespace OHOS { namespace EDM { namespace TEST { -bool g_visit = false; namespace PLUGIN { #ifndef ARRAY_TEST_PLUGIN #define ARRAY_TEST_PLUGIN @@ -194,6 +193,10 @@ class HandlePolicyBiFunctionPlg : public PluginSingleton #include "admin_manager.h" @@ -43,7 +44,7 @@ void AdminManagerTest::TearDown() adminMgr_->DeleteAdmin(admin->adminInfo_.packageName_, DEFAULT_USER_ID); } adminMgr_.reset(); - adminMgr_ = nullptr; + adminMgr_->instance_.reset(); PermissionManager::DestroyInstance(); CmdUtils::ExecCmdSync(TEAR_DOWN_CMD); diff --git a/test/unittest/src/enterprise_device_mgr_ability_test.cpp b/test/unittest/src/enterprise_device_mgr_ability_test.cpp index 476a5be9642558e6b32b93aeea17488a19e91726..d27d2ce74c497d0f783fb2c9e72417aa0dbded6e 100644 --- a/test/unittest/src/enterprise_device_mgr_ability_test.cpp +++ b/test/unittest/src/enterprise_device_mgr_ability_test.cpp @@ -14,12 +14,20 @@ */ #define private public +#define protected public #include #include #include +#include "accesstoken_kit.h" +#include "cmd_utils.h" +#include "enterprise_device_mgr_ability_test.h" #include "enterprise_device_mgr_ability.h" +#include "iplugin_template.h" +#include "iplugin_template_test.h" +#include "nativetoken_kit.h" #include "plugin_manager_test.h" +#include "token_setproc.h" using namespace testing::ext; @@ -27,66 +35,257 @@ namespace OHOS { namespace EDM { namespace TEST { constexpr int32_t DEFAULT_USER_ID = 100; +constexpr int32_t ARRAY_MAP_TESTPLUGIN_POLICYCODE = 13; +constexpr int32_t HANDLE_POLICY_BIFUNCTIONPLG_POLICYCODE = 23; +constexpr int32_t INVALID_POLICYCODE = 123456; +const std::string ADMIN_PACKAGENAME = "com.edm.test.demo"; +const std::string EDM_MANAGE_DATETIME_PERMISSION = "ohos.permission.EDM_MANAGE_DATETIME"; +const std::string EDM_TEST_PERMISSION = "ohos.permission.EDM_TEST_PERMISSION"; +const std::string HANDLE_POLICY_BIFUNCTIONPLG_POLICYNAME = "HandlePolicyBiFunctionPlg"; +const std::string TEAR_DOWN_CMD = "rm /data/service/el1/public/edm/device_policies.json"; -class EnterpriseDeviceMgrAbilityTest : public testing::Test { -protected: - // Sets up the test fixture. - void SetUp() override; +void NativeTokenGet() +{ + uint64_t tokenId; + const char **perms = new const char *[1]; + perms[0] = EDM_MANAGE_DATETIME_PERMISSION.c_str(); + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_basic", + }; + + infoInstance.processName = "EdmServicesUnitTest"; + tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; +} - // Tears down the test fixture. - void TearDown() override; - sptr edmMgr_; -}; +void EnterpriseDeviceMgrAbilityTest::SetUpTestCase() {NativeTokenGet();} + +void EnterpriseDeviceMgrAbilityTest::TearDownTestCase() {} void EnterpriseDeviceMgrAbilityTest::SetUp() { + plugin_ = PLUGIN::ArrayMapTestPlugin::GetPlugin(); edmMgr_ = EnterpriseDeviceMgrAbility::GetInstance(); + edmMgr_->adminMgr_ = AdminManager::GetInstance(); + edmMgr_->pluginMgr_ = PluginManager::GetInstance(); + edmMgr_->policyMgr_ = PolicyManager::GetInstance(); } void EnterpriseDeviceMgrAbilityTest::TearDown() { - edmMgr_ = nullptr; + edmMgr_->policyMgr_->SetPolicy(ADMIN_PACKAGENAME, HANDLE_POLICY_BIFUNCTIONPLG_POLICYNAME, "", ""); + edmMgr_->adminMgr_->instance_.reset(); + edmMgr_->pluginMgr_->instance_.reset(); + edmMgr_->policyMgr_->instance_.reset(); + edmMgr_.clear(); + edmMgr_->instance_.clear(); + CmdUtils::ExecCmdSync(TEAR_DOWN_CMD); +} + +void EnterpriseDeviceMgrAbilityTest::PrepareBeforeHandleDevicePolicy() +{ + //Give testAdmin and plugin_ Initial value + Admin testAdmin; + testAdmin.adminInfo_.packageName_{ADMIN_PACKAGENAME}; + testAdmin.adminInfo_.permission_ = {EDM_MANAGE_DATETIME_PERMISSION}; + std::shared_ptr adminPtr = std::make_shared(testAdmin); + std::vector> adminVec = {adminPtr}; + edmMgr_->adminMgr_->admins_. + insert(std::pair>>(DEFAULT_USER_ID, adminVec)); + plugin_->permission_ = EDM_MANAGE_DATETIME_PERMISSION; + edmMgr_->pluginMgr_->AddPlugin(plugin_); } /** - * @tc.name: TestHandleDevicePolicy - * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function. + * @tc.name: HandleDevicePolicyFuncTest001 + * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function.(return ERR_OK) * @tc.type: FUNC * @tc.require: issueI5PBT1 */ -HWTEST_F(EnterpriseDeviceMgrAbilityTest, TestHandleDevicePolicy, TestSize.Level1) +HWTEST_F(EnterpriseDeviceMgrAbilityTest, HandleDevicePolicyFuncTest001, TestSize.Level1) { - ErrCode res; + //Test whether HandleDevicePolicy runs to the end + PrepareBeforeHandleDevicePolicy(); + uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, ARRAY_MAP_TESTPLUGIN_POLICYCODE); AppExecFwk::ElementName elementName; - elementName.SetBundleName("com.edm.test.demo"); + elementName.SetBundleName(ADMIN_PACKAGENAME); MessageParcel data; - uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, 0); + ErrCode res = edmMgr_->HandleDevicePolicy(code, elementName, data); + ASSERT_TRUE(res == ERR_OK); +} +/** + * @tc.name: HandleDevicePolicyFuncTest002 + * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function.(if (deviceAdmin == nullptr)) + * @tc.type: FUNC + * @tc.require: issueI5PBT1 + */ +HWTEST_F(EnterpriseDeviceMgrAbilityTest, HandleDevicePolicyFuncTest002, TestSize.Level1) +{ + //Test if deviceAdmin is empty + PrepareBeforeHandleDevicePolicy(); + uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, ARRAY_MAP_TESTPLUGIN_POLICYCODE); + AppExecFwk::ElementName elementName; + elementName.SetBundleName("com.edm.test.demoFail"); + MessageParcel data; + ErrCode res = edmMgr_->HandleDevicePolicy(code, elementName, data); + ASSERT_TRUE(res == ERR_EDM_GET_ADMIN_MGR_FAILED); +} - edmMgr_->adminMgr_ = AdminManager::GetInstance(); +/** + * @tc.name: HandleDevicePolicyFuncTest003 + * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function.(if (plugin == nullptr)) + * @tc.type: FUNC + * @tc.require: issueI5PBT1 + */ +HWTEST_F(EnterpriseDeviceMgrAbilityTest, HandleDevicePolicyFuncTest003, TestSize.Level1) +{ + //Test if plugin is empty + PrepareBeforeHandleDevicePolicy(); + uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, INVALID_POLICYCODE); + AppExecFwk::ElementName elementName; + elementName.SetBundleName(ADMIN_PACKAGENAME); + MessageParcel data; + ErrCode res = edmMgr_->HandleDevicePolicy(code, elementName, data); + ASSERT_TRUE(res == ERR_EDM_GET_PLUGIN_MGR_FAILED); +} + +/** + * @tc.name: HandleDevicePolicyFuncTest004 + * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function. + * @tc.desc: if (!deviceAdmin->CheckPermission(plugin->GetPermission()) + * @tc.desc: || !VerifyCallingPermission(plugin->GetPermission())) + * @tc.type: FUNC + * @tc.require: issueI5PBT1 + */ +HWTEST_F(EnterpriseDeviceMgrAbilityTest, HandleDevicePolicyFuncTest004, TestSize.Level1) +{ + //Test admin's authentication is fail and accesstoken's authentication is success + PrepareBeforeHandleDevicePolicy(); Admin testAdmin; - testAdmin.adminInfo_.packageName_ = "com.edm.test.demo"; - testAdmin.adminInfo_.permission_ = {"ohos.permission.EDM_TEST_PERMISSION"}; + testAdmin.adminInfo_.packageName_{ADMIN_PACKAGENAME}; std::shared_ptr adminPtr = std::make_shared(testAdmin); std::vector> adminVec; adminVec.push_back(adminPtr); - edmMgr_->adminMgr_->admins_ - .insert(std::pair>>(DEFAULT_USER_ID, adminVec)); + edmMgr_->adminMgr_->admins_.clear(); + edmMgr_->adminMgr_->admins_. + insert(std::pair>>(DEFAULT_USER_ID, adminVec)); + uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, ARRAY_MAP_TESTPLUGIN_POLICYCODE); + AppExecFwk::ElementName elementName; + elementName.SetBundleName(ADMIN_PACKAGENAME); + MessageParcel data; + ErrCode res = edmMgr_->HandleDevicePolicy(code, elementName, data); + ASSERT_TRUE(res == ERR_EDM_PERMISSION_ERROR); - edmMgr_->pluginMgr_ = PluginManager::GetInstance(); - edmMgr_->pluginMgr_->AddPlugin(std::make_shared()); + //Test admin's authentication is fail and accesstoken's authentication is fail + plugin_->permission_ = EDM_TEST_PERMISSION; + edmMgr_->pluginMgr_->pluginsCode_.clear(); + edmMgr_->pluginMgr_->pluginsName_.clear(); + edmMgr_->pluginMgr_->AddPlugin(plugin_); res = edmMgr_->HandleDevicePolicy(code, elementName, data); ASSERT_TRUE(res == ERR_EDM_PERMISSION_ERROR); - testAdmin.adminInfo_.permission_.clear(); + //Test admin's authentication is success and accesstoken's authentication is fail + testAdmin.adminInfo_.permission_ = {EDM_TEST_PERMISSION}; adminPtr = std::make_shared(testAdmin); adminVec.clear(); adminVec.push_back(adminPtr); edmMgr_->adminMgr_->admins_.clear(); - edmMgr_->adminMgr_->admins_ - .insert(std::pair>>(DEFAULT_USER_ID, adminVec)); + edmMgr_->adminMgr_->admins_. + insert(std::pair>>(DEFAULT_USER_ID, adminVec)); res = edmMgr_->HandleDevicePolicy(code, elementName, data); ASSERT_TRUE(res == ERR_EDM_PERMISSION_ERROR); } + +/** + * @tc.name: HandleDevicePolicyFuncTest005 + * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function. + * @tc.desc: if (plugin->OnHandlePolicy(code, data, policyValue, isChanged) != ERR_OK) + * @tc.type: FUNC + * @tc.require: issueI5PBT1 + */ +HWTEST_F(EnterpriseDeviceMgrAbilityTest, HandleDevicePolicyFuncTest005, TestSize.Level1) +{ + //Test the result of plugin->OnHandlePolicy is not OK + PrepareBeforeHandleDevicePolicy(); + plugin_ = PLUGIN::HandlePolicyBiFunctionPlg::GetPlugin(); + plugin_->permission_ = EDM_MANAGE_DATETIME_PERMISSION; + edmMgr_->pluginMgr_->pluginsCode_.clear(); + edmMgr_->pluginMgr_->pluginsName_.clear(); + edmMgr_->pluginMgr_->AddPlugin(plugin_); + uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, HANDLE_POLICY_BIFUNCTIONPLG_POLICYCODE); + AppExecFwk::ElementName elementName; + elementName.SetBundleName(ADMIN_PACKAGENAME); + MessageParcel data; + data.WriteString16(Str8ToStr16("ErrorData")); + ErrCode res = edmMgr_->HandleDevicePolicy(code, elementName, data); + ASSERT_TRUE(res == ERR_EDM_HANDLE_POLICY_FAILED); +} + +/** + * @tc.name: HandleDevicePolicyFuncTest006 + * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function.(if (plugin->NeedSavePolicy() && isChanged)) + * @tc.type: FUNC + * @tc.require: issueI5PBT1 + */ +HWTEST_F(EnterpriseDeviceMgrAbilityTest, HandleDevicePolicyFuncTest006, TestSize.Level1) +{ + //Test run into the branch if (plugin ->NeedSavePolicy() && isChanged) + PrepareBeforeHandleDevicePolicy(); + plugin_ = PLUGIN::HandlePolicyBiFunctionPlg::GetPlugin(); + plugin_->permission_ = EDM_MANAGE_DATETIME_PERMISSION; + edmMgr_->pluginMgr_->pluginsCode_.clear(); + edmMgr_->pluginMgr_->pluginsName_.clear(); + edmMgr_->pluginMgr_->AddPlugin(plugin_); + uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, HANDLE_POLICY_BIFUNCTIONPLG_POLICYCODE); + AppExecFwk::ElementName elementName; + elementName.SetBundleName(ADMIN_PACKAGENAME); + MessageParcel data; + data.WriteString16(Str8ToStr16("testValue")); + ErrCode res = edmMgr_->HandleDevicePolicy(code, elementName, data); + ASSERT_TRUE(res == ERR_OK); +} + +/** + * @tc.name: HandleDevicePolicyFuncTest007 + * @tc.desc: Test EnterpriseDeviceMgrAbility::HandleDevicePolicy function. + * @tc.desc: plugin->MergePolicyData(admin.GetBundleName(), mergedPolicy) != ERR_OK + * @tc.type: FUNC + * @tc.require: issueI5PBT1 + */ +HWTEST_F(EnterpriseDeviceMgrAbilityTest, HandleDevicePolicyFuncTest007, TestSize.Level1) +{ + //Test the MergePolicyData processing result is not OK + PrepareBeforeHandleDevicePolicy(); + plugin_ = PLUGIN::HandlePolicyBiFunctionPlg::GetPlugin(); + plugin_->permission_ = EDM_MANAGE_DATETIME_PERMISSION; + edmMgr_->pluginMgr_->pluginsCode_.clear(); + edmMgr_->pluginMgr_->pluginsName_.clear(); + edmMgr_->pluginMgr_->AddPlugin(plugin_); + + //Enter a string that cannot be parsed by JSON + AdminValueItemsMap map; + std::string errJsonStr = "v1,v2v3??v4"; + map.insert(std::pair(ADMIN_PACKAGENAME, errJsonStr)); + map.insert(std::pair("com.edm.test.demo2", errJsonStr)); + edmMgr_->policyMgr_->policyAdmins_ . + insert(std::pair(HANDLE_POLICY_BIFUNCTIONPLG_POLICYNAME, map)); + + uint32_t code = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, HANDLE_POLICY_BIFUNCTIONPLG_POLICYCODE); + AppExecFwk::ElementName elementName; + elementName.SetBundleName(ADMIN_PACKAGENAME); + MessageParcel data; + data.WriteString16(Str8ToStr16("testValue")); + ErrCode res = edmMgr_->HandleDevicePolicy(code, elementName, data); + ASSERT_TRUE(res == ERR_EDM_HANDLE_POLICY_FAILED); +} } // namespace TEST } // namespace EDM -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/src/iplugin_template_test.cpp b/test/unittest/src/iplugin_template_test.cpp index 7cefacba6dd916dcdc346a9e32946d15302c056a..c17f8b8c44fffa9d41b01e6bb5c2572c378dbb6c 100644 --- a/test/unittest/src/iplugin_template_test.cpp +++ b/test/unittest/src/iplugin_template_test.cpp @@ -20,6 +20,7 @@ using namespace testing::ext; namespace OHOS { namespace EDM { namespace TEST { +static bool g_visit = false; /** * @tc.name: TestTemplate * @tc.desc: Test PluginTemplate init.