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 0000000000000000000000000000000000000000..a8b35b30689789de08817c84ec5a868d92e1d8e7 --- /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 0000000000000000000000000000000000000000..008ad144ee4879c7b6aee6731f01812a738ff600 --- /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 618f6fcb72cbbe51810c5ea6e0710fc5d9b3c70f..7aea323e153daaf1129e9fb755f36b86023daa8c 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 68e54c60c7d487bc9fba72501156a4a9a000cfe6..2f3851ca93e3cd44062b70f76666ca03cc8ba311 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 0000000000000000000000000000000000000000..59cc81d7e53ed23ca2ae3c9d4e19b2cf01d7f2e9 --- /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 0000000000000000000000000000000000000000..00c4abb25cf36356f22220465b65ebba60b5c994 --- /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 0000000000000000000000000000000000000000..e4d396a621a10674f57ecad4a8d0484a802c2ce8 --- /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 0000000000000000000000000000000000000000..0192f1c4192ec5a4c6a126db4ebb8d716d5d1e6c --- /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 4043d0511e7656caf7f1703932713e4689c2e523..8fd19e2980addad352c17a55b53462ccbe10c8d7 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 d39912924a5d8282e35f82dd42779c65c1321ede..5cfd63b6e5306428e25ed446a2f87770ab9c0d69 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 756f5fa4ddcfb0290d17e8c5e6ca8729911f5487..00744a02d35fd3c2add0144a352a82f6ce70780a 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 a6db0130b3801335238fccf95b37ad8a60df3dc4..cb43ea5d2d42cf681f744580bd276392189a844d 100644 --- a/services/distributeddataservice/service/test/data_share_common_test.cpp +++ b/services/distributeddataservice/service/test/data_share_common_test.cpp @@ -17,6 +17,7 @@ #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" @@ -25,17 +26,33 @@ #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(){}; + void SetUp() override + { + adaptor_ = std::make_shared(); + callback_ = new MockCallback(); + adaptor_->callback_ = callback_; + }; void TearDown(){}; + std::shared_ptr adaptor_; + MockCallback* callback_; }; /** @@ -163,8 +180,8 @@ HWTEST_F(DataShareCommonTest, InsertEx001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataShare::DataShareValuesBucket valuesBucket; @@ -197,8 +214,8 @@ HWTEST_F(DataShareCommonTest, UpdateEx001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataShare::DataShareValuesBucket valuesBucket; @@ -232,8 +249,8 @@ HWTEST_F(DataShareCommonTest, DeleteEx001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataSharePredicates predicate; @@ -264,8 +281,8 @@ HWTEST_F(DataShareCommonTest, Query001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataSharePredicates predicate; @@ -299,8 +316,8 @@ HWTEST_F(DataShareCommonTest, Query002, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string sql = "testsql"; std::vector selectionArgs; @@ -331,8 +348,8 @@ HWTEST_F(DataShareCommonTest, QuerySql001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string sql = "testsql"; auto result = rdbDelegate.QuerySql(sql); @@ -362,8 +379,8 @@ HWTEST_F(DataShareCommonTest, UpdateSql001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string sql = "testsql"; auto result = rdbDelegate.UpdateSql(sql); @@ -571,42 +588,131 @@ HWTEST_F(DataShareCommonTest, ClearTimer003, TestSize.Level1) EXPECT_TRUE(manager.timerCache_.empty()); ZLOGI("ClearTimer003 end"); } - -/** - * @tc.name: DBDelegateTest001 - * @tc.desc: do nothing when delegate already inited - * @tc.type: FUNC - */ -HWTEST_F(DataShareCommonTest, DBDelegateTest001, TestSize.Level0) { - RdbDelegate delegate; - DistributedData::StoreMetaData meta; - meta.tokenId = 1; - - delegate.isInited_ = true; - delegate.Init({}, 0, false, "extUri", "backup"); - - EXPECT_TRUE(delegate.isInited_); - EXPECT_EQ(delegate.tokenId_, 0); - EXPECT_EQ(delegate.extUri_, ""); - EXPECT_EQ(delegate.backup_, ""); -} - -/** - * @tc.name : DBDelegateTest002 - * @tc.number: init members in delegate init - * @tc.type: FUNC - */ -HWTEST_F(DataShareCommonTest, DBDelegateTest002, TestSize.Level0) { - RdbDelegate delegate; - DistributedData::StoreMetaData meta; - meta.tokenId = 1; - - delegate.isInited_ = false; - delegate.Init(meta, 0, false, "extUri", "backup"); - - EXPECT_FALSE(delegate.isInited_); - EXPECT_EQ(delegate.tokenId_, meta.tokenId); - EXPECT_EQ(delegate.extUri_, "extUri"); - EXPECT_EQ(delegate.backup_, "backup"); -} + +/** + * @tc.name: DBDelegateTest001 + * @tc.desc: do nothing when delegate already inited + * @tc.type: FUNC + */ +HWTEST_F(DataShareCommonTest, DBDelegateTest001, TestSize.Level0) { + RdbDelegate delegate; + DistributedData::StoreMetaData meta; + meta.tokenId = 1; + + delegate.isInited_ = true; + delegate.Init({}, 0, false, "extUri", "backup"); + + EXPECT_TRUE(delegate.isInited_); + EXPECT_EQ(delegate.tokenId_, 0); + EXPECT_EQ(delegate.extUri_, ""); + EXPECT_EQ(delegate.backup_, ""); +} + +/** + * @tc.name : DBDelegateTest002 + * @tc.number: init members in delegate init + * @tc.type: FUNC + */ +HWTEST_F(DataShareCommonTest, DBDelegateTest002, TestSize.Level0) { + RdbDelegate delegate; + DistributedData::StoreMetaData meta; + meta.tokenId = 1; + + delegate.isInited_ = false; + delegate.Init(meta, 0, false, "extUri", "backup"); + + EXPECT_FALSE(delegate.isInited_); + EXPECT_EQ(delegate.tokenId_, meta.tokenId); + 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 17ca41e7ea604699c00f5b3795e897e9942db54f..331198a12e4187cf689b4db6764ee8449690cdbb 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 8f2dc9fbb4b701f19ee5edffcc26f5b392953b44..6387ddc04a450765c51727987a3ad017d950086b 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",