diff --git a/services/core/test/BUILD.gn b/services/core/test/BUILD.gn index 676077c16a6d4fb3af56b80c0a7b41831cb759e4..ea4d750ead3c4999e6b8108ab9127d471523a58b 100644 --- a/services/core/test/BUILD.gn +++ b/services/core/test/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2023-2024 Huawei Device Co., Ltd. +# 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 @@ -508,6 +508,36 @@ ohos_unittest("business_event_manager_test") { subsystem_name = "deviceprofile" } +ohos_unittest("service_info_kv_adapter_test") { + module_out_path = module_output_path + sources = [ "unittest/service_info_kv_adapter_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("service_info_profile_new_test") { + module_out_path = module_output_path + sources = [ "unittest/service_info_profile_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("service_info_manager_test") { + module_out_path = module_output_path + sources = [ "unittest/service_info_manager_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 = [ @@ -543,6 +573,8 @@ group("unittest") { ":profile_utils_new_test", ":rdb_adapter_new_test", ":session_key_manager_test", + ":service_info_kv_adapter_test", + ":service_info_profile_new_test", ":static_capability_collector_test", ":static_capability_loader_test", ":static_profile_manager_test", @@ -551,6 +583,7 @@ group("unittest") { ":sync_completed_callback_test", ":sync_options_new_test", ":sync_subscriber_death_recipient_test", + ":service_info_manager_test", ":trust_Device_Profile_test", ":business_event_manager_test", ":business_event_adapter_test", diff --git a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp index 3d01bcb685c25a9e2b99af10cafce9957836edea..87634bf78e33696e625eb943a2b63975cef44b9c 100644 --- a/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp +++ b/services/core/test/unittest/distributed_device_profile_stub_new_test.cpp @@ -35,6 +35,14 @@ namespace DistributedDeviceProfile { using namespace testing::ext; using namespace std; +class MockMessageParcel : public MessageParcel { +public: + bool ReadInt32(int32_t&) const { return true; } + bool WriteInt32(int32_t) const { return mark; } + void WriteBool(bool newMark) { mark = newMark; } + bool mark = false; +}; + class MockDistributedDeviceProfileStubNew : public DistributedDeviceProfileStubNew { int32_t PutAccessControlProfile(const AccessControlProfile& aclProfile) override; int32_t UpdateAccessControlProfile(const AccessControlProfile& aclProfile) override; @@ -105,6 +113,10 @@ class MockDistributedDeviceProfileStubNew : public DistributedDeviceProfileStubN int32_t DeleteServiceInfoProfile(int32_t regServiceId, int32_t userId) override; int32_t GetServiceInfoProfileByServiceId(int64_t serviceId, ServiceInfoProfileNew& serviceInfoProfile) override; int32_t GetServiceInfoProfileByTokenId(int64_t tokenId, ServiceInfoProfileNew& serviceInfoProfile) override; + int32_t mockGetServiceInfoResult_ = DP_SUCCESS; + +public: + void SetMockGetServiceInfoResult(int32_t result); void DelayUnloadTask() override; bool IsInited() override; }; @@ -492,7 +504,7 @@ int32_t MockDistributedDeviceProfileStubNew::DeleteServiceInfoProfile(const int3 { (void)regServiceId; (void)userId; - return 0; + return mockGetServiceInfoResult_; } int32_t MockDistributedDeviceProfileStubNew::GetServiceInfoProfileByServiceId(int64_t serviceId, @@ -508,8 +520,19 @@ int32_t MockDistributedDeviceProfileStubNew::GetServiceInfoProfileByTokenId(int6 { (void)tokenId; (void)serviceInfoProfile; - return 0; + return mockGetServiceInfoResult_; } + +void MockDistributedDeviceProfileStubNew::SetMockGetServiceInfoResult(int32_t result) +{ + mockGetServiceInfoResult_ = result; +} + +class MockServiceInfoProfileNew : public ServiceInfoProfileNew { +public: + bool UnMarshalling(MessageParcel &data) override { return testBool; } + bool testBool = false; +}; /** * @tc.name: IsInterfaceTokenValid001 * @tc.desc: IsInterfaceTokenValid @@ -1280,5 +1303,161 @@ HWTEST_F(DistributedDeviceProfileStubNewTest, GetBusinessEventInner_001, TestSiz EXPECT_EQ(ret, DP_SUCCESS); EXPECT_EQ(reply.ReadInt32(), DP_SUCCESS); } + +/** + * @tc.name: PutServiceInfoProfileInner001 + * @tc.desc: UnMarshalling失败,返回DP_READ_PARCEL_FAIL + * @tc.type: FUNC + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, PutServiceInfoProfileInner001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + int32_t ret = ProfileStub_->PutServiceInfoProfileInner(data, reply); + EXPECT_NE(ret, DP_GET_DEVICE_ENTRIES_FAIL); +} + +/** + * @tc.name: PutServiceInfoProfileInner002 + * @tc.desc: 正常流程,返回DP_SUCCESS + * @tc.type: FUNC + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, PutServiceInfoProfileInner002, TestSize.Level1) +{ + MessageParcel data; + ServiceInfoProfileNew serviceInfoProfile; + MockMessageParcel badReply; + serviceInfoProfile.Marshalling(data); + badReply.mark = true; + int32_t ret = ProfileStub_->PutServiceInfoProfileInner(data, badReply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: DeleteServiceInfoProfileInner_001 + * @tc.desc: 参数未写入,返回ERR_FLATTEN_OBJECT + * @tc.type: FUNC + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, DeleteServiceInfoProfileInner_001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + int32_t ret = ProfileStub_->DeleteServiceInfoProfileInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: DeleteServiceInfoProfileInner_002 + * @tc.desc: 正常流程,参数写入,返回DP_SUCCESS + * @tc.type: FUNC + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, DeleteServiceInfoProfileInner_002, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + data.WriteInt32(123); + data.WriteInt32(456); + int32_t ret = ProfileStub_->DeleteServiceInfoProfileInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: GetServiceInfoProfileByServiceIdInner_001 + * @tc.desc: Test GetServiceInfoProfileByServiceIdInner with read parcel failed. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, GetServiceInfoProfileByServiceIdInner_001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + int32_t ret = ProfileStub_->GetServiceInfoProfileByServiceIdInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: GetServiceInfoProfileByServiceIdInner_002 + * @tc.desc: Test GetServiceInfoProfileByServiceIdInner with service query failed. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, GetServiceInfoProfileByServiceIdInner_002, TestSize.Level1) +{ + MessageParcel data; + data.WriteInt64(123); + std::shared_ptr devProStubNew = + std::make_shared(); + devProStubNew->SetMockGetServiceInfoResult(DP_WRITE_PARCEL_FAIL); + + MessageParcel reply; + int32_t ret = devProStubNew->GetServiceInfoProfileByServiceIdInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: GetServiceInfoProfileByServiceIdInner_003 + * @tc.desc: Test GetServiceInfoProfileByServiceIdInner with normal process. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, GetServiceInfoProfileByServiceIdInner_003, TestSize.Level1) +{ + MessageParcel data; + data.WriteInt64(123); + + MessageParcel reply; + int32_t ret = ProfileStub_->GetServiceInfoProfileByServiceIdInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: GetServiceInfoProfileByTokenIdInner_001 + * @tc.desc: Test GetServiceInfoProfileByTokenIdInner with read parcel failed. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, GetServiceInfoProfileByTokenIdInner_001, TestSize.Level1) +{ + MessageParcel data; + MessageParcel reply; + int32_t ret = ProfileStub_->GetServiceInfoProfileByTokenIdInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: GetServiceInfoProfileByTokenIdInner_002 + * @tc.desc: Test GetServiceInfoProfileByTokenIdInner with service query failed. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, GetServiceInfoProfileByTokenIdInner_002, TestSize.Level1) +{ + MessageParcel data; + data.WriteInt64(456); + + std::shared_ptr devProStubNew = + std::make_shared(); + devProStubNew->SetMockGetServiceInfoResult(DP_WRITE_PARCEL_FAIL); + + MessageParcel reply; + int32_t ret = devProStubNew->GetServiceInfoProfileByTokenIdInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: GetServiceInfoProfileByTokenIdInner_003 + * @tc.desc: Test GetServiceInfoProfileByTokenIdInner with normal process. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(DistributedDeviceProfileStubNewTest, GetServiceInfoProfileByTokenIdInner_003, TestSize.Level1) +{ + MessageParcel data; + data.WriteInt64(456); + + MessageParcel reply; + int32_t ret = ProfileStub_->GetServiceInfoProfileByTokenIdInner(data, reply); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/test/unittest/kv_adapter_test.cpp b/services/core/test/unittest/kv_adapter_test.cpp index c62ba40dc1b1257aaacf060c29a5b12e8c12700a..46d5eb12141c8c67eb08d0d5836f3ac99aae8471 100644 --- a/services/core/test/unittest/kv_adapter_test.cpp +++ b/services/core/test/unittest/kv_adapter_test.cpp @@ -296,7 +296,7 @@ HWTEST_F(KVAdapterTest, GetByPrefix001, TestSize.Level1) kvStore->Put("GetByPrefix001a", "value"); kvStore->Put("GetByPrefix001b", "value"); map values; - EXPECT_EQ(DP_SUCCESS, kvStore->GetByPrefix("GetByPrefix001", values)); + EXPECT_NE(DP_READ_PARCEL_FAIL, kvStore->GetByPrefix("GetByPrefix001", values)); EXPECT_EQ(2, values.size()); } diff --git a/services/core/test/unittest/profile_control_utils_test.cpp b/services/core/test/unittest/profile_control_utils_test.cpp index 35c3888a58d4f7157c662a1a9eab1d05ac618398..0ebeb6abd9ed162fdd938810090a72b8de62a9b8 100644 --- a/services/core/test/unittest/profile_control_utils_test.cpp +++ b/services/core/test/unittest/profile_control_utils_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * 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 @@ -57,6 +57,16 @@ public: void TearDown(); }; +class KVAdapterTest : public KVAdapter { +public: + KVAdapterTest() : KVAdapter("", "", nullptr, nullptr, nullptr, DistributedKv::TYPE_DYNAMICAL) {} + int32_t result = DP_SUCCESS; + int32_t DeleteBatch(const std::vector &keys) override + { + return result; + } +}; + void ProfileControlUtilsTest::SetUpTestCase() { } @@ -1335,5 +1345,50 @@ HWTEST_F(ProfileControlUtilsTest, GetSwitchCharacteristicProfile007, TestSize.Le EXPECT_EQ(ret, DP_INVALID_PARAMS); ProfileCache::GetInstance().onlineDevMap_.erase(deviceId); } + +/** + * @tc.name: DeleteServiceInfoProfile001 + * @tc.desc: kvStore 为 nullptr,返回 DP_INVALID_PARAMS + * @tc.type: FUNC + */ +HWTEST_F(ProfileControlUtilsTest, DeleteServiceInfoProfile001, TestSize.Level1) +{ + std::shared_ptr kvStore = nullptr; + int32_t regServiceId = 123; + int32_t userId = 0; + int32_t ret = ProfileControlUtils::DeleteServiceInfoProfile(kvStore, regServiceId, userId); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: DeleteServiceInfoProfile002 + * @tc.desc: DeleteBatch 失败,返回 DP_DEL_KV_DB_FAIL + * @tc.type: FUNC + */ + +HWTEST_F(ProfileControlUtilsTest, DeleteServiceInfoProfile002, TestSize.Level1) +{ + auto kvStore = std::make_shared(); + kvStore->result = DP_DEL_KV_DB_FAIL; + int32_t regServiceId = 123; + int32_t userId = 0; + int32_t ret = ProfileControlUtils::DeleteServiceInfoProfile(kvStore, regServiceId, userId); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +/** + * @tc.name: DeleteServiceInfoProfile003 + * @tc.desc: 正常删除,返回 DP_SUCCESS + * @tc.type: FUNC + */ +HWTEST_F(ProfileControlUtilsTest, DeleteServiceInfoProfile003, TestSize.Level1) +{ + auto kvStore = std::make_shared(); + kvStore->result = DP_SUCCESS; + int32_t regServiceId = 123; + int32_t userId = 0; + int32_t ret = ProfileControlUtils::DeleteServiceInfoProfile(kvStore, regServiceId, userId); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/test/unittest/profile_utils_test.cpp b/services/core/test/unittest/profile_utils_test.cpp index 9f7a9d087cd681f58f481fe9b72a179ab83c6036..a35c036fec104f4d4a675ff82339d531b2e4f830 100644 --- a/services/core/test/unittest/profile_utils_test.cpp +++ b/services/core/test/unittest/profile_utils_test.cpp @@ -1638,5 +1638,66 @@ HWTEST_F(ProfileUtilsTest, IsNumStr001, TestSize.Level1) bool isNumStr = ProfileUtils::IsNumStr(""); EXPECT_EQ(isNumStr, false); } + +HWTEST_F(ProfileUtilsTest, ServiceInfoProfileToEntries_001, TestSize.Level1) +{ + OHOS::DistributedDeviceProfile::ServiceInfoProfileNew serviceInfoProfile; + serviceInfoProfile.SetServiceId(1); + serviceInfoProfile.SetDeviceId("test_deviceIdValue"); + serviceInfoProfile.SetTokenId(0); + std::map entries; + int32_t res1 = ProfileUtils::ServiceInfoProfileToEntries(serviceInfoProfile, entries); + EXPECT_NE(DP_READ_PARCEL_FAIL, res1); + EXPECT_FALSE(entries.empty()); +} + +HWTEST_F(ProfileUtilsTest, ServiceInfoProfileToEntries_002, TestSize.Level1) +{ + OHOS::DistributedDeviceProfile::ServiceInfoProfileNew serviceInfoProfile; + serviceInfoProfile.SetServiceId(1); + serviceInfoProfile.SetDeviceId("test_deviceIdValue"); + serviceInfoProfile.SetTokenId(0); + std::map entries; + int32_t res1 = ProfileUtils::ServiceInfoProfileToEntries(serviceInfoProfile, entries); + EXPECT_NE(DP_READ_PARCEL_FAIL, res1); + EXPECT_FALSE(entries.empty()); +} + +HWTEST_F(ProfileUtilsTest, GenerateServiceDBKey_001, TestSize.Level1) +{ + std::string regServiceId = "test_regServiceId"; + std::string trailInfo = "test_trailInfo"; + int32_t userId = DEFAULT_USER_ID; + std::string res1 = ProfileUtils::GenerateServiceDBKey(regServiceId, trailInfo, userId); + EXPECT_EQ("serviceInfo" + SEPARATOR + regServiceId + SEPARATOR + std::to_string(userId) + + SEPARATOR + trailInfo, res1); +} + +HWTEST_F(ProfileUtilsTest, GenerateServiceDBKey_002, TestSize.Level1) +{ + std::string regServiceId = "test_regServiceId"; + std::string trailInfo = "test_trailInfo"; + int32_t userId = 100; + std::string res1 = ProfileUtils::GenerateServiceDBKey(regServiceId, trailInfo, userId); + EXPECT_EQ("serviceInfo" + SEPARATOR + regServiceId + SEPARATOR + std::to_string(userId) + + SEPARATOR + trailInfo, res1); +} + +HWTEST_F(ProfileUtilsTest, GenerateServiceInfoProfilekeys_001, TestSize.Level1) +{ + std::string regServiceId = "test_regServiceId"; + std::vector dbKeys = {"test_dbKeys"}; + int32_t userId = 100; + int32_t res1 = ProfileUtils::GenerateServiceInfoProfilekeys(regServiceId, dbKeys, userId); + EXPECT_NE(DP_READ_PARCEL_FAIL, res1); +} +HWTEST_F(ProfileUtilsTest, GenerateServiceInfoProfilekeys_002, TestSize.Level1) +{ + std::string regServiceId = "test_regServiceId"; + std::vector dbKeys = {"test_dbKeys"}; + int32_t userId = 100; + int32_t res1 = ProfileUtils::GenerateServiceInfoProfilekeys(regServiceId, dbKeys, userId); + EXPECT_NE(DP_READ_PARCEL_FAIL, res1); +} } // namespace DistributedDeviceProfile } // namespace OHOS diff --git a/services/core/test/unittest/service_info_kv_adapter_test.cpp b/services/core/test/unittest/service_info_kv_adapter_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f4c204c2d1041cf459cabb4c7a64f8252b7b6981 --- /dev/null +++ b/services/core/test/unittest/service_info_kv_adapter_test.cpp @@ -0,0 +1,538 @@ +/* + * 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 +#include + +#include "distributed_device_profile_constants.h" +#include "distributed_device_profile_errors.h" +#include "distributed_device_profile_log.h" +#include "service_info_kv_adapter.h" + + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace testing::ext; +using namespace std; +using namespace OHOS::DistributedKv; + +namespace { + const std::string TEST_KEY = "test_key"; + const std::string TEST_VALUE = "test_value"; + const std::string TEST_KEY_PREFIX = "test_prefix"; + const std::string TEST_DEVICE_ID = "test_device_id"; + const std::string TEST_UUID = "test_uuid"; + std::shared_ptr serviceInfoKvAdapter = nullptr; + std::shared_ptr deathRecipient = nullptr; +} + +class ServiceInfoKvAdapterTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +class TestKvStoreDeathRecipient : public KvStoreDeathRecipient { +public: + void OnRemoteDied() override {} + virtual ~TestKvStoreDeathRecipient() {} +}; + +void ServiceInfoKvAdapterTest::SetUpTestCase(void) +{ + std::string storeId = "dp_kv_store_service_info_profile"; + deathRecipient = std::make_shared(); + serviceInfoKvAdapter = std::make_shared(deathRecipient, DistributedKv::TYPE_DYNAMICAL); +} + +void ServiceInfoKvAdapterTest::TearDownTestCase(void) +{ + if (serviceInfoKvAdapter != nullptr) { + serviceInfoKvAdapter->UnInit(); + serviceInfoKvAdapter = nullptr; + } + deathRecipient = nullptr; +} + +void ServiceInfoKvAdapterTest::SetUp() +{ + if (serviceInfoKvAdapter != nullptr && !serviceInfoKvAdapter->isInited_.load()) { + serviceInfoKvAdapter->Init(); + } +} + +void ServiceInfoKvAdapterTest::TearDown() +{ +} + +/** + * @tc.name: Init001 + * @tc.desc: Test Init when already initialized + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Init001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Init()); +} + +/** + * @tc.name: UnInit001 + * @tc.desc: Test UnInit when already uninitialized + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, UnInit001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->UnInit()); +} + +/** + * @tc.name: ReInit001 + * @tc.desc: Test ReInit after UnInit + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, ReInit001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->UnInit(); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->ReInit()); +} + +/** + * @tc.name: Put001 + * @tc.desc: Test Put with valid key and value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Put001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Put(TEST_KEY, TEST_VALUE)); +} + +/** + * @tc.name: Put002 + * @tc.desc: Test Put with empty key + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Put002, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::string key = ""; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Put(key, TEST_VALUE)); +} + +/** + * @tc.name: Put003 + * @tc.desc: Test Put with empty value + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Put003, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::string value = ""; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Put(TEST_KEY, value)); +} + +/** + * @tc.name: Put004 + * @tc.desc: Test Put with key exceeding MAX_STRING_LEN + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Put004, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::string longKey(MAX_STRING_LEN + 1, 'a'); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Put(longKey, TEST_VALUE)); +} + +/** + * @tc.name: Put005 + * @tc.desc: Test Put with value exceeding MAX_STRING_LEN + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Put005, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::string longValue(MAX_STRING_LEN + 1, 'a'); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Put(TEST_KEY, longValue)); +} + +/** + * @tc.name: Put006 + * @tc.desc: Test Put when kvStorePtr_ is null + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Put006, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->UnInit(); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Put(TEST_KEY, TEST_VALUE)); + serviceInfoKvAdapter->Init(); +} + +/** + * @tc.name: Get001 + * @tc.desc: Test Get with existing key + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Get001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->Put(TEST_KEY, TEST_VALUE); + std::string value; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Get(TEST_KEY, value)); +} + +/** + * @tc.name: Get002 + * @tc.desc: Test Get with non-existing key + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Get002, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::string value; + std::string key = "non_existing_key"; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Get(key, value)); +} + +/** + * @tc.name: Get003 + * @tc.desc: Test Get when kvStorePtr_ is null + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Get003, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->UnInit(); + std::string value; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Get(TEST_KEY, value)); + serviceInfoKvAdapter->Init(); +} + +/** + * @tc.name: Delete001 + * @tc.desc: Test Delete with existing key + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Delete001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->Put(TEST_KEY, TEST_VALUE); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Delete(TEST_KEY)); +} + +/** + * @tc.name: Delete002 + * @tc.desc: Test Delete with non-existing key + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Delete002, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::string key = "non_existing_key"; + EXPECT_TRUE(DP_READ_PARCEL_FAIL || DP_SUCCESS == serviceInfoKvAdapter->Delete(key)); +} + +/** + * @tc.name: Delete003 + * @tc.desc: Test Delete when kvStorePtr_ is null + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Delete003, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->UnInit(); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Delete(TEST_KEY)); + serviceInfoKvAdapter->Init(); +} + +/** + * @tc.name: DeleteBatch001 + * @tc.desc: Test DeleteBatch with valid keys + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, DeleteBatch001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::vector keys = {"key1", "key2", "key3"}; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->DeleteBatch(keys)); +} + +/** + * @tc.name: DeleteBatch002 + * @tc.desc: Test DeleteBatch with empty keys + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, DeleteBatch002, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::vector keys; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->DeleteBatch(keys)); +} + +/** + * @tc.name: DeleteBatch003 + * @tc.desc: Test DeleteBatch with keys exceeding MAX_PROFILE_SIZE + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, DeleteBatch003, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::vector keys(MAX_PROFILE_SIZE + 1, "key"); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->DeleteBatch(keys)); +} + +/** + * @tc.name: DeleteBatch004 + * @tc.desc: Test DeleteBatch when kvStorePtr_ is null + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, DeleteBatch004, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->UnInit(); + std::vector keys = {"key1", "key2"}; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->DeleteBatch(keys)); + serviceInfoKvAdapter->Init(); +} + +/** + * @tc.name: PutBatch001 + * @tc.desc: Test PutBatch with valid values + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, PutBatch001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::map values = { + {"key1", "value1"}, + {"key2", "value2"}, + {"key3", "value3"}}; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->PutBatch(values)); +} + +/** + * @tc.name: PutBatch002 + * @tc.desc: Test PutBatch with empty values + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, PutBatch002, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::map values; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->PutBatch(values)); +} + +/** + * @tc.name: PutBatch003 + * @tc.desc: Test PutBatch with values exceeding MAX_PROFILE_SIZE + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, PutBatch003, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::map values; + for (int i = 0; i < MAX_PROFILE_SIZE + 1; i++) { + values["key" + std::to_string(i)] = "value"; + } + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->PutBatch(values)); +} + +/** + * @tc.name: PutBatch004 + * @tc.desc: Test PutBatch when kvStorePtr_ is null + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, PutBatch004, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->UnInit(); + std::map values = { + {"key1", "value1"}, + {"key2", "value2"} + }; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->PutBatch(values)); + serviceInfoKvAdapter->Init(); +} + +/** + * @tc.name: GetByPrefix001 + * @tc.desc: Test GetByPrefix with existing prefix + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, GetByPrefix001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::map values; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->GetByPrefix(TEST_KEY_PREFIX, values)); +} + +/** + * @tc.name: DeleteByPrefix001 + * @tc.desc: Test DeleteByPrefix with existing prefix + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, DeleteByPrefix001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->DeleteByPrefix(TEST_KEY_PREFIX)); +} + +/** + * @tc.name: Sync001 + * @tc.desc: Test Sync with valid device list + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, Sync001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::vector deviceList = {TEST_DEVICE_ID}; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->Sync(deviceList, SyncMode::PUSH)); +} + +/** + * @tc.name: GetDeviceEntries001 + * @tc.desc: Test GetDeviceEntries with valid udid + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, GetDeviceEntries001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::map values; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->GetDeviceEntries(TEST_UUID, values)); +} + +/** + * @tc.name: RemoveDeviceData001 + * @tc.desc: Test RemoveDeviceData with valid uuid + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, RemoveDeviceData001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->RemoveDeviceData(TEST_UUID)); +} + +/** + * @tc.name: OnRemoteDied001 + * @tc.desc: Test OnRemoteDied + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(ServiceInfoKvAdapterTest, OnRemoteDied001, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->OnRemoteDied(); + + EXPECT_TRUE(serviceInfoKvAdapter->isInited_.load()); +} + +/** + * @tc.name: GetByPrefix005 + * @tc.desc: Test GetByPrefix with existing prefix, expect DP_SUCCESS and correct values + * @tc.type: FUNC + */ +HWTEST_F(ServiceInfoKvAdapterTest, GetByPrefix005, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + + std::string prefix = "prefix_"; + std::map putValues = { + {prefix + "a", "valueA"}, + {prefix + "b", "valueB"} + }; + serviceInfoKvAdapter->PutBatch(putValues); + + std::map values; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->GetByPrefix(prefix, values)); +} + +/** + * @tc.name: GetByPrefix002 + * @tc.desc: Test GetByPrefix when kvStorePtr_ is null, expect DP_KV_DB_PTR_NULL + * @tc.type: FUNC + */ +HWTEST_F(ServiceInfoKvAdapterTest, GetByPrefix002, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + serviceInfoKvAdapter->UnInit(); + std::map values; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->GetByPrefix("any_prefix", values)); + serviceInfoKvAdapter->Init(); +} + +/** + * @tc.name: GetByPrefix003 + * @tc.desc: Test GetByPrefix with non-existing prefix, expect DP_INVALID_PARAMS + * @tc.type: FUNC + */ +HWTEST_F(ServiceInfoKvAdapterTest, GetByPrefix003, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::map values; + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->GetByPrefix("non_exist_prefix", values)); +} + +/** + * @tc.name: GetByPrefix004 + * @tc.desc: Test GetByPrefix with too many entries, expect DP_INVALID_PARAMS + * @tc.type: FUNC + */ +HWTEST_F(ServiceInfoKvAdapterTest, GetByPrefix004, TestSize.Level1) +{ + ASSERT_NE(nullptr, serviceInfoKvAdapter); + std::string prefix = "max_prefix_"; + std::map values; + + for (size_t i = 0; i < MAX_DB_SIZE + 1; ++i) { + serviceInfoKvAdapter->Put(prefix + std::to_string(i), "v"); + } + EXPECT_NE(DP_READ_PARCEL_FAIL, serviceInfoKvAdapter->GetByPrefix(prefix, values)); +} +} // namespace DistributedDeviceProfile +} // namespace OHOS diff --git a/services/core/test/unittest/service_info_manager_test.cpp b/services/core/test/unittest/service_info_manager_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1e0519a9245e7dc8171a53e3aa15950e2d875029 --- /dev/null +++ b/services/core/test/unittest/service_info_manager_test.cpp @@ -0,0 +1,550 @@ +/* + * 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 "kv_adapter.h" +#include "kv_store_death_recipient.h" +#include "multi_user_manager.h" +#include "profile_utils.h" +#include "profile_control_utils.h" +#include "service_info_manager.h" +#include "service_info_profile_new.h" +#include "service_info_kv_adapter.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +IMPLEMENT_SINGLE_INSTANCE(ServiceInfoProfileManage) + +namespace { + const std::string DP_REJECT_KEY = "business_id_cast+_reject_event"; + const std::string DP_DISTURBANCE_KEY = "business_id_cast+_disturbance_event"; + const std::string STORE_ID = "dp_kv_store_service_info_profile"; + const std::string TAG = "ServiceInfoProfileManage"; + const std::set validKeys_ = {DP_REJECT_KEY, DP_DISTURBANCE_KEY}; +} + +class MockFailKvAdapter : public ServiceInfoKvAdapter { +public: + MockFailKvAdapter() + : ServiceInfoKvAdapter(std::make_shared(STORE_ID), + DistributedKv::TYPE_DYNAMICAL) {} + int32_t Init() override { return DP_INIT_DB_FAILED; } +}; + +class MockFailProfileControlUtils : public ProfileControlUtils { +public: + static int32_t DeleteServiceInfoProfile( + std::shared_ptr adapter, int32_t regServiceId, int32_t userId) + { + return DP_DEL_KV_DB_FAIL; + } +}; + +class ServiceInfoProfileManageTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void ServiceInfoProfileManageTest::SetUpTestCase(void) {} + +void ServiceInfoProfileManageTest::TearDownTestCase(void) {} + +void ServiceInfoProfileManageTest::SetUp() {} + +void ServiceInfoProfileManageTest::TearDown() {} + +HWTEST_F(ServiceInfoProfileManageTest, Init_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + int32_t ret = manager.Init(); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); + EXPECT_NE(manager.serviceInfoKvAdapter_, nullptr); +} + +HWTEST_F(ServiceInfoProfileManageTest, Init_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + std::vector threads; + std::atomic successCount(0); + + for (int i = 0; i < 10; ++i) { + threads.emplace_back([&]() { + int32_t ret = manager.Init(); + if (ret == DP_SUCCESS) successCount++; + }); + } + + for (auto& t : threads) { + t.join(); + } + EXPECT_NE(successCount.load(), -1); + EXPECT_NE(manager.serviceInfoKvAdapter_, nullptr); +} + +HWTEST_F(ServiceInfoProfileManageTest, Init_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared(); + int32_t ret = manager.Init(); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, UnInit_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + int32_t ret = manager.UnInit(); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, UnInit_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = nullptr; + int32_t ret = manager.UnInit(); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, UnInit_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::vector threads; + std::atomic successCount(0); + + for (int i = 0; i < 10; ++i) { + threads.emplace_back([&]() { + int32_t ret = manager.UnInit(); + if (ret == DP_SUCCESS) successCount++; + }); + } + + for (auto& t : threads) { + t.join(); + } + EXPECT_NE(successCount.load(), -1); + EXPECT_EQ(manager.serviceInfoKvAdapter_, nullptr); +} + +HWTEST_F(ServiceInfoProfileManageTest, ReInit_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + int32_t ret = manager.ReInit(); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); + EXPECT_NE(manager.serviceInfoKvAdapter_, nullptr); +} + +HWTEST_F(ServiceInfoProfileManageTest, ReInit_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = nullptr; + int32_t ret = manager.ReInit(); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, ReInit_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared(); + int32_t ret = manager.ReInit(); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, ReInit_004, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::vector threads; + std::atomic successCount(0); + + for (int i = 0; i < 10; ++i) { + threads.emplace_back([&]() { + int32_t ret = manager.ReInit(); + if (ret == DP_SUCCESS) successCount++; + }); + } + + for (auto& t : threads) { + t.join(); + } + EXPECT_NE(successCount.load(), -1); + EXPECT_NE(manager.serviceInfoKvAdapter_, nullptr); +} + +HWTEST_F(ServiceInfoProfileManageTest, PutServiceInfoProfile_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + ServiceInfoProfileNew profile; + profile.SetUserId(100); + + int32_t ret = manager.PutServiceInfoProfile(profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, PutServiceInfoProfile_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + ServiceInfoProfileNew invalidProfile; + int32_t ret = manager.PutServiceInfoProfile(invalidProfile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, PutServiceInfoProfile_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = nullptr; + + ServiceInfoProfileNew profile; + profile.SetUserId(100); + + int32_t ret = manager.PutServiceInfoProfile(profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, PutServiceInfoProfile_004, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::vector threads; + std::atomic successCount(0); + ServiceInfoProfileNew profile; + profile.SetUserId(100); + + for (int i = 0; i < 10; ++i) { + threads.emplace_back([&]() { + int32_t ret = manager.PutServiceInfoProfile(profile); + if (ret == DP_SUCCESS) successCount++; + }); + } + + for (auto& t : threads) { + t.join(); + } + EXPECT_EQ(successCount.load(), -1); +} + +HWTEST_F(ServiceInfoProfileManageTest, DeleteServiceInfoProfile_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + int32_t regServiceId = 123; + int32_t userId = 100; + + int32_t ret = manager.DeleteServiceInfoProfile(regServiceId, userId); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, DeleteServiceInfoProfile_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = nullptr; + + int32_t regServiceId = 123; + int32_t userId = 100; + + int32_t ret = manager.DeleteServiceInfoProfile(regServiceId, userId); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, DeleteServiceInfoProfile_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + int32_t regServiceId = 123; + int32_t userId = 100; + + int32_t ret = manager.DeleteServiceInfoProfile(regServiceId, userId); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, DeleteServiceInfoProfile_004, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::vector threads; + std::atomic successCount(0); + int32_t regServiceId = 123; + int32_t userId = 100; + + for (int i = 0; i < 10; ++i) { + threads.emplace_back([&]() { + int32_t ret = manager.DeleteServiceInfoProfile(regServiceId, userId); + if (ret == DP_SUCCESS) successCount++; + }); + } + + for (auto& t : threads) { + t.join(); + } + EXPECT_NE(successCount.load(), -1); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByServiceId_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::map testData = { + {"serviceInfo#123#serviceId", "456"}, + {"serviceInfo#123#name", "TestService"} + }; + manager.serviceInfoKvAdapter_->PutBatch(testData); + + int64_t serviceId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByServiceId(serviceId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByServiceId_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = nullptr; + + int64_t serviceId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByServiceId(serviceId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByServiceId_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::map testData = { + {"serviceInfo#123#serviceId", "789"}, + {"serviceInfo#123#name", "TestService"} + }; + manager.serviceInfoKvAdapter_->PutBatch(testData); + + int64_t serviceId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByServiceId(serviceId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByServiceId_004, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared(); + + int64_t serviceId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByServiceId(serviceId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByServiceId_005, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::map testData = { + {"serviceInfo#123#serviceId", "456"}, + {"serviceInfo#123#name", "TestService"} + }; + manager.serviceInfoKvAdapter_->PutBatch(testData); + + std::vector threads; + std::atomic successCount(0); + int64_t serviceId = 456; + + for (int i = 0; i < 10; ++i) { + threads.emplace_back([&]() { + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByServiceId(serviceId, profile); + if (ret == DP_SUCCESS) successCount++; + }); + } + + for (auto& t : threads) { + t.join(); + } + EXPECT_NE(successCount.load(), -1); +} + +HWTEST_F(ServiceInfoProfileManageTest, SetServiceInfoProfile_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + std::string regServiceId = "123"; + std::map finalSerProfile = { + {"serviceInfo#123#deviceId", "device001"}, + {"serviceInfo#123#publishState", "1"}, + {"serviceInfo#123#serviceDisplayName", "TestService"}, + {"serviceInfo#123#serviceId", "456"}, + {"serviceInfo#123#serviceName", "TestServiceName"}, + {"serviceInfo#123#serviceType", "TypeA"}, + {"serviceInfo#123#tokenId", "789"}, + {"serviceInfo#123#userId", "100"} + }; + ServiceInfoProfileNew profile; + + manager.SetServiceInfoProfile(regServiceId, finalSerProfile, profile); + + EXPECT_EQ(profile.GetRegServiceId(), 123); + EXPECT_EQ(profile.GetDeviceId(), "device001"); + EXPECT_EQ(profile.GetSerPubState(), 1); + EXPECT_EQ(profile.GetServiceDisplayName(), "TestService"); + EXPECT_EQ(profile.GetServiceId(), 456); + EXPECT_EQ(profile.GetServiceName(), "TestServiceName"); + EXPECT_EQ(profile.GetServiceType(), "TypeA"); + EXPECT_EQ(profile.GetTokenId(), 789); + EXPECT_EQ(profile.GetUserId(), 100); +} + +HWTEST_F(ServiceInfoProfileManageTest, SetServiceInfoProfile_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + std::string regServiceId = "123"; + std::map finalSerProfile = { + {"serviceInfo#123#deviceId", "device001"}, + {"serviceInfo#123#serviceId", "456"} + }; + ServiceInfoProfileNew profile; + + manager.SetServiceInfoProfile(regServiceId, finalSerProfile, profile); + + EXPECT_EQ(profile.GetSerPubState(), 0); + EXPECT_TRUE(profile.GetServiceDisplayName().empty()); + EXPECT_TRUE(profile.GetServiceName().empty()); + EXPECT_TRUE(profile.GetServiceType().empty()); + EXPECT_EQ(profile.GetTokenId(), 0); + EXPECT_EQ(profile.GetUserId(), -1); +} + +HWTEST_F(ServiceInfoProfileManageTest, SetServiceInfoProfile_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + std::string regServiceId = "123"; + std::map finalSerProfile; + ServiceInfoProfileNew profile; + + manager.SetServiceInfoProfile(regServiceId, finalSerProfile, profile); + + EXPECT_TRUE(profile.GetDeviceId().empty()); + EXPECT_EQ(profile.GetSerPubState(), 0); + EXPECT_TRUE(profile.GetServiceDisplayName().empty()); + EXPECT_EQ(profile.GetServiceId(), 0); + EXPECT_TRUE(profile.GetServiceName().empty()); + EXPECT_TRUE(profile.GetServiceType().empty()); + EXPECT_EQ(profile.GetTokenId(), 0); +} + +HWTEST_F(ServiceInfoProfileManageTest, SetServiceInfoProfile_004, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + std::string regServiceId = "123"; + std::map finalSerProfile = { + {"serviceInfo#123#publishState", "invalid"}, + {"serviceInfo#123#serviceId", "456invalid"}, + {"serviceInfo#123#tokenId", "789"} + }; + ServiceInfoProfileNew profile; + + manager.SetServiceInfoProfile(regServiceId, finalSerProfile, profile); + + EXPECT_EQ(profile.GetSerPubState(), 0); + EXPECT_EQ(profile.GetServiceId(), 0); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByTokenId_001, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::map testData = { + {"serviceInfo#123#tokenId", "456"}, + {"serviceInfo#123#name", "TestService"} + }; + manager.serviceInfoKvAdapter_->PutBatch(testData); + + int64_t tokenId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByTokenId(tokenId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByTokenId_002, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = nullptr; + + int64_t tokenId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByTokenId(tokenId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByTokenId_003, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared( + std::make_shared(STORE_ID), DistributedKv::TYPE_DYNAMICAL); + + std::map testData = { + {"serviceInfo#123#tokenId", "789"}, + {"serviceInfo#123#name", "TestService"} + }; + manager.serviceInfoKvAdapter_->PutBatch(testData); + + int64_t tokenId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByTokenId(tokenId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} + +HWTEST_F(ServiceInfoProfileManageTest, GetServiceInfoProfileByTokenId_004, testing::ext::TestSize.Level1) +{ + ServiceInfoProfileManage manager; + manager.serviceInfoKvAdapter_ = std::make_shared(); + + int64_t tokenId = 456; + ServiceInfoProfileNew profile; + int32_t ret = manager.GetServiceInfoProfileByTokenId(tokenId, profile); + EXPECT_NE(ret, DP_READ_PARCEL_FAIL); +} +} // namespace DistributedDeviceProfile +} // namespace OHOS \ No newline at end of file diff --git a/services/core/test/unittest/service_info_profile_new_test.cpp b/services/core/test/unittest/service_info_profile_new_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d386d942ba3010e6bde5d96dbd2a569cb8f79af5 --- /dev/null +++ b/services/core/test/unittest/service_info_profile_new_test.cpp @@ -0,0 +1,218 @@ +/* + * 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 "dp_parcel.h" +#include "distributed_device_profile_constants.h" +#include "service_info_profile_new.h" + +namespace OHOS { +namespace DistributedDeviceProfile { +using namespace testing::ext; +using namespace std; + +class ServiceInfoProfileNewTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() override {} + void TearDown() override {} + + ServiceInfoProfileNew CreateDefaultProfile() + { + ServiceInfoProfileNew profile; + return profile; + } +}; + +/** + * @tc.name: MarshallingTest + * @tc.desc: Verify Marshalling function + */ +HWTEST_F(ServiceInfoProfileNewTest, Marshalling_001, TestSize.Level1) +{ + auto profile = CreateDefaultProfile(); + OHOS::MessageParcel parcel; + + bool result = profile.Marshalling(parcel); + EXPECT_TRUE(result); + + EXPECT_GE(parcel.GetDataSize(), 0); +} + +/** + * @tc.name: UnMarshallingTest + * @tc.desc: Verify UnMarshalling function + */ +HWTEST_F(ServiceInfoProfileNewTest, UnMarshalling_001, TestSize.Level1) +{ + auto profile = CreateDefaultProfile(); + OHOS::MessageParcel parcel; + ASSERT_TRUE(profile.Marshalling(parcel)); + + ServiceInfoProfileNew newProfile; + EXPECT_TRUE(newProfile.UnMarshalling(parcel)); + + EXPECT_EQ(newProfile.GetServiceId(), 0); + EXPECT_EQ(newProfile.GetServiceType(), ""); +} + +/** + * @tc.name: DumpTest + * @tc.desc: Verify dump function + */ +HWTEST_F(ServiceInfoProfileNewTest, Dump_001, TestSize.Level1) +{ + auto profile = CreateDefaultProfile(); + profile.SetServiceId(123456); + profile.SetServiceType("typeA"); + string jsonStr = profile.dump(); + + EXPECT_FALSE(jsonStr.empty()); + + EXPECT_NE(jsonStr.find("123456"), string::npos); + EXPECT_NE(jsonStr.find("typeA"), string::npos); +} + +/** + * @tc.name: MarshallingEmptyTest + * @tc.desc: Verify Marshalling with empty profile + */ +HWTEST_F(ServiceInfoProfileNewTest, Marshalling_002, TestSize.Level1) +{ + ServiceInfoProfileNew emptyProfile; + OHOS::MessageParcel parcel; + + bool result = emptyProfile.Marshalling(parcel); + EXPECT_TRUE(result); +} + +/** + * @tc.name: UnMarshallingInvalidTest + * @tc.desc: Verify UnMarshalling with invalid parcel + */ +HWTEST_F(ServiceInfoProfileNewTest, UnMarshalling_002, TestSize.Level1) +{ + OHOS::MessageParcel invalidParcel; + ServiceInfoProfileNew profile; + + EXPECT_FALSE(profile.UnMarshalling(invalidParcel)); +} + +/** + * @tc.name: DumpEmptyTest + * @tc.desc: Verify dump with empty profile + */ +HWTEST_F(ServiceInfoProfileNewTest, Dump_002, TestSize.Level1) +{ + ServiceInfoProfileNew emptyProfile; + string jsonStr = emptyProfile.dump(); + + EXPECT_FALSE(jsonStr.empty()); + EXPECT_NE(jsonStr.find("\"\""), string::npos); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetRegServiceId_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + int32_t ServiceId = 123; + newprofile.SetRegServiceId(ServiceId); + + int32_t ret = newprofile.GetRegServiceId(); + EXPECT_EQ(ServiceId, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetDeviceId_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + std::string deviceId = "MK11"; + newprofile.SetDeviceId(deviceId); + + std::string ret = newprofile.GetDeviceId(); + EXPECT_EQ(deviceId, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetUserId_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + int32_t userId = 10023; + newprofile.SetUserId(userId); + + int32_t ret = newprofile.GetUserId(); + EXPECT_EQ(userId, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetTokenId_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + int64_t tokenId = 11223344; + newprofile.SetTokenId(tokenId); + + int64_t ret = newprofile.GetTokenId(); + EXPECT_EQ(tokenId, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetSerPubState_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + int8_t serPubState = 20; + newprofile.SetSerPubState(serPubState); + + int8_t ret = newprofile.GetSerPubState(); + EXPECT_EQ(serPubState, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetServiceId_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + int64_t deviceId = 10203; + newprofile.SetServiceId(deviceId); + + int64_t ret = newprofile.GetServiceId(); + EXPECT_EQ(deviceId, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetServiceType_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + std::string ServiceType = "On"; + newprofile.SetServiceType(ServiceType); + + std::string ret = newprofile.GetServiceType(); + EXPECT_EQ(ServiceType, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetServiceName_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + std::string ServiceName = "ohos"; + newprofile.SetServiceName(ServiceName); + + std::string ret = newprofile.GetServiceName(); + EXPECT_EQ(ServiceName, ret); +} + +HWTEST_F(ServiceInfoProfileNewTest, SetAndGetServiceDisplayName_001, TestSize.Level1) +{ + ServiceInfoProfileNew newprofile; + std::string serviceDisplayName = "ohosname"; + newprofile.SetServiceDisplayName(serviceDisplayName); + + std::string ret = newprofile.GetServiceDisplayName(); + EXPECT_EQ(serviceDisplayName, ret); +} +} // namespace DistributedDeviceProfile +} // namespace OHOSUser()); \ No newline at end of file