From ddc0444ecacc7a930c846f40250270f3100c9a5f Mon Sep 17 00:00:00 2001 From: lobty Date: Wed, 15 Oct 2025 10:15:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?observer=E5=85=A5=E5=8F=82=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=B0=83=E6=95=B4=E4=B8=BA=E6=99=BA=E8=83=BD=E6=8C=87?= =?UTF-8?q?=E9=92=88,=E6=A3=80=E8=A7=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lobty --- .../interfaces/include/kv_store_nb_delegate.h | 6 +- .../src/kv_store_delegate_manager.cpp | 6 +- .../src/kv_store_nb_delegate_impl.cpp | 30 +++---- .../src/kv_store_nb_delegate_impl.h | 16 ++-- .../storage/include/ikvdb_connection.h | 4 +- .../src/kv/generic_kvdb_connection.cpp | 4 +- .../storage/src/kv/generic_kvdb_connection.h | 4 +- .../src/sqlite/sqlite_cloud_kv_store.cpp | 4 +- .../src/sqlite/sqlite_cloud_kv_store.h | 4 +- .../sqlite/sqlite_single_ver_natural_store.h | 4 +- ...te_single_ver_natural_store_connection.cpp | 4 +- ...lite_single_ver_natural_store_connection.h | 4 +- ...sqlite_single_ver_natural_store_extend.cpp | 4 +- .../kvdelegatemanager_fuzzer.cpp | 84 +++++++++++++++++++ .../include/distributeddb_nb_test_tools.h | 8 +- .../src/distributeddb_nb_test_tools.cpp | 4 +- .../src/distributeddb_nb_observer_test.cpp | 2 +- ...erfaces_single_version_result_set_test.cpp | 2 +- 18 files changed, 140 insertions(+), 54 deletions(-) diff --git a/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h b/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h index 8ce850102bc..36e9df94f0d 100644 --- a/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h +++ b/frameworks/libs/distributeddb/interfaces/include/kv_store_nb_delegate.h @@ -45,7 +45,7 @@ public: std::string schema = ""; bool createDirByStoreIdOnly = false; SecurityOption secOption; // Add data security level parameter - std::weak_ptr observer; + std::shared_ptr observer; Key key; // The key that needs to be subscribed on obsever, empty means full subscription unsigned int mode = 0; // obsever mode int conflictType = 0; @@ -137,10 +137,10 @@ public: // synced data changes from remote devices, // local changes of local kv store. DB_API virtual DBStatus RegisterObserver(const Key &key, unsigned int mode, - const std::weak_ptr &observer) = 0; + std::shared_ptr observer) = 0; // UnRegister the registered observer. - DB_API virtual DBStatus UnRegisterObserver(const std::weak_ptr &observer) = 0; + DB_API virtual DBStatus UnRegisterObserver(std::shared_ptr observer) = 0; // Remove the device data synced from remote. DB_API virtual DBStatus RemoveDeviceData(const std::string &device) = 0; diff --git a/frameworks/libs/distributeddb/interfaces/src/kv_store_delegate_manager.cpp b/frameworks/libs/distributeddb/interfaces/src/kv_store_delegate_manager.cpp index 34eb25b6e84..c092868d9a2 100644 --- a/frameworks/libs/distributeddb/interfaces/src/kv_store_delegate_manager.cpp +++ b/frameworks/libs/distributeddb/interfaces/src/kv_store_delegate_manager.cpp @@ -128,8 +128,8 @@ bool CheckObserverConflictParam(const KvStoreNbDelegate::Option &option) LOGE("Invalid conflict type, conflict type is [%d]", option.conflictType); return false; } - if ((option.observer.lock() != nullptr && !ParamCheckUtils::CheckObserver(option.key, option.mode)) || - (option.observer.lock() == nullptr && (!option.key.empty() || option.mode != 0))) { + if ((option.observer != nullptr && !ParamCheckUtils::CheckObserver(option.key, option.mode)) || + (option.observer == nullptr && (!option.key.empty() || option.mode != 0))) { LOGE("Invalid observer param, observer mode is [%u]", option.mode); return false; } @@ -236,7 +236,7 @@ DBStatus KvStoreDelegateManager::SetObserverNotifier( KvStoreNbDelegate *kvStore, const KvStoreNbDelegate::Option &option) { DBStatus status; - if (option.observer.lock() != nullptr) { + if (option.observer != nullptr) { status = kvStore->RegisterObserver(option.key, option.mode, option.observer); if (status != OK) { LOGE("[KvStoreMgr] RegisterObserver failed."); diff --git a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp index aa2c806a6e6..c3c5f461d97 100644 --- a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp +++ b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp @@ -371,12 +371,12 @@ DBStatus KvStoreNbDelegateImpl::DeleteLocalBatch(const std::vector &keys) } DBStatus KvStoreNbDelegateImpl::RegisterObserver(const Key &key, unsigned int mode, - const std::weak_ptr &observer) + std::shared_ptr observer) { if (key.size() > DBConstant::MAX_KEY_SIZE) { return INVALID_ARGS; } - if (observer.lock() == nullptr) { + if (observer == nullptr) { LOGE("[KvStoreNbDelegate][RegisterObserver] Observer is null"); return INVALID_ARGS; } @@ -393,7 +393,7 @@ DBStatus KvStoreNbDelegateImpl::RegisterObserver(const Key &key, unsigned int mo } DBStatus KvStoreNbDelegateImpl::CheckDeviceObserver(const Key &key, unsigned int mode, - const std::weak_ptr &observer) + std::shared_ptr observer) { if (!ParamCheckUtils::CheckObserver(key, mode)) { LOGE("[KvStoreNbDelegate][CheckDeviceObserver] Register nb observer by illegal mode or key size!"); @@ -413,7 +413,7 @@ DBStatus KvStoreNbDelegateImpl::CheckDeviceObserver(const Key &key, unsigned int } DBStatus KvStoreNbDelegateImpl::RegisterDeviceObserver(const Key &key, unsigned int mode, - const std::weak_ptr &observer) + std::shared_ptr observer) { if (conn_->IsTransactionStarted()) { LOGE("[KvStoreNbDelegate][RegisterDeviceObserver] Transaction unfinished"); @@ -428,12 +428,13 @@ DBStatus KvStoreNbDelegateImpl::RegisterDeviceObserver(const Key &key, unsigned int errCode = E_OK; auto storeId = storeId_; + std::weak_ptr wkPtr = observer; KvDBObserverHandle *observerHandle = conn_->RegisterObserver( mode, key, - [observer, storeId](const KvDBCommitNotifyData ¬ifyData) { + [wkPtr, storeId](const KvDBCommitNotifyData ¬ifyData) { KvStoreChangedDataImpl data(¬ifyData); LOGD("[KvStoreNbDelegate][RegisterDeviceObserver] Trigger on change"); - if (auto observerPtr = observer.lock()) { + if (auto observerPtr = wkPtr.lock()) { observerPtr->OnChange(data); } else { LOGW("[KvStoreNbDelegate][RegisterDeviceObserver] observer released"); @@ -452,7 +453,7 @@ DBStatus KvStoreNbDelegateImpl::RegisterDeviceObserver(const Key &key, unsigned return OK; } -DBStatus KvStoreNbDelegateImpl::CheckCloudObserver(const std::weak_ptr &observer) +DBStatus KvStoreNbDelegateImpl::CheckCloudObserver(std::shared_ptr observer) { if (cloudObserverMap_.size() >= DBConstant::MAX_OBSERVER_COUNT) { LOGE("[KvStoreNbDelegate][CheckCloudObserver] The number of kv cloud observers over limit, storeId[%.3s]", @@ -467,7 +468,7 @@ DBStatus KvStoreNbDelegateImpl::CheckCloudObserver(const std::weak_ptr &observer) + std::shared_ptr observer) { std::lock_guard lockGuard(observerMapLock_); DBStatus status = CheckCloudObserver(observer); @@ -477,11 +478,12 @@ DBStatus KvStoreNbDelegateImpl::RegisterCloudObserver(const Key &key, unsigned i } auto storeId = storeId_; - ObserverAction action = [observer, storeId]( + std::weak_ptr wkPtr = observer; + ObserverAction action = [wkPtr, storeId]( const std::string &device, ChangedData &&changedData, bool isChangedData) { if (isChangedData) { LOGD("[KvStoreNbDelegate][RegisterCloudObserver] Trigger on change"); - if (auto observerPtr = observer.lock()) { + if (auto observerPtr = wkPtr.lock()) { observerPtr->OnChange(Origin::ORIGIN_CLOUD, device, std::move(changedData)); } else { LOGW("[KvStoreNbDelegate][RegisterCloudObserver] observer released"); @@ -498,9 +500,9 @@ DBStatus KvStoreNbDelegateImpl::RegisterCloudObserver(const Key &key, unsigned i return OK; } -DBStatus KvStoreNbDelegateImpl::UnRegisterObserver(const std::weak_ptr &observer) +DBStatus KvStoreNbDelegateImpl::UnRegisterObserver(std::shared_ptr observer) { - if (observer.lock() == nullptr) { + if (observer == nullptr) { return INVALID_ARGS; } @@ -517,7 +519,7 @@ DBStatus KvStoreNbDelegateImpl::UnRegisterObserver(const std::weak_ptr &observer) +DBStatus KvStoreNbDelegateImpl::UnRegisterDeviceObserver(std::shared_ptr observer) { std::lock_guard lockGuard(observerMapLock_); auto iter = observerMap_.find(observer); @@ -536,7 +538,7 @@ DBStatus KvStoreNbDelegateImpl::UnRegisterDeviceObserver(const std::weak_ptr &observer) +DBStatus KvStoreNbDelegateImpl::UnRegisterCloudObserver(std::shared_ptr observer) { std::lock_guard lockGuard(observerMapLock_); auto iter = cloudObserverMap_.find(observer); diff --git a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h index ef8c4ac91ed..15247c5cbf4 100644 --- a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h +++ b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h @@ -74,9 +74,9 @@ public: // Observer interfaces DBStatus RegisterObserver(const Key &key, unsigned int mode, - const std::weak_ptr &observer) override; + std::shared_ptr observer) override; - DBStatus UnRegisterObserver(const std::weak_ptr &observer) override; + DBStatus UnRegisterObserver(std::shared_ptr observer) override; DBStatus RemoveDeviceData(const std::string &device) override; @@ -219,17 +219,17 @@ private: void OnDeviceSyncProcess(const std::map &processMap, const DeviceSyncProcessCallback &onProcess) const; - DBStatus RegisterDeviceObserver(const Key &key, unsigned int mode, const std::weak_ptr &observer); + DBStatus RegisterDeviceObserver(const Key &key, unsigned int mode, std::shared_ptr observer); - DBStatus RegisterCloudObserver(const Key &key, unsigned int mode, const std::weak_ptr &observer); + DBStatus RegisterCloudObserver(const Key &key, unsigned int mode, std::shared_ptr observer); - DBStatus UnRegisterDeviceObserver(const std::weak_ptr &observer); + DBStatus UnRegisterDeviceObserver(std::shared_ptr observer); - DBStatus UnRegisterCloudObserver(const std::weak_ptr &observer); + DBStatus UnRegisterCloudObserver(std::shared_ptr observer); - DBStatus CheckDeviceObserver(const Key &key, unsigned int mode, const std::weak_ptr &observer); + DBStatus CheckDeviceObserver(const Key &key, unsigned int mode, std::shared_ptr observer); - DBStatus CheckCloudObserver(const std::weak_ptr &observer); + DBStatus CheckCloudObserver(std::shared_ptr observer); IKvDBConnection *conn_; std::string storeId_; diff --git a/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h b/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h index c01dd663508..617353638cc 100644 --- a/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h +++ b/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h @@ -153,10 +153,10 @@ public: virtual int32_t GetTaskCount() = 0; - virtual int RegisterObserverAction(const std::weak_ptr &observer, + virtual int RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) = 0; - virtual int UnRegisterObserverAction(const std::weak_ptr &observer) = 0; + virtual int UnRegisterObserverAction(std::shared_ptr observer) = 0; virtual void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) = 0; diff --git a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp index 55fe5252962..86840a212f7 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp @@ -410,13 +410,13 @@ int32_t GenericKvDBConnection::GetTaskCount() return 0; } -int GenericKvDBConnection::RegisterObserverAction([[gnu::unused]] const std::weak_ptr &observer, +int GenericKvDBConnection::RegisterObserverAction([[gnu::unused]] std::shared_ptr observer, [[gnu::unused]] const ObserverAction &action) { return E_OK; } -int GenericKvDBConnection::UnRegisterObserverAction([[gnu::unused]] const std::weak_ptr &observer) +int GenericKvDBConnection::UnRegisterObserverAction([[gnu::unused]] std::shared_ptr observer) { return E_OK; } diff --git a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h index 78c22a581f4..c5d4e6a580a 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h +++ b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h @@ -95,9 +95,9 @@ public: int32_t GetTaskCount() override; - int RegisterObserverAction(const std::weak_ptr &observer, const ObserverAction &action) override; + int RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) override; - int UnRegisterObserverAction(const std::weak_ptr &observer) override; + int UnRegisterObserverAction(std::shared_ptr observer) override; void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp index 3ff139f46e2..2537a793ff4 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp @@ -515,14 +515,14 @@ std::pair SqliteCloudKvStore::GetTr return {db, transactionHandle_}; } -void SqliteCloudKvStore::RegisterObserverAction(const std::weak_ptr &observer, +void SqliteCloudKvStore::RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) { std::lock_guard autoLock(observerMapMutex_); cloudObserverMap_[observer] = action; } -void SqliteCloudKvStore::UnRegisterObserverAction(const std::weak_ptr &observer) +void SqliteCloudKvStore::UnRegisterObserverAction(std::shared_ptr observer) { std::lock_guard autoLock(observerMapMutex_); cloudObserverMap_.erase(observer); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h index 562f2ab0fb4..227ce28a532 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h @@ -91,9 +91,9 @@ public: int SetCloudDbSchema(const std::map &schema); - void RegisterObserverAction(const std::weak_ptr &observer, const ObserverAction &action); + void RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action); - void UnRegisterObserverAction(const std::weak_ptr &observer); + void UnRegisterObserverAction(std::shared_ptr observer); int GetCloudVersion(const std::string &device, std::map &versionMap); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h index b774558ee88..1b7f491aac5 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h @@ -221,9 +221,9 @@ public: int SetCloudDbSchema(const std::map &schema); - int RegisterObserverAction(const std::weak_ptr &observer, const ObserverAction &action); + int RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action); - int UnRegisterObserverAction(const std::weak_ptr &observer); + int UnRegisterObserverAction(std::shared_ptr observer); void SetReceiveDataInterceptor(const DataInterceptor &interceptor) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp index 8f640988381..af6517d71ba 100755 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp @@ -1933,7 +1933,7 @@ int SQLiteSingleVerNaturalStoreConnection::SetCloudDbSchema(const std::map &observer, +int SQLiteSingleVerNaturalStoreConnection::RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) { auto naturalStore = GetDB(); @@ -1944,7 +1944,7 @@ int SQLiteSingleVerNaturalStoreConnection::RegisterObserverAction(const std::wea return naturalStore->RegisterObserverAction(observer, action); } -int SQLiteSingleVerNaturalStoreConnection::UnRegisterObserverAction(const std::weak_ptr &observer) +int SQLiteSingleVerNaturalStoreConnection::UnRegisterObserverAction(std::shared_ptr observer) { auto naturalStore = GetDB(); if (naturalStore == nullptr) { diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h index db654dbd49a..9219146516b 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h @@ -106,9 +106,9 @@ public: int UpdateKey(const UpdateKeyCallback &callback) override; - int RegisterObserverAction(const std::weak_ptr &observer, const ObserverAction &action) override; + int RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) override; - int UnRegisterObserverAction(const std::weak_ptr &observer) override; + int UnRegisterObserverAction(std::shared_ptr observer) override; int RemoveDeviceData(const std::string &device, ClearMode mode) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp index 14ff900e17c..983a85bf7e8 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp @@ -641,7 +641,7 @@ TimeOffset SQLiteSingleVerNaturalStore::GetLocalTimeOffsetForCloud() return GetLocalTimeOffset(); } -int SQLiteSingleVerNaturalStore::RegisterObserverAction(const std::weak_ptr &observer, +int SQLiteSingleVerNaturalStore::RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) { #ifdef USE_DISTRIBUTEDDB_CLOUD @@ -654,7 +654,7 @@ int SQLiteSingleVerNaturalStore::RegisterObserverAction(const std::weak_ptr &observer) +int SQLiteSingleVerNaturalStore::UnRegisterObserverAction(std::shared_ptr observer) { #ifdef USE_DISTRIBUTEDDB_CLOUD std::lock_guard autoLock(cloudStoreMutex_); diff --git a/frameworks/libs/distributeddb/test/fuzztest/kvdelegatemanager_fuzzer/kvdelegatemanager_fuzzer.cpp b/frameworks/libs/distributeddb/test/fuzztest/kvdelegatemanager_fuzzer/kvdelegatemanager_fuzzer.cpp index 3726117ca2b..62d3b8a441d 100644 --- a/frameworks/libs/distributeddb/test/fuzztest/kvdelegatemanager_fuzzer/kvdelegatemanager_fuzzer.cpp +++ b/frameworks/libs/distributeddb/test/fuzztest/kvdelegatemanager_fuzzer/kvdelegatemanager_fuzzer.cpp @@ -75,6 +75,90 @@ void CallbackFuzz(FuzzedDataProvider &fdp, std::string storeId) KvStoreDelegateManager::NotifyUserChanged(); } +<<<<<<< HEAD +======= +void SetStoreStatusNotifierV2Fuzz(FuzzedDataProvider &fdp) +{ + StoreStatusNotifierParam param; + param.userId = fdp.ConsumeRandomLengthString(); + param.appId = fdp.ConsumeRandomLengthString(); + param.storeId = fdp.ConsumeRandomLengthString(); + param.subUserId = fdp.ConsumeRandomLengthString(); + param.deviceId = fdp.ConsumeRandomLengthString(); + bool isCheckOk = false; + auto databaseStatusNotifyCallbackV2 = [&](const StoreStatusNotifierParam ¬ifierParam, + bool onLineStatus) -> void { + if (param.userId == notifierParam.userId && param.appId == notifierParam.appId && + param.storeId == notifierParam.storeId && param.deviceId == notifierParam.deviceId && + onLineStatus == true) { + isCheckOk = true; + } + }; + g_mgr.SetStoreStatusNotifier(databaseStatusNotifyCallbackV2); +} + +CipherPassword GetPassWord(FuzzedDataProvider &fdp) +{ + CipherPassword passwd; + size_t size = fdp.ConsumeIntegralInRange(0, PASSWDLEN); + uint8_t *val = static_cast(new uint8_t[size]); + fdp.ConsumeData(val, size); + passwd.SetValue(val, size); + delete[] static_cast(val); + val = nullptr; + return passwd; +} + +void SetAutoLaunchFuzz(FuzzedDataProvider &fdp) +{ + int securityLabel = fdp.ConsumeIntegralInRange(SecurityLabel::INVALID_SEC_LABEL, SecurityLabel::S4); + int securityFlag = fdp.ConsumeIntegralInRange(SecurityFlag::INVALID_SEC_FLAG, SecurityFlag::SECE); + AutoLaunchOption option = { + .createIfNecessary = fdp.ConsumeBool(), + .isEncryptedDb = fdp.ConsumeBool(), + .cipher = static_cast(fdp.ConsumeIntegralInRange(MIN_CIPHER_TYPE, MAX_CIPHER_TYPE)), + .passwd = GetPassWord(fdp), + .schema = fdp.ConsumeRandomLengthString(), + .createDirByStoreIdOnly = fdp.ConsumeBool(), + .dataDir = fdp.ConsumeRandomLengthString(), + .conflictType = fdp.ConsumeIntegral(), + .notifier = nullptr, + .secOption = {static_cast(securityLabel), static_cast(securityFlag)}, + .isNeedIntegrityCheck = fdp.ConsumeBool(), + .isNeedRmCorruptedDb = fdp.ConsumeBool(), + .isNeedCompressOnSync = fdp.ConsumeBool(), + .compressionRate = fdp.ConsumeIntegralInRange(MIN_COUNT, MAX_COMPRESSION_RATE), + .isAutoSync = fdp.ConsumeBool(), + .storeObserver = nullptr, + .syncDualTupleMode = fdp.ConsumeBool(), + .iterateTimes = fdp.ConsumeIntegral(), + .conflictResolvePolicy = fdp.ConsumeIntegralInRange(MIN_POLICY, MAX_POLICY), + .tableMode = static_cast(fdp.ConsumeIntegralInRange(MIN_MODE, MAX_MODE)), + }; + std::string appId = fdp.ConsumeRandomLengthString(); + std::string userId = fdp.ConsumeRandomLengthString(); + std::string storeId = fdp.ConsumeRandomLengthString(); + const AutoLaunchParam encryptedParam = { + .userId = fdp.ConsumeRandomLengthString(), + .appId = fdp.ConsumeRandomLengthString(), + .storeId = fdp.ConsumeRandomLengthString(), + .option = option, + .notifier = nullptr, + .path = fdp.ConsumeRandomLengthString(), + .subUser = fdp.ConsumeRandomLengthString(), + }; + std::string id = g_mgr.GetKvStoreIdentifier(userId, appId, storeId); + const AutoLaunchRequestCallback callback = [&](const std::string &identifier, AutoLaunchParam ¶m) { + if (id != identifier) { + return false; + } + param = encryptedParam; + return true; + }; + g_mgr.SetAutoLaunchRequestCallback(callback); +} + +>>>>>>> 078f60bcc (observer入参类型调整为智能指针,检视) void CombineTest(FuzzedDataProvider &fdp) { LOGD("Begin KvDelegateManagerFuzzer"); diff --git a/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/include/distributeddb_nb_test_tools.h b/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/include/distributeddb_nb_test_tools.h index 03eaf49aab4..cb78b530b9a 100644 --- a/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/include/distributeddb_nb_test_tools.h +++ b/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/include/distributeddb_nb_test_tools.h @@ -47,7 +47,7 @@ struct Option { #endif // endif of RELEASE_MODE_V2 #ifdef RELEASE_MODE_V3 DistributedDB::SecurityOption secOption; - std::weak_ptr observer; + std::shared_ptr observer; DistributedDB::Key key; unsigned int mode = 0; int conflictType = 0; @@ -67,7 +67,7 @@ struct Option { #ifdef RELEASE_MODE_V3 Option(bool createIfNecessary, bool isMemoryDb, bool isEncryptedDb, const DistributedDB::CipherType &cipher, const std::vector &passwd, const DistributedDB::SecurityOption &secOption, - const std::weak_ptr &observer, + std::shared_ptr observer, const DistributedDB::KvStoreNbConflictNotifier ¬ifier) : createIfNecessary(createIfNecessary), isMemoryDb(isMemoryDb), isEncryptedDb(isEncryptedDb), cipher(cipher), passwd(passwd), secOption(secOption), observer(observer), notifier(notifier) @@ -233,10 +233,10 @@ public: static DistributedDB::DBStatus RegisterObserver(DistributedDB::KvStoreNbDelegate &kvStoreNbDelegate, const DistributedDB::Key &key, unsigned int mode, - const std::weak_ptr &observer); + std::shared_ptr observer); static DistributedDB::DBStatus UnRegisterObserver(DistributedDB::KvStoreNbDelegate &kvStoreNbDelegate, - const std::weak_ptr &observer); + std::shared_ptr observer); static bool CloseNbAndRelease(DistributedDB::KvStoreDelegateManager *&manager, DistributedDB::KvStoreNbDelegate *&delegate); static bool ModifyDatabaseFile(const std::string &fileDir); diff --git a/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/src/distributeddb_nb_test_tools.cpp b/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/src/distributeddb_nb_test_tools.cpp index e252902fc36..c38db02870f 100644 --- a/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/src/distributeddb_nb_test_tools.cpp +++ b/frameworks/libs/distributeddb/test/moduletest/common/distributeddb/src/distributeddb_nb_test_tools.cpp @@ -443,13 +443,13 @@ DistributedDB::DBStatus DistributedDBNbTestTools::DeleteLocalBatch(DistributedDB } DistributedDB::DBStatus DistributedDBNbTestTools::RegisterObserver(DistributedDB::KvStoreNbDelegate &kvStoreNbDelegate, - const DistributedDB::Key &key, unsigned int mode, const std::weak_ptr &observer) + const DistributedDB::Key &key, unsigned int mode, std::shared_ptr observer) { return kvStoreNbDelegate.RegisterObserver(key, mode, observer); } DistributedDB::DBStatus DistributedDBNbTestTools::UnRegisterObserver( - DistributedDB::KvStoreNbDelegate &kvStoreNbDelegate, const std::weak_ptr &observer) + DistributedDB::KvStoreNbDelegate &kvStoreNbDelegate, std::shared_ptr observer) { return kvStoreNbDelegate.UnRegisterObserver(observer); } diff --git a/frameworks/libs/distributeddb/test/moduletest/src/distributeddb_nb_observer_test.cpp b/frameworks/libs/distributeddb/test/moduletest/src/distributeddb_nb_observer_test.cpp index 5aee191f9c2..d4e9424af72 100644 --- a/frameworks/libs/distributeddb/test/moduletest/src/distributeddb_nb_observer_test.cpp +++ b/frameworks/libs/distributeddb/test/moduletest/src/distributeddb_nb_observer_test.cpp @@ -1058,7 +1058,7 @@ HWTEST_F(DistributeddbNbObserverTest, Pressure004, TestSize.Level1) * @tc.steps: step1. UnRegister nullpter. * @tc.expected: step1. it will be failed to unregister nullptr and return INVALID_ARGS. */ - std::weak_ptr observer; + std::shared_ptr observer; DBStatus status = g_nbObserverDelegate->UnRegisterObserver(observer); EXPECT_EQ(status, INVALID_ARGS); diff --git a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_single_version_result_set_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_single_version_result_set_test.cpp index 5aecebb72d2..5faec617ece 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_single_version_result_set_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/interfaces/distributeddb_interfaces_single_version_result_set_test.cpp @@ -563,7 +563,7 @@ HWTEST_F(DistributedDBInterfacesSingleVersionResultSetTest, SingleVerNaturalStor std::map versionMap; EXPECT_EQ(storeObj.GetCloudVersion("", versionMap), -E_INTERNAL_ERROR); #endif - std::weak_ptr observer; + std::shared_ptr observer; EXPECT_EQ(storeObj.UnRegisterObserverAction(observer), -E_INTERNAL_ERROR); ObserverAction action; EXPECT_EQ(storeObj.RegisterObserverAction(observer, action), -E_INTERNAL_ERROR); -- Gitee From 5b0cf9f869e2c5e788cafc49869fdd95f9738646 Mon Sep 17 00:00:00 2001 From: lobty Date: Thu, 16 Oct 2025 10:06:29 +0800 Subject: [PATCH 2/2] codecheck Signed-off-by: lobty --- .../interfaces/src/kv_store_nb_delegate_impl.cpp | 12 ++++++------ .../interfaces/src/kv_store_nb_delegate_impl.h | 13 +++++++------ .../storage/include/ikvdb_connection.h | 4 ++-- .../storage/src/kv/generic_kvdb_connection.cpp | 4 ++-- .../storage/src/kv/generic_kvdb_connection.h | 4 ++-- .../storage/src/sqlite/sqlite_cloud_kv_store.cpp | 4 ++-- .../storage/src/sqlite/sqlite_cloud_kv_store.h | 4 ++-- .../src/sqlite/sqlite_single_ver_natural_store.h | 4 ++-- .../sqlite_single_ver_natural_store_connection.cpp | 4 ++-- .../sqlite_single_ver_natural_store_connection.h | 4 ++-- .../sqlite_single_ver_natural_store_extend.cpp | 4 ++-- 11 files changed, 31 insertions(+), 30 deletions(-) diff --git a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp index c3c5f461d97..f5b5b6ce2d2 100644 --- a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp +++ b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.cpp @@ -393,7 +393,7 @@ DBStatus KvStoreNbDelegateImpl::RegisterObserver(const Key &key, unsigned int mo } DBStatus KvStoreNbDelegateImpl::CheckDeviceObserver(const Key &key, unsigned int mode, - std::shared_ptr observer) + const std::shared_ptr &observer) { if (!ParamCheckUtils::CheckObserver(key, mode)) { LOGE("[KvStoreNbDelegate][CheckDeviceObserver] Register nb observer by illegal mode or key size!"); @@ -413,7 +413,7 @@ DBStatus KvStoreNbDelegateImpl::CheckDeviceObserver(const Key &key, unsigned int } DBStatus KvStoreNbDelegateImpl::RegisterDeviceObserver(const Key &key, unsigned int mode, - std::shared_ptr observer) + const std::shared_ptr &observer) { if (conn_->IsTransactionStarted()) { LOGE("[KvStoreNbDelegate][RegisterDeviceObserver] Transaction unfinished"); @@ -453,7 +453,7 @@ DBStatus KvStoreNbDelegateImpl::RegisterDeviceObserver(const Key &key, unsigned return OK; } -DBStatus KvStoreNbDelegateImpl::CheckCloudObserver(std::shared_ptr observer) +DBStatus KvStoreNbDelegateImpl::CheckCloudObserver(const std::shared_ptr &observer) { if (cloudObserverMap_.size() >= DBConstant::MAX_OBSERVER_COUNT) { LOGE("[KvStoreNbDelegate][CheckCloudObserver] The number of kv cloud observers over limit, storeId[%.3s]", @@ -468,7 +468,7 @@ DBStatus KvStoreNbDelegateImpl::CheckCloudObserver(std::shared_ptr observer) + const std::shared_ptr &observer) { std::lock_guard lockGuard(observerMapLock_); DBStatus status = CheckCloudObserver(observer); @@ -519,7 +519,7 @@ DBStatus KvStoreNbDelegateImpl::UnRegisterObserver(std::shared_ptr observer) +DBStatus KvStoreNbDelegateImpl::UnRegisterDeviceObserver(const std::shared_ptr &observer) { std::lock_guard lockGuard(observerMapLock_); auto iter = observerMap_.find(observer); @@ -538,7 +538,7 @@ DBStatus KvStoreNbDelegateImpl::UnRegisterDeviceObserver(std::shared_ptr observer) +DBStatus KvStoreNbDelegateImpl::UnRegisterCloudObserver(const std::shared_ptr &observer) { std::lock_guard lockGuard(observerMapLock_); auto iter = cloudObserverMap_.find(observer); diff --git a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h index 15247c5cbf4..b7857fc221a 100644 --- a/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h +++ b/frameworks/libs/distributeddb/interfaces/src/kv_store_nb_delegate_impl.h @@ -219,17 +219,18 @@ private: void OnDeviceSyncProcess(const std::map &processMap, const DeviceSyncProcessCallback &onProcess) const; - DBStatus RegisterDeviceObserver(const Key &key, unsigned int mode, std::shared_ptr observer); + DBStatus RegisterDeviceObserver(const Key &key, unsigned int mode, + const std::shared_ptr &observer); - DBStatus RegisterCloudObserver(const Key &key, unsigned int mode, std::shared_ptr observer); + DBStatus RegisterCloudObserver(const Key &key, unsigned int mode, const std::shared_ptr &observer); - DBStatus UnRegisterDeviceObserver(std::shared_ptr observer); + DBStatus UnRegisterDeviceObserver(const std::shared_ptr &observer); - DBStatus UnRegisterCloudObserver(std::shared_ptr observer); + DBStatus UnRegisterCloudObserver(const std::shared_ptr &observer); - DBStatus CheckDeviceObserver(const Key &key, unsigned int mode, std::shared_ptr observer); + DBStatus CheckDeviceObserver(const Key &key, unsigned int mode, const std::shared_ptr &observer); - DBStatus CheckCloudObserver(std::shared_ptr observer); + DBStatus CheckCloudObserver(const std::shared_ptr &observer); IKvDBConnection *conn_; std::string storeId_; diff --git a/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h b/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h index 617353638cc..a432cbd91e0 100644 --- a/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h +++ b/frameworks/libs/distributeddb/storage/include/ikvdb_connection.h @@ -153,10 +153,10 @@ public: virtual int32_t GetTaskCount() = 0; - virtual int RegisterObserverAction(std::shared_ptr observer, + virtual int RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action) = 0; - virtual int UnRegisterObserverAction(std::shared_ptr observer) = 0; + virtual int UnRegisterObserverAction(const std::shared_ptr &observer) = 0; virtual void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) = 0; diff --git a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp index 86840a212f7..66600bafee5 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.cpp @@ -410,13 +410,13 @@ int32_t GenericKvDBConnection::GetTaskCount() return 0; } -int GenericKvDBConnection::RegisterObserverAction([[gnu::unused]] std::shared_ptr observer, +int GenericKvDBConnection::RegisterObserverAction([[gnu::unused]] const std::shared_ptr &observer, [[gnu::unused]] const ObserverAction &action) { return E_OK; } -int GenericKvDBConnection::UnRegisterObserverAction([[gnu::unused]] std::shared_ptr observer) +int GenericKvDBConnection::UnRegisterObserverAction([[gnu::unused]] const std::shared_ptr &observer) { return E_OK; } diff --git a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h index c5d4e6a580a..6f12c5922b2 100644 --- a/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h +++ b/frameworks/libs/distributeddb/storage/src/kv/generic_kvdb_connection.h @@ -95,9 +95,9 @@ public: int32_t GetTaskCount() override; - int RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) override; + int RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action) override; - int UnRegisterObserverAction(std::shared_ptr observer) override; + int UnRegisterObserverAction(const std::shared_ptr &observer) override; void SetGenCloudVersionCallback(const GenerateCloudVersionCallback &callback) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp index 2537a793ff4..6a5dada1e67 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.cpp @@ -515,14 +515,14 @@ std::pair SqliteCloudKvStore::GetTr return {db, transactionHandle_}; } -void SqliteCloudKvStore::RegisterObserverAction(std::shared_ptr observer, +void SqliteCloudKvStore::RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action) { std::lock_guard autoLock(observerMapMutex_); cloudObserverMap_[observer] = action; } -void SqliteCloudKvStore::UnRegisterObserverAction(std::shared_ptr observer) +void SqliteCloudKvStore::UnRegisterObserverAction(const std::shared_ptr &observer) { std::lock_guard autoLock(observerMapMutex_); cloudObserverMap_.erase(observer); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h index 227ce28a532..9accc25fb27 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_cloud_kv_store.h @@ -91,9 +91,9 @@ public: int SetCloudDbSchema(const std::map &schema); - void RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action); + void RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action); - void UnRegisterObserverAction(std::shared_ptr observer); + void UnRegisterObserverAction(const std::shared_ptr &observer); int GetCloudVersion(const std::string &device, std::map &versionMap); diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h index 1b7f491aac5..e13ca329496 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store.h @@ -221,9 +221,9 @@ public: int SetCloudDbSchema(const std::map &schema); - int RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action); + int RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action); - int UnRegisterObserverAction(std::shared_ptr observer); + int UnRegisterObserverAction(const std::shared_ptr &observer); void SetReceiveDataInterceptor(const DataInterceptor &interceptor) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp index af6517d71ba..2e79c639d72 100755 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.cpp @@ -1933,7 +1933,7 @@ int SQLiteSingleVerNaturalStoreConnection::SetCloudDbSchema(const std::map observer, +int SQLiteSingleVerNaturalStoreConnection::RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action) { auto naturalStore = GetDB(); @@ -1944,7 +1944,7 @@ int SQLiteSingleVerNaturalStoreConnection::RegisterObserverAction(std::shared_pt return naturalStore->RegisterObserverAction(observer, action); } -int SQLiteSingleVerNaturalStoreConnection::UnRegisterObserverAction(std::shared_ptr observer) +int SQLiteSingleVerNaturalStoreConnection::UnRegisterObserverAction(const std::shared_ptr &observer) { auto naturalStore = GetDB(); if (naturalStore == nullptr) { diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h index 9219146516b..4dc0a5457c9 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_connection.h @@ -106,9 +106,9 @@ public: int UpdateKey(const UpdateKeyCallback &callback) override; - int RegisterObserverAction(std::shared_ptr observer, const ObserverAction &action) override; + int RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action) override; - int UnRegisterObserverAction(std::shared_ptr observer) override; + int UnRegisterObserverAction(const std::shared_ptr &observer) override; int RemoveDeviceData(const std::string &device, ClearMode mode) override; diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp index 983a85bf7e8..7c39ca31e9c 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/sqlite_single_ver_natural_store_extend.cpp @@ -641,7 +641,7 @@ TimeOffset SQLiteSingleVerNaturalStore::GetLocalTimeOffsetForCloud() return GetLocalTimeOffset(); } -int SQLiteSingleVerNaturalStore::RegisterObserverAction(std::shared_ptr observer, +int SQLiteSingleVerNaturalStore::RegisterObserverAction(const std::shared_ptr &observer, const ObserverAction &action) { #ifdef USE_DISTRIBUTEDDB_CLOUD @@ -654,7 +654,7 @@ int SQLiteSingleVerNaturalStore::RegisterObserverAction(std::shared_ptr observer) +int SQLiteSingleVerNaturalStore::UnRegisterObserverAction(const std::shared_ptr &observer) { #ifdef USE_DISTRIBUTEDDB_CLOUD std::lock_guard autoLock(cloudStoreMutex_); -- Gitee