diff --git a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp index 1472fdd593d2239797847c6403e2162908e11bf3..005cfc6da20319130dd7d2cf466e010effb313cb 100644 --- a/services/distributeddataservice/app/src/kvstore_meta_manager.cpp +++ b/services/distributeddataservice/app/src/kvstore_meta_manager.cpp @@ -652,8 +652,8 @@ void KvStoreMetaManager::CheckMetaDeviceId() if (deviceMeta.newUuid != localUuid) { UpdateStoreMetaData(localUuid, deviceMeta.newUuid); UpdateMetaDatas(localUuid, deviceMeta.newUuid); - ZLOGI("meta changed! cur uuid:%{public}s, old uuid:%{public}s", Anonymous::Change(localUuid).c_str(), - Anonymous::Change(deviceMeta.newUuid).c_str()); + ZLOGI("meta changed! curruuid:%{public}s, localUuid:%{public}s", Anonymous::Change(deviceMeta.newUuid).c_str(), + localUuid.c_str()); deviceMeta.newUuid = localUuid; MetaDataManager::GetInstance().SaveMeta(deviceMeta.GetKey(), deviceMeta, true); } diff --git a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp index 3eb5413ef7a34faf0fed0d8d53f6ca6723bd4b87..e2c6ebfb48caf6778a57f34c43feebf0e5466550 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.cpp @@ -36,6 +36,22 @@ KVDBObserverProxy::KVDBObserverProxy(const sptr &impl) : IRemoteP { } +int64_t KVDBObserverProxy::CalTotalSize(const ChangeNotification &changeNotification, MessageParcel &data) +{ + const int errorResult = -1; + int64_t insertSize = ITypesUtil::GetTotalSize(changeNotification.GetInsertEntries()); + int64_t updateSize = ITypesUtil::GetTotalSize(changeNotification.GetUpdateEntries()); + int64_t deleteSize = ITypesUtil::GetTotalSize(changeNotification.GetDeleteEntries()); + int64_t totalSize = insertSize + updateSize + deleteSize; + if (insertSize < 0 || updateSize < 0 || deleteSize < 0 || !data.WriteInt64(totalSize)) { + ZLOGE("Write ChangeNotification buffer size to parcel failed. I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ")", + insertSize, updateSize, deleteSize); + return errorResult; + } + ZLOGD("I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ") T(%" PRId64 ")", insertSize, updateSize, deleteSize, totalSize); + return totalSize; +} + void KVDBObserverProxy::OnChange(const ChangeNotification &changeNotification) { MessageParcel data; @@ -44,25 +60,37 @@ void KVDBObserverProxy::OnChange(const ChangeNotification &changeNotification) ZLOGE("Write descriptor failed"); return; } - int64_t insertSize = ITypesUtil::GetTotalSize(changeNotification.GetInsertEntries()); - int64_t updateSize = ITypesUtil::GetTotalSize(changeNotification.GetUpdateEntries()); - int64_t deleteSize = ITypesUtil::GetTotalSize(changeNotification.GetDeleteEntries()); - int64_t totalSize = insertSize + updateSize + deleteSize + sizeof(uint32_t); - if (insertSize < 0 || updateSize < 0 || deleteSize < 0 || !data.WriteInt32(totalSize)) { - ZLOGE("Write ChangeNotification buffer size to parcel failed."); + int64_t totalSize = CalTotalSize(changeNotification, data); + if (totalSize == -1) { return; } - ZLOGD("I(%" PRId64 ") U(%" PRId64 ") D(%" PRId64 ") T(%" PRId64 ")", insertSize, updateSize, deleteSize, totalSize); if (totalSize < SWITCH_RAW_DATA_SIZE) { if (!ITypesUtil::Marshal(data, changeNotification)) { ZLOGW("Write ChangeNotification to parcel failed."); return; } } else { - if (!ITypesUtil::Marshal(data, changeNotification.GetDeviceId(), uint32_t(changeNotification.IsClear())) || - !ITypesUtil::MarshalToBuffer(changeNotification.GetInsertEntries(), insertSize, data) || - !ITypesUtil::MarshalToBuffer(changeNotification.GetUpdateEntries(), updateSize, data) || - !ITypesUtil::MarshalToBuffer(changeNotification.GetDeleteEntries(), deleteSize, data)) { + if (!ITypesUtil::Marshal(data, changeNotification.GetDeviceId(), uint32_t(changeNotification.IsClear()))) { + ZLOGE("write deviceId to parcel failed, devId:%{public}s, clear:%{public}d", + changeNotification.GetDeviceId().c_str(), changeNotification.IsClear()); + return; + } + if (!data.WriteInt32(changeNotification.GetInsertEntries().size()) || + !data.WriteInt32(changeNotification.GetUpdateEntries().size()) || + !data.WriteInt32(changeNotification.GetDeleteEntries().size())) { + ZLOGE("write change size to parcel failed, insert:%{public}zu, update:%{public}zu, delete:%{public}zu", + changeNotification.GetInsertEntries().size(), changeNotification.GetUpdateEntries().size(), + changeNotification.GetDeleteEntries().size()); + return; + } + std::vector totalEntries; + totalEntries.insert(totalEntries.end(), changeNotification.GetInsertEntries().begin(), + changeNotification.GetInsertEntries().end()); + totalEntries.insert(totalEntries.end(), changeNotification.GetUpdateEntries().begin(), + changeNotification.GetUpdateEntries().end()); + totalEntries.insert(totalEntries.end(), changeNotification.GetDeleteEntries().begin(), + changeNotification.GetDeleteEntries().end()); + if (!ITypesUtil::MarshalToBuffer(totalEntries, totalSize, data)) { ZLOGE("WriteChangeList to Parcel by buffer failed"); return; } diff --git a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h index 9a706fd260c99091ef1d8e401517c00b4f68e1cf..35d619dbc7c9071a3c490111f4202bb6e95892b1 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h +++ b/services/distributeddataservice/service/kvdb/kvdb_observer_proxy.h @@ -32,6 +32,7 @@ public: void OnChange(const DataOrigin &origin, Keys &&keys) override; private: static inline BrokerDelegator delegator_; + int64_t CalTotalSize(const ChangeNotification &changeNotification, MessageParcel &data); }; } // namespace DistributedKv } // namespace OHOS diff --git a/services/distributeddataservice/service/test/kvdb_service_test.cpp b/services/distributeddataservice/service/test/kvdb_service_test.cpp index 22689b6d91a80ebf07a49f1255c4fcbe4dd62333..face05a5461207f5c3930e80ff1ea51a4e86984b 100644 --- a/services/distributeddataservice/service/test/kvdb_service_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_service_test.cpp @@ -441,6 +441,31 @@ HWTEST_F(KVDBWatcherTest, OnChange002, TestSize.Level0) EXPECT_EQ(result, GeneralError::E_NOT_INIT); } +/** +* @tc.name: OnChange003 +* @tc.desc: OnChange test function. +* @tc.type: FUNC +* @tc.require: +* @tc.author: zhiyihang + */ +HWTEST_F(KVDBWatcherTest, OnChange003, TestSize.Level0) +{ + GeneralWatcher::Origin origin; + origin.store = "store"; + GeneralWatcher::Fields fields; + GeneralWatcher::ChangeData datas; + std::string testStr = "bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rA" + "0~tB1bQ6#xq@L9!zP7m$sV1%kY*4nC&jF(0lR)oU2^aH5-iT3+eW8=gD9;hG:0bJ1,K4?lN6.M7|pO8\\qS9/rAtesttesttesttesttest"; + datas["store"][OP_DELETE].push_back({std::string(testStr)}); + std::shared_ptr watcher = std::make_shared(); + sptr observer = nullptr; + watcher->SetObserver(observer); + EXPECT_EQ(watcher->observer_, nullptr); + auto result = watcher->OnChange(origin, fields, std::move(datas)); + EXPECT_EQ(result, GeneralError::E_NOT_INIT); +} + + /** * @tc.name: ConvertToEntries * @tc.desc: ConvertToEntries test the return result of input with different values.