From 2c0ce266b7d7903802ae222b29e90429707926f9 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Tue, 25 Jul 2023 20:44:54 +0800 Subject: [PATCH 01/10] add inject event test Signed-off-by: wangxuanxuan --- test/BUILD.gn | 13 +- test/injecttest/client/BUILD.gn | 90 ++++++++ .../include/distributed_input_test_client.h | 31 +++ .../injecttest/client/include/socket_client.h | 53 +++++ .../src/distributed_input_test_client.cpp | 172 +++++++++++++++ test/injecttest/client/src/socket_client.cpp | 150 +++++++++++++ .../client/src/socket_main_test.cpp | 79 +++++++ test/injecttest/server/BUILD.gn | 92 ++++++++ .../server/include/dinput_dbg_itf.h | 39 ++++ .../include/distributed_input_test_server.h | 97 +++++++++ .../injecttest/server/include/socket_server.h | 60 +++++ test/injecttest/server/src/dinput_dbg_itf.cpp | 21 ++ .../src/distributed_input_test_server.cpp | 167 ++++++++++++++ .../server/src/socket_main_test.cpp | 74 +++++++ test/injecttest/server/src/socket_server.cpp | 205 ++++++++++++++++++ 15 files changed, 1342 insertions(+), 1 deletion(-) create mode 100644 test/injecttest/client/BUILD.gn create mode 100644 test/injecttest/client/include/distributed_input_test_client.h create mode 100644 test/injecttest/client/include/socket_client.h create mode 100644 test/injecttest/client/src/distributed_input_test_client.cpp create mode 100644 test/injecttest/client/src/socket_client.cpp create mode 100644 test/injecttest/client/src/socket_main_test.cpp create mode 100644 test/injecttest/server/BUILD.gn create mode 100644 test/injecttest/server/include/dinput_dbg_itf.h create mode 100644 test/injecttest/server/include/distributed_input_test_server.h create mode 100644 test/injecttest/server/include/socket_server.h create mode 100644 test/injecttest/server/src/dinput_dbg_itf.cpp create mode 100644 test/injecttest/server/src/distributed_input_test_server.cpp create mode 100644 test/injecttest/server/src/socket_main_test.cpp create mode 100644 test/injecttest/server/src/socket_server.cpp diff --git a/test/BUILD.gn b/test/BUILD.gn index d43e349..b752778 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2022 Huawei Device Co., Ltd. +# Copyright (c) 2021-2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -11,8 +11,19 @@ # See the License for the specific language governing permissions and # limitations under the License. +declare_args() { + distributedhardware_function_test = false +} + group("test") { testonly = true deps = [ "fuzztest:fuzztest" ] + + if (distributedhardware_function_test) { + deps += [ + "injecttest/server:distributed_input_inject_server_test", + "injecttest/client:distributed_input_inject_client_test", + ] + } } diff --git a/test/injecttest/client/BUILD.gn b/test/injecttest/client/BUILD.gn new file mode 100644 index 0000000..2495cb2 --- /dev/null +++ b/test/injecttest/client/BUILD.gn @@ -0,0 +1,90 @@ +# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_input/distributedinput.gni") + +module_out_path = "distributed_input/injecttest" + +## UnitTest distributed_input_inject_client_test {{{ +ohos_unittest("distributed_input_inject_client_test") { + module_out_path = module_out_path + + include_dirs = [ + "${distributedinput_path}/sourcehandler/include", + "${innerkits_path}/include", + "${innerkits_path}/src", + "${ipc_path}/include", + "${ipc_path}/src", + "${services_source_path}/sourcemanager/include", + "${frameworks_path}/include", + "${service_common}/include", + "${common_path}/include", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${distributedhardwarefwk_path}/utils/include/log", + "//third_party/json/include", + "${dfx_utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", + "${utils_path}/include", + "./include", + "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/sinkhandler/include", + ] + + sources = [ + "./src/socket_client.cpp", + "./src/socket_main_test.cpp", + "./src/distributed_input_test_client.cpp", + ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + "-Dprotected=public", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dinputtestsvr\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "${dfx_utils_path}:libdinput_dfx_utils", + "${innerkits_path}:libdinput_sdk", + "${utils_path}:libdinput_utils", + "//third_party/libevdev:libevdev", + "${distributedhardwarefwk_path}/utils:distributedhardwareutils", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "hisysevent:libhisysevent", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] +} +## UnitTest distributed_input_inject_client_test }}} diff --git a/test/injecttest/client/include/distributed_input_test_client.h b/test/injecttest/client/include/distributed_input_test_client.h new file mode 100644 index 0000000..0051939 --- /dev/null +++ b/test/injecttest/client/include/distributed_input_test_client.h @@ -0,0 +1,31 @@ +#ifndef DISTRIBUTED_INPUT_TEST_CLIENT_H +#define DISTRIBUTED_INPUT_TEST_CLIENT_H + +#include +#include "linux/uinput.h" + +#include "socket_client.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class DistributedInputTestClient { +public: + DistributedInputTestClient(std::string ip); + ~DistributedInputTestClient(); + void MakeVirtualEvent(); +private: + SocketClient socketCli; + bool SetPhys(const std::string deviceName); + bool InjectEvent(input_event event); + bool CreateKey(); + void ProcRes(bool result); +private: + struct uinput_user_dev dev_ {}; + bool injectResult_ = false; + int32_t fd_ = -1; +}; +} +} +} +#endif \ No newline at end of file diff --git a/test/injecttest/client/include/socket_client.h b/test/injecttest/client/include/socket_client.h new file mode 100644 index 0000000..220f271 --- /dev/null +++ b/test/injecttest/client/include/socket_client.h @@ -0,0 +1,53 @@ +#ifndef SOCKET_CLIENT_H +#define SOCKET_CLIENT_H + +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +using ResCallback = std::function; +class SocketClient { +public: + int32_t Init(std::string ip); + int32_t Release(); + int32_t PushRequest(std::string msg); + void ParseRecvSvrMsg(std::string msg); + void RegisterResCallback(ResCallback cb); +private: + void DoRecvSvrRsp(); + void SendRequest(); + void DoSendRequest(std::string msg); + +private: + int sock_cli; + fd_set rfds; + struct timeval tv; + int retval; + int maxfd; + +private: + std::mutex reqMtx_; + std::condition_variable reqQueueConVar_; + std::queue requestQueue_; + ResCallback resCallback_; +}; +} +} +} +#endif diff --git a/test/injecttest/client/src/distributed_input_test_client.cpp b/test/injecttest/client/src/distributed_input_test_client.cpp new file mode 100644 index 0000000..5d5fd6d --- /dev/null +++ b/test/injecttest/client/src/distributed_input_test_client.cpp @@ -0,0 +1,172 @@ +#include "distributed_input_test_client.h" + +#include +#include +#include +#include +#include + +#include "linux/input-event-codes.h" + +#include "idistributed_hardware_source.h" +#include "ipc_skeleton.h" +#include "nlohmann/json.hpp" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +#include "constants_dinput.h" +#include "dinput_errcode.h" +#include "distributed_hardware_log.h" +#include "softbus_bus_center.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +namespace { + const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; +} + +DistributedInputTestClient::DistributedInputTestClient(std::string ip) +{ + DHLOGI("Ctor DistributedInputTestClient ip: %s", ip.c_str()); + socketCli.Init(ip); + socketCli.RegisterResCallback(std::bind(&DistributedInputTestClient::ProcRes, this, std::placeholders::_1)); +} + +DistributedInputTestClient::~DistributedInputTestClient() +{ + DHLOGI("Dctor DistributedInputTestClient"); + socketCli.Release(); +} + +bool DistributedInputTestClient::SetPhys(const std::string deviceName) +{ + std::string phys; + phys.append(deviceName); + + if (ioctl(fd_, UI_SET_PHYS, phys.c_str()) < 0) { + return false; + } + return true; +} + +bool DistributedInputTestClient::CreateKey() +{ + const std::vector eventType{ EV_KEY, EV_REL, EV_MSC }; + for (auto it : eventType) { + if (ioctl(fd_, UI_SET_EVBIT, it) < 0) { + DHLOGE("CreateKye ioctl(fd_, UI_SET_EVBIT, it) failed!"); + } + } + + const std::vector keys { + BTN_LEFT, BTN_RIGHT, BTN_MIDDLE, BTN_SIDE, BTN_EXTRA, BTN_FORWARD, BTN_BACK, BTN_TASK }; + for (auto it : keys) { + if (ioctl(fd_, UI_SET_KEYBIT, it) < 0) { + DHLOGE("CreateKye ioctl(fd_, UI_SET_KEYBIT, it) failed!"); + } + } + + const std::vector rels { REL_X, REL_Y, REL_WHEEL, REL_WHEEL_HI_RES }; + for (auto it : rels) { + if (ioctl(fd_, UI_SET_RELBIT, it) < 0) { + DHLOGE("CreateKye ioctl(fd_, UI_SET_RELBIT, it) failed!"); + } + } + + if (ioctl(fd_, UI_SET_MSCBIT, MSC_SCAN) < 0) { + DHLOGE("CreateKye ioctl(fd_, UI_SET_MSCBIT, it) failed!"); + } + return true; +} + +bool DistributedInputTestClient::InjectEvent(input_event event) +{ + fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK); + if (fd_ < 0) { + DHLOGE("Failed to open uinput"); + return false; + } + + std::string deviceName_ = "virtual mouse"; + if (strncpy_s(dev_.name, sizeof(dev_.name), deviceName_.c_str(), deviceName_.size()) != 0) { + return false; + } + dev_.id.bustype = 0000; + dev_.id.vendor = 0000; + dev_.id.product = 0000; + dev_.id.version = 0000; + + if (!SetPhys(deviceName_)) { + DHLOGE("Failed to set PHYS!"); + return false; + } + + if (!CreateKey()) { + DHLOGE("Failed to CreateKey!"); + return false; + } + + if (write(fd_, &dev_, sizeof(dev_)) < 0) { + DHLOGE("Unable to set input device info"); + return false; + } + + if (ioctl(fd_, UI_DEV_CREATE) < 0) { + DHLOGE( + "fd = %d, ioctl(fd_, UI_DEV_CREATE) = %d", + fd_, ioctl(fd_, UI_DEV_CREATE)); + DHLOGE("Unable to create input device"); + return false; + } + DHLOGI("create fd %d", fd_); + + char sysfsDeviceName[16] = {0}; + if (ioctl(fd_, UI_GET_SYSNAME(sizeof(sysfsDeviceName)), sysfsDeviceName) < 0) { + DHLOGE("Unable to get input device name"); + } + DHLOGI("get input device name: %s, fd: %d", sysfsDeviceName, fd_); + + socketCli.PushRequest("open listen thread"); + sleep(5); + + DHLOGI("InjectInputEvent %d", fd_); + if (write(fd_, &event, sizeof(event)) < static_cast(sizeof(event))) { + DHLOGE("could not inject event, removed? (fd: %d", fd_); + return false; + } + + DHLOGI("InjectInputEvent synEvent %d", fd_); + struct input_event syncEvent = { + .type = EV_SYN, + .code = 0, + .value = 0 + }; + if (write(fd_, &syncEvent, sizeof(syncEvent)) < static_cast(sizeof(syncEvent))) { + DHLOGE("could not inject syncEvent, removed? (fd: %d", fd_); + return false; + } + DHLOGI("InjectInputEvent end\n"); + return true; +} + +void DistributedInputTestClient::MakeVirtualEvent() +{ + struct input_event event = { + .type = EV_KEY, + .code = BTN_LEFT, + .value = 1 + }; + DHLOGI("MakeVirtualEvent start"); + InjectEvent(event); +} + +void DistributedInputTestClient::ProcRes(bool result) +{ + DHLOGI("ProcRes start source inject event result: %d", result); + injectResult_ = result; + return; +} +} +} +} \ No newline at end of file diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp new file mode 100644 index 0000000..ac6f02d --- /dev/null +++ b/test/injecttest/client/src/socket_client.cpp @@ -0,0 +1,150 @@ +#include "socket_client.h" + +#include +#include + +#include "distributed_hardware_log.h" +#include "nlohmann/json.hpp" + +constexpr int32_t MYPORT = 7000; +constexpr int32_t BUFFER_SIZE = 4096; + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +namespace { + const int MAX_MSG_LENGTH = 4096; +} +int SocketClient::Init(std::string ip) +{ + DHLOGI("Init Socket Client"); + sock_cli = socket(AF_INET, SOCK_STREAM, 0); + struct sockaddr_in servaddr; + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(MYPORT); + servaddr.sin_addr.s_addr = inet_addr(ip.c_str()); + + if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { + DHLOGE("Connect Socker Server failed"); + return -1; + } + + int rBuf = 32 * 1024; + setsockopt(sock_cli, SOL_SOCKET, SO_RCVBUF, (const char*)&rBuf, sizeof(int)); + int sBuf = 32 * 1024; + setsockopt(sock_cli, SOL_SOCKET, SO_SNDBUF, (const char*)&sBuf, sizeof(int)); + + std::thread(&SocketClient::DoRecvSvrRsp, this).detach(); + std::thread(&SocketClient::SendRequest, this).detach(); + + return 0; +} + +int SocketClient::Release() +{ + DHLOGI("Release Socket client, Close Client Conn"); + if (sock_cli != -1) { + close(sock_cli); + sock_cli = -1; + } + return 0; +} + +void SocketClient::SendRequest() +{ + while (true) { + std::unique_lock lock(reqMtx_); + reqQueueConVar_.wait(lock, [this] { + return !(this->requestQueue_.empty()); + }); + + if (!requestQueue_.empty()) { + std::string msg = requestQueue_.front(); + DHLOGI("Client Pop Msg: %s", msg.c_str()); + DoSendRequest(msg); + requestQueue_.pop(); + } + } +} + +void SocketClient::DoSendRequest(std::string msg) +{ + char buf[msg.length() + 1]; + strcpy(buf, msg.c_str()); + DHLOGI("Client Send Message: %s", buf); + send(sock_cli, buf, strlen(buf), 0); +} + +void SocketClient::ParseRecvSvrMsg(std::string msg) +{ + DHLOGI("Client Recv Message: %s", msg.c_str()); + nlohmann::json jsonObj = nlohmann::json::parse(msg); + if (jsonObj.find("injectResult") == jsonObj.end()) { + DHLOGE("server msg is not rsp msg"); + return; + } + bool injectResult = jsonObj.at("injectResult").get(); + DHLOGI("ParseRecvSvrMsg injectResult: %d", injectResult); + resCallback_(injectResult); +} + +void SocketClient::DoRecvSvrRsp() +{ + while(true) { + FD_ZERO(&rfds); + FD_SET(0, &rfds); + maxfd = 0; + FD_SET(sock_cli, &rfds); + if (maxfd < sock_cli) { + maxfd = sock_cli; + } + tv.tv_sec = 10; + tv.tv_usec = 0; + retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); + if (retval == -1) { + DHLOGE("Socket Client select error"); + break; + } else if (retval == 0) { + continue; + } else { + if (FD_ISSET(sock_cli, &rfds)) { + char recvbuf[BUFFER_SIZE]; + int len; + len = recv(sock_cli, recvbuf, sizeof(recvbuf), 0); + if (len == 0) { + DHLOGI("empty msg, drop it"); + continue; + } + DHLOGI("Receive Server Rsp: %s", recvbuf); + std::string svrRsp(recvbuf, len); + std::cout << svrRsp << std::endl; + ParseRecvSvrMsg(svrRsp); + memset(recvbuf, 0, sizeof(recvbuf)); + } + } + } +} + +int SocketClient::PushRequest(std::string msg) +{ + DHLOGI("Client PushRequest: %s", msg.c_str()); + int msgLen = msg.length(); + if (msgLen > MAX_MSG_LENGTH) { + DHLOGE("Msg too long, length: %d", msgLen); + return -1; + } + + std::unique_lock lock(reqMtx_); + requestQueue_.push(msg); + reqQueueConVar_.notify_all(); + return 0; +} + +void SocketClient::RegisterResCallback(ResCallback cb) +{ + resCallback_ = cb; +} +} +} +} \ No newline at end of file diff --git a/test/injecttest/client/src/socket_main_test.cpp b/test/injecttest/client/src/socket_main_test.cpp new file mode 100644 index 0000000..ac1e168 --- /dev/null +++ b/test/injecttest/client/src/socket_main_test.cpp @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +#include "distributed_input_test_client.h" +#include "socket_client.h" +#include "softbus_bus_center.h" +#include "softbus_common.h" + +using namespace testing::ext; +using namespace OHOS::DistributedHardware; +using namespace OHOS::Security::AccessToken; + +static constexpr int32_t PERMISSION_NUM = 2; + +class SocketMainTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + SocketMainTest(); +}; +void SocketMainTest::SetUpTestCase(void) +{ + const char *perms[PERMISSION_NUM]; + perms[0] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER"; + perms[1] = "ohos.permission.DISTRIBUTED_DATASYNC"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = PERMISSION_NUM, + .aclsNum = 0, + .dcaps = NULL, + .perms = perms, + .acls = NULL, + .processName = "dinput_function_test_service", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + sleep(1); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +void SocketMainTest::TearDownTestCase(void) {} +void SocketMainTest::SetUp(void) {} +void SocketMainTest::TearDown(void) {} + +SocketMainTest::SocketMainTest(void) {} + +int main(int argc, char **argv) +{ + if (argc < 2) { + std::cout << "no param, please input ip" << std::endl; + return -1; + } + std::string ip = argv[1]; + OHOS::DistributedHardware::DistributedInput::DistributedInputTestClient inputTestClient(ip); + sleep(10); + EXPECT_EQ(true, inputTestClient.injectResult_); + return 0; +} diff --git a/test/injecttest/server/BUILD.gn b/test/injecttest/server/BUILD.gn new file mode 100644 index 0000000..2763ee2 --- /dev/null +++ b/test/injecttest/server/BUILD.gn @@ -0,0 +1,92 @@ +# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") +import( + "//foundation/distributedhardware/distributed_input/distributedinput.gni") + +module_out_path = "distributed_input/injecttest" + +## UnitTest distributed_input_inject_server_test {{{ +ohos_unittest("distributed_input_inject_server_test") { + module_out_path = module_out_path + + include_dirs = [ + "${distributedinput_path}/sourcehandler/include", + "${innerkits_path}/include", + "${innerkits_path}/src", + "${ipc_path}/include", + "${ipc_path}/src", + "${services_source_path}/sourcemanager/include", + "${frameworks_path}/include", + "${service_common}/include", + "${common_path}/include", + "${fwk_common_path}/log/include", + "${fwk_common_path}/utils/include", + "${distributedhardwarefwk_path}/utils/include/log", + "//third_party/json/include", + "${dfx_utils_path}/include", + "${fwk_interfaces_path}/include", + "${fwk_interfaces_path}/include/ipc", + "${services_source_path}/inputinject/include", + "${utils_path}/include", + "./include", + "${distributedinput_path}/inputdevicehandler/include", + "${distributedinput_path}/sinkhandler/include", + ] + + sources = [ + "./src/socket_server.cpp", + "./src/socket_main_test.cpp", + "./src/dinput_dbg_itf.cpp", + "./src/distributed_input_test_server.cpp", + ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + "-Dprotected=public", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"dinputtestsvr\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "${dfx_utils_path}:libdinput_dfx_utils", + "${innerkits_path}:libdinput_sdk", + "${utils_path}:libdinput_utils", + "//third_party/libevdev:libevdev", + "${distributedhardwarefwk_path}/utils:distributedhardwareutils", + "${services_source_path}/inputinject:libdinput_inject", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "dsoftbus:softbus_client", + "eventhandler:libeventhandler", + "hisysevent:libhisysevent", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + cflags_cc = [ "-DHILOG_ENABLE" ] +} +## UnitTest distributed_input_inject_server_test }}} diff --git a/test/injecttest/server/include/dinput_dbg_itf.h b/test/injecttest/server/include/dinput_dbg_itf.h new file mode 100644 index 0000000..dde5178 --- /dev/null +++ b/test/injecttest/server/include/dinput_dbg_itf.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DINPUT_DB_ITF_H +#define OHOS_DINPUT_DB_ITF_H + +#include +#include + +#include "distributed_input_test_server.h" + +namespace OHOS{ +namespace DistributedHardware { +namespace DistributedInput { +class DInputDBGItf { +public: + DInputDBGItf(); + int32_t Init(); +private: + std::shared_ptr dinputTestServerPtr_; +}; + +} +} +} + +#endif \ No newline at end of file diff --git a/test/injecttest/server/include/distributed_input_test_server.h b/test/injecttest/server/include/distributed_input_test_server.h new file mode 100644 index 0000000..afcb5cb --- /dev/null +++ b/test/injecttest/server/include/distributed_input_test_server.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTED_INPUT_TEST_SERVER_H +#define DISTRIBUTED_INPUT_TEST_SERVER_H + +#include +#include +#include +#include +#include + +#include + +#include "constants_dinput.h" +#include "distributed_input_handler.h" +#include "distributed_input_kit.h" +#include "distributed_input_sink_handler.h" +#include "distributed_input_source_handler.h" +#include "i_distributed_sink_input.h" +#include "i_distributed_source_input.h" +#include "input_node_listener_stub.h" +#include "prepare_d_input_call_back_stub.h" +#include "unprepare_d_input_call_back_stub.h" +#include "start_d_input_call_back_stub.h" +#include "stop_d_input_call_back_stub.h" +#include "simulation_event_listener_stub.h" + +#include "socket_server.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +class DistributedInputTestServer { +public: + void ProcRequest(std::string msg); + DistributedInputTestServer(); + ~DistributedInputTestServer(); + class TestPrepareDInputCallback : + public OHOS::DistributedHardware::DistributedInput::PrepareDInputCallbackStub { + public: + TestPrepareDInputCallback() = default; + virtual ~TestPrepareDInputCallback() = default; + void OnResult(const std::string& deviceId, const int32_t& status); + }; + class TestStartDInputCallback : + public OHOS::DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { + public: + TestStartDInputCallback() = default; + virtual ~TestStartDInputCallback() = default; + void OnResultDhids(const std::string &devId, const int32_t &status); + }; + class TestInputNodeListener : + public OHOS::DistributedHardware::DistributedInput::InputNodeListenerStub { + public: + TestInputNodeListener() = default; + virtual ~TestInputNodeListener() = default; + void OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId, + const std::string nodeDesc); + void OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId); + }; + class TestSimulationEventListener : + public OHOS::DistributedHardware::DistributedInput::SimulationEventListenerStub { + public: + TestSimulationEventListener() = default; + virtual ~TestSimulationEventListener() = default; + int32_t OnSimulationEvent(uint32_t type, uint32_t code, int32_t value); + }; + +private: + void GetRemoteDeviceId(); + void ListenerEvent(); + + OHOS::sptr inputNodeListener_; + OHOS::sptr simEventListener_; + + std::shared_ptr socketServerPtr_ = nullptr; + std::string remoteDevId_ = ""; + static std::vector dhIds_; + std::atomic testResult_ = false; +}; +} +} +} +#endif \ No newline at end of file diff --git a/test/injecttest/server/include/socket_server.h b/test/injecttest/server/include/socket_server.h new file mode 100644 index 0000000..587f0c9 --- /dev/null +++ b/test/injecttest/server/include/socket_server.h @@ -0,0 +1,60 @@ +#ifndef SOCKET_SERVER_H +#define SOCKET_SERVER_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +using RequestCallback = std::function; +class SocketServer { +public: + void Init(); + void Release(); + void PushResponse(std::string msg); + void RegisterRequestCallback(RequestCallback cb); +private: + void GetConn(); + void GetData(); + void SendMessage(); + void DoSendMessage(std::string msg); + void DoSendMessagePure(std::string msg, int conn); + + void ProcRequest(); +private: + int s; + struct sockaddr_in servaddr; + socklen_t len; + std::list li; + + RequestCallback reqCallback_; + +private: + std::mutex reqMtx_; + std::condition_variable reqQueueConVar_; + std::queue requestQueue_; + std::mutex rspMtx_; + std::condition_variable rspQueueConVar_; + std::queue responseQueue_; + + std::mutex tmpMsgMtx_; + std::vector tmpMsgs_; +}; +} +} +} +#endif \ No newline at end of file diff --git a/test/injecttest/server/src/dinput_dbg_itf.cpp b/test/injecttest/server/src/dinput_dbg_itf.cpp new file mode 100644 index 0000000..e3caa24 --- /dev/null +++ b/test/injecttest/server/src/dinput_dbg_itf.cpp @@ -0,0 +1,21 @@ +#include "dinput_dbg_itf.h" + +#include "distributed_hardware_log.h" +#include "distributed_input_test_server.h" + +namespace OHOS{ +namespace DistributedHardware { +namespace DistributedInput { +DInputDBGItf::DInputDBGItf() +{ + DHLOGI("Ctor DInputDBGItf"); +} +int32_t DInputDBGItf::Init() +{ + DHLOGI("Init DInputDBGItf"); + dinputTestServerPtr_ = std::make_shared(); + return 0; +} +} +} +} \ No newline at end of file diff --git a/test/injecttest/server/src/distributed_input_test_server.cpp b/test/injecttest/server/src/distributed_input_test_server.cpp new file mode 100644 index 0000000..f793bca --- /dev/null +++ b/test/injecttest/server/src/distributed_input_test_server.cpp @@ -0,0 +1,167 @@ +#include "distributed_input_test_server.h" + +#include +#include +#include +#include + +#include "idistributed_hardware_source.h" +#include "ipc_skeleton.h" +#include "linux/input-event-codes.h" +#include "nlohmann/json.hpp" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +#include "dinput_errcode.h" +#include "distributed_hardware_log.h" +#include "distributed_hardware_fwk_kit.h" +#include "distributed_input_inject.h" +#include "softbus_bus_center.h" + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +static std::shared_ptr socketServerStaticPtr = nullptr; +namespace { + const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; +} + +std::vector DistributedInputTestServer::dhIds_ = {}; + +void DistributedInputTestServer::TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) +{ + DHLOGI("DistributedInputTestServer::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); + return; +} + +void DistributedInputTestServer::TestStartDInputCallback::OnResultDhids(const std::string &devId, const int32_t &status) +{ + DHLOGI("DistributedInputTestServer::TestStartDInputCallback::OnResult: devId = %s, status = %d", devId.c_str(), status); +} + +void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, + const std::string sinkNodeId, const std::string nodeDesc) +{ + nlohmann::json jsonStr; + jsonStr["srcDevId"] = srcDevId; + jsonStr["sinkDevId"] = sinkDevId; + jsonStr["sinkNodeId"] = sinkNodeId; + jsonStr["nodeDesc"] = nodeDesc; + std::string msg = jsonStr.dump(); + DHLOGI("TestInputNodeListener::OnNodeOnline: %s", msg.c_str()); + DistributedInputTestServer::dhIds_.emplace_back(sinkNodeId); + if (socketServerStaticPtr == nullptr) { + DHLOGI("socketServerStaticPtr is null"); + return; + } + socketServerStaticPtr->PushResponse(msg); + return; +} + +void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, + const std::string sinkNodeId) +{ + std::string rsp = ""; + rsp += "TestInputNodeListener::OnNodeOffLine\n"; + DHLOGI("TestInputNodeListener::OnNodeOffLine: %s", rsp.c_str()); + if (socketServerStaticPtr == nullptr) { + DHLOGI("socketServerStaticPtr is null"); + return; + } + socketServerStaticPtr->PushResponse(rsp); + return; +} + +int32_t DistributedInputTestServer::TestSimulationEventListener::OnSimulationEvent(uint32_t type, uint32_t code, int32_t value) +{ + nlohmann::json jsonStr; + jsonStr["eventType"] = type; + jsonStr["eventCode"] = code; + jsonStr["eventValue"] = value; + std::string msg = jsonStr.dump(); + DHLOGI("TestSimulationEventListener::OnSimulationEvent: %s", msg.c_str()); + if (socketServerStaticPtr == nullptr) { + DHLOGI("socketServerStaticPtr is null"); + return -1; + } + socketServerStaticPtr->PushResponse(msg); + return 0; +} + +void DistributedInputTestServer::GetRemoteDeviceId() +{ + NodeBasicInfo *info = nullptr; + int32_t infoNum = 0; + auto ret = GetAllNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), &info, &infoNum); + if (ret != 0) { + DHLOGI("get remote device id fail."); + return; + } + remoteDevId_ = info->networkId; +} + +DistributedInputTestServer::DistributedInputTestServer():inputNodeListener_(new TestInputNodeListener()), + simEventListener_(new TestSimulationEventListener()), socketServerPtr_(std::make_shared()) +{ + DHLOGI("Init Socker Server"); + GetRemoteDeviceId(); + socketServerPtr_->Init(); + socketServerPtr_->RegisterRequestCallback(std::bind(&DistributedInputTestServer::ProcRequest, this, std::placeholders::_1)); + socketServerStaticPtr = socketServerPtr_; + + int32_t ret = DistributedInputKit::RegisterInputNodeListener(inputNodeListener_); + DHLOGI("Register Input Node Listener %s, ret: %d", (ret == 0 ? "success" : "error"), ret); + ret = DistributedInputKit::RegisterSimulationEventListener(simEventListener_); + DHLOGI("Register SimulationEvent Listener %s, ret: %d", (ret == 0 ? "success" : "error"), ret); + auto prepareCb = new(std::nothrow) TestPrepareDInputCallback(); + DistributedInputKit::PrepareRemoteInput(remoteDevId_, prepareCb); +} + +void DistributedInputTestServer::ListenerEvent() +{ + DHLOGE("ListenerEvent start"); + std::string device = "/dev/input/event16"; + struct input_event event; + struct pollfd pfd[1]; + int nfd = 1; + int len; + int fd = open(device.c_str(), O_RDONLY); + if (fd < 0) { + DHLOGE("failed to open device: %s", device.c_str()); + } + pfd[0].fd = fd; + pfd[0].events = POLLIN; + while(true) { + if (poll(pfd, nfd, -1) > 0) { + len = read(fd, &event, sizeof(event)); + if (len < 0) { + continue; + } + DHLOGI("Event: type %d, code: %d, value: %d\n", event.type, event.code, event.value); + if (event.type == EV_KEY && event.code == BTN_LEFT && event.value == 1) { + nlohmann::json jsonStr; + jsonStr["injectResult"] = true; + std::string msg = jsonStr.dump(); + this->testResult_.store(true); + socketServerStaticPtr->PushResponse(msg); + } + } + } +} + +DistributedInputTestServer::~DistributedInputTestServer() +{ + DHLOGI("DistributedInputTestServer destruction"); +} + +void DistributedInputTestServer::ProcRequest(std::string msg) +{ + DHLOGI("DistributedInputTestServer ProcRequest"); + sleep(2); + std::thread(&DistributedInputTestServer::ListenerEvent, this).detach(); + auto startCb = new(std::nothrow) TestStartDInputCallback(); + DistributedInputKit::StartRemoteInput(remoteDevId_, dhIds_, startCb); +} +} +} +} \ No newline at end of file diff --git a/test/injecttest/server/src/socket_main_test.cpp b/test/injecttest/server/src/socket_main_test.cpp new file mode 100644 index 0000000..10e1aa4 --- /dev/null +++ b/test/injecttest/server/src/socket_main_test.cpp @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "dinput_dbg_itf.h" +#include "socket_server.h" + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +#include "softbus_bus_center.h" +#include "softbus_common.h" + +using namespace testing::ext; +using namespace OHOS::DistributedHardware; +using namespace OHOS::Security::AccessToken; + +static constexpr int32_t PERMISSION_NUM = 2; + +class SocketMainTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + SocketMainTest(); +}; +void SocketMainTest::SetUpTestCase(void) +{ + const char *perms[PERMISSION_NUM]; + perms[0] = "ohos.permission.DISTRIBUTED_SOFTBUS_CENTER"; + perms[1] = "ohos.permission.DISTRIBUTED_DATASYNC"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = PERMISSION_NUM, + .aclsNum = 0, + .dcaps = NULL, + .perms = perms, + .acls = NULL, + .processName = "dinput_function_test_service", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + sleep(1); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +void SocketMainTest::TearDownTestCase(void) {} +void SocketMainTest::SetUp(void) {} +void SocketMainTest::TearDown(void) {} + +SocketMainTest::SocketMainTest(void) {} + +HWTEST_F(SocketMainTest, SocketMainTest_0001, TestSize.Level1) +{ + OHOS::DistributedHardware::DistributedInput::DInputDBGItf dbgItf; + dbgItf.Init(); + while (!dbgItf.dinputTestServerPtr_->testResult_.load()) {} + EXPECT_EQ(true, dbgItf.dinputTestServerPtr_->testResult_.load()); +} \ No newline at end of file diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp new file mode 100644 index 0000000..44b1745 --- /dev/null +++ b/test/injecttest/server/src/socket_server.cpp @@ -0,0 +1,205 @@ +#include "socket_server.h" + +#include +#include +#include +#include + +#include "distributed_hardware_log.h" + +constexpr int32_t PORT = 7000; + +namespace OHOS { +namespace DistributedHardware { +namespace DistributedInput { +namespace { + const int MAX_MSG_LENGTH = 4096; +} +void SocketServer::Init() +{ + DHLOGI("Init DBGSvr"); + s = socket(AF_INET, SOCK_STREAM, 0); + memset(&servaddr, 0, sizeof(servaddr)); + servaddr.sin_family = AF_INET; + servaddr.sin_port = htons(PORT); + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { + DHLOGE("Bind Socket Server error"); + return; + } + if (listen(s, 20) == -1) { + DHLOGE("Listener Socket Server error"); + return; + } + len = sizeof(servaddr); + + int rBuf = 32 * 1024; + setsockopt(s, SOL_SOCKET, SO_RCVBUF, (const char*)&rBuf, sizeof(int)); + int sBuf = 32 * 1024; + setsockopt(s, SOL_SOCKET, SO_SNDBUF, (const char*)&sBuf, sizeof(int)); + + std::thread(&SocketServer::GetConn, this).detach(); + std::thread(&SocketServer::SendMessage, this).detach(); + std::thread(&SocketServer::GetData, this).detach(); + std::thread(&SocketServer::ProcRequest, this).detach(); +} + +void SocketServer::GetConn() +{ + while(1) { + int conn = accept(s, (struct sockaddr *)&servaddr, &len); + li.push_back(conn); + DHLOGI("receive client connect, fd: %d\n", conn); + std::lock_guard lock(tmpMsgMtx_); + if (tmpMsgs_.size() != 0) { + DHLOGI("Send Old msg"); + for (auto &msg : tmpMsgs_) { + DoSendMessagePure(msg, conn); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + } + tmpMsgs_.clear(); + } +} + +void SocketServer::GetData() +{ + struct timeval tv; + tv.tv_sec = 10; + tv.tv_usec = 0; + while (1) { + std::list::iterator it; + for (it = li.begin(); it != li.end(); ++it) { + fd_set rfds; + FD_ZERO(&rfds); + int maxfd = 0; + int retval = 0; + FD_SET(*it, &rfds); + if (maxfd < *it) { + maxfd = *it; + } + retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); + if (retval == -1) { + DHLOGE("Server select error, ret: %d", retval); + } else if (retval == 0) { + DHLOGI("not message"); + } else { + char buf[MAX_MSG_LENGTH]; + memset(buf, 0, sizeof(buf)); + int len = recv(*it, buf, sizeof(buf), 0); + DHLOGI("Server Recv Msg: %s, len: %d", buf, len); + if (len > 0) { + std::string msg(buf, len); + std::lock_guard lock(reqMtx_); + requestQueue_.push(msg); + reqQueueConVar_.notify_all(); + } + } + } + sleep(1); + } +} + +void SocketServer::SendMessage() +{ + while(true) { + std::unique_lock lock(rspMtx_); + rspQueueConVar_.wait(lock, [this] { + return !(this->responseQueue_.empty()); + }); + + if (!responseQueue_.empty()) { + std::string msg = responseQueue_.front(); + DHLOGI("Pos Rsp Msg: %s", msg.c_str()); + DoSendMessage(msg); + responseQueue_.pop(); + std::this_thread::sleep_for(std::chrono::milliseconds(20)); + } + } +} + +void SocketServer::ProcRequest() +{ + while (true) { + std::unique_lock lock(reqMtx_); + reqQueueConVar_.wait(lock, [this] { + return !(this->requestQueue_.empty()); + }); + if (!requestQueue_.empty()) { + std::string msg = requestQueue_.front(); + if (reqCallback_ != nullptr) { + reqCallback_(msg); + } else { + DHLOGE("reqCallback_ is null"); + } + requestQueue_.pop(); + } + } +} + +void SocketServer::DoSendMessage(std::string msg) +{ + char buf[msg.length() + 1]; + strcpy(buf, msg.c_str()); + + DHLOGI("Server Send Message: %s", buf); + + if (li.size() == 0) { + DHLOGI("No client, save it"); + std::lock_guard lock(tmpMsgMtx_); + tmpMsgs_.push_back(msg); + } + + std::list::iterator it; + for (it = li.begin(); it != li.end(); ++it) { + send(*it, buf, sizeof(buf), 0); + } +} + +void SocketServer::DoSendMessagePure(std::string msg, int conn) +{ + char buf[msg.length() + 1]; + strcpy(buf, msg.c_str()); + + DHLOGI("Server Send Message Pure: %s", buf); + + send(conn, buf, sizeof(buf), 0); +} + +void SocketServer::PushResponse(std::string msg) +{ + DHLOGI("PushRespones: %s", msg.c_str()); + int msgLen = msg.length(); + if (msgLen > MAX_MSG_LENGTH) { + DHLOGE("Msg too long, length: %d", msgLen); + return; + } + + std::unique_lock lock(rspMtx_); + responseQueue_.push(msg); + rspQueueConVar_.notify_all(); +} + +void SocketServer::Release() +{ + DHLOGI("Close connection"); + std::list::iterator it; + for (it = li.begin(); it != li.end(); ++it) { + close(*it); + } + li.clear(); + + DHLOGI("Close socket fd"); + if (s != -1) { + close(s); + s = -1; + } +} + +void SocketServer::RegisterRequestCallback(RequestCallback cb) +{ + reqCallback_ = cb; +} +} +} +} \ No newline at end of file -- Gitee From 3ac2eb913fd1c49e469609cf1c885130b914d2c3 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Wed, 26 Jul 2023 09:27:28 +0800 Subject: [PATCH 02/10] add inject event test Signed-off-by: wangxuanxuan --- test/injecttest/client/BUILD.gn | 2 +- .../include/distributed_input_test_client.h | 15 +++++ .../injecttest/client/include/socket_client.h | 21 ++++++- .../src/distributed_input_test_client.cpp | 21 ++++++- test/injecttest/client/src/socket_client.cpp | 46 +++++++++----- .../client/src/socket_main_test.cpp | 8 ++- .../injecttest/server/include/socket_server.h | 27 ++++++-- test/injecttest/server/src/dinput_dbg_itf.cpp | 15 +++++ .../src/distributed_input_test_server.cpp | 30 ++++++--- test/injecttest/server/src/socket_server.cpp | 62 ++++++++++++------- 10 files changed, 189 insertions(+), 58 deletions(-) diff --git a/test/injecttest/client/BUILD.gn b/test/injecttest/client/BUILD.gn index 2495cb2..cb6806e 100644 --- a/test/injecttest/client/BUILD.gn +++ b/test/injecttest/client/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/test/injecttest/client/include/distributed_input_test_client.h b/test/injecttest/client/include/distributed_input_test_client.h index 0051939..7e3b2e3 100644 --- a/test/injecttest/client/include/distributed_input_test_client.h +++ b/test/injecttest/client/include/distributed_input_test_client.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef DISTRIBUTED_INPUT_TEST_CLIENT_H #define DISTRIBUTED_INPUT_TEST_CLIENT_H diff --git a/test/injecttest/client/include/socket_client.h b/test/injecttest/client/include/socket_client.h index 220f271..1478300 100644 --- a/test/injecttest/client/include/socket_client.h +++ b/test/injecttest/client/include/socket_client.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef SOCKET_CLIENT_H #define SOCKET_CLIENT_H @@ -35,11 +50,11 @@ private: void DoSendRequest(std::string msg); private: - int sock_cli; + int32_t sock_cli; fd_set rfds; struct timeval tv; - int retval; - int maxfd; + int32_t retval; + int32_t maxfd; private: std::mutex reqMtx_; diff --git a/test/injecttest/client/src/distributed_input_test_client.cpp b/test/injecttest/client/src/distributed_input_test_client.cpp index 5d5fd6d..7777b5a 100644 --- a/test/injecttest/client/src/distributed_input_test_client.cpp +++ b/test/injecttest/client/src/distributed_input_test_client.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "distributed_input_test_client.h" #include @@ -24,6 +39,8 @@ namespace DistributedHardware { namespace DistributedInput { namespace { const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; + constexpr int32_t WAIT_DH_SYNC_TIME = 5; + constexpr int32_t KEY_VALUE_DOWN = 1; } DistributedInputTestClient::DistributedInputTestClient(std::string ip) @@ -128,7 +145,7 @@ bool DistributedInputTestClient::InjectEvent(input_event event) DHLOGI("get input device name: %s, fd: %d", sysfsDeviceName, fd_); socketCli.PushRequest("open listen thread"); - sleep(5); + sleep(WAIT_DH_SYNC_TIME); DHLOGI("InjectInputEvent %d", fd_); if (write(fd_, &event, sizeof(event)) < static_cast(sizeof(event))) { @@ -155,7 +172,7 @@ void DistributedInputTestClient::MakeVirtualEvent() struct input_event event = { .type = EV_KEY, .code = BTN_LEFT, - .value = 1 + .value = KEY_STATE_DOWN }; DHLOGI("MakeVirtualEvent start"); InjectEvent(event); diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index ac6f02d..d6c693e 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "socket_client.h" #include @@ -6,16 +21,19 @@ #include "distributed_hardware_log.h" #include "nlohmann/json.hpp" -constexpr int32_t MYPORT = 7000; -constexpr int32_t BUFFER_SIZE = 4096; - namespace OHOS { namespace DistributedHardware { namespace DistributedInput { namespace { - const int MAX_MSG_LENGTH = 4096; + constexpr int32_t MYPORT = 7000; + constexpr int32_t BUFFER_SIZE = 4096; + constexpr int32_t MAX_MSG_LENGTH = 4096; + constexpr int32_t BUFF_MAX_LEN = 32 * 1024; + constexpr int32_t SEND_MSG_SPACE = 100; + constexpr int32_t BLOCK_TIME = 10; } -int SocketClient::Init(std::string ip) + +int32_t SocketClient::Init(std::string ip) { DHLOGI("Init Socket Client"); sock_cli = socket(AF_INET, SOCK_STREAM, 0); @@ -30,10 +48,10 @@ int SocketClient::Init(std::string ip) return -1; } - int rBuf = 32 * 1024; - setsockopt(sock_cli, SOL_SOCKET, SO_RCVBUF, (const char*)&rBuf, sizeof(int)); - int sBuf = 32 * 1024; - setsockopt(sock_cli, SOL_SOCKET, SO_SNDBUF, (const char*)&sBuf, sizeof(int)); + int32_t rBuf = BUFF_MAX_LEN; + setsockopt(sock_cli, SOL_SOCKET, SO_RCVBUF, (const char*)&rBuf, sizeof(int32_t)); + int32_t sBuf = BUFF_MAX_LEN; + setsockopt(sock_cli, SOL_SOCKET, SO_SNDBUF, (const char*)&sBuf, sizeof(int32_t)); std::thread(&SocketClient::DoRecvSvrRsp, this).detach(); std::thread(&SocketClient::SendRequest, this).detach(); @@ -41,7 +59,7 @@ int SocketClient::Init(std::string ip) return 0; } -int SocketClient::Release() +int32_t SocketClient::Release() { DHLOGI("Release Socket client, Close Client Conn"); if (sock_cli != -1) { @@ -99,7 +117,7 @@ void SocketClient::DoRecvSvrRsp() if (maxfd < sock_cli) { maxfd = sock_cli; } - tv.tv_sec = 10; + tv.tv_sec = BLOCK_TIME; tv.tv_usec = 0; retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); if (retval == -1) { @@ -110,7 +128,7 @@ void SocketClient::DoRecvSvrRsp() } else { if (FD_ISSET(sock_cli, &rfds)) { char recvbuf[BUFFER_SIZE]; - int len; + int32_t len; len = recv(sock_cli, recvbuf, sizeof(recvbuf), 0); if (len == 0) { DHLOGI("empty msg, drop it"); @@ -126,10 +144,10 @@ void SocketClient::DoRecvSvrRsp() } } -int SocketClient::PushRequest(std::string msg) +int32_t SocketClient::PushRequest(std::string msg) { DHLOGI("Client PushRequest: %s", msg.c_str()); - int msgLen = msg.length(); + int32_t msgLen = msg.length(); if (msgLen > MAX_MSG_LENGTH) { DHLOGE("Msg too long, length: %d", msgLen); return -1; diff --git a/test/injecttest/client/src/socket_main_test.cpp b/test/injecttest/client/src/socket_main_test.cpp index ac1e168..9facb3c 100644 --- a/test/injecttest/client/src/socket_main_test.cpp +++ b/test/injecttest/client/src/socket_main_test.cpp @@ -29,6 +29,8 @@ using namespace OHOS::DistributedHardware; using namespace OHOS::Security::AccessToken; static constexpr int32_t PERMISSION_NUM = 2; +static constexpr int32_t ARGC_NUM = 2; +static constexpr int32_t TEST_WAIT_TIME = 10; class SocketMainTest : public testing::Test { public: @@ -65,15 +67,15 @@ void SocketMainTest::TearDown(void) {} SocketMainTest::SocketMainTest(void) {} -int main(int argc, char **argv) +int32_t main(int32_t argc, char **argv) { - if (argc < 2) { + if (argc < ARGC_NUM) { std::cout << "no param, please input ip" << std::endl; return -1; } std::string ip = argv[1]; OHOS::DistributedHardware::DistributedInput::DistributedInputTestClient inputTestClient(ip); - sleep(10); + sleep(TEST_WAIT_TIME); EXPECT_EQ(true, inputTestClient.injectResult_); return 0; } diff --git a/test/injecttest/server/include/socket_server.h b/test/injecttest/server/include/socket_server.h index 587f0c9..2fc6efe 100644 --- a/test/injecttest/server/include/socket_server.h +++ b/test/injecttest/server/include/socket_server.h @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #ifndef SOCKET_SERVER_H #define SOCKET_SERVER_H @@ -32,26 +47,26 @@ private: void GetData(); void SendMessage(); void DoSendMessage(std::string msg); - void DoSendMessagePure(std::string msg, int conn); + void DoSendMessagePure(std::string msg, int32_t conn); void ProcRequest(); private: - int s; + int32_t s; struct sockaddr_in servaddr; socklen_t len; - std::list li; + std::list li; RequestCallback reqCallback_; private: std::mutex reqMtx_; + std::mutex tmpMsgMtx_; + std::mutex rspMtx_; + std::condition_variable reqQueueConVar_; std::queue requestQueue_; - std::mutex rspMtx_; std::condition_variable rspQueueConVar_; std::queue responseQueue_; - - std::mutex tmpMsgMtx_; std::vector tmpMsgs_; }; } diff --git a/test/injecttest/server/src/dinput_dbg_itf.cpp b/test/injecttest/server/src/dinput_dbg_itf.cpp index e3caa24..859d025 100644 --- a/test/injecttest/server/src/dinput_dbg_itf.cpp +++ b/test/injecttest/server/src/dinput_dbg_itf.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "dinput_dbg_itf.h" #include "distributed_hardware_log.h" diff --git a/test/injecttest/server/src/distributed_input_test_server.cpp b/test/injecttest/server/src/distributed_input_test_server.cpp index f793bca..7f15edb 100644 --- a/test/injecttest/server/src/distributed_input_test_server.cpp +++ b/test/injecttest/server/src/distributed_input_test_server.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "distributed_input_test_server.h" #include @@ -24,6 +39,8 @@ namespace DistributedInput { static std::shared_ptr socketServerStaticPtr = nullptr; namespace { const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; + constexpr int32_t WAIT_DH_SYNC_TIME = 2; + constexpr int32_t KEY_VALUE_DOWN = 1; } std::vector DistributedInputTestServer::dhIds_ = {}; @@ -61,8 +78,7 @@ void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std:: void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, const std::string sinkNodeId) { - std::string rsp = ""; - rsp += "TestInputNodeListener::OnNodeOffLine\n"; + std::string rsp = "TestInputNodeListener::OnNodeOffLine\n"; DHLOGI("TestInputNodeListener::OnNodeOffLine: %s", rsp.c_str()); if (socketServerStaticPtr == nullptr) { DHLOGI("socketServerStaticPtr is null"); @@ -123,9 +139,9 @@ void DistributedInputTestServer::ListenerEvent() std::string device = "/dev/input/event16"; struct input_event event; struct pollfd pfd[1]; - int nfd = 1; - int len; - int fd = open(device.c_str(), O_RDONLY); + int32_t nfd = 1; + int32_t len; + int32_t fd = open(device.c_str(), O_RDONLY); if (fd < 0) { DHLOGE("failed to open device: %s", device.c_str()); } @@ -138,7 +154,7 @@ void DistributedInputTestServer::ListenerEvent() continue; } DHLOGI("Event: type %d, code: %d, value: %d\n", event.type, event.code, event.value); - if (event.type == EV_KEY && event.code == BTN_LEFT && event.value == 1) { + if (event.type == EV_KEY && event.code == BTN_LEFT && event.value == KEY_VALUE_DOWN) { nlohmann::json jsonStr; jsonStr["injectResult"] = true; std::string msg = jsonStr.dump(); @@ -157,7 +173,7 @@ DistributedInputTestServer::~DistributedInputTestServer() void DistributedInputTestServer::ProcRequest(std::string msg) { DHLOGI("DistributedInputTestServer ProcRequest"); - sleep(2); + sleep(WAIT_DH_SYNC_TIME); std::thread(&DistributedInputTestServer::ListenerEvent, this).detach(); auto startCb = new(std::nothrow) TestStartDInputCallback(); DistributedInputKit::StartRemoteInput(remoteDevId_, dhIds_, startCb); diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp index 44b1745..2da7b25 100644 --- a/test/injecttest/server/src/socket_server.cpp +++ b/test/injecttest/server/src/socket_server.cpp @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + #include "socket_server.h" #include @@ -7,13 +22,16 @@ #include "distributed_hardware_log.h" -constexpr int32_t PORT = 7000; - namespace OHOS { namespace DistributedHardware { namespace DistributedInput { namespace { - const int MAX_MSG_LENGTH = 4096; + constexpr int32_t PORT = 7000; + constexpr int32_t MAX_MSG_LENGTH = 4096; + constexpr int32_t ESTABLISHED_MAX = 20; + constexpr int32_t BUFF_MAX_LEN = 32 * 1024; + constexpr int32_t SEND_MSG_SPACE = 100; + constexpr int32_t BLOCK_TIME = 10; } void SocketServer::Init() { @@ -27,16 +45,16 @@ void SocketServer::Init() DHLOGE("Bind Socket Server error"); return; } - if (listen(s, 20) == -1) { + if (listen(s, ESTABLISHED_MAX) == -1) { DHLOGE("Listener Socket Server error"); return; } len = sizeof(servaddr); - int rBuf = 32 * 1024; - setsockopt(s, SOL_SOCKET, SO_RCVBUF, (const char*)&rBuf, sizeof(int)); - int sBuf = 32 * 1024; - setsockopt(s, SOL_SOCKET, SO_SNDBUF, (const char*)&sBuf, sizeof(int)); + int32_t rBuf = BUFF_MAX_LEN; + setsockopt(s, SOL_SOCKET, SO_RCVBUF, (const char*)&rBuf, sizeof(int32_t)); + int32_t sBuf = BUFF_MAX_LEN; + setsockopt(s, SOL_SOCKET, SO_SNDBUF, (const char*)&sBuf, sizeof(int32_t)); std::thread(&SocketServer::GetConn, this).detach(); std::thread(&SocketServer::SendMessage, this).detach(); @@ -46,8 +64,8 @@ void SocketServer::Init() void SocketServer::GetConn() { - while(1) { - int conn = accept(s, (struct sockaddr *)&servaddr, &len); + while(true) { + int32_t conn = accept(s, (struct sockaddr *)&servaddr, &len); li.push_back(conn); DHLOGI("receive client connect, fd: %d\n", conn); std::lock_guard lock(tmpMsgMtx_); @@ -55,7 +73,7 @@ void SocketServer::GetConn() DHLOGI("Send Old msg"); for (auto &msg : tmpMsgs_) { DoSendMessagePure(msg, conn); - std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::this_thread::sleep_for(std::chrono::milliseconds(SEND_MSG_SPACE)); } } tmpMsgs_.clear(); @@ -65,15 +83,15 @@ void SocketServer::GetConn() void SocketServer::GetData() { struct timeval tv; - tv.tv_sec = 10; + tv.tv_sec = BLOCK_TIME; tv.tv_usec = 0; - while (1) { - std::list::iterator it; + while (true) { + std::list::iterator it; for (it = li.begin(); it != li.end(); ++it) { fd_set rfds; FD_ZERO(&rfds); - int maxfd = 0; - int retval = 0; + int32_t maxfd = 0; + int32_t retval = 0; FD_SET(*it, &rfds); if (maxfd < *it) { maxfd = *it; @@ -86,7 +104,7 @@ void SocketServer::GetData() } else { char buf[MAX_MSG_LENGTH]; memset(buf, 0, sizeof(buf)); - int len = recv(*it, buf, sizeof(buf), 0); + int32_t len = recv(*it, buf, sizeof(buf), 0); DHLOGI("Server Recv Msg: %s, len: %d", buf, len); if (len > 0) { std::string msg(buf, len); @@ -113,7 +131,7 @@ void SocketServer::SendMessage() DHLOGI("Pos Rsp Msg: %s", msg.c_str()); DoSendMessage(msg); responseQueue_.pop(); - std::this_thread::sleep_for(std::chrono::milliseconds(20)); + std::this_thread::sleep_for(std::chrono::milliseconds(SEND_MSG_SPACE)); } } } @@ -150,13 +168,13 @@ void SocketServer::DoSendMessage(std::string msg) tmpMsgs_.push_back(msg); } - std::list::iterator it; + std::list::iterator it; for (it = li.begin(); it != li.end(); ++it) { send(*it, buf, sizeof(buf), 0); } } -void SocketServer::DoSendMessagePure(std::string msg, int conn) +void SocketServer::DoSendMessagePure(std::string msg, int32_t conn) { char buf[msg.length() + 1]; strcpy(buf, msg.c_str()); @@ -169,7 +187,7 @@ void SocketServer::DoSendMessagePure(std::string msg, int conn) void SocketServer::PushResponse(std::string msg) { DHLOGI("PushRespones: %s", msg.c_str()); - int msgLen = msg.length(); + int32_t msgLen = msg.length(); if (msgLen > MAX_MSG_LENGTH) { DHLOGE("Msg too long, length: %d", msgLen); return; @@ -183,7 +201,7 @@ void SocketServer::PushResponse(std::string msg) void SocketServer::Release() { DHLOGI("Close connection"); - std::list::iterator it; + std::list::iterator it; for (it = li.begin(); it != li.end(); ++it) { close(*it); } -- Gitee From d539fca8f43944696c8161fbce6df5d6619b8591 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Thu, 27 Jul 2023 09:49:38 +0800 Subject: [PATCH 03/10] add inject event test Signed-off-by: wangxuanxuan --- test/injecttest/client/BUILD.gn | 19 +----------------- .../src/distributed_input_test_client.cpp | 6 +++--- test/injecttest/client/src/socket_client.cpp | 3 +-- .../client/src/socket_main_test.cpp | 1 + test/injecttest/server/BUILD.gn | 20 +------------------ .../include/distributed_input_test_server.h | 5 ----- test/injecttest/server/src/dinput_dbg_itf.cpp | 2 +- .../src/distributed_input_test_server.cpp | 9 +++++---- test/injecttest/server/src/socket_server.cpp | 4 +++- 9 files changed, 16 insertions(+), 53 deletions(-) diff --git a/test/injecttest/client/BUILD.gn b/test/injecttest/client/BUILD.gn index cb6806e..7e24e19 100644 --- a/test/injecttest/client/BUILD.gn +++ b/test/injecttest/client/BUILD.gn @@ -22,27 +22,14 @@ ohos_unittest("distributed_input_inject_client_test") { module_out_path = module_out_path include_dirs = [ - "${distributedinput_path}/sourcehandler/include", "${innerkits_path}/include", - "${innerkits_path}/src", "${ipc_path}/include", - "${ipc_path}/src", - "${services_source_path}/sourcemanager/include", "${frameworks_path}/include", "${service_common}/include", "${common_path}/include", - "${fwk_common_path}/log/include", - "${fwk_common_path}/utils/include", - "${distributedhardwarefwk_path}/utils/include/log", "//third_party/json/include", - "${dfx_utils_path}/include", - "${fwk_interfaces_path}/include", - "${fwk_interfaces_path}/include/ipc", - "${services_source_path}/inputinject/include", "${utils_path}/include", "./include", - "${distributedinput_path}/inputdevicehandler/include", - "${distributedinput_path}/sinkhandler/include", ] sources = [ @@ -61,16 +48,14 @@ ohos_unittest("distributed_input_inject_client_test") { defines = [ "HI_LOG_ENABLE", - "DH_LOG_TAG=\"dinputtestsvr\"", + "DH_LOG_TAG=\"dinputtestclt\"", "LOG_DOMAIN=0xD004100", ] deps = [ - "${dfx_utils_path}:libdinput_dfx_utils", "${innerkits_path}:libdinput_sdk", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", - "${distributedhardwarefwk_path}/utils:distributedhardwareutils", ] external_deps = [ @@ -80,9 +65,7 @@ ohos_unittest("distributed_input_inject_client_test") { "c_utils:utils", "dsoftbus:softbus_client", "eventhandler:libeventhandler", - "hisysevent:libhisysevent", "ipc:ipc_core", - "samgr:samgr_proxy", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/test/injecttest/client/src/distributed_input_test_client.cpp b/test/injecttest/client/src/distributed_input_test_client.cpp index 7777b5a..a2ae7fe 100644 --- a/test/injecttest/client/src/distributed_input_test_client.cpp +++ b/test/injecttest/client/src/distributed_input_test_client.cpp @@ -23,7 +23,6 @@ #include "linux/input-event-codes.h" -#include "idistributed_hardware_source.h" #include "ipc_skeleton.h" #include "nlohmann/json.hpp" #include "nativetoken_kit.h" @@ -31,7 +30,8 @@ #include "constants_dinput.h" #include "dinput_errcode.h" -#include "distributed_hardware_log.h" +#include "dinput_log.h" +#include "idistributed_hardware_source.h" #include "softbus_bus_center.h" namespace OHOS { @@ -172,7 +172,7 @@ void DistributedInputTestClient::MakeVirtualEvent() struct input_event event = { .type = EV_KEY, .code = BTN_LEFT, - .value = KEY_STATE_DOWN + .value = KEY_VALUE_DOWN }; DHLOGI("MakeVirtualEvent start"); InjectEvent(event); diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index d6c693e..bc415be 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -18,7 +18,7 @@ #include #include -#include "distributed_hardware_log.h" +#include "dinput_log.h" #include "nlohmann/json.hpp" namespace OHOS { @@ -29,7 +29,6 @@ namespace { constexpr int32_t BUFFER_SIZE = 4096; constexpr int32_t MAX_MSG_LENGTH = 4096; constexpr int32_t BUFF_MAX_LEN = 32 * 1024; - constexpr int32_t SEND_MSG_SPACE = 100; constexpr int32_t BLOCK_TIME = 10; } diff --git a/test/injecttest/client/src/socket_main_test.cpp b/test/injecttest/client/src/socket_main_test.cpp index 9facb3c..269d3e7 100644 --- a/test/injecttest/client/src/socket_main_test.cpp +++ b/test/injecttest/client/src/socket_main_test.cpp @@ -75,6 +75,7 @@ int32_t main(int32_t argc, char **argv) } std::string ip = argv[1]; OHOS::DistributedHardware::DistributedInput::DistributedInputTestClient inputTestClient(ip); + inputTestClient.MakeVirtualEvent(); sleep(TEST_WAIT_TIME); EXPECT_EQ(true, inputTestClient.injectResult_); return 0; diff --git a/test/injecttest/server/BUILD.gn b/test/injecttest/server/BUILD.gn index 2763ee2..587f60f 100644 --- a/test/injecttest/server/BUILD.gn +++ b/test/injecttest/server/BUILD.gn @@ -22,27 +22,14 @@ ohos_unittest("distributed_input_inject_server_test") { module_out_path = module_out_path include_dirs = [ - "${distributedinput_path}/sourcehandler/include", "${innerkits_path}/include", - "${innerkits_path}/src", "${ipc_path}/include", - "${ipc_path}/src", - "${services_source_path}/sourcemanager/include", "${frameworks_path}/include", "${service_common}/include", "${common_path}/include", - "${fwk_common_path}/log/include", - "${fwk_common_path}/utils/include", - "${distributedhardwarefwk_path}/utils/include/log", - "//third_party/json/include", - "${dfx_utils_path}/include", - "${fwk_interfaces_path}/include", - "${fwk_interfaces_path}/include/ipc", - "${services_source_path}/inputinject/include", "${utils_path}/include", "./include", - "${distributedinput_path}/inputdevicehandler/include", - "${distributedinput_path}/sinkhandler/include", + "//third_party/json/include", ] sources = [ @@ -67,12 +54,9 @@ ohos_unittest("distributed_input_inject_server_test") { ] deps = [ - "${dfx_utils_path}:libdinput_dfx_utils", "${innerkits_path}:libdinput_sdk", "${utils_path}:libdinput_utils", "//third_party/libevdev:libevdev", - "${distributedhardwarefwk_path}/utils:distributedhardwareutils", - "${services_source_path}/inputinject:libdinput_inject", ] external_deps = [ @@ -82,9 +66,7 @@ ohos_unittest("distributed_input_inject_server_test") { "c_utils:utils", "dsoftbus:softbus_client", "eventhandler:libeventhandler", - "hisysevent:libhisysevent", "ipc:ipc_core", - "samgr:samgr_proxy", ] cflags_cc = [ "-DHILOG_ENABLE" ] diff --git a/test/injecttest/server/include/distributed_input_test_server.h b/test/injecttest/server/include/distributed_input_test_server.h index afcb5cb..ec8cc71 100644 --- a/test/injecttest/server/include/distributed_input_test_server.h +++ b/test/injecttest/server/include/distributed_input_test_server.h @@ -25,12 +25,7 @@ #include #include "constants_dinput.h" -#include "distributed_input_handler.h" #include "distributed_input_kit.h" -#include "distributed_input_sink_handler.h" -#include "distributed_input_source_handler.h" -#include "i_distributed_sink_input.h" -#include "i_distributed_source_input.h" #include "input_node_listener_stub.h" #include "prepare_d_input_call_back_stub.h" #include "unprepare_d_input_call_back_stub.h" diff --git a/test/injecttest/server/src/dinput_dbg_itf.cpp b/test/injecttest/server/src/dinput_dbg_itf.cpp index 859d025..bebc7d8 100644 --- a/test/injecttest/server/src/dinput_dbg_itf.cpp +++ b/test/injecttest/server/src/dinput_dbg_itf.cpp @@ -15,7 +15,7 @@ #include "dinput_dbg_itf.h" -#include "distributed_hardware_log.h" +#include "dinput_log.h" #include "distributed_input_test_server.h" namespace OHOS{ diff --git a/test/injecttest/server/src/distributed_input_test_server.cpp b/test/injecttest/server/src/distributed_input_test_server.cpp index 7f15edb..317672a 100644 --- a/test/injecttest/server/src/distributed_input_test_server.cpp +++ b/test/injecttest/server/src/distributed_input_test_server.cpp @@ -20,7 +20,10 @@ #include #include -#include "idistributed_hardware_source.h" +#include +#include +#include + #include "ipc_skeleton.h" #include "linux/input-event-codes.h" #include "nlohmann/json.hpp" @@ -28,9 +31,7 @@ #include "token_setproc.h" #include "dinput_errcode.h" -#include "distributed_hardware_log.h" -#include "distributed_hardware_fwk_kit.h" -#include "distributed_input_inject.h" +#include "dinput_log.h" #include "softbus_bus_center.h" namespace OHOS { diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp index 2da7b25..af0326b 100644 --- a/test/injecttest/server/src/socket_server.cpp +++ b/test/injecttest/server/src/socket_server.cpp @@ -15,12 +15,14 @@ #include "socket_server.h" +#include "unistd.h" + #include #include #include #include -#include "distributed_hardware_log.h" +#include "dinput_log.h" namespace OHOS { namespace DistributedHardware { -- Gitee From 0a0cf93473c0009a286fafe4fabcdd2ba3e47dd7 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Thu, 27 Jul 2023 15:12:08 +0800 Subject: [PATCH 04/10] add inject event test Signed-off-by: wangxuanxuan --- test/BUILD.gn | 2 +- test/injecttest/client/BUILD.gn | 3 +- .../include/distributed_input_test_client.h | 4 +- .../injecttest/client/include/socket_client.h | 13 +----- .../src/distributed_input_test_client.cpp | 25 +++++++---- test/injecttest/client/src/socket_client.cpp | 42 ++++++++++++------- test/injecttest/server/BUILD.gn | 3 +- .../server/include/dinput_dbg_itf.h | 3 +- test/injecttest/server/src/dinput_dbg_itf.cpp | 2 +- .../src/distributed_input_test_server.cpp | 25 ++++++----- test/injecttest/server/src/socket_server.cpp | 13 +++--- 11 files changed, 72 insertions(+), 63 deletions(-) diff --git a/test/BUILD.gn b/test/BUILD.gn index b752778..627b32e 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -22,8 +22,8 @@ group("test") { if (distributedhardware_function_test) { deps += [ - "injecttest/server:distributed_input_inject_server_test", "injecttest/client:distributed_input_inject_client_test", + "injecttest/server:distributed_input_inject_server_test", ] } } diff --git a/test/injecttest/client/BUILD.gn b/test/injecttest/client/BUILD.gn index 7e24e19..fbec8ef 100644 --- a/test/injecttest/client/BUILD.gn +++ b/test/injecttest/client/BUILD.gn @@ -12,8 +12,7 @@ # limitations under the License. import("//build/test.gni") -import( - "//foundation/distributedhardware/distributed_input/distributedinput.gni") +import("../../../distributedinput.gni") module_out_path = "distributed_input/injecttest" diff --git a/test/injecttest/client/include/distributed_input_test_client.h b/test/injecttest/client/include/distributed_input_test_client.h index 7e3b2e3..f09b97e 100644 --- a/test/injecttest/client/include/distributed_input_test_client.h +++ b/test/injecttest/client/include/distributed_input_test_client.h @@ -16,9 +16,6 @@ #ifndef DISTRIBUTED_INPUT_TEST_CLIENT_H #define DISTRIBUTED_INPUT_TEST_CLIENT_H -#include -#include "linux/uinput.h" - #include "socket_client.h" namespace OHOS { @@ -32,6 +29,7 @@ public: private: SocketClient socketCli; bool SetPhys(const std::string deviceName); + bool CreateNode(); bool InjectEvent(input_event event); bool CreateKey(); void ProcRes(bool result); diff --git a/test/injecttest/client/include/socket_client.h b/test/injecttest/client/include/socket_client.h index 1478300..a58c188 100644 --- a/test/injecttest/client/include/socket_client.h +++ b/test/injecttest/client/include/socket_client.h @@ -16,19 +16,8 @@ #ifndef SOCKET_CLIENT_H #define SOCKET_CLIENT_H -#include -#include -#include -#include -#include -#include - -#include -#include -#include - #include -#include +#include #include #include #include diff --git a/test/injecttest/client/src/distributed_input_test_client.cpp b/test/injecttest/client/src/distributed_input_test_client.cpp index a2ae7fe..69b48c2 100644 --- a/test/injecttest/client/src/distributed_input_test_client.cpp +++ b/test/injecttest/client/src/distributed_input_test_client.cpp @@ -22,6 +22,8 @@ #include #include "linux/input-event-codes.h" +#include +#include "linux/uinput.h" #include "ipc_skeleton.h" #include "nlohmann/json.hpp" @@ -69,7 +71,7 @@ bool DistributedInputTestClient::SetPhys(const std::string deviceName) bool DistributedInputTestClient::CreateKey() { - const std::vector eventType{ EV_KEY, EV_REL, EV_MSC }; + const std::vector eventType { EV_KEY, EV_REL, EV_MSC }; for (auto it : eventType) { if (ioctl(fd_, UI_SET_EVBIT, it) < 0) { DHLOGE("CreateKye ioctl(fd_, UI_SET_EVBIT, it) failed!"); @@ -97,7 +99,7 @@ bool DistributedInputTestClient::CreateKey() return true; } -bool DistributedInputTestClient::InjectEvent(input_event event) +bool DistributedInputTestClient::CreateNode() { fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK); if (fd_ < 0) { @@ -107,6 +109,7 @@ bool DistributedInputTestClient::InjectEvent(input_event event) std::string deviceName_ = "virtual mouse"; if (strncpy_s(dev_.name, sizeof(dev_.name), deviceName_.c_str(), deviceName_.size()) != 0) { + DHLOGE("Failed to set device name"); return false; } dev_.id.bustype = 0000; @@ -130,30 +133,34 @@ bool DistributedInputTestClient::InjectEvent(input_event event) } if (ioctl(fd_, UI_DEV_CREATE) < 0) { - DHLOGE( - "fd = %d, ioctl(fd_, UI_DEV_CREATE) = %d", - fd_, ioctl(fd_, UI_DEV_CREATE)); - DHLOGE("Unable to create input device"); + DHLOGE("fd = %d, ioctl(fd_, UI_DEV_CREATE) failed", fd_); return false; } - DHLOGI("create fd %d", fd_); + DHLOGI("create fd %d", fd_); char sysfsDeviceName[16] = {0}; if (ioctl(fd_, UI_GET_SYSNAME(sizeof(sysfsDeviceName)), sysfsDeviceName) < 0) { DHLOGE("Unable to get input device name"); } DHLOGI("get input device name: %s, fd: %d", sysfsDeviceName, fd_); +} +bool DistributedInputTestClient::InjectEvent(input_event event) +{ + if (CreateNode()) { + DHLOGE("create node fail"); + return false; + } socketCli.PushRequest("open listen thread"); sleep(WAIT_DH_SYNC_TIME); - DHLOGI("InjectInputEvent %d", fd_); + DHLOGI("InjectEvent Inject down event start %d", fd_); if (write(fd_, &event, sizeof(event)) < static_cast(sizeof(event))) { DHLOGE("could not inject event, removed? (fd: %d", fd_); return false; } - DHLOGI("InjectInputEvent synEvent %d", fd_); + DHLOGI("InjectEvent inject sync event start %d", fd_); struct input_event syncEvent = { .type = EV_SYN, .code = 0, diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index bc415be..b919651 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -15,6 +15,17 @@ #include "socket_client.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include + #include #include @@ -37,7 +48,7 @@ int32_t SocketClient::Init(std::string ip) DHLOGI("Init Socket Client"); sock_cli = socket(AF_INET, SOCK_STREAM, 0); struct sockaddr_in servaddr; - memset(&servaddr, 0, sizeof(servaddr)); + (void)memset_s(&servaddr, sizeof(servaddr), 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(MYPORT); servaddr.sin_addr.s_addr = inet_addr(ip.c_str()); @@ -108,7 +119,7 @@ void SocketClient::ParseRecvSvrMsg(std::string msg) void SocketClient::DoRecvSvrRsp() { - while(true) { + while (true) { FD_ZERO(&rfds); FD_SET(0, &rfds); maxfd = 0; @@ -125,20 +136,21 @@ void SocketClient::DoRecvSvrRsp() } else if (retval == 0) { continue; } else { - if (FD_ISSET(sock_cli, &rfds)) { - char recvbuf[BUFFER_SIZE]; - int32_t len; - len = recv(sock_cli, recvbuf, sizeof(recvbuf), 0); - if (len == 0) { - DHLOGI("empty msg, drop it"); - continue; - } - DHLOGI("Receive Server Rsp: %s", recvbuf); - std::string svrRsp(recvbuf, len); - std::cout << svrRsp << std::endl; - ParseRecvSvrMsg(svrRsp); - memset(recvbuf, 0, sizeof(recvbuf)); + if (FD_ISSET(sock_cli, &rfds) <= 0) { + DHLOGE("Socket Client FD_ISSET error"); + continue; + } + char recvbuf[BUFFER_SIZE]; + int32_t len = recv(sock_cli, recvbuf, sizeof(recvbuf), 0); + if (len == 0) { + DHLOGI("empty msg, drop it"); + continue; } + DHLOGI("Receive Server Rsp: %s", recvbuf); + std::string svrRsp(recvbuf, len); + std::cout << svrRsp << std::endl; + ParseRecvSvrMsg(svrRsp); + (void)memset_s(recvbuf, sizeof(recvbuf), 0, sizeof(recvbuf)); } } } diff --git a/test/injecttest/server/BUILD.gn b/test/injecttest/server/BUILD.gn index 587f60f..dde2470 100644 --- a/test/injecttest/server/BUILD.gn +++ b/test/injecttest/server/BUILD.gn @@ -12,8 +12,7 @@ # limitations under the License. import("//build/test.gni") -import( - "//foundation/distributedhardware/distributed_input/distributedinput.gni") +import("../../../distributedinput.gni") module_out_path = "distributed_input/injecttest" diff --git a/test/injecttest/server/include/dinput_dbg_itf.h b/test/injecttest/server/include/dinput_dbg_itf.h index dde5178..703e612 100644 --- a/test/injecttest/server/include/dinput_dbg_itf.h +++ b/test/injecttest/server/include/dinput_dbg_itf.h @@ -21,7 +21,7 @@ #include "distributed_input_test_server.h" -namespace OHOS{ +namespace OHOS { namespace DistributedHardware { namespace DistributedInput { class DInputDBGItf { @@ -31,7 +31,6 @@ public: private: std::shared_ptr dinputTestServerPtr_; }; - } } } diff --git a/test/injecttest/server/src/dinput_dbg_itf.cpp b/test/injecttest/server/src/dinput_dbg_itf.cpp index bebc7d8..f3f8669 100644 --- a/test/injecttest/server/src/dinput_dbg_itf.cpp +++ b/test/injecttest/server/src/dinput_dbg_itf.cpp @@ -18,7 +18,7 @@ #include "dinput_log.h" #include "distributed_input_test_server.h" -namespace OHOS{ +namespace OHOS { namespace DistributedHardware { namespace DistributedInput { DInputDBGItf::DInputDBGItf() diff --git a/test/injecttest/server/src/distributed_input_test_server.cpp b/test/injecttest/server/src/distributed_input_test_server.cpp index 317672a..b00278a 100644 --- a/test/injecttest/server/src/distributed_input_test_server.cpp +++ b/test/injecttest/server/src/distributed_input_test_server.cpp @@ -46,19 +46,22 @@ namespace { std::vector DistributedInputTestServer::dhIds_ = {}; -void DistributedInputTestServer::TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) +void DistributedInputTestServer::TestPrepareDInputCallback::OnResult(const std::string& deviceId, + const int32_t& status) { DHLOGI("DistributedInputTestServer::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); return; } -void DistributedInputTestServer::TestStartDInputCallback::OnResultDhids(const std::string &devId, const int32_t &status) +void DistributedInputTestServer::TestStartDInputCallback::OnResultDhids(const std::string &devId, + const int32_t &status) { - DHLOGI("DistributedInputTestServer::TestStartDInputCallback::OnResult: devId = %s, status = %d", devId.c_str(), status); + DHLOGI("DistributedInputTestServer::TestStartDInputCallback::OnResult: devId = %s, status = %d", + devId.c_str(), status); } -void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, - const std::string sinkNodeId, const std::string nodeDesc) +void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, + const std::string sinkDevId, const std::string sinkNodeId, const std::string nodeDesc) { nlohmann::json jsonStr; jsonStr["srcDevId"] = srcDevId; @@ -76,8 +79,8 @@ void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std:: return; } -void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, - const std::string sinkNodeId) +void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, + const std::string sinkDevId, const std::string sinkNodeId) { std::string rsp = "TestInputNodeListener::OnNodeOffLine\n"; DHLOGI("TestInputNodeListener::OnNodeOffLine: %s", rsp.c_str()); @@ -89,7 +92,8 @@ void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std: return; } -int32_t DistributedInputTestServer::TestSimulationEventListener::OnSimulationEvent(uint32_t type, uint32_t code, int32_t value) +int32_t DistributedInputTestServer::TestSimulationEventListener::OnSimulationEvent(uint32_t type, + uint32_t code, int32_t value) { nlohmann::json jsonStr; jsonStr["eventType"] = type; @@ -123,7 +127,8 @@ DistributedInputTestServer::DistributedInputTestServer():inputNodeListener_(new DHLOGI("Init Socker Server"); GetRemoteDeviceId(); socketServerPtr_->Init(); - socketServerPtr_->RegisterRequestCallback(std::bind(&DistributedInputTestServer::ProcRequest, this, std::placeholders::_1)); + socketServerPtr_->RegisterRequestCallback(std::bind(&DistributedInputTestServer::ProcRequest, + this, std::placeholders::_1)); socketServerStaticPtr = socketServerPtr_; int32_t ret = DistributedInputKit::RegisterInputNodeListener(inputNodeListener_); @@ -148,7 +153,7 @@ void DistributedInputTestServer::ListenerEvent() } pfd[0].fd = fd; pfd[0].events = POLLIN; - while(true) { + while (true) { if (poll(pfd, nfd, -1) > 0) { len = read(fd, &event, sizeof(event)); if (len < 0) { diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp index af0326b..20b621d 100644 --- a/test/injecttest/server/src/socket_server.cpp +++ b/test/injecttest/server/src/socket_server.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "dinput_log.h" @@ -35,11 +36,12 @@ namespace { constexpr int32_t SEND_MSG_SPACE = 100; constexpr int32_t BLOCK_TIME = 10; } + void SocketServer::Init() { DHLOGI("Init DBGSvr"); s = socket(AF_INET, SOCK_STREAM, 0); - memset(&servaddr, 0, sizeof(servaddr)); + (void)memset(&servaddr, sizeof(servaddr), 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); @@ -66,7 +68,7 @@ void SocketServer::Init() void SocketServer::GetConn() { - while(true) { + while (true) { int32_t conn = accept(s, (struct sockaddr *)&servaddr, &len); li.push_back(conn); DHLOGI("receive client connect, fd: %d\n", conn); @@ -105,7 +107,7 @@ void SocketServer::GetData() DHLOGI("not message"); } else { char buf[MAX_MSG_LENGTH]; - memset(buf, 0, sizeof(buf)); + (void)memset_s(buf, sizeof(buf), 0, sizeof(buf)); int32_t len = recv(*it, buf, sizeof(buf), 0); DHLOGI("Server Recv Msg: %s, len: %d", buf, len); if (len > 0) { @@ -122,7 +124,7 @@ void SocketServer::GetData() void SocketServer::SendMessage() { - while(true) { + while (true) { std::unique_lock lock(rspMtx_); rspQueueConVar_.wait(lock, [this] { return !(this->responseQueue_.empty()); @@ -160,8 +162,7 @@ void SocketServer::ProcRequest() void SocketServer::DoSendMessage(std::string msg) { char buf[msg.length() + 1]; - strcpy(buf, msg.c_str()); - + strcpy_s(buf, msg.c_str()); DHLOGI("Server Send Message: %s", buf); if (li.size() == 0) { -- Gitee From f79571c047a2e16368f0babe4680620231b6c9f8 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Fri, 28 Jul 2023 09:00:13 +0800 Subject: [PATCH 05/10] add inject event test Signed-off-by: wangxuanxuan --- test/injecttest/client/BUILD.gn | 2 +- .../include/distributed_input_test_client.h | 3 + .../injecttest/client/include/socket_client.h | 3 + .../src/distributed_input_test_client.cpp | 20 +++--- test/injecttest/client/src/socket_client.cpp | 7 +-- .../client/src/socket_main_test.cpp | 3 +- test/injecttest/server/BUILD.gn | 6 +- .../include/distributed_input_test_server.h | 17 +++-- .../injecttest/server/include/socket_server.h | 1 + .../src/distributed_input_test_server.cpp | 19 ++++-- test/injecttest/server/src/socket_server.cpp | 63 ++++++++++--------- 11 files changed, 89 insertions(+), 55 deletions(-) diff --git a/test/injecttest/client/BUILD.gn b/test/injecttest/client/BUILD.gn index fbec8ef..fe8c400 100644 --- a/test/injecttest/client/BUILD.gn +++ b/test/injecttest/client/BUILD.gn @@ -32,9 +32,9 @@ ohos_unittest("distributed_input_inject_client_test") { ] sources = [ + "./src/distributed_input_test_client.cpp", "./src/socket_client.cpp", "./src/socket_main_test.cpp", - "./src/distributed_input_test_client.cpp", ] cflags = [ diff --git a/test/injecttest/client/include/distributed_input_test_client.h b/test/injecttest/client/include/distributed_input_test_client.h index f09b97e..c998c8c 100644 --- a/test/injecttest/client/include/distributed_input_test_client.h +++ b/test/injecttest/client/include/distributed_input_test_client.h @@ -16,6 +16,9 @@ #ifndef DISTRIBUTED_INPUT_TEST_CLIENT_H #define DISTRIBUTED_INPUT_TEST_CLIENT_H +#include +#include "linux/uinput.h" + #include "socket_client.h" namespace OHOS { diff --git a/test/injecttest/client/include/socket_client.h b/test/injecttest/client/include/socket_client.h index a58c188..c682055 100644 --- a/test/injecttest/client/include/socket_client.h +++ b/test/injecttest/client/include/socket_client.h @@ -16,6 +16,9 @@ #ifndef SOCKET_CLIENT_H #define SOCKET_CLIENT_H +#include +#include + #include #include #include diff --git a/test/injecttest/client/src/distributed_input_test_client.cpp b/test/injecttest/client/src/distributed_input_test_client.cpp index 69b48c2..c4d17fc 100644 --- a/test/injecttest/client/src/distributed_input_test_client.cpp +++ b/test/injecttest/client/src/distributed_input_test_client.cpp @@ -16,25 +16,24 @@ #include "distributed_input_test_client.h" #include +#include #include #include #include #include #include "linux/input-event-codes.h" -#include -#include "linux/uinput.h" #include "ipc_skeleton.h" #include "nlohmann/json.hpp" #include "nativetoken_kit.h" +#include "softbus_bus_center.h" #include "token_setproc.h" #include "constants_dinput.h" #include "dinput_errcode.h" #include "dinput_log.h" #include "idistributed_hardware_source.h" -#include "softbus_bus_center.h" namespace OHOS { namespace DistributedHardware { @@ -43,6 +42,8 @@ namespace { const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; constexpr int32_t WAIT_DH_SYNC_TIME = 5; constexpr int32_t KEY_VALUE_DOWN = 1; + constexpr int32_t DEFAULT_ID_PARAM = 0000; + constexpr int32_t DEVICE_NAME_LEN = 16; } DistributedInputTestClient::DistributedInputTestClient(std::string ip) @@ -112,10 +113,10 @@ bool DistributedInputTestClient::CreateNode() DHLOGE("Failed to set device name"); return false; } - dev_.id.bustype = 0000; - dev_.id.vendor = 0000; - dev_.id.product = 0000; - dev_.id.version = 0000; + dev_.id.bustype = DEFAULT_ID_PARAM; + dev_.id.vendor = DEFAULT_ID_PARAM; + dev_.id.product = DEFAULT_ID_PARAM; + dev_.id.version = DEFAULT_ID_PARAM; if (!SetPhys(deviceName_)) { DHLOGE("Failed to set PHYS!"); @@ -138,16 +139,17 @@ bool DistributedInputTestClient::CreateNode() } DHLOGI("create fd %d", fd_); - char sysfsDeviceName[16] = {0}; + char sysfsDeviceName[DEVICE_NAME_LEN] = {0}; if (ioctl(fd_, UI_GET_SYSNAME(sizeof(sysfsDeviceName)), sysfsDeviceName) < 0) { DHLOGE("Unable to get input device name"); } DHLOGI("get input device name: %s, fd: %d", sysfsDeviceName, fd_); + return true; } bool DistributedInputTestClient::InjectEvent(input_event event) { - if (CreateNode()) { + if (!CreateNode()) { DHLOGE("create node fail"); return false; } diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index b919651..23d68d4 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -18,14 +18,13 @@ #include #include #include -#include #include -#include #include #include #include +#include #include #include @@ -99,7 +98,7 @@ void SocketClient::SendRequest() void SocketClient::DoSendRequest(std::string msg) { char buf[msg.length() + 1]; - strcpy(buf, msg.c_str()); + (void)strcpy_s(buf, msg.length() + 1, msg.c_str()); DHLOGI("Client Send Message: %s", buf); send(sock_cli, buf, strlen(buf), 0); } @@ -107,7 +106,7 @@ void SocketClient::DoSendRequest(std::string msg) void SocketClient::ParseRecvSvrMsg(std::string msg) { DHLOGI("Client Recv Message: %s", msg.c_str()); - nlohmann::json jsonObj = nlohmann::json::parse(msg); + nlohmann::json jsonObj = nlohmann::json::parse(msg, nullptr, false); if (jsonObj.find("injectResult") == jsonObj.end()) { DHLOGE("server msg is not rsp msg"); return; diff --git a/test/injecttest/client/src/socket_main_test.cpp b/test/injecttest/client/src/socket_main_test.cpp index 269d3e7..dc61db8 100644 --- a/test/injecttest/client/src/socket_main_test.cpp +++ b/test/injecttest/client/src/socket_main_test.cpp @@ -30,7 +30,7 @@ using namespace OHOS::Security::AccessToken; static constexpr int32_t PERMISSION_NUM = 2; static constexpr int32_t ARGC_NUM = 2; -static constexpr int32_t TEST_WAIT_TIME = 10; +static constexpr int32_t TEST_WAIT_TIME = 5; class SocketMainTest : public testing::Test { public: @@ -75,6 +75,7 @@ int32_t main(int32_t argc, char **argv) } std::string ip = argv[1]; OHOS::DistributedHardware::DistributedInput::DistributedInputTestClient inputTestClient(ip); + sleep(TEST_WAIT_TIME); inputTestClient.MakeVirtualEvent(); sleep(TEST_WAIT_TIME); EXPECT_EQ(true, inputTestClient.injectResult_); diff --git a/test/injecttest/server/BUILD.gn b/test/injecttest/server/BUILD.gn index dde2470..33840ca 100644 --- a/test/injecttest/server/BUILD.gn +++ b/test/injecttest/server/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021-2023 Huawei Device Co., Ltd. +# Copyright (c) 2023 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -32,10 +32,10 @@ ohos_unittest("distributed_input_inject_server_test") { ] sources = [ - "./src/socket_server.cpp", - "./src/socket_main_test.cpp", "./src/dinput_dbg_itf.cpp", "./src/distributed_input_test_server.cpp", + "./src/socket_main_test.cpp", + "./src/socket_server.cpp", ] cflags = [ diff --git a/test/injecttest/server/include/distributed_input_test_server.h b/test/injecttest/server/include/distributed_input_test_server.h index ec8cc71..e3e189a 100644 --- a/test/injecttest/server/include/distributed_input_test_server.h +++ b/test/injecttest/server/include/distributed_input_test_server.h @@ -40,7 +40,6 @@ namespace DistributedHardware { namespace DistributedInput { class DistributedInputTestServer { public: - void ProcRequest(std::string msg); DistributedInputTestServer(); ~DistributedInputTestServer(); class TestPrepareDInputCallback : @@ -50,11 +49,18 @@ public: virtual ~TestPrepareDInputCallback() = default; void OnResult(const std::string& deviceId, const int32_t& status); }; - class TestStartDInputCallback : + class TestUnprepareDInputCallback : + public OHOS::DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { + public: + TestUnprepareDInputCallback() = default; + virtual ~TestUnprepareDInputCallback() = default; + void OnResult(const std::string& deviceId, const int32_t& status); + }; + class TestStartStopDInputCallback : public OHOS::DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { public: - TestStartDInputCallback() = default; - virtual ~TestStartDInputCallback() = default; + TestStartStopDInputCallback() = default; + virtual ~TestStartStopDInputCallback() = default; void OnResultDhids(const std::string &devId, const int32_t &status); }; class TestInputNodeListener : @@ -74,6 +80,9 @@ public: int32_t OnSimulationEvent(uint32_t type, uint32_t code, int32_t value); }; +public: + void ProcRequest(std::string msg); + private: void GetRemoteDeviceId(); void ListenerEvent(); diff --git a/test/injecttest/server/include/socket_server.h b/test/injecttest/server/include/socket_server.h index 2fc6efe..4128e35 100644 --- a/test/injecttest/server/include/socket_server.h +++ b/test/injecttest/server/include/socket_server.h @@ -45,6 +45,7 @@ public: private: void GetConn(); void GetData(); + void GetDataByFd(int32_t fd); void SendMessage(); void DoSendMessage(std::string msg); void DoSendMessagePure(std::string msg, int32_t conn); diff --git a/test/injecttest/server/src/distributed_input_test_server.cpp b/test/injecttest/server/src/distributed_input_test_server.cpp index b00278a..b7bf5ab 100644 --- a/test/injecttest/server/src/distributed_input_test_server.cpp +++ b/test/injecttest/server/src/distributed_input_test_server.cpp @@ -49,14 +49,21 @@ std::vector DistributedInputTestServer::dhIds_ = {}; void DistributedInputTestServer::TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) { - DHLOGI("DistributedInputTestServer::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); + DHLOGI("TestPrepareDInputCallback::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); return; } -void DistributedInputTestServer::TestStartDInputCallback::OnResultDhids(const std::string &devId, +void DistributedInputTestServer::TestUnprepareDInputCallback::OnResult(const std::string& deviceId, + const int32_t& status) +{ + DHLOGI("TestUnprepareDInputCallback::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); + return; +} + +void DistributedInputTestServer::TestStartStopDInputCallback::OnResultDhids(const std::string &devId, const int32_t &status) { - DHLOGI("DistributedInputTestServer::TestStartDInputCallback::OnResult: devId = %s, status = %d", + DHLOGI("DistributedInputTestServer::TestStartStopDInputCallback::OnResult: devId = %s, status = %d", devId.c_str(), status); } @@ -174,6 +181,10 @@ void DistributedInputTestServer::ListenerEvent() DistributedInputTestServer::~DistributedInputTestServer() { DHLOGI("DistributedInputTestServer destruction"); + auto stopCb = new(std::nothrow) TestStartStopDInputCallback(); + DistributedInputKit::StopRemoteInput(remoteDevId_, dhIds_, stopCb); + auto unprepareCb = new(std::nothrow) TestUnprepareDInputCallback(); + DistributedInputKit::UnprepareRemoteInput(remoteDevId_, unprepareCb); } void DistributedInputTestServer::ProcRequest(std::string msg) @@ -181,7 +192,7 @@ void DistributedInputTestServer::ProcRequest(std::string msg) DHLOGI("DistributedInputTestServer ProcRequest"); sleep(WAIT_DH_SYNC_TIME); std::thread(&DistributedInputTestServer::ListenerEvent, this).detach(); - auto startCb = new(std::nothrow) TestStartDInputCallback(); + auto startCb = new(std::nothrow) TestStartStopDInputCallback(); DistributedInputKit::StartRemoteInput(remoteDevId_, dhIds_, startCb); } } diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp index 20b621d..ff999a4 100644 --- a/test/injecttest/server/src/socket_server.cpp +++ b/test/injecttest/server/src/socket_server.cpp @@ -41,7 +41,7 @@ void SocketServer::Init() { DHLOGI("Init DBGSvr"); s = socket(AF_INET, SOCK_STREAM, 0); - (void)memset(&servaddr, sizeof(servaddr), 0, sizeof(servaddr)); + (void)memset_s(&servaddr, sizeof(servaddr), 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); @@ -84,39 +84,44 @@ void SocketServer::GetConn() } } -void SocketServer::GetData() +void SocketServer::GetDataByFd(int32_t fd) { struct timeval tv; tv.tv_sec = BLOCK_TIME; tv.tv_usec = 0; + fd_set rfds; + FD_ZERO(&rfds); + int32_t maxfd = 0; + int32_t retval = 0; + FD_SET(fd, &rfds); + if (maxfd < fd) { + maxfd = fd; + } + retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); + if (retval == -1) { + DHLOGE("Server select error, ret: %d", retval); + } else if (retval == 0) { + DHLOGI("not message"); + } else { + char buf[MAX_MSG_LENGTH]; + (void)memset_s(buf, sizeof(buf), 0, sizeof(buf)); + int32_t len = recv(fd, buf, sizeof(buf), 0); + DHLOGI("Server Recv Msg: %s, len: %d", buf, len); + if (len > 0) { + std::string msg(buf, len); + std::lock_guard lock(reqMtx_); + requestQueue_.push(msg); + reqQueueConVar_.notify_all(); + } + } +} + +void SocketServer::GetData() +{ while (true) { std::list::iterator it; for (it = li.begin(); it != li.end(); ++it) { - fd_set rfds; - FD_ZERO(&rfds); - int32_t maxfd = 0; - int32_t retval = 0; - FD_SET(*it, &rfds); - if (maxfd < *it) { - maxfd = *it; - } - retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); - if (retval == -1) { - DHLOGE("Server select error, ret: %d", retval); - } else if (retval == 0) { - DHLOGI("not message"); - } else { - char buf[MAX_MSG_LENGTH]; - (void)memset_s(buf, sizeof(buf), 0, sizeof(buf)); - int32_t len = recv(*it, buf, sizeof(buf), 0); - DHLOGI("Server Recv Msg: %s, len: %d", buf, len); - if (len > 0) { - std::string msg(buf, len); - std::lock_guard lock(reqMtx_); - requestQueue_.push(msg); - reqQueueConVar_.notify_all(); - } - } + GetDataByFd(*it); } sleep(1); } @@ -162,7 +167,7 @@ void SocketServer::ProcRequest() void SocketServer::DoSendMessage(std::string msg) { char buf[msg.length() + 1]; - strcpy_s(buf, msg.c_str()); + (void)strcpy_s(buf, msg.length() + 1, msg.c_str()); DHLOGI("Server Send Message: %s", buf); if (li.size() == 0) { @@ -180,7 +185,7 @@ void SocketServer::DoSendMessage(std::string msg) void SocketServer::DoSendMessagePure(std::string msg, int32_t conn) { char buf[msg.length() + 1]; - strcpy(buf, msg.c_str()); + (void)strcpy_s(buf, msg.length() + 1, msg.c_str()); DHLOGI("Server Send Message Pure: %s", buf); -- Gitee From e7b4f3181cef1fb47271e067f21899fc2641367a Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Fri, 28 Jul 2023 09:40:43 +0800 Subject: [PATCH 06/10] add inject event test Signed-off-by: wangxuanxuan --- test/injecttest/client/src/socket_client.cpp | 7 ++++++- test/injecttest/server/src/socket_server.cpp | 13 +++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index 23d68d4..4e51025 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -28,6 +28,7 @@ #include #include +#include "dinput_errcode.h" #include "dinput_log.h" #include "nlohmann/json.hpp" @@ -98,7 +99,11 @@ void SocketClient::SendRequest() void SocketClient::DoSendRequest(std::string msg) { char buf[msg.length() + 1]; - (void)strcpy_s(buf, msg.length() + 1, msg.c_str()); + int32_t ret = strcpy_s(buf, msg.length() + 1, msg.c_str()); + if (ret != DH_SUCCESS) { + DHLOGE("DoSendRequest strcpy_s failed"); + return; + } DHLOGI("Client Send Message: %s", buf); send(sock_cli, buf, strlen(buf), 0); } diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp index ff999a4..bddb141 100644 --- a/test/injecttest/server/src/socket_server.cpp +++ b/test/injecttest/server/src/socket_server.cpp @@ -23,6 +23,7 @@ #include #include +#include "dinput_errcode.h" #include "dinput_log.h" namespace OHOS { @@ -167,7 +168,11 @@ void SocketServer::ProcRequest() void SocketServer::DoSendMessage(std::string msg) { char buf[msg.length() + 1]; - (void)strcpy_s(buf, msg.length() + 1, msg.c_str()); + int32_t ret = strcpy_s(buf, msg.length() + 1, msg.c_str()); + if (ret != DH_SUCCESS) { + DHLOGE("DoSendMessage strcpy_s failed"); + return; + } DHLOGI("Server Send Message: %s", buf); if (li.size() == 0) { @@ -185,7 +190,11 @@ void SocketServer::DoSendMessage(std::string msg) void SocketServer::DoSendMessagePure(std::string msg, int32_t conn) { char buf[msg.length() + 1]; - (void)strcpy_s(buf, msg.length() + 1, msg.c_str()); + int32_t ret = strcpy_s(buf, msg.length() + 1, msg.c_str()); + if (ret != DH_SUCCESS) { + DHLOGE("DoSendMessagePure strcpy_s failed"); + return; + } DHLOGI("Server Send Message Pure: %s", buf); -- Gitee From a0fb2d5b6432c69b7c3fbdde749c6681fbb378a2 Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Sat, 29 Jul 2023 09:02:02 +0800 Subject: [PATCH 07/10] add inject event test Signed-off-by: wangxuanxuan --- test/injecttest/client/BUILD.gn | 10 +++++----- test/injecttest/client/Test.json | 21 +++++++++++++++++++++ test/injecttest/server/BUILD.gn | 7 +++++-- test/injecttest/server/Test.json | 21 +++++++++++++++++++++ 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 test/injecttest/client/Test.json create mode 100644 test/injecttest/server/Test.json diff --git a/test/injecttest/client/BUILD.gn b/test/injecttest/client/BUILD.gn index fe8c400..b9718c9 100644 --- a/test/injecttest/client/BUILD.gn +++ b/test/injecttest/client/BUILD.gn @@ -11,13 +11,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//build/config/features.gni") import("//build/test.gni") +import("//test/xts/tools/build/suite.gni") import("../../../distributedinput.gni") module_out_path = "distributed_input/injecttest" ## UnitTest distributed_input_inject_client_test {{{ -ohos_unittest("distributed_input_inject_client_test") { +ohos_moduletest_suite("distributed_input_inject_client_test") { module_out_path = module_out_path include_dirs = [ @@ -38,9 +40,6 @@ ohos_unittest("distributed_input_inject_client_test") { ] cflags = [ - "-Wall", - "-Werror", - "-g3", "-Dprivate=public", "-Dprotected=public", ] @@ -67,6 +66,7 @@ ohos_unittest("distributed_input_inject_client_test") { "ipc:ipc_core", ] - cflags_cc = [ "-DHILOG_ENABLE" ] + subsystem_name = "distributedhardware" + part_name = "distributed_input" } ## UnitTest distributed_input_inject_client_test }}} diff --git a/test/injecttest/client/Test.json b/test/injecttest/client/Test.json new file mode 100644 index 0000000..3c926ce --- /dev/null +++ b/test/injecttest/client/Test.json @@ -0,0 +1,21 @@ +{ + "description": "Config for disInput test cases", + "driver": { + "module-name": "distributed_input_inject_client_test", + "native-test-timeout": "120000", + "native-test-device-path": "/data/local/tmp", + "runtime-hint": "1s", + "type": "CppTest" + }, + "kits": [ + { + "post-push" : [ + "chmod -R 777 /data/local/tmp/*" + ], + "push": [ + "distributed_input_inject_client_test->/data/local/tmp/distributed_input_inject_client_test" + ], + "type": "PushKit" + } + ] +} \ No newline at end of file diff --git a/test/injecttest/server/BUILD.gn b/test/injecttest/server/BUILD.gn index 33840ca..d93a8e7 100644 --- a/test/injecttest/server/BUILD.gn +++ b/test/injecttest/server/BUILD.gn @@ -11,13 +11,15 @@ # See the License for the specific language governing permissions and # limitations under the License. +import("//build/config/features.gni") import("//build/test.gni") +import("//test/xts/tools/build/suite.gni") import("../../../distributedinput.gni") module_out_path = "distributed_input/injecttest" ## UnitTest distributed_input_inject_server_test {{{ -ohos_unittest("distributed_input_inject_server_test") { +ohos_moduletest_suite("distributed_input_inject_server_test") { module_out_path = module_out_path include_dirs = [ @@ -68,6 +70,7 @@ ohos_unittest("distributed_input_inject_server_test") { "ipc:ipc_core", ] - cflags_cc = [ "-DHILOG_ENABLE" ] + subsystem_name = "distributedhardware" + part_name = "distributed_input" } ## UnitTest distributed_input_inject_server_test }}} diff --git a/test/injecttest/server/Test.json b/test/injecttest/server/Test.json new file mode 100644 index 0000000..521583e --- /dev/null +++ b/test/injecttest/server/Test.json @@ -0,0 +1,21 @@ +{ + "description": "Config for disInput test cases", + "driver": { + "module-name": "distributed_input_inject_server_test", + "native-test-timeout": "120000", + "native-test-device-path": "/data/local/tmp", + "runtime-hint": "1s", + "type": "CppTest" + }, + "kits": [ + { + "post-push" : [ + "chmod -R 777 /data/local/tmp/*" + ], + "push": [ + "distributed_input_inject_server_test->/data/local/tmp/distributed_input_inject_server_test" + ], + "type": "PushKit" + } + ] +} \ No newline at end of file -- Gitee From cd39fe34dac772348bd538af6949faa807bb2bdd Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Sat, 29 Jul 2023 11:40:54 +0800 Subject: [PATCH 08/10] add inject event test Signed-off-by: wangxuanxuan --- .../client/src/distributed_input_test_client.cpp | 3 ++- test/injecttest/client/src/socket_client.cpp | 11 ++++++----- .../server/src/distributed_input_test_server.cpp | 12 ++++-------- test/injecttest/server/src/socket_server.cpp | 11 ++++++----- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/test/injecttest/client/src/distributed_input_test_client.cpp b/test/injecttest/client/src/distributed_input_test_client.cpp index c4d17fc..e858c9d 100644 --- a/test/injecttest/client/src/distributed_input_test_client.cpp +++ b/test/injecttest/client/src/distributed_input_test_client.cpp @@ -65,6 +65,7 @@ bool DistributedInputTestClient::SetPhys(const std::string deviceName) phys.append(deviceName); if (ioctl(fd_, UI_SET_PHYS, phys.c_str()) < 0) { + DHLOGE("DistributedInputTestClient SetPhys failed"); return false; } return true; @@ -178,12 +179,12 @@ bool DistributedInputTestClient::InjectEvent(input_event event) void DistributedInputTestClient::MakeVirtualEvent() { + DHLOGI("MakeVirtualEvent start"); struct input_event event = { .type = EV_KEY, .code = BTN_LEFT, .value = KEY_VALUE_DOWN }; - DHLOGI("MakeVirtualEvent start"); InjectEvent(event); } diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index 4e51025..900cb0c 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -41,6 +41,7 @@ namespace { constexpr int32_t MAX_MSG_LENGTH = 4096; constexpr int32_t BUFF_MAX_LEN = 32 * 1024; constexpr int32_t BLOCK_TIME = 10; + constexpr int32_t SOCKET_ERR = -1; } int32_t SocketClient::Init(std::string ip) @@ -55,7 +56,7 @@ int32_t SocketClient::Init(std::string ip) if (connect(sock_cli, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { DHLOGE("Connect Socker Server failed"); - return -1; + return SOCKET_ERR; } int32_t rBuf = BUFF_MAX_LEN; @@ -72,9 +73,9 @@ int32_t SocketClient::Init(std::string ip) int32_t SocketClient::Release() { DHLOGI("Release Socket client, Close Client Conn"); - if (sock_cli != -1) { + if (sock_cli != SOCKET_ERR) { close(sock_cli); - sock_cli = -1; + sock_cli = SOCKET_ERR; } return 0; } @@ -134,7 +135,7 @@ void SocketClient::DoRecvSvrRsp() tv.tv_sec = BLOCK_TIME; tv.tv_usec = 0; retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); - if (retval == -1) { + if (retval == SOCKET_ERR) { DHLOGE("Socket Client select error"); break; } else if (retval == 0) { @@ -165,7 +166,7 @@ int32_t SocketClient::PushRequest(std::string msg) int32_t msgLen = msg.length(); if (msgLen > MAX_MSG_LENGTH) { DHLOGE("Msg too long, length: %d", msgLen); - return -1; + return SOCKET_ERR; } std::unique_lock lock(reqMtx_); diff --git a/test/injecttest/server/src/distributed_input_test_server.cpp b/test/injecttest/server/src/distributed_input_test_server.cpp index b7bf5ab..b72e368 100644 --- a/test/injecttest/server/src/distributed_input_test_server.cpp +++ b/test/injecttest/server/src/distributed_input_test_server.cpp @@ -50,14 +50,12 @@ void DistributedInputTestServer::TestPrepareDInputCallback::OnResult(const std:: const int32_t& status) { DHLOGI("TestPrepareDInputCallback::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); - return; } void DistributedInputTestServer::TestUnprepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) { DHLOGI("TestUnprepareDInputCallback::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); - return; } void DistributedInputTestServer::TestStartStopDInputCallback::OnResultDhids(const std::string &devId, @@ -79,11 +77,10 @@ void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std:: DHLOGI("TestInputNodeListener::OnNodeOnline: %s", msg.c_str()); DistributedInputTestServer::dhIds_.emplace_back(sinkNodeId); if (socketServerStaticPtr == nullptr) { - DHLOGI("socketServerStaticPtr is null"); + DHLOGE("socketServerStaticPtr is null"); return; } socketServerStaticPtr->PushResponse(msg); - return; } void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, @@ -92,11 +89,10 @@ void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std: std::string rsp = "TestInputNodeListener::OnNodeOffLine\n"; DHLOGI("TestInputNodeListener::OnNodeOffLine: %s", rsp.c_str()); if (socketServerStaticPtr == nullptr) { - DHLOGI("socketServerStaticPtr is null"); + DHLOGE("socketServerStaticPtr is null"); return; } socketServerStaticPtr->PushResponse(rsp); - return; } int32_t DistributedInputTestServer::TestSimulationEventListener::OnSimulationEvent(uint32_t type, @@ -109,7 +105,7 @@ int32_t DistributedInputTestServer::TestSimulationEventListener::OnSimulationEve std::string msg = jsonStr.dump(); DHLOGI("TestSimulationEventListener::OnSimulationEvent: %s", msg.c_str()); if (socketServerStaticPtr == nullptr) { - DHLOGI("socketServerStaticPtr is null"); + DHLOGE("socketServerStaticPtr is null"); return -1; } socketServerStaticPtr->PushResponse(msg); @@ -122,7 +118,7 @@ void DistributedInputTestServer::GetRemoteDeviceId() int32_t infoNum = 0; auto ret = GetAllNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), &info, &infoNum); if (ret != 0) { - DHLOGI("get remote device id fail."); + DHLOGE("get remote device id fail."); return; } remoteDevId_ = info->networkId; diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp index bddb141..9d667eb 100644 --- a/test/injecttest/server/src/socket_server.cpp +++ b/test/injecttest/server/src/socket_server.cpp @@ -36,6 +36,7 @@ namespace { constexpr int32_t BUFF_MAX_LEN = 32 * 1024; constexpr int32_t SEND_MSG_SPACE = 100; constexpr int32_t BLOCK_TIME = 10; + constexpt int32_t SOCKET_ERR = -1; } void SocketServer::Init() @@ -46,11 +47,11 @@ void SocketServer::Init() servaddr.sin_family = AF_INET; servaddr.sin_port = htons(PORT); servaddr.sin_addr.s_addr = htonl(INADDR_ANY); - if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1) { + if (bind(s, (struct sockaddr *)&servaddr, sizeof(servaddr)) == SOCKET_ERR) { DHLOGE("Bind Socket Server error"); return; } - if (listen(s, ESTABLISHED_MAX) == -1) { + if (listen(s, ESTABLISHED_MAX) == SOCKET_ERR) { DHLOGE("Listener Socket Server error"); return; } @@ -99,7 +100,7 @@ void SocketServer::GetDataByFd(int32_t fd) maxfd = fd; } retval = select(maxfd + 1, &rfds, NULL, NULL, &tv); - if (retval == -1) { + if (retval == SOCKET_ERR) { DHLOGE("Server select error, ret: %d", retval); } else if (retval == 0) { DHLOGI("not message"); @@ -225,9 +226,9 @@ void SocketServer::Release() li.clear(); DHLOGI("Close socket fd"); - if (s != -1) { + if (s != SOCKET_ERR) { close(s); - s = -1; + s = SOCKET_ERR; } } -- Gitee From b170677ea80f95b94eef844ed5c021889bcb2cdb Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Tue, 1 Aug 2023 20:47:29 +0800 Subject: [PATCH 09/10] add inject event test Signed-off-by: wangxuanxuan --- .../src/distributed_input_test_client.cpp | 8 +++-- test/injecttest/client/src/socket_client.cpp | 16 +++++---- .../include/distributed_input_test_server.h | 2 ++ .../src/distributed_input_test_server.cpp | 36 +++++++++++-------- test/injecttest/server/src/socket_server.cpp | 14 ++++---- 5 files changed, 44 insertions(+), 32 deletions(-) diff --git a/test/injecttest/client/src/distributed_input_test_client.cpp b/test/injecttest/client/src/distributed_input_test_client.cpp index e858c9d..fc2070a 100644 --- a/test/injecttest/client/src/distributed_input_test_client.cpp +++ b/test/injecttest/client/src/distributed_input_test_client.cpp @@ -33,6 +33,7 @@ #include "constants_dinput.h" #include "dinput_errcode.h" #include "dinput_log.h" +#include "dinput_utils_tool.h" #include "idistributed_hardware_source.h" namespace OHOS { @@ -40,6 +41,7 @@ namespace DistributedHardware { namespace DistributedInput { namespace { const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; + const std::string INPUT_PATH = "/dev/uinput"; constexpr int32_t WAIT_DH_SYNC_TIME = 5; constexpr int32_t KEY_VALUE_DOWN = 1; constexpr int32_t DEFAULT_ID_PARAM = 0000; @@ -48,7 +50,7 @@ namespace { DistributedInputTestClient::DistributedInputTestClient(std::string ip) { - DHLOGI("Ctor DistributedInputTestClient ip: %s", ip.c_str()); + DHLOGI("Ctor DistributedInputTestClient ip: %s", GetAnonyString(ip).c_str()); socketCli.Init(ip); socketCli.RegisterResCallback(std::bind(&DistributedInputTestClient::ProcRes, this, std::placeholders::_1)); } @@ -103,14 +105,14 @@ bool DistributedInputTestClient::CreateKey() bool DistributedInputTestClient::CreateNode() { - fd_ = open("/dev/uinput", O_WRONLY | O_NONBLOCK); + fd_ = open(INPUT_PATH.c_str(), O_WRONLY | O_NONBLOCK); if (fd_ < 0) { DHLOGE("Failed to open uinput"); return false; } std::string deviceName_ = "virtual mouse"; - if (strncpy_s(dev_.name, sizeof(dev_.name), deviceName_.c_str(), deviceName_.size()) != 0) { + if (strncpy_s(dev_.name, sizeof(dev_.name), deviceName_.c_str(), deviceName_.size()) != EOK) { DHLOGE("Failed to set device name"); return false; } diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index 900cb0c..7ff49f1 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -30,6 +30,7 @@ #include "dinput_errcode.h" #include "dinput_log.h" +#include "dinput_utils_tool.h" #include "nlohmann/json.hpp" namespace OHOS { @@ -42,6 +43,7 @@ namespace { constexpr int32_t BUFF_MAX_LEN = 32 * 1024; constexpr int32_t BLOCK_TIME = 10; constexpr int32_t SOCKET_ERR = -1; + constexpr int32_t SOCKET_SUCCESS = 0; } int32_t SocketClient::Init(std::string ip) @@ -67,7 +69,7 @@ int32_t SocketClient::Init(std::string ip) std::thread(&SocketClient::DoRecvSvrRsp, this).detach(); std::thread(&SocketClient::SendRequest, this).detach(); - return 0; + return SOCKET_SUCCESS; } int32_t SocketClient::Release() @@ -77,7 +79,7 @@ int32_t SocketClient::Release() close(sock_cli); sock_cli = SOCKET_ERR; } - return 0; + return SOCKET_SUCCESS; } void SocketClient::SendRequest() @@ -101,7 +103,7 @@ void SocketClient::DoSendRequest(std::string msg) { char buf[msg.length() + 1]; int32_t ret = strcpy_s(buf, msg.length() + 1, msg.c_str()); - if (ret != DH_SUCCESS) { + if (ret != EOK) { DHLOGE("DoSendRequest strcpy_s failed"); return; } @@ -111,7 +113,7 @@ void SocketClient::DoSendRequest(std::string msg) void SocketClient::ParseRecvSvrMsg(std::string msg) { - DHLOGI("Client Recv Message: %s", msg.c_str()); + DHLOGI("Client Recv Message: %s", GetAnonyString(msg).c_str()); nlohmann::json jsonObj = nlohmann::json::parse(msg, nullptr, false); if (jsonObj.find("injectResult") == jsonObj.end()) { DHLOGE("server msg is not rsp msg"); @@ -151,9 +153,9 @@ void SocketClient::DoRecvSvrRsp() DHLOGI("empty msg, drop it"); continue; } - DHLOGI("Receive Server Rsp: %s", recvbuf); std::string svrRsp(recvbuf, len); - std::cout << svrRsp << std::endl; + DHLOGI("Receive Server Rsp: %s", GetAnonyString(svrRsp).c_str()); + std::cout << GetAnonyString(svrRsp) << std::endl; ParseRecvSvrMsg(svrRsp); (void)memset_s(recvbuf, sizeof(recvbuf), 0, sizeof(recvbuf)); } @@ -162,7 +164,7 @@ void SocketClient::DoRecvSvrRsp() int32_t SocketClient::PushRequest(std::string msg) { - DHLOGI("Client PushRequest: %s", msg.c_str()); + DHLOGI("Client PushRequest: %s", GetAnonyString(msg).c_str()); int32_t msgLen = msg.length(); if (msgLen > MAX_MSG_LENGTH) { DHLOGE("Msg too long, length: %d", msgLen); diff --git a/test/injecttest/server/include/distributed_input_test_server.h b/test/injecttest/server/include/distributed_input_test_server.h index e3e189a..f2ce93a 100644 --- a/test/injecttest/server/include/distributed_input_test_server.h +++ b/test/injecttest/server/include/distributed_input_test_server.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -93,6 +94,7 @@ private: std::shared_ptr socketServerPtr_ = nullptr; std::string remoteDevId_ = ""; static std::vector dhIds_; + static std::mutex dhIdsMutex_; std::atomic testResult_ = false; }; } diff --git a/test/injecttest/server/src/distributed_input_test_server.cpp b/test/injecttest/server/src/distributed_input_test_server.cpp index b72e368..401526a 100644 --- a/test/injecttest/server/src/distributed_input_test_server.cpp +++ b/test/injecttest/server/src/distributed_input_test_server.cpp @@ -32,6 +32,7 @@ #include "dinput_errcode.h" #include "dinput_log.h" +#include "dinput_utils_tool.h" #include "softbus_bus_center.h" namespace OHOS { @@ -40,29 +41,36 @@ namespace DistributedInput { static std::shared_ptr socketServerStaticPtr = nullptr; namespace { const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; + const std::string DINPUT_DEVICE_PATH = "/dev/input/event16"; constexpr int32_t WAIT_DH_SYNC_TIME = 2; constexpr int32_t KEY_VALUE_DOWN = 1; + constexpr int32_t DINPUT_OK = 0; + constexpr int32_t DINPUT_ERR = -1; + constexpr int32_t POLL_NFD = 1; } std::vector DistributedInputTestServer::dhIds_ = {}; +std::mutex DistributedInputTestServer::dhIdsMutex_; void DistributedInputTestServer::TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) { - DHLOGI("TestPrepareDInputCallback::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); + DHLOGI("TestPrepareDInputCallback::OnResult: deviceId = %s, status = %d", + GetAnonyString(deviceId).c_str(), status); } void DistributedInputTestServer::TestUnprepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) { - DHLOGI("TestUnprepareDInputCallback::OnResult: deviceId = %s, status = %d", deviceId.c_str(), status); + DHLOGI("TestUnprepareDInputCallback::OnResult: deviceId = %s, status = %d", + GetAnonyString(deviceId).c_str(), status); } void DistributedInputTestServer::TestStartStopDInputCallback::OnResultDhids(const std::string &devId, const int32_t &status) { DHLOGI("DistributedInputTestServer::TestStartStopDInputCallback::OnResult: devId = %s, status = %d", - devId.c_str(), status); + GetAnonyString(devId).c_str(), status); } void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, @@ -74,13 +82,14 @@ void DistributedInputTestServer::TestInputNodeListener::OnNodeOnLine(const std:: jsonStr["sinkNodeId"] = sinkNodeId; jsonStr["nodeDesc"] = nodeDesc; std::string msg = jsonStr.dump(); - DHLOGI("TestInputNodeListener::OnNodeOnline: %s", msg.c_str()); - DistributedInputTestServer::dhIds_.emplace_back(sinkNodeId); + DHLOGI("TestInputNodeListener::OnNodeOnline: %s", GetAnonyString(msg).c_str()); if (socketServerStaticPtr == nullptr) { DHLOGE("socketServerStaticPtr is null"); return; } socketServerStaticPtr->PushResponse(msg); + std::lock_guard lock(DistributedInputTestServer::dhIdsMutex_); + DistributedInputTestServer::dhIds_.emplace_back(sinkNodeId); } void DistributedInputTestServer::TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, @@ -106,10 +115,10 @@ int32_t DistributedInputTestServer::TestSimulationEventListener::OnSimulationEve DHLOGI("TestSimulationEventListener::OnSimulationEvent: %s", msg.c_str()); if (socketServerStaticPtr == nullptr) { DHLOGE("socketServerStaticPtr is null"); - return -1; + return DINPUT_ERR; } socketServerStaticPtr->PushResponse(msg); - return 0; + return DINPUT_OK; } void DistributedInputTestServer::GetRemoteDeviceId() @@ -117,7 +126,7 @@ void DistributedInputTestServer::GetRemoteDeviceId() NodeBasicInfo *info = nullptr; int32_t infoNum = 0; auto ret = GetAllNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), &info, &infoNum); - if (ret != 0) { + if (ret != DINPUT_OK) { DHLOGE("get remote device id fail."); return; } @@ -145,20 +154,17 @@ DistributedInputTestServer::DistributedInputTestServer():inputNodeListener_(new void DistributedInputTestServer::ListenerEvent() { DHLOGE("ListenerEvent start"); - std::string device = "/dev/input/event16"; struct input_event event; struct pollfd pfd[1]; - int32_t nfd = 1; - int32_t len; - int32_t fd = open(device.c_str(), O_RDONLY); + int32_t fd = open(DINPUT_DEVICE_PATH.c_str(), O_RDONLY); if (fd < 0) { - DHLOGE("failed to open device: %s", device.c_str()); + DHLOGE("failed to open device: %s", DINPUT_DEVICE_PATH.c_str()); } pfd[0].fd = fd; pfd[0].events = POLLIN; while (true) { - if (poll(pfd, nfd, -1) > 0) { - len = read(fd, &event, sizeof(event)); + if (poll(pfd, POLL_NFD, -1) > 0) { + int32_t len = read(fd, &event, sizeof(event)); if (len < 0) { continue; } diff --git a/test/injecttest/server/src/socket_server.cpp b/test/injecttest/server/src/socket_server.cpp index 9d667eb..204811a 100644 --- a/test/injecttest/server/src/socket_server.cpp +++ b/test/injecttest/server/src/socket_server.cpp @@ -25,6 +25,7 @@ #include "dinput_errcode.h" #include "dinput_log.h" +#include "dinput_utils_tool.h" namespace OHOS { namespace DistributedHardware { @@ -36,7 +37,7 @@ namespace { constexpr int32_t BUFF_MAX_LEN = 32 * 1024; constexpr int32_t SEND_MSG_SPACE = 100; constexpr int32_t BLOCK_TIME = 10; - constexpt int32_t SOCKET_ERR = -1; + constexpr int32_t SOCKET_ERR = -1; } void SocketServer::Init() @@ -168,13 +169,13 @@ void SocketServer::ProcRequest() void SocketServer::DoSendMessage(std::string msg) { + DHLOGI("Server Send Message: %s", GetAnonyString(msg).c_str()); char buf[msg.length() + 1]; int32_t ret = strcpy_s(buf, msg.length() + 1, msg.c_str()); - if (ret != DH_SUCCESS) { + if (ret != EOK) { DHLOGE("DoSendMessage strcpy_s failed"); return; } - DHLOGI("Server Send Message: %s", buf); if (li.size() == 0) { DHLOGI("No client, save it"); @@ -190,21 +191,20 @@ void SocketServer::DoSendMessage(std::string msg) void SocketServer::DoSendMessagePure(std::string msg, int32_t conn) { + DHLOGI("Server Send Message Pure: %s", GetAnonyString(msg).c_str()); char buf[msg.length() + 1]; int32_t ret = strcpy_s(buf, msg.length() + 1, msg.c_str()); - if (ret != DH_SUCCESS) { + if (ret != EOK) { DHLOGE("DoSendMessagePure strcpy_s failed"); return; } - DHLOGI("Server Send Message Pure: %s", buf); - send(conn, buf, sizeof(buf), 0); } void SocketServer::PushResponse(std::string msg) { - DHLOGI("PushRespones: %s", msg.c_str()); + DHLOGI("PushRespones: %s", GetAnonyString(msg).c_str()); int32_t msgLen = msg.length(); if (msgLen > MAX_MSG_LENGTH) { DHLOGE("Msg too long, length: %d", msgLen); -- Gitee From 6a2759fd7e6f225e77554e7fed08504ae038a7cf Mon Sep 17 00:00:00 2001 From: wangxuanxuan Date: Tue, 1 Aug 2023 20:49:09 +0800 Subject: [PATCH 10/10] add inject event test Signed-off-by: wangxuanxuan --- test/injecttest/client/src/socket_client.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/injecttest/client/src/socket_client.cpp b/test/injecttest/client/src/socket_client.cpp index 7ff49f1..58e6e69 100644 --- a/test/injecttest/client/src/socket_client.cpp +++ b/test/injecttest/client/src/socket_client.cpp @@ -174,7 +174,7 @@ int32_t SocketClient::PushRequest(std::string msg) std::unique_lock lock(reqMtx_); requestQueue_.push(msg); reqQueueConVar_.notify_all(); - return 0; + return SOCKET_SUCCESS; } void SocketClient::RegisterResCallback(ResCallback cb) -- Gitee