diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 8e3dbd99a44ad7edcb27a92eb8dc1ffdf8791940..310a09747480da90e4521f411949de6e39862bd4 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 @@ -173,6 +251,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 @@ -237,6 +326,7 @@ config("device_manager_test_common_public_config") { "//foundation/communication/dsoftbus/interfaces/kits/discovery", "//foundation/communication/dsoftbus/interfaces/inner_kits/transport", "//foundation/distributedhardware/devicemanager/test/unittest/mock", + "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk", "//base/security/deviceauth/interfaces/innerkits", "${services_path}/include/ability", "${services_path}/include/config", @@ -318,6 +408,7 @@ config("device_manager_test_common_public") { "//base/account/os_account/interfaces/innerkits/osaccount/native/include", "//base/account/os_account/frameworks/common/database/include", "//base/account/os_account/frameworks/common/account_error/include", + "//foundation/distributedschedule/safwk/services/safwk", ] cflags = [ 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..61d825e3a3ca781bd599c4f01ce83173e857890f --- /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 +#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" + +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/mock_ipc_client.h b/test/unittest/mock/mock_ipc_client.h new file mode 100644 index 0000000000000000000000000000000000000000..b7b973638f38d92d21c73cda10dab2a15a8444db --- /dev/null +++ b/test/unittest/mock/mock_ipc_client.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_MOCK_IPC_CLIENTMANAGER_H +#define OHOS_MOCK_IPC_CLIENTMANAGER_H + +#include +#include + +#include "ipc_client.h" + +namespace OHOS { +namespace DistributedHardware { +class MockIpcClient : public IpcClient { +public: + MOCK_METHOD1(Init,int32_t(std::string &pkgName)); + MOCK_METHOD1(UnInit,int32_t(std::string &pkgName)); + MOCK_METHOD3(SendRequest,int32_t(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp)); +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_MOCK_IPC_CLIENTMANAGER_H diff --git a/test/unittest/mock/mock_ipc_client_manager.h b/test/unittest/mock/mock_ipc_client_manager.h new file mode 100644 index 0000000000000000000000000000000000000000..46ed4705e0ed6b4636e4fe0a4676078d9227a9ad --- /dev/null +++ b/test/unittest/mock/mock_ipc_client_manager.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_MOCK_IPC_CLIENTMANAGER_H +#define OHOS_MOCK_IPC_CLIENTMANAGER_H + +#include +#include + +#include "ipc_client_server_proxy.h" + +namespace OHOS { +namespace DistributedHardware { +class MockIpcClientManager : public IpcClientServerProxy { +public: + explicit MockIpcClientManager (const sptr &impl): IpcClientServerProxy(impl) {}; + MOCK_METHOD3(SendCmd, int32_t(int32_t cmdCode, std::shared_ptr req, std::shared_ptr rsp)); +}; +} // namespace DistributedHardware +} // namespace OHOS + +#endif // OHOS_MOCK_IPC_CLIENTMANAGER_H