diff --git a/services/core/test/BUILD.gn b/services/core/test/BUILD.gn index 676077c16a6d4fb3af56b80c0a7b41831cb759e4..96053783e544cc5375996a6d356f2d043d49a64b 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 0000000000000000000000000000000000000000..d2b28a1d7eaf9b56ace25f0f98a4ec7cca2f53ed --- /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 0000000000000000000000000000000000000000..9e08e6bcaf6a38db1e677ad4278bf32022b50e6d --- /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 0000000000000000000000000000000000000000..1feea63dfbc9963f86b068378158a572ae56cf3f --- /dev/null +++ b/services/core/test/unittest/distributed_device_profile_service_new_test.cpp @@ -0,0 +1,230 @@ +/* + * 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