From 5f5bc72a9105eb8db239b52c2e1bf3fba235067c Mon Sep 17 00:00:00 2001 From: wangyingli Date: Sun, 6 Jul 2025 19:28:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6=E6=8B=89?= =?UTF-8?q?=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingli --- .../include/commonevent/back_search_event.h | 49 + .../commonevent/pull_extension_event.h | 58 + .../framework/include/eventcenter/event.h | 2 + .../service/data_share/BUILD.gn | 2 + .../common/search_callback_impl.cpp | 40 + .../data_share/common/search_callback_impl.h | 38 + .../common/search_connect_adaptor.cpp | 113 ++ .../common/search_connect_adaptor.h | 38 + .../data_share/data_share_service_impl.cpp | 30 + .../data_share/data_share_service_impl.h | 4 + .../service/test/BUILD.gn | 2 + .../service/test/data_share_common_test.cpp | 1224 +++++++++-------- .../datashareserviceimpl_fuzzer/BUILD.gn | 2 + .../datashareservicestub_fuzzer/BUILD.gn | 2 + 14 files changed, 1045 insertions(+), 559 deletions(-) create mode 100644 services/distributeddataservice/framework/include/commonevent/back_search_event.h create mode 100644 services/distributeddataservice/framework/include/commonevent/pull_extension_event.h create mode 100644 services/distributeddataservice/service/data_share/common/search_callback_impl.cpp create mode 100644 services/distributeddataservice/service/data_share/common/search_callback_impl.h create mode 100644 services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp create mode 100644 services/distributeddataservice/service/data_share/common/search_connect_adaptor.h diff --git a/services/distributeddataservice/framework/include/commonevent/back_search_event.h b/services/distributeddataservice/framework/include/commonevent/back_search_event.h new file mode 100644 index 000000000..a8b35b306 --- /dev/null +++ b/services/distributeddataservice/framework/include/commonevent/back_search_event.h @@ -0,0 +1,49 @@ +/* + * 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. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H + +#include "eventcenter/event.h" +#include "metadata/store_meta_data.h" +#include "visibility.h" +#include +#include +#include +#include + +namespace OHOS::DistributedData { +class API_EXPORT BackSearchEvent : public Event { +public: + struct BackInfo { + StoreMetaData meta; + std::string account; + }; + + BackSearchEvent(BackInfo backInfo, int32_t evtId = EVT_BACKSEARCH) + : Event(evtId), info_(backInfo) + { + } + const BackSearchEvent::BackInfo& GetInfo() const + { + return info_; + } + ~BackSearchEvent() override = default; + +private: + BackInfo info_; +}; +} // OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H diff --git a/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h b/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h new file mode 100644 index 000000000..008ad144e --- /dev/null +++ b/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h @@ -0,0 +1,58 @@ +/* + * 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. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H + +#include "eventcenter/event.h" +#include "metadata/store_meta_data.h" +#include "visibility.h" +#include +#include +#include + +namespace OHOS::DistributedData { +class API_EXPORT PullExtensionEvent : public Event { +public: + using StoreMetaData = DistributedData::StoreMetaData; + struct EventInfo { + StoreMetaData meta; + std::string Uri; + std::string accountId; + + std::shared_ptr mutex_; + std::shared_ptr condition_; + bool conditionMet_ = false; + + ~EventInfo() {} + }; + + PullExtensionEvent(EventInfo evtInfo, int32_t evtId = EVT_SEARCH) + : Event(evtId), info_(std::move(evtInfo)) + { + } + + ~PullExtensionEvent() override = default; + + const PullExtensionEvent::EventInfo& GetInfo() const + { + return info_; + } + +private: + EventInfo info_; +}; +} // OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H diff --git a/services/distributeddataservice/framework/include/eventcenter/event.h b/services/distributeddataservice/framework/include/eventcenter/event.h index 618f6fcb7..7aea323e1 100644 --- a/services/distributeddataservice/framework/include/eventcenter/event.h +++ b/services/distributeddataservice/framework/include/eventcenter/event.h @@ -31,6 +31,8 @@ public: EVT_CLOUD = 0x2000, EVT_BIND = 0X3000, EVT_CHANGE = 0X4000, + EVT_SEARCH = 0X5000, + EVT_BACKSEARCH = 0X6000 }; API_EXPORT Event(int32_t evtId); API_EXPORT Event(Event &&) noexcept = delete; diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index 68e54c60c..2f3851ca9 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -61,6 +61,8 @@ ohos_source_set("data_share_service") { "common/scheduler_manager.cpp", "common/seq_strategy.cpp", "common/uri_utils.cpp", + "common/search_connect_adaptor.cpp", + "common/search_callback_impl.cpp", "data/published_data.cpp", "data/resultset_json_formatter.cpp", "data/template_data.cpp", diff --git a/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp new file mode 100644 index 000000000..59cc81d7e --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp @@ -0,0 +1,40 @@ +/* + * 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 "ability_connect_callback_stub.h" +#include "block_data.h" +#include "iremote_object.h" +#include "datashare_log.h" +#include "search_callback_impl.h" + +namespace OHOS::DataShare { +void SearchCallbackImpl::OnAbilityConnectDone(const AppExecFwk::ElementName &element, + const sptr &remoteObject, int resultCode) +{ + std::unique_lock lock(connectingMtx_); + isConnectdone = true; + remoteObject_ = remoteObject; + connectCondition_.notify_all(); +} + +void SearchCallbackImpl::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + remoteObject_ = nullptr; +} + +sptr SearchCallbackImpl::GetRemoteObject() const +{ + return remoteObject_; +} +} diff --git a/services/distributeddataservice/service/data_share/common/search_callback_impl.h b/services/distributeddataservice/service/data_share/common/search_callback_impl.h new file mode 100644 index 000000000..00c4abb25 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_callback_impl.h @@ -0,0 +1,38 @@ +/* + * 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. + */ +#ifndef DATASHARESERVICE_SEARCH_CALLBACK_IMPL_H +#define DATASHARESERVICE_SEARCH_CALLBACK_IMPL_H + +#include "ability_connect_callback_stub.h" +#include "block_data.h" +#include "iremote_object.h" + +namespace OHOS::DataShare { +class SearchCallbackImpl : public AAFwk::AbilityConnectionStub { +public: + explicit SearchCallbackImpl() : remoteObject_(nullptr) {} + void OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; + sptr GetRemoteObject() const; + + std::mutex connectingMtx_; + std::condition_variable connectCondition_; + bool isConnectdone = true; +private: + sptr remoteObject_; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_SEARCH_CALLBACK_IMPL_H diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp new file mode 100644 index 000000000..e4d396a62 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp @@ -0,0 +1,113 @@ +/* + * 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. + */ +#define LOG_TAG "SearchConnectAdaptor" +#include "search_connect_adaptor.h" +#include +#include "app_connect_manager.h" +#include "search_callback_impl.h" +#include "extension_ability_manager.h" +#include "extension_ability_info.h" +#include "extension_mgr_proxy.h" +#include "log_print.h" +#include "uri_utils.h" +#include "itypes_util.h" + +namespace OHOS::DataShare { + +const std::u16string INTERFACE_TOKEN = u"OHOS.DataShare.IDataShare"; +static constexpr int REQUEST_CODE = 13; +constexpr int64_t WAITTIME = 1; + +sptr SearchConnectAdaptor::callback_ = nullptr; +SearchConnectAdaptor::SearchConnectAdaptor() +{ + callback_ = new (std::nothrow) SearchCallbackImpl(); +} + +bool SearchConnectAdaptor::DoConnect(const std::string &uri, const std::string &bundleName, + AAFwk::WantParams &wantParams) +{ + if (callback_ == nullptr) { + ZLOGE("The callback_ is nullptr"); + return false; + } + ErrCode ret = ExtensionMgrProxy::GetInstance()->Connect(uri, callback_->AsObject(), nullptr, wantParams); + if (ret != ERR_OK) { + ZLOGE("connect ability failed, ret = %{public}d, uri: %{public}s", ret, + URIUtils::Anonymous(uri).c_str()); + return false; + } + ZLOGI("Do connect, uri: %{public}s", URIUtils::Anonymous(uri).c_str()); + return true; +} + +std::pair SearchConnectAdaptor::Connect(const std::string &uri, const StoreMetaData &meta, + AAFwk::WantParams &wantParams, int maxWaitTime) +{ + bool isConnectdone; + { + std::unique_lock lock(callback_->connectingMtx_); + isConnectdone = callback_->isConnectdone; + } + if (isConnectdone) { + std::unique_lock lock(callback_->connectingMtx_); + callback_->isConnectdone = false; + } else { + std::unique_lock lock(connectMutex); + callback_->connectCondition_.wait_for(lock, std::chrono::seconds(WAITTIME)); + std::unique_lock lck(callback_->connectingMtx_); + callback_->isConnectdone = false; + } + + SearchConnectAdaptor strategy; + auto bundleName = meta.bundleName; + auto errCode = AppConnectManager::Wait( + bundleName, maxWaitTime, + [&uri, &bundleName, &strategy, &wantParams]() { + auto connectCode = strategy.DoConnect(uri, bundleName, wantParams); + return connectCode; + }, + [&strategy]() {}); + if (!errCode) { + ZLOGE("The wait errcode is:%{public}d, bundlename:%{public}s", errCode, meta.bundleName.c_str()); + } + MessageParcel reply; + MessageOption option; + MessageParcel data; + data.WriteInterfaceToken(INTERFACE_TOKEN); + Uri dataDir(meta.dataDir); + sptr remote = callback_->GetRemoteObject(); + if (remote == nullptr) { + ZLOGE("remote is null"); + return std::make_pair(false, ""); + } + Uri result(""); + int32_t err = remote->SendRequest(static_cast(REQUEST_CODE), data, reply, option); + if (err != ERR_NONE) { + ZLOGE("NormalizeUri fail to SendRequest. err: %{public}d", err); + return std::make_pair(false, ""); + } + Disconnect(); + return std::make_pair(true, result.ToString()); +} + +void SearchConnectAdaptor::Disconnect() +{ + if (callback_ == nullptr) { + return; + } + callback_ = nullptr; +} +} // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h new file mode 100644 index 000000000..0192f1c41 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h @@ -0,0 +1,38 @@ +/* + * 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. + */ + +#ifndef DATASHARESERVICE_SEARCH_CONNECT_ADAPTOR_H +#define DATASHARESERVICE_SEARCH_CONNECT_ADAPTOR_H + +#include "ability_connect_callback_interface.h" +#include "block_data.h" +#include "metadata/store_meta_data.h" +#include "search_callback_impl.h" + +namespace OHOS::DataShare { +class SearchConnectAdaptor { +public: + using StoreMetaData = DistributedData::StoreMetaData; + std::pair Connect(const std::string &uri, const StoreMetaData &meta, + AAFwk::WantParams &wantParams, int maxWaitTime = 1); + SearchConnectAdaptor(); +private: + void Disconnect(); + bool DoConnect(const std::string &uri, const std::string &bundleName, AAFwk::WantParams &wantParams); + static sptr callback_; + std::mutex connectMutex; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_SEARCH_CONNECT_ADAPTOR_H diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 4043d0511..8fd19e298 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -63,6 +63,8 @@ #include "proxy_data_manager.h" #include "datashare_observer.h" #include "subscriber_managers/proxy_data_subscriber_manager.h" +#include "search_connect_adaptor.h" +#include "commonevent/back_search_event.h" namespace OHOS::DataShare { using FeatureSystem = DistributedData::FeatureSystem; @@ -606,6 +608,7 @@ int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) SubscribeConcurrentTask(); SubscribeTimeChanged(); SubscribeChange(); + SubscribeSearch(); ZLOGI("end"); return E_OK; } @@ -652,6 +655,33 @@ void DataShareServiceImpl::SubscribeChange() }); } +void DataShareServiceImpl::SubscribeSearch() +{ + EventCenter::GetInstance().Subscribe(PullExtensionEvent::EVT_SEARCH, [this](const Event &event) { + auto &evt = static_cast(event); + auto eventInfo = evt.GetInfo(); + PullService(eventInfo); + }); +} + +void DataShareServiceImpl::PullService(PullExtensionEvent::EventInfo &eventInfo) +{ + const StoreMetaData &meta = eventInfo.meta; + const std::string &uri = eventInfo.Uri; + AAFwk::WantParams wantParams; + SearchConnectAdaptor adaptor; + auto[errcode, reply] = adaptor.Connect(uri, meta, wantParams); + if (!errcode) { + ZLOGE("Pull failed, bundlename:%{public}s", meta.bundleName.c_str()); + return; + } + BackSearchEvent::BackInfo backInfo; + backInfo.meta = meta; + backInfo.account = reply; + auto evt = std::make_unique(std::move(backInfo), Event::EVT_BACKSEARCH); + EventCenter::GetInstance().PostEvent(std::move(evt)); +} + void DataShareServiceImpl::SaveLaunchInfo(const std::string &bundleName, const std::string &userId, const std::string &deviceId) { diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index d39912924..5cfd63b6e 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -41,10 +41,12 @@ #include "template_strategy.h" #include "uri_utils.h" #include "visibility.h" +#include "commonevent/pull_extension_event.h" namespace OHOS::DataShare { class DataShareServiceImpl : public DataShareServiceStub { public: + using StoreMetaData = DistributedData::StoreMetaData; using Handler = std::function> &)>; using ExecuteCallbackEx = std::function(DataProviderConfig::ProviderInfo &, DistributedData::StoreMetaData &, std::shared_ptr)>; @@ -138,7 +140,9 @@ private: void SubscribeConcurrentTask(); static void InitSubEvent(); void AutoLaunch(const DistributedData::Event &event); + void PullService(DistributedData::PullExtensionEvent::EventInfo &eventInfo); void SubscribeChange(); + void SubscribeSearch(); bool AllowCleanDataLaunchApp(const DistributedData::Event &event, bool launchForCleanData); static void SaveLaunchInfo(const std::string &bundleName, const std::string &userId, const std::string &deviceId); diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 756f5fa4d..00744a02d 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1031,6 +1031,8 @@ ohos_unittest("DataShareServiceImplTest") { "${data_service_path}/service/data_share/common/div_strategy.cpp", "${data_service_path}/service/data_share/common/extension_ability_manager.cpp", "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_callback_impl.cpp", "${data_service_path}/service/data_share/common/extension_mgr_proxy.cpp", "${data_service_path}/service/data_share/common/proxy_data_manager.cpp", "${data_service_path}/service/data_share/common/kv_delegate.cpp", diff --git a/services/distributeddataservice/service/test/data_share_common_test.cpp b/services/distributeddataservice/service/test/data_share_common_test.cpp index a6db0130b..cba33e7fb 100644 --- a/services/distributeddataservice/service/test/data_share_common_test.cpp +++ b/services/distributeddataservice/service/test/data_share_common_test.cpp @@ -1,576 +1,593 @@ -/* -* 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. -*/ -#define LOG_TAG "DataShareCommonTest" - -#include -#include -#include "extension_connect_adaptor.h" -#include "data_share_profile_config.h" -#include "div_strategy.h" -#include "log_print.h" -#include "rdb_delegate.h" -#include "rdb_subscriber_manager.h" -#include "scheduler_manager.h" -#include "seq_strategy.h" -#include "strategy.h" - -namespace OHOS::Test { -using namespace testing::ext; -using namespace OHOS::DataShare; -class DataShareCommonTest : public testing::Test { -public: - static constexpr int64_t USER_TEST = 100; - static void SetUpTestCase(void){}; - static void TearDownTestCase(void){}; - void SetUp(){}; - void TearDown(){}; -}; - -/** -* @tc.name: DivStrategy001 -* @tc.desc: test DivStrategy function when three parameters are all nullptr -* @tc.type: FUNC -* @tc.require:SQL -*/ -HWTEST_F(DataShareCommonTest, DivStrategy001, TestSize.Level1) -{ - ZLOGI("DataShareCommonTest DivStrategy001 start"); - std::shared_ptr check = nullptr; - std::shared_ptr trueAction = nullptr; - std::shared_ptr falseAction = nullptr; - DivStrategy divStrategy(check, trueAction, falseAction); - auto context = std::make_shared(); - bool result = divStrategy.operator()(context); - EXPECT_EQ(result, false); - ZLOGI("DataShareCommonTest DivStrategy001 end"); -} - -/** -* @tc.name: DivStrategy002 -* @tc.desc: test DivStrategy function when trueAction and falseAction are nullptr -* @tc.type: FUNC -* @tc.require:SQL -*/ -HWTEST_F(DataShareCommonTest, DivStrategy002, TestSize.Level1) -{ - ZLOGI("DataShareCommonTest DivStrategy002 start"); - std::shared_ptr check = std::make_shared(); - std::shared_ptr trueAction = nullptr; - std::shared_ptr falseAction = nullptr; - DivStrategy divStrategy(check, trueAction, falseAction); - auto context = std::make_shared(); - bool result = divStrategy.operator()(context); - EXPECT_EQ(result, false); - ZLOGI("DataShareCommonTest DivStrategy002 end"); -} - -/** -* @tc.name: DivStrategy003 -* @tc.desc: test DivStrategy function when only falseAction is nullptr -* @tc.type: FUNC -* @tc.require:SQL -*/ -HWTEST_F(DataShareCommonTest, DivStrategy003, TestSize.Level1) -{ - ZLOGI("DataShareCommonTest DivStrategy003 start"); - std::shared_ptr check = std::make_shared(); - std::shared_ptr trueAction = std::make_shared(); - std::shared_ptr falseAction = nullptr; - DivStrategy divStrategy(check, trueAction, falseAction); - auto context = std::make_shared(); - bool result = divStrategy.operator()(context); - EXPECT_EQ(result, false); - ZLOGI("DataShareCommonTest DivStrategy003 end"); -} - -/** -* @tc.name: SeqStrategyInit001 -* @tc.desc: test SeqStrategyInit function when item is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a seqStrategy object and strategy2 = nullptr - 2.call Init function -* @tc.experct: Init failed and return false -*/ -HWTEST_F(DataShareCommonTest, SeqStrategyInit001, TestSize.Level1) -{ - ZLOGI("SeqStrategyInit001 start"); - Strategy* strategy1 = new Strategy(); - Strategy* strategy2 = nullptr; - SeqStrategy seqStrategy; - bool result = seqStrategy.Init({strategy1, strategy2}); - EXPECT_FALSE(result); - delete strategy1; - ZLOGI("SeqStrategyInit001 end"); -} - -/** -* @tc.name: DoConnect001 -* @tc.desc: test DoConnect function when callback_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a ExtensionConnectAdaptor object and callback = nullptr - 2.call DoConnect function -* @tc.experct: DoConnect failed and return false -*/ -HWTEST_F(DataShareCommonTest, DoConnect001, TestSize.Level1) -{ - ZLOGI("DoConnect001 start"); - std::string uri = "testUri"; - std::string bundleName = "testBundle"; - AAFwk::WantParams wantParams; - ExtensionConnectAdaptor extensionConnectAdaptor; - bool result = extensionConnectAdaptor.DoConnect(uri, bundleName, wantParams); - EXPECT_FALSE(result); - ZLOGI("DoConnect001 end"); -} - -/** -* @tc.name: InsertEx001 -* @tc.desc: test InsertEx function when store_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a rdbDelegate object and store_ = nullptr - 2.call InsertEx function -* @tc.experct: InsertEx failed and return E_DB_ERROR -*/ -HWTEST_F(DataShareCommonTest, InsertEx001, TestSize.Level1) -{ - ZLOGI("InsertEx001 start"); - DistributedData::StoreMetaData metaData; - metaData.user = "100"; - metaData.bundleName = "test"; - metaData.area = 1; - int version = 0; - bool registerFunction = false; - std::string extUri = "uri"; - std::string backup = "backup"; +/* +* 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. +*/ +#define LOG_TAG "DataShareCommonTest" + +#include +#include +#include "extension_connect_adaptor.h" +#include "search_connect_adaptor.h" +#include "data_share_profile_config.h" +#include "div_strategy.h" +#include "log_print.h" +#include "rdb_delegate.h" +#include "rdb_subscriber_manager.h" +#include "scheduler_manager.h" +#include "seq_strategy.h" +#include "strategy.h" +#include "metadata/store_meta_data.h" + +namespace OHOS::Test { +using namespace testing::ext; +using namespace OHOS::DataShare; +using StoreMetaData = DistributedData::StoreMetaData; + +class MockCallback : public SearchCallbackImpl { +public: + MockCallback() = default; + ~MockCallback() = default; +}; + +class DataShareCommonTest : public testing::Test { +public: + static constexpr int64_t USER_TEST = 100; + static void SetUpTestCase(void){}; + static void TearDownTestCase(void){}; + void SetUp() override + { + adaptor_ = std::make_shared(); + callback_ = new MockCallback(); + adaptor_->callback_ = callback_; + }; + void TearDown(){}; + std::shared_ptr adaptor_; + MockCallback* callback_; +}; + +/** +* @tc.name: DivStrategy001 +* @tc.desc: test DivStrategy function when three parameters are all nullptr +* @tc.type: FUNC +* @tc.require:SQL +*/ +HWTEST_F(DataShareCommonTest, DivStrategy001, TestSize.Level1) +{ + ZLOGI("DataShareCommonTest DivStrategy001 start"); + std::shared_ptr check = nullptr; + std::shared_ptr trueAction = nullptr; + std::shared_ptr falseAction = nullptr; + DivStrategy divStrategy(check, trueAction, falseAction); + auto context = std::make_shared(); + bool result = divStrategy.operator()(context); + EXPECT_EQ(result, false); + ZLOGI("DataShareCommonTest DivStrategy001 end"); +} + +/** +* @tc.name: DivStrategy002 +* @tc.desc: test DivStrategy function when trueAction and falseAction are nullptr +* @tc.type: FUNC +* @tc.require:SQL +*/ +HWTEST_F(DataShareCommonTest, DivStrategy002, TestSize.Level1) +{ + ZLOGI("DataShareCommonTest DivStrategy002 start"); + std::shared_ptr check = std::make_shared(); + std::shared_ptr trueAction = nullptr; + std::shared_ptr falseAction = nullptr; + DivStrategy divStrategy(check, trueAction, falseAction); + auto context = std::make_shared(); + bool result = divStrategy.operator()(context); + EXPECT_EQ(result, false); + ZLOGI("DataShareCommonTest DivStrategy002 end"); +} + +/** +* @tc.name: DivStrategy003 +* @tc.desc: test DivStrategy function when only falseAction is nullptr +* @tc.type: FUNC +* @tc.require:SQL +*/ +HWTEST_F(DataShareCommonTest, DivStrategy003, TestSize.Level1) +{ + ZLOGI("DataShareCommonTest DivStrategy003 start"); + std::shared_ptr check = std::make_shared(); + std::shared_ptr trueAction = std::make_shared(); + std::shared_ptr falseAction = nullptr; + DivStrategy divStrategy(check, trueAction, falseAction); + auto context = std::make_shared(); + bool result = divStrategy.operator()(context); + EXPECT_EQ(result, false); + ZLOGI("DataShareCommonTest DivStrategy003 end"); +} + +/** +* @tc.name: SeqStrategyInit001 +* @tc.desc: test SeqStrategyInit function when item is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a seqStrategy object and strategy2 = nullptr + 2.call Init function +* @tc.experct: Init failed and return false +*/ +HWTEST_F(DataShareCommonTest, SeqStrategyInit001, TestSize.Level1) +{ + ZLOGI("SeqStrategyInit001 start"); + Strategy* strategy1 = new Strategy(); + Strategy* strategy2 = nullptr; + SeqStrategy seqStrategy; + bool result = seqStrategy.Init({strategy1, strategy2}); + EXPECT_FALSE(result); + delete strategy1; + ZLOGI("SeqStrategyInit001 end"); +} + +/** +* @tc.name: DoConnect001 +* @tc.desc: test DoConnect function when callback_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a ExtensionConnectAdaptor object and callback = nullptr + 2.call DoConnect function +* @tc.experct: DoConnect failed and return false +*/ +HWTEST_F(DataShareCommonTest, DoConnect001, TestSize.Level1) +{ + ZLOGI("DoConnect001 start"); + std::string uri = "testUri"; + std::string bundleName = "testBundle"; + AAFwk::WantParams wantParams; + ExtensionConnectAdaptor extensionConnectAdaptor; + bool result = extensionConnectAdaptor.DoConnect(uri, bundleName, wantParams); + EXPECT_FALSE(result); + ZLOGI("DoConnect001 end"); +} + +/** +* @tc.name: InsertEx001 +* @tc.desc: test InsertEx function when store_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a rdbDelegate object and store_ = nullptr + 2.call InsertEx function +* @tc.experct: InsertEx failed and return E_DB_ERROR +*/ +HWTEST_F(DataShareCommonTest, InsertEx001, TestSize.Level1) +{ + ZLOGI("InsertEx001 start"); + DistributedData::StoreMetaData metaData; + metaData.user = "100"; + metaData.bundleName = "test"; + metaData.area = 1; + int version = 0; + bool registerFunction = false; + std::string extUri = "uri"; + std::string backup = "backup"; RdbDelegate rdbDelegate; rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); - rdbDelegate.store_ = nullptr; - std::string tableName = ""; - DataShare::DataShareValuesBucket valuesBucket; - std::string name0 = ""; - valuesBucket.Put("", name0); - auto result = rdbDelegate.InsertEx(tableName, valuesBucket); - EXPECT_EQ(result.first, E_DB_ERROR); - ZLOGI("InsertEx001 end"); -} - -/** -* @tc.name: UpdateEx001 -* @tc.desc: test UpdateEx function when store_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a RdbDelegate object and store_ = nullptr - 2.call UpdateEx function -* @tc.experct: UpdateEx failed and return E_DB_ERROR -*/ -HWTEST_F(DataShareCommonTest, UpdateEx001, TestSize.Level1) -{ - ZLOGI("UpdateEx001 start"); - DistributedData::StoreMetaData metaData; - metaData.user = "100"; - metaData.bundleName = "test"; - metaData.area = 1; - int version = 0; - bool registerFunction = false; - std::string extUri = "uri"; - std::string backup = "backup"; + rdbDelegate.store_ = nullptr; + std::string tableName = ""; + DataShare::DataShareValuesBucket valuesBucket; + std::string name0 = ""; + valuesBucket.Put("", name0); + auto result = rdbDelegate.InsertEx(tableName, valuesBucket); + EXPECT_EQ(result.first, E_DB_ERROR); + ZLOGI("InsertEx001 end"); +} + +/** +* @tc.name: UpdateEx001 +* @tc.desc: test UpdateEx function when store_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a RdbDelegate object and store_ = nullptr + 2.call UpdateEx function +* @tc.experct: UpdateEx failed and return E_DB_ERROR +*/ +HWTEST_F(DataShareCommonTest, UpdateEx001, TestSize.Level1) +{ + ZLOGI("UpdateEx001 start"); + DistributedData::StoreMetaData metaData; + metaData.user = "100"; + metaData.bundleName = "test"; + metaData.area = 1; + int version = 0; + bool registerFunction = false; + std::string extUri = "uri"; + std::string backup = "backup"; RdbDelegate rdbDelegate; rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); - rdbDelegate.store_ = nullptr; - std::string tableName = ""; - DataShare::DataShareValuesBucket valuesBucket; - std::string name0 = ""; - valuesBucket.Put("", name0); - DataSharePredicates predicate; - auto result = rdbDelegate.UpdateEx(tableName, predicate, valuesBucket); - EXPECT_EQ(result.first, E_DB_ERROR); - ZLOGI("UpdateEx001 end"); -} - -/** -* @tc.name: DeleteEx001 -* @tc.desc: test DeleteEx function when store_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a RdbDelegate object and store_ = nullptr - 2.call DeleteEx function -* @tc.experct: DeleteEx failed and return E_DB_ERROR -*/ -HWTEST_F(DataShareCommonTest, DeleteEx001, TestSize.Level1) -{ - ZLOGI("DeleteEx001 start"); - DistributedData::StoreMetaData metaData; - metaData.user = "100"; - metaData.bundleName = "test"; - metaData.area = 1; - int version = 0; - bool registerFunction = false; - std::string extUri = "uri"; - std::string backup = "backup"; + rdbDelegate.store_ = nullptr; + std::string tableName = ""; + DataShare::DataShareValuesBucket valuesBucket; + std::string name0 = ""; + valuesBucket.Put("", name0); + DataSharePredicates predicate; + auto result = rdbDelegate.UpdateEx(tableName, predicate, valuesBucket); + EXPECT_EQ(result.first, E_DB_ERROR); + ZLOGI("UpdateEx001 end"); +} + +/** +* @tc.name: DeleteEx001 +* @tc.desc: test DeleteEx function when store_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a RdbDelegate object and store_ = nullptr + 2.call DeleteEx function +* @tc.experct: DeleteEx failed and return E_DB_ERROR +*/ +HWTEST_F(DataShareCommonTest, DeleteEx001, TestSize.Level1) +{ + ZLOGI("DeleteEx001 start"); + DistributedData::StoreMetaData metaData; + metaData.user = "100"; + metaData.bundleName = "test"; + metaData.area = 1; + int version = 0; + bool registerFunction = false; + std::string extUri = "uri"; + std::string backup = "backup"; RdbDelegate rdbDelegate; rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); - rdbDelegate.store_ = nullptr; - std::string tableName = ""; - DataSharePredicates predicate; - auto result = rdbDelegate.DeleteEx(tableName, predicate); - EXPECT_EQ(result.first, E_DB_ERROR); - ZLOGI("DeleteEx001 end"); -} - -/** -* @tc.name: Query001 -* @tc.desc: test Query function when store_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a RdbDelegate object and store_ = nullptr - 2.call Query function -* @tc.experct: Query failed and return errCode -*/ -HWTEST_F(DataShareCommonTest, Query001, TestSize.Level1) -{ - ZLOGI("Query001 start"); - DistributedData::StoreMetaData metaData; - metaData.user = "100"; - metaData.bundleName = "test"; - metaData.area = 1; - int version = 0; - bool registerFunction = false; - std::string extUri = "uri"; - std::string backup = "backup"; + rdbDelegate.store_ = nullptr; + std::string tableName = ""; + DataSharePredicates predicate; + auto result = rdbDelegate.DeleteEx(tableName, predicate); + EXPECT_EQ(result.first, E_DB_ERROR); + ZLOGI("DeleteEx001 end"); +} + +/** +* @tc.name: Query001 +* @tc.desc: test Query function when store_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a RdbDelegate object and store_ = nullptr + 2.call Query function +* @tc.experct: Query failed and return errCode +*/ +HWTEST_F(DataShareCommonTest, Query001, TestSize.Level1) +{ + ZLOGI("Query001 start"); + DistributedData::StoreMetaData metaData; + metaData.user = "100"; + metaData.bundleName = "test"; + metaData.area = 1; + int version = 0; + bool registerFunction = false; + std::string extUri = "uri"; + std::string backup = "backup"; RdbDelegate rdbDelegate; rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); - rdbDelegate.store_ = nullptr; - std::string tableName = ""; - DataSharePredicates predicate; - std::vector columns; - int32_t callingPid = 1; - uint32_t callingTokenId = 1; - auto result = rdbDelegate.Query(tableName, predicate, columns, callingPid, callingTokenId); - EXPECT_EQ(result.first, rdbDelegate.errCode_); - ZLOGI("Query001 end"); -} - -/** -* @tc.name: Query002 -* @tc.desc: test Query function when store_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a RdbDelegate object and store_ = nullptr - 2.call Query function -* @tc.experct: Query failed and return "" -*/ -HWTEST_F(DataShareCommonTest, Query002, TestSize.Level1) -{ - ZLOGI("Query002 start"); - DistributedData::StoreMetaData metaData; - metaData.user = "100"; - metaData.bundleName = "test"; - metaData.area = 1; - int version = 0; - bool registerFunction = false; - std::string extUri = "uri"; - std::string backup = "backup"; + rdbDelegate.store_ = nullptr; + std::string tableName = ""; + DataSharePredicates predicate; + std::vector columns; + int32_t callingPid = 1; + uint32_t callingTokenId = 1; + auto result = rdbDelegate.Query(tableName, predicate, columns, callingPid, callingTokenId); + EXPECT_EQ(result.first, rdbDelegate.errCode_); + ZLOGI("Query001 end"); +} + +/** +* @tc.name: Query002 +* @tc.desc: test Query function when store_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a RdbDelegate object and store_ = nullptr + 2.call Query function +* @tc.experct: Query failed and return "" +*/ +HWTEST_F(DataShareCommonTest, Query002, TestSize.Level1) +{ + ZLOGI("Query002 start"); + DistributedData::StoreMetaData metaData; + metaData.user = "100"; + metaData.bundleName = "test"; + metaData.area = 1; + int version = 0; + bool registerFunction = false; + std::string extUri = "uri"; + std::string backup = "backup"; RdbDelegate rdbDelegate; rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); - rdbDelegate.store_ = nullptr; - std::string sql = "testsql"; - std::vector selectionArgs; - auto result = rdbDelegate.Query(sql, selectionArgs); - EXPECT_EQ(result, ""); - ZLOGI("Query002 end"); -} - -/** -* @tc.name: QuerySql001 -* @tc.desc: test QuerySql function when store_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a RdbDelegate object and store_ = nullptr - 2.call QuerySql function -* @tc.experct: QuerySql failed and return nullptr -*/ -HWTEST_F(DataShareCommonTest, QuerySql001, TestSize.Level1) -{ - ZLOGI("QuerySql001 start"); - DistributedData::StoreMetaData metaData; - metaData.user = "100"; - metaData.bundleName = "test"; - metaData.area = 1; - int version = 0; - bool registerFunction = false; - std::string extUri = "uri"; - std::string backup = "backup"; + rdbDelegate.store_ = nullptr; + std::string sql = "testsql"; + std::vector selectionArgs; + auto result = rdbDelegate.Query(sql, selectionArgs); + EXPECT_EQ(result, ""); + ZLOGI("Query002 end"); +} + +/** +* @tc.name: QuerySql001 +* @tc.desc: test QuerySql function when store_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a RdbDelegate object and store_ = nullptr + 2.call QuerySql function +* @tc.experct: QuerySql failed and return nullptr +*/ +HWTEST_F(DataShareCommonTest, QuerySql001, TestSize.Level1) +{ + ZLOGI("QuerySql001 start"); + DistributedData::StoreMetaData metaData; + metaData.user = "100"; + metaData.bundleName = "test"; + metaData.area = 1; + int version = 0; + bool registerFunction = false; + std::string extUri = "uri"; + std::string backup = "backup"; RdbDelegate rdbDelegate; rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); - rdbDelegate.store_ = nullptr; - std::string sql = "testsql"; - auto result = rdbDelegate.QuerySql(sql); - EXPECT_EQ(result, nullptr); - ZLOGI("QuerySql001 end"); -} - -/** -* @tc.name: UpdateSql001 -* @tc.desc: test UpdateSql function when store_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a RdbDelegate object and store_ = nullptr - 2.call UpdateSql function -* @tc.experct: UpdateSql failed and return E_ERROR -*/ -HWTEST_F(DataShareCommonTest, UpdateSql001, TestSize.Level1) -{ - ZLOGI("UpdateSql001 start"); - DistributedData::StoreMetaData metaData; - metaData.user = "100"; - metaData.bundleName = "test"; - metaData.area = 1; - int version = 0; - bool registerFunction = false; - std::string extUri = "uri"; - std::string backup = "backup"; + rdbDelegate.store_ = nullptr; + std::string sql = "testsql"; + auto result = rdbDelegate.QuerySql(sql); + EXPECT_EQ(result, nullptr); + ZLOGI("QuerySql001 end"); +} + +/** +* @tc.name: UpdateSql001 +* @tc.desc: test UpdateSql function when store_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a RdbDelegate object and store_ = nullptr + 2.call UpdateSql function +* @tc.experct: UpdateSql failed and return E_ERROR +*/ +HWTEST_F(DataShareCommonTest, UpdateSql001, TestSize.Level1) +{ + ZLOGI("UpdateSql001 start"); + DistributedData::StoreMetaData metaData; + metaData.user = "100"; + metaData.bundleName = "test"; + metaData.area = 1; + int version = 0; + bool registerFunction = false; + std::string extUri = "uri"; + std::string backup = "backup"; RdbDelegate rdbDelegate; rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); - rdbDelegate.store_ = nullptr; - std::string sql = "testsql"; - auto result = rdbDelegate.UpdateSql(sql); - EXPECT_EQ(result.first, OHOS::DataShare::E_ERROR); - ZLOGI("UpdateSql001 end"); -} - -/** -* @tc.name: EraseTimerTaskId001 -* @tc.desc: test EraseTimerTaskId function when not find key -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object and timerCache_ is null - 2.call EraseTimerTaskId function -* @tc.experct: not find key, EraseTimerTaskId failed and return -1 -*/ -HWTEST_F(DataShareCommonTest, EraseTimerTaskId001, TestSize.Level1) -{ - ZLOGI("EraseTimerTaskId001 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - int64_t result = manager.EraseTimerTaskId(key); - EXPECT_EQ(result, -1); - ZLOGI("EraseTimerTaskId001 end"); -} - -/** -* @tc.name: EraseTimerTaskId002 -* @tc.desc: test EraseTimerTaskId function when find key -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object and timerCache_[key] = timeid - 2.call EraseTimerTaskId function -* @tc.experct: find key, EraseTimerTaskId success and return timeid -*/ -HWTEST_F(DataShareCommonTest, EraseTimerTaskId002, TestSize.Level1) -{ - ZLOGI("EraseTimerTaskId002 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - int64_t timeid = 1; - manager.timerCache_[key] = timeid; - int64_t result = manager.EraseTimerTaskId(key); - EXPECT_EQ(result, timeid); - ZLOGI("EraseTimerTaskId002 end"); -} - -/** -* @tc.name: GetSchedulerStatus001 -* @tc.desc: test GetSchedulerStatus function when not find key -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object and schedulerStatusCache_ is null - 2.call GetSchedulerStatus function -* @tc.experct: not find key, GetSchedulerStatus failed and return false -*/ -HWTEST_F(DataShareCommonTest, GetSchedulerStatus001, TestSize.Level1) -{ - ZLOGI("GetSchedulerStatus001 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - bool result = manager.GetSchedulerStatus(key); - EXPECT_FALSE(result); - ZLOGI("GetSchedulerStatus001 end"); -} - -/** -* @tc.name: GetSchedulerStatus002 -* @tc.desc: test GetSchedulerStatus function when find key -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object and schedulerStatusCache_[key] = true; - 2.call GetSchedulerStatus function -* @tc.experct: find key, GetSchedulerStatus success and return true -*/ -HWTEST_F(DataShareCommonTest, GetSchedulerStatus002, TestSize.Level1) -{ - ZLOGI("GetSchedulerStatus002 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - manager.schedulerStatusCache_[key] = true; - bool result = manager.GetSchedulerStatus(key); - EXPECT_TRUE(result); - ZLOGI("GetSchedulerStatus002 end"); -} - -/** -* @tc.name: RemoveTimer001 -* @tc.desc: test RemoveTimer function when executor_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object what timerCache_[key] = 1 and executor_ is nullptr - 2.call RemoveTimer function to remove timer -* @tc.experct: RemoveTimer failed and timerCache_ is not null -*/ -HWTEST_F(DataShareCommonTest, RemoveTimer001, TestSize.Level1) -{ - ZLOGI("RemoveTimer001 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - manager.executor_ = nullptr; - manager.timerCache_[key] = 1; - manager.RemoveTimer(key); - EXPECT_NE(manager.timerCache_.find(key), manager.timerCache_.end()); - ZLOGI("RemoveTimer001 end"); -} - -/** -* @tc.name: RemoveTimer002 -* @tc.desc: test RemoveTimer function when executor_ is not nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object and timerCache_[key] = 1 and executor_ is not nullptr - 2.call RemoveTimer function to remove timer -* @tc.experct: RemoveTimer success and timerCache_ is null -*/ -HWTEST_F(DataShareCommonTest, RemoveTimer002, TestSize.Level1) -{ - ZLOGI("RemoveTimer002 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - manager.executor_ = std::make_shared(5, 3); - manager.timerCache_[key] = 1; - manager.RemoveTimer(key); - EXPECT_EQ(manager.timerCache_.find(key), manager.timerCache_.end()); - ZLOGI("RemoveTimer002 end"); -} - -/** -* @tc.name: ClearTimer001 -* @tc.desc: test ClearTimer function when executor_ is nullptr -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object what timerCache_[key] = 1 and executor_ is nullptr - 2.call ClearTimer function to clear timer -* @tc.experct: ClearTimer failed and timerCache_ is not empty -*/ -HWTEST_F(DataShareCommonTest, ClearTimer001, TestSize.Level1) -{ - ZLOGI("ClearTimer001 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - manager.executor_ = nullptr; - manager.timerCache_[key] = 1; - manager.ClearTimer(); - EXPECT_FALSE(manager.timerCache_.empty()); - ZLOGI("ClearTimer001 end"); -} - -/** -* @tc.name: ClearTimer002 -* @tc.desc: test ClearTimer function when executor_ is not nullptr and find key -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object and timerCache_[key] = 1 and executor_ is not nullptr - 2.call ClearTimer function to clear timer -* @tc.experct: ClearTimer success and timerCache_ is empty -*/ -HWTEST_F(DataShareCommonTest, ClearTimer002, TestSize.Level1) -{ - ZLOGI("ClearTimer002 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - manager.executor_ = std::make_shared(5, 3); - manager.timerCache_[key] = 1; - manager.ClearTimer(); - EXPECT_TRUE(manager.timerCache_.empty()); - ZLOGI("ClearTimer002 end"); -} - -/** -* @tc.name: ClearTimer003 -* @tc.desc: test ClearTimer function when executor_ is not nullptr and not find key -* @tc.type: FUNC -* @tc.require:issueIBX9E1 -* @tc.precon: None -* @tc.step: - 1.Creat a SchedulerManager object and timerCache_ is null - 2.call ClearTimer function -* @tc.experct: ClearTimer failed and timerCache_ is empty -*/ -HWTEST_F(DataShareCommonTest, ClearTimer003, TestSize.Level1) -{ - ZLOGI("ClearTimer003 start"); - Key key("uri1", 1, "name1"); - auto& manager = SchedulerManager::GetInstance(); - manager.executor_ = std::make_shared(5, 3); - manager.ClearTimer(); - EXPECT_TRUE(manager.timerCache_.empty()); - ZLOGI("ClearTimer003 end"); -} + rdbDelegate.store_ = nullptr; + std::string sql = "testsql"; + auto result = rdbDelegate.UpdateSql(sql); + EXPECT_EQ(result.first, OHOS::DataShare::E_ERROR); + ZLOGI("UpdateSql001 end"); +} + +/** +* @tc.name: EraseTimerTaskId001 +* @tc.desc: test EraseTimerTaskId function when not find key +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object and timerCache_ is null + 2.call EraseTimerTaskId function +* @tc.experct: not find key, EraseTimerTaskId failed and return -1 +*/ +HWTEST_F(DataShareCommonTest, EraseTimerTaskId001, TestSize.Level1) +{ + ZLOGI("EraseTimerTaskId001 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + int64_t result = manager.EraseTimerTaskId(key); + EXPECT_EQ(result, -1); + ZLOGI("EraseTimerTaskId001 end"); +} + +/** +* @tc.name: EraseTimerTaskId002 +* @tc.desc: test EraseTimerTaskId function when find key +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object and timerCache_[key] = timeid + 2.call EraseTimerTaskId function +* @tc.experct: find key, EraseTimerTaskId success and return timeid +*/ +HWTEST_F(DataShareCommonTest, EraseTimerTaskId002, TestSize.Level1) +{ + ZLOGI("EraseTimerTaskId002 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + int64_t timeid = 1; + manager.timerCache_[key] = timeid; + int64_t result = manager.EraseTimerTaskId(key); + EXPECT_EQ(result, timeid); + ZLOGI("EraseTimerTaskId002 end"); +} + +/** +* @tc.name: GetSchedulerStatus001 +* @tc.desc: test GetSchedulerStatus function when not find key +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object and schedulerStatusCache_ is null + 2.call GetSchedulerStatus function +* @tc.experct: not find key, GetSchedulerStatus failed and return false +*/ +HWTEST_F(DataShareCommonTest, GetSchedulerStatus001, TestSize.Level1) +{ + ZLOGI("GetSchedulerStatus001 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + bool result = manager.GetSchedulerStatus(key); + EXPECT_FALSE(result); + ZLOGI("GetSchedulerStatus001 end"); +} + +/** +* @tc.name: GetSchedulerStatus002 +* @tc.desc: test GetSchedulerStatus function when find key +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object and schedulerStatusCache_[key] = true; + 2.call GetSchedulerStatus function +* @tc.experct: find key, GetSchedulerStatus success and return true +*/ +HWTEST_F(DataShareCommonTest, GetSchedulerStatus002, TestSize.Level1) +{ + ZLOGI("GetSchedulerStatus002 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + manager.schedulerStatusCache_[key] = true; + bool result = manager.GetSchedulerStatus(key); + EXPECT_TRUE(result); + ZLOGI("GetSchedulerStatus002 end"); +} + +/** +* @tc.name: RemoveTimer001 +* @tc.desc: test RemoveTimer function when executor_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object what timerCache_[key] = 1 and executor_ is nullptr + 2.call RemoveTimer function to remove timer +* @tc.experct: RemoveTimer failed and timerCache_ is not null +*/ +HWTEST_F(DataShareCommonTest, RemoveTimer001, TestSize.Level1) +{ + ZLOGI("RemoveTimer001 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + manager.executor_ = nullptr; + manager.timerCache_[key] = 1; + manager.RemoveTimer(key); + EXPECT_NE(manager.timerCache_.find(key), manager.timerCache_.end()); + ZLOGI("RemoveTimer001 end"); +} + +/** +* @tc.name: RemoveTimer002 +* @tc.desc: test RemoveTimer function when executor_ is not nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object and timerCache_[key] = 1 and executor_ is not nullptr + 2.call RemoveTimer function to remove timer +* @tc.experct: RemoveTimer success and timerCache_ is null +*/ +HWTEST_F(DataShareCommonTest, RemoveTimer002, TestSize.Level1) +{ + ZLOGI("RemoveTimer002 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + manager.executor_ = std::make_shared(5, 3); + manager.timerCache_[key] = 1; + manager.RemoveTimer(key); + EXPECT_EQ(manager.timerCache_.find(key), manager.timerCache_.end()); + ZLOGI("RemoveTimer002 end"); +} + +/** +* @tc.name: ClearTimer001 +* @tc.desc: test ClearTimer function when executor_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object what timerCache_[key] = 1 and executor_ is nullptr + 2.call ClearTimer function to clear timer +* @tc.experct: ClearTimer failed and timerCache_ is not empty +*/ +HWTEST_F(DataShareCommonTest, ClearTimer001, TestSize.Level1) +{ + ZLOGI("ClearTimer001 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + manager.executor_ = nullptr; + manager.timerCache_[key] = 1; + manager.ClearTimer(); + EXPECT_FALSE(manager.timerCache_.empty()); + ZLOGI("ClearTimer001 end"); +} + +/** +* @tc.name: ClearTimer002 +* @tc.desc: test ClearTimer function when executor_ is not nullptr and find key +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object and timerCache_[key] = 1 and executor_ is not nullptr + 2.call ClearTimer function to clear timer +* @tc.experct: ClearTimer success and timerCache_ is empty +*/ +HWTEST_F(DataShareCommonTest, ClearTimer002, TestSize.Level1) +{ + ZLOGI("ClearTimer002 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + manager.executor_ = std::make_shared(5, 3); + manager.timerCache_[key] = 1; + manager.ClearTimer(); + EXPECT_TRUE(manager.timerCache_.empty()); + ZLOGI("ClearTimer002 end"); +} + +/** +* @tc.name: ClearTimer003 +* @tc.desc: test ClearTimer function when executor_ is not nullptr and not find key +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SchedulerManager object and timerCache_ is null + 2.call ClearTimer function +* @tc.experct: ClearTimer failed and timerCache_ is empty +*/ +HWTEST_F(DataShareCommonTest, ClearTimer003, TestSize.Level1) +{ + ZLOGI("ClearTimer003 start"); + Key key("uri1", 1, "name1"); + auto& manager = SchedulerManager::GetInstance(); + manager.executor_ = std::make_shared(5, 3); + manager.ClearTimer(); + EXPECT_TRUE(manager.timerCache_.empty()); + ZLOGI("ClearTimer003 end"); +} /** * @tc.name: DBDelegateTest001 @@ -609,4 +626,93 @@ HWTEST_F(DataShareCommonTest, DBDelegateTest002, TestSize.Level0) { EXPECT_EQ(delegate.extUri_, "extUri"); EXPECT_EQ(delegate.backup_, "backup"); } + +/** +* @tc.name: DoConnect002 +* @tc.desc: test DoConnect function in SearchConnectAdaptor when callback_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SearchConnectAdaptor object and callback = nullptr + 2.call DoConnect function +* @tc.experct: DoConnect failed and return false +*/ +HWTEST_F(DataShareCommonTest, DoConnect002, TestSize.Level1) +{ + ZLOGI("DoConnect002 start"); + std::string uri = "testUri"; + std::string bundleName = "testBundle"; + AAFwk::WantParams wantParams; + SearchConnectAdaptor searchConnectAdaptor; + bool result = adaptor_->DoConnect(uri, bundleName, wantParams); + EXPECT_FALSE(result); + ZLOGI("DoConnect002 end"); +} + +/** +* @tc.name: DisConnectTest +* @tc.desc: test DisConnect function in SearchConnectAdaptor when callback_ is not null +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SearchConnectAdaptor object and callback = nullptr + 2.call DisConnect function +* @tc.experct: callback_ is nullptr +*/ +HWTEST_F(DataShareCommonTest, DisConnectTest, TestSize.Level1) +{ + std::shared_ptr adaptor_ = std::make_shared(); + EXPECT_NE(adaptor_->callback_, nullptr); + adaptor_->DisConnect(); + EXPECT_EQ(adaptor_->callback_, nullptr); +} + +/** +* @tc.name: ConnectTest +* @tc.desc: test Connect function in SearchConnectAdaptor +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Prepare test data + 2.call Connect function +*/ +HWTEST_F(DataShareCommonTest, ConnectTest001, TestSize.Level1) +{ + const std::string uri = "testUri"; + StoreMetaData meta; + meta.dataDir = "testdir"; + meta.bundlename = "test_bundlename" + AAFwk::WantParams wantParams; + const int maxWaitTime = 1; + auto result = adaptor_->Connect(uri, meta, wantParams, maxWaitTime); + EXPECT_TRUE(result.first); + EXPECT_FALSE(result.second.empty()); +} + +/** +* @tc.name: ConnectTest +* @tc.desc: test Connect function in SearchConnectAdaptor +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Prepare test data + 2.call Connect function +*/ +HWTEST_F(DataShareCommonTest, ConnectTest002, TestSize.Level1) +{ + const std::string uri = "testUri"; + StoreMetaData meta; + meta.dataDir = "testdir"; + meta.bundlename = "test_bundlename" + AAFwk::WantParams wantParams; + const int maxWaitTime = 1; + adaptor_->callback_ = nullptr; + auto result = adaptor_->Connect(uri, meta, wantParams, maxWaitTime); + EXPECT_FALSE(result.first); + EXPECT_TRUE(result.second.empty()); +} } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn index 17ca41e7e..331198a12 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn @@ -58,6 +58,8 @@ ohos_fuzztest("DataShareServiceImplFuzzTest") { "${data_service_path}/service/data_share/common/div_strategy.cpp", "${data_service_path}/service/data_share/common/extension_ability_manager.cpp", "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_callback_impl.cpp", "${data_service_path}/service/data_share/common/extension_mgr_proxy.cpp", "${data_service_path}/service/data_share/common/kv_delegate.cpp", "${data_service_path}/service/data_share/common/proxy_data_manager.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn index 8f2dc9fbb..6387ddc04 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn @@ -56,6 +56,8 @@ ohos_fuzztest("DataShareServiceStubFuzzTest") { "${data_service_path}/service/data_share/common/div_strategy.cpp", "${data_service_path}/service/data_share/common/extension_ability_manager.cpp", "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_callback_impl.cpp", "${data_service_path}/service/data_share/common/extension_mgr_proxy.cpp", "${data_service_path}/service/data_share/common/kv_delegate.cpp", "${data_service_path}/service/data_share/common/proxy_data_manager.cpp", -- Gitee