From 64a3fc653058f4587f13ccbe6a5f4275e4885dab Mon Sep 17 00:00:00 2001 From: caochuan Date: Tue, 29 Aug 2023 09:24:10 +0800 Subject: [PATCH 1/4] fix: test case Signed-off-by: caochuan --- test/unittest/external_notify_test.cpp | 121 +++++++++++++++++++++++++ 1 file changed, 121 insertions(+) diff --git a/test/unittest/external_notify_test.cpp b/test/unittest/external_notify_test.cpp index 73e9bbde..f13ed46f 100644 --- a/test/unittest/external_notify_test.cpp +++ b/test/unittest/external_notify_test.cpp @@ -497,6 +497,127 @@ HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0003, testing::ext GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0003"; } +/** + * @tc.number: user_file_service_external_file_access_notify_0004 + * @tc.name: external_file_access_notify_0004 + * @tc.desc: Test function of RegisterNotify and UnregisterNotify interface for SUCCESS. + * @tc.size: MEDIUM + * @tc.type: FUNC + * @tc.level Level 1 + * @tc.require: SR000H0386 + */ +HWTEST_F(FileExtensionNotifyTest, external_file_access_notify_0004, testing::ext::TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-begin external_file_access_notify_0004"; + try { + vector info; + int result = g_fah->GetRoots(info); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + bool notifyForDescendants = true; + sptr myObserver1 = new (std::nothrow) MyObserver(); + sptr myObserver2 = new (std::nothrow) MyObserver(); + Uri parentUri(info[1].uri); + Uri newDirUriTest1(""); + result = g_fah->Mkdir(parentUri, "uri_dir14", newDirUriTest1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newDirUriTest2(""); + result = g_fah->Mkdir(parentUri, "uri_dir24", newDirUriTest2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newDirUriTest3(""); + result = g_fah->Mkdir(parentUri, "uri_dir34", newDirUriTest3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri1(""); + result = g_fah->CreateFile(parentUri, "uri_file14", newFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri2(""); + result = g_fah->CreateFile(parentUri, "uri_file24", newFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + Uri newFileUri3(""); + result = g_fah->CreateFile(parentUri, "uri_file34", newFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest1, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest2, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newDirUriTest3, notifyForDescendants, myObserver1); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri1, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri2, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->RegisterNotify(newFileUri3, notifyForDescendants, myObserver2); + EXPECT_GE(result, OHOS::FileAccessFwk::ERR_OK); + + Uri renameDirUri1(""); + result = g_fah->Rename(newDirUriTest1, "renameDir1", renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME * 2); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newDirUriTest1.ToString()); + Uri renameDirUri2(""); + result = g_fah->Rename(newDirUriTest2, "renameDir2", renameDirUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newDirUriTest2.ToString()); + Uri renameDirUri3(""); + result = g_fah->Rename(newDirUriTest3, "renameDir3", renameDirUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newDirUriTest3.ToString()); + + Uri renameFileUri1(""); + result = g_fah->Rename(newFileUri1, "renamefile1", renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME * 2); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + Uri renameFileUri2(""); + result = g_fah->Rename(newFileUri2, "renamefile2", renameFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newFileUri2.ToString()); + Uri renameFileUri3(""); + result = g_fah->Rename(newFileUri3, "renamefile3", renameFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + usleep(SLEEP_TIME); + EXPECT_EQ(g_notifyEvent, MOVED_SELF); + EXPECT_EQ(g_notifyUri, newFileUri3.ToString()); + + sleep(2); + result = g_fah->UnregisterNotify(newDirUriTest1, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newDirUriTest2, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newDirUriTest3, myObserver1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri1, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri2, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->UnregisterNotify(newFileUri3, myObserver2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + result = g_fah->Delete(renameDirUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameDirUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameDirUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri1); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri2); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + result = g_fah->Delete(renameFileUri3); + EXPECT_EQ(result, OHOS::FileAccessFwk::ERR_OK); + + } catch (...) { + GTEST_LOG_(ERROR) << "external_file_access_notify_0004 occurs an exception."; + } + GTEST_LOG_(INFO) << "FileExtensionNotifyTest-end external_file_access_notify_0004"; +} + /** * @tc.number: user_file_service_external_file_access_notify_0005 * @tc.name: external_file_access_notify_0005 -- Gitee From 677b3c7925f17fdb82e7b1b0c43abd9d2a94a904 Mon Sep 17 00:00:00 2001 From: caochuan Date: Wed, 30 Aug 2023 09:09:41 +0800 Subject: [PATCH 2/4] add debug log Signed-off-by: caochuan --- services/native/file_access_service/src/file_access_service.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/native/file_access_service/src/file_access_service.cpp b/services/native/file_access_service/src/file_access_service.cpp index ad336acc..366b874b 100644 --- a/services/native/file_access_service/src/file_access_service.cpp +++ b/services/native/file_access_service/src/file_access_service.cpp @@ -125,10 +125,12 @@ int32_t FileAccessService::RegisterNotify(Uri uri, bool notifyForDescendants, co }); if (code == HolderManager>::CODE_CAN_NOT_FIND) { // this is new callback, save this context + HILOG_DEBUG("this is new callback, save this context, uri: %{public}s", uri.ToString().c_str()); obsContext->Ref(); code = obsManager_.save(obsContext); } else { // this callback is already in manager, add ref. + HILOG_DEBUG("this callback is already in manager, add ref, uri: %{public}s", uri.ToString().c_str()); obsManager_.get(code)->Ref(); } string uriStr = uri.ToString(); -- Gitee From ab8091a5cd78dbbd0642326d1c290ae00303c37b Mon Sep 17 00:00:00 2001 From: caochuan Date: Wed, 30 Aug 2023 10:51:30 +0800 Subject: [PATCH 3/4] add flag Signed-off-by: caochuan --- .../include/file_access_service.h | 4 ++ .../src/file_access_service.cpp | 45 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/services/native/file_access_service/include/file_access_service.h b/services/native/file_access_service/include/file_access_service.h index b34ca915..53f787bf 100644 --- a/services/native/file_access_service/include/file_access_service.h +++ b/services/native/file_access_service/include/file_access_service.h @@ -36,6 +36,10 @@ class ObserverContext { ObserverContext(sptr obs): obs_(obs) {} virtual ~ObserverContext() = default; sptr obs_ = nullptr; + sptr GetObs() + { + return obs_; + } void Ref() { if (ref_ == std::numeric_limits::max()) { diff --git a/services/native/file_access_service/src/file_access_service.cpp b/services/native/file_access_service/src/file_access_service.cpp index 366b874b..6ba9e62a 100644 --- a/services/native/file_access_service/src/file_access_service.cpp +++ b/services/native/file_access_service/src/file_access_service.cpp @@ -16,7 +16,6 @@ #include "file_access_service.h" #include - #include "file_access_framework_errno.h" #include "hilog_wrapper.h" #include "hitrace_meter.h" @@ -123,16 +122,43 @@ int32_t FileAccessService::RegisterNotify(Uri uri, bool notifyForDescendants, co uint32_t code = obsManager_.getId([obsContext](const shared_ptr &afterContext) { return obsContext->EqualTo(afterContext); }); + string uriString = uri.ToString(); + auto segment = std::stoi(uriString.substr(uriString.length() - 1, 1)); if (code == HolderManager>::CODE_CAN_NOT_FIND) { // this is new callback, save this context HILOG_DEBUG("this is new callback, save this context, uri: %{public}s", uri.ToString().c_str()); + if (segment == 9) { + HILOG_ERROR("enter 1"); + sleep(5); + } + if (segment == 8) { + HILOG_ERROR("enter 2"); + HILOG_ERROR("this is new callback, save this context, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (observer->AsObject()).GetRefPtr()); + } obsContext->Ref(); code = obsManager_.save(obsContext); } else { // this callback is already in manager, add ref. HILOG_DEBUG("this callback is already in manager, add ref, uri: %{public}s", uri.ToString().c_str()); + if (segment == 9) { + HILOG_ERROR("enter 1"); + sleep(5); + } + if (segment == 8) { + HILOG_ERROR("enter 2"); + HILOG_ERROR("this is new callback, save this context, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (observer->AsObject()).GetRefPtr()); + } obsManager_.get(code)->Ref(); } + if (segment == 7) { + for (int i = 0; i < 100; i++) { + if (obsManager_.exist(i)) { + auto context = obsManager_.get(i); + auto obsPtr = context->GetObs(); + HILOG_ERROR("for each, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (obsPtr->AsObject()).GetRefPtr()); + } + } + } string uriStr = uri.ToString(); lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); @@ -248,6 +274,22 @@ int32_t FileAccessService::UnregisterNotify(Uri uri, const sptr &afterContext) { return obsContext->EqualTo(afterContext); }); + string uriString = uri.ToString(); + auto segment = std::stoi(uriString.substr(uriString.length() - 1, 1)); + if (segment == 9) { + HILOG_ERROR("unregister enter segment == 1"); + sleep(5); + } + if (segment == 7) { + for (int i = 0; i < 100; i++) { + if (obsManager_.exist(i)) { + auto context = obsManager_.get(i); + auto obsPtr = context->GetObs(); + HILOG_ERROR("unregister for each, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (obsPtr->AsObject()).GetRefPtr()); + } + } + } + if (code == HolderManager>::CODE_CAN_NOT_FIND) { HILOG_ERROR("Can not find observer"); FinishTrace(HITRACE_TAG_FILEMANAGEMENT); @@ -271,6 +313,7 @@ int32_t FileAccessService::UnregisterNotify(Uri uri, const sptrIsValid()) { + HILOG_ERROR("data refcount is invalid, uri: %{public}s", uri.ToString().c_str()); obsManager_.release(code); } RemoveRelations(uriStr, obsNode); -- Gitee From 3d50d472c92da24179bf0f9199bee93d06035080 Mon Sep 17 00:00:00 2001 From: caochuan Date: Thu, 31 Aug 2023 09:05:57 +0800 Subject: [PATCH 4/4] restore Signed-off-by: caochuan --- .../src/file_access_service.cpp | 86 +++++++++---------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/services/native/file_access_service/src/file_access_service.cpp b/services/native/file_access_service/src/file_access_service.cpp index 6ba9e62a..389812da 100644 --- a/services/native/file_access_service/src/file_access_service.cpp +++ b/services/native/file_access_service/src/file_access_service.cpp @@ -123,42 +123,42 @@ int32_t FileAccessService::RegisterNotify(Uri uri, bool notifyForDescendants, co return obsContext->EqualTo(afterContext); }); string uriString = uri.ToString(); - auto segment = std::stoi(uriString.substr(uriString.length() - 1, 1)); + //auto segment = std::stoi(uriString.substr(uriString.length() - 1, 1)); if (code == HolderManager>::CODE_CAN_NOT_FIND) { // this is new callback, save this context - HILOG_DEBUG("this is new callback, save this context, uri: %{public}s", uri.ToString().c_str()); - if (segment == 9) { - HILOG_ERROR("enter 1"); - sleep(5); - } - if (segment == 8) { - HILOG_ERROR("enter 2"); - HILOG_ERROR("this is new callback, save this context, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (observer->AsObject()).GetRefPtr()); - } + // HILOG_DEBUG("this is new callback, save this context, uri: %{public}s", uri.ToString().c_str()); + // if (segment == 9) { + // HILOG_ERROR("enter 1"); + // sleep(5); + // } + // if (segment == 8) { + // HILOG_ERROR("enter 2"); + // HILOG_ERROR("this is new callback, save this context, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (observer->AsObject()).GetRefPtr()); + // } obsContext->Ref(); code = obsManager_.save(obsContext); } else { // this callback is already in manager, add ref. - HILOG_DEBUG("this callback is already in manager, add ref, uri: %{public}s", uri.ToString().c_str()); - if (segment == 9) { - HILOG_ERROR("enter 1"); - sleep(5); - } - if (segment == 8) { - HILOG_ERROR("enter 2"); - HILOG_ERROR("this is new callback, save this context, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (observer->AsObject()).GetRefPtr()); - } + // HILOG_DEBUG("this callback is already in manager, add ref, uri: %{public}s", uri.ToString().c_str()); + // if (segment == 9) { + // HILOG_ERROR("enter 1"); + // sleep(5); + // } + // if (segment == 8) { + // HILOG_ERROR("enter 2"); + // HILOG_ERROR("this is new callback, save this context, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (observer->AsObject()).GetRefPtr()); + // } obsManager_.get(code)->Ref(); } - if (segment == 7) { - for (int i = 0; i < 100; i++) { - if (obsManager_.exist(i)) { - auto context = obsManager_.get(i); - auto obsPtr = context->GetObs(); - HILOG_ERROR("for each, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (obsPtr->AsObject()).GetRefPtr()); - } - } - } + // if (segment == 7) { + // for (int i = 0; i < 100; i++) { + // if (obsManager_.exist(i)) { + // auto context = obsManager_.get(i); + // auto obsPtr = context->GetObs(); + // HILOG_ERROR("for each, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (obsPtr->AsObject()).GetRefPtr()); + // } + // } + // } string uriStr = uri.ToString(); lock_guard lock(nodeMutex_); auto iter = relationshipMap_.find(uriStr); @@ -274,21 +274,21 @@ int32_t FileAccessService::UnregisterNotify(Uri uri, const sptr &afterContext) { return obsContext->EqualTo(afterContext); }); - string uriString = uri.ToString(); - auto segment = std::stoi(uriString.substr(uriString.length() - 1, 1)); - if (segment == 9) { - HILOG_ERROR("unregister enter segment == 1"); - sleep(5); - } - if (segment == 7) { - for (int i = 0; i < 100; i++) { - if (obsManager_.exist(i)) { - auto context = obsManager_.get(i); - auto obsPtr = context->GetObs(); - HILOG_ERROR("unregister for each, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (obsPtr->AsObject()).GetRefPtr()); - } - } - } + // string uriString = uri.ToString(); + // auto segment = std::stoi(uriString.substr(uriString.length() - 1, 1)); + // if (segment == 9) { + // HILOG_ERROR("unregister enter segment == 1"); + // sleep(5); + // } + // if (segment == 7) { + // for (int i = 0; i < 100; i++) { + // if (obsManager_.exist(i)) { + // auto context = obsManager_.get(i); + // auto obsPtr = context->GetObs(); + // HILOG_ERROR("unregister for each, uri: %{public}s addr: %{public}p", uri.ToString().c_str(), (obsPtr->AsObject()).GetRefPtr()); + // } + // } + // } if (code == HolderManager>::CODE_CAN_NOT_FIND) { HILOG_ERROR("Can not find observer"); -- Gitee