From 055fac28b6ac3a3a45671f65d4d002afde5f6f9b Mon Sep 17 00:00:00 2001 From: yihengfeng Date: Mon, 16 Oct 2023 17:51:09 +0800 Subject: [PATCH] NTPServer Signed-off-by: yihengfeng --- bundle.json | 2 + etc/init/edm.cfg | 2 +- interfaces/inner_api/BUILD.gn | 2 + .../common/include/edm_ipc_interface_code.h | 1 + .../include/system_manager_proxy.h | 35 +++++ .../src/system_manager_proxy.cpp | 76 ++++++++++ interfaces/kits/system_manager/BUILD.gn | 49 +++++++ .../include/system_manager_addon.h | 42 ++++++ .../src/system_manager_addon.cpp | 102 ++++++++++++++ services/edm_plugin/BUILD.gn | 11 ++ .../edm_plugin/include/ntp_server_plugin.h | 35 +++++ services/edm_plugin/src/ntp_server_plugin.cpp | 53 +++++++ test/unittest/BUILD.gn | 1 + test/unittest/edm_plugin/BUILD.gn | 2 + .../edm_plugin/src/ntp_server_plugin_test.cpp | 72 ++++++++++ test/unittest/system_manager_proxy/BUILD.gn | 74 ++++++++++ .../system_manager_proxy_test.cpp | 132 ++++++++++++++++++ 17 files changed, 690 insertions(+), 1 deletion(-) create mode 100644 interfaces/inner_api/system_manager/include/system_manager_proxy.h create mode 100644 interfaces/inner_api/system_manager/src/system_manager_proxy.cpp create mode 100644 interfaces/kits/system_manager/BUILD.gn create mode 100644 interfaces/kits/system_manager/include/system_manager_addon.h create mode 100644 interfaces/kits/system_manager/src/system_manager_addon.cpp create mode 100644 services/edm_plugin/include/ntp_server_plugin.h create mode 100644 services/edm_plugin/src/ntp_server_plugin.cpp create mode 100644 test/unittest/edm_plugin/src/ntp_server_plugin_test.cpp create mode 100644 test/unittest/system_manager_proxy/BUILD.gn create mode 100644 test/unittest/system_manager_proxy/system_manager_proxy_test.cpp diff --git a/bundle.json b/bundle.json index 9f5819005..5364edc81 100644 --- a/bundle.json +++ b/bundle.json @@ -96,6 +96,7 @@ "//base/customization/enterprise_device_management/interfaces/kits/device_settings:devicesettings", "//base/customization/enterprise_device_management/interfaces/kits/network_manager:networkmanager", "//base/customization/enterprise_device_management/interfaces/kits/restrictions:restrictions", + "//base/customization/enterprise_device_management/interfaces/kits/system_manager:systemmanager", "//base/customization/enterprise_device_management/interfaces/kits/usb_manager:usbmanager", "//base/customization/enterprise_device_management/interfaces/kits/wifi_manager:wifimanager", "//base/customization/enterprise_device_management/framework/extension:enterprise_admin_extension", @@ -117,6 +118,7 @@ "//base/customization/enterprise_device_management/services/edm_plugin:disallow_modify_datetime_plugin", "//base/customization/enterprise_device_management/services/edm_plugin:network_manager_plugin", "//base/customization/enterprise_device_management/services/edm_plugin:restrictions_plugin", + "//base/customization/enterprise_device_management/services/edm_plugin:system_manager_plugin", "//base/customization/enterprise_device_management/services/edm_plugin:usb_manager_plugin", "//base/customization/enterprise_device_management/services/edm_plugin:wifi_manager_plugin", "//base/customization/enterprise_device_management/services/edm_plugin:bundle_manager_plugin", diff --git a/etc/init/edm.cfg b/etc/init/edm.cfg index 239fabfd6..1b301313c 100644 --- a/etc/init/edm.cfg +++ b/etc/init/edm.cfg @@ -12,7 +12,7 @@ "name" : "edm", "path" : ["/system/bin/sa_main", "/system/profile/edm.json"], "uid" : "edm", - "gid" : ["edm", "shell"], + "gid" : ["edm", "shell", "time"], "ondemand" : true, "apl" : "system_core", "permission" : [ diff --git a/interfaces/inner_api/BUILD.gn b/interfaces/inner_api/BUILD.gn index 4125786e1..8ce7abf9b 100644 --- a/interfaces/inner_api/BUILD.gn +++ b/interfaces/inner_api/BUILD.gn @@ -26,6 +26,7 @@ config("edmservice_kits_config") { "device_settings/include", "network_manager/include", "restrictions/include", + "system_manager/include", "usb_manager/include", "wifi_manager/include", ] @@ -49,6 +50,7 @@ ohos_shared_library("edmservice_kits") { "./device_settings/src/device_settings_proxy.cpp", "./network_manager/src/network_manager_proxy.cpp", "./restrictions/src/restrictions_proxy.cpp", + "./system_manager/src/system_manager_proxy.cpp", "./usb_manager/src/usb_manager_proxy.cpp", "./wifi_manager/src/wifi_manager_proxy.cpp", ] diff --git a/interfaces/inner_api/common/include/edm_ipc_interface_code.h b/interfaces/inner_api/common/include/edm_ipc_interface_code.h index 77b073c3d..e7453f5a6 100644 --- a/interfaces/inner_api/common/include/edm_ipc_interface_code.h +++ b/interfaces/inner_api/common/include/edm_ipc_interface_code.h @@ -61,6 +61,7 @@ enum EdmInterfaceCode : uint32_t { USB_READ_ONLY = 1026, INSTALL_CERTIFICATE = 1027, INSTALL = 1028, + NTP_SERVER = 1029, POLICY_CODE_END = 3000, }; } // namespace EDM diff --git a/interfaces/inner_api/system_manager/include/system_manager_proxy.h b/interfaces/inner_api/system_manager/include/system_manager_proxy.h new file mode 100644 index 000000000..27412ac9e --- /dev/null +++ b/interfaces/inner_api/system_manager/include/system_manager_proxy.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 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 + * + * 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 INTERFACES_INNER_API_SYSTEM_MANAGER_INCLUDE_SYSTEM_MANAGER_PROXY_H +#define INTERFACES_INNER_API_SYSTEM_MANAGER_INCLUDE_SYSTEM_MANAGER_PROXY_H +#include "enterprise_device_mgr_proxy.h" + +namespace OHOS { +namespace EDM { +class SystemManagerProxy { +public: + static std::shared_ptr GetSystemManagerProxy(); + int32_t SetNTPServer(const AppExecFwk::ElementName &admin, const std::string &value); + int32_t GetNTPServer(const AppExecFwk::ElementName &admin, std::string &value); + +private: + static std::shared_ptr instance_; + static std::mutex mutexLock_; +}; +} // namespace EDM +} // namespace OHOS + +#endif // INTERFACES_INNER_API_SYSTEM_MANAGER_INCLUDE_SYSTEM_MANAGER_PROXY_H diff --git a/interfaces/inner_api/system_manager/src/system_manager_proxy.cpp b/interfaces/inner_api/system_manager/src/system_manager_proxy.cpp new file mode 100644 index 000000000..0525ef541 --- /dev/null +++ b/interfaces/inner_api/system_manager/src/system_manager_proxy.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "system_manager_proxy.h" + +#include "edm_log.h" +#include "func_code.h" + +namespace OHOS { +namespace EDM { +std::shared_ptr SystemManagerProxy::instance_ = nullptr; +std::mutex SystemManagerProxy::mutexLock_; +const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr"; + +std::shared_ptr SystemManagerProxy::GetSystemManagerProxy() +{ + if (instance_ == nullptr) { + std::lock_guard lock(mutexLock_); + if (instance_ == nullptr) { + std::shared_ptr temp = std::make_shared(); + instance_ = temp; + } + } + return instance_; +} + +int32_t SystemManagerProxy::SetNTPServer(const AppExecFwk::ElementName &admin, const std::string &value) +{ + EDMLOGD("SystemManagerProxy::SetNTPServer"); + MessageParcel data; + data.WriteInterfaceToken(DESCRIPTOR); + data.WriteInt32(WITHOUT_USERID); + data.WriteParcelable(&admin); + data.WriteString(value); + std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::NTP_SERVER); + return EnterpriseDeviceMgrProxy::GetInstance()->HandleDevicePolicy(funcCode, data); +} + +int32_t SystemManagerProxy::GetNTPServer(const AppExecFwk::ElementName &admin, std::string &value) +{ + EDMLOGD("SystemManagerProxy::GetNTPServer"); + auto proxy = EnterpriseDeviceMgrProxy::GetInstance(); + if (proxy == nullptr) { + EDMLOGE("can not get EnterpriseDeviceMgrProxy"); + return EdmReturnErrCode::SYSTEM_ABNORMALLY; + } + MessageParcel data; + MessageParcel reply; + data.WriteInterfaceToken(DESCRIPTOR); + data.WriteInt32(WITHOUT_USERID); + data.WriteInt32(HAS_ADMIN); + data.WriteParcelable(&admin); + proxy->GetPolicy(EdmInterfaceCode::NTP_SERVER, data, reply); + int32_t ret = ERR_INVALID_VALUE; + bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK); + if (!blRes) { + EDMLOGE("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret); + return ret; + } + reply.ReadString(value); + return ERR_OK; +} +} // namespace EDM +} // namespace OHOS diff --git a/interfaces/kits/system_manager/BUILD.gn b/interfaces/kits/system_manager/BUILD.gn new file mode 100644 index 000000000..5c72359a8 --- /dev/null +++ b/interfaces/kits/system_manager/BUILD.gn @@ -0,0 +1,49 @@ +# Copyright (c) 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_shared_library("systemmanager") { + include_dirs = [ + "include", + "../common/include", + ] + + sources = [ + "../common/src/napi_edm_common.cpp", + "../common/src/napi_edm_error.cpp", + "./src/system_manager_addon.cpp", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ "../../inner_api:edmservice_kits" ] + + external_deps = [ + "ability_base:want", + "hilog:libhilog", + "napi:ace_napi", + ] + + relative_install_dir = "module/enterprise" + sanitize = { + boundary_sanitize = true + cfi = true + debug = false + integer_overflow = true + stack_protector_ret = true + ubsan = true + } + subsystem_name = "customization" + part_name = "enterprise_device_management" +} diff --git a/interfaces/kits/system_manager/include/system_manager_addon.h b/interfaces/kits/system_manager/include/system_manager_addon.h new file mode 100644 index 000000000..853f36d4a --- /dev/null +++ b/interfaces/kits/system_manager/include/system_manager_addon.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 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 + * + * 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 INTERFACES_KITS_SYSTEM_MANAGER_INCLUDE_SYSTEM_MANAGER_ADDON_H +#define INTERFACES_KITS_SYSTEM_MANAGER_INCLUDE_SYSTEM_MANAGER_ADDON_H + +#include "system_manager_proxy.h" +#include "edm_errors.h" +#include "napi/native_api.h" +#include "napi/native_common.h" +#include "napi/native_node_api.h" +#include "napi_edm_common.h" +#include "napi_edm_error.h" +#include "want.h" + +namespace OHOS { +namespace EDM { + +class SystemManagerAddon { +public: + static napi_value Init(napi_env env, napi_value exports); + +private: + static napi_value SetNTPServer(napi_env env, napi_callback_info info); + static napi_value GetNTPServer(napi_env env, napi_callback_info info); +}; +} // namespace EDM +} // namespace OHOS + +#endif // INTERFACES_KITS_SYSTEM_MANAGER_INCLUDE_SYSTEM_MANAGER_ADDON_H diff --git a/interfaces/kits/system_manager/src/system_manager_addon.cpp b/interfaces/kits/system_manager/src/system_manager_addon.cpp new file mode 100644 index 000000000..9e520cea9 --- /dev/null +++ b/interfaces/kits/system_manager/src/system_manager_addon.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "system_manager_addon.h" +#include "edm_log.h" + +using namespace OHOS::EDM; + +napi_value SystemManagerAddon::Init(napi_env env, napi_value exports) +{ + napi_property_descriptor property[] = { + DECLARE_NAPI_FUNCTION("setNTPServer", SetNTPServer), + DECLARE_NAPI_FUNCTION("getNTPServer", GetNTPServer), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property)); + return exports; +} + +napi_value SystemManagerAddon::SetNTPServer(napi_env env, napi_callback_info info) +{ + EDMLOGI("NAPI_SetNTPServer called"); + size_t argc = ARGS_SIZE_TWO; + napi_value argv[ARGS_SIZE_TWO] = {nullptr}; + napi_value thisArg = nullptr; + void *data = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data)); + ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "parameter count error"); + ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object), "parameter admin error"); + ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ONE], napi_string), "parameter string error"); + OHOS::AppExecFwk::ElementName elementName; + ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]), + "element name param error"); + EDMLOGD( + "GetNTPServer: elementName.bundlename %{public}s, " + "elementName.abilityname:%{public}s", + elementName.GetBundleName().c_str(), + elementName.GetAbilityName().c_str()); + std::string ntpParm; + ASSERT_AND_THROW_PARAM_ERROR(env, ParseString(env, ntpParm, argv[ARR_INDEX_ONE]), "element name param error"); + int32_t ret = SystemManagerProxy::GetSystemManagerProxy()->SetNTPServer(elementName, ntpParm); + if (FAILED(ret)) { + napi_throw(env, CreateError(env, ret)); + EDMLOGE("SetNTPServer failed!"); + } + return nullptr; +} + +napi_value SystemManagerAddon::GetNTPServer(napi_env env, napi_callback_info info) +{ + EDMLOGI("NAPI_GetNTPServer called"); + size_t argc = ARGS_SIZE_TWO; + napi_value argv[ARGS_SIZE_TWO] = {nullptr}; + napi_value thisArg = nullptr; + void *data = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data)); + ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "parameter count error"); + ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object), "parameter admin error"); + OHOS::AppExecFwk::ElementName elementName; + ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]), + "element name param error"); + EDMLOGD( + "GetNTPServer: elementName.bundlename %{public}s, " + "elementName.abilityname:%{public}s", + elementName.GetBundleName().c_str(), + elementName.GetAbilityName().c_str()); + std::string ntpParm; + int32_t ret = SystemManagerProxy::GetSystemManagerProxy()->GetNTPServer(elementName, ntpParm); + if (FAILED(ret)) { + napi_throw(env, CreateError(env, ret)); + EDMLOGE("GetNTPServer failed!"); + return nullptr; + } + napi_value ntpServerString = nullptr; + napi_create_string_utf8(env, ntpParm.c_str(), ntpParm.size(), &ntpServerString); + return ntpServerString; +} + +static napi_module g_systemManagerModule = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = nullptr, + .nm_register_func = SystemManagerAddon::Init, + .nm_modname = "enterprise.systemManager", + .nm_priv = ((void *)0), + .reserved = { 0 }, +}; + +extern "C" __attribute__((constructor)) void SystemManagerRegister() +{ + napi_module_register(&g_systemManagerModule); +} \ No newline at end of file diff --git a/services/edm_plugin/BUILD.gn b/services/edm_plugin/BUILD.gn index 744620f5c..b9505fbd7 100644 --- a/services/edm_plugin/BUILD.gn +++ b/services/edm_plugin/BUILD.gn @@ -165,6 +165,17 @@ edm_plugin_shared_library("device_settings_plugin") { ] } +edm_plugin_shared_library("system_manager_plugin") { + sources = [ "./src/ntp_server_plugin.cpp" ] + + deps = [ "../../common/native:edm_commom" ] + + external_deps = [ + "c_utils:utils", + "init:libbegetutil", + ] +} + edm_plugin_shared_library("usb_manager_plugin") { sources = [ "./src/usb_read_only_plugin.cpp" ] diff --git a/services/edm_plugin/include/ntp_server_plugin.h b/services/edm_plugin/include/ntp_server_plugin.h new file mode 100644 index 000000000..a0fdac002 --- /dev/null +++ b/services/edm_plugin/include/ntp_server_plugin.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 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 + * + * 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 SERVICES_EDM_PLUGIN_INCLUDE_NTP_SERVER_PLUGIN_H +#define SERVICES_EDM_PLUGIN_INCLUDE_NTP_SERVER_PLUGIN_H + +#include "iplugin_manager.h" +#include "plugin_singleton.h" + +namespace OHOS { +namespace EDM { +class NTPServerPlugin : public PluginSingleton { +public: + void InitPlugin(std::shared_ptr> ptr) override; + + ErrCode OnSetPolicy(std::string &value); + + ErrCode OnGetPolicy(std::string &value, MessageParcel &data, MessageParcel &reply, int32_t userId) override; +}; +} // namespace EDM +} // namespace OHOS + +#endif // SERVICES_EDM_PLUGIN_INCLUDE_NTP_SERVER_PLUGIN_H diff --git a/services/edm_plugin/src/ntp_server_plugin.cpp b/services/edm_plugin/src/ntp_server_plugin.cpp new file mode 100644 index 000000000..1ab5875bb --- /dev/null +++ b/services/edm_plugin/src/ntp_server_plugin.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ntp_server_plugin.h" + +#include "edm_data_ability_utils.h" +#include "edm_ipc_interface_code.h" +#include "string_serializer.h" +#include "parameters.h" + +namespace OHOS { +namespace EDM { +const std::string KEY_NTP_SERVER = "persist.time.ntpserver_specific"; + +const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(NTPServerPlugin::GetPlugin()); + +void NTPServerPlugin::InitPlugin(std::shared_ptr> ptr) +{ + EDMLOGD("NTPServerPlugin InitPlugin..."); + ptr->InitAttribute(EdmInterfaceCode::NTP_SERVER, "ntp_server", + "ohos.permission.ENTERPRISE_MANAGE_SYSTEM", IPlugin::PermissionType::SUPER_DEVICE_ADMIN); + ptr->SetSerializer(StringSerializer::GetInstance()); + ptr->SetOnHandlePolicyListener(&NTPServerPlugin::OnSetPolicy, FuncOperateType::SET); +} + +ErrCode NTPServerPlugin::OnSetPolicy(std::string &value) +{ + EDMLOGI("NTPServerPlugin OnSetPolicy"); + return system::SetParameter(KEY_NTP_SERVER, value) ? ERR_OK : EdmReturnErrCode::SYSTEM_ABNORMALLY; +} + +ErrCode NTPServerPlugin::OnGetPolicy(std::string &value, MessageParcel &data, MessageParcel &reply, int32_t userId) +{ + EDMLOGI("NTPServerPlugin OnGetPolicy"); + std::string result = system::GetParameter(KEY_NTP_SERVER, ""); + reply.WriteInt32(ERR_OK); + reply.WriteString(result); + return ERR_OK; +} +} // namespace EDM +} // namespace OHOS diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 4eb49a07a..dbecedb71 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -196,6 +196,7 @@ group("unittest") { "enterprise_device_mgr_proxy:unittest", "network_manager_proxy:unittest", "restrictions_proxy:unittest", + "system_manager_proxy:unittest", "usb_manager_proxy:unittest", "wifi_manager_proxy:unittest", ] diff --git a/test/unittest/edm_plugin/BUILD.gn b/test/unittest/edm_plugin/BUILD.gn index 163f6982b..f1adaae71 100644 --- a/test/unittest/edm_plugin/BUILD.gn +++ b/test/unittest/edm_plugin/BUILD.gn @@ -45,6 +45,7 @@ ohos_unittest("EdmPluginTest") { "./src/iptables_rule_plugin_test.cpp", "./src/is_wifi_active_plugin_test.cpp", "./src/network_manager_plugin_test.cpp", + "./src/ntp_server_plugin_test.cpp", "./src/reset_factory_plugin_test.cpp", "./src/screen_off_time_plugin_test.cpp", "./src/set_browser_policies_plugin_test.cpp", @@ -130,6 +131,7 @@ ohos_static_library("edm_all_plugin_static") { "../../../services/edm_plugin/src/install_plugin.cpp", "../../../services/edm_plugin/src/iptables_rule_plugin.cpp", "../../../services/edm_plugin/src/is_wifi_active_plugin.cpp", + "../../../services/edm_plugin/src/ntp_server_plugin.cpp", "../../../services/edm_plugin/src/reset_factory_plugin.cpp", "../../../services/edm_plugin/src/screen_off_time_plugin.cpp", "../../../services/edm_plugin/src/set_browser_policies_plugin.cpp", diff --git a/test/unittest/edm_plugin/src/ntp_server_plugin_test.cpp b/test/unittest/edm_plugin/src/ntp_server_plugin_test.cpp new file mode 100644 index 000000000..7ad5fcc15 --- /dev/null +++ b/test/unittest/edm_plugin/src/ntp_server_plugin_test.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ntp_server_plugin.h" + +#include + +#include "edm_data_ability_utils_mock.h" +#include "edm_ipc_interface_code.h" +#include "iplugin_manager.h" +#include "utils.h" + +using namespace testing::ext; +using namespace testing; + +namespace OHOS { +namespace EDM { +namespace TEST { + +class NTPServerPluginTest : public testing::TestWithParam> { +protected: + static void SetUpTestSuite(void); + + static void TearDownTestSuite(void); +}; + +void NTPServerPluginTest::SetUpTestSuite(void) +{ + Utils::SetEdmServiceEnable(); + Utils::SetEdmInitialEnv(); +} + +void NTPServerPluginTest::TearDownTestSuite(void) +{ + Utils::SetEdmServiceDisable(); + Utils::ResetTokenTypeAndUid(); + ASSERT_TRUE(Utils::IsOriginalUTEnv()); + std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl; +} + +/** + * @tc.name: TestOnGetPolicy + * @tc.desc: Test OnGetPolicy function. + * @tc.type: FUNC + */ +HWTEST_F(NTPServerPluginTest, TestOnGetPolicy, TestSize.Level1) +{ + std::shared_ptr plugin = NTPServerPlugin::GetPlugin(); + std::string policyValue{"TestString"}; + MessageParcel data; + MessageParcel reply; + + plugin->OnGetPolicy(policyValue, data, reply, DEFAULT_USER_ID); + EXPECT_TRUE(reply.ReadInt32() == ERR_OK); + EXPECT_TRUE(reply.ReadString() == ""); +} + +} // namespace TEST +} // namespace EDM +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/system_manager_proxy/BUILD.gn b/test/unittest/system_manager_proxy/BUILD.gn new file mode 100644 index 000000000..91f0e63d8 --- /dev/null +++ b/test/unittest/system_manager_proxy/BUILD.gn @@ -0,0 +1,74 @@ +# Copyright (c) 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 +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "enterprise_device_management/enterprise_device_management" + +ohos_unittest("EdmSystemManagerProxyUnitTest") { + module_out_path = module_output_path + + 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", + "./system_manager_proxy_test.cpp", + ] + + configs = [ "../../../common/config:coverage_flags" ] + + deps = [ + "../../../interfaces/inner_api:edmservice_kits", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil", + "ipc:ipc_core", + "netmanager_base:net_conn_manager_if", + "samgr:samgr_proxy", + ] + + sanitize = { + boundary_sanitize = true + cfi = true + debug = false + integer_overflow = true + stack_protector_ret = true + ubsan = true + } + subsystem_name = "customization" + part_name = "enterprise_device_management" +} + +group("unittest") { + testonly = true + + deps = [ + # deps file + ":EdmSystemManagerProxyUnitTest", + ] +} diff --git a/test/unittest/system_manager_proxy/system_manager_proxy_test.cpp b/test/unittest/system_manager_proxy/system_manager_proxy_test.cpp new file mode 100644 index 000000000..ed5c24167 --- /dev/null +++ b/test/unittest/system_manager_proxy/system_manager_proxy_test.cpp @@ -0,0 +1,132 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "system_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 { +namespace TEST { +const std::string ADMIN_PACKAGENAME = "com.edm.test.demo"; +class SystemManagerProxyTest : public testing::Test { +protected: + void SetUp() override; + + void TearDown() override; + + static void TearDownTestSuite(void); + std::shared_ptr systemmanagerProxy = nullptr; + std::shared_ptr edmSysManager_ = nullptr; + sptr object_ = nullptr; +}; + +void SystemManagerProxyTest::SetUp() +{ + systemmanagerProxy = SystemManagerProxy::GetSystemManagerProxy(); + edmSysManager_ = std::make_shared(); + object_ = new (std::nothrow) EnterpriseDeviceMgrStubMock(); + edmSysManager_->RegisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID, object_); + Utils::SetEdmServiceEnable(); +} + +void SystemManagerProxyTest::TearDown() +{ + systemmanagerProxy.reset(); + edmSysManager_->UnregisterSystemAbilityOfRemoteObject(ENTERPRISE_DEVICE_MANAGER_SA_ID); + object_ = nullptr; + Utils::SetEdmServiceDisable(); +} + +void SystemManagerProxyTest::TearDownTestSuite() +{ + ASSERT_FALSE(Utils::GetEdmServiceState()); +} + +/** + * @tc.name: TestSetNTPServerSuc + * @tc.desc: Test SetNTPServer func. + * @tc.type: FUNC + */ +HWTEST_F(SystemManagerProxyTest, TestSetNTPServerSuc, TestSize.Level1) +{ + AppExecFwk::ElementName admin; + admin.SetBundleName(ADMIN_PACKAGENAME); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestSetPolicy)); + std::string server = "ntp.aliyun.com"; + int32_t ret = systemmanagerProxy->SetNTPServer(admin, server); + ASSERT_TRUE(ret == ERR_OK); +} + +/** + * @tc.name: TestSetNTPServerFail + * @tc.desc: Test SetNTPServer func. + * @tc.type: FUNC + */ +HWTEST_F(SystemManagerProxyTest, TestSetNTPServerFail, TestSize.Level1) +{ + Utils::SetEdmServiceDisable(); + AppExecFwk::ElementName admin; + admin.SetBundleName(ADMIN_PACKAGENAME); + std::string server = "ntp.aliyun.com"; + int32_t ret = systemmanagerProxy->SetNTPServer(admin, server); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); +} + +/** + * @tc.name: TestGetNTPServerSuc + * @tc.desc: Test GetNTPServer func. + * @tc.type: FUNC + */ +HWTEST_F(SystemManagerProxyTest, TestGetNTPServerSuc, TestSize.Level1) +{ + AppExecFwk::ElementName admin; + admin.SetBundleName(ADMIN_PACKAGENAME); + EXPECT_CALL(*object_, SendRequest(_, _, _, _)) + .Times(1) + .WillOnce(Invoke(object_.GetRefPtr(), &EnterpriseDeviceMgrStubMock::InvokeSendRequestGetPolicy)); + std::string server = "ntp.aliyun.com"; + int32_t ret = systemmanagerProxy->GetNTPServer(admin, server); + ASSERT_TRUE(ret == ERR_OK); + ASSERT_TRUE(server == RETURN_STRING); +} + +/** + * @tc.name: TestGetNTPServerFail + * @tc.desc: Test GetNTPServer func. + * @tc.type: FUNC + */ +HWTEST_F(SystemManagerProxyTest, TestGetNTPServerFail, TestSize.Level1) +{ + Utils::SetEdmServiceDisable(); + AppExecFwk::ElementName admin; + admin.SetBundleName(ADMIN_PACKAGENAME); + std::string server = ""; + int32_t ret = systemmanagerProxy->GetNTPServer(admin, server); + ASSERT_TRUE(ret == EdmReturnErrCode::ADMIN_INACTIVE); +} +} // namespace TEST +} // namespace EDM +} // namespace OHOS -- Gitee