From 99a90e19e10c00af783a7d54a2072a7bcaad1e9a Mon Sep 17 00:00:00 2001 From: renguang1116 Date: Wed, 12 Jan 2022 21:57:49 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4DM=E6=A8=A1=E5=9D=97TDD?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20Signed-off-by:=20renguang1116=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/BUILD.gn | 18 + test/unittest/BUILD.gn | 327 +++++++++ test/unittest/UTTest_auth_request_state.cpp | 665 ++++++++++++++++++ test/unittest/UTTest_auth_request_state.h | 39 + test/unittest/UTTest_auth_response_state.cpp | 480 +++++++++++++ test/unittest/UTTest_auth_response_state.h | 36 + .../UTTest_device_manager_service.cpp | 346 +++++++++ test/unittest/UTTest_device_manager_service.h | 47 ++ ...UTTest_device_manager_service_listener.cpp | 112 +++ .../UTTest_device_manager_service_listener.h | 44 ++ test/unittest/UTTest_device_message.cpp | 514 ++++++++++++++ test/unittest/UTTest_device_message.h | 40 ++ test/unittest/UTTest_dm_auth_manager.cpp | 569 +++++++++++++++ test/unittest/UTTest_dm_auth_manager.h | 47 ++ .../UTTest_dm_device_info_manager.cpp | 62 ++ test/unittest/UTTest_dm_device_info_manager.h | 45 ++ .../UTTest_dm_device_state_manager.cpp | 200 ++++++ .../unittest/UTTest_dm_device_state_manager.h | 44 ++ test/unittest/UTTest_dm_discovery_manager.cpp | 252 +++++++ test/unittest/UTTest_dm_discovery_manager.h | 43 ++ test/unittest/UTTest_hichain_connector.cpp | 490 +++++++++++++ test/unittest/UTTest_hichain_connector.h | 51 ++ test/unittest/UTTest_softbus_connector.cpp | 447 ++++++++++++ test/unittest/UTTest_softbus_connector.h | 55 ++ test/unittest/UTTest_softbus_session.cpp | 209 ++++++ test/unittest/UTTest_softbus_session.h | 49 ++ test/unittest/auth_request_state.cpp | 192 +++++ test/unittest/device_manager_impl_test.cpp | 192 +++++ test/unittest/device_manager_impl_test.h | 55 ++ test/unittest/mock/ipc_server_listener.cpp | 31 + test/unittest/mock/ipc_server_listener.h | 39 + test/unittest/mock/mock_ipc_client_proxy.h | 33 + 32 files changed, 5773 insertions(+) create mode 100644 test/BUILD.gn create mode 100644 test/unittest/BUILD.gn create mode 100644 test/unittest/UTTest_auth_request_state.cpp create mode 100644 test/unittest/UTTest_auth_request_state.h create mode 100644 test/unittest/UTTest_auth_response_state.cpp create mode 100644 test/unittest/UTTest_auth_response_state.h create mode 100644 test/unittest/UTTest_device_manager_service.cpp create mode 100644 test/unittest/UTTest_device_manager_service.h create mode 100644 test/unittest/UTTest_device_manager_service_listener.cpp create mode 100644 test/unittest/UTTest_device_manager_service_listener.h create mode 100644 test/unittest/UTTest_device_message.cpp create mode 100644 test/unittest/UTTest_device_message.h create mode 100644 test/unittest/UTTest_dm_auth_manager.cpp create mode 100644 test/unittest/UTTest_dm_auth_manager.h create mode 100644 test/unittest/UTTest_dm_device_info_manager.cpp create mode 100644 test/unittest/UTTest_dm_device_info_manager.h create mode 100644 test/unittest/UTTest_dm_device_state_manager.cpp create mode 100644 test/unittest/UTTest_dm_device_state_manager.h create mode 100644 test/unittest/UTTest_dm_discovery_manager.cpp create mode 100644 test/unittest/UTTest_dm_discovery_manager.h create mode 100644 test/unittest/UTTest_hichain_connector.cpp create mode 100644 test/unittest/UTTest_hichain_connector.h create mode 100644 test/unittest/UTTest_softbus_connector.cpp create mode 100644 test/unittest/UTTest_softbus_connector.h create mode 100644 test/unittest/UTTest_softbus_session.cpp create mode 100644 test/unittest/UTTest_softbus_session.h create mode 100644 test/unittest/auth_request_state.cpp create mode 100644 test/unittest/device_manager_impl_test.cpp create mode 100644 test/unittest/device_manager_impl_test.h create mode 100644 test/unittest/mock/ipc_server_listener.cpp create mode 100644 test/unittest/mock/ipc_server_listener.h create mode 100644 test/unittest/mock/mock_ipc_client_proxy.h diff --git a/test/BUILD.gn b/test/BUILD.gn new file mode 100644 index 00000000..ece19432 --- /dev/null +++ b/test/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2021 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. + +group("test") { + testonly = true + + deps = [ "unittest:unittest" ] +} \ No newline at end of file diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn new file mode 100644 index 00000000..d1f6bda8 --- /dev/null +++ b/test/unittest/BUILD.gn @@ -0,0 +1,327 @@ +# Copyright (c) 2021 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/devicemanager/devicemanager.gni") +module_out_path = "deviceManager_stander/inner_kits" + +group("unittest") { + testonly = true + + deps = [ + ":UTTest_device_manager_service", + ":UTTest_device_manager_service_listener", + ":UTTest_dm_device_info_manager", + ":UTTest_dm_device_state_manager", + ":UTTest_dm_discovery_manager", + ":UTTest_hichain_connector", + ":UTTest_softbus_connector", + ":UTTest_softbus_session", + ":device_manager_impl_test", + ] +} + +## UnitTest device_manager_impl_test {{{ +ohos_unittest("device_manager_impl_test") { + module_out_path = module_out_path + + sources = [ "device_manager_impl_test.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest device_manager_impl_test }}} + +## UnitTest UTTest_device_manager_service {{{ +ohos_unittest("UTTest_device_manager_service") { + module_out_path = module_out_path + + sources = [ "UTTest_device_manager_service.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest UTTest_device_manager_service }}} + +## UnitTest UTTest_hichain_connector {{{ +ohos_unittest("UTTest_hichain_connector") { + module_out_path = module_out_path + + sources = [ "UTTest_hichain_connector.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest UTTest_hichain_connector }}} + +## UnitTest UTTest_dm_device_info_manager {{{ +ohos_unittest("UTTest_dm_device_info_manager") { + module_out_path = module_out_path + + sources = [ "UTTest_dm_device_info_manager.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest UTTest_dm_device_info_manager }}} + +## UnitTest UTTest_softbus_connector {{{ +ohos_unittest("UTTest_softbus_connector") { + module_out_path = module_out_path + + sources = [ "UTTest_softbus_connector.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest UTTest_softbus_connector }}} + +## UnitTest UTTest_softbus_session {{{ +ohos_unittest("UTTest_softbus_session") { + module_out_path = module_out_path + + sources = [ "UTTest_softbus_session.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest UTTest_softbus_session }}} + +## UnitTest UTTest_dm_device_state_manager {{{ +ohos_unittest("UTTest_dm_device_state_manager") { + module_out_path = module_out_path + + sources = [ + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/adapter/standard/dm_adapter_manager.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/config/config_manager.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/dependency/softbus/softbus_session.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/device_manager_service_listener.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/devicestate/dm_device_state_manager.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/ipc_server_listener.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/parameter.cpp", + "UTTest_dm_device_state_manager.cpp", + ] + + deps = [ ":device_manager_test" ] +} + +## UnitTest UTTest_dm_device_state_manager }}} + +## UnitTest UTTest_device_manager_service_listener {{{ +ohos_unittest("UTTest_device_manager_service_listener") { + module_out_path = module_out_path + + sources = [ + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/device_manager_service_listener.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/ipc_server_listener.cpp", + "UTTest_device_manager_service_listener.cpp", + ] + + deps = [ ":device_manager_test" ] +} + +## UnitTest UTTest_device_manager_service_listener }}} + +## UnitTest UTTest_dm_discovery_manager {{{ +ohos_unittest("UTTest_dm_discovery_manager") { + module_out_path = module_out_path + + sources = [ + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/dependency/softbus/softbus_connector.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/dependency/softbus/softbus_session.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/dependency/timer/dm_timer.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/device_manager_service_listener.cpp", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/src/discovery/dm_discovery_manager.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/ipc_server_listener.cpp", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/parameter.cpp", + "UTTest_dm_discovery_manager.cpp", + ] + + deps = [ ":device_manager_test" ] +} + +## UnitTest UTTest_dm_discovery_manager }}} +## Build device_manager_test_common.a {{{ +config("device_manager_test_common_public_config") { + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/ipc/standard", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/ipc", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include", + "${services_path}/include/dependency/timer", + "${services_path}/include/discovery", + "${services_path}/include/dependency/softbus", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/dependency/softbus", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/softbus_lite/interfaces/kits/transport", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/authentication", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/adapter", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/ipc/standard", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/discovery", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/dependency/hichain", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/deviceinfo/", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/devicestate", + "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//base/security/deviceauth/interfaces/innerkits", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/ability", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/config", + ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + "-Dprotected=public", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"device_manager_UTtest\"", + "LOG_DOMAIN=0xD004190", + ] +} + +ohos_static_library("device_manager_test_common") { + testonly = true + + visibility = [ ":*" ] + + public_configs = [ ":device_manager_test_common_public_config" ] + + public_deps = [ + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice:devicemanagerservice", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + "//utils/native/base:utils", + ] +} + +## Build device_manager_test_common.a }}} + +## Build device_manager_test_common.a {{{ +config("device_manager_test_common_public") { + include_dirs = [ + "//utils/native/base/include", + "//utils/system/safwk/native/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/ipc", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/notify", + "//third_party/json/include", + "${common_path}/include", + "${common_path}/include/ipc", + "${common_path}/include/ipc/model", + "${utils_path}/include", + "${utils_path}/include/ipc/standard", + "${services_path}/include", + "${services_path}/include/dependency/timer", + "${services_path}/include/discovery", + "${services_path}/include/dependency/softbus", + "//foundation/communication/dsoftbus/interfaces/kits/bus_center", + "//foundation/communication/ipc_lite/interfaces/kits", + "//foundation/communication/dsoftbus/interfaces/kits/common", + "//foundation/communication/dsoftbus/interfaces/kits/discovery", + "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/dependency/softbus", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/authentication", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/adapter", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/discovery", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/dependency/hichain", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/deviceinfo/", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/devicestate", + "//foundation/distributedhardware/devicemanager/test/unittest/mock/", + "//base/security/deviceauth/interfaces/innerkits", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/ability", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/config", + ] + + cflags = [ + "-Wall", + "-Werror", + "-g3", + "-Dprivate=public", + "-Dprotected=public", + ] + + defines = [ + "HI_LOG_ENABLE", + "DH_LOG_TAG=\"device_manager_UTtest\"", + "LOG_DOMAIN=0xD004190", + ] +} + +ohos_static_library("device_manager_test") { + testonly = true + + visibility = [ ":*" ] + + public_configs = [ ":device_manager_test_common_public" ] + + public_deps = [ + "${utils_path}:devicemanagerutils", + "//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp:devicemanagersdk", + "//foundation/distributedhardware/devicemanager/interfaces/kits/js:devicemanager", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", + "//third_party/googletest:gmock", + "//third_party/googletest:gtest", + "//utils/native/base:utils", + ] + + deps = [ + "${innerkits_path}/native_cpp:devicemanagersdk", + "${utils_path}:devicemanagerutils", + "//base/security/deviceauth/services:deviceauth_sdk", + "//foundation/aafwk/standard/interfaces/innerkits/ability_manager:ability_manager", + "//foundation/aafwk/standard/interfaces/innerkits/want:want", + "//foundation/aafwk/standard/services/abilitymgr:abilityms", + ] + + external_deps = [ + "appexecfwk_standard:appexecfwk_base", + "appexecfwk_standard:appexecfwk_core", + "appexecfwk_standard:libeventhandler", + "ces_standard:cesfwk_core", + "ces_standard:cesfwk_innerkits", + "dsoftbus_standard:softbus_client", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "safwk:system_ability_fwk", + "samgr_standard:samgr_proxy", + ] +} +## Build device_manager_test.a }}} diff --git a/test/unittest/UTTest_auth_request_state.cpp b/test/unittest/UTTest_auth_request_state.cpp new file mode 100644 index 00000000..eded6b94 --- /dev/null +++ b/test/unittest/UTTest_auth_request_state.cpp @@ -0,0 +1,665 @@ +/* + * Copyright (c) 2021 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 "UTTest_auth_request_state.h" + +#include "dm_auth_manager.h" +#include "dm_constants.h" + +namespace OHOS { +namespace DistributedHardware { +void AuthRequestStateTest::SetUp() +{ +} +void AuthRequestStateTest::TearDown() +{ +} +void AuthRequestStateTest::SetUpTestCase() +{ +} +void AuthRequestStateTest::TearDownTestCase() +{ +} + +namespace { + +std::string AUTHENTICATE_TIMEOUT_TASK = "authenticateTimeoutTask"; +std::string NEGOTIATE_TIMEOUT_TASK = "negotiateTimeoutTask"; +std::string CONFIRM_TIMEOUT_TASK = "confirmTimeoutTask"; +std::string INPUT_TIMEOUT_TASK = "inputTimeoutTask"; +std::string ADD_TIMEOUT_TASK = "addTimeoutTask"; + +/** + * @tc.name: AuthRequestInitState::SetAuthManager_001 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestInitState::SetAuthManager with authManager != null + * 3 check ret is authResponseState->authManager_.use_count() + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, SetAuthManager_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + authRequestState->SetAuthManager(authManager); + int32_t ret = authRequestState->authManager_.use_count(); + authRequestState->authManager_.reset(); + ASSERT_EQ(ret, 1); +} + +/** + * @tc.name: AuthRequestInitState::SetAuthManager_002 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestInitState::SetAuthManager with authManager = null + * 3 check ret is authResponseState->authManager_.use_count() + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, SetAuthManager_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->authManager_.use_count(); + authRequestState->authManager_.reset(); + ASSERT_EQ(ret, 0); +} + +/** + * @tc.name: AuthRequestNegotiateState::TransitionTo_001 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestInitState::TransitionTo with authManager = null + * 4 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, TransitionTo_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + authManager = nullptr; + authRequestState->authManager_ = authManager; + int32_t ret = authRequestState->TransitionTo(std::shared_ptr(new AuthRequestNegotiateState())); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestInitState::TransitionTo_002 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestInitState::TransitionTo with authManager != null + * 4 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, TransitionTo_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr context = std::make_shared(); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestNegotiateState()); + authManager->authRequestContext_ = std::make_shared(); + authManager->authMessageProcessor_ = std::make_shared(authManager); + context->sessionId = 123456; + authRequestState->SetAuthContext(context); + authRequestState->SetAuthManager(authManager); + int32_t ret = authRequestState->TransitionTo(std::shared_ptr(new AuthRequestNegotiateState())); + ASSERT_EQ(ret, DM_OK); + sleep(20); +} + +/** + * @tc.name: AuthRequestInitState::GetStateType_001 + * @tc.desc: 1 call AuthRequestInitState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_INIT); + sleep(15); +} + +/** + * @tc.name: AuthRequestInitState::Enter_001 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestInitState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + authManager = nullptr; + authRequestState->SetAuthManager(authManager); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestInitState::Enter_002 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestInitState::Enter with authManager != null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestInitState()); + authRequestState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + context->deviceId = "123456"; + authRequestState->SetAuthContext(context); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthRequestNegotiateState::GetStateType_002 + * @tc.desc: 1 call AuthRequestNegotiateState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_NEGOTIATE + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNegotiateState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_NEGOTIATE); + sleep(15); +} + +/** + * @tc.name: AuthRequestNegotiateState::Enter_001 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestNegotiateState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_003, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNegotiateState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestNegotiateState::Enter_002 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestNegotiateState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_004, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNegotiateState()); + std::shared_ptr hiChainConnector = std::make_shared(); + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestNegotiateState()); + authManager->authRequestContext_->deviceId = "111"; + authRequestState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + context->deviceId = "123456"; + context->sessionId = 22222; + authRequestState->SetAuthContext(context); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthRequestNegotiateDoneState::GetStateType_003 + * @tc.desc: 1 call AuthRequestNegotiateDoneState::GetStateType + * 2 check ret is AuthState::AUTH_REQUEST_NEGOTIATE_DONE + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_003, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNegotiateDoneState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_NEGOTIATE_DONE); + sleep(15); +} + +/** + * @tc.name: AuthRequestNegotiateDoneState::Enter_005 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestNegotiateDoneState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_005, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNegotiateDoneState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestNegotiateDoneState::Enter_006 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestNegotiateDoneState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_006, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNegotiateDoneState()); + std::shared_ptr hiChainConnector = std::make_shared(); + std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); + authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_); + authRequestState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + context->sessionId = 333333; + authRequestState->SetAuthContext(context); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthRequestReplyState::GetStateType_004 + * @tc.desc: 1 call AuthRequestReplyState::GetStateType + * 2 check ret is AuthState::AUTH_REQUEST_REPLY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_004, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestReplyState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_REPLY); + sleep(15); +} + +/** + * @tc.name: AuthRequestReplyState::Enter_007 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestReplyState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_007, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestReplyState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestReplyState::Enter_008 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestReplyState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_008, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestReplyState()); + std::shared_ptr hiChainConnector = std::make_shared(); + std::shared_ptr inputStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); + authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestReplyState()); + authManager->authResponseContext_->sessionId = 1; + authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_); + authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_); + authManager->SetAuthRequestState(authRequestState); + authRequestState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + context->sessionId = 333333; + authRequestState->SetAuthContext(context); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthRequestInputState::GetStateType_005 + * @tc.desc: 1 call AuthRequestInputState::GetStateType + * 2 check ret is AuthState::AUTH_REQUEST_INPUT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_005, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInputState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_INPUT); + sleep(15); +} + +/** + * @tc.name: AuthRequestInputState::Enter_009 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestInputState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_009, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInputState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestInputState::Enter_010 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestInputState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_010, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInputState()); + authRequestState->SetAuthManager(authManager); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthRequestJoinState::GetStateType_006 + * @tc.desc: 1 call AuthRequestJoinState::GetStateType + * 2 check ret is AuthState::AUTH_REQUEST_JOIN + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_006, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestJoinState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_JOIN); + sleep(15); +} + +/** + * @tc.name: AuthRequestJoinState::Enter_011 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestJoinState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_011, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestJoinState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestJoinState::Enter_012 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestJoinState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_012, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestJoinState()); + authManager->authRequestContext_ = std::make_shared(); + std::shared_ptr hiChainConnector = std::make_shared(); + std::shared_ptr joinStartTimer = std::make_shared(ADD_TIMEOUT_TASK); + authManager->timerMap_[ADD_TIMEOUT_TASK] = joinStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + nlohmann::json jsonObject; + authManager->authResponseContext_->groupId = "111"; + authManager->authResponseContext_->groupName = "222"; + authManager->authResponseContext_->code = 123; + authManager->authResponseContext_->requestId = 234; + authManager->authResponseContext_->deviceId = "234"; + jsonObject[TAG_GROUP_ID] = authManager->authResponseContext_->groupId; + jsonObject[TAG_GROUP_NAME] = authManager->authResponseContext_->groupName; + jsonObject[PIN_CODE_KEY] = authManager->authResponseContext_->code; + jsonObject[TAG_REQUEST_ID] = authManager->authResponseContext_->requestId; + jsonObject[TAG_DEVICE_ID] = authManager->authResponseContext_->deviceId; + authRequestState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + context->deviceId = "44444"; + context->sessionId = 55555; + authRequestState->SetAuthContext(context); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthRequestNetworkState::GetStateType_007 + * @tc.desc: 1 call AuthRequestNetworkState::GetStateType + * 2 check ret is AuthState::AUTH_REQUEST_NETWORK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_007, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNetworkState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_NETWORK); + sleep(15); +} + +/** + * @tc.name: AuthRequestNetworkState::Enter_013 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestNetworkState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_013, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNetworkState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestNetworkState::Enter_014 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestNetworkState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_014, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestNetworkState()); + std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); + authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestNetworkState()); + authManager->SetAuthRequestState(authRequestState); + authRequestState->SetAuthManager(authManager); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthRequestFinishState::GetStateType_008 + * @tc.desc: 1 call AuthRequestFinishState::GetStateType + * 2 check ret is AuthState::AUTH_REQUEST_FINISH + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, GetStateType_008, testing::ext::TestSize.Level0) +{ + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestFinishState()); + int32_t ret = authRequestState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_REQUEST_FINISH); + sleep(15); +} + +/** + * @tc.name: AuthRequestFinishState::Enter_015 + * @tc.desc: 1 set authManager to null + * 2 call AuthRequestFinishState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_015, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestFinishState()); + authRequestState->SetAuthManager(nullptr); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthRequestFinishState::Enter_016 + * @tc.desc: 1 set authManager not null + * 2 call AuthRequestNetworkState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthRequestStateTest, Enter_016, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = + std::shared_ptr(new AuthRequestFinishState()); + std::shared_ptr hiChainConnector = std::make_shared(); + std::shared_ptr inputStartTimer = std::make_shared(CONFIRM_TIMEOUT_TASK); + authManager->timerMap_[CONFIRM_TIMEOUT_TASK] = inputStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseFinishState()); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestFinishState()); + authManager->authResponseContext_->sessionId = 1; + authManager->authMessageProcessor_->SetResponseContext(authManager->authResponseContext_); + authManager->authMessageProcessor_->SetRequestContext(authManager->authRequestContext_); + authManager->SetAuthRequestState(authRequestState); + authRequestState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + context->sessionId = 333333; + authRequestState->SetAuthContext(context); + int32_t ret = authRequestState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_auth_request_state.h b/test/unittest/UTTest_auth_request_state.h new file mode 100644 index 00000000..11896312 --- /dev/null +++ b/test/unittest/UTTest_auth_request_state.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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_DM_AUTH_REQUEST_STATE_TEST_H +#define OHOS_DM_AUTH_REQUEST_STATE_TEST_H + +#include + +#include +#include +#include + +#include "auth_request_state.h" +#include "dm_log.h" + +namespace OHOS { +namespace DistributedHardware { +class AuthRequestStateTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/test/unittest/UTTest_auth_response_state.cpp b/test/unittest/UTTest_auth_response_state.cpp new file mode 100644 index 00000000..48e886fe --- /dev/null +++ b/test/unittest/UTTest_auth_response_state.cpp @@ -0,0 +1,480 @@ +/* + * Copyright (c) 2021 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 "UTTest_auth_response_state.h" + +#include "auth_response_state.h" +#include "dm_auth_manager.h" +#include "dm_constants.h" +#include "dm_log.h" + +namespace OHOS { +namespace DistributedHardware { +void AuthResponseStateTest::SetUp() +{ +} +void AuthResponseStateTest::TearDown() +{ +} +void AuthResponseStateTest::SetUpTestCase() +{ +} +void AuthResponseStateTest::TearDownTestCase() +{ +} + +namespace { + +/** + * @tc.name: AuthResponseInitState::SetAuthManager_001 + * @tc.desc: 1 set authManager not null + * 2 call AuthResponseInitState::SetAuthManager with authManager != null + * 3 check ret is authResponseState->authManager_.use_count() + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, SetAuthManager_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseInitState()); + authResponseState->SetAuthManager(authManager); + int32_t ret = authResponseState->authManager_.use_count(); + ASSERT_EQ(ret, 1); + sleep(15); +} + +/** + * @tc.name: AuthResponseInitState::SetAuthManager_002 + * @tc.desc: 1 set authManager to null + * 2 call AuthResponseInitState::SetAuthManager with authManager = null + * 3 check ret is authResponseState->authManager_.use_count() + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, SetAuthManager_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseInitState()); + authResponseState->SetAuthManager(nullptr); + int32_t ret = authResponseState->authManager_.use_count(); + ASSERT_EQ(ret, 0); + sleep(15); +} + +/** + * @tc.name: AuthResponseInitState::TransitionTo_001 + * @tc.desc: 1 set authManager to null + * 2 call AuthResponseInitState::TransitionTo with authManager = null + * 4 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, TransitionTo_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseInitState()); + authManager = nullptr; + authResponseState->authManager_ = authManager; + int32_t ret = authResponseState->TransitionTo(std::shared_ptr(new AuthResponseNegotiateState())); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthResponseInitState::TransitionTo_002 + * @tc.desc: 1 set authManager not null + * 2 call AuthResponseInitState::TransitionTo with authManager != null + * 4 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, TransitionTo_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr context = std::make_shared(); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestNegotiateState()); + authManager->authRequestContext_ = std::make_shared(); + authManager->authMessageProcessor_ = std::make_shared(authManager); + context->sessionId = 123456; + authRequestState->SetAuthContext(context); + authRequestState->SetAuthManager(authManager); + int32_t ret = authRequestState->TransitionTo(std::shared_ptr(new AuthRequestNegotiateState())); + ASSERT_EQ(ret, DM_OK); + sleep(20); +} + +/** + * @tc.name: AuthResponseInitState::GetStateType_001 + * @tc.desc: 1 call AuthResponseInitState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, GetStateType_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseInitState()); + int32_t ret = authResponseState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_RESPONSE_INIT); + sleep(15); +} + +/** + * @tc.name: AuthResponseInitState::Enter_001 + * @tc.desc: 1 call AuthResponseInitState::GetStateType + * 2 check ret is AuthResponseInitState::Enter + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseInitState()); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthResponseNegotiateState::GetStateType_002 + * @tc.desc: 1 call AuthResponseNegotiateState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_NEGOTIATE + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, GetStateType_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseNegotiateState()); + int32_t ret = authResponseState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_RESPONSE_NEGOTIATE); + sleep(15); +} + +/** + * @tc.name: AuthResponseNegotiateState::Enter_002 + * @tc.desc: 1 set authManager to null + * 2 call AuthResponseNegotiateState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseNegotiateState()); + authManager = nullptr; + authResponseState->SetAuthManager(authManager); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthResponseNegotiateState::Enter_003 + * @tc.desc: 1 set authManager not null + * 2 call AuthResponseNegotiateState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_003, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseNegotiateState()); + std::shared_ptr hiChainConnector = std::make_shared(); + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseNegotiateState()); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestNegotiateState()); + authManager->authResponseContext_->deviceId = "111"; + authManager->authResponseContext_->localDeviceId = "222"; + authResponseState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + context->deviceId = "123456"; + context->sessionId = 22222; + authResponseState->SetAuthContext(context); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: AuthResponseConfirmState::GetStateType_003 + * @tc.desc: 1 call AuthResponseConfirmState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_CONFIRM + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, GetStateType_003, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseConfirmState()); + int32_t ret = authResponseState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_RESPONSE_CONFIRM); + sleep(15); +} + +/** + * @tc.name: AuthResponseConfirmState::Enter_004 + * @tc.desc: 1 set authManager to null + * 2 call AuthResponseConfirmState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_004, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseConfirmState()); + authResponseState->SetAuthManager(nullptr); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthResponseConfirmState::Enter_005 + * @tc.desc: 1 set authManager not null + * 2 call AuthResponseConfirmState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_005, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseConfirmState()); + authResponseState->SetAuthManager(authManager); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthResponseGroupState::GetStateType_004 + * @tc.desc: 1 call AuthResponseGroupState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_GROUP + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, GetStateType_004, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseGroupState()); + int32_t ret = authResponseState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_RESPONSE_GROUP); + sleep(15); +} + +/** + * @tc.name: AuthResponseGroupState::Enter_006 + * @tc.desc: 1 set authManager to null + * 2 call AuthResponseGroupState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_006, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseGroupState()); + authResponseState->SetAuthManager(nullptr); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthResponseGroupState::Enter_007 + * @tc.desc: 1 set authManager not null + * 2 call AuthResponseGroupState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_007, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseGroupState()); + std::shared_ptr hiChainConnector = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authResponseContext_ = std::make_shared(); + authResponseState->SetAuthManager(authManager); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthResponseShowState::GetStateType_005 + * @tc.desc: 1 call AuthResponseShowState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_SHOW + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, GetStateType_005, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseShowState()); + int32_t ret = authResponseState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_RESPONSE_SHOW); + sleep(15); +} + +/** + * @tc.name: AuthResponseShowState::Enter_008 + * @tc.desc: 1 set authManager to null + * 2 call AuthResponseShowState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_008, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseShowState()); + authResponseState->SetAuthManager(nullptr); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthResponseShowState::Enter_009 + * @tc.desc: 1 set authManager not null + * 2 call AuthResponseShowState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_009, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseShowState()); + authResponseState->SetAuthManager(authManager); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthResponseFinishState::GetStateType_006 + * @tc.desc: 1 call AuthResponseShowState::GetStateType + * 2 check ret is AuthState::AUTH_RESPONSE_SHOW + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, GetStateType_006, testing::ext::TestSize.Level0) +{ + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseFinishState()); + int32_t ret = authResponseState->GetStateType(); + ASSERT_EQ(ret, AuthState::AUTH_RESPONSE_FINISH); + sleep(15); +} + +/** + * @tc.name: AuthResponseFinishState::Enter_010 + * @tc.desc: 1 set authManager to null + * 2 call AuthResponseFinishState::Enter with authManager = null + * 3 check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_010, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseFinishState()); + authResponseState->SetAuthManager(nullptr); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthResponseFinishState::Enter_011 + * @tc.desc: 1 set authManager not null + * 2 call AuthResponseFinishState::Enter with authManager != null + * 3 check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthResponseStateTest, Enter_011, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = + std::shared_ptr(new AuthResponseFinishState()); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestFinishState()); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseFinishState()); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->listener_ = std::make_shared(); + authManager->authResponseContext_->sessionId = 1; + authManager->authRequestContext_->deviceId = "2"; + authManager->authRequestContext_->hostPkgName = "3"; + authManager->authRequestContext_->token = "4"; + authManager->authResponseContext_->reply = 5; + authManager->authRequestContext_->reason = 6; + authManager->SetAuthResponseState(authResponseState); + authResponseState->SetAuthManager(authManager); + std::shared_ptr context = std::make_shared(); + authResponseState->SetAuthContext(context); + int32_t ret = authResponseState->Enter(); + ASSERT_EQ(ret, DM_OK); + sleep(15); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_auth_response_state.h b/test/unittest/UTTest_auth_response_state.h new file mode 100644 index 00000000..270e5caf --- /dev/null +++ b/test/unittest/UTTest_auth_response_state.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021 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_AUTH_RESPONSE_STATE_TEST_H +#define OHOS_AUTH_RESPONSE_STATE_TEST_H + +#include + +#include + +#include "auth_response_state.h" + +namespace OHOS { +namespace DistributedHardware { +class AuthResponseStateTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/test/unittest/UTTest_device_manager_service.cpp b/test/unittest/UTTest_device_manager_service.cpp new file mode 100644 index 00000000..585cea34 --- /dev/null +++ b/test/unittest/UTTest_device_manager_service.cpp @@ -0,0 +1,346 @@ +/* + * Copyright (c) 2021 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 "UTTest_device_manager_service.h" + +#include "dm_constants.h" +#include "dm_device_info.h" +#include "dm_log.h" + +namespace OHOS { +namespace DistributedHardware { + +IMPLEMENT_SINGLE_INSTANCE(DeviceManagerService); + +void DeviceManagerServiceTest::SetUp() +{ +} + +void DeviceManagerServiceTest::TearDown() +{ +} + +void DeviceManagerServiceTest::SetUpTestCase() +{ +} + +void DeviceManagerServiceTest::TearDownTestCase() +{ +} +namespace { + +std::string INPUT_TIMEOUT_TASK = "inputTimeoutTask"; + +/** + * @tc.name: StartDeviceDiscovery_001 + * @tc.desc: Set StartDeviceDiscovery's flag bit intFlag_ to False and return DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, StartDeviceDiscovery_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + std::string pkgName = "com.ohos.test"; + DmSubscribeInfo subscribeInfo; + std::string extra = "test"; + int ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeInfo, extra); + EXPECT_EQ(ret, DM_NOT_INIT); +} + +/** + * @tc.name: StartDeviceDiscovery_002 + * @tc.desc: Empty pkgName of StartDeviceDiscovery and return DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, StartDeviceDiscovery_002, testing::ext::TestSize.Level0) +{ + std::string pkgName; + DmSubscribeInfo subscribeInfo; + std::string extra = "test"; + DeviceManagerService::GetInstance().Init(); + int ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeInfo, extra); + EXPECT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: StartDeviceDiscovery_003 + * @tc.desc: Call StartDeviceDiscovery twice with pkgName not null and flag bit not false and return + * DM_DISCOVERY_REPEATED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, StartDeviceDiscovery_003, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + DmSubscribeInfo subscribeInfo; + std::string extra = "test"; + int ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeInfo, extra); + pkgName = "1com.ohos.test1"; + ret = DeviceManagerService::GetInstance().StartDeviceDiscovery(pkgName, subscribeInfo, extra); + EXPECT_EQ(ret, DM_DISCOVERY_FAILED); +} + +/** + * @tc.name: StopDeviceDiscovery_001 + * @tc.desc: Set StopDeviceDiscovery's flag bit intFlag_ to false and return DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, StopDeviceDiscovery_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + std::string pkgName = "com.ohos.test"; + uint16_t subscribeId = 1; + int ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId); + EXPECT_EQ(ret, DM_NOT_INIT); +} + +/** + * @tc.name:StopDeviceDiscovery_002 + * @tc.desc: StopDeviceDiscovery is initialized, pkgName is null, and its return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, StopDeviceDiscovery_002, testing::ext::TestSize.Level0) +{ + std::string pkgName; + uint16_t subscribeId = 1; + DeviceManagerService::GetInstance().Init(); + int ret = DeviceManagerService::GetInstance().StopDeviceDiscovery(pkgName, subscribeId); + EXPECT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: GetLocalDeviceInfo_001 + * @tc.desc: Set the flag bit of GetLocalDeviceInfo to intFlag_ to false; The return value is DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, GetLocalDeviceInfo_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + DmDeviceInfo info; + int ret = DeviceManagerService::GetInstance().GetLocalDeviceInfo(info); + EXPECT_EQ(ret, DM_NOT_INIT); +} + +/** + * @tc.name:GetLocalDeviceInfo_002 + * @tc.desc: Initialize the GetLocalDeviceInfo function with the return value DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, GetLocalDeviceInfo_002, testing::ext::TestSize.Level0) +{ + DmDeviceInfo info; + DeviceManagerService::GetInstance().Init(); + int ret = DeviceManagerService::GetInstance().GetLocalDeviceInfo(info); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: Init_001 + * @tc.desc: The OnDeviceFound function does the worong case and return DM_INT_MULTIPLE + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, Init_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = true; + int ret = DeviceManagerService::GetInstance().Init(); + EXPECT_EQ(ret, DM_INT_MULTIPLE); +} + +/** + * @tc.name: Init_002 + * @tc.desc: The OnDeviceFound function does the correct case and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, Init_002, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + std::shared_ptr softbusConnector_ = nullptr; + int ret = DeviceManagerService::GetInstance().Init(); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: GetTrustedDeviceList_001 + * @tc.desc: Set the intFlag of GetTrustedDeviceList to false. The return value is DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ + +HWTEST_F(DeviceManagerServiceTest, GetTrustedDeviceList_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + std::string pkgName = "com.ohos.test"; + std::string extra = "jdddd"; + std::vector deviceList; + int ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList); + EXPECT_EQ(ret, DM_NOT_INIT); +} + +/** + * @tc.name: GetTrustedDeviceList_002 + * @tc.desc:Set the intFlag of GetTrustedDeviceList to true and pkgName = null ; The return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, GetTrustedDeviceList_002, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = true; + std::string pkgName; + std::string extra = "jdddd"; + std::vector deviceList; + int ret = DeviceManagerService::GetInstance().GetTrustedDeviceList(pkgName, extra, deviceList); + EXPECT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: AuthenticateDevice_001 + * @tc.desc: 将GAuthenticateDevice的intFlag设置为false,设置pkgName = "com.ohos.test";其返回值为DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, AuthenticateDevice_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + std::string pkgName = "com.ohos.test"; + std::string extra = "jdddd"; + int32_t authType = 0; + std::string deviceId = " 2345"; + int ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, DM_NOT_INIT); +} + +/** + * @tc.name: AuthenticateDevice_002 + * @tc.desc: Set intFlag for GAuthenticateDevice to True and pkgName to null; The return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, AuthenticateDevice_002, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = true; + std::string pkgName; + std::string extra = "jdddd"; + int32_t authType = 0; + std::string deviceId = " 2345"; + int ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: AuthenticateDevice_003 + * @tc.desc: Set intFlag for GAuthenticateDevice to true and pkgName to com.ohos.test; The return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, AuthenticateDevice_003, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = true; + std::string pkgName = "com.ohos.test"; + std::string extra = "jdddd"; + int32_t authType = 0; + std::string deviceId = "123456"; + int ret = DeviceManagerService::GetInstance().AuthenticateDevice(pkgName, authType, deviceId, extra); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: UnAuthenticateDevice_001 + * @tc.desc: 将UnAuthenticateDevice的intFlag设置为false,设置pkgName = "com.ohos.test";其返回值为DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, UnAuthenticateDevice_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + std::string pkgName = "com.ohos.test"; + std::string deviceId = "12345"; + int ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, deviceId); + EXPECT_EQ(ret, DM_NOT_INIT); +} + +/** + * @tc.name: UnAuthenticateDevice_002 + * @tc.desc: Set intFlag for UnAuthenticateDevice to True and pkgName to null; The return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, UnAuthenticateDevice_002, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = true; + std::string pkgName; + std::string deviceId = "12345"; + int ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, deviceId); + EXPECT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: UnAuthenticateDevice_003 + * @tc.desc: Set intFlag for UnAuthenticateDevice to true and pkgName to com.ohos.test; set deviceId null ,The return + * value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, UnAuthenticateDevice_003, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = true; + std::string pkgName = "com.ohos.test"; + std::string deviceId; + int ret = DeviceManagerService::GetInstance().UnAuthenticateDevice(pkgName, deviceId); + EXPECT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: VerifyAuthentication_001 + * @tc.desc: Set intFlag for VerifyAuthentication to false and set authParam = "jdjjjj",The return value is + * DM_NOT_INIT + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, VerifyAuthentication_001, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = false; + std::string authParam = "jdjjjj"; + int ret = DeviceManagerService::GetInstance().VerifyAuthentication(authParam); + EXPECT_EQ(ret, DM_NOT_INIT); +} + +/** + * @tc.name: VerifyAuthentication_002 + * @tc.desc:Set intFlag for VerifyAuthentication to true and set authParam = "jdjjjj",The return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceTest, VerifyAuthentication_002, testing::ext::TestSize.Level0) +{ + DeviceManagerService::GetInstance().intFlag_ = true; + std::string authParam = "jdjjjj"; + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener_ = std::make_shared(); + DeviceManagerService::GetInstance().authMgr_ = std::make_shared(softbusConnector, listener_); + std::shared_ptr inputStartTimer = std::make_shared(INPUT_TIMEOUT_TASK); + DeviceManagerService::GetInstance().authMgr_->timerMap_[INPUT_TIMEOUT_TASK] = inputStartTimer; + int ret = DeviceManagerService::GetInstance().VerifyAuthentication(authParam); + EXPECT_EQ(ret, DM_FAILED); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_device_manager_service.h b/test/unittest/UTTest_device_manager_service.h new file mode 100644 index 00000000..30693e87 --- /dev/null +++ b/test/unittest/UTTest_device_manager_service.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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_UTTEST_DM_SERVICE_H +#define OHOS_UTTEST_DM_SERVICE_H + +#include +#include + +#include +#include + +#include "device_manager_service.h" +#include "device_manager_service_listener.h" +#include "dm_auth_manager.h" +#include "dm_device_info_manager.h" +#include "dm_device_state_manager.h" +#include "dm_discovery_manager.h" +#include "single_instance.h" +#include "softbus_connector.h" + +namespace OHOS { +namespace DistributedHardware { + +class DeviceManagerServiceTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#undef private +#undef protected +#endif \ No newline at end of file diff --git a/test/unittest/UTTest_device_manager_service_listener.cpp b/test/unittest/UTTest_device_manager_service_listener.cpp new file mode 100644 index 00000000..3d63e1f0 --- /dev/null +++ b/test/unittest/UTTest_device_manager_service_listener.cpp @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2021 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 "UTTest_device_manager_service_listener.h" + +#include + +#include "dm_anonymous.h" +#include "dm_constants.h" +#include "dm_log.h" +#include "ipc_notify_auth_result_req.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_notify_discover_result_req.h" +#include "ipc_notify_verify_auth_result_req.h" + +namespace OHOS { +namespace DistributedHardware { +void DeviceManagerServiceListenerTest::SetUp() +{ +} +void DeviceManagerServiceListenerTest::TearDown() +{ +} +void DeviceManagerServiceListenerTest::SetUpTestCase() +{ +} +void DeviceManagerServiceListenerTest::TearDownTestCase() +{ +} +namespace { + +/** + * @tc.name: OnDeviceStateChange_001 + * @tc.desc: OnDeviceStateChange, construct a dummy listener, pass in pkgName, use the constructed listener to get + * deviceTypeId + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceListenerTest, OnDeviceStateChange_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr listener_ = std::make_shared(); + std::string pkgName = "com.ohos.helloworld"; + DmDeviceState state = DEVICE_STATE_ONLINE; + DmDeviceInfo info = { + .deviceId = "asdad", + .deviceName = "asda", + .deviceTypeId = 1, + }; + listener_->OnDeviceStateChange(pkgName, state, info); + std::shared_ptr pReq = + std::static_pointer_cast(listener_->ipcServerListener_.req_); + int32_t dmState = pReq->GetDeviceState(); + EXPECT_EQ(1, dmState); +} + +/** + * @tc.name: OnAuthResult_001 + * @tc.desc:OnAuthResult, construct a dummy listener, pass in pkgName, use the constructed listener to get pkgName + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceListenerTest, OnAuthResult_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr listener_ = std::make_shared(); + std::string pkgName = "com.ohos.helloworld"; + std::string deviceId = "dkdkd"; + std::string token = "kdkddk"; + int32_t status = 3; + std::string reason = "kddk"; + listener_->OnAuthResult(pkgName, deviceId, token, status, reason); + std::shared_ptr pReq = + std::static_pointer_cast(listener_->ipcServerListener_.req_); + std ::string ret = pReq->GetPkgName(); + EXPECT_EQ(ret, pkgName); +} + +/** + * @tc.name: OnVerifyAuthResult_001 + * @tc.desc: OnVerifyAuthResult,construct a dummy listener, pass in pkgName, use the constructed listener to get + * deviceId + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerServiceListenerTest, OnVerifyAuthResult_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr listener_ = std::make_shared(); + std::string pkgName = "com.ohos.helloworld"; + std::string deviceId = "dkdkd"; + int32_t resultCode = 1; + std::string flag = "true"; + listener_->OnVerifyAuthResult(pkgName, deviceId, resultCode, flag); + std::shared_ptr pReq = + std::static_pointer_cast(listener_->ipcServerListener_.req_); + std ::string ret = pReq->GetDeviceId(); + EXPECT_EQ(ret, deviceId); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/UTTest_device_manager_service_listener.h b/test/unittest/UTTest_device_manager_service_listener.h new file mode 100644 index 00000000..2c07604c --- /dev/null +++ b/test/unittest/UTTest_device_manager_service_listener.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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_UTTEST_DM_SERVICE_LISTENER_H +#define OHOS_UTTEST_DM_SERVICE_LISTENER_H + +#define private public +#define protected public + +#include +#include + +#include + +#include "device_manager_service_listener.h" +#include "dm_device_info.h" +#include "ipc_server_listener.h" + +namespace OHOS { +namespace DistributedHardware { + +class DeviceManagerServiceListenerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#undef private +#undef protected +#endif \ No newline at end of file diff --git a/test/unittest/UTTest_device_message.cpp b/test/unittest/UTTest_device_message.cpp new file mode 100644 index 00000000..ba730896 --- /dev/null +++ b/test/unittest/UTTest_device_message.cpp @@ -0,0 +1,514 @@ +/* + * Copyright (c) 2021 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 "dm_log.h" +#include "dm_constants.h" +#include "UTTest_device_message.h" +#include "auth_message_processor.h" +#include "softbus_connector.h" +#include "softbus_session.h" +#include "dm_auth_manager.h" + +namespace OHOS { +namespace DistributedHardware { +void AuthMessageProcessorTest::SetUp() +{ +} +void AuthMessageProcessorTest::TearDown() +{ +} +void AuthMessageProcessorTest::SetUpTestCase() +{ +} +void AuthMessageProcessorTest::TearDownTestCase() +{ +} + +namespace{ + +/** + * @tc.name: AuthMessageProcessor::CreateNegotiateMessage_001 + * @tc.desc: 1 set cryptoAdapter_ to null + * 2 call AuthMessageProcessor::CreateNegotiateMessage_001 with cryptoAdapter_ = nullptr + * 3 check ret is authMessageProcessor->CreateNegotiateMessage(jsonObj); + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, CreateNegotiateMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + int32_t msgType = MSG_TYPE_NEGOTIATE; + nlohmann::json jsonObj; + jsonObj[TAG_VER] = DM_ITF_VER; + jsonObj[TAG_TYPE] = msgType; + authMessageProcessor->cryptoAdapter_ = nullptr; + authMessageProcessor->CreateNegotiateMessage(jsonObj); + std::string str1 = jsonObj.dump(); + nlohmann::json jsonObject; + jsonObject[TAG_VER] = DM_ITF_VER; + jsonObject[TAG_TYPE] = msgType; + jsonObject[TAG_CRYPTO_SUPPORT] = false; + std::string str2 = jsonObject.dump(); + ASSERT_EQ(str1, str2); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::CreateSyncGroupMessage_001 + * @tc.desc: Compare JSON before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, CreateSyncGroupMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + authMessageProcessor->authRequestContext_= std::make_shared(); + nlohmann::json jsona; + nlohmann::json jsonObj; + authMessageProcessor->authRequestContext_->deviceId = "132416546"; + std::vector syncGroupList ; + syncGroupList.push_back("1111"); + authMessageProcessor->authRequestContext_->syncGroupList = syncGroupList; + jsona[TAG_DEVICE_ID] = authMessageProcessor->authRequestContext_->deviceId; + jsona[TAG_GROUPIDS] = authMessageProcessor->authRequestContext_->syncGroupList; + authMessageProcessor->CreateSyncGroupMessage(jsonObj); + std::string str1 = jsona.dump(); + std::string str2 = jsonObj.dump(); + ASSERT_EQ(str1, str2); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::CreateResponseAuthMessage_001 + * @tc.desc: Compare JSON before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, CreateResponseAuthMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + authMessageProcessor->authResponseContext_ = std::make_shared(); + nlohmann::json jsona; + nlohmann::json jsonObj; + authMessageProcessor->authResponseContext_->reply = 0; + authMessageProcessor->authResponseContext_->deviceId = "132416546"; + nlohmann::json jsonb; + jsonb[TAG_GROUP_ID] = "123456"; + authMessageProcessor->authResponseContext_->groupId = jsonb.dump(); + authMessageProcessor->authResponseContext_->code = 1; + authMessageProcessor->authResponseContext_->networkId = "11112222"; + authMessageProcessor->authResponseContext_->requestId = 222222; + authMessageProcessor->authResponseContext_->groupName = "333333"; + jsona[TAG_REPLY] = authMessageProcessor->authResponseContext_->reply; + jsona[TAG_DEVICE_ID] = authMessageProcessor->authResponseContext_->deviceId; + jsona[PIN_CODE_KEY] = authMessageProcessor->authResponseContext_->code; + jsona[TAG_NET_ID] = authMessageProcessor->authResponseContext_->networkId; + jsona[TAG_REQUEST_ID] = authMessageProcessor->authResponseContext_->requestId; + jsona[TAG_GROUP_ID] = "123456"; + jsona[TAG_GROUP_NAME] = authMessageProcessor->authResponseContext_->groupName; + authMessageProcessor->CreateResponseAuthMessage(jsonObj); + std::string str1 = jsona.dump(); + std::string str2 = jsonObj.dump(); + ASSERT_EQ(str1, str2); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::CreateResponseFinishMessage_001 + * @tc.desc: Compare JSON before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, CreateResponseFinishMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + authMessageProcessor->authResponseContext_ = std::make_shared(); + nlohmann::json jsona; + nlohmann::json jsonObj; + authMessageProcessor->authResponseContext_->reply= 1; + jsona[TAG_REPLY] = authMessageProcessor->authResponseContext_->reply; + authMessageProcessor->CreateResponseFinishMessage(jsonObj); + std::string str1 = jsona.dump(); + std::string str2 = jsonObj.dump(); + ASSERT_EQ(str1, str2); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseResponseFinishMessage_001 + * @tc.desc: Compare JSON before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseResponseFinishMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext= std::make_shared(); + authMessageProcessor->authResponseContext_= std::make_shared(); + nlohmann::json jsonObj; + authMessageProcessor->authResponseContext_->reply = 1; + jsonObj[TAG_REPLY] = authMessageProcessor->authResponseContext_->reply; + authMessageProcessor->SetResponseContext(authResponseContext); + authMessageProcessor->ParseResponseFinishMessage(jsonObj); + ASSERT_EQ(authMessageProcessor->authResponseContext_,authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseAuthResponseMessage_001 + * @tc.desc: Compare JSON before and after assi gnment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseAuthResponseMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + nlohmann::json jsona; + authResponseContext->reply = 0; + authResponseContext->deviceId = "11111"; + authResponseContext->code = 1; + authResponseContext->networkId = "12345"; + authResponseContext->requestId = 2; + authResponseContext->groupId = "23456"; + authResponseContext->groupName = "34567"; + jsona[TAG_REPLY] = authResponseContext->reply; + jsona[TAG_DEVICE_ID] = authResponseContext->deviceId; + jsona[PIN_CODE_KEY] = authResponseContext->code; + jsona[TAG_NET_ID] = authResponseContext->networkId; + jsona[TAG_REQUEST_ID] = authResponseContext->requestId; + jsona[TAG_GROUP_ID] = authResponseContext->groupId; + jsona[TAG_GROUP_NAME] = authResponseContext->groupName; + authMessageProcessor->SetResponseContext(authResponseContext); + authMessageProcessor->ParseAuthResponseMessage(jsona); + ASSERT_EQ(authMessageProcessor->authResponseContext_, authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseAuthRequestMessage_001 + * @tc.desc: Compare JSON before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseAuthRequestMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->SetResponseContext(authResponseContext); + nlohmann::json json; + nlohmann::json jsonThumbnail; + authResponseContext->deviceId = "123"; + authResponseContext->reply = 0; + authResponseContext->authType = 222; + authResponseContext->networkId = "234"; + authResponseContext->groupId = "345"; + authResponseContext->groupName = "456"; + authResponseContext->requestId = 2333; + jsonThumbnail[TAG_DEVICE_ID] = authResponseContext->deviceId; + jsonThumbnail[TAG_REPLY] = authResponseContext->reply; + jsonThumbnail[TAG_AUTH_TYPE] = authResponseContext->authType; + jsonThumbnail[TAG_NET_ID] = authResponseContext->networkId; + jsonThumbnail[TAG_GROUP_ID] = authResponseContext->groupId ; + jsonThumbnail[TAG_GROUP_NAME] = authResponseContext->groupName; + jsonThumbnail[TAG_REQUEST_ID] = authResponseContext->requestId; + authMessageProcessor->authSplitJsonList_.push_back(jsonThumbnail); + int32_t ret = authMessageProcessor->ParseAuthRequestMessage(); + ASSERT_EQ(ret,DM_FAILED); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseAuthRequestMessage_002 + * @tc.desc: Compare JSON before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseAuthRequestMessage_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->SetResponseContext(authResponseContext); + nlohmann::json json; + nlohmann::json jsonThumbnail; + authResponseContext->deviceId = "123"; + authResponseContext->reply = 1; + authResponseContext->authType = 222; + authResponseContext->networkId = "234"; + authResponseContext->groupId = "345"; + authResponseContext->groupName = "456"; + authResponseContext->requestId = 2333; + jsonThumbnail[TAG_DEVICE_ID] = authResponseContext->deviceId; + jsonThumbnail[TAG_REPLY] = authResponseContext->reply; + jsonThumbnail[TAG_AUTH_TYPE] = authResponseContext->authType; + jsonThumbnail[TAG_NET_ID] = authResponseContext->networkId; + jsonThumbnail[TAG_GROUP_ID] = authResponseContext->groupId ; + jsonThumbnail[TAG_GROUP_NAME] = authResponseContext->groupName; + jsonThumbnail[TAG_REQUEST_ID] = authResponseContext->requestId; + authMessageProcessor->authSplitJsonList_.push_back(jsonThumbnail); + int32_t ret = authMessageProcessor->ParseAuthRequestMessage(); + ASSERT_EQ(ret,DM_OK); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseNegotiateMessage_001 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseNegotiateMessage_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->authResponseContext_ = std::make_shared(); + nlohmann::json jsonObj; + jsonObj[TAG_CRYPTO_SUPPORT]="CRYPTOSUPPORT"; + jsonObj[TAG_CRYPTO_SUPPORT] = authMessageProcessor->authResponseContext_->cryptoSupport; + authMessageProcessor->SetResponseContext(authResponseContext); + authMessageProcessor->ParseNegotiateMessage(jsonObj); + ASSERT_EQ( authMessageProcessor->authResponseContext_, authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseNegotiateMessage_002 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseNegotiateMessage_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->authResponseContext_ = std::make_shared(); + nlohmann::json jsonObj; + jsonObj[TAG_CRYPTO_NAME]="CRYPTONAME"; + jsonObj[TAG_CRYPTO_NAME] = authResponseContext->cryptoSupport; + authMessageProcessor->SetResponseContext(authResponseContext); + authMessageProcessor->ParseNegotiateMessage(jsonObj); + ASSERT_EQ( authMessageProcessor->authResponseContext_, authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseNegotiateMessage_003 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseNegotiateMessage_003, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->authResponseContext_ = std::make_shared(); + nlohmann::json jsonObj; + jsonObj[TAG_CRYPTO_VERSION]="CRYPTOVERSION"; + jsonObj[TAG_CRYPTO_VERSION] = authResponseContext->cryptoSupport; + authMessageProcessor->SetResponseContext(authResponseContext); + authMessageProcessor->ParseNegotiateMessage(jsonObj); + ASSERT_EQ( authMessageProcessor->authResponseContext_, authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseNegotiateMessage_004 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseNegotiateMessage_004, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->authResponseContext_ = std::make_shared(); + nlohmann::json jsonObj; + jsonObj[TAG_DEVICE_ID]="DEVICEID"; + jsonObj[TAG_DEVICE_ID] = authResponseContext->deviceId; + authMessageProcessor->SetResponseContext(authResponseContext); + authMessageProcessor->ParseNegotiateMessage(jsonObj); + ASSERT_EQ( authMessageProcessor->authResponseContext_, authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::ParseNegotiateMessage_005 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, ParseNegotiateMessage_005, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor= std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->authResponseContext_ = std::make_shared(); + nlohmann::json jsonObj; + jsonObj[TAG_LOCAL_DEVICE_ID]="LOCALDEVICEID"; + jsonObj[TAG_LOCAL_DEVICE_ID] = authResponseContext->localDeviceId; + authMessageProcessor->SetResponseContext(authResponseContext); + authMessageProcessor->ParseNegotiateMessage(jsonObj); + ASSERT_EQ( authMessageProcessor->authResponseContext_, authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::SetRequestContext_001 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, SetRequestContext_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor = std::make_shared(data); + std::shared_ptr authRequestContext = std::make_shared(); + authMessageProcessor->SetRequestContext(authRequestContext); + ASSERT_EQ(authMessageProcessor->authRequestContext_,authRequestContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::SetRequestContext_002 + * @tc.desc: Judge whether authrequestcontext is empty + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, SetRequestContext_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor = std::make_shared(data); + std::shared_ptr authRequestContext = std::make_shared(); + authMessageProcessor->SetRequestContext(nullptr); + ASSERT_EQ(authMessageProcessor->authRequestContext_,nullptr); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::SetResponseContext_001 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, SetResponseContext_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor = std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->SetResponseContext(authResponseContext); + ASSERT_EQ(authMessageProcessor->authResponseContext_,authResponseContext); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::SetResponseContext_002 + * @tc.desc: Judge whether authrequestcontext is empty + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, SetResponseContext_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor = std::make_shared(data); + std::shared_ptr authResponseContext = std::make_shared(); + authMessageProcessor->SetResponseContext(nullptr); + ASSERT_EQ(authMessageProcessor->authResponseContext_,nullptr); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::GetResponseContext_001 + * @tc.desc: Compare authResponseContext before and after assignment + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, GetResponseContext_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor = std::make_shared(data); + authMessageProcessor->authResponseContext_ = std::make_shared(); + std::shared_ptr authResponseContext = authMessageProcessor->GetResponseContext(); + ASSERT_EQ(authResponseContext, authMessageProcessor->authResponseContext_); + sleep(15); +} + +/** + * @tc.name: AuthMessageProcessor::GetResponseContext_002 + * @tc.desc: Judge whether authrequestcontext is empty + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(AuthMessageProcessorTest, GetResponseContext_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr data = std::make_shared(softbusConnector, listener); + std::shared_ptr authMessageProcessor = std::make_shared(data); + authMessageProcessor->authResponseContext_ = std::make_shared(); + std::shared_ptr authResponseContext = authMessageProcessor->GetResponseContext(); + ASSERT_NE(authResponseContext,nullptr); + sleep(15); +} +} +} +} diff --git a/test/unittest/UTTest_device_message.h b/test/unittest/UTTest_device_message.h new file mode 100644 index 00000000..3d818e9a --- /dev/null +++ b/test/unittest/UTTest_device_message.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2021 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_DEVICE_MESSAGE_TEST_H +#define OHOS_DEVICE_MESSAGE_TEST_H +#define private public +#define protected public + +#include +#include +#include +#include "nlohmann/json.hpp" +#include "auth_message_processor.h" + +namespace OHOS { +namespace DistributedHardware { +class AuthMessageProcessorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} +} +#endif // OHOS_DM_IMPL_TEST_H +#undef private +#undef protected diff --git a/test/unittest/UTTest_dm_auth_manager.cpp b/test/unittest/UTTest_dm_auth_manager.cpp new file mode 100644 index 00000000..a911efe3 --- /dev/null +++ b/test/unittest/UTTest_dm_auth_manager.cpp @@ -0,0 +1,569 @@ +/* + * Copyright (c) 2021 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 "dm_auth_manager.h" + +#include "nlohmann/json.hpp" + +#include "dm_log.h" +#include "dm_constants.h" +#include "auth_message_processor.h" +#include "UTTest_dm_auth_manager.h" + +namespace OHOS { +namespace DistributedHardware { +void DmAuthManagerTest::SetUp() +{ +} +void DmAuthManagerTest::TearDown() +{ +} +void DmAuthManagerTest::SetUpTestCase() +{ +} +void DmAuthManagerTest::TearDownTestCase() +{ +} + +namespace{ + + std::string AUTHENTICATE_TIMEOUT_TASK = "authenticateTimeoutTask"; + std::string NEGOTIATE_TIMEOUT_TASK = "negotiateTimeoutTask"; + std::string CONFIRM_TIMEOUT_TASK = "confirmTimeoutTask"; + std::string INPUT_TIMEOUT_TASK = "inputTimeoutTask"; + std::string ADD_TIMEOUT_TASK = "addTimeoutTask"; + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_001 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_AUTH_BUSINESS_BUSY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestInitState()); + std::shared_ptr context = std::make_shared(); + std::string pkgName = "111"; + int32_t authType = 1; + std::string deviceId = "222"; + std::string extra = "333"; + authRequestState->SetAuthContext(context); + authManager->SetAuthRequestState(authRequestState); + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret,DM_AUTH_BUSINESS_BUSY); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_002 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_AUTH_BUSINESS_BUSY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = std::shared_ptr(new AuthResponseInitState()); + std::shared_ptr context = std::make_shared(); + const std::string pkgName = "111"; + int32_t authType = 1; + const std::string deviceId = "222"; + const std::string extra = "333"; + authResponseState->SetAuthContext(context); + authManager->SetAuthResponseState(authResponseState); + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret,DM_AUTH_BUSINESS_BUSY); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_003 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_003, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + std::string pkgName = "111"; + int32_t authType = 1; + std::string deviceId = "222"; + std::string extra = ""; + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_004 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_004, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + nlohmann::json json; + const std::string pkgName = "111"; + int32_t authType = 4; + const std::string deviceId = "222"; + const std::string extra = "targetPkgName"; + authRequestContext->targetPkgName = "targetPkgName"; + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret, DM_INPUT_PARA_EMPTY); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_005 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_005, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + nlohmann::json json; + const std::string pkgName = "111"; + int32_t authType = 1; + const std::string deviceId = "222"; + const std::string extra = "targetPkgName"; + authRequestContext->targetPkgName = "targetPkgName"; + json[TARGET_PKG_NAME_KEY] = authRequestContext->targetPkgName; + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_006 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_006, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + nlohmann::json json; + const std::string pkgName = "111"; + int32_t authType = 1; + const std::string deviceId = "222"; + const std::string extra = "appName"; + authRequestContext->targetPkgName = "appName"; + json[APP_NAME_KEY] = authRequestContext->targetPkgName; + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_007 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_007, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + nlohmann::json json; + const std::string pkgName = "111"; + int32_t authType = 1; + const std::string deviceId = "222"; + const std::string extra = "appDescription"; + authRequestContext->targetPkgName = "appDescription"; + json[APP_DESCRIPTION_KEY] = authRequestContext->targetPkgName; + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_008 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_008, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + nlohmann::json json; + const std::string pkgName = "111"; + int32_t authType = 1; + const std::string deviceId = "222"; + const std::string extra = "appThumbnail"; + authRequestContext->targetPkgName = "appThumbnail"; + json[APP_THUMBNAIL] = authRequestContext->targetPkgName; + int32_t ret =authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DmAuthManager::AuthenticateDevice_009 + * @tc.desc: Call authenticatedevice to check whether the return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AuthenticateDevice_009, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + nlohmann::json json; + const std::string pkgName = "111"; + int32_t authType = 1; + const std::string deviceId = "222"; + const std::string extra = "appIcon"; + authRequestContext->targetPkgName = "appIcon"; + json[APP_ICON_KEY] = authRequestContext->targetPkgName; + int32_t ret = authManager->AuthenticateDevice(pkgName,authType,deviceId,extra); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DmAuthManager::UnAuthenticateDevice_001 + * @tc.desc: Call unauthenticateddevice to check whether the return value is DM_ FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, UnAuthenticateDevice_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + std::string pkgName = ""; + std::string deviceId = "222"; + int32_t ret =authManager->UnAuthenticateDevice(pkgName,deviceId); + ASSERT_EQ(ret,DM_FAILED); +} + +/** + * @tc.name: DmAuthManager::VerifyAuthentication_001 + * @tc.desc: Call verifyauthentication to check whether the return value is DM_ FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, VerifyAuthentication_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestNetworkState()); + std::shared_ptr negotiateStartTimer = std::make_shared(INPUT_TIMEOUT_TASK); + authManager->timerMap_[INPUT_TIMEOUT_TASK] = negotiateStartTimer; + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestNetworkState()); + authManager->authResponseContext_=nullptr; + authManager->SetAuthRequestState(authRequestState); + int32_t ret =authManager->HandleAuthenticateTimeout(); + ASSERT_EQ(ret,DM_FAILED); +} + + + + +/** + * @tc.name: DmAuthManager::HandleAuthenticateTimeout_001 + * @tc.desc: authResponseContext_= nullptr; Call handleauthenticatemeout to check whether the return value is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestNetworkState()); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestNetworkState()); + authManager->authResponseContext_=nullptr; + authManager->SetAuthRequestState(authRequestState); + int32_t ret =authManager->HandleAuthenticateTimeout(); + ASSERT_EQ(ret,DM_FAILED); +} + +/** + * @tc.name: DmAuthManager::HandleAuthenticateTimeout_002 + * @tc.desc: authResponseContext_= nullptr; Call handleauthenticatemeout to check whether the return value is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, HandleAuthenticateTimeout_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestFinishState()); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestFinishState()); + authManager->SetAuthRequestState(authRequestState); + int32_t ret =authManager->HandleAuthenticateTimeout(); + ASSERT_EQ(ret,DM_OK); +} + +/** + * @tc.name: DmAuthManager::EstablishAuthChannel_001 + * @tc.desc: Call establishauthchannel to check whether the return value is DM_ FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, EstablishAuthChannel_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr sessionSession = std::shared_ptr(); + std::shared_ptr authRequestContext = std::make_shared(); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestFinishState()); + std::string deviceId1; + int32_t ret =authManager->EstablishAuthChannel(deviceId1); + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: DmAuthManager::SendAuthRequest_001 + * @tc.desc: Cryptosupport = true call sendauthrequest to check whether the return value is DM_ FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, SendAuthRequest_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestNegotiateState()); + std::shared_ptr negotiateStartTimer = std::make_shared(NEGOTIATE_TIMEOUT_TASK); + authManager->timerMap_[NEGOTIATE_TIMEOUT_TASK] = negotiateStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared();; + authManager->authRequestState_ = std::shared_ptr(new AuthRequestFinishState()); + authManager->authResponseContext_->cryptoSupport = true; + authManager->SetAuthRequestState(authRequestState); + int32_t sessionId=1; + int32_t ret =authManager->SendAuthRequest(sessionId); + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: DmAuthManager::StartAuthProcess_001 + * @tc.desc: Whether the return value of calling startauthprocess is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, StartAuthProcess_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = std::shared_ptr(new AuthResponseConfirmState()); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseConfirmState()); + authManager->SetAuthResponseState(authResponseState); + int32_t action = 0; + int32_t ret =authManager->StartAuthProcess(action); + ASSERT_EQ(ret,DM_FAILED); +} + +/** + * @tc.name: DmAuthManager::StartAuthProcess_002 + * @tc.desc: Whether the return value of calling startauthprocess is DM_ OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, StartAuthProcess_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = std::shared_ptr(new AuthResponseConfirmState()); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseConfirmState()); + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->SetAuthResponseState(authResponseState); + authManager->authResponseContext_->sessionId=111; + int32_t action = 1; + int32_t ret =authManager->StartAuthProcess(action); + ASSERT_EQ(ret,DM_OK); +} + +/** + * @tc.name: DmAuthManager::CreateGroup_001 + * @tc.desc: Whether the return value of calling creategroup is DM_ OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, CreateGroup_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = std::shared_ptr(new AuthResponseConfirmState()); + std::shared_ptr hiChainConnector = std::make_shared(); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseConfirmState()); + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->SetAuthResponseState(authResponseState); + authManager->authResponseContext_->requestId=111; + authManager->authResponseContext_->groupName="111"; + int32_t action = 1; + int32_t ret =authManager->StartAuthProcess(action); + ASSERT_EQ(ret,DM_OK); +} + +/** + * @tc.name: DmAuthManager::AddMember_001 + * @tc.desc: Whether the return value of calling addmember is DM_ OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, AddMember_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = std::shared_ptr(new AuthResponseInitState()); + authManager->authRequestContext_ = std::make_shared(); + std::shared_ptr hiChainConnector = std::make_shared(); + std::shared_ptr joinStartTimer = std::make_shared(ADD_TIMEOUT_TASK); + authManager->timerMap_[ADD_TIMEOUT_TASK] = joinStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + nlohmann::json jsonObject; + authManager->authResponseContext_->groupId="111"; + authManager->authResponseContext_->groupName="222"; + authManager->authResponseContext_->code=123; + authManager->authResponseContext_->requestId=234; + authManager->authResponseContext_->deviceId="234"; + jsonObject[TAG_GROUP_ID] = authManager->authResponseContext_->groupId; + jsonObject[TAG_GROUP_NAME] = authManager->authResponseContext_->groupName; + jsonObject[PIN_CODE_KEY] = authManager->authResponseContext_->code; + jsonObject[TAG_REQUEST_ID] = authManager->authResponseContext_->requestId; + jsonObject[TAG_DEVICE_ID] = authManager->authResponseContext_->deviceId; + std::string deviceId = "44444"; + authManager->SetAuthResponseState(authResponseState); + int32_t ret =authManager->AddMember(deviceId); + ASSERT_EQ(ret,DM_OK); + sleep(15); +} + +/** + * @tc.name: DmAuthManager::JoinNetwork_001 + * @tc.desc: Whether the return value of calling joinnetwork is DM_ OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, JoinNetwork_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authRequestState = std::shared_ptr(new AuthRequestFinishState()); + std::shared_ptr authenticateStartTimer = std::make_shared(AUTHENTICATE_TIMEOUT_TASK); + authManager->timerMap_[AUTHENTICATE_TIMEOUT_TASK] = authenticateStartTimer; + authManager->authMessageProcessor_ = std::make_shared(authManager); + authManager->authResponseContext_ = std::make_shared(); + authManager->authRequestContext_ = std::make_shared(); + authManager->authRequestState_ = std::shared_ptr(new AuthRequestFinishState()); + authManager->SetAuthRequestState(authRequestState); + int32_t ret =authManager->JoinNetwork(); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DmAuthManager::GetIsCryptoSupport_001 + * @tc.desc: authResponseState_ Equal to nullptr + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, GetIsCryptoSupport_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseFinishState()); + bool isCryptoSupport = false; + int32_t ret =authManager->GetIsCryptoSupport(isCryptoSupport); + ASSERT_EQ(ret,DM_OK); +} + +/** + * @tc.name: DmAuthManager::SetAuthResponseState_001 + * @tc.desc: Is the authresponsestate assignment successful + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, SetAuthResponseState_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + std::shared_ptr authResponseState = std::shared_ptr(new AuthResponseFinishState()); + authManager->authResponseState_ = std::shared_ptr(new AuthResponseFinishState()); + authManager->SetAuthResponseState(authResponseState); + int32_t ret =authManager->SetAuthResponseState(authResponseState); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DmAuthManager::GetPinCode_001 + * @tc.desc: Return authresponsecontext - > code + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmAuthManagerTest, GetPinCode_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr listener = std::make_shared(); + std::shared_ptr authManager = std::make_shared(softbusConnector, listener); + authManager->authResponseContext_ = std::make_shared();; + int32_t ret =authManager->GetPinCode(); + ASSERT_EQ(ret,authManager->authResponseContext_->code); +} +} +} +} diff --git a/test/unittest/UTTest_dm_auth_manager.h b/test/unittest/UTTest_dm_auth_manager.h new file mode 100644 index 00000000..12aec029 --- /dev/null +++ b/test/unittest/UTTest_dm_auth_manager.h @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2021 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_DM_AUTH_MANAGER_TEST_H +#define OHOS_DM_AUTH_MANAGER_TEST_H + +#include +#include +#include + +#include "authentication.h" +#include "softbus_session.h" +#include "device_manager_service_listener.h" +#include "dm_adapter_manager.h" +#include "dm_constants.h" +#include "softbus_connector.h" +#include "hichain_connector.h" +#include "auth_request_state.h" +#include "auth_response_state.h" +#include "auth_message_processor.h" +#include "dm_timer.h" +#include "dm_auth_manager.h" + +namespace OHOS { +namespace DistributedHardware { +class DmAuthManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} +} +#endif diff --git a/test/unittest/UTTest_dm_device_info_manager.cpp b/test/unittest/UTTest_dm_device_info_manager.cpp new file mode 100644 index 00000000..992f5a01 --- /dev/null +++ b/test/unittest/UTTest_dm_device_info_manager.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2021 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 "UTTest_dm_device_info_manager.h" + +#include "dm_constants.h" +#include "dm_log.h" + +namespace OHOS { +namespace DistributedHardware { +void DeviceManagerImplTest::SetUp() +{ +} + +void DeviceManagerImplTest::TearDown() +{ +} + +void DeviceManagerImplTest::SetUpTestCase() +{ +} + +void DeviceManagerImplTest::TearDownTestCase() +{ +} + +namespace { + +std::shared_ptr softbusConnector = std::make_shared(); +std::shared_ptr listener_ = std::make_shared(); +std::shared_ptr dmDeviceStateManager = + std::shared_ptr(new DmDeviceInfoManager(softbusConnector)); + +/** + * @tc.name: DmDeviceInfoManager_001 + * @tc.desc: Returns a new pointer to the constructor DmDeviceInfoManager new + * to construct an environment where the device has been discovered, and stop discovering + * the device. Its return value is DM_INPUT_PARA_EMPTY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DeviceManagerImplTest, DmDeviceInfoManager_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr p = + std::shared_ptr(new DmDeviceInfoManager(softbusConnector)); + ASSERT_NE(p, nullptr); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/UTTest_dm_device_info_manager.h b/test/unittest/UTTest_dm_device_info_manager.h new file mode 100644 index 00000000..565d7a8f --- /dev/null +++ b/test/unittest/UTTest_dm_device_info_manager.h @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2021 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_UTTEST_DM_DEVICEINFO_MANAGER_H +#define OHOS_UTTEST_DM_DEVICEINFO_MANAGER_H + +#include +#include + +#include +#include + +#include "device_manager_service_listener.h" +#include "dm_adapter_manager.h" +#include "dm_device_info.h" +#include "dm_device_info_manager.h" +#include "softbus_connector.h" +#include "softbus_session_callback.h" + +namespace OHOS { +namespace DistributedHardware { + +class DeviceManagerImplTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#undef private +#undef protected +#endif \ No newline at end of file diff --git a/test/unittest/UTTest_dm_device_state_manager.cpp b/test/unittest/UTTest_dm_device_state_manager.cpp new file mode 100644 index 00000000..8ea55d52 --- /dev/null +++ b/test/unittest/UTTest_dm_device_state_manager.cpp @@ -0,0 +1,200 @@ +/* + * Copyright (c) 2021 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 "UTTest_dm_device_state_manager.h" +#include "dm_log.h" +#include "dm_constants.h" +#include "dm_adapter_manager.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_notify_auth_result_req.h" +#include "ipc_notify_verify_auth_result_req.h" +#include "dm_device_state_manager.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_discover_result_req.h" + +namespace OHOS { +namespace DistributedHardware { + +void DmDeviceStateManagerTest::SetUp() +{ +} + +void DmDeviceStateManagerTest::TearDown() +{ +} + +void DmDeviceStateManagerTest::SetUpTestCase() +{ +} + +void DmDeviceStateManagerTest::TearDownTestCase() +{ +} +namespace { + +std::shared_ptr softbusConnector = std::make_shared(); +std::shared_ptr listener_ = std::make_shared(); +std::shared_ptr dmDeviceStateManager = std::shared_ptr(new DmDeviceStateManager(softbusConnector, listener_)); + +/** + * @tc.name: DmDeviceStateManager_001 + * @tc.desc: set DmDeviceStateManager to tne new pointer,and it's not nullptr + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr p = std::shared_ptr(new DmDeviceStateManager(softbusConnector, listener_)); + ASSERT_NE(p, nullptr); + +} + +/** + * @tc.name: DmDeviceStateManager_002 + * @tc.desc: set DmDeviceStateManager to tne new pointer,it's not nullptr and delete it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr p = std::shared_ptr(new DmDeviceStateManager(softbusConnector, listener_)); + p.reset(); + EXPECT_EQ(p, nullptr); +} + +/** + * @tc.name: OnDeviceOnline_001 + * @tc.desc: set info.deviceId to some para,and return it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, OnDeviceOnline_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "123"; + DmDeviceInfo info ; + strncpy (info.deviceId, "123", sizeof(info.deviceId)); + dmDeviceStateManager->OnDeviceOnline(pkgName, info); + std::shared_ptr pReq = std::static_pointer_cast (listener_->ipcServerListener_.req_); + DmDeviceInfo ret = pReq->GetDeviceInfo(); + int result = strcmp(info.deviceId,ret.deviceId); + EXPECT_EQ (result ,0); +} + +/** + * @tc.name: OnDeviceOffline_001 + * @tc.desc: set info.deviceId to some para,and return it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, OnDeviceOffline_001, testing::ext::TestSize.Level0) +{ + std::string pkgName; + DmDeviceInfo info; + strncpy (info.deviceId, "123", sizeof(info.deviceId)); + dmDeviceStateManager->OnDeviceOffline(pkgName, info); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + DmDeviceInfo ret = pReq->GetDeviceInfo(); + int result = strcmp(info.deviceId,ret.deviceId); + EXPECT_EQ (result ,0); +} + +/** + * @tc.name: OnDeviceChanged_001 + * @tc.desc: set info.deviceId to some para,and return it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_001, testing::ext::TestSize.Level0) +{ + std::string pkgName; + DmDeviceInfo info ; + strncpy (info.deviceId, "123", sizeof(info.deviceId)); + dmDeviceStateManager->OnDeviceChanged(pkgName, info); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + DmDeviceInfo ret = pReq->GetDeviceInfo(); + int result = strcmp(info.deviceId,ret.deviceId); + EXPECT_EQ (result ,0); +} + +/** + * @tc.name: OnDeviceReady_001 + * @tc.desc: set info.deviceId to some para,and return it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, OnProfileReady_001, testing::ext::TestSize.Level0) +{ + std::string pkgName; + std::string deviceId; + DmDeviceInfo info ; + strncpy (info.deviceId, "123", sizeof(info.deviceId)); + dmDeviceStateManager->OnProfileReady(pkgName, deviceId); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + DmDeviceInfo ret = pReq->GetDeviceInfo(); + int result = strcmp(info.deviceId,ret.deviceId); + ASSERT_GE(result,0); +} + +/** + * @tc.name: OnDeviceReady_001 + * @tc.desc: set info.deviceId to 123,and call OnDeviceReady ,change info.deviceId to 4 + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, OnDeviceReady_001, testing::ext::TestSize.Level0) +{ + std::string pkgName; + DmDeviceInfo info ; + strncpy (info.deviceId, "123", sizeof(info.deviceId)); + dmDeviceStateManager->OnDeviceReady(pkgName,info); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + DmDeviceInfo ret = pReq->GetDeviceInfo(); + int result = strcmp(info.deviceId,ret.deviceId); + ASSERT_GE(result,0); +} + +/** + * @tc.name: OnDeviceChanged_001 + * @tc.desc: set info.deviceId to 123,and call OnDeviceChanged ,change info.deviceId to 4 + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_002, testing::ext::TestSize.Level0) +{ + std::string pkgName; + DmDeviceInfo info ; + strncpy (info.deviceId, "123", sizeof(info.deviceId)); + dmDeviceStateManager->OnDeviceChanged(pkgName, info); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + DmDeviceInfo ret = pReq->GetDeviceInfo(); + int result = strcmp(info.deviceId,ret.deviceId); + ASSERT_GE(result,0); +} + +/** + * @tc.name: RegisterSoftbusStateCallback_001 + * @tc.desc: call RegisterSoftbusStateCallback and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDeviceStateManagerTest, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0) +{ + int ret = dmDeviceStateManager->RegisterSoftbusStateCallback(); + EXPECT_EQ (ret,DM_OK); +} +} +} +} \ No newline at end of file diff --git a/test/unittest/UTTest_dm_device_state_manager.h b/test/unittest/UTTest_dm_device_state_manager.h new file mode 100644 index 00000000..68f88a29 --- /dev/null +++ b/test/unittest/UTTest_dm_device_state_manager.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2021 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_UTTEST_DM_DEVICE_STATE_MANAGER_H +#define OHOS_UTTEST_DM_DEVICE_STATE_MANAGER_H + +#include +#include + +#include "softbus_connector.h" +#include "dm_adapter_manager.h" +#include "device_manager_service_listener.h" +#include "softbus_session_callback.h" +#include "dm_discovery_manager.h" + +namespace OHOS +{ +namespace DistributedHardware +{ + +class DmDeviceStateManagerTest : public testing::Test +{ +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} +} +#undef private +#undef protected +#endif \ No newline at end of file diff --git a/test/unittest/UTTest_dm_discovery_manager.cpp b/test/unittest/UTTest_dm_discovery_manager.cpp new file mode 100644 index 00000000..c8e89ae0 --- /dev/null +++ b/test/unittest/UTTest_dm_discovery_manager.cpp @@ -0,0 +1,252 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +#include "dm_log.h" +#include "dm_constants.h" +#include "dm_anonymous.h" +#include "ipc_server_listener.h" +#include "device_manager_service_listener.h" +#include "UTTest_dm_discovery_manager.h" +#include "discovery_service.h" + +namespace OHOS { +namespace DistributedHardware { + +void DmDiscoveryManagerTest::SetUp() { +} + +void DmDiscoveryManagerTest::TearDown() { +} + +void DmDiscoveryManagerTest::SetUpTestCase() { +} + +void DmDiscoveryManagerTest::TearDownTestCase() { +} + +namespace +{ +std::shared_ptr softbusConnector_ = std::make_shared(); +std::shared_ptr listener_ = std::make_shared(); +std::shared_ptr discoveryMgr_ = std::make_shared(softbusConnector_, listener_); + +/** + * @tc.name: DmDiscoveryManager_001 + * @tc.desc: Test whether the DmDiscoveryManager function can generate a new pointer + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_001, testing::ext::TestSize.Level0) { + std::shared_ptr Test = std::make_shared(softbusConnector_, listener_); + ASSERT_NE(Test, nullptr); +} + +/** + * @tc.name: DmDiscoveryManager_002 + * @tc.desc: Test whether the DmDiscoveryManager function can delete a new pointer + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, DmDiscoveryManager_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr Test = std::make_shared(softbusConnector_, listener_); + Test.reset(); + EXPECT_EQ(Test, nullptr); +} + +/** + * @tc.name:StartDeviceDiscovery_001 + * @tc.desc: keeping pkgame unchanged, call StartDeviceDiscovery twice so that its discoveryQueue is not empty and return DM_DISCOVERY_REPEATED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + DmSubscribeInfo subscribeInfo; + const std::string extra; + discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra); + int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra); + EXPECT_EQ (ret, DM_DISCOVERY_REPEATED); +} + +/** + * @tc.name:StartDeviceDiscovery_002 + * @tc.desc: pkgame changed, call StartDeviceDiscovery twice so that its discoveryQueue is not empty and return DM_DISCOVERY_REPEATED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, StartDeviceDiscovery_002, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + std::string extra; + DmSubscribeInfo subscribeInfo; + discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra); + pkgName = "com.ohos.helloworld.new"; + int32_t ret = discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra); + ASSERT_EQ(ret, DM_DISCOVERY_FAILED); +} + +/** + * @tc.name:StopDeviceDiscovery_001 + * @tc.desc: Assign the correct value + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +//doing +HWTEST_F(DmDiscoveryManagerTest, StopDeviceDiscovery_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + const std::string extra; + DmSubscribeInfo subscribeInfo; + uint16_t subscribeId = 1; + discoveryMgr_->StartDeviceDiscovery(pkgName, subscribeInfo, extra); + int ret = discoveryMgr_->StopDeviceDiscovery(pkgName, subscribeId); + ASSERT_NE(ret, DM_OK); +} + +/** + * @tc.name: OnDeviceFound_001 + * @tc.desc: The OnDeviceFound function does the correct case and assigns pkgName + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + uint16_t aaa = 11; + DmDiscoveryContext context{pkgName, "121110", aaa}; + discoveryMgr_->discoveryContextMap_[pkgName] = context; + sleep(1); + DmDeviceInfo info; + info.deviceId[0] = '\0'; + info.deviceName[0] = '\0'; + discoveryMgr_->OnDeviceFound(pkgName, info); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName); + EXPECT_EQ (ret1, 1); + std ::string ret = pReq->GetPkgName(); + EXPECT_EQ (ret,pkgName); +} + +/** + * @tc.name: OnDeviceFound_002 + * @tc.desc: set pkgName not null and discoveryContextMap_ null and return + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, OnDeviceFound_002, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + DmDeviceInfo info; + discoveryMgr_->OnDeviceFound(pkgName, info); + int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName); + EXPECT_EQ (ret1, 1); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + std ::string ret = pReq->GetPkgName(); + EXPECT_EQ (ret, pkgName); +} + +/** + * @tc.name: OnDiscoveryFailed_001 + * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + int32_t subscribeId = 1; + int32_t failedReason = 3; + discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + std ::string ret = pReq->GetPkgName(); + EXPECT_EQ (ret, pkgName); +} + +/** + * @tc.name: OnDiscoveryFailed_002 + * @tc.desc: The OnDeviceFound function takes the wrong case and emptying pkgName + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, OnDiscoveryFailed_002, testing::ext::TestSize.Level0) { + std::string pkgName; + int32_t subscribeId = 1; + int32_t failedReason = 3; + discoveryMgr_->OnDiscoveryFailed(pkgName, subscribeId, failedReason); + int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName); + EXPECT_EQ (ret1, 0); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + std ::string ret = pReq->GetPkgName(); + EXPECT_EQ (ret, pkgName); +} + +/** + * @tc.name: OnDiscoverySuccess_001 + * @tc.desc: The OnDeviceFound function takes the wrong case and return pkgName + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + int32_t subscribeId = 1; + discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + std ::string ret = pReq->GetPkgName(); + EXPECT_EQ (ret, pkgName); +} + +/** + * @tc.name: OnDiscoverySuccess_002 + * @tc.desc: set pkgName null and return discoveryContextMap_ null and return pkgName(null) + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, OnDiscoverySuccess_002, testing::ext::TestSize.Level0) +{ + std::string pkgName ; + int32_t subscribeId = 1; + discoveryMgr_->OnDiscoverySuccess(pkgName, subscribeId); + int ret1 = discoveryMgr_->discoveryContextMap_.count(pkgName); + EXPECT_EQ (ret1, 1); + std::shared_ptr pReq = std::static_pointer_cast(listener_->ipcServerListener_.req_); + std ::string ret = pReq->GetPkgName(); + EXPECT_EQ (ret, pkgName); +} + +/** + * @tc.name: HandleDiscoveryTimeout_001 + * @tc.desc: set pkgName not null and return 1(true) + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(DmDiscoveryManagerTest, HandleDiscoveryTimeout_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + discoveryMgr_->HandleDiscoveryTimeout() ; + int ret = discoveryMgr_->discoveryContextMap_.count(pkgName); + EXPECT_EQ (ret, 1); +} +} +} +} + + diff --git a/test/unittest/UTTest_dm_discovery_manager.h b/test/unittest/UTTest_dm_discovery_manager.h new file mode 100644 index 00000000..44a25e2b --- /dev/null +++ b/test/unittest/UTTest_dm_discovery_manager.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2021 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_UTTEST_DM_DISCOVERY_MANAGER_H +#define OHOS_UTTEST_DM_DISCOVERY_MANAGER_H +#include +#include +#include + +#include "softbus_connector.h" +#include "device_manager_service_listener.h" +#include "dm_discovery_manager.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_discover_result_req.h" + +#define private public +namespace OHOS +{ +namespace DistributedHardware +{ +class DmDiscoveryManagerTest : public testing::Test +{ +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} +} +#endif // OHOS_UTTEST_DM_DISCOVERY_MANAGER_H \ No newline at end of file diff --git a/test/unittest/UTTest_hichain_connector.cpp b/test/unittest/UTTest_hichain_connector.cpp new file mode 100644 index 00000000..db28b460 --- /dev/null +++ b/test/unittest/UTTest_hichain_connector.cpp @@ -0,0 +1,490 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "parameter.h" +#include "dm_anonymous.h" +#include "dm_log.h" +#include "dm_constants.h" +#include "dm_random.h" +#include "UTTest_hichain_connector.h" +#include "hichain_connector.h" + +namespace OHOS +{ +namespace DistributedHardware +{ +void HichainConnectorTest::SetUp() +{ +} +void HichainConnectorTest::TearDown() +{ +} +void HichainConnectorTest::SetUpTestCase() +{ +} +void HichainConnectorTest::TearDownTestCase() +{ +} +namespace { +/** + * @tc.name: CreateGroup_001 + * @tc.desc: Set the deviceGroupManager_ pointer to CreateGroup to NULlptr and return DM_INVALID_VALUE + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, CreateGroup_001, testing::ext::TestSize.Level0) +{ + int64_t requestId = 123456; + std::string groupName = "dfggg"; + std::shared_ptr hichainConnector = std::make_shared(); + hichainConnector-> deviceGroupManager_ = nullptr; + int ret = hichainConnector->CreateGroup(requestId, groupName); + EXPECT_EQ(ret, DM_INVALID_VALUE); +} + +/** + * @tc.name: CreateGroup_002 + * @tc.desc: Set CreateGroup to the correct process and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, CreateGroup_002, testing::ext::TestSize.Level0) +{ + int64_t requestId = 123456; + std::string groupName = "uuiioo"; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->CreateGroup(requestId, groupName); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: GetGroupInfo_001 + * @tc.desc: set groupName not null and return false + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetGroupInfo_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr hichainConnector = std::make_shared(); + std::string groupName = "dcdkdkd1"; + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_NAME] = groupName.c_str(); + std::string queryParams = jsonObj.dump(); + std::vector groupList; + int ret = hichainConnector->GetGroupInfo(queryParams, groupList); + EXPECT_EQ(ret, 0); +} + +/** + * @tc.name: GetGroupInfo_003 + * @tc.desc: set groupName nou null groupListot null and return 0 + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetGroupInfo_003, testing::ext::TestSize.Level0) +{ + std::string groupName = "lcdkddkd1 "; + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_NAME] = groupName.c_str(); + std::string queryParams = jsonObj.dump(); + GroupInfo aa; + aa.groupName = "afa"; + std::vector groupList; + groupList.push_back(aa); + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->GetGroupInfo(queryParams, groupList); + EXPECT_EQ(ret, 0); +} + +/** + * @tc.name: IsGroupInfoInvalid_001 + * @tc.desc: GroupType is GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP, group.groupVisibility is not GROUP_VISIBILITY_PUBLIC. + Group.return true + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ + +HWTEST_F(HichainConnectorTest, IsGroupInfoInvalid_001, testing::ext::TestSize.Level0) +{ + GroupInfo group; + group.groupName = "dkdkkdkdk"; + group.groupId = 1; + group.groupOwner = "ohos.distributedhardware.devicemanager"; + group.groupType = 7; + group.groupVisibility = 1; + std::shared_ptr hichainConnector = std::make_shared(); + bool ret = hichainConnector->IsGroupInfoInvalid(group); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: IsGroupInfoInvalid_002 + * @tc.desc: GroupType is GROUP_TYPE_IDENTICAL_ACCOUNT_GROUP, group.groupVisibility is GROUP_VISIBILITY_PUBLIC, + Grou. groupOwner is not equal to DM_PKG_NAME. The value is true + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, IsGroupInfoInvalid_002, testing::ext::TestSize.Level0) +{ + GroupInfo group; + group.groupName = "test"; + group.groupId = 1; + group.groupOwner = "ohos.disware"; + group.groupType = 1; + group.groupVisibility = -1; + std::shared_ptr hichainConnector = std::make_shared(); + bool ret = hichainConnector->IsGroupInfoInvalid(group); + EXPECT_EQ(ret, true); +} + +/** + * @tc.name: DelMemberFromGroup_001 + * @tc.desc:set groupId, deviceId null and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, DelMemberFromGroup_001, testing::ext::TestSize.Level0) +{ + std::string groupId ; + std::string deviceId; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->DelMemberFromGroup(groupId, deviceId); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: DelMemberFromGroup_002 + * @tc.desc: The groupId "34451"; The deviceId = "123"; Can be deleted correctly + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, DelMemberFromGroup_002, testing::ext::TestSize.Level0) +{ + std::string groupId = "34451"; + std::string deviceId = "123"; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->DelMemberFromGroup(groupId, deviceId); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: GenRequestId_001 + * @tc.desc:Call the GenRequestId function + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GenRequestId_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->GenRequestId(); + ASSERT_NE(ret, 0); +} + +/** + * @tc.name: from_json_001 + * @tc.desc: Pass in arguments to the from_JSON function and convert it to the correct value + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, from_json_001, testing::ext::TestSize.Level0) +{ + GroupInfo groupInfo; + groupInfo.groupName = "aaaa"; + groupInfo.groupId = "345678"; + groupInfo.groupOwner = "lllll"; + groupInfo.groupType = 5; + groupInfo.groupVisibility = 5; + nlohmann::json jsonObject; + jsonObject[FIELD_GROUP_NAME] = groupInfo.groupName; + jsonObject[FIELD_GROUP_ID] = groupInfo.groupId ; + jsonObject[ FIELD_GROUP_OWNER] = groupInfo.groupOwner; + jsonObject[ FIELD_GROUP_TYPE] = groupInfo.groupType; + jsonObject[FIELD_GROUP_VISIBILITY] = groupInfo.groupVisibility; + from_json(jsonObject, groupInfo); + EXPECT_EQ(groupInfo.groupName, "aaaa"); + EXPECT_EQ(groupInfo.groupId, "345678"); + EXPECT_EQ(groupInfo.groupOwner, "lllll"); + EXPECT_EQ(groupInfo.groupType, 5); + EXPECT_EQ(groupInfo.groupVisibility, 5); +} + +/** + * @tc.name: HiChainConnector_001 + * @tc.desc: Returns a new pointer to the HiChainConnector constructor new + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, HiChainConnector_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr m_HiChainConnector = std::make_shared(); + ASSERT_NE(m_HiChainConnector, nullptr); +} + +/** + * @tc.name: HiChainConnector_002 + * @tc.desc: Give the HiChainConnector constructor new a new pointer and delete it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, HiChainConnector_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr m_HiChainConnector = std::make_shared(); + m_HiChainConnector.reset(); + EXPECT_EQ(m_HiChainConnector, nullptr); +} + +/** + * @tc.name:RegisterHiChainCallback_001 + * @tc.desc: Call the RegisterHiChainCallback function with a return value of DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, RegisterHiChainCallback_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.softbus.test"; + std::shared_ptr listener_ = std::make_shared(); + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr discoveryMgr_ = std::make_shared(softbusConnector, listener_); + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->RegisterHiChainCallback(pkgName, std::shared_ptr(discoveryMgr_)); + int ret1 = HiChainConnector:: hiChainConnectorCallbackMap_.count(pkgName); + EXPECT_EQ(ret1, 1); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: IsGroupCreated_001 + * @tc.desc: Call the RegisterHiChainCallback function with a return value of DM_OK + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, IsGroupCreated_001, testing::ext::TestSize.Level0) +{ + std::string groupName = "dcdkdkd1"; + nlohmann::json jsonObj; + jsonObj[FIELD_GROUP_NAME] = groupName.c_str(); + std::string queryParams = jsonObj.dump(); + std::vector groupList ; + GroupInfo groupInfo; + std::shared_ptr hichainConnector = std::make_shared(); + bool ret = hichainConnector->IsGroupCreated( groupName, groupInfo); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: AddMember_001 + * @tc.desc: set deviceGroupManager_ = nullptr; + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, AddMember_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr hichainConnector = std::make_shared(); + hichainConnector->deviceGroupManager_ = nullptr; + std::string deviceId ; + std::string connectInfo; + int ret = hichainConnector->AddMember(deviceId, connectInfo); + EXPECT_EQ(ret, -1); +} + +/** + * @tc.name: AddMember_002 + * @tc.desc: set deviceId and connectInfo = null; + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, AddMember_002, testing::ext::TestSize.Level0) +{ + std::string deviceId; + std::string connectInfo; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->AddMember(deviceId, connectInfo); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: AddMember_002 + * @tc.desc: set deviceId and connectInfo = null; + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, AddMember3, testing::ext::TestSize.Level0) +{ + std::string deviceId = "123456"; + std::string connectInfo = "dkdkk"; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->AddMember(deviceId, connectInfo); + ASSERT_GE(ret, 1); +} + +/** + * @tc.name: onRequest_001 + * @tc.desc:set operationCode != GroupOperationCode::MEMBER_JOIN(3); return nullptr ; + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, onRequest_001, testing::ext::TestSize.Level0) +{ + int64_t requestId = 2; + int32_t operationCode = 2; + char *reqParams; + std::shared_ptr hichainConnector = std::make_shared(); + char *ret = hichainConnector->onRequest(requestId, operationCode, reqParams); + EXPECT_EQ(ret, nullptr); +} + +/** + * @tc.name: GetConnectPara_001 + * @tc.desc: set para not null and go to the second master + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetConnectPara_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "softbus"; + std::shared_ptr listener_ = std::make_shared(); + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr discoveryMgr_ = std::make_shared(softbusConnector, listener_); + std::shared_ptr hichainConnector = std::make_shared(); + hichainConnector->RegisterHiChainCallback(pkgName, std::shared_ptr(discoveryMgr_)); + std::string deviceId = "23445"; + std::string reqDeviceId = "234566"; + std::string p; + std::string ret = hichainConnector->GetConnectPara(deviceId, reqDeviceId); + EXPECT_EQ(ret, p); +} + +/** + * @tc.name: GetConnectPara_002 + * @tc.desc:Empty deviceId so that jsonObject.is_discarded is null and the value of connectAddr is returned + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetConnectPara_002, testing::ext::TestSize.Level0) +{ + std::string deviceId; + std::string reqDeviceId = "234566"; + std::string pkgName = "softbus"; + std::shared_ptr listener_ = std::make_shared(); + std::shared_ptr softbusConnector = std::make_shared(); + std::shared_ptr discoveryMgr_ = std::make_shared(softbusConnector, listener_); + std::shared_ptr hichainConnector = std::make_shared(); + hichainConnector->RegisterHiChainCallback(pkgName, std::shared_ptr(discoveryMgr_)); + std::string ret = hichainConnector->GetConnectPara(deviceId, reqDeviceId); + EXPECT_EQ(ret,""); +} + +/** + * @tc.name: DeleteGroup_001 + * @tc.desc: set groupId = "34567",and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, DeleteGroup_001, testing::ext::TestSize.Level0) +{ + std::string groupId = "34567"; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->DeleteGroup(groupId); + EXPECT_EQ(ret,DM_OK); +} + +/** + * @tc.name: GetRelatedGroups_001 + * @tc.desc: set DeviceId 123 groupList null and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetRelatedGroups_001, testing::ext::TestSize.Level0) +{ + std::string DeviceId = "123"; + std::vector groupList; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->GetRelatedGroups(DeviceId, groupList); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: GetRelatedGroups_003 + * @tc.desc: set DeviceId = 12345,groupList null and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetRelatedGroups_003, testing::ext::TestSize.Level0) +{ + std::string DeviceId = "12345"; + std::vector groupList; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->GetRelatedGroups(DeviceId, groupList); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: SyncGroups_001 + * @tc.desc: set deviceId = "34567",and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, SyncGroups_001, testing::ext::TestSize.Level0) +{ + std::string deviceId = "34567"; + std::vector remoteGroupIdList; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->SyncGroups(deviceId, remoteGroupIdList); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: GetSyncGroupList_001 + * @tc.desc: set groupList null,and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetSyncGroupList_001, testing::ext::TestSize.Level0) +{ + std::vector groupList; + std::vector syncGroupList; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->GetSyncGroupList(groupList, syncGroupList); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: GetSyncGroupList_002 + * @tc.desc: set groupList not null,and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(HichainConnectorTest, GetSyncGroupList_002, testing::ext::TestSize.Level0) +{ + std::vector groupList; + GroupInfo groupList1; + groupList1.groupName = "hichainconnector"; + groupList1.groupId = "123456"; + groupList1.groupOwner ="doftbus"; + groupList1.groupType = 1; + groupList1.groupVisibility = 2; + groupList.push_back(groupList1); + std::vector syncGroupList; + std::shared_ptr hichainConnector = std::make_shared(); + int ret = hichainConnector->GetSyncGroupList(groupList, syncGroupList); + EXPECT_EQ(ret, DM_OK); +} +} +} +} + diff --git a/test/unittest/UTTest_hichain_connector.h b/test/unittest/UTTest_hichain_connector.h new file mode 100644 index 00000000..29b1ac99 --- /dev/null +++ b/test/unittest/UTTest_hichain_connector.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021 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_UTTEST_HICHAIN_CONNECTOR_H +#define OHOS_UTTEST_HICHAIN_CONNECTOR_H + +#include +#include +#include +#include +#include +#include +#include + +#include "nlohmann/json.hpp" +#include "device_auth.h" +#include "single_instance.h" +#include "hichain_connector_callback.h" +#include "device_manager_service_listener.h" +#include "dm_auth_manager.h" +#include "dm_device_state_manager.h" +#include "hichain_connector.h" + +namespace OHOS +{ +namespace DistributedHardware +{ +class HichainConnectorTest : public testing::Test +{ +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} +} +#undef private +#undef protected +#endif // OHOS_HICHAIN_CONNECTOR_H diff --git a/test/unittest/UTTest_softbus_connector.cpp b/test/unittest/UTTest_softbus_connector.cpp new file mode 100644 index 00000000..df6a4f61 --- /dev/null +++ b/test/unittest/UTTest_softbus_connector.cpp @@ -0,0 +1,447 @@ +/* + * Copyright (c) 2021 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 "UTTest_softbus_connector.h" + +#include +#include + +#include +#include + +#include "dm_anonymous.h" +#include "dm_constants.h" +#include "dm_device_info.h" +#include "dm_log.h" +#include "parameter.h" +#include "softbus_connector.h" +#include "system_ability_definition.h" + +namespace OHOS { +namespace DistributedHardware { +void SoftbusConnectorTest::SetUp() +{ +} +void SoftbusConnectorTest::TearDown() +{ +} +void SoftbusConnectorTest::SetUpTestCase() +{ +} +void SoftbusConnectorTest::TearDownTestCase() +{ +} + +namespace { +static std::shared_ptr softbusConnector = std::make_shared(); +/** + * @tc.name: Init_001 + * @tc.desc: go tothe corrort case and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, Init_001, testing::ext::TestSize.Level0) +{ + int ret = softbusConnector->Init(); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: RegisterSoftbusDiscoveryCallback_001 + * @tc.desc: set pkgName = "com.ohos.helloworld";call RegisterSoftbusDiscoveryCallback function to corrort ,return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, RegisterSoftbusDiscoveryCallback_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + std::shared_ptr listener_ = std::make_shared(); + std::shared_ptr discoveryMgr_ = + std::make_shared(softbusConnector, listener_); + int ret = softbusConnector->RegisterSoftbusDiscoveryCallback( + pkgName, std::shared_ptr(discoveryMgr_)); + int ret1 = SoftbusConnector::discoveryCallbackMap_.count(pkgName); + EXPECT_EQ(ret1, 1); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: UnRegisterSoftbusDiscoveryCallback_001 + * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusDiscoveryCallback function to corrort ,return + * DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusDiscoveryCallback_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + int ret = softbusConnector->UnRegisterSoftbusDiscoveryCallback(pkgName); + int ret1 = SoftbusConnector::discoveryCallbackMap_.count(pkgName); + EXPECT_EQ(ret1, 0); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: UnRegisterSoftbusStateCallback_001 + * @tc.desc: set pkgName = "com.ohos.helloworld";call UnRegisterSoftbusStateCallback function to corrort ,return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, UnRegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.helloworld"; + int ret = softbusConnector->UnRegisterSoftbusStateCallback(pkgName); + int ret1 = SoftbusConnector::stateCallbackMap_.count(pkgName); + EXPECT_EQ(ret1, 0); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: GetTrustedDeviceList_001 + * @tc.desc: create GetAllNodeDeviceInfo not equal 0, and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetTrustedDeviceList_001, testing::ext::TestSize.Level0) +{ + std::vector deviceInfoList; + int ret = softbusConnector->GetTrustedDeviceList(deviceInfoList); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: GetLocalDeviceInfo_001 + * @tc.desc: set pkgName to com.softbus.test,define deviceInfo,and return DM_OK. + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetLocalDeviceInfo_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.softbus.test"; + DmDeviceInfo deviceInfo; + int ret = softbusConnector->GetLocalDeviceInfo(deviceInfo); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: StartDiscovery_001 + * @tc.desc: get StartDiscovery to wroing master and return DM_DISCOVERY_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, StartDiscovery_001, testing::ext::TestSize.Level0) +{ + DmSubscribeInfo dmSubscribeInfo; + int ret = softbusConnector->StartDiscovery(dmSubscribeInfo); + EXPECT_EQ(ret, DM_DISCOVERY_FAILED); +} + +/** + * @tc.name: IsDeviceOnLine_001 + * @tc.desc: set deviceId = "12333";,and return false + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, IsDeviceOnLine_001, testing::ext::TestSize.Level0) +{ + std::string deviceId = "12333"; + bool ret = softbusConnector->IsDeviceOnLine(deviceId); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: IsDeviceOnLine_002 + * @tc.desc: + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, IsDeviceOnLine_002, testing::ext::TestSize.Level0) +{ + std::string deviceId = "145677"; + NodeBasicInfo Info; + strncpy(Info.networkId, "145677", sizeof(Info.networkId)); + bool ret = softbusConnector->IsDeviceOnLine(deviceId); + EXPECT_EQ(ret, false); +} + +/** + * @tc.name: SoftbusConnector_001 + * @tc.desc: set SoftbusConnector to new a pointer ,and the pointer nou equal nullptr + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, SoftbusConnector_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr m_SoftbusConnector = std::make_shared(); + ASSERT_NE(m_SoftbusConnector, nullptr); +} + +/** + * @tc.name: SoftbusConnector_002 + * @tc.desc: set SoftbusConnector to new a pointer ,and the pointer nou equal nullptr,and delete it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, SoftbusConnector_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr m_SoftbusConnector = std::make_shared(); + m_SoftbusConnector.reset(); + EXPECT_EQ(m_SoftbusConnector, nullptr); +} + +/** + * @tc.name: GetConnectionIpAddress_001 + * @tc.desc: set deviceId null,adb return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_001, testing::ext::TestSize.Level0) +{ + std::string deviceId; + std::string ipAddress = "123456"; + int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: GetConnectionIpAddress_002 + * @tc.desc: set some corrort para,and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_002, testing::ext::TestSize.Level0) +{ + std::string deviceId = "213456"; + std::string ipAddress = "123456"; + int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress); + EXPECT_EQ(ret, DM_FAILED); +} +/** + * @tc.name: GetConnectionIpAddress_003 + * @tc.desc: set deviceInfo.addrNum = -1;go to second master and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetConnectionIpAddress_003, testing::ext::TestSize.Level0) +{ + DeviceInfo deviceInfo; + deviceInfo.addrNum = -1; + std::string ipAddress; + std::string deviceId = "3338848"; + int ret = softbusConnector->GetConnectionIpAddress(deviceId, ipAddress); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: GetConnectAddrByType_001 + * @tc.desc: set deviceInfo'pointer null, go to first master,and return nullptr + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_001, testing::ext::TestSize.Level0) +{ + ConnectionAddrType type; + type = CONNECTION_ADDR_MAX; + ConnectionAddr *p = nullptr; + ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(nullptr, type); + EXPECT_EQ(p, ret); +} + +/** + * @tc.name: GetConnectAddrByType_002 + * @tc.desc:set deviceInfo to some corrort para,and return nullptr + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetConnectAddrByType_002, testing::ext::TestSize.Level0) +{ + DeviceInfo deviceInfo; + deviceInfo.addrNum = 1; + ConnectionAddrType type; + type = CONNECTION_ADDR_BR; + ConnectionAddr *p = nullptr; + ConnectionAddr *ret = softbusConnector->GetConnectAddrByType(&deviceInfo, type); + EXPECT_EQ(ret, p); +} + +/** + * @tc.name: GetConnectAddr_001 + * @tc.desc: set deviceId to null,and return nullptr + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetConnectAddr_001, testing::ext::TestSize.Level0) +{ + std::string deviceId; + std::string connectAddr; + ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr); + EXPECT_EQ(ret, nullptr); +} + +/** + * @tc.name: GetConnectAddr_002 + * @tc.desc:set deviceId nit null set deviceInfo.addrNum = -1; and return nullptr + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, GetConnectAddr_002, testing::ext::TestSize.Level0) +{ + std::string deviceId = "123345"; + std::string connectAddr; + DeviceInfo deviceInfo; + deviceInfo.addrNum = -1; + ConnectionAddr *ret = softbusConnector->GetConnectAddr(deviceId, connectAddr); + EXPECT_EQ(ret, nullptr); +} + +/** + * @tc.name: CovertNodeBasicInfoToDmDevice_001 + * @tc.desc: go to the corrort case and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, CovertNodeBasicInfoToDmDevice_001, testing::ext::TestSize.Level0) +{ + NodeBasicInfo nodeBasicInfo; + DmDeviceInfo dmDeviceInfo; + int ret = softbusConnector->CovertNodeBasicInfoToDmDevice(nodeBasicInfo, dmDeviceInfo); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnPublishSuccess_001 + * @tc.desc: go to the corrort case and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnPublishSuccess_001, testing::ext::TestSize.Level0) +{ + int32_t publishId = 0; + int ret = softbusConnector->OnPublishSuccess(publishId); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnPublishSuccess_001 + * @tc.desc: go to the corrort case and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnPublishFail1, testing::ext::TestSize.Level0) +{ + int32_t publishId = 0; + PublishFailReason reason; + reason = PUBLISH_FAIL_REASON_NOT_SUPPORT_MEDIUM; + int ret = softbusConnector->OnPublishFail(publishId, reason); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnSoftBusDeviceOnline_001 + * @tc.desc: set info to some corrort parp. and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnSoftBusDeviceOnline_001, testing::ext::TestSize.Level0) +{ + NodeBasicInfo info; + int ret = softbusConnector->OnSoftBusDeviceOnline(&info); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnSoftbusDeviceOffline_001 + * @tc.desc: set info to some corrort parp. and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnSoftbusDeviceOffline_001, testing::ext::TestSize.Level0) +{ + NodeBasicInfo info; + int ret = softbusConnector->OnSoftbusDeviceOffline(&info); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnSoftbusDeviceInfoChanged_001 + * @tc.desc: go to the corrort case and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnSoftbusDeviceInfoChanged_001, testing::ext::TestSize.Level0) +{ + NodeBasicInfo info; + NodeBasicInfoType type; + type = TYPE_NETWORK_ID; + int ret = softbusConnector->OnSoftbusDeviceInfoChanged(type, &info); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnSoftbusDeviceFound_001 + * @tc.desc: set device some corrort para and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnSoftbusDeviceFound_001, testing::ext::TestSize.Level0) +{ + DeviceInfo device; + int ret = softbusConnector->OnSoftbusDeviceFound(&device); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnSoftbusDiscoveryFailed_001 + * @tc.desc: set some corrort para and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnSoftbusDiscoveryFailed_001, testing::ext::TestSize.Level0) +{ + int32_t subscribeId = 0; + DiscoveryFailReason failReason; + failReason = DISCOVERY_FAIL_REASON_NOT_SUPPORT_MEDIUM; + int ret = softbusConnector->OnSoftbusDiscoveryFailed(subscribeId, failReason); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnSoftbusDiscoverySuccess_001 + * @tc.desc: set some corrort para and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnSoftbusDiscoverySuccess_001, testing::ext::TestSize.Level0) +{ + int32_t subscribeId = 0; + int ret = softbusConnector->OnSoftbusDiscoverySuccess(subscribeId); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnParameterChgCallback_001 + * @tc.desc: set some corrort para and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusConnectorTest, OnParameterChgCallback_001, testing::ext::TestSize.Level0) +{ + char *key; + char *value; + void *context = nullptr; + int ret = softbusConnector->OnParameterChgCallback(key, value, context); + EXPECT_EQ(ret, DM_OK); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/UTTest_softbus_connector.h b/test/unittest/UTTest_softbus_connector.h new file mode 100644 index 00000000..03857486 --- /dev/null +++ b/test/unittest/UTTest_softbus_connector.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 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_UTTest_DM_SOFTBUS_CONNECTOR_H +#define OHOS_UTTest_DM_SOFTBUS_CONNECTOR_H + +#define private public // hack complier +#define protected public + +#include +#include + +#include +#include +#include +#include +#include + +#include "device_manager_service_listener.h" +#include "discovery_service.h" +#include "dm_device_info.h" +#include "dm_device_state_manager.h" +#include "dm_discovery_manager.h" +#include "dm_subscribe_info.h" +#include "softbus_bus_center.h" +#include "softbus_connector.h" +#include "softbus_discovery_callback.h" +#include "softbus_session.h" +#include "softbus_state_callback.h" +namespace OHOS { +namespace DistributedHardware { + +class SoftbusConnectorTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#undef private +#undef protected +#endif diff --git a/test/unittest/UTTest_softbus_session.cpp b/test/unittest/UTTest_softbus_session.cpp new file mode 100644 index 00000000..0be9cf6d --- /dev/null +++ b/test/unittest/UTTest_softbus_session.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2021 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 "UTTest_softbus_session.h" + +#include "dm_anonymous.h" +#include "dm_constants.h" +#include "dm_log.h" +#include "nlohmann/json.hpp" +#include "softbus_connector.h" +#include "softbus_session.h" + +namespace OHOS { +namespace DistributedHardware { +void SoftbusSessionTest::SetUp() +{ +} +void SoftbusSessionTest::TearDown() +{ +} +void SoftbusSessionTest::SetUpTestCase() +{ +} +void SoftbusSessionTest::TearDownTestCase() +{ +} + +namespace { + +std::shared_ptr softbusSession = std::make_shared(); + +/** + * @tc.name: OpenAuthSession_001 + * @tc.desc: set deviceId =null, return sessionId(1) + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, OpenAuthSession_001, testing::ext::TestSize.Level0) +{ + std::string deviceId = ""; + int ret = softbusSession->OpenAuthSession(deviceId); + EXPECT_EQ(ret, -1); +} + +/** + * @tc.name: OpenAuthSession_002 + * @tc.desc: set deviceId = "123456";and return sessionId + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, OpenAuthSession_002, testing::ext::TestSize.Level0) +{ + std::string deviceId = "123456"; + int ret = softbusSession->OpenAuthSession(deviceId); + EXPECT_EQ(ret, -1); +} + +/** + * @tc.name: SendData_001 + * @tc.desc: set message null and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, SendData_001, testing::ext::TestSize.Level0) +{ + std::string message = ""; + int32_t sessionId = -1; + int ret = softbusSession->SendData(sessionId, message); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: SendData_002 + * @tc.desc: set sessionId = 0 ,go to the SendBytes'smaster and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, SendData_002, testing::ext::TestSize.Level0) +{ + int32_t msgType = 2; + nlohmann::json jsonObj; + jsonObj[TAG_VER] = DM_ITF_VER; + jsonObj[TAG_TYPE] = msgType; + std::string message = jsonObj.dump(); + int32_t sessionId = 0; + int ret = softbusSession->SendData(sessionId, message); + EXPECT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: OnSessionOpened_001 + * @tc.desc: set result = 0,sessionId = -1;and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, OnSessionOpened_001, testing::ext::TestSize.Level0) +{ + int result = 0; + int32_t sessionId = -1; + int ret = softbusSession->OnSessionOpened(sessionId, result); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SoftbusSession_001 + * @tc.desc: set SoftbusSession to make a new pointer ,and it not nullptr + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, SoftbusSession_001, testing::ext::TestSize.Level0) +{ + std::shared_ptr m_SoftbusSession = std::make_shared(); + ASSERT_NE(m_SoftbusSession, nullptr); +} + +/** + * @tc.name: SoftbusSession_002 + * @tc.desc: set SoftbusSession to make a new pointer , it not nullptr and delete it + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, SoftbusSession_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr m_SoftbusSession = std::make_shared(); + m_SoftbusSession.reset(); + EXPECT_EQ(m_SoftbusSession, nullptr); +} + +/** + * @tc.name: OnSessionClosed_001 + * @tc.desc: set some corrort para,and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, OnSessionClosed_001, testing::ext::TestSize.Level0) +{ + int32_t sessionId = 3; + int ret = softbusSession->OnSessionClosed(sessionId); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: OnBytesReceived_002 + * @tc.desc: set sessionId = 3 and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, OnBytesReceived_002, testing::ext::TestSize.Level0) +{ + int32_t sessionId = 3; + void *data; + uint32_t dataLen = 1; + int ret = softbusSession->OnBytesReceived(sessionId, data, dataLen); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: CloseAuthSession_001 + * @tc.desc: set sessionId = 3, and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, CloseAuthSession_001, testing::ext::TestSize.Level0) +{ + int32_t sessionId = 3; + int ret = softbusSession->CloseAuthSession(sessionId); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: GetPeerDeviceId_001 + * @tc.desc: set sessionId = 3 and return DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, GetPeerDeviceId_001, testing::ext::TestSize.Level0) +{ + int32_t sessionId = 3; + std::string peerDevId; + int ret = softbusSession->GetPeerDeviceId(sessionId, peerDevId); + EXPECT_EQ(ret, DM_OK); +} + +/** + * @tc.name: UnRegisterSessionCallback_001 + * @tc.desc: set info to null and return DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(SoftbusSessionTest, UnRegisterSessionCallback_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = "softbus"; + int ret = softbusSession->UnRegisterSessionCallback(pkgName); + EXPECT_EQ(ret, DM_OK); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_softbus_session.h b/test/unittest/UTTest_softbus_session.h new file mode 100644 index 00000000..41998d6f --- /dev/null +++ b/test/unittest/UTTest_softbus_session.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2021 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_UTTest_DM_SOFTBUS_SESSION_H +#define OHOS_UTTest_DM_SOFTBUS_SESSION_H + +#include +#include + +#include +#include +#include +#include +#include + +#include "device_manager_service_listener.h" +#include "dm_device_state_manager.h" +#include "dm_discovery_manager.h" +#include "inner_session.h" +#include "session.h" +#include "softbus_session.h" +#include "softbus_session_callback.h" + +namespace OHOS { +namespace DistributedHardware { + +class SoftbusSessionTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS +#undef private +#undef protected +#endif \ No newline at end of file diff --git a/test/unittest/auth_request_state.cpp b/test/unittest/auth_request_state.cpp new file mode 100644 index 00000000..bab92c0b --- /dev/null +++ b/test/unittest/auth_request_state.cpp @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2021 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 "auth_request_state.h" + +#include "dm_auth_manager.h" +#include "dm_constants.h" + +namespace OHOS { +namespace DistributedHardware { + +int32_t AuthRequestState::Leave() +{ + return DM_OK; +} + +int32_t AuthRequestState::SetAuthManager(std::shared_ptr authManager) +{ + authManager_ = std::move(authManager); + return DM_OK; +} + +int32_t AuthRequestState::SetAuthContext(std::shared_ptr context) +{ + context_ = std::move(context); + return DM_OK; +} + +std::shared_ptr AuthRequestState::GetAuthContext() +{ + return context_; +} + +int32_t AuthRequestState::TransitionTo(std::shared_ptr state) +{ + LOGE("AuthRequestState::TransitionTo"); + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + state->SetAuthManager(stateAuthManager); + stateAuthManager->SetAuthRequestState(state); + state->SetAuthContext(context_); + this->Leave(); + state->Enter(); + return DM_OK; +} + +int32_t AuthRequestInitState::GetStateType() +{ + return AuthState::AUTH_REQUEST_INIT; +} + +int32_t AuthRequestInitState::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->EstablishAuthChannel(context_->deviceId); + return DM_OK; +} + +int32_t AuthRequestNegotiateState::GetStateType() +{ + return AuthState::AUTH_REQUEST_NEGOTIATE; +} + +int32_t AuthRequestNegotiateState::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->StartNegotiate(context_->sessionId); + return DM_OK; +} + +int32_t AuthRequestNegotiateDoneState::GetStateType() +{ + return AuthState::AUTH_REQUEST_NEGOTIATE_DONE; +} + +int32_t AuthRequestNegotiateDoneState::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->SendAuthRequest(context_->sessionId); + return DM_OK; +} + +int32_t AuthRequestReplyState::GetStateType() +{ + return AuthState::AUTH_REQUEST_REPLY; +} + +int32_t AuthRequestReplyState::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->StartRespAuthProcess(); + return DM_OK; +} + +int32_t AuthRequestInputState::GetStateType() +{ + return AuthState::AUTH_REQUEST_INPUT; +} + +int32_t AuthRequestInputState::Enter() +{ + LOGE("DmAuthManager::AuthRequestInputState"); + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->ShowStartAuthDialog(); + return DM_OK; +} + +int32_t AuthRequestJoinState::GetStateType() +{ + return AuthState::AUTH_REQUEST_JOIN; +} + +int32_t AuthRequestJoinState::Enter() +{ + LOGE("DmAuthManager::AuthRequestJoinState"); + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->AddMember(context_->deviceId); + return DM_OK; +} + +int32_t AuthRequestNetworkState::GetStateType() +{ + return AuthState::AUTH_REQUEST_NETWORK; +} + +int32_t AuthRequestNetworkState::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->JoinNetwork(); + return DM_OK; +} + +int32_t AuthRequestFinishState::GetStateType() +{ + return AuthState::AUTH_REQUEST_FINISH; +} + +int32_t AuthRequestFinishState::Enter() +{ + std::shared_ptr stateAuthManager = authManager_.lock(); + if (stateAuthManager == nullptr) { + LOGE("AuthRequestState::authManager_ null"); + return DM_FAILED; + } + stateAuthManager->AuthenticateFinish(); + return DM_OK; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/device_manager_impl_test.cpp b/test/unittest/device_manager_impl_test.cpp new file mode 100644 index 00000000..e44c6bb6 --- /dev/null +++ b/test/unittest/device_manager_impl_test.cpp @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2021 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 "device_manager_impl_test.h" + +#include + +#include "dm_constants.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +void DeviceManagerImplTest::SetUp() +{ +} + +void DeviceManagerImplTest::TearDown() +{ +} + +void DeviceManagerImplTest::SetUpTestCase() +{ +} + +void DeviceManagerImplTest::TearDownTestCase() +{ +} + +namespace { +HWTEST_F(DeviceManagerImplTest, InitDeviceManager, testing::ext::TestSize.Level0) +{ + std::string packName = ""; + int32_t ret = DeviceManager::GetInstance().InitDeviceManager(packName, nullptr); + ASSERT_EQ(ret, DM_INVALID_VALUE); +} + +HWTEST_F(DeviceManagerImplTest, AuthenticateDevice1, testing::ext::TestSize.Level0) +{ + std::string packName = ""; + DmDeviceInfo dmDeviceInfo; + DmAppImageInfo dmAppImageInfo; + std::string extra = ""; + std::shared_ptr callback = nullptr; + int32_t ret = + DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, dmAppImageInfo, extra, callback); + ASSERT_EQ(ret, DM_INVALID_VALUE); +} + +HWTEST_F(DeviceManagerImplTest, AuthenticateDevice2, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.helloworld"; + DmDeviceInfo dmDeviceInfo; + DmAppImageInfo dmAppImageInfo; + std::string extra = ""; + std::shared_ptr callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(DM_FAILED)); + int32_t ret = + DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, dmAppImageInfo, extra, callback); + ASSERT_EQ(ret, DM_IPC_FAILED); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr; +} + +HWTEST_F(DeviceManagerImplTest, AuthenticateDevice3, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.helloworld"; + DmDeviceInfo dmDeviceInfo; + DmAppImageInfo dmAppImageInfo; + std::string extra = ""; + std::shared_ptr callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(DM_OK)); + int32_t ret = + DeviceManager::GetInstance().AuthenticateDevice(packName, dmDeviceInfo, dmAppImageInfo, extra, callback); + ASSERT_EQ(ret, DM_OK); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr; +} + +HWTEST_F(DeviceManagerImplTest, CheckAuthentication1, testing::ext::TestSize.Level0) +{ + std::string packName = ""; + std::string authPara = ""; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().CheckAuthentication(packName, authPara, callback); + ASSERT_EQ(ret, DM_INVALID_VALUE); +} + +HWTEST_F(DeviceManagerImplTest, CheckAuthentication2, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.helloworld"; + std::string authPara = ""; + std::shared_ptr callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(DM_FAILED)); + int32_t ret = DeviceManager::GetInstance().CheckAuthentication(packName, authPara, callback); + ASSERT_EQ(ret, DM_IPC_FAILED); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr; +} + +HWTEST_F(DeviceManagerImplTest, CheckAuthentication3, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.helloworld"; + std::string authPara = ""; + std::shared_ptr callback = nullptr; + std::shared_ptr mockInstance = std::make_shared(); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(DM_OK)); + int32_t ret = DeviceManager::GetInstance().CheckAuthentication(packName, authPara, callback); + ASSERT_EQ(ret, DM_OK); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr; +} + +HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery1, testing::ext::TestSize.Level0) +{ + std::string packName = ""; + DmSubscribeInfo subscribeInfo; + std::shared_ptr callback = nullptr; + int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, callback); + ASSERT_EQ(ret, DM_INVALID_VALUE); +} + +HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery2, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.helloworld"; + DmSubscribeInfo subscribeInfo; + test_callback_ = std::make_shared(); + std::shared_ptr mockInstance = std::make_shared(); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(DM_OK)); + int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, test_callback_); + ASSERT_EQ(ret, DM_OK); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr; +} + +HWTEST_F(DeviceManagerImplTest, StartDeviceDiscovery3, testing::ext::TestSize.Level0) +{ + std::string packName = "com.ohos.helloworld"; + DmSubscribeInfo subscribeInfo; + test_callback_ = std::make_shared(); + std::shared_ptr mockInstance = std::make_shared(); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1) + .WillOnce(testing::Return(DM_FAILED)); + int32_t ret = DeviceManager::GetInstance().StartDeviceDiscovery(packName, subscribeInfo, test_callback_); + ASSERT_EQ(ret, DM_IPC_FAILED); + DeviceManagerImpl::GetInstance().ipcClientProxy_ = nullptr; +} +} // namespace + +void DeviceDiscoveryCallback::OnDiscoverySuccess(uint16_t subscribeId) +{ + (void)subscribeId; +} + +void DeviceDiscoveryCallback::OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) +{ + (void)subscribeId; + (void)failedReason; +} + +void DeviceDiscoveryCallback::OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) +{ + (void)subscribeId; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/device_manager_impl_test.h b/test/unittest/device_manager_impl_test.h new file mode 100644 index 00000000..ffcb70e3 --- /dev/null +++ b/test/unittest/device_manager_impl_test.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2021 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_DM_IMPL_TEST_H +#define OHOS_DM_IMPL_TEST_H + +#include +#include + +#include "device_manager.h" +#include "device_manager_callback.h" +#include "device_manager_impl.h" +#include "mock/mock_ipc_client_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class DeviceManagerImplTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; + +private: + std::shared_ptr test_callback_ = nullptr; +}; + +class DeviceDiscoveryCallback : public DiscoveryCallback { +public: + DeviceDiscoveryCallback() : DiscoveryCallback() + { + } + virtual ~DeviceDiscoveryCallback() override + { + } + virtual void OnDiscoverySuccess(uint16_t subscribeId) override; + virtual void OnDiscoveryFailed(uint16_t subscribeId, int32_t failedReason) override; + virtual void OnDeviceFound(uint16_t subscribeId, const DmDeviceInfo &deviceInfo) override; +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_DM_IMPL_TEST_H diff --git a/test/unittest/mock/ipc_server_listener.cpp b/test/unittest/mock/ipc_server_listener.cpp new file mode 100644 index 00000000..4feadd49 --- /dev/null +++ b/test/unittest/mock/ipc_server_listener.cpp @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2021 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 "ipc_server_listener.h" + +namespace OHOS { +namespace DistributedHardware { +int32_t IpcServerListener::SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + req_ = req; + return 0; +} + +int32_t IpcServerListener::SendAll(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp) +{ + req_ = req; + return 0; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/ipc_server_listener.h b/test/unittest/mock/ipc_server_listener.h new file mode 100644 index 00000000..e0f072ac --- /dev/null +++ b/test/unittest/mock/ipc_server_listener.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2021 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_DM_IPC_SERVER_LISTENER_H +#define OHOS_DM_IPC_SERVER_LISTENER_H + +#include +#include + +#include "ipc_req.h" +#include "ipc_rsp.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServerListener { +public: + IpcServerListener() = default; + virtual ~IpcServerListener() = default; + +public: + int32_t SendRequest(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); + int32_t SendAll(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp); + std::shared_ptr req_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_IPC_SERVER_LISTENER_H \ No newline at end of file diff --git a/test/unittest/mock/mock_ipc_client_proxy.h b/test/unittest/mock/mock_ipc_client_proxy.h new file mode 100644 index 00000000..ece83e9f --- /dev/null +++ b/test/unittest/mock/mock_ipc_client_proxy.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2021 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_MOCK_IPC_CLIENT_PROXY_H +#define OHOS_MOCK_IPC_CLIENT_PROXY_H + +#include +#include + +#include "ipc_client_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class MockIpcClientProxy : public IpcClientProxy, public RefBase { +public: + MOCK_METHOD3(SendRequest, int32_t(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp)); +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_MOCK_IPC_CLIENT_PROXY_H