diff --git a/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn b/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn index 54392bffff00f494340fe1a7013c090819c3d76a..b3c1e72797f90adad5c737f907a0601ae34be7a2 100644 --- a/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn +++ b/services/distributeddataservice/adapter/communicator/test/fuzztest/softbusadapter_fuzzer/BUILD.gn @@ -35,7 +35,17 @@ ohos_fuzztest("SoftBusAdapterFuzzTest") { "-fno-omit-frame-pointer", ] - sources = [ "softbusadapter_fuzzer.cpp" ] + sources = [ + "${data_service_path}/adapter/communicator/src/communicator_context.cpp", + "${data_service_path}/adapter/communicator/src/device_manager_adapter.cpp", + "${data_service_path}/adapter/communicator/src/softbus_client.cpp", + "softbusadapter_fuzzer.cpp", + ] + + deps = [ + "${data_service_path}/adapter/utils:distributeddata_utils", + "${data_service_path}/framework:distributeddatasvcfwk", + ] external_deps = [ "access_token:libaccesstoken_sdk", diff --git a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp index f6a82fe28a770ad1f9d5f56adb94e2a1951739b1..35e0ceaff77ffcbcc0885856e928c5e7046bbd41 100644 --- a/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp +++ b/services/distributeddataservice/adapter/dfx/src/hiview_adapter.cpp @@ -135,6 +135,7 @@ void HiViewAdapter::ReportDBFault(int dfxCode, const DBFaultMsg &msg, std::share return; } ExecutorPool::Task task([dfxCode, msg]() { + std::string storeId = Anonymous::Change(msg.storeId); struct HiSysEventParam params[] = { { .name = { *APP_ID }, .t = HISYSEVENT_STRING, @@ -142,7 +143,7 @@ void HiViewAdapter::ReportDBFault(int dfxCode, const DBFaultMsg &msg, std::share .arraySize = 0 }, { .name = { *STORE_ID }, .t = HISYSEVENT_STRING, - .v = { .s = const_cast(Anonymous::Change(msg.storeId).c_str()) }, + .v = { .s = const_cast(storeId.c_str()) }, .arraySize = 0 }, { .name = { *MODULE_NAME }, .t = HISYSEVENT_STRING, @@ -172,6 +173,7 @@ void HiViewAdapter::ReportCommFault(int dfxCode, const CommFaultMsg &msg, std::s } ExecutorPool ::Task task([dfxCode, msg]() { std::string message; + std::string storeId = Anonymous::Change(msg.storeId); for (size_t i = 0; i < msg.deviceId.size(); i++) { message.append("No: ").append(std::to_string(i)) .append(" sync to device: ").append(msg.deviceId[i]) @@ -188,7 +190,7 @@ void HiViewAdapter::ReportCommFault(int dfxCode, const CommFaultMsg &msg, std::s .arraySize = 0 }, { .name = { *STORE_ID }, .t = HISYSEVENT_STRING, - .v = { .s = const_cast(Anonymous::Change(msg.storeId).c_str()) }, + .v = { .s = const_cast(storeId.c_str()) }, .arraySize = 0 }, { .name = { *SYNC_ERROR_INFO }, .t = HISYSEVENT_STRING, @@ -244,6 +246,7 @@ void HiViewAdapter::ReportBehaviour(int dfxCode, const BehaviourMsg &msg, std::s } ExecutorPool::Task task([dfxCode, msg]() { std::string message; + std::string storeId = Anonymous::Change(msg.storeId); message.append("Behaviour type : ").append(std::to_string(static_cast(msg.behaviourType))) .append(" behaviour info : ").append(msg.extensionInfo); struct HiSysEventParam params[] = { @@ -257,7 +260,7 @@ void HiViewAdapter::ReportBehaviour(int dfxCode, const BehaviourMsg &msg, std::s .arraySize = 0 }, { .name = { *STORE_ID }, .t = HISYSEVENT_STRING, - .v = { .s = const_cast(Anonymous::Change(msg.storeId).c_str()) }, + .v = { .s = const_cast(storeId.c_str()) }, .arraySize = 0 }, { .name = { *BEHAVIOUR_INFO }, .t = HISYSEVENT_STRING, @@ -303,7 +306,7 @@ void HiViewAdapter::ReportDbSize(const StatisticWrap &stat) if (!vh.CalcValueHash(stat.val.userId, userId)) { return; } - + std::string storeId = Anonymous::Change(stat.val.storeId); struct HiSysEventParam params[] = { { .name = { *USER_ID }, .t = HISYSEVENT_STRING, @@ -315,7 +318,7 @@ void HiViewAdapter::ReportDbSize(const StatisticWrap &stat) .arraySize = 0 }, { .name = { *STORE_ID }, .t = HISYSEVENT_STRING, - .v = { .s = const_cast(Anonymous::Change(stat.val.storeId).c_str()) }, + .v = { .s = const_cast(storeId.c_str()) }, .arraySize = 0 }, { .name = { *DB_SIZE }, .t = HISYSEVENT_UINT64, .v = { .ui64 = dbSize }, .arraySize = 0 }, }; @@ -384,6 +387,7 @@ void HiViewAdapter::InvokeTraffic() if (!vh.CalcValueHash(kv.second.val.deviceId, deviceId)) { continue; } + std::string devId = Anonymous::Change(deviceId); struct HiSysEventParam params[] = { { .name = { *TAG }, .t = HISYSEVENT_STRING, .v = { .s = const_cast(POWERSTATS) }, .arraySize = 0 }, { .name = { *APP_ID }, @@ -392,7 +396,7 @@ void HiViewAdapter::InvokeTraffic() .arraySize = 0 }, { .name = { *DEVICE_ID }, .t = HISYSEVENT_STRING, - .v = { .s = const_cast(Anonymous::Change(deviceId).c_str()) }, + .v = { .s = const_cast(devId.c_str()) }, .arraySize = 0 }, { .name = { *SEND_SIZE }, .t = HISYSEVENT_INT64, diff --git a/services/distributeddataservice/adapter/schema_helper/BUILD.gn b/services/distributeddataservice/adapter/schema_helper/BUILD.gn index c3516c441a77a4c80451f2493c5568a9e1419b4a..4bdf79b0c67a044f0784069be32aeb14dd6a3e43 100644 --- a/services/distributeddataservice/adapter/schema_helper/BUILD.gn +++ b/services/distributeddataservice/adapter/schema_helper/BUILD.gn @@ -31,7 +31,6 @@ ohos_source_set("distributeddata_schema_helper") { cflags_cc = [ "-fvisibility=hidden", - "-fstack-protector-strong", ] public_configs = [ ":module_public_config" ] diff --git a/services/distributeddataservice/adapter/screenlock/BUILD.gn b/services/distributeddataservice/adapter/screenlock/BUILD.gn index 766eb3f9e9d0530888a346939f9609656e56ad60..453ee8212d056adc850f1bc5cc1dcc05065fa43e 100644 --- a/services/distributeddataservice/adapter/screenlock/BUILD.gn +++ b/services/distributeddataservice/adapter/screenlock/BUILD.gn @@ -31,7 +31,6 @@ ohos_source_set("distributeddata_screenlock") { cflags_cc = [ "-fvisibility=hidden", - "-fstack-protector-strong", ] public_configs = [ ":module_public_config" ] diff --git a/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp b/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp index 9374568e57385ac1ca1fa5a348bfa796271fd8a5..5384155bcb5dc94b87ca1164aa6286e4ddb4fe07 100644 --- a/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp +++ b/services/distributeddataservice/adapter/screenlock/test/screen_lock_test.cpp @@ -53,6 +53,8 @@ public: void TearDown() { } +protected: + static constexpr int32_t MAX_RETRY_TIME = 300; }; /** @@ -125,4 +127,57 @@ HWTEST_F(ScreenLockTest, SubscribeScreenEvent001, TestSize.Level0) screenLock->UnsubscribeScreenEvent(); screenLock->executors_ = nullptr; } + +/** +* @tc.name: SubscribeScreenEvent002 +* @tc.desc: subscribe ScreenEvent +* @tc.type: FUNC +* @tc.author: +*/ +HWTEST_F(ScreenLockTest, SubscribeScreenEvent002, TestSize.Level0) +{ + auto screenLock = std::make_shared(); + EXPECT_EQ(screenLock->executors_, nullptr); + EXPECT_EQ(screenLock->eventSubscriber_, nullptr); + screenLock->SubscribeScreenEvent(); + EXPECT_NE(screenLock->eventSubscriber_, nullptr); + screenLock->UnsubscribeScreenEvent(); +} + +/** +* @tc.name: GetTask001 +* @tc.desc: subscribe ScreenEvent +* @tc.type: FUNC +* @tc.author: +*/ +HWTEST_F(ScreenLockTest, GetTask001, TestSize.Level0) +{ + auto screenLock = std::make_shared(); + EXPECT_EQ(screenLock->executors_, nullptr); + EXPECT_EQ(screenLock->eventSubscriber_, nullptr); + auto Task = screenLock->GetTask(0); + Task(); + auto executor = std::make_shared(12, 5); + screenLock->BindExecutor(executor); + ASSERT_NE(screenLock->executors_, nullptr); + Task = screenLock->GetTask(MAX_RETRY_TIME - 1); + Task(); +} + +/** +* @tc.name: GetTask002 +* @tc.desc: subscribe ScreenEvent +* @tc.type: FUNC +* @tc.author: +*/ +HWTEST_F(ScreenLockTest, GetTask002, TestSize.Level0) +{ + auto screenLock = std::make_shared(); + EXPECT_EQ(screenLock->executors_, nullptr); + EXPECT_EQ(screenLock->eventSubscriber_, nullptr); + screenLock->SubscribeScreenEvent(); + EXPECT_NE(screenLock->eventSubscriber_, nullptr); + auto Task = screenLock->GetTask(0); + Task(); +} } // namespace \ No newline at end of file diff --git a/services/distributeddataservice/service/kvdb/kvdb_general_store.h b/services/distributeddataservice/service/kvdb/kvdb_general_store.h index 80e030f05502be847d9d51d4c89f70fc2ca9d1f3..b5849b0182705bfbd3ac8e54ceed7cfc52feefc5 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_general_store.h +++ b/services/distributeddataservice/service/kvdb/kvdb_general_store.h @@ -97,8 +97,8 @@ private: DBProcessCB GetDBProcessCB(DetailAsync async); DBStatus CloudSync(const Devices &devices, DistributedDB::SyncMode cloudSyncMode, DetailAsync async, int64_t wait, const std::string &prepareTraceId); - void GetIdentifierParams( - std::vector &devices, const std::vector &uuids, int32_t authType); + void GetIdentifierParams(std::vector &devices, + const std::vector &uuids, int32_t authType); void Report(const std::string &faultType, int32_t errCode, const std::string &appendix); class ObserverProxy : public DistributedDB::KvStoreObserver { public: diff --git a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index c497a1288eb9c3d409f49d361888856b94368bbb..5bce7eda4fb46886a5e4551eca23119f1459e490 100644 --- a/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -104,9 +104,9 @@ void KVDBServiceImpl::Init() StoreMetaMapping meta(storeInfo); meta.deviceId = DMAdapter::GetInstance().GetLocalDevice().uuid; if (!MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), meta, true)) { + ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s, user = %{public}s", meta.bundleName.c_str(), + meta.GetStoreAlias().c_str(), meta.user.c_str()); if (meta.user == "0") { - ZLOGE("meta empty, bundleName:%{public}s, storeId:%{public}s, user = %{public}s", - meta.bundleName.c_str(), meta.GetStoreAlias().c_str(), meta.user.c_str()); return; } meta.user = "0"; diff --git a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp index c34db72a617fcc3173e4615191b60aef0798a614..3566bb00790b96cd89b6509dd990ffe276c6cc99 100644 --- a/services/distributeddataservice/service/test/kvdb_general_store_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_general_store_test.cpp @@ -59,10 +59,9 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - protected: - static constexpr const char *bundleName = "test_distributeddata"; - static constexpr const char *storeName = "test_service_meta"; + static constexpr const char *BUNDLE_NAME = "test_distributeddata"; + static constexpr const char *STORE_NAME = "test_service_meta"; void InitMetaData(); static std::vector Random(uint32_t len); @@ -76,15 +75,15 @@ static const uint32_t KEY_LENGTH = 32; void KVDBGeneralStoreTest::InitMetaData() { - metaData_.bundleName = bundleName; - metaData_.appId = bundleName; + metaData_.bundleName = BUNDLE_NAME; + metaData_.appId = BUNDLE_NAME; metaData_.user = "0"; metaData_.area = OHOS::DistributedKv::EL1; metaData_.instanceId = 0; metaData_.isAutoSync = true; metaData_.storeType = DistributedKv::KvStoreType::SINGLE_VERSION; - metaData_.storeId = storeName; - metaData_.dataDir = "/data/service/el1/public/database/" + std::string(bundleName) + "/kvdb"; + metaData_.storeId = STORE_NAME; + metaData_.dataDir = "/data/service/el1/public/database/" + std::string(BUNDLE_NAME) + "/kvdb"; metaData_.securityLevel = SecurityLevel::S2; } @@ -404,7 +403,7 @@ HWTEST_F(KVDBGeneralStoreTest, BusyClose, TestSize.Level0) HWTEST_F(KVDBGeneralStoreTest, SyncTest, TestSize.Level0) { ZLOGI("SyncTest start"); - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -540,14 +539,14 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync001, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); KvStoreNbDelegateMock mockDelegate; store->delegate_ = &mockDelegate; std::vector devices = { "device1", "device2" }; auto asyncs = [](const GenDetails &result) {}; store->storeInfo_.user = 0; auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); std::string prepareTraceId; std::vector users; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)).Times(1).WillOnce(DoAll(SetArgReferee<0>(users), Return(false))); @@ -568,20 +567,22 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync002, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); KvStoreNbDelegateMock mockDelegate; store->delegate_ = &mockDelegate; std::vector devices = { "device1", "device2" }; auto asyncs = [](const GenDetails &result) {}; store->storeInfo_.user = 0; auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); std::string prepareTraceId; std::vector users; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) .Times(1) - .WillOnce( - DoAll(SetArgReferee<0>(users), Invoke([](std::vector &users) { users.clear(); }), Return(true))); + .WillOnce(DoAll( + SetArgReferee<0>(users), + Invoke([](std::vector& users) { users.clear(); }), + Return(true))); auto ret = store->CloudSync(devices, cloudSyncMode, asyncs, 0, prepareTraceId); EXPECT_EQ(ret, DBStatus::DB_ERROR); @@ -604,14 +605,14 @@ HWTEST_F(KVDBGeneralStoreTest, CloudSync003, TestSize.Level0) { auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); KvStoreNbDelegateMock mockDelegate; store->delegate_ = &mockDelegate; std::vector devices = { "device1", "device2" }; auto asyncs = [](const GenDetails &result) {}; store->storeInfo_.user = 0; auto cloudSyncMode = DistributedDB::SyncMode::SYNC_MODE_PUSH_ONLY; - store->SetEqualIdentifier(bundleName, storeName); + store->SetEqualIdentifier(BUNDLE_NAME, STORE_NAME); std::string prepareTraceId; std::vector users = {0, 1}; EXPECT_CALL(*accountDelegateMock, QueryUsers(_)) @@ -654,7 +655,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetIdentifierParams, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) { - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -689,7 +690,7 @@ HWTEST_F(KVDBGeneralStoreTest, Sync002, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, Sync003, TestSize.Level0) { - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -974,7 +975,7 @@ HWTEST_F(KVDBGeneralStoreTest, GetDBOption001, TestSize.Level0) */ HWTEST_F(KVDBGeneralStoreTest, Sync001, TestSize.Level0) { - mkdir(("/data/service/el1/public/database/" + std::string(bundleName)).c_str(), + mkdir(("/data/service/el1/public/database/" + std::string(BUNDLE_NAME)).c_str(), (S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)); auto store = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store, nullptr); @@ -1209,7 +1210,7 @@ HWTEST_F(KVDBGeneralStoreTest, ConstructorTest, TestSize.Level0) delete store3; // Test observer registration - metaData_.storeId = storeName; + metaData_.storeId = STORE_NAME; metaData_.storeType = DistributedKv::KvStoreType::SINGLE_VERSION; auto store4 = new (std::nothrow) KVDBGeneralStore(metaData_); ASSERT_NE(store4, nullptr); diff --git a/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp b/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp index 039577976be7a8734d7a87fad25c39288a4dbbfc..1d0d7d625033cbec3f486044c71eda09b7c9fc87 100644 --- a/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_service_impl_test.cpp @@ -325,6 +325,31 @@ HWTEST_F(KvdbServiceImplTest, OnInitialize001, TestSize.Level0) EXPECT_EQ(result, 1); // CODE_SYNC } +/** +* @tc.name: OnInitialize002 +* @tc.desc: OnInitialize function test. +* @tc.type: FUNC +* @tc.author: my +*/ +HWTEST_F(KvdbServiceImplTest, OnInitialize002, TestSize.Level0) +{ + auto result = kvdbServiceImpl_->OnInitialize(); + EXPECT_EQ(result, Status::SUCCESS); + DistributedData::StoreInfo storeInfo; + storeInfo.bundleName = "bundleName"; + storeInfo.storeName = "storeName"; + storeInfo.user = 100; + + EXPECT_CALL(*metaDataManagerMock, LoadMeta(testing::_, testing::_, testing::_)) + .WillOnce(testing::Return(false)) + .WillRepeatedly(testing::Return(true)); + + auto event = std::make_unique(CloudEvent::CLOUD_SYNC, storeInfo); + EXPECT_NE(event, nullptr); + result = EventCenter::GetInstance().PostEvent(move(event)); + EXPECT_EQ(result, 1); // CODE_SYNC +} + /** * @tc.name: GetStoreIdsTest001 * @tc.desc: GetStoreIds