diff --git a/README_zh.md b/README_zh.md index 1f95cbb2c819b49883f10fc852535cf75e00f1a0..c8124d2e5986c9033d0cd600add3f3a4fa9156ab 100644 --- a/README_zh.md +++ b/README_zh.md @@ -23,13 +23,12 @@ ## **目录** ``` -/foundation/distributedhardware/distributed_hardware_fwk +/foundation/distributedhardware/distributedhardware_distributed_hardware_fwk ├── common # 分布式硬件管理框架为各个部件提供的公共接口类 ├── sa_profile # 分布式硬件管理框架的SA配置信息 ├── services # 分布式硬件管理框架的SA具体实现 │   └── distributedhardwarefwkservice # 分布式硬件管理框架的服务层 -│   └── accessmanager # 硬件接入管理 -│ └── distributedhardwarefwkserviceimpl # 分布式硬件管理框架的服务实现层 +│   ├── accessmanager # 硬件接入管理 │ ├── resourcemanager # 硬件资源管理 │ ├── componentmanager # 分布式硬件部件管理 │ ├── localhardwaremanager # 本地硬件信息管理 @@ -65,20 +64,20 @@ 以分布式相机为例,某设备下线后,分布式硬件管理框架去使能下线设备的相机硬件,本地移除分布式相机驱动并通知到相机框架,此时下线设备的分布式相机不可用。 -## **涉及仓** +## **相关仓** **** **分布式硬件子系统:** 设备管理 -[device_manager](https://gitee.com/openharmony/device_manager) +[distributedhardware_device_manager](https://gitee.com/openharmony/distributedhardware_device_manager) **分步式硬件管理框架 -[distributed_hardware_fwk](https://gitee.com/openharmony/distributed_hardware_fwk)** +[distributedhardware_distributed_hardware_fwk](https://gitee.com/openharmony/distributedhardware_distributed_hardware_fwk)** 分布式相机 -[distributed_camera](https://gitee.com/openharmony/distributed_camera) +[distributedhardware_distributed_camera](https://gitee.com/openharmony/distributedhardware_distributed_camera) 分布式屏幕 -[distributed_screen](https://gitee.com/openharmony/distributed_screen) +[distributedhardware_distributed_screen](https://gitee.com/openharmony/distributedhardware_distributed_screen) diff --git a/bundle.json b/bundle.json index 7cf5e4ade89892608ade60eb3f941ee60986514a..652bc657a83cbc51e9e020006ce8cb5d542219a6 100644 --- a/bundle.json +++ b/bundle.json @@ -32,11 +32,11 @@ "distributeddatamgr", "eventhandler", "hitrace_native", - "utils_base", + "c_utils", "hiviewdfx_hilog_native", "startup_l2", "bundle_framework", - "samgr_standard", + "samgr", "ipc", "safwk", "common", @@ -53,17 +53,17 @@ "sub_component": [ "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhfwk_sa_profile", - "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhardware.cfg" + "//foundation/distributedhardware/distributed_hardware_fwk/sa_profile:dhardware.cfg", + "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits:libdhfwk_sdk" ], "test": [ "//foundation/distributedhardware/distributed_hardware_fwk/utils/test/unittest:utils_test", - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl/test/unittest:svrimpl_test", "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/unittest:test", + "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits/test/unittest:kit_test", "//foundation/distributedhardware/distributed_hardware_fwk/utils/test/fuzztest:fuzztest", - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl/test/fuzztest:fuzztest", - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest:fuzztest" + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest:fuzztest", + "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits/test/fuzztest:fuzztest" ] } } diff --git a/common/utils/include/constants.h b/common/utils/include/constants.h index 37b1dfa6f0edaf12eeed62337ae408d884d4e98c..319daed24ffaa811be47ece17d028877c72d4294 100644 --- a/common/utils/include/constants.h +++ b/common/utils/include/constants.h @@ -24,10 +24,19 @@ namespace DistributedHardware { constexpr int32_t LOG_MAX_LEN = 4096; constexpr int32_t ENABLE_TIMEOUT_MS = 1000; constexpr int32_t DISABLE_TIMEOUT_MS = 500; + constexpr uint32_t MAX_MESSAGE_LEN = 40 * 1024 * 1024; + constexpr uint32_t MAX_ID_LEN = 256; + constexpr uint32_t MAX_TOPIC_SIZE = 128; + constexpr uint32_t MAX_LISTENER_SIZE = 256; + constexpr uint32_t MAX_COMP_SIZE = 128; + constexpr uint32_t MAX_DB_RECORD_SIZE = 10000; + constexpr uint32_t MAX_ONLINE_DEVICE_SIZE = 10000; const std::u16string DHMS_STUB_INTERFACE_TOKEN = u"ohos.distributedhardware.accessToken"; - const std::string COMPONENTSLOAD_PROFILE_PATH = R"(/etc/distributed_hardware_components_cfg.json)"; + const std::string COMPONENTSLOAD_PROFILE_PATH = + R"(/vendor/etc/distributedhardware/distributed_hardware_components_cfg.json)"; const std::string APP_ID = "dtbhardware_manager_service"; const std::string GLOBAL_CAPABILITY_ID = "global_capability_info"; + const std::string GLOBAL_VERSION_ID = "global_version_info"; const std::string RESOURCE_SEPARATOR = "###"; const std::string DH_ID = "dh_id"; const std::string DEV_ID = "dev_id"; @@ -36,6 +45,13 @@ namespace DistributedHardware { const std::string DH_TYPE = "dh_type"; const std::string DH_ATTRS = "dh_attrs"; const std::string DH_LOG_TITLE_TAG = "DHFWK"; + const std::string DH_VER = "dh_ver"; + const std::string COMP_VER = "comp_ver"; + const std::string NAME = "name"; + const std::string TYPE = "type"; + const std::string HANDLER = "handler"; + const std::string SOURCE_VER = "source_ver"; + const std::string SINK_VER = "sink_ver"; const std::string DH_TASK_NAME_PREFIX = "Task_"; const std::string DH_FWK_PKG_NAME = "ohos.dhardware"; const std::string DH_COMPONENT_VERSIONS = "componentVersions"; diff --git a/common/utils/include/device_type.h b/common/utils/include/device_type.h index 06f425140584ff27d6ca83ca41681e17096869b1..805b8012834a9288d4c16da2b6236ed131b6687a 100644 --- a/common/utils/include/device_type.h +++ b/common/utils/include/device_type.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -24,11 +24,10 @@ namespace DistributedHardware { enum class DHType : uint32_t { UNKNOWN = 0x0, // unknown device CAMERA = 0x01, // Camera - MIC = 0x02, // Mic - SPEAKER = 0x04, // Speaker - DISPLAY = 0x08, // Display + AUDIO = 0x02, // Mic + SCREEN = 0x08, // Display GPS = 0x10, // GPS - INPUT = 0x20, // Key board + INPUT = 0x20, // Key board HFP = 0x40, // HFP External device A2D = 0x80, // A2DP External device VIRMODEM_MIC = 0x100, // Cellular call MIC @@ -38,9 +37,8 @@ enum class DHType : uint32_t { const std::unordered_map DHTypeStrMap = { { DHType::CAMERA, "CAMERA" }, - { DHType::MIC, "MIC" }, - { DHType::SPEAKER, "SPEAKER" }, - { DHType::DISPLAY, "DISPLAY" }, + { DHType::AUDIO, "AUDIO" }, + { DHType::SCREEN, "SCREEN" }, { DHType::GPS, "GPS" }, { DHType::INPUT, "INPUT" }, { DHType::HFP, "HFP" }, @@ -58,6 +56,13 @@ struct DeviceInfo { explicit DeviceInfo(std::string uuid, std::string deviceId, std::string deviceName, uint16_t deviceType) : uuid(uuid), deviceId(deviceId), deviceName(deviceName), deviceType(deviceType) {} }; + +/* The key is DHType, the value is the prefix of DHId */ +const std::unordered_map DHTypePrefixMap = { + {DHType::CAMERA, "Camera"}, + {DHType::SCREEN, "Screen"}, + {DHType::INPUT, "Input"}, +}; } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/common/utils/include/distributed_hardware_errno.h b/common/utils/include/distributed_hardware_errno.h index 96a88bfe39b3e2a247f8d8f02b5d75878a64b0c0..5a8a55c3a018984c31119ba1ed5008932e5558da 100644 --- a/common/utils/include/distributed_hardware_errno.h +++ b/common/utils/include/distributed_hardware_errno.h @@ -29,6 +29,8 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY = -10001; constexpr int32_t ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY = -10002; constexpr int32_t ERR_DH_FWK_TYPE_NOT_EXIST = -10003; + constexpr int32_t ERR_DH_FWK_JSON_PARSE_FAILED = -10004; + constexpr int32_t ERR_DH_FWK_POINTER_IS_NULL = -10005; /* VersionManager errno, range: [-10200, -10299] */ constexpr int32_t ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST = -10200; @@ -44,6 +46,8 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_COMPONENT_UNREGISTER_FAILED = -10007; constexpr int32_t ERR_DH_FWK_COMPONENT_GET_REMOTE_SA_FAILED = -10008; constexpr int32_t ERR_DH_FWK_COMPONENT_GET_SINK_VERSION_FAILED = -10009; + constexpr int32_t ERR_DH_FWK_COMPONENT_DHTYPE_NOT_FOUND = -10010; + constexpr int32_t ERR_DH_FWK_COMPONENT_MONITOR_NULL = -10011; /* ResourceManager errno, range: [-10400, -10499] */ constexpr int32_t ERR_DH_FWK_RESOURCE_DB_ADAPTER_POINTER_NULL = -10400; @@ -57,18 +61,20 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_RESOURCE_REGISTER_DB_FAILED = -10408; constexpr int32_t ERR_DH_FWK_RESOURCE_UNREGISTER_DB_FAILED = -10409; constexpr int32_t ERR_DH_FWK_RESOURCE_KEY_IS_EMPTY = -10410; + constexpr int32_t ERR_DH_FWK_RESOURCE_SYNC_VERSIONINFO_FAIL = -10411; + constexpr int32_t ERR_DH_FWK_RESOURCE_DEVICE_ID_NOT_EXIST = -10412; + constexpr int32_t ERR_DH_FWK_RESOURCE_UUID_NOT_FOUND = -10413; + constexpr int32_t ERR_DH_FWK_RESOURCE_CAPINFO_POINTER_NULL = -10414; + constexpr int32_t ERR_DH_FWK_RESOURCE_RES_DB_DATA_INVALID = -10415; /* DistributedHardwareManager errno, range: [-10500, -10599] */ constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE = -10500; constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_OFFLINE = -10501; - constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_LOAD_IMPL_FAILED = -10502; - constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_LIB_IMPL_OPEN_FAILED = -10503; - constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_LIB_IMPL_GET_INSTANCE_FAILED = -10504; - constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_LIB_IMPL_IS_NULL = -10505; + constexpr int32_t ERR_DH_FWK_HARDWARE_MANAGER_INIT_FAILED = -10502; /* ComponentLoader errno, range: [-10600, -10699] */ constexpr int32_t ERR_DH_FWK_LOADER_HANDLER_IS_NULL = -10600; - constexpr int32_t ERR_DH_FWK_LOADER_COMPONENT_PROFILE_IS_EMPTY = -10601; + constexpr int32_t ERR_DH_FWK_LOADER_CONFIG_JSON_INVALID = -10601; constexpr int32_t ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL = -10602; constexpr int32_t ERR_DH_FWK_LOADER_DLCLOSE_FAIL = -10603; @@ -83,6 +89,9 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_SERVICE_LOCAL_VERSION_NOT_EXIST = -10804; constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL = -10805; constexpr int32_t ERR_DH_FWK_SERVICE_REMOTE_IS_NULL = -10806; + constexpr int32_t ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL = -10807; + constexpr int32_t ERR_DH_FWK_PUBLISHER_LISTENER_IS_NULL = -10808; + constexpr int32_t ERR_DH_FWK_SERVICE_MSG_INVALID = -10809; /* AccessManager errno, range: [-10900, -10999] */ constexpr int32_t ERR_DH_FWK_ACCESS_INIT_DM_FAILED = -10900; @@ -90,10 +99,14 @@ namespace DistributedHardware { constexpr int32_t ERR_DH_FWK_ACCESS_REGISTER_DM_FAILED = -10902; constexpr int32_t ERR_DH_FWK_ACCESS_UNREGISTER_DM_FAILED = -10903; - /* Hidump errno, range: [-11000, -11000] */ + /* Hidump errno, range: [-11000, -11099] */ constexpr int32_t ERR_DH_FWK_HIDUMP_ERROR = -11000; constexpr int32_t ERR_DH_FWK_HIDUMP_INVALID_ARGS = -11001; constexpr int32_t ERR_DH_FWK_HIDUMP_DPRINTF_ERROR = -11002; + + /* DHFWK Publisher errno, range: [-11100, -11199] */ + constexpr int32_t ERR_DH_FWK_PUBLISH_MSG_FAILED = -11100; + constexpr uint32_t ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE = -11101; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_ERRNO diff --git a/common/utils/include/idistributed_hardware.h b/common/utils/include/idistributed_hardware.h index c0db431881465a385b768bd1c103b6f335e43791..84a4a4b6dc3e15030947dc5ea663dba2370b24a7 100644 --- a/common/utils/include/idistributed_hardware.h +++ b/common/utils/include/idistributed_hardware.h @@ -16,11 +16,15 @@ #ifndef OHOS_IDISTRIBUTED_HARDWARE_H #define OHOS_IDISTRIBUTED_HARDWARE_H +#include +#include #include #include "iremote_broker.h" #include "device_type.h" +#include "ipublisher_listener.h" + namespace OHOS { namespace DistributedHardware { class IDistributedHardware : public OHOS::IRemoteBroker { @@ -28,13 +32,16 @@ public: DECLARE_INTERFACE_DESCRIPTOR(u"ohos.distributedhardware.distributedhardwarefwk"); IDistributedHardware() = default; virtual ~IDistributedHardware() = default; - virtual int32_t QuerySinkVersion(std::unordered_map &versionMap) = 0; - + virtual int32_t RegisterPublisherListener(const DHTopic topic, const sptr &listener) = 0; + virtual int32_t UnregisterPublisherListener(const DHTopic topic, const sptr &listener) = 0; + virtual int32_t PublishMessage(const DHTopic topic, const std::string &msg) = 0; public: - enum { - QUERY_SINK_VERSION = 1 + enum class Message : uint32_t { + REG_PUBLISHER_LISTNER = 1, + UNREG_PUBLISHER_LISTENER = 2, + PUBLISH_MESSAGE = 3 }; }; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_IDISTRIBUTED_HARDWARE_H diff --git a/common/utils/include/idistributed_hardware_manager.h b/common/utils/include/idistributed_hardware_manager.h index 46dcd6ad026ce6d89fda841cb88c88cd32215248..dc2cf8948f1c1d6b342370cbfd1cfb9bb1e670d5 100644 --- a/common/utils/include/idistributed_hardware_manager.h +++ b/common/utils/include/idistributed_hardware_manager.h @@ -38,4 +38,4 @@ public: }; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_IDISTRIBUTED_HARDWARE_MANAGER_H diff --git a/common/utils/include/ipublisher_listener.h b/common/utils/include/ipublisher_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..5ac03510d990c0ff60b04cfbdb0388f205207481 --- /dev/null +++ b/common/utils/include/ipublisher_listener.h @@ -0,0 +1,53 @@ +/* + * 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 OHOS_IPUBLISHER_LISTENER_H +#define OHOS_IPUBLISHER_LISTENER_H + +#include +#include + +#include + +namespace OHOS { +namespace DistributedHardware { +enum class DHTopic : uint32_t { + // Topic min border, not use for real topic + TOPIC_MIN = 0, + // Start project distributed screen + TOPIC_START_DSCREEN = 1, + // Publish Sink Project Window Info + TOPIC_SINK_PROJECT_WINDOW_INFO = 2, + // Stop distributed screen project + TOPIC_STOP_DSCREEN = 3, + // publish device offline message + TOPIC_DEV_OFFLINE = 4, + // Topic max border, not use for real topic + TOPIC_MAX = 5 +}; + +class IPublisherListener : public IRemoteBroker { +public: + virtual void OnMessage(const DHTopic topic, const std::string& message) = 0; + + enum class Message : uint32_t { + ON_MESSAGE, + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.DistributedHardware.DistributedHardwareFwk.IPublisherListener"); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_IPUBLISHER_LISTENER_H \ No newline at end of file diff --git a/common/utils/include/single_instance.h b/common/utils/include/single_instance.h index 78da97ce7a495a383adbdfb7b1f46b1bbfc9d493..104681acb50af230904324ad9cc21b1580a8b396 100644 --- a/common/utils/include/single_instance.h +++ b/common/utils/include/single_instance.h @@ -18,6 +18,13 @@ namespace OHOS { namespace DistributedHardware { +#define REMOVE_NO_USE_CONSTRUCTOR(className) \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete; \ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + #define DECLARE_SINGLE_INSTANCE_BASE(className) \ public: \ static className & GetInstance(); \ @@ -41,4 +48,4 @@ className & className::GetInstance() \ } } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_SINGLE_INSTANCE_H diff --git a/interfaces/inner_kits/BUILD.gn b/interfaces/inner_kits/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3c78bfa807ba572ff540a152aa933d3647fa9a85 --- /dev/null +++ b/interfaces/inner_kits/BUILD.gn @@ -0,0 +1,62 @@ +# 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. + +import("//build/ohos.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +ohos_shared_library("libdhfwk_sdk") { + include_dirs = [ + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "${innerkits_path}/include", + "${innerkits_path}/include/ipc", + "${common_path}/log/include", + "${common_path}/utils/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${services_path}/distributedhardwarefwkservice/include", + "//third_party/json/include", + ] + + sources = [ + "${innerkits_path}/src/distributed_hardware_fwk_kit.cpp", + "${innerkits_path}/src/ipc/dhfwk_sa_manager.cpp", + "${innerkits_path}/src/ipc/distributed_hardware_proxy.cpp", + "${innerkits_path}/src/ipc/publisher_listener_stub.cpp", + ] + + deps = [ + "${services_path}/distributedhardwarefwkservice:distributedhardwarefwksvr", + "${utils_path}:distributedhardwareutils", + "//foundation/systemabilitymgr/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dhfwkkit\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "c_utils:utils", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] + + subsystem_name = "distributedhardware" + + part_name = "distributed_hardware_fwk" +} diff --git a/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h new file mode 100644 index 0000000000000000000000000000000000000000..bcd53d91d4d226baa555a28acaae03ced6f85518 --- /dev/null +++ b/interfaces/inner_kits/include/distributed_hardware_fwk_kit.h @@ -0,0 +1,54 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H +#define OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H + +#include +#include +#include +#include +#include +#include "refbase.h" + +#include "ipublisher_listener.h" +#include "idistributed_hardware.h" + +#ifndef API_EXPORT +#define API_EXPORT __attribute__((visibility("default"))) +#endif + +namespace OHOS { +namespace DistributedHardware { +class DistributedHardwareFwkKit final { +public: + API_EXPORT DistributedHardwareFwkKit(); + API_EXPORT ~DistributedHardwareFwkKit(); + API_EXPORT int32_t RegisterPublisherListener(const DHTopic topic, sptr listener); + API_EXPORT int32_t UnregisterPublisherListener(const DHTopic topic, sptr listener); + API_EXPORT int32_t PublishMessage(const DHTopic topic, const std::string &message); + + void OnDHFWKOnLine(bool isOnLine); +private: + bool IsDHTopicValid(DHTopic topic); + +private: + std::mutex listenerMutex_; + std::unordered_map>> listenerMap_; + std::atomic isDHFWKOnLine_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_HARDWARE_FWK_KIT_H \ No newline at end of file diff --git a/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..ef35c38a4210f1994395b738f5994e595116605e --- /dev/null +++ b/interfaces/inner_kits/include/ipc/dhfwk_sa_manager.h @@ -0,0 +1,60 @@ +/* + * 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 OHOS_DHFWK_SA_MANAGER_H +#define OHOS_DHFWK_SA_MANAGER_H + +#include +#include +#include + +#include "refbase.h" +#include "system_ability_status_change_stub.h" + +#include "single_instance.h" +#include "idistributed_hardware.h" + +namespace OHOS { +namespace DistributedHardware { +// DHFWK sa state callback, param true for started, false for stopped. +using DHFWKSAStateCb = std::function; +class DHFWKSAManager { +DECLARE_SINGLE_INSTANCE_BASE(DHFWKSAManager); +public: + DHFWKSAManager(); + virtual ~DHFWKSAManager(); + void RegisterAbilityListener(); + sptr GetDHFWKProxy(); + void RegisterSAStateCallback(DHFWKSAStateCb callback); + +public: +class SystemAbilityListener : public SystemAbilityStatusChangeStub { +public: + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; +}; + +private: + std::atomic dhfwkOnLine_; + std::atomic isSubscribeDHFWKSAChangeListener; + std::mutex proxyMutex_; + sptr dhfwkProxy_; + sptr saListener_; + std::mutex saStatCbMutex_; + DHFWKSAStateCb saStateCallback; +}; +} // DistributedHardware +} // OHOS +#endif // OHOS_DHFWK_SA_MANAGER_H \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/include/distributed_hardware_proxy.h b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h similarity index 78% rename from services/distributedhardwarefwkserviceimpl/include/distributed_hardware_proxy.h rename to interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h index 13bea7e2ea169df21a5f923e804fc7ace51f6599..bf3b38e59efd72aa97f18e5578f3df87b7eae1d5 100644 --- a/services/distributedhardwarefwkserviceimpl/include/distributed_hardware_proxy.h +++ b/interfaces/inner_kits/include/ipc/distributed_hardware_proxy.h @@ -32,13 +32,13 @@ public: } virtual ~DistributedHardwareProxy() {} - int32_t QuerySinkVersion(std::unordered_map &versionMap) override; + int32_t RegisterPublisherListener(const DHTopic topic, const sptr &listener) override; + int32_t UnregisterPublisherListener(const DHTopic topic, const sptr &listener) override; + int32_t PublishMessage(const DHTopic topic, const std::string &msg) override; private: - std::unordered_map FromJson(const std::string &json) const; - static inline BrokerDelegator delegator_; }; } // namespace DistributedHardware } // namespace OHOS -#endif +#endif // OHOS_DISTRIBUTED_HARDWARE_PROXY_H diff --git a/interfaces/inner_kits/include/ipc/publisher_listener_stub.h b/interfaces/inner_kits/include/ipc/publisher_listener_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..30a0e90696ebb93d55696b1c79abf53be07955f4 --- /dev/null +++ b/interfaces/inner_kits/include/ipc/publisher_listener_stub.h @@ -0,0 +1,36 @@ +/* + * 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 OHOS_PUBLISHER_LISTENER_STUB_H +#define OHOS_PUBLISHER_LISTENER_STUB_H + +#include "ipublisher_listener.h" + +#include "iremote_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class PublisherListenerStub : public IRemoteStub { +public: + PublisherListenerStub(); + virtual ~PublisherListenerStub() override; + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + DISALLOW_COPY_AND_MOVE(PublisherListenerStub); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_PUBLISHER_LISTENER_STUB_H \ No newline at end of file diff --git a/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b55a32a93f7dd459183bbd52c7c9ec5ecda5abaa --- /dev/null +++ b/interfaces/inner_kits/src/distributed_hardware_fwk_kit.cpp @@ -0,0 +1,144 @@ +/* + * 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. + */ + +#include "distributed_hardware_fwk_kit.h" + +#include + +#include "constants.h" +#include "dhfwk_sa_manager.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "idistributed_hardware.h" + +namespace OHOS { +namespace DistributedHardware { +DistributedHardwareFwkKit::DistributedHardwareFwkKit() : listenerMap_({}), isDHFWKOnLine_(false) +{ + DHLOGI("Ctor DistributedHardwareFwkKit"); + DHFWKSAManager::GetInstance().RegisterSAStateCallback( + std::bind(&DistributedHardwareFwkKit::OnDHFWKOnLine, this, std::placeholders::_1)); + DHFWKSAManager::GetInstance().RegisterAbilityListener(); +} + +DistributedHardwareFwkKit::~DistributedHardwareFwkKit() +{ + DHLOGI("Dtor DistributedHardwareFwkKit"); +} + +int32_t DistributedHardwareFwkKit::RegisterPublisherListener(const DHTopic topic, sptr listener) +{ + DHLOGI("Register publisher listener, topic: %" PRIu32 ", is DHFWK online: %s", + (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false"); + if (!IsDHTopicValid(topic)) { + DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic); + return ERR_DH_FWK_PARA_INVALID; + } + + int32_t ret = DH_FWK_SUCCESS; + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { + ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(topic, listener); + DHLOGI("Register publisher listener to DHFWK, ret: %" PRId32, ret); + } else { + DHLOGI("DHFWK not online, or get proxy failed, save listener temporary"); + std::lock_guard lock(listenerMutex_); + if (listenerMap_.size() >= MAX_TOPIC_SIZE || listenerMap_[topic].size() >= MAX_LISTENER_SIZE) { + DHLOGE("listeners are over size!"); + return ERR_DH_FWK_PUBLISH_LISTENER_OVER_SIZE; + } + listenerMap_[topic].insert(listener); + } + + return ret; +} + +int32_t DistributedHardwareFwkKit::UnregisterPublisherListener(const DHTopic topic, sptr listener) +{ + DHLOGI("Unregister publisher listener, topic: %" PRIu32 ", is DHFWK online: %s", + (uint32_t)topic, isDHFWKOnLine_ ? "true" : "false"); + if (!IsDHTopicValid(topic)) { + DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic); + return ERR_DH_FWK_PARA_INVALID; + } + + int32_t ret = DH_FWK_SUCCESS; + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() != nullptr) { + ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->UnregisterPublisherListener(topic, listener); + DHLOGI("Unregister publisher listener to DHFWK, ret: %" PRId32, ret); + } + std::lock_guard lock(listenerMutex_); + listenerMap_[topic].erase(listener); + + return ret; +} + +int32_t DistributedHardwareFwkKit::PublishMessage(const DHTopic topic, const std::string &message) +{ + DHLOGI("Publish message, topic: %" PRIu32, (uint32_t)topic); + if (!IsDHTopicValid(topic)) { + DHLOGE("Topic invalid, topic: %" PRIu32, (uint32_t)topic); + return ERR_DH_FWK_PARA_INVALID; + } + if (message.empty() || message.size() > MAX_MESSAGE_LEN) { + DHLOGE("Message size is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } + + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + DHLOGI("DHFWK not online, can not publish message"); + return ERR_DH_FWK_PUBLISH_MSG_FAILED; + } + + int32_t ret = DHFWKSAManager::GetInstance().GetDHFWKProxy()->PublishMessage(topic, message); + DHLOGI("Publish message to DHFWK, ret: %" PRId32, ret); + + return ret; +} + +bool DistributedHardwareFwkKit::IsDHTopicValid(DHTopic topic) +{ + return topic > DHTopic::TOPIC_MIN && topic < DHTopic::TOPIC_MAX; +} + +void DistributedHardwareFwkKit::OnDHFWKOnLine(bool isOnLine) +{ + DHLOGI("Receive DHFWK online callback, %s", (isOnLine ? "true" : "false")); + isDHFWKOnLine_ = isOnLine; + + if (DHFWKSAManager::GetInstance().GetDHFWKProxy() == nullptr) { + return; + } + + DHLOGI("DHFWK online, register saved listener to it"); + std::unordered_map>> regSuccListeners; + std::lock_guard lock(listenerMutex_); + for (const auto &entry : listenerMap_) { + for (const auto &listener : entry.second) { + int32_t ret = + DHFWKSAManager::GetInstance().GetDHFWKProxy()->RegisterPublisherListener(entry.first, listener); + if (ret == DH_FWK_SUCCESS) { + regSuccListeners[entry.first].insert(listener); + } + } + } + + for (const auto &succ : regSuccListeners) { + for (const auto &listener : succ.second) { + listenerMap_[succ.first].erase(listener); + } + } +} +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..556d566f96b2afb7240d0e9ad93fb688157931ec --- /dev/null +++ b/interfaces/inner_kits/src/ipc/dhfwk_sa_manager.cpp @@ -0,0 +1,140 @@ +/* + * 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. + */ + +#include "dhfwk_sa_manager.h" + +#include + +#include "iservice_registry.h" +#include "system_ability_definition.h" + +#include "distributed_hardware_log.h" +#include "distributed_hardware_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(DHFWKSAManager); +DHFWKSAManager::DHFWKSAManager() + : dhfwkOnLine_(false), isSubscribeDHFWKSAChangeListener(false), dhfwkProxy_(nullptr), + saListener_(new SystemAbilityListener()), saStateCallback(nullptr) +{ + DHLOGI("Ctor DHFWKSAManager"); +} +DHFWKSAManager::~DHFWKSAManager() +{ + DHLOGI("Dtor DHFWKSAManager"); + dhfwkOnLine_ = false; + isSubscribeDHFWKSAChangeListener = false; + dhfwkProxy_ = nullptr; + saListener_ = nullptr; +} + +void DHFWKSAManager::RegisterAbilityListener() +{ + DHLOGI("Register DHFWK sa listener"); + sptr saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saMgr == nullptr) { + DHLOGE("Get System Ability Manager failed"); + return; + } + + if (!isSubscribeDHFWKSAChangeListener) { + DHLOGI("try subscribe sa change listener, sa id: %d", DISTRIBUTED_HARDWARE_SA_ID); + int32_t ret = saMgr->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_SA_ID, saListener_); + if (ret != 0) { + DHLOGE("subscribe DHFWK sa change listener failed, ret: %d", ret); + return; + } + isSubscribeDHFWKSAChangeListener = true; + } +} + +sptr DHFWKSAManager::GetDHFWKProxy() +{ + if (!isSubscribeDHFWKSAChangeListener) { + RegisterAbilityListener(); + } + + std::lock_guard lock(proxyMutex_); + if (dhfwkProxy_ == nullptr) { + sptr saMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (saMgr == nullptr) { + DHLOGE("Get System Ability Manager failed"); + return nullptr; + } + DHLOGI("Try get DHFWK sa"); + sptr remoteObject = saMgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_SA_ID); + if (remoteObject == nullptr) { + DHLOGE("Get DHFWK proxy return null"); + return nullptr; + } + + dhfwkProxy_ = iface_cast(remoteObject); + if (!dhfwkProxy_ || !dhfwkProxy_->AsObject()) { + DHLOGE("Failed to Get DHFWK Proxy"); + return nullptr; + } + } + + return dhfwkProxy_; +} + +void DHFWKSAManager::RegisterSAStateCallback(DHFWKSAStateCb callback) +{ + std::lock_guard lock(saStatCbMutex_); + saStateCallback = callback; +} + +void DHFWKSAManager::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + (void)deviceId; + if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) { + DHLOGW("Receive SA Start, but sa id is not DHFWK, id: %" PRId32, systemAbilityId); + return; + } + + DHFWKSAManager::GetInstance().dhfwkOnLine_ = true; + { + std::lock_guard lock(DHFWKSAManager::GetInstance().saStatCbMutex_); + if (DHFWKSAManager::GetInstance().saStateCallback != nullptr) { + DHFWKSAManager::GetInstance().saStateCallback(true); + } + } + DHLOGI("sa %" PRId32 " started", systemAbilityId); +} + +void DHFWKSAManager::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + (void)deviceId; + if (systemAbilityId != DISTRIBUTED_HARDWARE_SA_ID) { + DHLOGW("Receive SA Stop, but sa id is not DHFWK, id: %" PRId32, systemAbilityId); + return; + } + + DHFWKSAManager::GetInstance().dhfwkOnLine_ = false; + { + std::lock_guard lock(DHFWKSAManager::GetInstance().proxyMutex_); + DHFWKSAManager::GetInstance().dhfwkProxy_ = nullptr; + } + { + std::lock_guard lock(DHFWKSAManager::GetInstance().saStatCbMutex_); + if (DHFWKSAManager::GetInstance().saStateCallback != nullptr) { + DHFWKSAManager::GetInstance().saStateCallback(false); + } + } + DHLOGI("sa %" PRId32 " stopped", systemAbilityId); +} +} // DistributedHardware +} // OHOS \ No newline at end of file diff --git a/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cb54443031adbda5abe02cad501308adc93d094f --- /dev/null +++ b/interfaces/inner_kits/src/ipc/distributed_hardware_proxy.cpp @@ -0,0 +1,181 @@ +/* + * 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. + */ + +#include "distributed_hardware_proxy.h" + +#include + +#include "anonymous_string.h" +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "nlohmann/json.hpp" +#include "parcel.h" + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "DistributedHardwareProxy" +const std::unordered_set DH_TYPE_SET { + DHType::UNKNOWN, DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::GPS, DHType::INPUT, + DHType::HFP, DHType::A2D, DHType::VIRMODEM_MIC, DHType::VIRMODEM_SPEAKER, DHType::MAX_DH, + }; + +int32_t DistributedHardwareProxy::RegisterPublisherListener(const DHTopic topic, + const sptr &listener) +{ + if (listener == nullptr) { + DHLOGE("publisher listener is null"); + return ERR_DH_FWK_PUBLISHER_LISTENER_IS_NULL; + } + + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; + } + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteUint32((uint32_t)topic)) { + DHLOGE("DistributedHardwareProxy write topic failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("DistributedHardwareProxy write listener failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest((uint32_t)IDistributedHardware::Message::REG_PUBLISHER_LISTNER, + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + ret = reply.ReadInt32(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Register Publisher Listener failed, ret: %d", ret); + } + + return ret; +} + +int32_t DistributedHardwareProxy::UnregisterPublisherListener(const DHTopic topic, + const sptr &listener) +{ + if (listener == nullptr) { + DHLOGE("publisher listener is null"); + return ERR_DH_FWK_PUBLISHER_LISTENER_IS_NULL; + } + + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; + } + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteUint32((uint32_t)topic)) { + DHLOGE("DistributedHardwareProxy write topic failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteRemoteObject(listener->AsObject())) { + DHLOGE("DistributedHardwareProxy write listener failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest((uint32_t)IDistributedHardware::Message::UNREG_PUBLISHER_LISTENER, + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + ret = reply.ReadInt32(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("Unregister Publisher Listener failed, ret: %d", ret); + } + + return ret; +} + +int32_t DistributedHardwareProxy::PublishMessage(const DHTopic topic, const std::string &msg) +{ + sptr remote = Remote(); + if (remote == nullptr) { + DHLOGE("remote service is null"); + return ERR_DH_FWK_SERVICE_REMOTE_IS_NULL; + } + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_DH_FWK_PARA_INVALID; + } + if (msg.empty() || msg.size() > MAX_MESSAGE_LEN) { + DHLOGE("Msg is invalid"); + return ERR_DH_FWK_SERVICE_MSG_INVALID; + } + + MessageParcel data; + MessageParcel reply; + MessageOption option; + + if (!data.WriteInterfaceToken(GetDescriptor())) { + DHLOGE("WriteInterfaceToken fail!"); + return ERR_DH_FWK_SERVICE_WRITE_TOKEN_FAIL; + } + if (!data.WriteUint32((uint32_t)topic)) { + DHLOGE("DistributedHardwareProxy write topic failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + if (!data.WriteString(msg)) { + DHLOGE("DistributedHardwareProxy write listener failed"); + return ERR_DH_FWK_SERVICE_WRITE_INFO_FAIL; + } + int32_t ret = remote->SendRequest((uint32_t)IDistributedHardware::Message::PUBLISH_MESSAGE, + data, reply, option); + if (ret != NO_ERROR) { + DHLOGE("Send Request failed, ret: %d", ret); + return ERR_DH_FWK_SERVICE_IPC_SEND_REQUEST_FAIL; + } + + ret = reply.ReadInt32(); + if (ret != DH_FWK_SUCCESS) { + DHLOGE("PublishMessage failed, ret: %d", ret); + } + + return ret; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp b/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9296ad6a8a2434bf1ee2ee47ecb6af63e7f61456 --- /dev/null +++ b/interfaces/inner_kits/src/ipc/publisher_listener_stub.cpp @@ -0,0 +1,62 @@ +/* + * 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. + */ + +#include "publisher_listener_stub.h" + +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +namespace OHOS { +namespace DistributedHardware { +PublisherListenerStub::PublisherListenerStub() +{ +} + +PublisherListenerStub::~PublisherListenerStub() +{ +} + +int32_t PublisherListenerStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + if (data.ReadInterfaceToken() != GetDescriptor()) { + DHLOGE("PublisherListenerStub read valid token failed"); + return ERR_INVALID_DATA; + } + IPublisherListener::Message msgCode = static_cast(code); + switch (msgCode) { + case IPublisherListener::Message::ON_MESSAGE: { + DHTopic topic = (DHTopic)data.ReadUint32(); + if (DHTopic::TOPIC_MIN > topic || topic > DHTopic::TOPIC_MAX) { + DHLOGE("Topic is invalid!"); + return ERR_INVALID_DATA; + } + std::string message = data.ReadString(); + if (message.empty() || message.size() > MAX_MESSAGE_LEN) { + DHLOGE("Message is invalid!"); + return ERR_INVALID_DATA; + } + OnMessage(topic, message); + break; + } + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } + + return DH_FWK_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/BUILD.gn b/interfaces/inner_kits/test/fuzztest/BUILD.gn similarity index 80% rename from services/distributedhardwarefwkserviceimpl/test/fuzztest/BUILD.gn rename to interfaces/inner_kits/test/fuzztest/BUILD.gn index 9def5f890e2a38d002aa72ae62a98b00cffbed9a..dfa7fe54041c4fb83a7dbe7f62cce0fe0fef2275 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/BUILD.gn +++ b/interfaces/inner_kits/test/fuzztest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# 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 @@ -14,8 +14,5 @@ group("fuzztest") { testonly = true - deps = [ - "componentmanager_fuzzer:fuzztest", - "resourcemanager_fuzzer:fuzztest", - ] + deps = [ "distributedhardwarefwkkit_fuzzer:fuzztest" ] } diff --git a/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/BUILD.gn b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..815c6b4fbd180f211ba3a61b5fbb767bcbbb974e --- /dev/null +++ b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/BUILD.gn @@ -0,0 +1,66 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DistributedHardwareFwkKitFuzzTest") { + module_out_path = "distributed_hardware_fwk/distributedhardwarefwkkit" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer" + + include_dirs = [ + "include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${common_path}/utils/include", + "${common_path}/log/include", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/accessmanager", + "${innerkits_path}/include", + "${innerkits_path}/include/ipc", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "distributedhardwarefwkkit_fuzzer.cpp" ] + + deps = [ "${innerkits_path}:libdhfwk_sdk" ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DistributedHardwareFwkKitFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "c_utils:utils", + "ipc:ipc_core", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DistributedHardwareFwkKitFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/corpus/init b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/corpus/init similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/fuzztest/componentmanager_fuzzer/corpus/init rename to interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/corpus/init diff --git a/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.cpp b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9834b50c5f38b7111580eded9c179973b4dd6b6a --- /dev/null +++ b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.cpp @@ -0,0 +1,83 @@ +/* + * 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. + */ + +#include "distributedhardwarefwkkit_fuzzer.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "distributed_hardware_fwk_kit.h" +#include "distributed_hardware_errno.h" +#include "publisher_listener_stub.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + std::map TOPIC_MSGS = { + { DHTopic::TOPIC_START_DSCREEN, "Start DScreen" }, + { DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, "Sink Project Window" }, + { DHTopic::TOPIC_STOP_DSCREEN, "Stop DScreen" }, + { DHTopic::TOPIC_DEV_OFFLINE, "Dev Offline" } + }; + const int32_t SLEEP_TIME_MS = 1000; +} + +void TestPublisherListener::OnMessage(const DHTopic topic, const std::string &message) +{ + (void)topic; + (void)message; +} + +void DistributedHardwareFwkKitFuzzTest(const uint8_t *data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + sptr listener = new TestPublisherListener(); + DistributedHardwareFwkKit dhfwkKit; + dhfwkKit.RegisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, listener); + dhfwkKit.RegisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, listener); + dhfwkKit.RegisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, listener); + dhfwkKit.RegisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, listener); + + dhfwkKit.PublishMessage(DHTopic::TOPIC_START_DSCREEN, TOPIC_MSGS[DHTopic::TOPIC_START_DSCREEN]); + dhfwkKit.PublishMessage(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, + TOPIC_MSGS[DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO]); + dhfwkKit.PublishMessage(DHTopic::TOPIC_STOP_DSCREEN, TOPIC_MSGS[DHTopic::TOPIC_STOP_DSCREEN]); + dhfwkKit.PublishMessage(DHTopic::TOPIC_DEV_OFFLINE, TOPIC_MSGS[DHTopic::TOPIC_DEV_OFFLINE]); + + dhfwkKit.UnregisterPublisherListener(DHTopic::TOPIC_START_DSCREEN, listener); + dhfwkKit.UnregisterPublisherListener(DHTopic::TOPIC_SINK_PROJECT_WINDOW_INFO, listener); + dhfwkKit.UnregisterPublisherListener(DHTopic::TOPIC_STOP_DSCREEN, listener); + dhfwkKit.UnregisterPublisherListener(DHTopic::TOPIC_DEV_OFFLINE, listener); + std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_MS)); +} +} // namespace DistributedHardware +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::DistributedHardwareFwkKitFuzzTest(data, size); + return 0; +} \ No newline at end of file diff --git a/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.h b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..d165f8a6eb5d85b2cd0c3fa83b4dee0cbe7265d6 --- /dev/null +++ b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/distributedhardwarefwkkit_fuzzer.h @@ -0,0 +1,34 @@ +/* + * 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 TEST_DISTRIBUTEDHARDWAREFWK_KIT_FUZZER_H +#define TEST_DISTRIBUTEDHARDWAREFWK_KIT_FUZZER_H + +#define FUZZ_PROJECT_NAME "distributedhardwarefwkkit_fuzzer" + +#include +#include "publisher_listener_stub.h" + +namespace OHOS { +namespace DistributedHardware { +class TestPublisherListener : public PublisherListenerStub { +public: + TestPublisherListener() = default; + virtual ~TestPublisherListener() = default; + void OnMessage(const DHTopic topic, const std::string &message); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/project.xml b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/project.xml similarity index 95% rename from services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/project.xml rename to interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/project.xml index 85e7ef2c1cc6471e288306f6e3dcea5287a78b0e..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/project.xml +++ b/interfaces/inner_kits/test/fuzztest/distributedhardwarefwkkit_fuzzer/project.xml @@ -1,5 +1,5 @@ - + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/BUILD.gn similarity index 65% rename from services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/BUILD.gn rename to services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/BUILD.gn index bda4171b24c07dcf4db31484acb9c5fb31cbb8d7..cd2bba4110632edf5349a5c2fd6d1d24bd677685 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# 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 @@ -20,20 +20,22 @@ import( ##############################fuzztest########################################## ohos_fuzztest("ResourcemanagerFuzzTest") { module_out_path = "distributed_hardware_fwk/resourcemanager" - fuzz_config_file = "${services_path}/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer" + fuzz_config_file = "${services_path}/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer" include_dirs = [ "${utils_path}/include", "${utils_path}/include/log", "${utils_path}/include/eventbus", - "${services_path}/distributedhardwarefwkserviceimpl/include", - "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", - "//utils/native/base/include", + "//base/notification/eventhandler/interfaces/inner_api", + "//commonlibrary/c_utils/base/include", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", "//third_party/json/include", - "//utils/native/base/include", "//utils/system/safwk/native/include", ] cflags = [ @@ -44,11 +46,7 @@ ohos_fuzztest("ResourcemanagerFuzzTest") { ] sources = [ "resourcemanager_fuzzer.cpp" ] - deps = [ - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", - "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", - "//utils/native/base:utils", - ] + deps = [ "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr" ] defines = [ "HI_LOG_ENABLE", @@ -56,14 +54,7 @@ ohos_fuzztest("ResourcemanagerFuzzTest") { "LOG_DOMAIN=0xD004100", ] - external_deps = [ - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "distributeddatamgr:distributeddata_inner", - "eventhandler:libeventhandler", - "hisysevent_native:libhisysevent", - "hiviewdfx_hilog_native:libhilog", - ] + external_deps = [ "c_utils:utils" ] } ############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.cpp similarity index 94% rename from services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.cpp rename to services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.cpp index 0d2097978d4900066f5fee6a566a2b5058245ec5..852fc8bb9662a5f7aa9413ed0dea6678f55f1ec8 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.cpp +++ b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -31,7 +31,7 @@ namespace DistributedHardware { namespace { const uint32_t DH_TYPE_SIZE = 10; const DHType dhTypeFuzz[DH_TYPE_SIZE] = { - DHType::CAMERA, DHType::MIC, DHType::SPEAKER, DHType::DISPLAY, DHType::VIRMODEM_MIC, + DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_MIC, DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER }; } diff --git a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.h similarity index 93% rename from services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.h rename to services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.h index 591d5d0cc6df41b6898dea687b4d37b369a86e86..5c73c7779554e84e401c86b165ab16eddc48fd4a 100644 --- a/services/distributedhardwarefwkserviceimpl/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.h +++ b/services/distributedhardwarefwkservice/test/fuzztest/resourcemanager_fuzzer/resourcemanager_fuzzer.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 diff --git a/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..30dd02cb398b98fba39fda40089e185b0026303b --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/BUILD.gn @@ -0,0 +1,59 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("TaskFuzzTest") { + module_out_path = "distributed_hardware_fwk/task" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer" + + include_dirs = [ + "include", + "//third_party/json/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//commonlibrary/c_utils/base/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "task_fuzzer.cpp" ] + + deps = [ "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr" ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"TaskFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + deps = [ ":TaskFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..22f17590b8ed3b756dfc34f4fa8eefee2989eb8c --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.cpp @@ -0,0 +1,77 @@ +/* + * 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. + */ + +#include "task_fuzzer.h" + +#include +#include + +#include "constants.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "task_board.h" +#include "task_executor.h" +#include "task_factory.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + const uint32_t DH_TYPE_SIZE = 10; + const DHType dhTypeFuzz[DH_TYPE_SIZE] = { + DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_MIC, + DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER + }; + + const uint32_t TASK_TYPE_SIZE = 5; + const TaskType taskTypeFuzz[TASK_TYPE_SIZE] = { + TaskType::UNKNOWN, TaskType::ENABLE, TaskType::DISABLE, TaskType::ON_LINE, TaskType::OFF_LINE + }; +} + +void TaskFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + std::string networkId(reinterpret_cast(data), size); + std::string uuid(reinterpret_cast(data), size); + std::string dhId(reinterpret_cast(data), size); + DHType dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE]; + + TaskParam taskParam = { + .networkId = networkId, + .uuid = uuid, + .dhId = dhId, + .dhType = dhType + }; + TaskType taskType = taskTypeFuzz[data[0] % TASK_TYPE_SIZE]; + + auto task = TaskFactory::GetInstance().CreateTask(taskType, taskParam, nullptr); + if (task != nullptr) { + TaskBoard::GetInstance().RemoveTask(task->GetId()); + } +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::TaskFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..bc78043591d8f67de8eee6799dd6d5fc9c9e572d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/task_fuzzer/task_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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 TEST_COMPONENTMANAGER_FUZZER_H +#define TEST_COMPONENTMANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "componentmanager_fuzzer" + +#endif diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/BUILD.gn b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9b71d23db24a9c6a17ce06c391e0fa59df5ddb86 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/BUILD.gn @@ -0,0 +1,65 @@ +# 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +##############################fuzztest########################################## +ohos_fuzztest("VersioninfoManagerFuzzTest") { + module_out_path = "distributed_hardware_fwk/versioninfomanager" + fuzz_config_file = "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer" + + include_dirs = [ + "//base/notification/eventhandler/interfaces/inner_api", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + sources = [ "versioninfomanager_fuzzer.cpp" ] + + deps = [ "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr" ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"VersioninfoManagerFuzzTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ "c_utils:utils" ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":VersioninfoManagerFuzzTest" ] +} +############################################################################### diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/corpus/init b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..8eb5a7d6eb6b7d71f0c70c244e5768d62bee6ac5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/project.xml b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..6e8ad2cfde8f8bda4beb6cabbe7efd8bc3c54eec --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.cpp b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..264e442167b48992eacee4fbdea2ffa94659e39f --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.cpp @@ -0,0 +1,76 @@ +/* + * 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. + */ + +#include "versioninfomanager_fuzzer.h" + +#include +#include +#include +#include +#include +#include + +#include "version_info_manager.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_manager_factory.h" + +namespace OHOS { +namespace DistributedHardware { +namespace { + constexpr uint32_t SLEEP_TIME_US = 10 * 1000; + const uint32_t DH_TYPE_SIZE = 10; + const DHType dhTypeFuzz[DH_TYPE_SIZE] = { + DHType::CAMERA, DHType::AUDIO, DHType::SCREEN, DHType::VIRMODEM_MIC, + DHType::INPUT, DHType::A2D, DHType::GPS, DHType::HFP, DHType::VIRMODEM_SPEAKER + }; +} + +void VersioninfoManagerFuzzTest(const uint8_t* data, size_t size) +{ + if ((data == nullptr) || (size <= 0)) { + return; + } + + VersionInfo versionInfo; + versionInfo.deviceId = std::string(reinterpret_cast(data), size); + versionInfo.dhVersion = std::string(reinterpret_cast(data), size); + + CompVersion compVer; + compVer.dhType = dhTypeFuzz[data[0] % DH_TYPE_SIZE]; + compVer.name = std::string(reinterpret_cast(data), size); + compVer.handlerVersion = std::string(reinterpret_cast(data), size); + compVer.sourceVersion = std::string(reinterpret_cast(data), size); + compVer.sinkVersion = std::string(reinterpret_cast(data), size); + versionInfo.compVersions.insert(std::pair(compVer.dhType, compVer)); + + VersionInfo info; + VersionInfoManager::GetInstance()->AddVersion(versionInfo); + VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(versionInfo.deviceId, info); + VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(versionInfo.deviceId); + VersionInfoManager::GetInstance()->RemoveVersionInfoByDeviceId(versionInfo.deviceId); + + usleep(SLEEP_TIME_US); +} +} +} + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + /* Run your code on data */ + OHOS::DistributedHardware::VersioninfoManagerFuzzTest(data, size); + return 0; +} + diff --git a/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.h b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..241f550d51ed0b58f29965ea58ea4b88609ee5f2 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/fuzztest/versioninfomanager_fuzzer/versioninfomanager_fuzzer.h @@ -0,0 +1,22 @@ +/* + * 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 TEST_ACCESSMANAGER_FUZZER_H +#define TEST_ACCESSMANAGER_FUZZER_H + +#define FUZZ_PROJECT_NAME "versioninfomanager_fuzzer" + +#endif + diff --git a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn index def0e61e6ed2f35d37c6b2cdb39e3e2bbb18126b..eb44ec1b2828ec989195af6e00761070263fc733 100644 --- a/services/distributedhardwarefwkservice/test/unittest/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 @@ -14,5 +14,17 @@ group("test") { testonly = true - deps = [ "common/accessmanager:AccessManagerTest" ] + deps = [ + "common/accessmanager:AccessManagerTest", + "common/componentloader:component_loader_test", + "common/componentmanager:component_manager_test", + "common/dbadapter:db_adapter_test", + "common/distributedhardwaremanager:distributed_hardware_manager_test", + "common/distributedhardwareservice:distributed_hardware_service_test", + "common/localhardwaremanager:local_hardware_manager_test", + "common/resourcemanager:resource_manager_test", + "common/task:dh_task_test", + "common/versioninfomanager:versioninfo_manager_test", + "common/versionmanager:version_manager_test", + ] } diff --git a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn index 0720b0e01f84918f1acc21fb26bd0314e8bed8c0..212af2c63f5f234b3972e867f354bab2e92e300e 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/BUILD.gn @@ -19,7 +19,10 @@ module_out_path = "distributed_hardware_fwk/access_manager_test" config("module_private_config") { include_dirs = [ - "//utils/native/base/include", + "//base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include", + "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", + "//base/notification/eventhandler/interfaces/inner_api", + "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp/include", "//foundation/distributedhardware/device_manager/common/include", @@ -28,7 +31,9 @@ config("module_private_config") { "${common_path}/utils/include", "${common_path}/log/include", "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/utils", "${services_path}/distributedhardwarefwkservice/include/accessmanager", + "${utils_path}/include/eventbus", ] } @@ -48,7 +53,6 @@ ohos_unittest("AccessManagerTest") { "${utils_path}:distributedhardwareutils", "//foundation/distributedhardware/device_manager/interfaces/inner_kits/native_cpp:devicemanagersdk", "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", - "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", "//third_party/googletest:gtest_main", ] defines = [ @@ -57,16 +61,7 @@ ohos_unittest("AccessManagerTest") { "LOG_DOMAIN=0xD004100", ] - external_deps = [ - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "init:libbegetutil", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] + external_deps = [ "init:libbegetutil" ] } group("unittest") { diff --git a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp index a42e67448454fc74fdc693868ff09798aeb6c4cc..49e18d06c57663990a400b1a8e74fc6c178098eb 100644 --- a/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/accessmanager/access_manager_test.cpp @@ -104,12 +104,12 @@ HWTEST_F(AccessManagerTest, SendOnLineEvent_002, TestSize.Level1) auto ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent("", TEST_DEVICES[0].second, TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY, ret); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, ret); ASSERT_TRUE(DistributedHardwareManagerFactory::GetInstance().IsInit()); ret = DistributedHardwareManagerFactory::GetInstance().SendOnLineEvent(TEST_DEVICES[0].first, "", TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY, ret); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, ret); ASSERT_TRUE(DistributedHardwareManagerFactory::GetInstance().IsInit()); } @@ -150,12 +150,12 @@ HWTEST_F(AccessManagerTest, SendOffLineEvent_002, TestSize.Level1) auto ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent("", TEST_DEVICES[0].second, TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY, ret); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, ret); ASSERT_TRUE(DistributedHardwareManagerFactory::GetInstance().IsInit()); ret = DistributedHardwareManagerFactory::GetInstance().SendOffLineEvent(TEST_DEVICES[0].first, "", TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY, ret); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, ret); ASSERT_TRUE(DistributedHardwareManagerFactory::GetInstance().IsInit()); } @@ -225,5 +225,16 @@ HWTEST_F(AccessManagerTest, SendOffLineEvent_003, TestSize.Level0) for_each(threadVec.begin(), threadVec.end(), [](std::thread &t) { t.join(); }); } + +/** + * @tc.name: AccessManagerInit + * @tc.desc: Verify the Init function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(AccessManagerTest, AccessManagerInit, TestSize.Level0) +{ + EXPECT_EQ(DH_FWK_SUCCESS, AccessManager::GetInstance()->Init()); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/BUILD.gn similarity index 75% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn rename to services/distributedhardwarefwkservice/test/unittest/common/componentloader/BUILD.gn index e83d680cff7148867f9c23ea1722c695849dd476..55056e875c9ec4d555568ce47e33f0beb93a6b9e 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/BUILD.gn @@ -22,13 +22,16 @@ config("module_private_config") { include_dirs = [ "include", "${utils_path}/include", + "${utils_path}/include/eventbus", "${utils_path}/include/log", - "${services_path}/distributedhardwarefwkserviceimpl/include", - "${services_path}/distributedhardwarefwkserviceimpl/include/versionmanager", - "${services_path}/distributedhardwarefwkserviceimpl/include/componentloader", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/componentloader", + "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", + "//third_party/json/include", ] } @@ -40,12 +43,10 @@ ohos_unittest("ComponentLoaderTest") { configs = [ ":module_private_config" ] deps = [ - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", "//third_party/googletest:gtest_main", ] - external_deps = [ "hisysevent_native:libhisysevent" ] - defines = [ "HI_LOG_ENABLE", "DH_LOG_TAG=\"ComponentLoaderTest\"", diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/include/component_loader_test.h b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/include/component_loader_test.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/include/component_loader_test.h rename to services/distributedhardwarefwkservice/test/unittest/common/componentloader/include/component_loader_test.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/src/component_loader_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp similarity index 92% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/src/component_loader_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp index 6e7c858910da915d3b6766544a661100ac7e5cdb..7838556d0c6294349c6a13ea90677f705f28f816 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentloader/src/component_loader_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentloader/src/component_loader_test.cpp @@ -73,7 +73,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_002, TestSize.Level0) } /** - * @tc.name: component_loader_test_004 + * @tc.name: component_loader_test_003 * @tc.desc: Verify the GetSource function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -89,7 +89,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_003, TestSize.Level0) } /** - * @tc.name: component_loader_test_005 + * @tc.name: component_loader_test_004 * @tc.desc: Verify the GetSink function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -105,7 +105,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_004, TestSize.Level0) } /** - * @tc.name: component_loader_test_006 + * @tc.name: component_loader_test_005 * @tc.desc: Verify the ReleaseHardwareHandler function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -120,7 +120,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_005, TestSize.Level0) } /** - * @tc.name: component_loader_test_007 + * @tc.name: component_loader_test_006 * @tc.desc: Verify the ReleaseSource function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -135,7 +135,7 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_006, TestSize.Level0) } /** - * @tc.name: component_loader_test_008 + * @tc.name: component_loader_test_007 * @tc.desc: Verify the ReleaseSink function. * @tc.type: FUNC * @tc.require: AR000GHSK3 @@ -148,5 +148,17 @@ HWTEST_F(ComponentLoaderTest, component_loader_test_007, TestSize.Level0) EXPECT_TRUE(ComponentLoader::GetInstance().compHandlerMap_[iter.first].sinkHandler == nullptr); } } + +/** + * @tc.name: component_loader_test_008 + * @tc.desc: Verify the GetAllCompTypes function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(ComponentLoaderTest, component_loader_test_008, TestSize.Level0) +{ + auto vec = ComponentLoader::GetInstance().GetAllCompTypes(); + EXPECT_EQ(vec.size(), ComponentLoader::GetInstance().compHandlerMap_.size()); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/BUILD.gn similarity index 66% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn rename to services/distributedhardwarefwkservice/test/unittest/common/componentmanager/BUILD.gn index f37e19bdc2fd2da700f7b2872bed6e3347a433cb..ac41e4393966126cbeb8feeea26f980b24e27fcd 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/BUILD.gn @@ -20,15 +20,23 @@ module_out_path = "distributed_hardware_fwk/component_manager_test" config("module_private_config") { visibility = [ ":*" ] include_dirs = [ + "//base/notification/eventhandler/interfaces/inner_api", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core/include", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", "include", "${utils_path}/include", "${utils_path}/include/log", - "${services_path}/distributedhardwarefwkserviceimpl/include", - "${services_path}/distributedhardwarefwkserviceimpl/include/componentmanager", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/componentloader", + "${services_path}/distributedhardwarefwkservice/include/componentmanager", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", + "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", - "//utils/native/base/include", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", ] } @@ -40,7 +48,7 @@ ohos_unittest("ComponentManagerTest") { configs = [ ":module_private_config" ] deps = [ - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", @@ -51,11 +59,6 @@ ohos_unittest("ComponentManagerTest") { "DH_LOG_TAG=\"ComponentManagerTest\"", "LOG_DOMAIN=0xD004100", ] - - external_deps = [ - "hisysevent_native:libhisysevent", - "ipc:ipc_core", - ] } group("component_manager_test") { diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/include/component_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/include/component_manager_test.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/include/component_manager_test.h rename to services/distributedhardwarefwkservice/test/unittest/common/componentmanager/include/component_manager_test.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/include/mock_idistributed_hardware_sink.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/include/mock_idistributed_hardware_sink.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/include/mock_idistributed_hardware_sink.h rename to services/distributedhardwarefwkservice/test/unittest/common/componentmanager/include/mock_idistributed_hardware_sink.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/include/mock_idistributed_hardware_source.h b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/include/mock_idistributed_hardware_source.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/include/mock_idistributed_hardware_source.h rename to services/distributedhardwarefwkservice/test/unittest/common/componentmanager/include/mock_idistributed_hardware_source.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/src/component_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/src/component_manager_test.cpp similarity index 70% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/src/component_manager_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/componentmanager/src/component_manager_test.cpp index b279b5063d7615ff245a75ce7a6670ca4b92b108..a09736fa7048bfe2009a2d9814c98ff9b5d12ae5 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/componentmanager/src/component_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/componentmanager/src/component_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-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 @@ -16,6 +16,9 @@ #include "component_manager_test.h" #include +#include +#include +#include #include #include @@ -23,14 +26,21 @@ #include "component_disable.h" #include "component_enable.h" +#include "component_loader.h" +#include "capability_info.h" +#include "capability_info_manager.h" #define private public #include "component_manager.h" #undef private #include "constants.h" +#include "dh_context.h" +#include "dh_utils_tool.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" #include "mock_idistributed_hardware_sink.h" #include "mock_idistributed_hardware_source.h" +#include "version_info_manager.h" +#include "version_manager.h" using namespace testing::ext; @@ -39,13 +49,39 @@ namespace DistributedHardware { #undef DH_LOG_TAG #define DH_LOG_TAG "ComponentManagerTest" +namespace { constexpr int32_t EXECUTE_TIME_TEST = 1000; +constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11; +const std::string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/"; +const std::string NAME_CAMERA = "distributed_camera"; +const std::string VERSION_1 = "1.0"; const std::string DEV_ID_TEST = "123456"; const std::string DH_ID_TEST = "Camera_0"; +const std::string NETWORK_TEST = "nt36a637105409e904d4da83790a4a8"; +const std::string UUID_TEST = "bb536a637105409e904d4da78290ab1"; +const std::string DH_ATTR_1 = "attr1"; +const std::string DEV_NAME = "Dev1"; +const std::string DH_ID_1 = "Camera_1"; +const std::shared_ptr CAP_INFO_1 = + std::make_shared(DH_ID_1, GetDeviceIdByUUID(UUID_TEST), DEV_NAME, + TEST_DEV_TYPE_PAD, DHType::CAMERA, DH_ATTR_1); +} -void ComponentManagerTest::SetUpTestCase(void) {} +void ComponentManagerTest::SetUpTestCase(void) +{ + auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret != 0) { + DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} -void ComponentManagerTest::TearDownTestCase(void) {} +void ComponentManagerTest::TearDownTestCase(void) +{ + auto ret = remove(DATABASE_DIR.c_str()); + if (ret != 0) { + DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} void ComponentManagerTest::SetUp() { @@ -152,12 +188,12 @@ HWTEST_F(ComponentManagerTest, init_test_003, TestSize.Level0) ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::CAMERA, &cameraSource)); EXPECT_CALL(cameraSource, InitSource(testing::_)).Times(1).WillOnce(testing::Invoke(handler)); - MockIDistributedHardwareSource speakerSource; - ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::SPEAKER, &speakerSource)); - EXPECT_CALL(speakerSource, InitSource(testing::_)).Times(1).WillOnce(testing::Invoke(handler)); - + MockIDistributedHardwareSource displaySource; + ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::SCREEN, &displaySource)); + EXPECT_CALL(displaySource, InitSource(testing::_)).Times(1).WillOnce(testing::Invoke(handler)); + MockIDistributedHardwareSink micSink; - ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::MIC, &micSink)); + ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::AUDIO, &micSink)); EXPECT_CALL(micSink, InitSink(testing::_)).Times(1).WillOnce(testing::Invoke(handler)); auto start = std::chrono::system_clock::now(); @@ -187,12 +223,12 @@ HWTEST_F(ComponentManagerTest, unInit_test_001, TestSize.Level0) ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::CAMERA, &cameraSource)); EXPECT_CALL(cameraSource, ReleaseSource()).Times(1).WillOnce(testing::Invoke(handler)); - MockIDistributedHardwareSink speakerSink; - ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::SPEAKER, &speakerSink)); - EXPECT_CALL(speakerSink, ReleaseSink()).Times(1).WillOnce(testing::Invoke(handler)); - + MockIDistributedHardwareSink displaycSink; + ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::SCREEN, &displaycSink)); + EXPECT_CALL(displaycSink, ReleaseSink()).Times(1).WillOnce(testing::Invoke(handler)); + MockIDistributedHardwareSink micSink; - ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::MIC, &micSink)); + ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::AUDIO, &micSink)); EXPECT_CALL(micSink, ReleaseSink()).Times(1).WillOnce(testing::Invoke(handler)); auto start = std::chrono::system_clock::now(); @@ -339,5 +375,94 @@ HWTEST_F(ComponentManagerTest, disable_test_004, TestSize.Level0) thread5.join(); thread6.join(); } + +/** + * @tc.name: init_compSource_test_001 + * @tc.desc: Verify the InitCompSource + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, init_compSource_test_001, TestSize.Level0) +{ + ComponentLoader::GetInstance().Init(); + ComponentManager::GetInstance().compSource_.clear(); + auto ret = ComponentManager::GetInstance().InitCompSource(); + EXPECT_NE(ret, ComponentManager::GetInstance().compSource_.empty()); +} + +/** + * @tc.name: init_compSink_test_001 + * @tc.desc: Verify the InitCompSource + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, init_compSink_test_001, TestSize.Level0) +{ + ComponentLoader::GetInstance().Init(); + ComponentManager::GetInstance().compSink_.clear(); + auto ret = ComponentManager::GetInstance().InitCompSink(); + EXPECT_NE(ret, ComponentManager::GetInstance().compSink_.empty()); +} + +/** + * @tc.name: get_enableparam_test_001 + * @tc.desc: Verify the GetEnableParam + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, get_enableparam_test_001, TestSize.Level0) +{ + DHContext::GetInstance().AddOnlineDevice(NETWORK_TEST, UUID_TEST); + + CapabilityInfoManager::GetInstance()->Init(); + std::vector> resInfos { CAP_INFO_1 }; + CapabilityInfoManager::GetInstance()->AddCapability(resInfos); + + CompVersion compVersions1 = { + .name = NAME_CAMERA, + .dhType = DHType::CAMERA, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + VersionInfo verInfo1; + verInfo1.deviceId = GetDeviceIdByUUID(UUID_TEST); + verInfo1.dhVersion = VERSION_1; + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + VersionInfoManager::GetInstance()->Init(); + VersionInfoManager::GetInstance()->AddVersion(verInfo1); + + EnableParam param; + auto ret = ComponentManager::GetInstance().GetEnableParam(NETWORK_TEST, UUID_TEST, + DH_ID_1, DHType::CAMERA, param); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: get_sinkversionfromvermgr_test_001 + * @tc.desc: Verify the GetSinkVersionFromVerMgr + * @tc.type: FUNC + * @tc.require: AR000GHSK9 + */ +HWTEST_F(ComponentManagerTest, get_sinkversion_fromvermgr_test_001, TestSize.Level0) +{ + CompVersion compVersions1 = { + .name = NAME_CAMERA, + .dhType = DHType::CAMERA, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + DHVersion dhVersion; + dhVersion.uuid = UUID_TEST; + dhVersion.dhVersion = VERSION_1; + dhVersion.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + VersionManager::GetInstance().AddDHVersion(UUID_TEST, dhVersion); + std::string sinkVersion; + auto ret = ComponentManager::GetInstance().GetSinkVersionFromVerMgr(UUID_TEST, DHType::CAMERA, sinkVersion); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8418a17d321fe14c85a5874bbb08cd0a549ba3a4 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/BUILD.gn @@ -0,0 +1,64 @@ +# 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/db_adapter_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "//base/notification/eventhandler/interfaces/inner_api", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "include", + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//commonlibrary/c_utils/base/include", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", + "//utils/system/safwk/native/include", + ] +} + +## UnitTest db_adapter_test +ohos_unittest("DbAdapterTest") { + module_out_path = module_out_path + + sources = [ + "src/db_adapter_test.cpp", + "src/mock_db_change_listener.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ "c_utils:utils" ] +} + +group("db_adapter_test") { + testonly = true + deps = [ ":DbAdapterTest" ] +} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/db_adapter_test.h b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/db_adapter_test.h new file mode 100644 index 0000000000000000000000000000000000000000..e344d5763878c885254fef333b61828d6e7ee272 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/db_adapter_test.h @@ -0,0 +1,32 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_DB_ADAPTER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_DB_ADAPTER_TEST_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class DbAdapterTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/mock_db_change_listener.h b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/mock_db_change_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..d4e5628f6f18c11343e5803b1ef603bfb5410ecb --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/include/mock_db_change_listener.h @@ -0,0 +1,33 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_MOCK_DB_CHANGE_LISTENER_H +#define OHOS_DISTRIBUTED_HARDWARE_MOCK_DB_CHANGE_LISTENER_H + +#include "distributed_kv_data_manager.h" +#include "kvstore_observer.h" + +namespace OHOS { +namespace DistributedHardware { +class MockDBChangeListener : public DistributedKv::KvStoreObserver { +public: + MockDBChangeListener() = default; + virtual ~MockDBChangeListener() = default; + + void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/db_adapter_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/db_adapter_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17387888d32bd87cd7012827e3d0aa0c16e99a95 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/db_adapter_test.cpp @@ -0,0 +1,239 @@ +/* + * 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. + */ + +#include "db_adapter_test.h" + +#include +#include +#include +#include + +#include "capability_info.h" +#include "constants.h" +#define private public +#include "db_adapter.h" +#undef private +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#include "mock_db_change_listener.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "DbAdapterTest" + +namespace { +const string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/"; +const string TEST_DEV_ID_0 = "bb536a637105409e904d4da83791aa11"; +const string TEST_DEV_ID_1 = "bb536a637105409e904d4da83791bb22"; +const string TEST_DEV_ID_2 = "bb536a637105409e904d4da83791bb33"; +const string TEST_DEV_NAME = "Dev1"; +const string TEST_DH_ID_0 = "Camera_00"; +const string TEST_DH_ID_1 = "Mic_01"; +const string TEST_DH_ID_2 = "Gps_02"; +const string TEST_DH_ID_3 = "Display_03"; +const string TEST_DH_ID_4 = "Input_04"; +const string TEST_DH_ATTR_0 = "db_test_attr0"; +const string TEST_DH_ATTR_1 = "db_test_attr1"; +constexpr uint16_t TEST_DEV_TYPE = 0x11; + +const shared_ptr CAP_INFO_0 = + make_shared(TEST_DH_ID_0, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::CAMERA, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_1 = + make_shared(TEST_DH_ID_1, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::AUDIO, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_2 = + make_shared(TEST_DH_ID_2, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::GPS, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_3 = + make_shared(TEST_DH_ID_3, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::HFP, TEST_DH_ATTR_0); +const shared_ptr CAP_INFO_4 = + make_shared(TEST_DH_ID_4, TEST_DEV_ID_0, DEV_NAME, TEST_DEV_TYPE, DHType::INPUT, TEST_DH_ATTR_0); + +const shared_ptr CAP_INFO_5 = + make_shared(TEST_DH_ID_0, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::CAMERA, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_6 = + make_shared(TEST_DH_ID_1, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::AUDIO, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_7 = + make_shared(TEST_DH_ID_2, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::GPS, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_8 = + make_shared(TEST_DH_ID_3, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::HFP, TEST_DH_ATTR_1); +const shared_ptr CAP_INFO_9 = + make_shared(TEST_DH_ID_4, TEST_DEV_ID_1, DEV_NAME, TEST_DEV_TYPE, DHType::INPUT, TEST_DH_ATTR_1); + +std::shared_ptr g_dbAdapterPtr; +} + +void DbAdapterTest::SetUpTestCase(void) +{ + auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret != 0) { + DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } + + std::shared_ptr changeListener = std::make_shared(); + g_dbAdapterPtr = std::make_shared(APP_ID, GLOBAL_CAPABILITY_ID, changeListener); + if (g_dbAdapterPtr != nullptr) { + g_dbAdapterPtr->Init(); + } +} + +void DbAdapterTest::TearDownTestCase(void) +{ + if (g_dbAdapterPtr != nullptr) { + g_dbAdapterPtr->UnInit(); + } + + g_dbAdapterPtr = nullptr; + + auto ret = remove(DATABASE_DIR.c_str()); + if (ret != 0) { + DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} + +void DbAdapterTest::SetUp() +{ +} + +void DbAdapterTest::TearDown() +{ +} + +/** + * @tc.name: db_adapter_test_000 + * @tc.desc: Verify the Init function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(DbAdapterTest, db_adapter_test_000, TestSize.Level0) +{ + EXPECT_EQ(g_dbAdapterPtr->Init(), DH_FWK_SUCCESS); +} + +/** + * @tc.name: db_adapter_test_001 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(DbAdapterTest, db_adapter_test_001, TestSize.Level0) +{ + vector> resInfos { CAP_INFO_0, CAP_INFO_1, CAP_INFO_2, CAP_INFO_3, CAP_INFO_4, + CAP_INFO_5, CAP_INFO_6, CAP_INFO_7, CAP_INFO_8, CAP_INFO_9 }; + + std::vector keys; + std::vector values; + std::string key; + for (auto &resInfo : resInfos) { + key = resInfo->GetKey(); + keys.push_back(key); + values.push_back(resInfo->ToJsonString()); + } + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), DH_FWK_SUCCESS); + for (auto &resInfo : resInfos) { + g_dbAdapterPtr->RemoveDataByKey(resInfo->GetKey()); + } +} + +/** + * @tc.name: db_adapter_test_002 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_002, TestSize.Level0) +{ + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector values { TEST_DH_ATTR_0 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), DH_FWK_SUCCESS); + g_dbAdapterPtr->RemoveDataByKey(keys[0]); +} + +/** + * @tc.name: db_adapter_test_003 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_003, TestSize.Level0) +{ + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector valuesEmpty; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, valuesEmpty), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_004 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_004, TestSize.Level0) +{ + std::vector keysEmpty; + std::vector values { TEST_DH_ATTR_0 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keysEmpty, values), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_005 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_005, TestSize.Level0) +{ + std::vector keysEmpty; + std::vector valuesEmpty; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keysEmpty, valuesEmpty), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_006 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_006, TestSize.Level0) +{ + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector values { TEST_DH_ATTR_0, TEST_DH_ATTR_1 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), ERR_DH_FWK_PARA_INVALID); +} + +/** + * @tc.name: db_adapter_test_007 + * @tc.desc: Verify the PutDataBatch function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(DbAdapterTest, db_adapter_test_007, TestSize.Level0) +{ + g_dbAdapterPtr->kvStoragePtr_ = nullptr; + std::vector keys { std::string(TEST_DEV_ID_2 + TEST_DH_ID_0) }; + std::vector values { TEST_DH_ATTR_0 }; + + EXPECT_EQ(g_dbAdapterPtr->PutDataBatch(keys, values), ERR_DH_FWK_RESOURCE_KV_STORAGE_POINTER_NULL); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/mock_db_change_listener.cpp b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/mock_db_change_listener.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fa4895f89ef16fa42cb26c431b9790954f74e40a --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/dbadapter/src/mock_db_change_listener.cpp @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#include "mock_db_change_listener.h" + +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +class DBAdapter; +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "MockDBChangeListener" + +void MockDBChangeListener::OnChange(const DistributedKv::ChangeNotification &changeNotification) +{ + (void)changeNotification; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/BUILD.gn similarity index 67% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn rename to services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/BUILD.gn index 035bc692c97122a1b27131aba15e3045a477119d..ecf7d4aa0414c2733a07baa62227bbc8ee87c389 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 @@ -20,29 +20,30 @@ module_out_path = "distributed_hardware_fwk/dh_manager_test" config("module_private_config") { visibility = [ ":*" ] include_dirs = [ + "//base/notification/eventhandler/interfaces/inner_api", "include", "${utils_path}/include", "${utils_path}/include/log", "${utils_path}/include/eventbus", "${common_path}/log/include", "${common_path}/utils/include", - "${services_path}/distributedhardwarefwkserviceimpl/include", - "${services_path}/distributedhardwarefwkserviceimpl/include/task", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", - "//utils/native/base/include", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/utils", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", ] } ohos_unittest("DistributedHardwareManagerTest") { module_out_path = module_out_path - sources = [ "distributed_hardware_manager_test.cpp" ] + sources = [ "src/distributed_hardware_manager_test.cpp" ] configs = [ ":module_private_config" ] deps = [ - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", - "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", "//third_party/googletest:gmock_main", "//third_party/googletest:gtest_main", ] @@ -52,14 +53,6 @@ ohos_unittest("DistributedHardwareManagerTest") { "DH_LOG_TAG=\"DistributedHardwareManagerTest\"", "LOG_DOMAIN=0xD004100", ] - - external_deps = [ - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "dsoftbus:softbus_client", - "eventhandler:libeventhandler", - "hiviewdfx_hilog_native:libhilog", - ] } group("distributed_hardware_manager_test") { diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/distributed_hardware_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/include/distributed_hardware_manager_test.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/distributed_hardware_manager_test.h rename to services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/include/distributed_hardware_manager_test.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/distributed_hardware_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/src/distributed_hardware_manager_test.cpp similarity index 93% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/distributed_hardware_manager_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/src/distributed_hardware_manager_test.cpp index 34c6bf24d53163da9f24f1826f6d0e47916ecbf5..dcf836cc134cded7f0fa736ab6cdedefea1e0ac3 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/distributed_hardware_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwaremanager/src/distributed_hardware_manager_test.cpp @@ -80,7 +80,7 @@ HWTEST_F(DistributedHardwareManagerTest, sendOnLineEvent_test_002, TestSize.Leve onlineResult = DistributedHardwareManager::GetInstance().SendOnLineEvent(TEST_DEVICES[0].first, TEST_DEVICES[0].second, TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_ONLINE, onlineResult); + ASSERT_EQ(DH_FWK_SUCCESS, onlineResult); ASSERT_EQ(DH_FWK_SUCCESS, TaskBoard::GetInstance().WaitForALLTaskFinish()); ASSERT_TRUE(TaskBoard::GetInstance().IsAllTaskFinish()); @@ -95,11 +95,11 @@ HWTEST_F(DistributedHardwareManagerTest, sendOnLineEvent_test_002, TestSize.Leve HWTEST_F(DistributedHardwareManagerTest, sendOnLineEvent_test_003, TestSize.Level0) { auto onlineResult = DistributedHardwareManager::GetInstance().SendOnLineEvent("", "", TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY, onlineResult); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, onlineResult); onlineResult = DistributedHardwareManager::GetInstance().SendOnLineEvent(TEST_DEVICES[0].first, "", TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY, onlineResult); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, onlineResult); ASSERT_EQ(DH_FWK_SUCCESS, TaskBoard::GetInstance().WaitForALLTaskFinish()); ASSERT_TRUE(TaskBoard::GetInstance().IsAllTaskFinish()); @@ -143,7 +143,7 @@ HWTEST_F(DistributedHardwareManagerTest, sendOffLineEvent_test_002, TestSize.Lev offlineResult = DistributedHardwareManager::GetInstance().SendOffLineEvent(TEST_DEVICES[0].first, TEST_DEVICES[0].second, TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_HARDWARE_MANAGER_DEVICE_REPEAT_OFFLINE, offlineResult); + ASSERT_EQ(DH_FWK_SUCCESS, offlineResult); ASSERT_EQ(DH_FWK_SUCCESS, TaskBoard::GetInstance().WaitForALLTaskFinish()); ASSERT_TRUE(TaskBoard::GetInstance().IsAllTaskFinish()); @@ -159,11 +159,11 @@ HWTEST_F(DistributedHardwareManagerTest, sendOffLineEvent_test_003, TestSize.Lev { auto offlineResult = DistributedHardwareManager::GetInstance().SendOffLineEvent("", "", TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_NETWORK_ID_IS_EMPTY, offlineResult); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, offlineResult); offlineResult = DistributedHardwareManager::GetInstance().SendOffLineEvent(TEST_DEVICES[0].first, "", TEST_DEV_TYPE_PAD); - ASSERT_EQ(ERR_DH_FWK_REMOTE_DEVICE_ID_IS_EMPTY, offlineResult); + ASSERT_EQ(ERR_DH_FWK_PARA_INVALID, offlineResult); ASSERT_EQ(DH_FWK_SUCCESS, TaskBoard::GetInstance().WaitForALLTaskFinish()); ASSERT_TRUE(TaskBoard::GetInstance().IsAllTaskFinish()); diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..66965cdaa9ed73c427d7b368f369d914e89e3edb --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/BUILD.gn @@ -0,0 +1,67 @@ +# 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/distributed_hardware_service_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${common_path}/log/include", + "${common_path}/utils/include", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/utils", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", + "//base/notification/eventhandler/interfaces/inner_api", + ] +} + +ohos_unittest("DistributedHardwareServiceTest") { + module_out_path = module_out_path + + sources = [ "src/distributed_hardware_service_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DistributedHardwareServiceTest\"", + "LOG_DOMAIN=0xD004100", + ] + + external_deps = [ + "c_utils:utils", + "ipc:ipc_core", + "safwk:system_ability_fwk", + ] +} + +group("distributed_hardware_service_test") { + testonly = true + deps = [ ":DistributedHardwareServiceTest" ] +} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h new file mode 100644 index 0000000000000000000000000000000000000000..44ebcd936c51cb59dbe15293ff711e2f66df8b24 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/distributed_hardware_service_test.h @@ -0,0 +1,32 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_SERVICE_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_SERVICE_TEST_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class DistributedHardwareServiceTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/mock_publisher_listener.h b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/mock_publisher_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..985317c0e3e7ca3422ae72a6f77102cbdfa5183f --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/include/mock_publisher_listener.h @@ -0,0 +1,40 @@ +/* + * 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 OHOS_DISTRIBUTED_MOCK_PUBLISHER_LISTENER_H +#define OHOS_DISTRIBUTED_MOCK_PUBLISHER_LISTENER_H + +#include "ipublisher_listener.h" + +namespace OHOS { +namespace DistributedHardware { +class MockPublisherListener : public IPublisherListener { +public: + MockPublisherListener() = default; + ~MockPublisherListener() = default; + void OnMessage(const DHTopic topic, const std::string& message) + { + (void) topic; + (void) message; + } + + sptr AsObject() + { + return nullptr; + } +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2e6e5fe95a7b5b57ef20d6d45faef12d91485147 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/distributedhardwareservice/src/distributed_hardware_service_test.cpp @@ -0,0 +1,140 @@ +/* + * 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. + */ + +#include "distributed_hardware_service_test.h" + +#include +#include +#include + +#include "constants.h" +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" +#define protected public +#define private public +#include "distributed_hardware_service.h" +#include "distributed_hardware_manager.h" +#include "task_board.h" +#undef private +#undef protected +#include "mock_publisher_listener.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +namespace { + const int32_t ASID = 4801; + const DHTopic TOPIC = DHTopic::TOPIC_START_DSCREEN; + sptr g_listener = sptr(new MockPublisherListener()); + + const std::u16string ARGS_H = u"-h"; + const std::u16string ARGS_L = u"-l"; + const std::u16string ARGS_E = u"-e"; + const std::u16string ARGS_T = u"-t"; + const std::u16string ARGS_C = u"-c"; +} +void DistributedHardwareServiceTest::SetUpTestCase(void) {} + +void DistributedHardwareServiceTest::TearDownTestCase(void) {} + +void DistributedHardwareServiceTest::SetUp() {} + +void DistributedHardwareServiceTest::TearDown() {} + +/** + * @tc.name: register_publisher_listener_001 + * @tc.desc: Verify the RegisterPublisherListener function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, register_publisher_listener_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + auto ret = service.RegisterPublisherListener(TOPIC, g_listener); + + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: unregister_publisher_listener_001 + * @tc.desc: Verify the UnregisterPublisherListener function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, unregister_publisher_listener_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + service.RegisterPublisherListener(TOPIC, g_listener); + auto ret = service.UnregisterPublisherListener(TOPIC, g_listener); + + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: publish_message_001 + * @tc.desc: Verify the PublishMessage function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, publish_message_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + std::string msg; + service.RegisterPublisherListener(TOPIC, g_listener); + auto ret = service.PublishMessage(TOPIC, msg); + + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} + +/** + * @tc.name: onStop_test_002 + * @tc.desc: Verify the OnStop function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, onStop_test_002, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + service.OnStop(); + + EXPECT_EQ(service.state_, ServiceRunningState::STATE_NOT_START); +} + +/** + * @tc.name: dump_test_001 + * @tc.desc: Verify the Dump function + * @tc.type: FUNC + * @tc.require: AR000GHSJM + */ +HWTEST_F(DistributedHardwareServiceTest, dump_test_001, TestSize.Level0) +{ + DistributedHardwareService service(ASID, true); + int32_t fd = 1; + + auto ret = service.Dump(fd, std::vector { ARGS_H }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_L }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_E }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_T }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); + ret = service.Dump(fd, std::vector { ARGS_C }); + EXPECT_EQ(ret, DH_FWK_SUCCESS); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..37505648a53f9db71e152b7e54bd7a42a66fdba9 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/BUILD.gn @@ -0,0 +1,71 @@ +# 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/local_hardware_manager_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${utils_path}/include", + "${utils_path}/include/eventbus", + "${utils_path}/include/log", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/componentloader", + "${services_path}/distributedhardwarefwkservice/include/localhardwaremanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${common_path}/utils/include", + "${common_path}/log/include", + "//third_party/json/include", + ] +} + +ohos_unittest("LocalHardwareManagerTest") { + module_out_path = module_out_path + + sources = [ + "src/local_hardware_manager_test.cpp", + "src/mock_hardware_handler.cpp", + "src/plugin_listener_impl_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "eventhandler:libeventhandler", + "hisysevent_native:libhisysevent", + "kv_store:distributeddata_inner", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"LocalHardwareManagerTest\"", + "LOG_DOMAIN=0xD004100", + ] +} + +group("local_hardware_manager_test") { + testonly = true + deps = [ ":LocalHardwareManagerTest" ] +} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/local_hardware_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/local_hardware_manager_test.h new file mode 100644 index 0000000000000000000000000000000000000000..57de623904e83813760faf251a43c3b582bc2da0 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/local_hardware_manager_test.h @@ -0,0 +1,32 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_COMPONENT_LOADER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_COMPONENT_LOADER_TEST_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class LocalHardwareManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/mock_hardware_handler.h b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/mock_hardware_handler.h new file mode 100644 index 0000000000000000000000000000000000000000..7b59c15c127ded1c3f0194937b798aad0dfd92d3 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/mock_hardware_handler.h @@ -0,0 +1,44 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_MCOK_HARDWARE_HANDLER_H +#define OHOS_DISTRIBUTED_HARDWARE_MCOK_HARDWARE_HANDLER_H + +#include + +#include "ihardware_handler.h" + +namespace OHOS { +namespace DistributedHardware { +class MockHardwareHandler : public IHardwareHandler { +public: + MockHardwareHandler() = default; + virtual ~MockHardwareHandler() = default; + + int32_t Initialize() override; + std::vector Query() override; + std::map QueryExtraInfo() override; + bool IsSupportPlugin() override; + void RegisterPluginListener(std::shared_ptr listener) override; + void UnRegisterPluginListener() override; + + int32_t PluginHardware(const std::string &dhId, const std::string &attr); + int32_t UnPluginHardware(const std::string &dhId); + + std::shared_ptr listener_ = nullptr; + std::vector> listenerVec; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/plugin_listener_impl_test.h b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/plugin_listener_impl_test.h new file mode 100644 index 0000000000000000000000000000000000000000..a6d34db69cb1cee862b64045674b93ac02e98147 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/include/plugin_listener_impl_test.h @@ -0,0 +1,36 @@ +/* + * 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 OHOS_DISTRIBUTED_PLUGIN_LISTENER_IMPL_TEST_H +#define OHOS_DISTRIBUTED_PLUGIN_LISTENER_IMPL_TEST_H + +#include + +#define private public +#include "plugin_listener_impl.h" +#undef private + +namespace OHOS { +namespace DistributedHardware { +class PluginListenerImplTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/local_hardware_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/local_hardware_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..deccf44c60c46e87ce0a841ff4da4b8e7ab33673 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/local_hardware_manager_test.cpp @@ -0,0 +1,64 @@ +/* + * 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. + */ + +#include "local_hardware_manager_test.h" + +#define private public +#include "capability_info_manager.h" +#include "component_loader.h" +#include "local_hardware_manager.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void LocalHardwareManagerTest::SetUpTestCase(void) +{ + ComponentLoader::GetInstance().Init(); +} + +void LocalHardwareManagerTest::TearDownTestCase(void) +{ + ComponentLoader::GetInstance().UnInit(); +} + +void LocalHardwareManagerTest::SetUp() {} + +void LocalHardwareManagerTest::TearDown() {} + +/** + * @tc.name: local_hardware_manager_test_001 + * @tc.desc: Verify the Init function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(LocalHardwareManagerTest, local_hardware_manager_test_001, TestSize.Level0) +{ + LocalHardwareManager::GetInstance().Init(); +} + +/** + * @tc.name: local_hardware_manager_test_002 + * @tc.desc: Verify the UnInit function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(LocalHardwareManagerTest, local_hardware_manager_test_002, TestSize.Level0) +{ + LocalHardwareManager::GetInstance().UnInit(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/mock_hardware_handler.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/mock_hardware_handler.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b94efa681455cb3a645ae91da77211ecc5635ca5 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/mock_hardware_handler.cpp @@ -0,0 +1,70 @@ +/* + * 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. + */ + +#include "mock_hardware_handler.h" + +#include "distributed_hardware_errno.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t MockHardwareHandler::Initialize() +{ + return DH_FWK_SUCCESS; +} + +std::vector MockHardwareHandler::Query() +{ + DHItem item; + return std::vector { item }; +} + +std::map MockHardwareHandler::QueryExtraInfo() +{ + std::map extraInfo; + return extraInfo; +} + +bool MockHardwareHandler::IsSupportPlugin() +{ + return true; +} + +void MockHardwareHandler::RegisterPluginListener(std::shared_ptr listener) +{ + listener_ = listener; +} + +void MockHardwareHandler::UnRegisterPluginListener() +{ + listener_ = nullptr; +} + +int32_t MockHardwareHandler::PluginHardware(const std::string &dhId, const std::string &attr) +{ + if (listener_ != nullptr) { + listener_->PluginHardware(dhId, attr); + } + return DH_FWK_SUCCESS; +} + +int32_t MockHardwareHandler::UnPluginHardware(const std::string &dhId) +{ + if (listener_ != nullptr) { + listener_->UnPluginHardware(dhId); + } + return DH_FWK_SUCCESS; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/plugin_listener_impl_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/plugin_listener_impl_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4a26a0413af6bb2b44d18eca98b3ce792105262e --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/localhardwaremanager/src/plugin_listener_impl_test.cpp @@ -0,0 +1,69 @@ +/* + * 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. + */ + +#include "plugin_listener_impl_test.h" + +#include "distributed_hardware_errno.h" +#include "mock_hardware_handler.h" + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +namespace { +const std::string dhId = "00000000000000000"; +const std::string attrs = "11111111111111111"; +std::shared_ptr g_listener; +std::shared_ptr g_mockHardwareHandler; +} + +void PluginListenerImplTest::SetUpTestCase(void) +{ + DHType dhType = DHType::CAMERA; + g_listener = std::make_shared(dhType); + g_mockHardwareHandler = std::make_shared(); +} + +void PluginListenerImplTest::TearDownTestCase(void) {} + +void PluginListenerImplTest::SetUp() {} + +void PluginListenerImplTest::TearDown() {} + +/** + * @tc.name: plugin_listener_impl_test_001 + * @tc.desc: Verify the PluginHardware function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(PluginListenerImplTest, plugin_listener_impl_test_001, TestSize.Level0) +{ + g_mockHardwareHandler->RegisterPluginListener(g_listener); + EXPECT_EQ(g_mockHardwareHandler->PluginHardware(dhId, attrs), DH_FWK_SUCCESS); +} + +/** + * @tc.name: plugin_listener_impl_test_002 + * @tc.desc: Verify the UnPluginHardware function. + * @tc.type: FUNC + * @tc.require: AR000GHSK3 + */ +HWTEST_F(PluginListenerImplTest, plugin_listener_impl_test_002, TestSize.Level0) +{ + EXPECT_EQ(g_mockHardwareHandler->UnPluginHardware(dhId), DH_FWK_SUCCESS); + g_mockHardwareHandler->UnRegisterPluginListener(); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn similarity index 71% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/BUILD.gn rename to services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn index 14d15c23a98ff67d1a4ffa8a8ca770554b4b37b7..ecc7de1ae1e017aeb27ff8d82eb4f19093ff50a3 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/BUILD.gn @@ -20,18 +20,20 @@ module_out_path = "distributed_hardware_fwk/resource_manager_test" config("module_private_config") { visibility = [ ":*" ] include_dirs = [ + "//base/notification/eventhandler/interfaces/inner_api", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", "include", "${utils_path}/include", "${utils_path}/include/log", "${utils_path}/include/eventbus", - "${services_path}/distributedhardwarefwkserviceimpl/include", - "${services_path}/distributedhardwarefwkserviceimpl/include/resourcemanager", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", - "//utils/native/base/include", + "//commonlibrary/c_utils/base/include", "//third_party/json/include", - "//utils/native/base/include", + "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", ] } @@ -45,20 +47,12 @@ ohos_unittest("ResourceManagerTest") { configs = [ ":module_private_config" ] deps = [ - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", "//third_party/googletest:gtest_main", - "//utils/native/base:utils", ] - external_deps = [ - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "distributeddatamgr:distributeddata_inner", - "eventhandler:libeventhandler", - "hisysevent_native:libhisysevent", - "hiviewdfx_hilog_native:libhilog", - ] + external_deps = [ "c_utils:utils" ] } group("resource_manager_test") { diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/include/resource_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/include/resource_manager_test.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/include/resource_manager_test.h rename to services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/include/resource_manager_test.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/src/resource_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp similarity index 94% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/src/resource_manager_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp index de1d68a30154d660ab0137aa441269b26468cb69..c37786b31762904316ecd17e61b008ec538c6a71 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/resourcemanager/src/resource_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/resourcemanager/src/resource_manager_test.cpp @@ -15,6 +15,9 @@ #include "resource_manager_test.h" +#include +#include +#include #include #include "capability_info.h" @@ -34,12 +37,13 @@ namespace DistributedHardware { #define DH_LOG_TAG "ResourceManagerTest" namespace { +const string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/"; const string DEV_ID_0 = "bb536a637105409e904d4da83790a4a7"; const string DEV_ID_1 = "bb536a637105409e904d4da83790a4a8"; const string DEV_NAME = "Dev1"; const string DH_ID_0 = "Camera_0"; const string DH_ID_1 = "Mic_0"; -const string DH_ID_2 = "Speaker_0"; +const string DH_ID_2 = "Gps_0"; const string DH_ID_3 = "Display_0"; const string DH_ID_4 = "Input_0"; const string DH_ATTR_0 = "attr0"; @@ -47,7 +51,7 @@ const string DH_ATTR_1 = "attr1"; constexpr uint16_t TEST_DEV_TYPE_PAD = 0x11; constexpr uint32_t TEST_DH_TYPE_CAMERA = 0x01; constexpr uint32_t TEST_DH_TYPE_MIC = 0x02; -constexpr uint32_t TEST_DH_TYPE_SPEAKER = 0x4; +constexpr uint32_t TEST_DH_TYPE_GPS = 0x10; constexpr uint32_t TEST_DH_TYPE_DISPLAY = 0x08; constexpr uint32_t TEST_DH_TYPE_BUTTON = 0x20; constexpr uint32_t TEST_SIZE_0 = 0; @@ -59,29 +63,41 @@ const std::string EMPTY_PREFIX = ""; const shared_ptr CAP_INFO_0 = make_shared(DH_ID_0, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::CAMERA, DH_ATTR_0); const shared_ptr CAP_INFO_1 = - make_shared(DH_ID_1, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::MIC, DH_ATTR_0); + make_shared(DH_ID_1, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::AUDIO, DH_ATTR_0); const shared_ptr CAP_INFO_2 = - make_shared(DH_ID_2, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::SPEAKER, DH_ATTR_0); + make_shared(DH_ID_2, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::GPS, DH_ATTR_0); const shared_ptr CAP_INFO_3 = - make_shared(DH_ID_3, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::DISPLAY, DH_ATTR_0); + make_shared(DH_ID_3, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::SCREEN, DH_ATTR_0); const shared_ptr CAP_INFO_4 = make_shared(DH_ID_4, DEV_ID_0, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::INPUT, DH_ATTR_0); const shared_ptr CAP_INFO_5 = make_shared(DH_ID_0, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::CAMERA, DH_ATTR_1); const shared_ptr CAP_INFO_6 = - make_shared(DH_ID_1, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::MIC, DH_ATTR_1); + make_shared(DH_ID_1, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::AUDIO, DH_ATTR_1); const shared_ptr CAP_INFO_7 = - make_shared(DH_ID_2, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::SPEAKER, DH_ATTR_1); + make_shared(DH_ID_2, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::GPS, DH_ATTR_1); const shared_ptr CAP_INFO_8 = - make_shared(DH_ID_3, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::DISPLAY, DH_ATTR_1); + make_shared(DH_ID_3, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::SCREEN, DH_ATTR_1); const shared_ptr CAP_INFO_9 = make_shared(DH_ID_4, DEV_ID_1, DEV_NAME, TEST_DEV_TYPE_PAD, DHType::INPUT, DH_ATTR_1); } -void ResourceManagerTest::SetUpTestCase(void) {} +void ResourceManagerTest::SetUpTestCase(void) +{ + auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret != 0) { + DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} -void ResourceManagerTest::TearDownTestCase(void) {} +void ResourceManagerTest::TearDownTestCase(void) +{ + auto ret = remove(DATABASE_DIR.c_str()); + if (ret != 0) { + DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} void ResourceManagerTest::SetUp() { @@ -227,7 +243,7 @@ HWTEST_F(ResourceManagerTest, resource_manager_test_008, TestSize.Level0) map queryMap10 { { CapabilityInfoFilter::FILTER_DH_TYPE, to_string(TEST_DH_TYPE_MIC) } }; map queryMap11 { { CapabilityInfoFilter::FILTER_DH_TYPE, - to_string(TEST_DH_TYPE_SPEAKER) } }; + to_string(TEST_DH_TYPE_GPS) } }; map queryMap12 { { CapabilityInfoFilter::FILTER_DH_TYPE, to_string(TEST_DH_TYPE_DISPLAY) } }; map queryMap13 { { CapabilityInfoFilter::FILTER_DH_TYPE, diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn similarity index 81% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn rename to services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn index bbddc9ad8a451a49fdf8bfba0fcf1c4f8bb1ef1c..c837f278eedbcbd3dc21ccd08a3f5d5fd039d4fe 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-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 @@ -21,14 +21,15 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ "include", + "//third_party/json/include", "${utils_path}/include", "${utils_path}/include/log", - "${services_path}/distributedhardwarefwkserviceimpl/include", - "${services_path}/distributedhardwarefwkserviceimpl/include/task", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/task", + "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", - "//utils/native/base/include", + "//commonlibrary/c_utils/base/include", ] } @@ -48,7 +49,7 @@ ohos_unittest("DHTaskTest") { configs = [ ":module_private_config" ] deps = [ - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", "//third_party/googletest:gtest_main", ] diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_disable_task.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_disable_task.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_disable_task.h rename to services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_disable_task.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_enable_task.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_enable_task.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_enable_task.h rename to services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_enable_task.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_offline_task.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_offline_task.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_offline_task.h rename to services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_offline_task.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_online_task.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_online_task.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_online_task.h rename to services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_online_task.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_task_factory.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_task_factory.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_task_factory.h rename to services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_task_factory.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_task_utils.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_task_utils.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/mock_task_utils.h rename to services/distributedhardwarefwkservice/test/unittest/common/task/include/mock_task_utils.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/task_test.h b/services/distributedhardwarefwkservice/test/unittest/common/task/include/task_test.h similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/include/task_test.h rename to services/distributedhardwarefwkservice/test/unittest/common/task/include/task_test.h diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_disable_task.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_disable_task.cpp similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_disable_task.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_disable_task.cpp diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_enable_task.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_enable_task.cpp similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_enable_task.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_enable_task.cpp diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_offline_task.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_offline_task.cpp similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_offline_task.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_offline_task.cpp diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_online_task.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_online_task.cpp similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_online_task.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_online_task.cpp diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_task_factory.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_task_factory.cpp similarity index 100% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/mock_task_factory.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/task/src/mock_task_factory.cpp diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/task_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/task/src/task_test.cpp similarity index 93% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/task_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/task/src/task_test.cpp index 6a473835b21140494e9c122559ded861c7bac356..4e648fd86ee821e72f1edc8f34cc41780e24a611 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/task/src/task_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/task/src/task_test.cpp @@ -38,17 +38,17 @@ namespace { const std::string DEV_ID_1 = "bb536a637105409e904d4da83790a4a8"; const std::string DEV_NETWORK_ID_1 = "nt36a637105409e904d4da83790a4a8"; const MockDevInfo DEV_INFO_11 = { DEV_NETWORK_ID_1, DEV_ID_1, "Camera_1", DHType::CAMERA }; -const MockDevInfo DEV_INFO_12 = { DEV_NETWORK_ID_1, DEV_ID_1, "Speaker_1", DHType::SPEAKER }; -const MockDevInfo DEV_INFO_13 = { DEV_NETWORK_ID_1, DEV_ID_1, "Mic_1", DHType::MIC }; -const MockDevInfo DEV_INFO_14 = { DEV_NETWORK_ID_1, DEV_ID_1, "Display_1", DHType::DISPLAY }; +const MockDevInfo DEV_INFO_12 = { DEV_NETWORK_ID_1, DEV_ID_1, "Gps_1", DHType::GPS }; +const MockDevInfo DEV_INFO_13 = { DEV_NETWORK_ID_1, DEV_ID_1, "Audio_1", DHType::AUDIO }; +const MockDevInfo DEV_INFO_14 = { DEV_NETWORK_ID_1, DEV_ID_1, "Display_1", DHType::SCREEN }; const MockDevInfo DEV_INFO_15 = { DEV_NETWORK_ID_1, DEV_ID_1, "Input_1", DHType::INPUT }; const std::string DEV_ID_2 = "06d177ffa09543389f3b445b4722f9be"; const std::string DEV_NETWORK_ID_2 = "ntd177ffa09543389f3b445b4722f9be"; const MockDevInfo DEV_INFO_21 = { DEV_NETWORK_ID_2, DEV_ID_2, "Camera_1", DHType::CAMERA }; -const MockDevInfo DEV_INFO_22 = { DEV_NETWORK_ID_2, DEV_ID_2, "Speaker_1", DHType::SPEAKER }; -const MockDevInfo DEV_INFO_23 = { DEV_NETWORK_ID_2, DEV_ID_2, "Mic_1", DHType::MIC }; -const MockDevInfo DEV_INFO_24 = { DEV_NETWORK_ID_2, DEV_ID_2, "Display_1", DHType::DISPLAY }; +const MockDevInfo DEV_INFO_22 = { DEV_NETWORK_ID_2, DEV_ID_2, "Gps_1", DHType::GPS }; +const MockDevInfo DEV_INFO_23 = { DEV_NETWORK_ID_2, DEV_ID_2, "Audio_1", DHType::AUDIO }; +const MockDevInfo DEV_INFO_24 = { DEV_NETWORK_ID_2, DEV_ID_2, "Display_1", DHType::SCREEN }; const MockDevInfo DEV_INFO_25 = { DEV_NETWORK_ID_2, DEV_ID_2, "Input_1", DHType::INPUT }; const TaskParam TASK_PARAM_1 = { diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c2562bac7cb81cacdabbfd82e9779d018a61ed35 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/BUILD.gn @@ -0,0 +1,64 @@ +# 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. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") + +module_out_path = "distributed_hardware_fwk/versioninfo_manager_test" + +config("module_private_config") { + visibility = [ ":*" ] + include_dirs = [ + "include", + "${utils_path}/include", + "${utils_path}/include/log", + "${utils_path}/include/eventbus", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/utils", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", + "${common_path}/utils/include", + "${common_path}/log/include", + "//base/notification/eventhandler/interfaces/inner_api", + "//commonlibrary/c_utils/base/include", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", + "//third_party/json/include", + "//utils/system/safwk/native/include", + ] +} + +## UnitTest versioninfo_manager_test +ohos_unittest("VersioninfoManagerTest") { + module_out_path = module_out_path + + sources = [ + "src/version_info_manager_test.cpp", + "src/version_info_test.cpp", + ] + + configs = [ ":module_private_config" ] + + deps = [ + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", + "//foundation/distributedhardware/distributed_hardware_fwk/utils:distributedhardwareutils", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ "c_utils:utils" ] +} + +group("versioninfo_manager_test") { + testonly = true + deps = [ ":VersioninfoManagerTest" ] +} diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h new file mode 100644 index 0000000000000000000000000000000000000000..b2c20ca5534f73e3b2c0a73183aab3239e513161 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_manager_test.h @@ -0,0 +1,35 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_TEST_H + +#include +#include + +#include "version_info.h" + +namespace OHOS { +namespace DistributedHardware { +class VersionInfoManagerTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_test.h new file mode 100644 index 0000000000000000000000000000000000000000..3e49b244555f9e198a282c98f999cd87b92af30d --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/include/version_info_test.h @@ -0,0 +1,33 @@ +/* + * 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 OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_TEST_H +#define OHOS_DISTRIBUTED_HARDWARE_VERSION_INFO_MANAGER_TEST_H + +#include +#include + +namespace OHOS { +namespace DistributedHardware { +class VersionInfoTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9716e056c5e32c041e1bd3a39de265c1915f1b0 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_manager_test.cpp @@ -0,0 +1,200 @@ +/* + * 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. + */ + +#include "version_info_manager_test.h" + +#include +#include +#include + +#define private public +#include "version_info_manager.h" +#include "version_manager.h" +#undef private +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfoManagerTest" + +namespace { +const string DATABASE_DIR = "/data/service/el1/public/database/dtbhardware_manager_service/"; +const string DEV_ID_1 = "bb536a637105409e904d4da83790a4a7"; +const string DEV_ID_2 = "bb536a637105409e904d4da83790a4a8"; +const string DEV_ID_3 = "bb536a637105409e904d4da83790a4a9"; +const string NAME_SCREEN = "distributed_screen"; +const string NAME_CAMERA = "distributed_camera"; +const string NAME_INPUT = "distributed_input"; +const string VERSION_1 = "1.0"; +const string VERSION_2 = "2.0"; +const string VERSION_3 = "3.0"; +std::vector g_versionInfos; +} + +std::vector CreateVersionInfos() +{ + CompVersion compVersions1 = { + .name = NAME_CAMERA, + .dhType = DHType::CAMERA, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + + CompVersion compVersions2 = { + .name = NAME_SCREEN, + .dhType = DHType::SCREEN, + .handlerVersion = VERSION_2, + .sourceVersion = VERSION_2, + .sinkVersion = VERSION_2 + }; + + CompVersion compVersions3 = { + .name = NAME_INPUT, + .dhType = DHType::INPUT, + .handlerVersion = VERSION_3, + .sourceVersion = VERSION_3, + .sinkVersion = VERSION_3 + }; + + VersionInfo verInfo1; + verInfo1.deviceId = DEV_ID_1; + verInfo1.dhVersion = VERSION_1; + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + VersionInfo verInfo2; + verInfo2.deviceId = DEV_ID_2; + verInfo1.dhVersion = VERSION_2; + verInfo1.compVersions.insert(std::pair(compVersions2.dhType, compVersions2)); + verInfo1.compVersions.insert(std::pair(compVersions2.dhType, compVersions2)); + verInfo1.compVersions.insert(std::pair(compVersions2.dhType, compVersions2)); + + VersionInfo verInfo3; + verInfo3.deviceId = DEV_ID_3; + verInfo1.dhVersion = VERSION_3; + verInfo1.compVersions.insert(std::pair(compVersions3.dhType, compVersions3)); + verInfo1.compVersions.insert(std::pair(compVersions3.dhType, compVersions3)); + verInfo1.compVersions.insert(std::pair(compVersions3.dhType, compVersions3)); + + return std::vector { verInfo1, verInfo2, verInfo3 }; +} + +void VersionInfoManagerTest::SetUpTestCase(void) +{ + auto ret = mkdir(DATABASE_DIR.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (ret != 0) { + DHLOGE("mkdir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } + + g_versionInfos = CreateVersionInfos(); +} + +void VersionInfoManagerTest::TearDownTestCase(void) +{ + auto ret = remove(DATABASE_DIR.c_str()); + if (ret != 0) { + DHLOGE("remove dir failed, path: %s, errno : %d", DATABASE_DIR.c_str(), errno); + } +} + +void VersionInfoManagerTest::SetUp() +{ +} + +void VersionInfoManagerTest::TearDown() +{ +} + +/** + * @tc.name:version_info_manager_test_001 + * @tc.desc: Verify the VersionInfoManager Init function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_001, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->Init(), DH_FWK_SUCCESS); +} + +/** + * @tc.name:version_info_manager_test_002 + * @tc.desc: Verify the VersionInfoManager SyncRemoteVersionInfos function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_002, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->SyncRemoteVersionInfos(), DH_FWK_SUCCESS); +} + +/** + * @tc.name:version_info_manager_test_003 + * @tc.desc: Verify the VersionInfoManager AddVersion function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_004, TestSize.Level0) +{ + for (const auto& verInfo : g_versionInfos) { + EXPECT_EQ(VersionInfoManager::GetInstance()->AddVersion(verInfo), DH_FWK_SUCCESS); + } +} + +/** + * @tc.name:version_info_manager_test_005 + * @tc.desc: Verify the VersionInfoManager GetVersionInfoByDeviceId function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_005, TestSize.Level0) +{ + VersionInfo versionInfo; + for (const auto& verInfo : g_versionInfos) { + EXPECT_EQ(VersionInfoManager::GetInstance()->GetVersionInfoByDeviceId(verInfo.deviceId, versionInfo), + DH_FWK_SUCCESS); + } +} + +/** + * @tc.name:version_info_manager_test_006 + * @tc.desc: Verify the VersionInfoManager SyncVersionInfoFromDB function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_006, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->SyncVersionInfoFromDB(DEV_ID_1), DH_FWK_SUCCESS); +} + +/** + * @tc.name:version_info_manager_test_007 + * @tc.desc: Verify the VersionInfoManager UnInit function. + * @tc.type: FUNC + * @tc.require: AR000GHSJE + */ +HWTEST_F(VersionInfoManagerTest, version_info_manager_test_007, TestSize.Level0) +{ + EXPECT_EQ(VersionInfoManager::GetInstance()->UnInit(), DH_FWK_SUCCESS); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2c73db990d46c3da56a06d62c0d105e77e99d233 --- /dev/null +++ b/services/distributedhardwarefwkservice/test/unittest/common/versioninfomanager/src/version_info_test.cpp @@ -0,0 +1,90 @@ +/* + * 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. + */ + +#include "version_info_test.h" + +#include +#include +#include + +#define private public +#include "version_info_manager.h" +#include "version_manager.h" +#include "version_info.h" +#undef private +#include "dh_context.h" +#include "distributed_hardware_errno.h" +#include "distributed_hardware_log.h" + +using namespace testing::ext; +using namespace std; +namespace OHOS { +namespace DistributedHardware { +#undef DH_LOG_TAG +#define DH_LOG_TAG "VersionInfoTest" + +namespace { +const string DEV_ID_1 = "bb536a637105409e904d4da83790a4a7"; +const string NAME_SCREEN = "distributed_screen"; +const string VERSION_1 = "1.0"; +} + +void VersionInfoTest::SetUpTestCase(void) +{ +} + +void VersionInfoTest::TearDownTestCase(void) +{ +} + +void VersionInfoTest::SetUp() +{ +} + +void VersionInfoTest::TearDown() +{ +} + +/** + * @tc.name:version_info_test_001 + * @tc.desc: Verify the VersionInfoTest ToJson function. + * @tc.type: FUNC + * @tc.require: AR000GHSCV + */ +HWTEST_F(VersionInfoTest, version_info_test_001, TestSize.Level0) +{ + CompVersion compVersions1 = { + .name = NAME_SCREEN, + .dhType = DHType::SCREEN, + .handlerVersion = VERSION_1, + .sourceVersion = VERSION_1, + .sinkVersion = VERSION_1 + }; + + VersionInfo verInfo1; + verInfo1.deviceId = DEV_ID_1; + verInfo1.dhVersion = VERSION_1; + verInfo1.compVersions.insert(std::pair(compVersions1.dhType, compVersions1)); + + std::string jsonStr = verInfo1.ToJsonString(); + EXPECT_NE(jsonStr.empty(), true); + + VersionInfo verInfo2; + verInfo2.FromJsonString(jsonStr); + + EXPECT_EQ(verInfo2.ToJsonString(), jsonStr); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/BUILD.gn similarity index 66% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn rename to services/distributedhardwarefwkservice/test/unittest/common/versionmanager/BUILD.gn index 60f92bc841ef6708d23e55676944542d4fb7c442..d55ea1270fdb4bcc8afbd14b6b17b56c0b05f38d 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/BUILD.gn +++ b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# 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 @@ -20,16 +20,21 @@ module_out_path = "distributed_hardware_fwk/version_manager_test" config("module_private_config") { visibility = [ ":*" ] include_dirs = [ + "//base/notification/eventhandler/interfaces/inner_api", + "//foundation/distributeddatamgr/kv_store/interfaces/innerkits/distributeddata/include", "include", "${utils_path}/include", + "${utils_path}/include/eventbus", "${utils_path}/include/log", - "${services_path}/distributedhardwarefwkserviceimpl/include", - "${services_path}/distributedhardwarefwkserviceimpl/include/componentloader", - "${services_path}/distributedhardwarefwkserviceimpl/include/versionmanager", - "${services_path}/distributedhardwarefwkserviceimpl/include/utils", + "${services_path}/distributedhardwarefwkservice/include", + "${services_path}/distributedhardwarefwkservice/include/componentloader", + "${services_path}/distributedhardwarefwkservice/include/resourcemanager", + "${services_path}/distributedhardwarefwkservice/include/versionmanager", + "${services_path}/distributedhardwarefwkservice/include/utils", "${common_path}/utils/include", "${common_path}/log/include", - "//utils/native/base/include", + "//third_party/json/include", + "//commonlibrary/c_utils/base/include", ] } @@ -41,7 +46,7 @@ ohos_unittest("VersionManagerTest") { configs = [ ":module_private_config" ] deps = [ - "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkserviceimpl:distributedhardwarefwksvr_impl", + "//foundation/distributedhardware/distributed_hardware_fwk/services/distributedhardwarefwkservice:distributedhardwarefwksvr", "//third_party/googletest:gtest_main", ] diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/include/version_manager_test.h b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/include/version_manager_test.h similarity index 95% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/include/version_manager_test.h rename to services/distributedhardwarefwkservice/test/unittest/common/versionmanager/include/version_manager_test.h index ac3e715a9eb82f785e152ef7ca3ad55c352d30e2..652467dee4cc702166daa486d02c55c1f13f04d9 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/include/version_manager_test.h +++ b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/include/version_manager_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 diff --git a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/src/version_manager_test.cpp similarity index 93% rename from services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp rename to services/distributedhardwarefwkservice/test/unittest/common/versionmanager/src/version_manager_test.cpp index 874900c867654a98344e625b4d99ee7020f20567..4193cc1bd7bf96163bee2b6848965430fbcff0a1 100644 --- a/services/distributedhardwarefwkserviceimpl/test/unittest/common/versionmanager/src/version_manager_test.cpp +++ b/services/distributedhardwarefwkservice/test/unittest/common/versionmanager/src/version_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 @@ -52,7 +52,7 @@ const std::string TEST_SINK_VERSION_2 = "2.0"; const std::string TEST_SINK_VERSION_3 = "2.0"; const std::string TEST_DH_VERSION = "3.1"; const std::string TEST_COMPONENT_NAME_1 = "distributed_camera"; -const std::string TEST_COMPONENT_NAME_2 = "distributed_speaker"; +const std::string TEST_COMPONENT_NAME_2 = "distributed_audio"; const std::string TEST_COMPONENT_NAME_3 = "distributed_mic"; } @@ -91,10 +91,10 @@ HWTEST_F(VersionManagerTest, version_manager_test_002, TestSize.Level0) CompVersionGetValue(cVs1, TEST_COMPONENT_NAME_1, DHType::CAMERA, TEST_HANDLER_VERSION_1, TEST_SOURCE_VERSION_1, TEST_SINK_VERSION_1); CompVersion cVs2; - CompVersionGetValue(cVs2, TEST_COMPONENT_NAME_2, DHType::SPEAKER, TEST_HANDLER_VERSION_2, TEST_SOURCE_VERSION_2, + CompVersionGetValue(cVs2, TEST_COMPONENT_NAME_2, DHType::AUDIO, TEST_HANDLER_VERSION_2, TEST_SOURCE_VERSION_2, TEST_SINK_VERSION_2); CompVersion cVs3; - CompVersionGetValue(cVs3, TEST_COMPONENT_NAME_3, DHType::MIC, TEST_HANDLER_VERSION_3, TEST_SOURCE_VERSION_3, + CompVersionGetValue(cVs3, TEST_COMPONENT_NAME_3, DHType::SCREEN, TEST_HANDLER_VERSION_3, TEST_SOURCE_VERSION_3, TEST_SINK_VERSION_3); dhVersion.uuid = TEST_DEVICE_ID_1; dhVersion.dhVersion = TEST_DH_VERSION; @@ -131,7 +131,7 @@ HWTEST_F(VersionManagerTest, version_manager_test_004, TestSize.Level0) DHVersion dhVersion; int32_t ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_2, dhVersion); EXPECT_EQ(DH_FWK_SUCCESS, ret); - EXPECT_EQ(TEST_HANDLER_VERSION_2, dhVersion.compVersions[DHType::SPEAKER].handlerVersion); + EXPECT_EQ(TEST_HANDLER_VERSION_2, dhVersion.compVersions[DHType::AUDIO].handlerVersion); EXPECT_EQ(TEST_DH_VERSION, dhVersion.dhVersion); ret = VersionManager::GetInstance().GetDHVersion(TEST_DEVICE_ID_3, dhVersion); EXPECT_EQ(ERR_DH_FWK_VERSION_DEVICE_ID_NOT_EXIST, ret); diff --git a/services/distributedhardwarefwkserviceimpl/BUILD.gn b/services/distributedhardwarefwkserviceimpl/BUILD.gn deleted file mode 100644 index ce1c1fe1a85356d538efdd48cd6d6612ccf45d20..0000000000000000000000000000000000000000 --- a/services/distributedhardwarefwkserviceimpl/BUILD.gn +++ /dev/null @@ -1,98 +0,0 @@ -# Copyright (c) 2021-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. - -import("//build/ohos.gni") -import("//build/ohos_var.gni") -import( - "//foundation/distributedhardware/distributed_hardware_fwk/distributedhardwarefwk.gni") - -ohos_shared_library("distributedhardwarefwksvr_impl") { - include_dirs = [ - "//utils/native/base/include", - "//utils/system/safwk/native/include", - "//third_party/json/include", - "include", - "include/componentloader", - "include/versionmanager", - "include/componentmanager", - "include/task", - "include/utils", - "include/localhardwaremanager", - "include/resourcemanager", - "include/hidumphelper", - "${common_path}/log/include", - "${common_path}/utils/include", - "${utils_path}/include", - "${utils_path}/include/log", - "${utils_path}/include/eventbus", - ] - - sources = [ - "src/componentloader/component_loader.cpp", - "src/componentmanager/component_disable.cpp", - "src/componentmanager/component_enable.cpp", - "src/componentmanager/component_manager.cpp", - "src/distributed_hardware_manager.cpp", - "src/distributed_hardware_proxy.cpp", - "src/hidumphelper/enabled_comps_dump.cpp", - "src/hidumphelper/hidump_helper.cpp", - "src/localhardwaremanager/local_hardware_manager.cpp", - "src/localhardwaremanager/plugin_listener_impl.cpp", - "src/resourcemanager/capability_info.cpp", - "src/resourcemanager/capability_info_manager.cpp", - "src/resourcemanager/capability_utils.cpp", - "src/resourcemanager/db_adapter.cpp", - "src/task/disable_task.cpp", - "src/task/enable_task.cpp", - "src/task/offline_task.cpp", - "src/task/online_task.cpp", - "src/task/task.cpp", - "src/task/task_board.cpp", - "src/task/task_executor.cpp", - "src/task/task_factory.cpp", - "src/utils/dh_context.cpp", - "src/versionmanager/version_manager.cpp", - ] - - deps = [ - "${utils_path}:distributedhardwareutils", - "//foundation/ability/ability_runtime/interfaces/inner_api/ability_manager:ability_manager", - "//foundation/ability/ability_runtime/services/abilitymgr:abilityms", - "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", - "//utils/native/base:utils", - ] - - defines = [ - "HI_LOG_ENABLE", - "DH_LOG_TAG=\"dhfwksvr\"", - "LOG_DOMAIN=0xD004100", - ] - - external_deps = [ - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "distributeddatamgr:distributeddata_inner", - "eventhandler:libeventhandler", - "hisysevent_native:libhisysevent", - "hitrace_native:hitrace_meter", - "hiviewdfx_hilog_native:libhilog", - "init:libbegetutil", - "ipc:ipc_core", - "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", - ] - - subsystem_name = "distributedhardware" - - part_name = "distributed_hardware_fwk" -} diff --git a/utils/BUILD.gn b/utils/BUILD.gn index 4abac824e973eaffe933ec6803589a8794e11887..789d4a8047e5a6e1d64544c62fbd2e74fe0b462a 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -17,8 +17,10 @@ import( ohos_shared_library("distributedhardwareutils") { include_dirs = [ + "//base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include", + "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", "//third_party/openssl:libcrypto_static", - "//utils/native/base/include", + "//commonlibrary/c_utils/base/include", "//utils/system/safwk/native/include", "${common_path}/log/include", "${common_path}/utils/include", @@ -35,10 +37,7 @@ ohos_shared_library("distributedhardwareutils") { "src/log/dh_log.cpp", ] - deps = [ - "//third_party/openssl:libcrypto_static", - "//utils/native/base:utils", - ] + deps = [ "//third_party/openssl:libcrypto_static" ] defines = [ "HI_LOG_ENABLE", @@ -47,10 +46,8 @@ ohos_shared_library("distributedhardwareutils") { ] external_deps = [ - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", + "c_utils:utils", "dsoftbus:softbus_client", - "eventhandler:libeventhandler", "hisysevent_native:libhisysevent", "hitrace_native:hitrace_meter", "hiviewdfx_hilog_native:libhilog", diff --git a/utils/include/dh_utils_hisysevent.h b/utils/include/dh_utils_hisysevent.h index fcdfdf6b196c3ebf96dcd2f0c6b4d86b5c42fe4f..b8a0939e9506cebc7606bc36e7bffb13126ac15c 100644 --- a/utils/include/dh_utils_hisysevent.h +++ b/utils/include/dh_utils_hisysevent.h @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_DHUTIL_HISYSEVENT_H #define OHOS_DISTRIBUTED_HARDWARE_DHUTIL_HISYSEVENT_H +#include #include #include "hisysevent.h" diff --git a/utils/include/dh_utils_hitrace.h b/utils/include/dh_utils_hitrace.h index 502215443013275dc73642f6abf56395aec6ce73..1deb311f498d2c4d265abcf150be9841d54b088a 100644 --- a/utils/include/dh_utils_hitrace.h +++ b/utils/include/dh_utils_hitrace.h @@ -16,6 +16,7 @@ #ifndef OHOS_DH_HITRACE_H #define OHOS_DH_HITRACE_H +#include #include #include "device_type.h" diff --git a/utils/include/dh_utils_tool.h b/utils/include/dh_utils_tool.h index 77f60983d095cb79109bc02d6122edb31496c195..a81b74f02acc6a00f06940dee3d985ad6754ba05 100644 --- a/utils/include/dh_utils_tool.h +++ b/utils/include/dh_utils_tool.h @@ -17,7 +17,6 @@ #define OHOS_DISTRIBUTED_HARDWARE_DHUTILS_TOOL_H #include -#include #include "device_type.h" @@ -39,6 +38,8 @@ DeviceInfo GetLocalDeviceInfo(); /* Convert uuid to deviceId by sha256 encode */ std::string GetDeviceIdByUUID(const std::string &uuid); + +std::string Sha256(const std::string& string); } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/utils/include/eventbus/event_bus.h b/utils/include/eventbus/event_bus.h index 4a4840dc1b8275beed399381fe15d65a9133fc10..9e4f013e0ffe691bcbf444899a8742d7ab620bc3 100644 --- a/utils/include/eventbus/event_bus.h +++ b/utils/include/eventbus/event_bus.h @@ -16,8 +16,11 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_H #define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_H +#include #include #include +#include +#include #include #include @@ -45,14 +48,35 @@ public: EventBus() { ULOGI("ctor EventBus"); - if (!eventbusHandler_) { - auto busRunner = OHOS::AppExecFwk::EventRunner::Create("DHEventbusHandler"); - eventbusHandler_ = std::make_shared(busRunner); + if (eventbusHandler_ == nullptr) { + eventThread_ = std::thread(&EventBus::StartEvent, this); + std::unique_lock lock(eventMutex_); + eventCon_.wait(lock, [this] { + return eventbusHandler_ != nullptr; + }); } } + + EventBus(const std::string &threadName) + { + ULOGI("ctor EventBus threadName: %s", threadName.c_str()); + if (eventbusHandler_ == nullptr) { + eventThread_ = std::thread(&EventBus::StartEventWithName, this, threadName); + std::unique_lock lock(eventMutex_); + eventCon_.wait(lock, [this] { + return eventbusHandler_ != nullptr; + }); + } + } + ~EventBus() { ULOGI("dtor EventBus"); + if ((eventbusHandler_ != nullptr) && (eventbusHandler_->GetEventRunner() != nullptr)) { + eventbusHandler_->GetEventRunner()->Stop(); + } + eventThread_.join(); + eventbusHandler_ = nullptr; } template @@ -201,6 +225,29 @@ private: } } + void StartEvent() + { + auto busRunner = AppExecFwk::EventRunner::Create(false); + { + std::lock_guard lock(eventMutex_); + eventbusHandler_ = std::make_shared(busRunner); + } + eventCon_.notify_all(); + busRunner->Run(); + } + + void StartEventWithName(const std::string &threadName) + { + prctl(PR_SET_NAME, threadName.c_str()); + auto busRunner = AppExecFwk::EventRunner::Create(false); + { + std::lock_guard lock(eventMutex_); + eventbusHandler_ = std::make_shared(busRunner); + } + eventCon_.notify_all(); + busRunner->Run(); + } + private: std::shared_ptr eventbusHandler_; @@ -208,6 +255,9 @@ private: std::mutex handlerMtx; using TypeMap = std::unordered_map> *>; TypeMap handlers; + std::thread eventThread_; + std::condition_variable eventCon_; + std::mutex eventMutex_; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/src/anonymous_string.cpp b/utils/src/anonymous_string.cpp index 291fd9e9986a1b30dbc890d844ae6eae03b2d73f..be63436b0a5e215ef1bafa0961cdc43d77761258 100644 --- a/utils/src/anonymous_string.cpp +++ b/utils/src/anonymous_string.cpp @@ -15,6 +15,9 @@ #include "anonymous_string.h" +#include +#include + #include "securec.h" namespace OHOS { diff --git a/utils/src/dh_utils_hisysevent.cpp b/utils/src/dh_utils_hisysevent.cpp index aac98804e796d1d939086c8d96f935273ad6e043..499e00a160ab7184c13067b06ca44a1690a340f3 100644 --- a/utils/src/dh_utils_hisysevent.cpp +++ b/utils/src/dh_utils_hisysevent.cpp @@ -14,10 +14,8 @@ */ #include "dh_utils_hisysevent.h" -#include #include -#include "anonymous_string.h" #include "distributed_hardware_errno.h" #include "distributed_hardware_log.h" diff --git a/utils/src/dh_utils_hitrace.cpp b/utils/src/dh_utils_hitrace.cpp index 0c0d8cbf49b7917dce6d7f90b33e18289c1665be..3bc80bb3abf387282008399b4a98757e3f873318 100644 --- a/utils/src/dh_utils_hitrace.cpp +++ b/utils/src/dh_utils_hitrace.cpp @@ -14,7 +14,6 @@ */ #include "dh_utils_hitrace.h" -#include #include namespace OHOS { diff --git a/utils/src/dh_utils_tool.cpp b/utils/src/dh_utils_tool.cpp index dfc12b8bb7e066a799f8e05981a89aa76ca3e822..8b49ad54ff5594c193453dd6c0cd6104f26d0d01 100644 --- a/utils/src/dh_utils_tool.cpp +++ b/utils/src/dh_utils_tool.cpp @@ -15,12 +15,14 @@ #include "dh_utils_tool.h" +#include #include #include #include #include #include "openssl/sha.h" +#include "softbus_common.h" #include "softbus_bus_center.h" #include "constants.h" @@ -80,24 +82,33 @@ std::string GetUUIDBySoftBus(const std::string &networkId) std::string GetDeviceIdByUUID(const std::string &uuid) { - unsigned char hash[SHA256_DIGEST_LENGTH * 2 + 1] = {0}; + if (uuid.size() == 0 || uuid.size() > MAX_ID_LEN) { + DHLOGE("uuid is invalid!"); + return ""; + } + return Sha256(uuid); +} + +std::string Sha256(const std::string& in) +{ + unsigned char out[SHA256_DIGEST_LENGTH * 2 + 1] = {0}; SHA256_CTX ctx; SHA256_Init(&ctx); - SHA256_Update(&ctx, uuid.data(), uuid.size()); - SHA256_Final(&hash[SHA256_DIGEST_LENGTH], &ctx); + SHA256_Update(&ctx, in.data(), in.size()); + SHA256_Final(&out[SHA256_DIGEST_LENGTH], &ctx); // here we translate sha256 hash to hexadecimal. each 8-bit char will be presented by two characters([0-9a-f]) constexpr int32_t WIDTH = 4; constexpr unsigned char MASK = 0x0F; const char* hexCode = "0123456789abcdef"; constexpr int32_t DOUBLE_TIMES = 2; for (int32_t i = 0; i < SHA256_DIGEST_LENGTH; ++i) { - unsigned char value = hash[SHA256_DIGEST_LENGTH + i]; + unsigned char value = out[SHA256_DIGEST_LENGTH + i]; // uint8_t is 2 digits in hexadecimal. - hash[i * DOUBLE_TIMES] = hexCode[(value >> WIDTH) & MASK]; - hash[i * DOUBLE_TIMES + 1] = hexCode[value & MASK]; + out[i * DOUBLE_TIMES] = hexCode[(value >> WIDTH) & MASK]; + out[i * DOUBLE_TIMES + 1] = hexCode[value & MASK]; } - hash[SHA256_DIGEST_LENGTH * DOUBLE_TIMES] = 0; - return reinterpret_cast(hash); + out[SHA256_DIGEST_LENGTH * DOUBLE_TIMES] = 0; + return reinterpret_cast(out); } DeviceInfo GetLocalDeviceInfo() diff --git a/utils/test/fuzztest/utils_fuzzer/BUILD.gn b/utils/test/fuzztest/utils_fuzzer/BUILD.gn index fb65842d7f9525582d9a06d3b30716a0c7b537eb..57c7f42bce3afbe434e8ce60bda0d9c33f4cf26f 100644 --- a/utils/test/fuzztest/utils_fuzzer/BUILD.gn +++ b/utils/test/fuzztest/utils_fuzzer/BUILD.gn @@ -26,7 +26,7 @@ ohos_fuzztest("UtilsFuzzTest") { "${utils_path}/include", "${utils_path}/include/log", "${common_path}/utils/include", - "//utils/native/base/include", + "//commonlibrary/c_utils/base/include", ] cflags = [ "-g", diff --git a/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp b/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp index f76469e508b5b15525a714329150fcde1535575e..e1b65aa13b7a61c02ca47312baa1b526ced6d8ea 100644 --- a/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp +++ b/utils/test/fuzztest/utils_fuzzer/utils_fuzzer.cpp @@ -17,6 +17,7 @@ #include #include +#include #include "anonymous_string.h" #include "dh_utils_tool.h" diff --git a/utils/test/unittest/common/eventbus/BUILD.gn b/utils/test/unittest/common/eventbus/BUILD.gn index c85bc2c4a5317cd3991f7c073809518d25db2bc1..6025ffbf5710a677310a96ddce045907df89ea61 100644 --- a/utils/test/unittest/common/eventbus/BUILD.gn +++ b/utils/test/unittest/common/eventbus/BUILD.gn @@ -25,7 +25,7 @@ config("module_private_config") { "${utils_path}/include", "${utils_path}/include/log", "${utils_path}/include/eventbus", - "//utils/native/base/include", + "//commonlibrary/c_utils/base/include", ] } diff --git a/utils/test/unittest/common/eventbus/event_bus_test.cpp b/utils/test/unittest/common/eventbus/event_bus_test.cpp index d0015d046587b79878a0d7e4e3279bbca48e6b52..8ca543ca2708480dfaffcdf0b594cf29faf504e3 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.cpp +++ b/utils/test/unittest/common/eventbus/event_bus_test.cpp @@ -13,10 +13,16 @@ * limitations under the License. */ -#include "distributed_hardware_log.h" - #include "event_bus_test.h" +#include +#include +#include + +#include "distributed_hardware_log.h" +#include "event_bus.h" +#include "event_registration.h" + using namespace testing::ext; namespace OHOS { diff --git a/utils/test/unittest/common/eventbus/event_bus_test.h b/utils/test/unittest/common/eventbus/event_bus_test.h index f3435a529c4da32332e1961b2facee90a7b4d91f..3d953e75d55a6f156a8b29e3561b841dbf46391f 100644 --- a/utils/test/unittest/common/eventbus/event_bus_test.h +++ b/utils/test/unittest/common/eventbus/event_bus_test.h @@ -16,15 +16,11 @@ #ifndef OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H #define OHOS_DISTRIBUTED_HARDWARE_EVENT_BUS_TEST_H -#include +#include +#include #include -#include - -#include "event_handler.h" - #include "event.h" -#include "event_bus.h" #include "event_sender.h" #include "eventbus_handler.h" diff --git a/utils/test/unittest/common/utilstool/BUILD.gn b/utils/test/unittest/common/utilstool/BUILD.gn index 3287ca4337a5ea1cbc6c1e91f0ef9eb73cb99531..2371d935b35b2d8a5563dbbea693cd875d311a60 100644 --- a/utils/test/unittest/common/utilstool/BUILD.gn +++ b/utils/test/unittest/common/utilstool/BUILD.gn @@ -24,7 +24,7 @@ config("module_private_config") { "${utils_path}/include", "${utils_path}/include/log", "${common_path}/utils/include", - "//utils/native/base/include", + "//commonlibrary/c_utils/base/include", ] } diff --git a/utils/test/unittest/common/utilstool/utils_tool_test.cpp b/utils/test/unittest/common/utilstool/utils_tool_test.cpp index d83b3356d2b15fe2ee75e3ef5c3792dce1aa0361..b8981972b12e9b0bb773b8b36cd2d6321d97730a 100644 --- a/utils/test/unittest/common/utilstool/utils_tool_test.cpp +++ b/utils/test/unittest/common/utilstool/utils_tool_test.cpp @@ -16,7 +16,10 @@ #include "utils_tool_test.h" #include +#include +#include #include +#include #include #include "anonymous_string.h"