diff --git a/test/BUILD.gn b/test/BUILD.gn index d43e3493366a5667c7ada6064a6f257bcfa1d3bf..00e85cbcd071aad9e950a0ba4f4d23b0f58e05ac 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,16 @@ # 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 += [ "functiontest:dinputfunctiontest" ] + } } diff --git a/test/functiontest/BUILD.gn b/test/functiontest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..247b2ee839b252196c963f549f9970e7ff488cd6 --- /dev/null +++ b/test/functiontest/BUILD.gn @@ -0,0 +1,58 @@ +# Copyright (c) 2023 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/features.gni") +import("//build/test.gni") +import("//test/xts/tools/build/suite.gni") +import("../../distributedinput.gni") + +ohos_moduletest_suite("dinputfunctiontest") { + module_out_path = "distributed_input/functiontest/" + + include_dirs = [ + "${distributedinput_path}/interfaces/ipc/include", + "${utils_path}/include", + "../functiontest", + ] + + sources = [ + "dinput_automat_test.cpp", + "dinput_demo.cpp", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"DinputFunctionTest\"", + "LOG_DOMAIN=0xD004100", + ] + + deps = [ + "${innerkits_path}:libdinput_sdk", + "${utils_path}:libdinput_utils", + "//third_party/jsoncpp:jsoncpp", + "//third_party/libevdev:libevdev", + "//third_party/openssl:libcrypto_static", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "dsoftbus:softbus_client", + "ipc:ipc_core", + ] + + subsystem_name = "distributedhardware" + part_name = "distributed_input" +} diff --git a/test/functiontest/Test.json b/test/functiontest/Test.json new file mode 100644 index 0000000000000000000000000000000000000000..679de3082b65618312f5d76755574571c14e37d6 --- /dev/null +++ b/test/functiontest/Test.json @@ -0,0 +1,21 @@ +{ + "description": "Config for disInput test cases", + "driver": { + "module-name": "DctsdisInputTest", + "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": [ + "DctsdisInputTest->/data/local/tmp/DctsdisInputTest" + ], + "type": "PushKit" + } + ] +} \ No newline at end of file diff --git a/test/functiontest/dinput_automat_test.cpp b/test/functiontest/dinput_automat_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1ce28abcce18f0a3e23abf7535da61eccfe04474 --- /dev/null +++ b/test/functiontest/dinput_automat_test.cpp @@ -0,0 +1,135 @@ +/* + * 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_demo.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; + +namespace { + constexpr int32_t RUN_TIMES = 50; + constexpr int32_t PERMISSION_NUM = 2; + constexpr int32_t SLEEP_OK = 1; + constexpr int32_t DINPUT_OK = 0; +} + +class DInputAutomatTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + DInputAutomatTest(); +}; +void DInputAutomatTest::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(SLEEP_OK); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +void DInputAutomatTest::TearDownTestCase(void) {} +void DInputAutomatTest::SetUp(void) {} +void DInputAutomatTest::TearDown(void) {} + +DInputAutomatTest::DInputAutomatTest(void) {} + +HWTEST_F(DInputAutomatTest, DInputTest_0001, TestSize.Level1) +{ + int32_t ret = PrepareRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0002, TestSize.Level1) +{ + int32_t ret = StartRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0003, TestSize.Level1) +{ + int32_t ret = StopRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0004, TestSize.Level1) +{ + for (int32_t i = 0; i < RUN_TIMES; i++) { + int32_t ret = StartRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + ret = StopRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + } +} + +HWTEST_F(DInputAutomatTest, DInputTest_0005, TestSize.Level1) +{ + int32_t ret = StartRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0006, TestSize.Level1) +{ + int32_t ret = StopRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} + +HWTEST_F(DInputAutomatTest, DInputTest_0007, TestSize.Level1) +{ + for (int32_t i = 0; i < RUN_TIMES; i++) { + int32_t ret = StartRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + ret = StopRemoteByDhIds(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); + } +} + +HWTEST_F(DInputAutomatTest, DInputTest_0008, TestSize.Level1) +{ + int32_t ret = UnprepareRemote(); + EXPECT_EQ(DINPUT_OK, ret); + sleep(SLEEP_OK); +} \ No newline at end of file diff --git a/test/functiontest/dinput_demo.cpp b/test/functiontest/dinput_demo.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2d0fdeee68ff0173aaa91e27cc4ff10ca194b3ac --- /dev/null +++ b/test/functiontest/dinput_demo.cpp @@ -0,0 +1,241 @@ +/* + * 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_demo.h" + +#include +#include +#include + +#include "dinput_log.h" +#include "dinput_utils_tool.h" +#include "distributed_input_client.h" +#include "i_prepare_d_input_call_back.h" + +#include "softbus_bus_center.h" +#include "softbus_common.h" + +using namespace OHOS::DistributedHardware; + +namespace { + const std::string DINPUT_PKG_NAME = "ohos.dhardware.dinput"; + constexpr int32_t DINPUT_OK = 0; + constexpr int32_t DINPUT_FAIL = -1; + constexpr int32_t SLEEP_OK = 1; + constexpr uint32_t INPUT_TYPE_ALL = 0x0007; + std::string g_srcId; + std::string g_sinkId; + std::vector g_dhIds; + std::mutex g_dhIdsMutex; +} + +void TestPrepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) +{ + (void)deviceId; + (void)status; +} + +void TestUnprepareDInputCallback::OnResult(const std::string& deviceId, const int32_t& status) +{ + (void)deviceId; + (void)status; +} + +void TestStartDInputCallback::OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status) +{ + (void)deviceId; + (void)inputTypes; + (void)status; +} + +void TestStopDInputCallback::OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status) +{ + (void)deviceId; + (void)inputTypes; + (void)status; +} + +void TestInputNodeListener::OnNodeOnLine(const std::string srcDevId, const std::string sinkDevId, + const std::string sinkNodeId, const std::string nodeDesc) +{ + (void)srcDevId; + (void)sinkDevId; + (void)nodeDesc; + std::lock_guard lock(g_dhIdsMutex); + g_dhIds.emplace_back(sinkNodeId); +} + +void TestInputNodeListener::OnNodeOffLine(const std::string srcDevId, const std::string sinkDevId, + const std::string sinkNodeId) +{ + (void)srcDevId; + (void)sinkDevId; + std::lock_guard lock(g_dhIdsMutex); + for (auto it = g_dhIds.begin(); it != g_dhIds.end();) { + if (*it == sinkNodeId) { + it = g_dhIds.erase(it); + continue; + } + ++it; + } +} + +void TestStartStopDInputCallback::OnResultDhids(const std::string &devId, const int32_t &status) +{ + (void)devId; + (void)status; +} + +int32_t GetSrcDevId() +{ + std::cout << "Start get local device id." << std::endl; + auto info = std::make_unique(); + auto ret = GetLocalNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), info.get()); + if (ret != DINPUT_OK) { + std::cout << "GetLocalNodeDeviceInfo is fail." << "errCode = " << ret << std::endl; + return DINPUT_FAIL; + } + g_srcId = info->networkId; + if (g_srcId.empty()) { + std::cout << "g_srcId is empty." << std::endl; + return DINPUT_FAIL; + } + std::cout << "g_srcId = " << DistributedInput::GetAnonyString(g_srcId) << std::endl; + return DINPUT_OK; +} + +int32_t GetSinkDevId() +{ + sleep(SLEEP_OK); + std::cout << "Start get remote device id." << std::endl; + NodeBasicInfo *info = NULL; + int32_t infoNum = 0; + auto ret = GetAllNodeDeviceInfo(DINPUT_PKG_NAME.c_str(), &info, &infoNum); + if (ret != DINPUT_OK) { + std::cout << "get remote device id fail." << std::endl; + return DINPUT_FAIL; + } + g_sinkId = info->networkId; + if (g_sinkId.empty()) { + std::cout << "g_sinkId is empty." << std::endl; + return DINPUT_FAIL; + } + std::cout << "g_sinkId = " << DistributedInput::GetAnonyString(g_sinkId) << std::endl; + return DINPUT_OK; +} + +int32_t PrepareRemote() +{ + if ((GetSrcDevId() != DINPUT_OK) || (GetSinkDevId() != DINPUT_OK)) { + std::cout << "get sink or src id failed." << std::endl; + return DINPUT_FAIL; + } + auto prepareCb = new(std::nothrow) TestPrepareDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::PrepareRemoteInput(g_srcId, g_sinkId, prepareCb); + if (ret != DINPUT_OK) { + std::cout << "PrepareRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "PrepareRemoteInput is success." << std::endl; + + OHOS::sptr inputNodeListener(new TestInputNodeListener()); + ret = DistributedInput::DistributedInputKit::RegisterInputNodeListener(inputNodeListener); + if (ret != DINPUT_OK) { + std::cout << "Register Input Node Listener is failed." << std::endl; + return ret; + } + std::cout << "Register Input Node Listener success." << std::endl; + return ret; +} + +int32_t UnprepareRemote() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto unprepareCb = new(std::nothrow) TestUnprepareDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::UnprepareRemoteInput(g_srcId, g_sinkId, unprepareCb); + if (ret != DINPUT_OK) { + std::cout << "UnprepareRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "UnprepareRemoteInput is success." << std::endl; + return ret; +} + +int32_t StartRemote() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto startCb = new(std::nothrow) TestStartDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, INPUT_TYPE_ALL, startCb); + if (ret != DINPUT_OK) { + std::cout << "StartRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "StartRemoteInput is success." << std::endl; + return ret; +} + +int32_t StopRemote() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto stopCb = new(std::nothrow) TestStopDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, INPUT_TYPE_ALL, stopCb); + if (ret != DINPUT_OK) { + std::cout << "StopRemoteInput is failed." << std::endl; + return ret; + } + std::cout << "StopRemoteInput is success." << std::endl; + return ret; +} + +int32_t StartRemoteByDhIds() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto startCb = new(std::nothrow) TestStartStopDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::StartRemoteInput(g_sinkId, g_dhIds, startCb); + if (ret != DINPUT_OK) { + std::cout << "StartRemoteByDhIds is failed." << std::endl; + return ret; + } + std::cout << "StartRemoteByDhIds is success." << std::endl; + return ret; +} + +int32_t StopRemoteByDhIds() +{ + if (g_srcId.empty() || g_sinkId.empty()) { + std::cout << "sink or src id is empty." << std::endl; + return DINPUT_FAIL; + } + auto stopCb = new(std::nothrow) TestStartStopDInputCallback(); + int32_t ret = DistributedInput::DistributedInputKit::StopRemoteInput(g_sinkId, g_dhIds, stopCb); + if (ret != DINPUT_OK) { + std::cout << "StopRemoteByDhIds is failed." << std::endl; + return ret; + } + std::cout << "StopRemoteByDhIds is success." << std::endl; + return ret; +} \ No newline at end of file diff --git a/test/functiontest/dinput_demo.h b/test/functiontest/dinput_demo.h new file mode 100644 index 0000000000000000000000000000000000000000..11d86d062adb3caab74900e3636551b4ceae87b0 --- /dev/null +++ b/test/functiontest/dinput_demo.h @@ -0,0 +1,109 @@ +/* + * 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 DINPUT_DEMO_H +#define DINPUT_DEMO_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "distributed_input_kit.h" +#include "input_node_listener_stub.h" +#include "prepare_d_input_call_back_stub.h" +#include "start_d_input_call_back_stub.h" +#include "stop_d_input_call_back_stub.h" +#include "unprepare_d_input_call_back_stub.h" + +int32_t GetSrcDevId(void); +int32_t GetSinkDevId(void); +int32_t PrepareRemote(void); +int32_t UnprepareRemote(void); +int32_t StartRemote(void); +int32_t StopRemote(void); +int32_t StartRemoteByDhIds(void); +int32_t StopRemoteByDhIds(void); + +class TestPrepareDInputCallback : + public OHOS::DistributedHardware::DistributedInput::PrepareDInputCallbackStub { +public: + TestPrepareDInputCallback() = default; + virtual ~TestPrepareDInputCallback() = default; + void OnResult(const std::string& deviceId, const int32_t& status); +}; + +class TestUnprepareDInputCallback : + public OHOS::DistributedHardware::DistributedInput::UnprepareDInputCallbackStub { +public: + TestUnprepareDInputCallback() = default; + virtual ~TestUnprepareDInputCallback() = default; + void OnResult(const std::string& deviceId, const int32_t& status); +}; + +class TestStartDInputCallback : + public OHOS::DistributedHardware::DistributedInput::StartDInputCallbackStub { +public: + TestStartDInputCallback() = default; + virtual ~TestStartDInputCallback() = default; + void OnResult(const std::string& deviceId, const uint32_t& inputTypes, const int32_t& status); +}; + +class TestStopDInputCallback : + public OHOS::DistributedHardware::DistributedInput::StopDInputCallbackStub { +public: + TestStopDInputCallback() = default; + virtual ~TestStopDInputCallback() = default; + void OnResult(const std::string& deviceId, const uint32_t& inputTypes, 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 TestStartStopDInputCallback : + public OHOS::DistributedHardware::DistributedInput::StartStopDInputsCallbackStub { +public: + TestStartStopDInputCallback() = default; + virtual ~TestStartStopDInputCallback() = default; + void OnResultDhids(const std::string &devId, const int32_t &status); +}; +#define CooDevMgr ::OHOS::DelayedSingleton::GetInstance() +#endif // DINPUT_DEMO_H \ No newline at end of file