diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index fd0d387c720c81215493e802c43a7c7fea2765dc..fd9346612d8d79207e97ee114d65e5bd6556529a 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -31,6 +31,12 @@ group("unittest") { ":UTTest_hichain_connector", ":UTTest_softbus_connector", ":UTTest_softbus_session", + ":UTTest_ipc_client_manager", + ":UTTest_ipc_client_proxy", + ":UTTest_ipc_client_stub", + ":UTTest_ipc_server_client_proxy", + ":UTTest_ipc_server_listener", + ":UTTest_ipc_server_stub", ] } @@ -100,6 +106,78 @@ ohos_unittest("UTTest_softbus_session") { ## UnitTest UTTest_softbus_session }}} +## UnitTest UTTest_ipc_client_manager {{{ +ohos_unittest("UTTest_ipc_client_manager") { + module_out_path = module_out_path + + sources = [ "UTTest_ipc_client_manager.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest UTTest_ipc_client_manager }}} + +## UnitTest UTTest_ipc_client_manager }}} + +## UnitTest UTTest_ipc_client_proxy {{{ +ohos_unittest("UTTest_ipc_client_proxy") { + module_out_path = module_out_path + + sources = [ "UTTest_ipc_client_proxy.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest UTTest_ipc_client_proxy }}} + +## UnitTest UTTest_ipc_client_stub {{{ +ohos_unittest("UTTest_ipc_client_stub") { + module_out_path = module_out_path + + sources = [ "UTTest_ipc_client_stub.cpp" ] + + deps = [ ":device_manager_test_common" ] + +} + +## UnitTest UTTest_ipc_client_stub }}} + +## UnitTest UTTest_ipc_server_client_proxy {{{ +ohos_unittest("UTTest_ipc_server_client_proxy") { + module_out_path = module_out_path + + sources = [ "UTTest_ipc_server_client_proxy.cpp" ] + + deps = [ ":device_manager_test_common" ] + +} + +## UnitTest UTTest_ipc_server_client_proxy }}} + +## UnitTest UTTest_ipc_server_listener {{{ +ohos_unittest("UTTest_ipc_server_listener") { + module_out_path = module_out_path + + sources = [ "UTTest_ipc_server_listener.cpp" ] + + deps = [ ":device_manager_test_common" ] + +} + +## UnitTest UTTest_ipc_server_listener }}} + +## UnitTest UTTest_ipc_server_stub {{{ +ohos_unittest("UTTest_ipc_server_stub") { + module_out_path = module_out_path + + sources = [ "UTTest_ipc_server_stub.cpp" ] + + deps = [ ":device_manager_test_common" ] + +} + +## UnitTest UTTest_ipc_server_stub }}} + ## UnitTest UTTest_dm_device_state_manager {{{ ohos_unittest("UTTest_dm_device_state_manager") { module_out_path = module_out_path @@ -171,6 +249,17 @@ ohos_unittest("UTTest_auth_request_state") { ## UTTest_auth_request_state }}} +## UnitTest ipc_client_manager_test {{{ +ohos_unittest("ipc_client_manager_test") { + module_out_path = module_out_path + + sources = [ "ipc_client_manager_test.cpp" ] + + deps = [ ":device_manager_test_common" ] +} + +## UnitTest ipc_client_manager_test }}} + ## UnitTest UTTest_dm_auth_manager {{{ ohos_unittest("UTTest_dm_auth_manager") { module_out_path = module_out_path @@ -238,6 +327,10 @@ config("device_manager_test_common_public_config") { "//base/security/deviceauth/interfaces/innerkits", "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/ability", "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/config", + "//foundation/distributedhardware/devicemanager/services/devicemanagerservice/include/ipc/standard", + "//foundation/communication/dsoftbus/interfaces/kits/transport", + "//foundation/distributedhardware/devicemanager/interfaces/inner_kits/native_cpp/include/ipc/standard", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", ] cflags = [ @@ -363,7 +456,6 @@ ohos_static_library("device_manager_test") { "ces_standard:cesfwk_core", "ces_standard:cesfwk_innerkits", "dsoftbus_standard:softbus_client", - "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", diff --git a/test/unittest/UTTest_dm_device_state_manager.cpp b/test/unittest/UTTest_dm_device_state_manager.cpp index 83b6eb9c05613561ebf6eb7ec1f3bff9859780b1..507cdca5d066332d489fb0997d800cd881ce6b0e 100644 --- a/test/unittest/UTTest_dm_device_state_manager.cpp +++ b/test/unittest/UTTest_dm_device_state_manager.cpp @@ -23,6 +23,7 @@ #include "dm_device_state_manager.h" #include "ipc_notify_device_found_req.h" #include "ipc_notify_discover_result_req.h" +#include "hichain_connector.h" #include "UTTest_dm_device_state_manager.h" namespace OHOS { @@ -43,10 +44,11 @@ void DmDeviceStateManagerTest::TearDownTestCase() { } namespace { + std::shared_ptr hiChainConnector_ = std::make_shared(); std::shared_ptr softbusConnector = std::make_shared(); std::shared_ptr listener_ = std::make_shared(); std::shared_ptr dmDeviceStateManager = - std::make_shared(softbusConnector, listener_); + std::make_shared(softbusConnector, listener_, hiChainConnector_); /** * @tc.name: DmDeviceStateManager_001 @@ -56,7 +58,8 @@ std::shared_ptr dmDeviceStateManager = */ HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_001, testing::ext::TestSize.Level0) { - std::shared_ptr p = std::make_shared(softbusConnector, listener_); + std::shared_ptr p = std::make_shared(softbusConnector, listener_, + hiChainConnector_); ASSERT_NE(p, nullptr); } @@ -68,7 +71,8 @@ HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_001, testing::ext::TestS */ HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_002, testing::ext::TestSize.Level0) { - std::shared_ptr p = std::make_shared(softbusConnector, listener_); + std::shared_ptr p = std::make_shared(softbusConnector, listener_, + hiChainConnector_); p.reset(); EXPECT_EQ(p, nullptr); } diff --git a/test/unittest/UTTest_ipc_client_manager.cpp b/test/unittest/UTTest_ipc_client_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..17eeb2b03b1f16bb5ff7a8494b210e2a39516083 --- /dev/null +++ b/test/unittest/UTTest_ipc_client_manager.cpp @@ -0,0 +1,583 @@ +/* + * 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_ipc_client_manager.h" +#include "device_manager_notify.h" +#include "dm_device_info.h" +#include "ipc_client_stub.h" +#include "ipc_register_listener_req.h" +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "dm_constants.h" +#include "system_ability_definition.h" + +#include + +namespace OHOS { +namespace DistributedHardware { +void IpcClientManagerTest::SetUp() +{ +} + +void IpcClientManagerTest::TearDown() +{ +} + +void IpcClientManagerTest::SetUpTestCase() +{ +} + +void IpcClientManagerTest::TearDownTestCase() +{ +} + +namespace { +/** + * @tc.name: ClientInit_001 + * @tc.desc: 1. new a dmInterface + * 2. set IpcClientManager dmInterface_ not null + * 3. call ClientInit + * 4. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, ClientInit_001, testing::ext::TestSize.Level0) +{ + // 1. new a dmInterface + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); + sptr dmInterface = iface_cast(object); + // 2. set IpcClientManager dmInterface_ not null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = dmInterface; + // 3. call ClientInit + int ret = instance->ClientInit(); + // 4. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: ClientInit_002 + * @tc.desc: 1. new a dmInterface + * 2. set IpcClientManager dmInterface_ not null + * 3. call ClientInit + * 4. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, ClientInit_002, testing::ext::TestSize.Level0) +{ + std::shared_ptr instance = std::make_shared(); + // 3. call ClientInit + int ret = instance->ClientInit(); + // 4. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: Init1 + * @tc.desc: 1. new a listener + * 2. set a pkgName not null + * 3. add listener and pkgName in dmListener_ Map + * 4. call Init with pkgName + * 5. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, Init1, testing::ext::TestSize.Level0) +{ + // 1. new a listener + sptr listener = sptr(new IpcClientStub()); + // 2. set a pkgName not null + std::string pkgName = "com.ohos.test"; + std::shared_ptr instance = std::make_shared(); + // 3. add listener and pkgName in dmListener_ Map + instance->dmListener_[pkgName] = listener; + // 4. call Init with pkgName + int32_t ret = instance->Init(pkgName); + // 5. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: Init_002 + * @tc.desc: 1. set pkcName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_FAILED + * 3. call Init with pkgName + * 4. check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, Init_002, testing::ext::TestSize.Level0) +{ + // 1. set pkcName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DM_FAILED + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_FAILED)); + // 3. call Init with pkgName + int32_t ret = instance->Init(pkgName); + // 4. check ret is DM_FAILED + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: Init_003 + * @tc.desc: 1. set pkcName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_OK + * 3. call Init with pkgName + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, Init_003, testing::ext::TestSize.Level0) +{ + // 1. set pkcName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DM_OK + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_OK)); + // 3. call Init with pkgName + int32_t ret = instance->Init(pkgName); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: Init_004 + * @tc.desc: 1. set pkcName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_OK + * 3. call Init with pkgName + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, Init_004, testing::ext::TestSize.Level0) +{ + // 1. set pkcName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DEVICEMANAGER_SERVICE_NOT_READY + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_SERVICE_NOT_READY)); + // 3. call Init with pkgName + int32_t ret = instance->Init(pkgName); + // 4. check ret is DEVICEMANAGER_OK + ASSERT_EQ(ret, ret); +} + +/** + * @tc.name: Init_005 + * @tc.desc: 1. set pkcName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_IPC_FAILED + * 3. call Init with pkgName + * 4. check ret is DM_IPC_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, Init_005, testing::ext::TestSize.Level0) +{ + // 1. set pkcName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DM_OK + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_IPC_FAILED)); + // 3. call Init with pkgName + int32_t ret = instance->Init(pkgName); + // 4. check ret is DM_IPC_FAILED + ASSERT_EQ(ret, DM_IPC_FAILED); +} + +/** + * @tc.name: UnInit_001 + * @tc.desc: 1. set pkgName null + * set IpcClientManager dmInterface_ null + * 2. call UnInit with pkgName + * 3. check ret is DEVICEMANAGER_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, UnInit1, testing::ext::TestSize.Level0) +{ + // 1. set pkgName null + std::string pkgName = ""; + // set IpcClientManager dmInterface_ null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = nullptr; + // 2. call UnInit with pkgName + int32_t ret = instance->UnInit(pkgName); + // 3. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: UnInit_002 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_FAILED + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call UnInit with pkgName + * 6. check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, UnInit_002, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DM_FAILED + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_FAILED)); + // 3. set IpcClientManager dmInterface_ not null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + // 4. set IpcClientManager dmListener_ not null + sptr listener = sptr(new IpcClientStub()); + instance->dmListener_[pkgName] = listener; + // 5. call UnInit with pkgName + int32_t ret = instance->UnInit(pkgName); + // 6. check ret is DM_FAILED + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: UnInit_003 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_OK + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call UnInit with pkgName + * 6. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, UnInit_003, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DM_OK + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_OK)); + // 3. set IpcClientManager dmInterface_ not null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + // 4. set IpcClientManager dmListener_ not null + sptr listener = sptr(new IpcClientStub()); + instance->dmListener_[pkgName] = listener; + // 5. call UnInit with pkgName + int32_t ret = instance->UnInit(pkgName); + // 6. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: UnInit_004 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_SERVICE_NOT_READY + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call UnInit with pkgName + * 6. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, UnInit_004, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DM_SERVICE_NOT_READY + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_SERVICE_NOT_READY)); + // 3. set IpcClientManager dmInterface_ not null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + // 4. set IpcClientManager dmListener_ not null + sptr listener = sptr(new IpcClientStub()); + instance->dmListener_[pkgName] = listener; + // 5. call UnInit with pkgName + int32_t ret = instance->UnInit(pkgName); + // 6. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: UnInit_005 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_IPC_FAILED + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call UnInit with pkgName + * 6. check ret is DM_IPC_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, UnInit_005, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClientServerProxy SendCmd return DM_IPC_FAILED + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_IPC_FAILED)); + // 3. set IpcClientManager dmInterface_ not null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = mockInstance; + // 4. set IpcClientManager dmListener_ not null + sptr listener = sptr(new IpcClientStub()); + instance->dmListener_[pkgName] = listener; + // 5. call UnInit with pkgName + int32_t ret = instance->UnInit(pkgName); + // 6. check ret is DM_IPC_FAILED + ASSERT_EQ(ret, DM_IPC_FAILED); +} + +/** + * @tc.name: SendRequest_001 + * @tc.desc: 1. set pkgName null + * 2. set IpcClientManager dmInterface_null + * 3. call SendRequest with parameter + * 4. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, SendRequest_001, testing::ext::TestSize.Level0) +{ + // 1. set pkgName null + std::string pkgName = ""; + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 2. set IpcClientManager dmInterface_null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = nullptr; + // 3. call SendRequest with parameter + int ret = instance->SendRequest(0, req, rsp); + // 4. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: SendRequest_002 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_FAILED + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call SendRequest with parameter + * 6. check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, SendRequest_002, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 2. Mock IpcClientServerProxy SendCmd return DM_FAILED + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_FAILED)); + std::shared_ptr instance = std::make_shared(); + // 3. set IpcClientManager dmInterface_ not null + instance->dmInterface_ = mockInstance; + sptr listener = sptr(new IpcClientStub()); + // 4. set IpcClientManager dmListener_ not null + instance->dmListener_[pkgName] = listener; + // 5. call SendRequest with parameter + int ret = instance->SendRequest(0, req, rsp); + // 6. check ret is DM_FAILED + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: SendRequest_003 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_OK + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call SendRequest with parameter + * 6. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, SendRequest_003, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 2. Mock IpcClientServerProxy SendCmd return DM_OK + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_OK)); + std::shared_ptr instance = std::make_shared(); + // 3. set IpcClientManager dmInterface_ not null + instance->dmInterface_ = mockInstance; + sptr listener = sptr(new IpcClientStub()); + // 4. set IpcClientManager dmListener_ not null + instance->dmListener_[pkgName] = listener; + // 5. call SendRequest with parameter + int ret = instance->SendRequest(0, req, rsp); + // 6. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendRequest_004 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_SERVICE_NOT_READY + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call SendRequest with parameter + * 6. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, SendRequest_004, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 2. Mock IpcClientServerProxy SendCmd return DM_SERVICE_NOT_READY + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_SERVICE_NOT_READY)); + std::shared_ptr instance = std::make_shared(); + // 3. set IpcClientManager dmInterface_ not null + instance->dmInterface_ = mockInstance; + sptr listener = sptr(new IpcClientStub()); + // 4. set IpcClientManager dmListener_ not null + instance->dmListener_[pkgName] = listener; + // 5. call SendRequest with parameter + int ret = instance->SendRequest(0, req, rsp); + // 6. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: SendRequest_005 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClientServerProxy SendCmd return DM_IPC_FAILED + * 3. set IpcClientManager dmInterface_ not null + * 4. set IpcClientManager dmListener_ not null + * 5. call SendRequest with parameter + * 6. check ret is DM_IPC_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, SendRequest_005, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 2. Mock IpcClientServerProxy SendCmd return DM_IPC_FAILED + sptr remoteObject = nullptr; + auto mockInstance = new MockIpcClientManager(remoteObject); + EXPECT_CALL(*mockInstance, SendCmd(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_IPC_FAILED)); + std::shared_ptr instance = std::make_shared(); + // 3. set IpcClientManager dmInterface_ not null + instance->dmInterface_ = mockInstance; + sptr listener = sptr(new IpcClientStub()); + // 4. set IpcClientManager dmListener_ not null + instance->dmListener_[pkgName] = listener; + // 5. call SendRequest with parameter + int ret = instance->SendRequest(0, req, rsp); + // 6. check ret is DM_IPC_FAILED + ASSERT_EQ(ret, DM_IPC_FAILED); +} + +/** + * @tc.name: IsInit_001 + * @tc.desc: 1. set pkgName null + * 2. set IpcClientManager dmInterface_null + * 3. call IsInit with parameter + * 4. check ret is false + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, IsInit_001, testing::ext::TestSize.Level0) +{ + // 1. set pkgName null + std::string pkgName = ""; + // 2. set IpcClientManager dmInterface_null + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = nullptr; + // 3. call SendRequest with parameter + bool ret = instance->IsInit(pkgName); + // 4. check ret is false + ASSERT_EQ(ret, false); +} + +/** + * @tc.name: IsInit_002 + * @tc.desc: 1. set pkgName not null + * 2. set IpcClientManager dmInterface_ not null + * 3. call IsInit with parameter + * 4. check ret is false + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientManagerTest, IsInit_002, testing::ext::TestSize.Level0) +{ + // 1. set pkgName null + std::string pkgName = ""; + // 2. set IpcClientManager dmInterface_ not null + auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); + sptr dmInterface = iface_cast(object); + std::shared_ptr instance = std::make_shared(); + instance->dmInterface_ = dmInterface; + // 3. call IsInit with parameter + bool ret = instance->IsInit(pkgName); + // 4. check ret is false + ASSERT_EQ(ret, false); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_ipc_client_manager.h b/test/unittest/UTTest_ipc_client_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..bd25fb4de836f01f7eef14a6d480fbbdfcff66a2 --- /dev/null +++ b/test/unittest/UTTest_ipc_client_manager.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_IPC_CLIENT_MANAGER_TEST_H +#define OHOS_IPC_CLIENT_MANAGER_TEST_H + +#include +#include + +#include "mock/mock_ipc_client_manager.h" +#include "ipc_client_manager.h" +namespace OHOS { +namespace DistributedHardware { +class IpcClientManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_IPC_CLIENT_MANAGER_TEST_H diff --git a/test/unittest/UTTest_ipc_client_proxy.cpp b/test/unittest/UTTest_ipc_client_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..336800b964157d84cc07840048686ae8d61126a5 --- /dev/null +++ b/test/unittest/UTTest_ipc_client_proxy.cpp @@ -0,0 +1,414 @@ +/* + * 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_ipc_client_proxy.h" +#include "dm_device_info.h" +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "ipc_client_manager.h" +#include "dm_constants.h" + +#include + +namespace OHOS { +namespace DistributedHardware { +void IpcClientProxyTest::SetUp() +{ +} + +void IpcClientProxyTest::TearDown() +{ +} + +void IpcClientProxyTest::SetUpTestCase() +{ +} + +void IpcClientProxyTest::TearDownTestCase() +{ +} + +namespace { +/** + * @tc.name: Init_001 + * @tc.desc: 1. set pkgName not null + * 2. set IpcClientProxy ipcClientManager nullptr + * 3. call IpcClientProxy Init + * 4. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, Init_001, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set IpcClientProxy ipcClientManager nullptr + std::shared_ptr ipcClientManager = nullptr; + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + // 3. call IpcClientProxy + int32_t ret = ipcClientProxy->Init(pkgName); + // 4. check ret is DM_POINT_NULL + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: Init_002 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient Init return DM_FAILED + * 3. call IpcClientProxy Init + * 4. check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, Init_002, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_FAILED + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_FAILED)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->Init(pkgName); + // 4. check ret is DM_FAILED + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: Init_003 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient Init return DM_OK + * 3. call IpcClientProxy Init + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, Init_003, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_OK + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_OK)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->Init(pkgName); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: Init_004 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient Init return DM_SERVICE_NOT_READY + * 3. call IpcClientProxy Init + * 4. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, Init_004, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_SERVICE_NOT_READY + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_SERVICE_NOT_READY)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->Init(pkgName); + // 4. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: Init_005 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient Init return DM_IPC_FAILED + * 3. call IpcClientProxy Init + * 4. check ret is DM_IPC_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, Init_005, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_IPC_FAILED + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, Init(testing::_)).Times(1).WillOnce(testing::Return(DM_IPC_FAILED)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->Init(pkgName); + // 4. check ret is DM_IPC_FAILED + ASSERT_EQ(ret, DM_IPC_FAILED); +} + +/** + * @tc.name: UnInit_001 + * @tc.desc: 1. set pkgName not null + * 2. set IpcClientProxy ipcClientManager nullptr + * 3. call IpcClientProxy UnInit + * 4. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, UnInit_001, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set IpcClientProxy ipcClientManager nullptr + std::shared_ptr ipcClientManager = nullptr; + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + // 3. call IpcClientProxy + int32_t ret = ipcClientProxy->UnInit(pkgName); + // 4. check ret is DM_POINT_NULL + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: UnInit_002 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient Init return DM_FAILED + * 3. call IpcClientProxy UnInit + * 4. check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, UnInit_002, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_FAILED + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_FAILED)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->UnInit(pkgName); + // 4. check ret is DM_FAILED + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: UnInit_003 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient UnInit return DM_OK + * 3. call IpcClientProxy UnInit + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, UnInit_003, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_OK + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_OK)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->UnInit(pkgName); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: UnInit_004 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient UnInit return DM_SERVICE_NOT_READY + * 3. call IpcClientProxy UnInit + * 4. check ret is DM_SERVICE_NOT_READY + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, UnInit_004, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_SERVICE_NOT_READY + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_SERVICE_NOT_READY)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->UnInit(pkgName); + // 4. check ret is DM_SERVICE_NOT_READY + ASSERT_EQ(ret, DM_SERVICE_NOT_READY); +} + +/** + * @tc.name: UnInit_005 + * @tc.desc: 1. set pkgName not null + * 2. Mock IpcClient UnInit return DM_IPC_FAILED + * 3. call IpcClientProxy UnInit + * 4. check ret is DM_IPC_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, UnInit_005, testing::ext::TestSize.Level0) +{ + // 1. set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. Mock IpcClient Init return DM_IPC_FAILED + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, UnInit(testing::_)).Times(1).WillOnce(testing::Return(DM_IPC_FAILED)); + // 3. call IpcClientProxy Init + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->UnInit(pkgName); + // 4. check ret is DM_IPC_FAILED + ASSERT_EQ(ret, DM_IPC_FAILED); +} + +/** + * @tc.name: SendRequest_001 + * @tc.desc: 1. set req nullptr + * set rsp not nullptr + * set IpcClientProxy ipcClientManager not null + * 2. call IpcClientProxy SendRequest + * 3. check ret is DEVICEMANAGER_NULLPTR + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, SendRequest_001, testing::ext::TestSize.Level0) +{ + // 1. set req nullptr + std::shared_ptr req = nullptr; + // set rsp not nullptr + std::shared_ptr rsp = std::make_shared(); + // set pcClientProxy ipcClientManager not null + std::shared_ptr ipcClientManager = std::make_shared(); + // 2. call IpcClientProxy SendRequest + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->SendRequest(0, req, rsp); + // 3. check ret is DEVICEMANAGER_NULLPTR + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: SendRequest_002 + * @tc.desc: 1. set req not nullptr + * set rsp nullptr + * set IpcClientProxy ipcClientManager not null + * 2. call IpcClientProxy SendRequest + * 3. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, SendRequest_002, testing::ext::TestSize.Level0) +{ + // 1. set req not nullptr + std::shared_ptr req = std::make_shared(); + // set rsp nullptr + std::shared_ptr rsp = nullptr; + // set pcClientProxy ipcClientManager not null + std::shared_ptr ipcClientManager = std::make_shared(); + // 2. call IpcClientProxy SendRequest + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->SendRequest(0, req, rsp); + // 3. check ret is DM_POINT_NULL + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: SendRequest_003 + * @tc.desc: 1. set req not nullptr + * set rsp not nullptr + * set IpcClientProxy ipcClientManager null + * 2. call IpcClientProxy SendRequest + * 3. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, SendRequest_003, testing::ext::TestSize.Level0) +{ + // 1. set req not nullptr + std::shared_ptr req = std::make_shared(); + // set rsp not nullptr + std::shared_ptr rsp = std::make_shared(); + // set pcClientProxy ipcClientManager null + std::shared_ptr ipcClientManager = nullptr; + // 2. call IpcClientProxy SendRequest + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->SendRequest(0, req, rsp); + // 3. check ret is DM_POINT_NULL + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: SendRequest_004 + * @tc.desc: 1. set req not nullptr + * set rsp not nullptr + * 2. Mock IpcClient SendRequest return DM_FAILED + * 3. call IpcClientProxy SendRequest + * 4. check ret is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, SendRequest_004, testing::ext::TestSize.Level0) +{ + // 1. set req not nullptr + std::shared_ptr req = std::make_shared(); + // set rsp not nullptr + std::shared_ptr rsp = std::make_shared(); + // 2. Mock IpcClient SendRequest return DM_FAILED + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_FAILED)); + // 3. call IpcClientProxy SendRequest + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->SendRequest(0, req, rsp); + // 4. check ret is DM_FAILED + ASSERT_EQ(ret, DM_FAILED); +} + +/** + * @tc.name: SendRequest_004 + * @tc.desc: 1. set req not nullptr + * set rsp not nullptr + * 2. Mock IpcClient SendRequest return DM_OK + * 3. call IpcClientProxy SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientProxyTest, SendRequest5, testing::ext::TestSize.Level0) +{ + // 1. set req not nullptr + std::shared_ptr req = std::make_shared(); + // set rsp not nullptr + std::shared_ptr rsp = std::make_shared(); + // 2. Mock IpcClient SendRequest return DM_FAILED + std::shared_ptr mockInstance = std::make_shared(); + std::shared_ptr ipcClientManager = mockInstance; + EXPECT_CALL(*mockInstance, SendRequest(testing::_, testing::_, testing::_)) + .Times(1).WillOnce(testing::Return(DM_OK)); + // 3. call IpcClientProxy SendRequest + std::shared_ptr ipcClientProxy = std::make_shared(ipcClientManager); + int32_t ret = ipcClientProxy->SendRequest(0, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_ipc_client_proxy.h b/test/unittest/UTTest_ipc_client_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..b6c6556ac3884604c91564edf6c7b6bb1eccc416 --- /dev/null +++ b/test/unittest/UTTest_ipc_client_proxy.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_IPC_CLIENT_PROXY_TEST_H +#define OHOS_IPC_CLIENT_PROXY_TEST_H + +#include +#include + +#include "mock/mock_ipc_client.h" +#include "ipc_client_proxy.h" +#include "ipc_client.h" +#include "ipc_req.h" +#include "ipc_rsp.h" +namespace OHOS { +namespace DistributedHardware { +class IpcClientProxyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_IPC_CLIENT_PROXY_TEST_H diff --git a/test/unittest/UTTest_ipc_client_stub.cpp b/test/unittest/UTTest_ipc_client_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..223d7406f2fa4cb92d428a4c2761762cf1b9e97a --- /dev/null +++ b/test/unittest/UTTest_ipc_client_stub.cpp @@ -0,0 +1,138 @@ +/* + * 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_ipc_client_stub.h" +#include "dm_device_info.h" +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "ipc_client_manager.h" +#include "ipc_set_useroperation_req.h" +#include "ipc_cmd_register.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "ipc_rsp.h" +#include "ipc_def.h" +#include "dm_constants.h" + +#include + +namespace OHOS { +namespace DistributedHardware { +void IpcClientStubTest::SetUp() +{ +} + +void IpcClientStubTest::TearDown() +{ +} + +void IpcClientStubTest::SetUpTestCase() +{ +} + +void IpcClientStubTest::TearDownTestCase() +{ +} + +namespace { +/** + * @tc.name: OnRemoteRequest_001 + * @tc.desc: 1. set MessageOption not null + * set MessageParcel not null + * set MessageParcel not null + * 2. set set code is 999 + * 3. call IpcClientStub OnRemoteRequest with parameter + * 4. check result is DM_FAILED + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientStubTest, OnRemoteRequest_001, testing::ext::TestSize.Level0) +{ + // 1. set MessageOption not null + MessageOption option; + // set MessageParcel not null + MessageParcel data; + // set MessageParcel not null + MessageParcel reply; + // 2. set set code is 999 + int code = 999; + sptr instance = new IpcClientStub(); + // 3. call IpcClientStub OnRemoteRequest with parameter + int32_t ret = instance->OnRemoteRequest(code, data, reply, option); + int32_t result = DM_OK; + if (ret != result) { + result = DM_FAILED; + } + // 4. check result is DM_FAILED + ASSERT_EQ(result, DM_FAILED); +} + +/** + * @tc.name: OnRemoteRequest_002 + * @tc.desc: 1. set MessageOption not null + * set MessageParcel not null + * set MessageParcel not null + * 2. set set code is SERVER_DEVICE_FA_NOTIFY + * 3. call IpcClientStub OnRemoteRequest with parameter + * 4. check result is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientStubTest, OnRemoteRequest_002, testing::ext::TestSize.Level0) +{ + // 1. set MessageOption not null + MessageOption option; + // set MessageParcel not null + MessageParcel data; + // set MessageParcel not null + MessageParcel reply; + // 2. set set code is SERVER_DEVICE_FA_NOTIFY + int code = SERVER_DEVICE_FA_NOTIFY; + sptr instance = new IpcClientStub(); + // 3. call IpcClientStub OnRemoteRequest with parameter + int ret = instance->OnRemoteRequest(code, data, reply, option); + // 4. check result is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendCmd_001 + * @tc.desc: 1. set set code is SERVER_DEVICE_FA_NOTIFY + * set req is nullptr + * set rsp is nullptr + * 2. call IpcClientStub SendCmd with parameter + * 3. check result is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcClientStubTest, SendCmd_001, testing::ext::TestSize.Level0) +{ + // 1. set set code is SERVER_DEVICE_FA_NOTIFY + int cmdCode = SERVER_DEVICE_FA_NOTIFY; + // set req is nullptr + std::shared_ptr req = nullptr; + // set rsp is nullptr + std::shared_ptr rsp = nullptr; + sptr instance = new IpcClientStub(); + // 2. call IpcClientStub SendCmd with parameter + int ret = instance->SendCmd(cmdCode, req, rsp); + // 3. check result is DM_OK + ASSERT_EQ(ret, DM_OK); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_ipc_client_stub.h b/test/unittest/UTTest_ipc_client_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..5ba7391c9f0b0e077cb9413e6f2a2a1e9dcf66f1 --- /dev/null +++ b/test/unittest/UTTest_ipc_client_stub.h @@ -0,0 +1,35 @@ +/* + * 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_IPC_CLIENT_STUB_TEST_H +#define OHOS_IPC_CLIENT_STUB_TEST_H + +#include +#include + +#include "ipc_client_stub.h" +namespace OHOS { +namespace DistributedHardware { +class IpcClientStubTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_IPC_CLIENT_STUB_TEST_H diff --git a/test/unittest/UTTest_ipc_server_client_proxy.cpp b/test/unittest/UTTest_ipc_server_client_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1d5cd1f90021326f3647dec6179151c36be44f54 --- /dev/null +++ b/test/unittest/UTTest_ipc_server_client_proxy.cpp @@ -0,0 +1,193 @@ +/* + * 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_ipc_server_client_proxy.h" +#include "dm_device_info.h" +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "ipc_client_manager.h" +#include "ipc_set_useroperation_req.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_rsp.h" +#include "ipc_notify_device_found_req.h" +#include "ipc_notify_discover_result_req.h" +#include "dm_constants.h" +#include + +namespace OHOS { +namespace DistributedHardware { +void IpcServerClientProxyTest::SetUp() +{ +} + +void IpcServerClientProxyTest::TearDown() +{ +} + +void IpcServerClientProxyTest::SetUpTestCase() +{ +} + +void IpcServerClientProxyTest::TearDownTestCase() +{ +} + +namespace { +/** + * @tc.name: SendCmd_001 + * @tc.desc: 1. set cmdCode not null + * 2. set remoteObject nullptr + * 3. call IpcServerClientProxy SendCmd + * 4. check ret is DEVICEMANAGER_NULLPTR + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerClientProxyTest, SendCmd_001, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = 1; + // 2. set remoteObject nullptr + sptr remoteObject = nullptr; + // 3. call IpcServerClientProxy SendCmd + auto instance = new IpcServerClientProxy(remoteObject); + int ret = instance->SendCmd(cmdCode, nullptr, nullptr); + // 4. check ret is DEVICEMANAGER_NULLPTR + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: SendCmd_002 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * set action not null + * 2. set remoteObject not nullptr + * set req not null + * set rsp not null + * 3. call IpcServerClientProxy SendCmd with parameter + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerClientProxyTest, SendCmd_002, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // set action not null + int deviceState = 1; + DmDeviceInfo deviceInfo; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + // set req not null + req->SetPkgName(pkgName); + // set rsp not null + req->SetDeviceState(deviceState); + req->SetDeviceInfo(deviceInfo); + // 3. call IpcServerClientProxy SendCmd with parameter + int ret = 0; + std::shared_ptr ipcServerListener = std::make_shared(); + ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendCmd_003 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * set action not null + * 2. set remoteObject not nullptr + * set req not null + * set rsp not null + * 3. call IpcServerClientProxy SendCmd with parameter + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerClientProxyTest, SendCmd_003, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DEVICE_FOUND; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // set action not null + uint16_t subscribeId = 1; + DmDeviceInfo dmDeviceInfo; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + // set req not null + req->SetPkgName(pkgName); + // set rsp not null + req->SetSubscribeId(subscribeId); + req->SetDeviceInfo(dmDeviceInfo); + // 3. call IpcServerClientProxy SendCmd with parameter + int ret = 0; + std::shared_ptr ipcServerListener = std::make_shared(); + ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendCmd_004 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * set action not null + * 2. set remoteObject not nullptr + * set req not null + * set rsp not null + * 3. call IpcServerClientProxy SendCmd with parameter + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerClientProxyTest, SendCmd_004, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DISCOVER_FINISH; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // set action not null + uint16_t subscribeId = 1; + int32_t result = 1; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + // set req not null + req->SetPkgName(pkgName); + // set rsp not null + req->SetSubscribeId(subscribeId); + req->SetResult(result); + // 3. call IpcServerClientProxy SendCmd with parameter + int ret = 0; + std::shared_ptr ipcServerListener = std::make_shared(); + ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_ipc_server_client_proxy.h b/test/unittest/UTTest_ipc_server_client_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..2de4bdf6ccf6b2b38a020de08ce12b0b75fa5bf7 --- /dev/null +++ b/test/unittest/UTTest_ipc_server_client_proxy.h @@ -0,0 +1,38 @@ +/* + * 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_IPC_SERVER_CLIENT_PROXY_TEST_H +#define OHOS_IPC_SERVER_CLIENT_PROXY_TEST_H + +#include + +#include "ipc_server_client_proxy.h" +#include "ipc_client_stub.h" +#include "ipc_server_stub.h" +#include "ipc_server_listener.h" + +namespace OHOS { +namespace DistributedHardware { +class IpcServerClientProxyTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_IPC_SERVER_CLIENT_PROXY_TEST_H diff --git a/test/unittest/UTTest_ipc_server_listener.cpp b/test/unittest/UTTest_ipc_server_listener.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ec412dca490866fd60bd01005fc91ca2044c7d4f --- /dev/null +++ b/test/unittest/UTTest_ipc_server_listener.cpp @@ -0,0 +1,381 @@ +/* + * 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_ipc_server_listener.h" + +#include +#include "dm_device_info.h" +#include "ipc_remote_broker.h" +#include "iremote_object.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "ipc_client_manager.h" +#include "ipc_set_useroperation_req.h" +#include "ipc_notify_device_state_req.h" +#include "ipc_req.h" +#include "ipc_rsp.h" +#include "dm_constants.h" + + +namespace OHOS { +namespace DistributedHardware { +void IpcServerListenerTest::SetUp() +{ +} + +void IpcServerListenerTest::TearDown() +{ +} + +void IpcServerListenerTest::SetUpTestCase() +{ +} + +void IpcServerListenerTest::TearDownTestCase() +{ +} + +namespace { +/** + * @tc.name: SendRequest_001 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendRequest_001, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = 999; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set remoteObject nullptr + sptr remoteObject = nullptr; + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_POINT_NULL + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: SendRequest_002 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendRequest_002, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendRequest_003 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendRequest_003, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = 9999; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendRequest_004 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendRequest_004, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY; + // set pkgName not null + std::string pkgName = ""; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_POINT_NULL + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: SendRequest_005 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendRequest_005, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = 9999; + // set pkgName not null + std::string pkgName = ""; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendRequest(cmdCode, req, rsp); + // 4. check ret is DM_POINT_NULL + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: SendAll_001 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendAll_001, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendAll(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendAll_002 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendAll_002, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = 9999; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendAll(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendAll_003 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendAll_003, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY; + // set pkgName not null + std::string pkgName = ""; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendAll(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendAll_004 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendAll_004, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = 9999; + // set pkgName not null + std::string pkgName = ""; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = std::make_shared(); + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendAll(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendAll_005 + * @tc.desc: 1. set cmdCode not null + * set pkgName not null + * 2. set remoteObject nullptr + * set req not null + * set rsp not null + * 3. call IpcServerListener SendRequest + * 4. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerListenerTest, SendAll_005, testing::ext::TestSize.Level0) +{ + // 1. set cmdCode not null + int32_t cmdCode = SERVER_DEVICE_STATE_NOTIFY; + // set pkgName not null + std::string pkgName = "com.ohos.test"; + // 2. set remoteObject not nullptr + sptr remoteObject = sptr(new IpcClientStub()); + IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, remoteObject); + // set req not null + std::shared_ptr req = std::make_shared(); + // set rsp not null + std::shared_ptr rsp = nullptr; + req->SetPkgName(pkgName); + // 3. call IpcServerListener SendRequest + std::shared_ptr ipcServerListener = std::make_shared(); + int ret = ipcServerListener->SendAll(cmdCode, req, rsp); + // 4. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_ipc_server_listener.h b/test/unittest/UTTest_ipc_server_listener.h new file mode 100644 index 0000000000000000000000000000000000000000..0d51a7aac18205373b59204df0920b469e82414b --- /dev/null +++ b/test/unittest/UTTest_ipc_server_listener.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_IPC_SERVER_LISTENER_TEST_H +#define OHOS_IPC_SERVER_LISTENER_TEST_H + +#include + +#include "ipc_client_stub.h" +#include "ipc_server_stub.h" +#include "ipc_server_listener.h" +namespace OHOS { +namespace DistributedHardware { +class IpcServerListenerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_IPC_SERVER_CLIENT_PROXY_TEST_H diff --git a/test/unittest/UTTest_ipc_server_stub.cpp b/test/unittest/UTTest_ipc_server_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9bd4cf34970f9798567968783cf8e56577dacfdb --- /dev/null +++ b/test/unittest/UTTest_ipc_server_stub.cpp @@ -0,0 +1,546 @@ +/* + * 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_ipc_server_stub.h" +#include "dm_device_info.h" +#include "ipc_server_stub.h" +#include "device_manager_impl.h" +#include "dm_constants.h" +#include "if_system_ability_manager.h" +#include "ipc_cmd_register.h" +#include "ipc_skeleton.h" +#include "ipc_types.h" +#include "iservice_registry.h" +#include "string_ex.h" +#include "system_ability_definition.h" + +#include +#include +#include + +namespace OHOS { +namespace DistributedHardware { +void IpcServerStubTest::SetUp() +{ +} + +void IpcServerStubTest::TearDown() +{ +} + +void IpcServerStubTest::SetUpTestCase() +{ +} + +void IpcServerStubTest::TearDownTestCase() +{ +} + +namespace { +/** + * @tc.name: OnStart_001 + * @tc.desc: 1. Call IpcServerStub OnStart + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, OnStart_001, testing::ext::TestSize.Level0) +{ + // 1. Call IpcServerStub OnStart + IpcServerStub::GetInstance().OnStart(); + // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + ASSERT_EQ(ServiceRunningState::STATE_RUNNING, IpcServerStub::GetInstance().state_); +} + +/** + * @tc.name: OnStart_002 + * @tc.desc: 1. set IpcServerStub state is ServiceRunningState::STATE_RUNNING + * 2. Call IpcServerStub OnStart + * 3. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, OnStart_002, testing::ext::TestSize.Level0) +{ + // 1. set IpcServerStub state is ServiceRunningState::STATE_RUNNING + IpcServerStub::GetInstance().state_ = ServiceRunningState::STATE_RUNNING; + // 2. Call IpcServerStub OnStart + IpcServerStub::GetInstance().OnStart(); + // 3. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + ASSERT_EQ(ServiceRunningState::STATE_RUNNING, IpcServerStub::GetInstance().state_); +} + +/** + * @tc.name: Init_001 + * @tc.desc: 1. Call IpcServerStub OnStart + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, Init_001, testing::ext::TestSize.Level0) +{ + IpcServerStub::GetInstance().registerToService_=true; + bool result = IpcServerStub::GetInstance().Init(); + // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + ASSERT_EQ(result, true); +} + +/** + * @tc.name: Init_002 + * @tc.desc: 1. Call IpcServerStub OnStart + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, Init_002, testing::ext::TestSize.Level0) +{ + IpcServerStub::GetInstance().registerToService_=false; + bool result = IpcServerStub::GetInstance().Init(); + // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + ASSERT_EQ(result, true); +} + +/** + * @tc.name: OnStop_001 + * @tc.desc: 1. Call IpcServerStub OnStop + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, OnStop_001, testing::ext::TestSize.Level0) +{ + // 1. Call IpcServerStub OnStop + IpcServerStub::GetInstance().OnStop(); + // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + ASSERT_EQ(ServiceRunningState::STATE_NOT_START, IpcServerStub::GetInstance().state_); + ASSERT_EQ(IpcServerStub::GetInstance().registerToService_, false); +} + +/** + * @tc.name: OnRemoteRequest_001 + * @tc.desc: 1. Set Code = 999 + * 2. Call IpcServerStub OnRemoteRequest with param + * 3. check ret not DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, OnRemoteRequest_001, testing::ext::TestSize.Level0) +{ + // 1. Set Code = 999 + uint32_t code = 999; + MessageParcel data; + MessageParcel reply; + MessageOption option; + int ret = 0; + // 2. Call IpcServerStub OnRemoteRequest with param + ret = IpcServerStub::GetInstance().OnRemoteRequest(code, data, reply, option); + // 3. check ret not DM_OK + ASSERT_NE(ret, DM_OK); +} + +/** + * @tc.name: OnRemoteRequest_002 + * @tc.desc: 1. Set Code = 999 + * 2. Call IpcServerStub OnRemoteRequest with param + * 3. check ret not DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, OnRemoteRequest_002, testing::ext::TestSize.Level0) +{ + // 1. Set Code is SERVER_DEVICE_STATE_NOTIFY + uint32_t code = SERVER_DEVICE_STATE_NOTIFY; + MessageParcel data; + MessageParcel reply; + MessageOption option; + int ret = 0; + // 2. Call IpcServerStub OnRemoteRequest with param + ret = IpcServerStub::GetInstance().OnRemoteRequest(code, data, reply, option); + // 3. check ret not DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: SendCmd_001 + * @tc.desc: 1. Call IpcServerStub SendCmd + * 2. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, SendCmd_001, testing::ext::TestSize.Level0) +{ + int32_t cmdCode = 1; + std::shared_ptr req = std::make_shared(); + std::shared_ptr rsp = std::make_shared(); + // 1. Call IpcServerStub SendCmd + int32_t ret = IpcServerStub::GetInstance().SendCmd(cmdCode, req, rsp); + // 2. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: QueryServiceState_001 + * @tc.desc: 1. Call IpcServerStub QueryServiceState + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, QueryServiceState_001, testing::ext::TestSize.Level0) +{ + IpcServerStub::GetInstance().state_ = ServiceRunningState::STATE_NOT_START; + // 1. Call IpcServerStub QueryServiceState + ServiceRunningState state = IpcServerStub::GetInstance().QueryServiceState(); + // 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + ASSERT_EQ(state, ServiceRunningState::STATE_NOT_START); +} + +/** + * @tc.name: RegisterDeviceManagerListener_001 + * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int ret = 0; + sptr listener = nullptr; + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: RegisterDeviceManagerListener_002 + * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_002, testing::ext::TestSize.Level0) +{ + std::string pkgName = "com.ohos.test"; + int ret = 0; + sptr listener = sptr(new IpcClientStub()); + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: RegisterDeviceManagerListener_003 + * @tc.desc: 1. Call IpcServerStub RegisterDeviceManagerListener + * 2. check IpcServerStub.state is ServiceRunningState::STATE_RUNNING + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_003, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int ret = 0; + sptr listener = sptr(new IpcClientStub()); + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: RegisterDeviceManagerListener_004 + * @tc.desc: 1. Set PkgName is com.ohos.test + * 2. Call IpcServerStub RegisterDeviceManagerListener with param + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_004, testing::ext::TestSize.Level0) +{ + // 1. Set PkgName is com.ohos.test + std::string pkgName = "com.ohos.test"; + int ret = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: RegisterDeviceManagerListener_005 + * @tc.desc: 1. Set PkgName is com.ohos.test + * 2. Call IpcServerStub RegisterDeviceManagerListener with param + * 3. check ret is DM_OK + * 4. Call IpcServerStub RegisterDeviceManagerListener with same pkgName another listener + * 5. check result is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, RegisterDeviceManagerListener_005, testing::ext::TestSize.Level0) +{ + // 1. Set PkgName is com.ohos.test + std::string pkgName = "com.ohos.test"; + int ret = 0; + int result = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); + sptr listener2 = sptr(new IpcClientStub()); + // 4. Call IpcServerStub RegisterDeviceManagerListener with same pkgName another listener + result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener2); + // 5. check result is DM_OK + ASSERT_EQ(result, DM_OK); +} + +/** + * @tc.name: UnRegisterDeviceManagerListener_001 + * @tc.desc: 1. Call IpcServerStub UnRegisterDeviceManagerListener + * 2. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_001, testing::ext::TestSize.Level0) +{ + std::string pkgName = ""; + int ret = 0; + ret = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName); + ASSERT_EQ(ret, DM_POINT_NULL); +} + +/** + * @tc.name: UnRegisterDeviceManagerListener_002 + * @tc.desc: 1. Set PkgName is com.ohos.test + * 2. Call IpcServerStub RegisterDeviceManagerListener with param + * 3. check ret is DM_OK + * 4. Call IpcServerStub UnRegisterDeviceManagerListener + * 5. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_002, testing::ext::TestSize.Level0) +{ + // 1. Set PkgName is com.ohos.test + std::string pkgName = "com.ohos.test"; + int ret = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); + int result = 0; + // 4. Call IpcServerStub UnRegisterDeviceManagerListener + result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName); + // 5. check ret is DM_OK + ASSERT_EQ(result, DM_OK); +} + +/** + * @tc.name: UnRegisterDeviceManagerListener_003 + * @tc.desc: 1. Set pkgName is com.ohos.test + * 2. Call IpcServerStub UnRegisterDeviceManagerListener + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_003, testing::ext::TestSize.Level0) +{ + // 1. Set pkgName is com.ohos.test + std::string pkgName = "com.ohos.test"; + int ret = 0; + // 2. Call IpcServerStub UnRegisterDeviceManagerListener + ret = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName); + // 3. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); +} + +/** + * @tc.name: UnRegisterDeviceManagerListener_004 + * @tc.desc: 1. Set PkgName is com.ohos.test + * 2. Call IpcServerStub RegisterDeviceManagerListener with param + * 3. check ret is DM_OK + * 4. Call IpcServerStub UnRegisterDeviceManagerListener + * 5. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_004, testing::ext::TestSize.Level0) +{ + // 1. Set PkgName is com.ohos.test + std::string pkgName = "com.ohos.test1"; + int ret = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); + int result = 0; + // 4. Call IpcServerStub UnRegisterDeviceManagerListener + result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName); + // 5. check ret is DM_OK + ASSERT_EQ(result, DM_OK); + sptr dmListener = IpcServerStub::GetInstance().dmListener_[pkgName]; + ASSERT_EQ(dmListener, nullptr); +} + +/** + * @tc.name: UnRegisterDeviceManagerListener_005 + * @tc.desc: 1. Set PkgName is com.ohos.test + * 2. Call IpcServerStub RegisterDeviceManagerListener with param + * 3. check ret is DM_OK + * 4. Call IpcServerStub UnRegisterDeviceManagerListener + * 5. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, UnRegisterDeviceManagerListener_005, testing::ext::TestSize.Level0) +{ + // 1. Set PkgName is com.ohos.test + std::string pkgName = "com.ohos.test2"; + int ret = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + ret = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_OK + ASSERT_EQ(ret, DM_OK); + int result = 0; + // 4. Call IpcServerStub UnRegisterDeviceManagerListener + std::string testPkgName = "com.test"; + result = IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(testPkgName); + // 5. check ret is DM_OK + ASSERT_EQ(result, DM_OK); + sptr dmListener = IpcServerStub::GetInstance().dmListener_[pkgName]; + ASSERT_NE(dmListener, nullptr); +} + +/** + * @tc.name: GetDmListener_001 + * @tc.desc: 1. Set pkgName is com.ohos.test + * 2. Call IpcServerStub GetDmListener + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, GetDmListener_001, testing::ext::TestSize.Level0) +{ + // 1. Set pkgName is com.ohos.test + std::string pkgName = "com.ohos.test"; + sptr ret = nullptr; + // 2. Call IpcServerStub UnRegisterDeviceManagerListener + ret = IpcServerStub::GetInstance().GetDmListener(pkgName); + // 3. check ret is DM_OK + ASSERT_EQ(ret, nullptr); +} + +/** + * @tc.name: GetDmListener_002 + * @tc.desc: 1. Set pkgName is com.ohos.test + * 2. Call IpcServerStub GetDmListener + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, GetDmListener_002, testing::ext::TestSize.Level0) +{ + // 1. Set pkgName is com.ohos.test + std::string pkgName = "com.ohos.test"; + int result = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_OK + ASSERT_EQ(result, DM_OK); + sptr ret = nullptr; + // 2. Call IpcServerStub UnRegisterDeviceManagerListener + ret = IpcServerStub::GetInstance().GetDmListener(pkgName); + // 3. check ret is DM_OK + ASSERT_NE(ret, nullptr); +} + +/** + * @tc.name: GetDmListener_003 + * @tc.desc: 1. Set pkgName is com.ohos.test + * 2. Call IpcServerStub GetDmListener + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, GetDmListener_003, testing::ext::TestSize.Level0) +{ + // 1. Set pkgName is com.ohos.test + std::string pkgName = "com.ohos.test"; + int result = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_OK + ASSERT_EQ(result, DM_OK); + sptr ret = nullptr; + // 2. Call IpcServerStub UnRegisterDeviceManagerListener + std::string testPkgName = "test"; + ret = IpcServerStub::GetInstance().GetDmListener(testPkgName); + // 3. check ret is DM_OK + ASSERT_EQ(ret, nullptr); +} + +/** + * @tc.name: GetDmListener_004 + * @tc.desc: 1. Set pkgName is com.ohos.test + * 2. Call IpcServerStub GetDmListener + * 3. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, GetDmListener_004, testing::ext::TestSize.Level0) +{ + // 1. Set pkgName is null + std::string pkgName = ""; + int result = 0; + sptr listener = sptr(new IpcClientStub()); + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_POINT_NULL + ASSERT_EQ(result, DM_POINT_NULL); + sptr ret = nullptr; + // 2. Call IpcServerStub UnRegisterDeviceManagerListener + ret = IpcServerStub::GetInstance().GetDmListener(pkgName); + // 3. check ret is nullptr + ASSERT_EQ(ret, nullptr); +} + +/** + * @tc.name: GetDmListener_005 + * @tc.desc: 1. Set pkgName is com.ohos.test + * 2. Call IpcServerStub GetDmListener + * 3. check ret is DM_POINT_NULL + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, GetDmListener_005, testing::ext::TestSize.Level0) +{ + // 1. Set pkgName is null + std::string pkgName = "com.test.ohos"; + int result = 0; + sptr listener = nullptr; + // 2. Call IpcServerStub RegisterDeviceManagerListener with param + result = IpcServerStub::GetInstance().RegisterDeviceManagerListener(pkgName, listener); + // 3. check ret is DM_POINT_NULL + ASSERT_EQ(result, DM_POINT_NULL); + sptr ret = nullptr; + // 2. Call IpcServerStub UnRegisterDeviceManagerListener + ret = IpcServerStub::GetInstance().GetDmListener(pkgName); + // 3. check ret is nullptr + ASSERT_EQ(ret, nullptr); +} +} // namespace +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_ipc_server_stub.h b/test/unittest/UTTest_ipc_server_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..f4b00c361c056baa66c58a217b0c5aa9ba54c87d --- /dev/null +++ b/test/unittest/UTTest_ipc_server_stub.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_IPC_SERVER_STUB_TEST_H +#define OHOS_IPC_SERVER_STUB_TEST_H + +#include + +#include "ipc_client_stub.h" +#include "ipc_server_stub.h" +#include "ipc_server_listener.h" +namespace OHOS { +namespace DistributedHardware { +class IpcServerStubTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_IPC_SERVER_STUB_TEST_H diff --git a/test/unittest/mock/device_auth.h b/test/unittest/mock/device_auth.h index 39b701f894cc4f7c38a9b204873b120c8a83570c..68e7cba41e08f83cc745f6d9c1812ae402f76999 100644 --- a/test/unittest/mock/device_auth.h +++ b/test/unittest/mock/device_auth.h @@ -4,7 +4,7 @@ * 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 + * 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, @@ -16,10 +16,11 @@ #ifndef DEVICE_AUTH_H #define DEVICE_AUTH_H -#include +#include +#include #if defined(__LINUX__) || defined(_UNIX) -#define DEVICE_AUTH_API_PUBLIC __attribute__((visibility("default"))) +#define DEVICE_AUTH_API_PUBLIC __attribute__ ((visibility("default"))) #else #define DEVICE_AUTH_API_PUBLIC #endif @@ -65,51 +66,68 @@ #define FIELD_GROUP_VISIBILITY "groupVisibility" #define FIELD_EXPIRE_TIME "expireTime" #define FIELD_IS_DELETE_ALL "isDeleteAll" +#define FIELD_BLE_CHALLENGE "bleChallenge" +#define FIELD_OS_ACCOUNT_ID "osAccountId" -enum GroupType { +typedef enum { + DEFAULT_OS_ACCOUNT = 0, + INVALID_OS_ACCOUNT = -1, + ANY_OS_ACCOUNT = -2, +} OsAccountEnum; + +typedef enum { ALL_GROUP = 0, IDENTICAL_ACCOUNT_GROUP = 1, PEER_TO_PEER_GROUP = 256, COMPATIBLE_GROUP = 512, ACROSS_ACCOUNT_AUTHORIZE_GROUP = 1282 -}; +} GroupType; -enum GroupOperationCode { +typedef enum { GROUP_CREATE = 0, GROUP_DISBAND = 1, MEMBER_INVITE = 2, MEMBER_JOIN = 3, MEMBER_DELETE = 4, ACCOUNT_BIND = 5 -}; +} GroupOperationCode; -enum GroupAuthForm { +typedef enum { AUTH_FORM_INVALID_TYPE = -1, AUTH_FORM_ACCOUNT_UNRELATED = 0, AUTH_FORM_IDENTICAL_ACCOUNT = 1, AUTH_FORM_ACROSS_ACCOUNT = 2, -}; +} GroupAuthForm; -enum CredentialCode { - CREDENTIAL_SAVE = 0, - CREDENTIAL_CLEAR = 1, - CREDENTIAL_UPDATE = 2, - CREDENTIAL_QUERY = 3, -}; +typedef enum { + IMPORT_SELF_CREDENTIAL = 0, + DELETE_SELF_CREDENTIAL = 1, + QUERY_SELF_CREDENTIAL_INFO = 2, + IMPORT_TRUSTED_CREDENTIALS = 3, + DELETE_TRUSTED_CREDENTIALS = 4, + QUERY_TRUSTED_CREDENTIALS = 5, + REQUEST_SIGNATURE = 6, +} CredentialCode; -enum UserType { +typedef enum { DEVICE_TYPE_ACCESSORY = 0, DEVICE_TYPE_CONTROLLER = 1, DEVICE_TYPE_PROXY = 2 -}; +} UserType; + +typedef enum { + EXPIRE_TIME_INDEFINITE = -1, + EXPIRE_TIME_MIN = 1, + EXPIRE_TIME_MAX = 90, +} ExpireTime; -enum RequestResponse { +typedef enum { REQUEST_REJECTED = 0x80000005, REQUEST_ACCEPTED = 0x80000006, REQUEST_WAITING = 0x80000007 -}; +} RequestResponse; -struct DataChangeListener { +typedef struct { void (*onGroupCreated)(const char *groupInfo); void (*onGroupDeleted)(const char *groupInfo); void (*onDeviceBound)(const char *peerUdid, const char *groupInfo); @@ -117,38 +135,40 @@ struct DataChangeListener { void (*onDeviceNotTrusted)(const char *peerUdid); void (*onLastGroupDeleted)(const char *peerUdid, int groupType); void (*onTrustedDeviceNumChanged)(int curTrustedDeviceNum); -}; +} DataChangeListener; -struct DeviceAuthCallback { +typedef struct { bool (*onTransmit)(int64_t requestId, const uint8_t *data, uint32_t dataLen); void (*onSessionKeyReturned)(int64_t requestId, const uint8_t *sessionKey, uint32_t sessionKeyLen); void (*onFinish)(int64_t requestId, int operationCode, const char *returnData); void (*onError)(int64_t requestId, int operationCode, int errorCode, const char *errorReturn); char *(*onRequest)(int64_t requestId, int operationCode, const char *reqParams); -}; +} DeviceAuthCallback; -struct GroupAuthManager { +typedef struct { int32_t (*processData)(int64_t authReqId, const uint8_t *data, uint32_t dataLen, - const DeviceAuthCallback *gaCallback); + const DeviceAuthCallback *gaCallback); int32_t (*queryTrustedDeviceNum)(void); bool (*isTrustedDevice)(const char *udid); - int32_t (*getAuthState)(int64_t authReqId, const char *groupId, const char *peerUdid, uint8_t *out, - uint32_t *outLen); - int32_t (*authDevice)(int64_t authReqId, const char *authParams, const DeviceAuthCallback *gaCallback); + int32_t (*getAuthState)(int64_t authReqId, const char *groupId, const char *peerUdid, + uint8_t *out, uint32_t *outLen); + int32_t (*authDevice)(int32_t osAccountId, int64_t authReqId, const char *authParams, + const DeviceAuthCallback *gaCallback); void (*informDeviceDisconnection)(const char *udid); -}; +} GroupAuthManager; -struct DeviceGroupManager { +typedef struct { int32_t (*regCallback)(const char *appId, const DeviceAuthCallback *callback); int32_t (*unRegCallback)(const char *appId); int32_t (*regDataChangeListener)(const char *appId, const DataChangeListener *listener); int32_t (*unRegDataChangeListener)(const char *appId); - int32_t (*createGroup)(int64_t requestId, const char *appId, const char *createParams); - int32_t (*deleteGroup)(int64_t requestId, const char *appId, const char *disbandParams); - int32_t (*addMemberToGroup)(int64_t requestId, const char *appId, const char *addParams); - int32_t (*deleteMemberFromGroup)(int64_t requestId, const char *appId, const char *deleteParams); + int32_t (*createGroup)(int32_t osAccountId, int64_t requestId, const char *appId, const char *createParams); + int32_t (*deleteGroup)(int32_t osAccountId, int64_t requestId, const char *appId, const char *disbandParams); + int32_t (*addMemberToGroup)(int32_t osAccountId, int64_t requestId, const char *appId, const char *addParams); + int32_t (*deleteMemberFromGroup)(int32_t osAccountId, int64_t requestId, const char *appId, + const char *deleteParams); int32_t (*processData)(int64_t requestId, const uint8_t *data, uint32_t dataLen); - int32_t (*confirmRequest)(int64_t requestId, const char *appId, const char *confirmParams); + int32_t (*confirmRequest)(int32_t osAccountId, int64_t requestId, const char *appId, const char *confirmParams); int32_t (*bindPeer)(int64_t requestId, const char *appId, const char *bindParams); int32_t (*unbindPeer)(int64_t requestId, const char *appId, const char *unbindParams); int32_t (*processLiteData)(int64_t requestId, const char *appId, const uint8_t *data, uint32_t dataLen); @@ -157,24 +177,32 @@ struct DeviceGroupManager { int32_t (*processCredential)(int operationCode, const char *reqJsonStr, char **returnJsonStr); int32_t (*getRegisterInfo)(char **returnRegisterInfo); int32_t (*getLocalConnectInfo)(char *returnInfo, int32_t bufLen); - int32_t (*checkAccessToGroup)(const char *appId, const char *groupId); - int32_t (*getPkInfoList)(const char *appId, const char *queryParams, char **returnInfoList, - uint32_t *returnInfoNum); - int32_t (*addGroupManager)(const char *appId, const char *groupId, const char *managerAppId); - int32_t (*addGroupFriend)(const char *appId, const char *groupId, const char *friendAppId); - int32_t (*deleteGroupManager)(const char *appId, const char *groupId, const char *managerAppId); - int32_t (*deleteGroupFriend)(const char *appId, const char *groupId, const char *friendAppId); - int32_t (*getGroupManagers)(const char *appId, const char *groupId, char **returnManagers, uint32_t *returnSize); - int32_t (*getGroupFriends)(const char *appId, const char *groupId, char **returnFriends, uint32_t *returnSize); - int32_t (*getGroupInfoById)(const char *appId, const char *groupId, char **returnGroupInfo); - int32_t (*getGroupInfo)(const char *appId, const char *queryParams, char **returnGroupVec, uint32_t *groupNum); - int32_t (*getJoinedGroups)(const char *appId, int groupType, char **returnGroupVec, uint32_t *groupNum); - int32_t (*getRelatedGroups)(const char *appId, const char *peerDeviceId, char **returnGroupVec, uint32_t *groupNum); - int32_t (*getDeviceInfoById)(const char *appId, const char *deviceId, const char *groupId, char **returnDeviceInfo); - int32_t (*getTrustedDevices)(const char *appId, const char *groupId, char **returnDevInfoVec, uint32_t *deviceNum); - bool (*isDeviceInGroup)(const char *appId, const char *groupId, const char *deviceId); + int32_t (*checkAccessToGroup)(int32_t osAccountId, const char *appId, const char *groupId); + int32_t (*getPkInfoList)(int32_t osAccountId, const char *appId, const char *queryParams, char **returnInfoList, + uint32_t *returnInfoNum); + int32_t (*addGroupManager)(int32_t osAccountId, const char *appId, const char *groupId, const char *managerAppId); + int32_t (*addGroupFriend)(int32_t osAccountId, const char *appId, const char *groupId, const char *friendAppId); + int32_t (*deleteGroupManager)(int32_t osAccountId, const char *appId, const char *groupId, + const char *managerAppId); + int32_t (*deleteGroupFriend)(int32_t osAccountId, const char *appId, const char *groupId, const char *friendAppId); + int32_t (*getGroupManagers)(int32_t osAccountId, const char *appId, const char *groupId, char **returnManagers, + uint32_t *returnSize); + int32_t (*getGroupFriends)(int32_t osAccountId, const char *appId, const char *groupId, + char **returnFriends, uint32_t *returnSize); + int32_t (*getGroupInfoById)(int32_t osAccountId, const char *appId, const char *groupId, char **returnGroupInfo); + int32_t (*getGroupInfo)(int32_t osAccountId, const char *appId, const char *queryParams, + char **returnGroupVec, uint32_t *groupNum); + int32_t (*getJoinedGroups)(int32_t osAccountId, const char *appId, int groupType, + char **returnGroupVec, uint32_t *groupNum); + int32_t (*getRelatedGroups)(int32_t osAccountId, const char *appId, const char *peerDeviceId, + char **returnGroupVec, uint32_t *groupNum); + int32_t (*getDeviceInfoById)(int32_t osAccountId, const char *appId, const char *deviceId, const char *groupId, + char **returnDeviceInfo); + int32_t (*getTrustedDevices)(int32_t osAccountId, const char *appId, const char *groupId, + char **returnDevInfoVec, uint32_t *deviceNum); + bool (*isDeviceInGroup)(int32_t osAccountId, const char *appId, const char *groupId, const char *deviceId); void (*destroyInfo)(char **returnInfo); -}; +} DeviceGroupManager; #ifdef __cplusplus extern "C" { diff --git a/test/unittest/mock/parameter.cpp b/test/unittest/mock/parameter.cpp index a51d9ed9371769c4abf055d1e37e95aa6524e528..99fa1c54e68aef636a0716033158640d33abdb97 100644 --- a/test/unittest/mock/parameter.cpp +++ b/test/unittest/mock/parameter.cpp @@ -29,3 +29,8 @@ int WatchParameter(const char *keyprefix, ParameterChgPtr callback, void *contex { return 0; } + +int GetDevUdid(char *udid, int size) +{ + return 0; +} \ No newline at end of file diff --git a/test/unittest/mock/parameter.h b/test/unittest/mock/parameter.h index 23da1b6a60ebf597bb6dc6fddbab8e58b4da85ec..77b456430108bc778dbb64ada18c40cb9fe43014 100644 --- a/test/unittest/mock/parameter.h +++ b/test/unittest/mock/parameter.h @@ -29,6 +29,7 @@ int SetParameter(const char *key, const char *value); typedef void (*ParameterChgPtr)(const char *key, const char *value, void *context); int WatchParameter(const char *keyprefix, ParameterChgPtr callback, void *context); +int GetDevUdid(char *udid, int size); #ifdef __cplusplus #if __cplusplus }