From 100ef96b0c6d73545990659ef202e39966dd7a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Sat, 13 Sep 2025 15:17:05 +0800 Subject: [PATCH 1/4] add serviceUT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- services/core/test/BUILD.gn | 31 +++ ...distributed_device_profile_client_test.cpp | 71 ++++++ .../distributed_device_profile_proxy_test.cpp | 69 ++++++ ...ibuted_device_profile_service_new_test.cpp | 233 ++++++++++++++++++ 4 files changed, 404 insertions(+) create mode 100644 services/core/test/unittest/distributed_device_profile_client_test.cpp create mode 100644 services/core/test/unittest/distributed_device_profile_proxy_test.cpp create mode 100644 services/core/test/unittest/distributed_device_profile_service_new_test.cpp diff --git a/services/core/test/BUILD.gn b/services/core/test/BUILD.gn index 676077c1..96053783 100644 --- a/services/core/test/BUILD.gn +++ b/services/core/test/BUILD.gn @@ -508,6 +508,34 @@ ohos_unittest("business_event_manager_test") { subsystem_name = "deviceprofile" } +ohos_unittest("distributed_device_profile_service_new_test") { + module_out_path = module_output_path + sources = [ "unittest/distributed_device_profile_service_new_test.cpp" ] + configs = device_profile_configs + deps = device_profile_deps + external_deps = device_profile_external_deps + part_name = "device_info_manager" + subsystem_name = "deviceprofile" +} +ohos_unittest("distributed_device_profile_client_test") { + module_out_path = module_output_path + sources = [ "unittest/distributed_device_profile_client_test.cpp" ] + configs = device_profile_configs + deps = device_profile_deps + external_deps = device_profile_external_deps + part_name = "device_info_manager" + subsystem_name = "deviceprofile" +} + +ohos_unittest("distributed_device_profile_proxy_test") { + module_out_path = module_output_path + sources = [ "unittest/distributed_device_profile_proxy_test.cpp" ] + configs = device_profile_configs + deps = device_profile_deps + external_deps = device_profile_external_deps + part_name = "device_info_manager" + subsystem_name = "deviceprofile" +} group("unittest") { testonly = true deps = [ @@ -525,6 +553,9 @@ group("unittest") { ":device_profile_manager_new_test", ":distributed_device_profile_client_kv_new_test", ":distributed_device_profile_stub_new_test", + ":distributed_device_profile_client_test", + ":distributed_device_profile_proxy_test", + ":distributed_device_profile_service_new_test", ":dp_account_common_event_test", ":dp_content_sensor_test", ":dp_dm_adapter_test", diff --git a/services/core/test/unittest/distributed_device_profile_client_test.cpp b/services/core/test/unittest/distributed_device_profile_client_test.cpp new file mode 100644 index 00000000..d2b28a1d --- /dev/null +++ b/services/core/test/unittest/distributed_device_profile_client_test.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "distributed_device_profile_client.h" +#include "service_info_profile_new.h" + + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace testing::ext; +using namespace std; + + +class DistributedDeviceProfileClientTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() override {} + void TearDown() override {} +}; + +HWTEST_F(DistributedDeviceProfileClientTest, PutServiceInfoProfile_001, TestSize.Level1) +{ + DistributedDeviceProfileClient client; + ServiceInfoProfileNew profile; + + int32_t ret = client.PutServiceInfoProfile(profile); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileClientTest, DelServiceInfoProfile_001, TestSize.Level1) +{ + DistributedDeviceProfileClient client; + int32_t regServiceId = 123; + int32_t userId = 0; + int32_t ret = client.DeleteServiceInfoProfile(regServiceId, userId); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileClientTest, GetServiceInfoProfileByServiceId_001, TestSize.Level1) +{ + DistributedDeviceProfileClient client; + ServiceInfoProfileNew profile; + int64_t serviceId = 0; + int32_t ret = client.GetServiceInfoProfileByServiceId(serviceId, profile); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileClientTest, GetServiceInfoProfileByTokenId_001, TestSize.Level1) +{ + DistributedDeviceProfileClient client; + ServiceInfoProfileNew profile; + int64_t tokenId = 0; + int32_t ret = client.GetServiceInfoProfileByTokenId(tokenId, profile); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} +} //namespace DistributedDeviceProfile +} //namespace OHOS \ No newline at end of file diff --git a/services/core/test/unittest/distributed_device_profile_proxy_test.cpp b/services/core/test/unittest/distributed_device_profile_proxy_test.cpp new file mode 100644 index 00000000..9e08e6bc --- /dev/null +++ b/services/core/test/unittest/distributed_device_profile_proxy_test.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "service_info_profile_new.h" +#include "distributed_device_profile_proxy.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace testing::ext; +using namespace std; + +class DistributedDeviceProfileProxyTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() override {} + void TearDown() override {} +}; + +HWTEST_F(DistributedDeviceProfileProxyTest, PutServiceInfoProfile_001, TestSize.Level0) +{ + DistributedDeviceProfileProxy proxy(nullptr); + ServiceInfoProfileNew profile; + + int32_t ret = proxy.PutServiceInfoProfile(profile); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileProxyTest, DeleteServiceInfoProfile_001, TestSize.Level0) +{ + DistributedDeviceProfileProxy proxy(nullptr); + int32_t regServiceId = 123; + int32_t userId = 0; + int32_t ret = proxy.DeleteServiceInfoProfile(regServiceId, userId); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileProxyTest, GetServiceInfoProfileByServiceId_001, TestSize.Level0) +{ + DistributedDeviceProfileProxy proxy(nullptr); + ServiceInfoProfileNew profile; + int64_t serviceId = 0; + int32_t ret = proxy.GetServiceInfoProfileByServiceId(serviceId, profile); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileProxyTest, GetServiceInfoProfileByTokenId_001, TestSize.Level0) +{ + DistributedDeviceProfileProxy proxy(nullptr); + ServiceInfoProfileNew profile; + int64_t tokenId = 0; + int32_t ret = proxy.GetServiceInfoProfileByTokenId(tokenId = 0, profile); + EXPECT_FALSE(ret == DP_READ_PARCEL_FAIL); +} +} //namespace DistributedDeviceProfile +} //namespace OHOS diff --git a/services/core/test/unittest/distributed_device_profile_service_new_test.cpp b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp new file mode 100644 index 00000000..6f8795b8 --- /dev/null +++ b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2023-2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_service_new.h" +#include "kv_adapter.h" +#include "multi_user_manager.h" +#include "profile_utils.h" +#include "permission_manager.h" +#include "service_info_manager.h" +#include "service_info_profile_new.h" + +using namespace testing::ext; +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace std; + +class PermissionManagerTest : public PermissionManager +{ +public: + bool CheckCallerPermission() + { + return mockCheckPermissionResult; + } + + static PermissionManagerTest &GetInstance() + { + static PermissionManagerTest instance; + return instance; + } + + void SetMockCheckPermissionResult(bool result) + { + mockCheckPermissionResult = result; + } + + bool mockCheckPermissionResult = true; +}; + +class ServiceInfoProfileManageTest : public ServiceInfoProfileManage +{ +public: + int32_t PutServiceInfoProfile(const ServiceInfoProfileNew &profile) + { + return mockPutServiceInfoResult; + } + + int32_t GetServiceInfoProfile(ServiceInfoProfileNew &profile) + { + return mockGetServiceInfoResult; + } + + int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) + { + return mockDeleteServiceInfoResult; + } + + static ServiceInfoProfileManageTest *GetInstance() + { + static ServiceInfoProfileManageTest instance; + return &instance; + } + + void SetMockPutServiceInfoResult(int32_t result) + { + mockPutServiceInfoResult = result; + } + + void SetMockGetServiceInfoResult(int32_t result) + { + mockGetServiceInfoResult = result; + } + + void SetMockDeleteServiceInfoResult(int32_t result) + { + mockDeleteServiceInfoResult = result; + } + +private: + int32_t mockPutServiceInfoResult = DP_SUCCESS; + int32_t mockGetServiceInfoResult = DP_SUCCESS; + int32_t mockDeleteServiceInfoResult = DP_SUCCESS; +}; + +class DistributedDeviceProfileServiceNewTest : public testing::Test +{ +public: + static void SetUpTestCase(void) {} + static void TearDownTestCase(void) {} + + void SetUp() + { + service = std::make_shared(); + serviceInfoProfile = std::make_shared(); + } + + void TearDown() + { + service.reset(); + serviceInfoProfile.reset(); + } + + std::shared_ptr service; + std::shared_ptr serviceInfoProfile; +}; +HWTEST_F(DistributedDeviceProfileServiceNewTest, PutServiceInfoProfile_001, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(false); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, PutServiceInfoProfile_002, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockPutServiceInfoResult(DP_SUCCESS); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, PutServiceInfoProfile_003, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockPutServiceInfoResult(DP_PERMISSION_DENIED); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().PutServiceInfoProfile(serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, DeleteServiceInfoProfile_001, TestSize.Level1) +{ + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(false); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().DeleteServiceInfoProfile( + 123, 0); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, DeleteServiceInfoProfile_002, TestSize.Level1) +{ + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockDeleteServiceInfoResult(DP_SUCCESS); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().DeleteServiceInfoProfile( + 123, 0); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, DeleteServiceInfoProfile_003, TestSize.Level1) +{ + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockDeleteServiceInfoResult(DP_PERMISSION_DENIED); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().DeleteServiceInfoProfile( + 123, 0); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, GetServiceInfoProfileByServiceId_001, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(false); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetServiceInfoProfileByServiceId( + 123, serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, GetServiceInfoProfileByServiceId_002, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockGetServiceInfoResult(DP_SUCCESS); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetServiceInfoProfileByServiceId( + 123, serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, GetServiceInfoProfileByServiceId_003, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockGetServiceInfoResult(DP_PERMISSION_DENIED); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetServiceInfoProfileByServiceId( + 123, serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, GetServiceInfoProfileByTokenId_001, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(false); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetServiceInfoProfileByTokenId( + 456, serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, GetServiceInfoProfileByTokenId_002, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockGetServiceInfoResult(DP_SUCCESS); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetServiceInfoProfileByTokenId( + 456, serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(DistributedDeviceProfileServiceNewTest, GetServiceInfoProfileByTokenId_003, TestSize.Level1) +{ + ServiceInfoProfileNew serviceInfoProfile; + PermissionManagerTest::GetInstance().SetMockCheckPermissionResult(true); + ServiceInfoProfileManageTest::GetInstance()->SetMockGetServiceInfoResult(DP_PERMISSION_DENIED); + int32_t ret = DistributedDeviceProfileServiceNew::GetInstance().GetServiceInfoProfileByTokenId( + 456, serviceInfoProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file -- Gitee From baf7f81cfb45b532e56e7ef6a15825bc1d5c733d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Sat, 13 Sep 2025 15:28:58 +0800 Subject: [PATCH 2/4] add serviceUT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../unittest/distributed_device_profile_service_new_test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/core/test/unittest/distributed_device_profile_service_new_test.cpp b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp index 6f8795b8..d53c6770 100644 --- a/services/core/test/unittest/distributed_device_profile_service_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp @@ -99,8 +99,7 @@ private: int32_t mockDeleteServiceInfoResult = DP_SUCCESS; }; -class DistributedDeviceProfileServiceNewTest : public testing::Test -{ +class DistributedDeviceProfileServiceNewTest : public testing::Test { public: static void SetUpTestCase(void) {} static void TearDownTestCase(void) {} -- Gitee From dd1bbadab0d47369ce173cb7371ac9d07ba2d193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Sat, 13 Sep 2025 15:39:31 +0800 Subject: [PATCH 3/4] add serviceUT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../unittest/distributed_device_profile_service_new_test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/core/test/unittest/distributed_device_profile_service_new_test.cpp b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp index d53c6770..61b8c90f 100644 --- a/services/core/test/unittest/distributed_device_profile_service_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp @@ -54,8 +54,7 @@ public: bool mockCheckPermissionResult = true; }; -class ServiceInfoProfileManageTest : public ServiceInfoProfileManage -{ +class ServiceInfoProfileManageTest : public ServiceInfoProfileManage { public: int32_t PutServiceInfoProfile(const ServiceInfoProfileNew &profile) { -- Gitee From d81ecbaaf93c39a9c54f9169ca341d307fd5c0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E9=9B=B7?= Date: Sat, 13 Sep 2025 15:48:18 +0800 Subject: [PATCH 4/4] add serviceUT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张雷 --- .../unittest/distributed_device_profile_service_new_test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/core/test/unittest/distributed_device_profile_service_new_test.cpp b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp index 61b8c90f..1feea63d 100644 --- a/services/core/test/unittest/distributed_device_profile_service_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp @@ -32,8 +32,7 @@ namespace OHOS { namespace DistributedDeviceProfile { using namespace std; -class PermissionManagerTest : public PermissionManager -{ +class PermissionManagerTest : public PermissionManager { public: bool CheckCallerPermission() { -- Gitee