From b26ba4472382c57c81a32e9f1260b294c7ce20b0 Mon Sep 17 00:00:00 2001 From: fuletian Date: Sat, 6 Sep 2025 22:43:07 +0800 Subject: [PATCH 01/26] add tdd Signed-off-by: fuletian --- .../sync_folder/include/bit_ops.h | 63 ++ .../sync_folder/include/block_queue.h | 97 +++ .../include/cloud_disk_service_logfile.h | 166 ++++ .../include/cloud_disk_service_metafile.h | 156 ++++ .../include/cloud_disk_service_syncfolder.h | 39 + .../sync_folder/include/converter.h | 31 + .../sync_folder/include/uuid_helper.h | 34 + .../src/cloud_disk_service_logfile.cpp | 654 ++++++++++++++ .../src/cloud_disk_service_metafile.cpp | 796 ++++++++++++++++++ .../src/cloud_disk_service_syncfolder.cpp | 62 ++ .../sync_folder/src/converter.cpp | 57 ++ .../sync_folder/src/uuid_helper.cpp | 52 ++ .../utils/include/cloud_disk_service_error.h | 29 + .../cloud_disk_service_metafile_test.cpp | 388 +++++++++ 14 files changed, 2624 insertions(+) create mode 100644 services/clouddiskservice/sync_folder/include/bit_ops.h create mode 100644 services/clouddiskservice/sync_folder/include/block_queue.h create mode 100644 services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h create mode 100644 services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h create mode 100644 services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h create mode 100644 services/clouddiskservice/sync_folder/include/converter.h create mode 100644 services/clouddiskservice/sync_folder/include/uuid_helper.h create mode 100644 services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp create mode 100644 services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp create mode 100644 services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp create mode 100644 services/clouddiskservice/sync_folder/src/converter.cpp create mode 100644 services/clouddiskservice/sync_folder/src/uuid_helper.cpp create mode 100644 services/clouddiskservice/utils/include/cloud_disk_service_error.h create mode 100644 test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp diff --git a/services/clouddiskservice/sync_folder/include/bit_ops.h b/services/clouddiskservice/sync_folder/include/bit_ops.h new file mode 100644 index 000000000..0dc6c4e5f --- /dev/null +++ b/services/clouddiskservice/sync_folder/include/bit_ops.h @@ -0,0 +1,63 @@ +/* + * 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 BIT_OPS_H +#define BIT_OPS_H + +namespace OHOS::FileManagement::CloudDiskService { + +struct BitOps { + static const uint8_t BIT_PER_BYTE = 8; + static int TestBit(uint32_t nr, const uint8_t addr[]) + { + return 1 & (addr[nr / BIT_PER_BYTE] >> (nr & (BIT_PER_BYTE - 1))); + } + + static void ClearBit(uint32_t nr, uint8_t addr[]) + { + addr[nr / BIT_PER_BYTE] &= ~(1UL << ((nr) % BIT_PER_BYTE)); + } + + static void SetBit(uint32_t nr, uint8_t addr[]) + { + addr[nr / BIT_PER_BYTE] |= (1UL << ((nr) % BIT_PER_BYTE)); + } + + static uint32_t FindNextBit(const uint8_t addr[], uint32_t maxSlots, uint32_t start) + { + while (start < maxSlots) { + if (BitOps::TestBit(start, addr)) { + return start; + } + start++; + } + return maxSlots; + } + + static uint32_t FindNextZeroBit(const uint8_t addr[], uint32_t maxSlots, uint32_t start) + { + while (start < maxSlots) { + if (!BitOps::TestBit(start, addr)) { + return start; + } + start++; + } + return maxSlots; + } +}; + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // BIT_OPS_H diff --git a/services/clouddiskservice/sync_folder/include/block_queue.h b/services/clouddiskservice/sync_folder/include/block_queue.h new file mode 100644 index 000000000..52019bc31 --- /dev/null +++ b/services/clouddiskservice/sync_folder/include/block_queue.h @@ -0,0 +1,97 @@ +/* + * 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 BLOCK_QUEUE_H +#define BLOCK_QUEUE_H + +#include +#include + +#include "ffrt_inner.h" + +namespace OHOS::FileManagement::CloudDiskService { + +template +class BlockQueue { +public: + BlockQueue() + { + std::lock_guard lock(mutex_); + running_ = true; + } + + ~BlockQueue() + { + std::lock_guard lock(mutex_); + running_ = false; + } + + void push(T item) + { + std::lock_guard lock(mutex_); + if (!running_) { + return; + } + queue_.push(std::move(item)); + cv_.notify_one(); + } + + void pushBatch(std::vector &items) + { + std::lock_guard lock(mutex_); + if (!running_ || items.empty()) { + return; + } + for (const auto &item : items) { + queue_.push(std::move(item)); + } + cv_.notify_one(); + } + + std::optional pop() + { + std::unique_lock lock(mutex_); + cv_.wait(lock, [this] {return !running_ || !queue_.empty(); }); // run = false !run = true empty = true !empty = false + if (!running_ || queue_.empty()) { + return std::nullopt; + } + T item = std::move(queue_.front()); + queue_.pop(); + return item; + } + + bool empty() + { + std::lock_guard lock(mutex_); + return queue_.empty(); + } + + void shutdown() + { + std::lock_guard lock(mutex_); + running_ = false; + } + +private: + bool running_; + std::queue queue_; + ffrt::mutex mutex_; + ffrt::condition_variable cv_; +}; + + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // BLOCK_QUEUE_H diff --git a/services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h b/services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h new file mode 100644 index 000000000..b88958e3c --- /dev/null +++ b/services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h @@ -0,0 +1,166 @@ +/* + * 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 CLOUD_DISK_SERVICE_LOGFILE_H +#define CLOUD_DISK_SERVICE_LOGFILE_H + +#include +#include +#include +#include +#include +#include + +#include "block_queue.h" +#include "cloud_disk_common.h" +#include "cloud_disk_service_metafile.h" +#include "disk_types.h" +#include "unique_fd.h" + +namespace OHOS::FileManagement::CloudDiskService { + +constexpr uint32_t LOGBLOCK_RESERVED_LENGTH = 2; +constexpr uint32_t LOGGROUP_RESERVED_LENGTH = 62; +constexpr uint32_t LOGBLOCK_PER_GROUP = 64; +constexpr uint32_t LOGGROUP_SHIFT = 0X6; +constexpr uint32_t LOGGROUP_SIZE = 4096; +constexpr uint32_t LOGGROUP_SIZE_SHIFT = 0XC; +constexpr uint32_t LOGGROUP_MAX = 1024; +constexpr uint32_t LOGGROUP_INDEX_MASK = 0XFFFF; +constexpr uint32_t LOGGROUP_HEADER = 4096; +constexpr uint32_t LOG_INDEX_MASK = 0X3F; +constexpr uint32_t LOG_COUNT_MAX = 65536; + +#pragma pack(push, 1) +struct LogBlock { + uint64_t line; + uint64_t timestamp; + uint64_t parentInode; + uint32_t hash; + uint8_t operationType; + uint8_t recordId[RECORD_ID_LEN]; + uint8_t parentRecordId[RECORD_ID_LEN]; + uint8_t reserved[LOGBLOCK_RESERVED_LENGTH]; +}; + +struct LogGroup { + uint8_t logVersion; + uint8_t logBlockCnt; + struct LogBlock nsl[LOGBLOCK_PER_GROUP]; + uint8_t reserved[LOGGROUP_RESERVED_LENGTH]; +}; +static_assert(sizeof(LogGroup) == LOGGROUP_SIZE); +#pragma pack(pop) + +class CloudDiskServiceLogFile { +public: + CloudDiskServiceLogFile() = delete; + ~CloudDiskServiceLogFile() = default; + explicit CloudDiskServiceLogFile(const uint32_t userId, const uint32_t syncFolderIndex); + + int32_t ProduceLog(struct EventInfo &eventInfo); + int32_t PraseLog(const uint64_t line, ChangeData &data); + int32_t FillChildForDir(const std::string &path, const uint64_t timestamp); + // int32_t PraseLogs(const uint64_t start, const uint64_t count, struct ChangesResult &changesResult); + void StartCallback(); + void StopCallback(); + + int32_t OnDataChange(); +private: + // int32_t DecodeLogHeader(); + + int32_t ProductLogForOperate(const std::shared_ptr parentMetaFile, + const std::string &path, const std::string &name, std::string &childRecordId, + uint8_t operationType); + int32_t ProduceCreateLog(const std::shared_ptr parentMetaFile, const std::string &path, + const std::string &name, std::string &childRecordId); + int32_t ProduceUnlinkLog(const std::shared_ptr parentMetaFile, const std::string &name, + std::string &childRecordId); + int32_t ProduceRenameOldLog(const std::shared_ptr parentMetaFile, const std::string &name, + std::string &childRecordId); + int32_t ProduceRenameNewLog(const std::shared_ptr parentMetaFile, const std::string &path, + const std::string &name, std::string &childRecordId); + int32_t ProduceCloseAndWriteLog(const std::shared_ptr parentMetaFile, + const std::string &path, const std::string &name, std::string &childRecordId); + + + // int32_t ConsumeCreateLog(const LogStruct log, ChangeData &data); + // int32_t ConsumeUnlinkLog(const LogStruct log, ChangeData &data); + // int32_t ConsumeRenameOldLog(const LogStruct log, ChangeData &data); + // int32_t ConsumeRenameNewLog(const LogStruct log, ChangeData &data); + // int32_t ConsumeCloseAndWriteLog(const LogStruct log, ChangeData &data); + int32_t WriteLogFile(const struct LogBlock &logBlock); + int32_t ReadLogFile(const uint64_t line, struct LogBlock &logBlock); + int32_t GenerateLogBlock(struct EventInfo &eventInfo, const uint64_t parentInode, + const std::string &childRecordId, const std::string &parentRecordId, uint64_t &line); + int32_t GenerateChangeData(struct EventInfo &eventInfo, uint64_t line, + const std::string &childRecordId, const std::string &parentRecordId); + + uint32_t userId_; + uint32_t syncFolderIndex_; + bool needCallback_; + std::string syncFolderPath_; + std::string logFilePath_; + std::string renameRecordId_; + UniqueFd fd_{}; + + std::atomic currentLine_; + + // std::mutex queueMtx_{}; + // std::queue> logQueue_; + // std::mutex syncMtx_{}; + // std::queue> syncQueue_; + std::mutex vectorMtx_{}; + std::vector changeDatas_; +}; + +typedef std::pair LogFileKey; + +class LogFileMgr { +public: + static LogFileMgr& GetInstance(); + std::shared_ptr GetCloudDiskServiceLogFile(const uint32_t userId, + const uint32_t syncFolderIndex); + + int32_t ProduceRequest(std::vector &eventInfos); + int32_t ProduceRequest(struct EventInfo &eventInfo); + int32_t PraseRequest(const int32_t userId, const uint32_t syncFolderIndex, const uint64_t start, + const uint64_t count, struct ChangesResult &changesResult); + + // int32_t ConsumeLog(); + int32_t RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, const std::string &path); + int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex); + void RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); + void UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); + +private: + LogFileMgr(); // 启动线程 + ~LogFileMgr(); + LogFileMgr(const LogFileMgr &m) = delete; + const LogFileMgr &operator=(const LogFileMgr &m) = delete; + + int32_t ConsumeRequest(); + // int32_t WriteLogFile(); + int32_t OnDataChange(); + + bool running_; + std::mutex mtx_{}; + std::map> LogFiles_; + BlockQueue eventBlockQueue_; +}; + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // CLOUD_DISK_SERVICE_LOGFILE_H \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h b/services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h new file mode 100644 index 000000000..18f15c635 --- /dev/null +++ b/services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h @@ -0,0 +1,156 @@ +/* + * 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 CLOUD_DISK_SERVICE_METAFILE_H +#define CLOUD_DISK_SERVICE_METAFILE_H + +#include +#include +#include +#include +#include + +#include "unique_fd.h" + +namespace OHOS::FileManagement::CloudDiskService { + +constexpr uint32_t DENTRYGROUP_SIZE = 4096; +constexpr uint32_t DENTRY_NAME_LEN = 16; +constexpr uint32_t DENTRY_RESERVED_LENGTH = 3; +constexpr uint32_t DENTRY_PER_GROUP = 60; +constexpr uint32_t DENTRY_BITMAP_LENGTH = 8; +constexpr uint32_t DENTRY_GROUP_RESERVED = 7; +constexpr uint32_t DENTRYGROUP_HEADER = 4096; +constexpr uint32_t MAX_BUCKET_LEVEL = 63; +constexpr uint32_t BUCKET_BLOCKS = 2; +constexpr uint32_t BYTE_PER_SLOT = 16; +constexpr uint32_t HMDFS_SLOT_LEN_BITS = 4; +constexpr uint32_t RECORD_ID_LEN = 16; +constexpr uint32_t DENTRYFILE_NAME_LEN = 16; +const std::string ROOT_PARENTDENTRYFILE = "."; + +#pragma pack(push, 1) +struct CloudDiskServiceDentry { + uint8_t revalidate; + uint8_t recordId[RECORD_ID_LEN]; + uint32_t hash; + uint16_t mode; + uint16_t namelen; + uint64_t size; + uint64_t mtime; + uint64_t atime; + /* reserved bytes for long term extend, total 52 bytes */ + uint8_t reserved[DENTRY_RESERVED_LENGTH]; +}; + +struct CloudDiskServiceDentryGroup { + uint8_t dentryVersion; + uint8_t bitmap[DENTRY_BITMAP_LENGTH]; + struct CloudDiskServiceDentry nsl[DENTRY_PER_GROUP]; + uint8_t fileName[DENTRY_PER_GROUP][DENTRY_NAME_LEN]; + uint8_t reserved[DENTRY_GROUP_RESERVED]; +}; +static_assert(sizeof(CloudDiskServiceDentryGroup) == DENTRYGROUP_SIZE); + +struct CloudDiskServiceDcacheHeader { + uint8_t parentDentryfile[DENTRYFILE_NAME_LEN]; + uint8_t selfRecordId[RECORD_ID_LEN]; + uint32_t selfHash; +}; +#pragma pack(pop) + +struct MetaBase { + MetaBase(const std::string &nameOrRecordId, const bool isName) + { + if (isName) { + name = nameOrRecordId; + } else { + recordId = nameOrRecordId; + } + } + MetaBase(const std::string &name, const std::string &recordId) : name(name), recordId(recordId) {} + MetaBase() = default; + + uint32_t mode{0}; + uint32_t hash{0}; + uint64_t atime{0}; + uint64_t mtime{0}; + uint64_t size{0}; + std::string name{}; + std::string recordId{}; +}; + +class CloudDiskServiceMetaFile { +public: + CloudDiskServiceMetaFile() = delete; + ~CloudDiskServiceMetaFile() = default; + using CloudDiskServiceMetaFileCallBack = std::function; + explicit CloudDiskServiceMetaFile(const uint32_t userId, const uint32_t syncFolderIndex, const uint64_t inode); + + int32_t DoCreate(const MetaBase &base); + int32_t DoRemove(const MetaBase &base, std::string &recordId); + int32_t DoFlush(const MetaBase &base, std::string &recordId); + int32_t DoUpdate(const MetaBase &base, std::string &recordId); + int32_t DoRenameOld(const MetaBase &base, std::string &recordId); + int32_t DoRenameNew(const MetaBase &base, std::string &recordId); + int32_t DoRename(MetaBase &metaBase, const std::string &newName, + std::shared_ptr newMetaFile); + int32_t DoLookupByName(MetaBase &base); + int32_t DoLookupByRecordId(MetaBase &base); + + int32_t DecodeDentryHeader(); + int32_t GenericDentryHeader(); + + std::string parentDentryFile_{}; + std::string selfRecordId_{}; + std::string selfInode_{}; + uint32_t selfHash_{0}; + + std::string syncFolderIndex_{}; + uint32_t userId_{0}; + +private: + int32_t GetCreateInfo(const MetaBase &base, uint32_t &bitPos, uint32_t &namehash, unsigned long &bidx, + struct CloudDiskServiceDentryGroup &dentryBlk); + int32_t HandleFileByFd(unsigned long &endBlock, uint32_t level); + std::mutex mtx_{}; + UniqueFd fd_{}; + std::string cacheFile_{}; +}; + +typedef std::pair MetaFileKey; +typedef std::pair> MetaFileListEle; + +class MetaFileMgr { +public: + static MetaFileMgr& GetInstance(); + std::shared_ptr GetCloudDiskServiceMetaFile(const uint32_t userId, + const uint32_t syncFolderIndex, const uint64_t inode); + + int32_t GetRelativePath(const std::shared_ptr metaFile, std::string &path); +private: + MetaFileMgr() = default; + ~MetaFileMgr() = default; + MetaFileMgr(const MetaFileMgr &m) = delete; + const MetaFileMgr &operator=(const MetaFileMgr &m) = delete; + + std::mutex mtx_{}; + std::list metaFileList_; + std::map::iterator> metaFiles_; +}; + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // CLOUD_DISK_SERVICE_METAFILE_H \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h b/services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h new file mode 100644 index 000000000..5210a5a98 --- /dev/null +++ b/services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h @@ -0,0 +1,39 @@ +/* + * 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 CLOUD_DISK_SERVICE_SYNCFOLDER_H +#define CLOUD_DISK_SERVICE_SYNCFOLDER_H + +#include +#include + +#include "disk_types.h" + +namespace OHOS::FileManagement::CloudDiskService { + +class CloudDiskServiceSyncFolder { +public: + static int32_t RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, const std::string &path); + static int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex); + static void RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); + static void UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); + static int32_t GetSyncFolderChanges(const int32_t userId, const uint32_t syncFolderIndex, const uint64_t start, + const uint64_t count, struct ChangesResult &changesResult); + static int32_t SetSyncFolderChanges(std::vector &eventInfos); +}; + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // CLOUD_DISK_SERVICE_SYNCFOLDER_H \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/include/converter.h b/services/clouddiskservice/sync_folder/include/converter.h new file mode 100644 index 000000000..2925d57a1 --- /dev/null +++ b/services/clouddiskservice/sync_folder/include/converter.h @@ -0,0 +1,31 @@ +/* + * 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 CONVERTER_H +#define CONVERTER_H + +#include + +namespace OHOS::FileManagement::CloudDiskService { + +class Converter { +public: + static std::string ConvertToHex(uint64_t value); + static uint64_t ConvertFromHex(const std::string &hex); +}; + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // CONVERTER_H diff --git a/services/clouddiskservice/sync_folder/include/uuid_helper.h b/services/clouddiskservice/sync_folder/include/uuid_helper.h new file mode 100644 index 000000000..6247e8874 --- /dev/null +++ b/services/clouddiskservice/sync_folder/include/uuid_helper.h @@ -0,0 +1,34 @@ +/* + * 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 UUID_HELPER_H +#define UUID_HELPER_H + +#include + +#include "uuid.h" // e2fsprogs:libext2_uuid + +namespace OHOS::FileManagement::CloudDiskService { + +class UuidHelper { +public: + static std::string GenerateUuid(); + static std::string GenerateUuidWithoutDelim(); + static std::string GenerateUuidOnly(); +}; + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // UUID_HELPER_H diff --git a/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp b/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp new file mode 100644 index 000000000..6d44cc369 --- /dev/null +++ b/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp @@ -0,0 +1,654 @@ +/* + * 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 "cloud_disk_service_logfile.h" + +#include +#include +#include +#include +#include + +#include "cloud_disk_service_callback_manager.h" +#include "cloud_disk_service_error.h" +#include "cloud_disk_service_manager.h" +#include "cloud_file_utils.h" +#include "converter.h" +#include "ffrt_inner.h" +#include "file_utils.h" +#include "utils_directory.h" +#include "utils_log.h" +#include "uuid_helper.h" + +namespace OHOS::FileManagement::CloudDiskService { + +const std::string LOGFILENAME = "history.log"; +const unsigned int STAT_MODE_DIR = 0771; +const unsigned int MAX_CHANGEDATAS_SIZE = 20; + +static std::string GetLogFileByPath(const uint32_t userId, const uint32_t syncFolderIndex) +{ + std::string rootDir = + "/data/service/el2/" + std::to_string(userId) + + "/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/" + + Converter::ConvertToHex(syncFolderIndex) + "/"; + Storage::DistributedFile::Utils::ForceCreateDirectory(rootDir, STAT_MODE_DIR); + return rootDir + LOGFILENAME; +} + +static void GetBucketAndOffset(const uint64_t line, uint32_t &bucket, uint32_t &offset) +{ + uint32_t temp = line & LOGGROUP_INDEX_MASK; + bucket = temp >> LOGGROUP_SHIFT; + offset = temp & LOG_INDEX_MASK; +} + +static off_t GetBucketaddr(const uint32_t bucket) +{ + return (static_cast(bucket) << LOGGROUP_SIZE_SHIFT) + LOGGROUP_HEADER; +} + +static std::unique_ptr LoadCurrentPage(int fd, uint32_t bucket) +{ + auto logGroup = std::make_unique(); + off_t pos = GetBucketaddr(bucket); + auto ret = FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_WRLCK); + if (ret) { + return nullptr; + } + ssize_t size = FileUtils::ReadFile(fd, pos, LOGGROUP_SIZE, logGroup.get()); + if (size != LOGGROUP_SIZE) { + (void)FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_UNLCK); + return nullptr; + } + return logGroup; +} + +static int32_t SyncCurrentPage(LogGroup &logGroup, int fd, uint32_t line) +{ + struct stat fileStat; + int err = fstat(fd, &fileStat); + if (err < 0) { + return EINVAL; + } + + off_t pos = fileStat.st_size - LOGGROUP_SIZE; + int size = FileUtils::WriteFile(fd, &logGroup, pos, LOGGROUP_SIZE); + if (size != LOGGROUP_SIZE) { + LOGD("WriteFile failed, size %{public}d != %{public}d", size, LOGGROUP_SIZE); + (void)FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_UNLCK); + return EINVAL; + } + auto ret = FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_UNLCK); + if (ret) { + return ret; + } + return E_OK; +} + +static int32_t FillLogGroup(struct LogGroup &logGroup, const struct LogBlock &logBlock, uint32_t offset) +{ + if (logGroup.nsl[offset].timestamp != 0) { + (void) memset_s(&logGroup, LOGGROUP_SIZE, 0, LOGGROUP_SIZE); + // todo 完全消费日志 + } + auto ret = memcpy_s(&logGroup.nsl[offset], sizeof(struct LogBlock), &logBlock, sizeof(struct LogBlock)); + if (ret != 0) { + LOGE("memcpy_s struct LogBlock failed, errno = %{public}d", errno); + return -1; + } + logGroup.logBlockCnt++; + return E_OK; +} + +static uint32_t GetCurrentLine(int fd) +{ + uint32_t startLine = 0; + uint32_t offset = 0; + for (uint32_t i = 0; i < LOGGROUP_MAX; i++) { + auto logGroup = LoadCurrentPage(fd, i); + if (logGroup->nsl[0].timestamp != 0) { + break; + } else if (logGroup->nsl[0].line != startLine + offset) { + break; + } else { + startLine = logGroup->nsl[0].line; + offset = logGroup->logBlockCnt; + } + } + return startLine + offset; +} + +int32_t CloudDiskServiceLogFile::WriteLogFile(const struct LogBlock &logBlock) +{ + uint32_t bucket; + uint32_t offset; + GetBucketAndOffset(logBlock.line, bucket, offset); + + auto logGroup = LoadCurrentPage(fd_, bucket); + FillLogGroup(*logGroup, logBlock, offset); + SyncCurrentPage(*logGroup, fd_, bucket); + return E_OK; +} + +int32_t CloudDiskServiceLogFile::ReadLogFile(const uint64_t line, struct LogBlock &logBlock) +{ + uint32_t bucket; + uint32_t offset; + GetBucketAndOffset(logBlock.line, bucket, offset); + + auto logGroup = LoadCurrentPage(fd_, bucket); + logBlock = logGroup->nsl[offset]; + return E_OK; +} + +int32_t CloudDiskServiceLogFile::OnDataChange() +{ + std::lock_guard lock(vectorMtx_); + + if (changeDatas_.empty()) { + return E_OK; + } + CloudDiskServiceCallbackManager::GetInstance().OnChangeData(syncFolderIndex_, changeDatas_); + changeDatas_.clear(); + + return E_OK; +} + +CloudDiskServiceLogFile::CloudDiskServiceLogFile(const uint32_t userId, const uint32_t syncFolderIndex) +{ + userId_ = userId; + syncFolderIndex_ = syncFolderIndex; + needCallback_ = false; + // syncFolderPath_ = CloudDiskSyncFolder::GetInstance().syncFolderMap[syncFolderIndex].path; + logFilePath_ = GetLogFileByPath(userId_, syncFolderIndex_); + + if (access(logFilePath_.c_str(), F_OK) == 0) { + fd_ = UniqueFd{open(logFilePath_.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; + currentLine_ = GetCurrentLine(fd_); + } else { + fd_ = UniqueFd{open(logFilePath_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; + ftruncate(fd_, LOGGROUP_SIZE * LOGGROUP_MAX + LOGGROUP_HEADER); + currentLine_ = 0; + } +} + +int32_t CloudDiskServiceLogFile::GenerateLogBlock(struct EventInfo &eventInfo, const uint64_t parentInode, + const std::string &childRecordId, const std::string &parentRecordId, + uint64_t &line) +{ + // std::lock_guard lock(queueMtx_); + struct LogBlock logBlock; + logBlock.line = currentLine_++; + logBlock.timestamp = eventInfo.timestamp; + logBlock.parentInode = parentInode; + logBlock.hash = CloudDisk::CloudFileUtils::DentryHash(eventInfo.name); + logBlock.operationType = eventInfo.operateType; + auto ret = memcpy_s(logBlock.recordId, RECORD_ID_LEN, childRecordId.c_str(), RECORD_ID_LEN); + if (ret != 0) { + LOGE("memcpy_s recordId failed, errno = %{public}d", errno); + return -1; + } + ret = memcpy_s(logBlock.parentRecordId, RECORD_ID_LEN, parentRecordId.c_str(), RECORD_ID_LEN); + if (ret != 0) { + LOGE("memcpy_s parentRecordId failed, errno = %{public}d", errno); + return -1; + } + // logQueue_.push(logBlock); + WriteLogFile(logBlock); + line = logBlock.line; + return E_OK; +} + +int32_t CloudDiskServiceLogFile::GenerateChangeData(struct EventInfo &eventInfo, uint64_t line, + const std::string &childRecordId, const std::string &parentRecordId) +{ + std::lock_guard lock(vectorMtx_); + struct ChangeData changeData{}; + changeData.updateSequenceNumber = line; + changeData.fileId = childRecordId; + changeData.parentFileId = parentRecordId; + changeData.relativePath = eventInfo.path + "/" + eventInfo.name; + changeData.operationType = static_cast(eventInfo.operateType); + + struct stat childStat; + if (stat(changeData.relativePath.c_str(), &childStat) != 0) { + changeData.size = 0; + changeData.mtime = 0; + } else { + changeData.size = childStat.st_size; + changeData.mtime = childStat.st_mtime; + } + + changeData.timeStamp = eventInfo.timestamp; + changeDatas_.push_back(changeData); + if (changeDatas_.size() >= MAX_CHANGEDATAS_SIZE) { + CloudDiskServiceCallbackManager::GetInstance().OnChangeData(syncFolderIndex_, changeDatas_); + changeDatas_.clear(); + } + return E_OK; +} + +int32_t CloudDiskServiceLogFile::FillChildForDir(const std::string &path, const uint64_t timestamp) +{ + struct stat statInfo; + if (stat(path.c_str(), &statInfo) != 0) { + LOGE("stat parent failed for %{public}d", errno); + return errno; + } + + if (!S_ISDIR(statInfo.st_mode)) { + return E_OK; + } + DIR *dir = opendir(path.c_str()); + if (!dir) { + LOGE("opendir failed errno = %{public}d", errno); + return errno; + } + + struct dirent *entry; + while ((entry = readdir(dir)) != NULL) { + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { + continue; + } + struct EventInfo eventInfo(userId_, syncFolderIndex_, OperationType::CREATE, path + "/" + entry->d_name); + LogFileMgr::GetInstance().ProduceRequest(eventInfo); + } + + closedir(dir); + return E_OK; +} + + +void CloudDiskServiceLogFile::StartCallback() +{ + needCallback_ = true; +} + +void CloudDiskServiceLogFile::StopCallback() +{ + needCallback_ = false; +} + +int32_t CloudDiskServiceLogFile::ProduceLog(struct EventInfo &eventInfo) +{ + struct stat parentStat; + if (stat(eventInfo.path.c_str(), &parentStat) != 0) { + LOGE("stat parent failed for %{public}d", errno); + return errno; + } + + int32_t ret = 0; + if (eventInfo.operateType == static_cast(OperationType::SYNC_FOLDER_INVALID)) + { + ret = CloudDiskServiceManager::GetInstance().UnregisterForSa(eventInfo.path + "/" + eventInfo.name); + if (ret != 0) { + return ret; + } else { + return LogFileMgr::GetInstance().UnRegisterSyncFolder(userId_, syncFolderIndex_); + } + } + + auto parentMetaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, + parentStat.st_ino); + + std::string childRecordId; + ret = ProductLogForOperate(parentMetaFile, eventInfo.path, eventInfo.name, childRecordId, + eventInfo.operateType); + if (ret != 0) { + LOGE("create log failed"); + return ret; + } + + if (eventInfo.operateType == static_cast(OperationType::CREATE)) { + ffrt::submit( + [eventInfo, this] { FillChildForDir(eventInfo.path + "/" + eventInfo.name, eventInfo.timestamp); }); + } + + uint64_t line = 0; + GenerateLogBlock(eventInfo, parentStat.st_ino, childRecordId, parentMetaFile->selfRecordId_, line); + if (needCallback_) { + GenerateChangeData(eventInfo, line, childRecordId, parentMetaFile->selfRecordId_); + } + return E_OK; +} + +int32_t CloudDiskServiceLogFile::PraseLog(const uint64_t line, ChangeData &data) +{ + // 可校验下父亲是否是父亲,存不存在inode冲突的情况,根据parentRecordId + if (currentLine_.load() - line >= LOG_COUNT_MAX) { + return E_OUT_OF_RANGE; + } + + struct LogBlock logBlock; + auto ret = ReadLogFile(line, logBlock); + if (ret != 0) { + return ret; + } + + auto parentMetaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, + logBlock.parentInode); + std::string relativePath; + ret = MetaFileMgr::GetInstance().GetRelativePath(parentMetaFile, relativePath); + if (ret != 0) { + LOGE("get relative path failed"); + return ret; + } + + std::string recordId = std::string(reinterpret_cast(logBlock.recordId), RECORD_ID_LEN); + std::string parentRecordId = std::string(reinterpret_cast(logBlock.parentRecordId), RECORD_ID_LEN); + MetaBase mBase(recordId, false); + auto metaFile = parentMetaFile->DoLookupByRecordId(mBase); + + data.updateSequenceNumber = logBlock.line; + data.fileId = recordId; + data.parentFileId = parentRecordId; + //data.syncFolderIndex = syncFolderIndex_; + data.relativePath = relativePath + "/" + mBase.name; + data.operationType = static_cast(logBlock.operationType); + data.size = mBase.size; + data.mtime = mBase.mtime; + data.timeStamp = logBlock.timestamp; + + if (line == currentLine_.load()) { + return E_END_OF_LOGFILE; + } + return E_OK; +} + +int32_t CloudDiskServiceLogFile::ProductLogForOperate(const std::shared_ptr parentMetaFile, + const std::string &path, const std::string &name, + std::string &childRecordId, uint8_t operationType) +{ + OperationType type = static_cast(operationType); + switch (type) { + case OperationType::CREATE: + return ProduceCreateLog(parentMetaFile, path, name, childRecordId); + case OperationType::DELETE: + return ProduceUnlinkLog(parentMetaFile, name, childRecordId); + case OperationType::MOVE_FROM: + return ProduceRenameOldLog(parentMetaFile, name, childRecordId); + case OperationType::MOVE_TO: + return ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); + case OperationType::CLOSE_WRITE: + return ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId); + case OperationType::SYNC_FOLDER_INVALID: + case OperationType::OPERATION_MAX: + return EINVAL; + } +} + +int32_t CloudDiskServiceLogFile::ProduceCreateLog(const std::shared_ptr parentMetaFile, + const std::string &path, const std::string &name, + std::string &childRecordId) +{ + childRecordId = UuidHelper::GenerateUuidOnly(); + MetaBase mBase(name, childRecordId); + + struct stat childStat; + if (stat((path + "/" + name).c_str(), &childStat) != 0) { + LOGE("stat child failed for %{public}d", errno); + return errno; + } + mBase.mode = childStat.st_mode; + mBase.atime = childStat.st_atime; + mBase.mtime = childStat.st_mtime; + mBase.size = childStat.st_size; + auto ret = parentMetaFile->DoCreate(mBase); + if (ret != 0) { + LOGE("create failed"); + return -1; + } + + if (!S_ISDIR(childStat.st_ino)) { + return E_OK; + } + auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, childStat.st_ino); + metaFile->parentDentryFile_ = parentMetaFile->selfInode_; + metaFile->selfRecordId_ = childRecordId; + metaFile->selfHash_ = CloudDisk::CloudFileUtils::DentryHash(name); + metaFile->GenericDentryHeader(); + return E_OK; +} + +int32_t CloudDiskServiceLogFile::ProduceUnlinkLog(const std::shared_ptr parentMetaFile, + const std::string &name, std::string &childRecordId) +{ + MetaBase mBase(name, true); + auto ret = parentMetaFile->DoRemove(mBase, childRecordId); + if (ret != 0) { + LOGE("remove failed"); + return -1; + } + return E_OK; +} + +int32_t CloudDiskServiceLogFile::ProduceRenameOldLog(const std::shared_ptr parentMetaFile, + const std::string &name, std::string &childRecordId) +{ + MetaBase mBase(name, true); + auto ret = parentMetaFile->DoRenameOld(mBase, childRecordId); + if (ret != 0) { + LOGE("renameold failed"); + return -1; + } + renameRecordId_ = childRecordId; + return E_OK; +} + +int32_t CloudDiskServiceLogFile::ProduceRenameNewLog(const std::shared_ptr parentMetaFile, + const std::string &path, const std::string &name, + std::string &childRecordId) +{ + MetaBase mBase(name, true); + struct stat childStat; + if (stat((path + "/" + name).c_str(), &childStat) != 0) { + LOGE("stat child failed for %{public}d", errno); + return errno; + } + mBase.mode = childStat.st_mode; + mBase.atime = childStat.st_atime; + mBase.mtime = childStat.st_mtime; + mBase.size = childStat.st_size; + auto ret = parentMetaFile->DoRenameNew(mBase, renameRecordId_); + if (ret != 0) { + LOGE("renamenew failed"); + return -1; + } + childRecordId = renameRecordId_; + + if (!S_ISDIR(childStat.st_ino)) { + return E_OK; + } + auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, childStat.st_ino); + metaFile->parentDentryFile_ = parentMetaFile->selfInode_; + metaFile->selfRecordId_ = childRecordId; + metaFile->selfHash_ = CloudDisk::CloudFileUtils::DentryHash(name); + metaFile->GenericDentryHeader(); + return E_OK; +} + +int32_t CloudDiskServiceLogFile::ProduceCloseAndWriteLog(const std::shared_ptr parentMetaFile, + const std::string &path, const std::string &name, + std::string &childRecordId) +{ + MetaBase mBase(name, true); + struct stat childStat; + if (stat((path + "/" + name).c_str(), &childStat) != 0) { + LOGE("stat child failed for %{public}d", errno); + return errno; + } + mBase.mode = childStat.st_mode; + mBase.atime = childStat.st_atime; + mBase.mtime = childStat.st_mtime; + mBase.size = childStat.st_size; + auto ret = parentMetaFile->DoUpdate(mBase, childRecordId); + if (ret != 0) { + LOGE("update failed"); + return -1; + } + return E_OK; +} + +LogFileMgr& LogFileMgr::GetInstance() +{ + static LogFileMgr instance_; + return instance_; +} + +LogFileMgr::LogFileMgr() +{ + running_ = true; + ffrt::submit([this] { ConsumeRequest(); }); +} + +LogFileMgr::~LogFileMgr() +{ + running_ = false; + eventBlockQueue_.shutdown(); +} + +int32_t LogFileMgr::ProduceRequest(std::vector &eventInfos) +{ + if (!running_) { + return -1; + } + eventBlockQueue_.pushBatch(eventInfos); + return E_OK; +} + +int32_t LogFileMgr::ProduceRequest(struct EventInfo &eventInfo) +{ + if (!running_) { + return -1; + } + eventBlockQueue_.push(eventInfo); + return E_OK; +} + +int32_t LogFileMgr::PraseRequest(const int32_t userId, const uint32_t syncFolderIndex, const uint64_t start, + const uint64_t count, struct ChangesResult &changesResult) +{ + int32_t ret; + auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); + for (uint64_t line = start; line < start + count; line++) { + struct ChangeData changeData; + ret = logFile->PraseLog(line, changeData); + if (ret == E_OK) { + changesResult.changesData.push_back(changeData); + continue; + } else if (ret == E_END_OF_LOGFILE) { + changesResult.nextUsn = start + line; + changesResult.isEof = true; + break; + } else { + break; + } + } + if (ret == E_OK) { + changesResult.nextUsn = start + count; + changesResult.isEof = false; + } + return ret; +} + +int32_t LogFileMgr::RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, const std::string &path) +{ + auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); + + struct stat st; + if (stat(path.c_str(), &st) != 0) { + LOGE("stat failed for %{public}d", errno); + return errno; + } + auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId, syncFolderIndex, st.st_ino); + metaFile->parentDentryFile_ = ROOT_PARENTDENTRYFILE; + metaFile->selfRecordId_ = UuidHelper::GenerateUuidOnly(); + metaFile->selfHash_ = CloudDisk::CloudFileUtils::DentryHash(ROOT_PARENTDENTRYFILE); + + metaFile->GenericDentryHeader(); + + ffrt::submit([logFile, path] { logFile->FillChildForDir(path, UTCTimeMilliSeconds()); }); + return E_OK; +} + +int32_t LogFileMgr::UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) +{ + std::lock_guard lock(mtx_); + LogFileKey key(userId, syncFolderIndex); + LogFiles_.erase(key); + + std::string rootDir = + "/data/service/el2/" + std::to_string(userId) + + "/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/" + + Converter::ConvertToHex(syncFolderIndex) + "/"; + if (!OHOS::Storage::DistributedFile::Utils::ForceRemoveDirectoryDeepFirst(rootDir)) { + LOGW("remove photo dentry dir failed, errno: %{public}d", errno); + } + return E_OK; +} + +void LogFileMgr::RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) +{ + auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); + logFile->StartCallback(); +} + +void LogFileMgr::UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) +{ + auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); + logFile->StopCallback(); +} + +std::shared_ptr LogFileMgr::GetCloudDiskServiceLogFile(const uint32_t userId, + const uint32_t syncFolderIndex) +{ + std::shared_ptr lFile = nullptr; + std::lock_guard lock(mtx_); + LogFileKey key(userId, syncFolderIndex); + auto it = LogFiles_.find(key); + if (it != LogFiles_.end()) { + lFile = it->second; + } else { + lFile = std::make_shared(userId, syncFolderIndex); + LogFiles_[key] = lFile; + } + return lFile; +} + +int32_t LogFileMgr::ConsumeRequest() +{ + while (running_) { + auto front = eventBlockQueue_.pop(); + if (!front) { + continue; + } + + auto eventInfo = std::move(front.value()); + auto logFile = GetCloudDiskServiceLogFile(eventInfo.userId, eventInfo.syncFolderIndex); + logFile->ProduceLog(eventInfo); + } + return E_OK; +} + +int32_t LogFileMgr::OnDataChange() +{ + for (const auto &it : LogFiles_) { + it.second->OnDataChange(); + } + return E_OK; +} + +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp b/services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp new file mode 100644 index 000000000..29736cd1b --- /dev/null +++ b/services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp @@ -0,0 +1,796 @@ +/* + * 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 "cloud_disk_service_metafile.h" + +#include +#include +#include +#include +#include // e2fsprogs:libext2_uuid + +#include "bit_ops.h" +#include "cloud_disk_service_error.h" +#include "cloud_file_utils.h" +#include "converter.h" +#include "file_utils.h" +#include "utils_directory.h" +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace OHOS::FileManagement; + +constexpr uint8_t VALIDATE = 0x01; +constexpr uint8_t INVALIDATE = 0x02; +constexpr uint8_t FIND_BOTH = 0x03; +constexpr uint32_t MAX_META_FILE_NUM = 150; +const unsigned int STAT_MODE_DIR = 0771; + +struct DcacheLookupCtx { + int fd{-1}; + std::string name{}; + std::string recordId{}; + uint32_t hash{0}; + uint32_t bidx{0}; + std::unique_ptr page{nullptr}; +}; + +static inline uint32_t GetDentrySlots(size_t namelen) +{ + return static_cast((namelen + BYTE_PER_SLOT - 1) >> HMDFS_SLOT_LEN_BITS); +} + +static inline off_t GetDentryGroupPos(size_t bidx) +{ + return bidx * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; +} + +static inline uint64_t GetDentryGroupCnt(uint64_t size) +{ + return (size >= DENTRYGROUP_HEADER) ? ((size - DENTRYGROUP_HEADER) / DENTRYGROUP_SIZE) : 0; +} + +static uint32_t GetOverallBucket(uint32_t level) +{ + if (level >= MAX_BUCKET_LEVEL) { + LOGD("level = %{public}d overflow", level); + return E_OK; + } + uint64_t buckets = (1ULL << (level + 1)) - 1; + return static_cast(buckets); +} + +static size_t GetDcacheFileSize(uint32_t level) +{ + size_t buckets = GetOverallBucket(level); + return buckets * DENTRYGROUP_SIZE * BUCKET_BLOCKS + DENTRYGROUP_HEADER; +} + +static uint32_t GetBucketByLevel(uint32_t level) +{ + if (level >= MAX_BUCKET_LEVEL) { + LOGD("level = %{public}d overflow", level); + return E_OK; + } + + uint64_t buckets = (1ULL << level); + return static_cast(buckets); +} + +static uint32_t GetBucketaddr(uint32_t level, uint32_t buckoffset) +{ + if (level >= MAX_BUCKET_LEVEL) { + return E_OK; + } + + uint64_t curLevelMaxBucks = (1ULL << level); + if (buckoffset >= curLevelMaxBucks) { + return E_OK; + } + + return static_cast(curLevelMaxBucks) + buckoffset - 1; +} + +static unsigned long GetBidxFromLevel(uint32_t level, uint32_t namehash) +{ + uint32_t bucket = GetBucketByLevel(level); + if (bucket == 0) { + return E_OK; + } + return BUCKET_BLOCKS * GetBucketaddr(level, namehash % bucket); +} + +static std::unique_ptr FindDentryPage(uint64_t index, DcacheLookupCtx *ctx) +{ + auto dentryBlk = std::make_unique(); + + off_t pos = GetDentryGroupPos(index); + auto ret = FileRangeLock::FilePosLock(ctx->fd, pos, DENTRYGROUP_SIZE, F_WRLCK); + if (ret) { + return nullptr; + } + ssize_t size = FileUtils::ReadFile(ctx->fd, pos, DENTRYGROUP_SIZE, dentryBlk.get()); + if (size != DENTRYGROUP_SIZE) { + (void)FileRangeLock::FilePosLock(ctx->fd, pos, DENTRYGROUP_SIZE, F_UNLCK); + return nullptr; + } + return dentryBlk; +} + +static CloudDiskServiceDentry *FindInBlockById(CloudDiskServiceDentryGroup &dentryBlk, uint32_t namehash, + const std::string &recordId, uint8_t revalidate) +{ + uint32_t bitPos = 0; + CloudDiskServiceDentry *de = nullptr; + + while (bitPos < DENTRY_PER_GROUP) { + if (!BitOps::TestBit(bitPos, dentryBlk.bitmap)) { + bitPos++; + continue; + } + de = &dentryBlk.nsl[bitPos]; + if (!de->namelen) { + bitPos++; + continue; + } + + if ((de->revalidate & revalidate) != 0 && !memcmp(recordId.c_str(), de->recordId, recordId.length())) { + return de; + } + bitPos += GetDentrySlots(de->namelen); + } + + return nullptr; +} + +static CloudDiskServiceDentry *FindInBlock(CloudDiskServiceDentryGroup &dentryBlk, uint32_t namehash, + const std::string &name, uint8_t revalidate) +{ + uint32_t bitPos = 0; + CloudDiskServiceDentry *de = nullptr; + + while (bitPos < DENTRY_PER_GROUP) { + if (!BitOps::TestBit(bitPos, dentryBlk.bitmap)) { + bitPos++; + continue; + } + de = &dentryBlk.nsl[bitPos]; + if (!de->namelen) { + bitPos++; + continue; + } + + if ((de->revalidate & revalidate) != 0 && de->hash == namehash && de->namelen == name.length() && + !memcmp(name.c_str(), dentryBlk.fileName[bitPos], de->namelen)) { + return de; + } + bitPos += GetDentrySlots(de->namelen); + } + + return nullptr; +} + +static CloudDiskServiceDentry *InLevel(uint32_t level, DcacheLookupCtx *ctx, bool byId, uint8_t revalidate) + __attribute__((no_sanitize("unsigned-integer-overflow"))) +{ + CloudDiskServiceDentry *de = nullptr; + + uint32_t nbucket = GetBucketByLevel(level); + if (nbucket == 0) { + return de; + } + + uint32_t bidx = GetBucketaddr(level, ctx->hash % nbucket) * BUCKET_BLOCKS; + uint32_t endBlock = bidx + BUCKET_BLOCKS; + + for (; bidx < endBlock; bidx++) { + auto dentryBlk = FindDentryPage(bidx, ctx); + if (dentryBlk == nullptr) { + break; + } + + if (byId) { + de = FindInBlockById(*dentryBlk, ctx->hash, ctx->recordId, revalidate); + } else { + de = FindInBlock(*dentryBlk, ctx->hash, ctx->name, revalidate); + } + if (de != nullptr) { + ctx->page = std::move(dentryBlk); + break; + } + off_t pos = GetDentryGroupPos(bidx); + (void)FileRangeLock::FilePosLock(ctx->fd, pos, DENTRYGROUP_SIZE, F_UNLCK); + } + ctx->bidx = bidx; + return de; +} + +static CloudDiskServiceDentry *FindDentry(DcacheLookupCtx *ctx, bool byId = false, uint8_t revalidate = VALIDATE) +{ + for (uint32_t level = 0; level < MAX_BUCKET_LEVEL; level++) { + CloudDiskServiceDentry *de = InLevel(level, ctx, byId, revalidate); + if (de != nullptr) { + return de; + } + } + return nullptr; +} + +static void InitDcacheLookupCtx(DcacheLookupCtx *ctx, const MetaBase &base, uint32_t hash, int fd) +{ + ctx->fd = fd; + ctx->name = base.name; + ctx->bidx = 0; + ctx->page = nullptr; + ctx->hash = hash; +} + +static uint32_t RoomForFilename(const uint8_t bitmap[], size_t slots, uint32_t maxSlots) +{ + uint32_t bitStart = 0; + bool loopFlag = true; + while (loopFlag) { + uint32_t zeroStart = BitOps::FindNextZeroBit(bitmap, maxSlots, bitStart); + if (zeroStart >= maxSlots) { + return maxSlots; + } + + uint32_t zeroEnd = BitOps::FindNextBit(bitmap, maxSlots, zeroStart); + if (zeroEnd - zeroStart >= slots) { + return zeroStart; + } + + bitStart = zeroEnd + 1; + if (zeroEnd + 1 >= maxSlots) { + return maxSlots; + } + } + return E_OK; +} + +static bool CreateDentry(CloudDiskServiceDentryGroup &d, const MetaBase &base, uint32_t nameHash, uint32_t bitPos, + std::string recordId) +{ + CloudDiskServiceDentry *de; + const std::string name = base.name; + uint32_t slots = GetDentrySlots(name.length()); + + de = &d.nsl[bitPos]; + de->hash = nameHash; + de->namelen = name.length(); + auto ret = memcpy_s(d.fileName[bitPos], slots * DENTRY_NAME_LEN, name.c_str(), name.length()); + if (ret != 0) { + LOGE("memcpy_s failed, dstLen = %{public}d, srcLen = %{public}zu", slots * DENTRY_NAME_LEN, name.length()); + return false; + } + de->revalidate = VALIDATE; + de->atime = base.atime; + de->mtime = base.mtime; + de->size = base.size; + de->mode = base.mode; + (void) memset_s(de->recordId, RECORD_ID_LEN, 0, RECORD_ID_LEN); + ret = memcpy_s(de->recordId, RECORD_ID_LEN, recordId.c_str(), recordId.length()); + if (ret != 0) { + LOGE("memcpy_s failed, dstLen = %{public}d, srcLen = %{public}zu", RECORD_ID_LEN, recordId.length()); + return false; + } + + for (uint32_t i = 0; i < slots; i++) { + BitOps::SetBit(bitPos + i, d.bitmap); + if (i) { + (de + i)->namelen = 0; + } + } + return true; +} + +static std::string GetDentryFileByPath(const uint32_t userId, const std::string &syncFolderIndex, + const std::string &inode) +{ + std::string cacheDir = + "/data/service/el2/" + std::to_string(userId) + + "/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/" + syncFolderIndex + "/" + + std::to_string(CloudDisk::CloudFileUtils::GetBucketId(inode)) + "/"; + Storage::DistributedFile::Utils::ForceCreateDirectory(cacheDir, STAT_MODE_DIR); + return cacheDir + inode; +} + +CloudDiskServiceMetaFile::CloudDiskServiceMetaFile(const uint32_t userId, const uint32_t syncFolderIndex, + const uint64_t inode) +{ + userId_ = userId; + syncFolderIndex_ = Converter::ConvertToHex(syncFolderIndex); + selfInode_ = Converter::ConvertToHex(inode); + cacheFile_ = GetDentryFileByPath(userId_, syncFolderIndex_, selfInode_); + + if (access(cacheFile_.c_str(), F_OK) == 0) { + fd_ = UniqueFd{open(cacheFile_.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; + DecodeDentryHeader(); + } else { + fd_ = UniqueFd{open(cacheFile_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; + } +} + +int32_t CloudDiskServiceMetaFile::DecodeDentryHeader() +{ + auto ret = FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_WRLCK); + if (ret) { + return ret; + } + struct CloudDiskServiceDcacheHeader header; + ssize_t size = FileUtils::ReadFile(fd_, 0, sizeof(CloudDiskServiceDcacheHeader), &header); + (void)FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_UNLCK); + if (size != sizeof(CloudDiskServiceDcacheHeader)) { + return size; + } + + parentDentryFile_ = std::string(reinterpret_cast(header.parentDentryfile), DENTRYFILE_NAME_LEN); + selfRecordId_ = std::string(reinterpret_cast(header.selfRecordId), RECORD_ID_LEN); + selfHash_ = header.selfHash; + + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::GenericDentryHeader() +{ + struct stat fileStat; + int err = fstat(fd_, &fileStat); + if (err < 0) { + return EINVAL; + } + if (fileStat.st_size < DENTRYGROUP_HEADER && ftruncate(fd_, DENTRYGROUP_HEADER)) { + return ENOENT; + } + + struct CloudDiskServiceDcacheHeader header; + auto ret = memcpy_s(header.parentDentryfile, DENTRYFILE_NAME_LEN, parentDentryFile_.c_str(), DENTRYFILE_NAME_LEN); + ret += memcpy_s(header.selfRecordId, RECORD_ID_LEN, selfRecordId_.c_str(), RECORD_ID_LEN); + if (ret != 0) { + LOGE("memcpy_s failed errno=%{public}d", errno); + return errno; + } + header.selfHash = selfHash_; + + ret = FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_WRLCK); + if (ret) { + return ret; + } + ssize_t size = FileUtils::WriteFile(fd_, &header, 0, sizeof(CloudDiskServiceDcacheHeader)); + (void)FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_UNLCK); + if (size != sizeof(CloudDiskServiceDcacheHeader)) { + return size; + } + + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoCreate(const MetaBase &base) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + // validate the length of name, maximum length is 52*8 byte + uint32_t slots = GetDentrySlots(base.name.length()); + if (slots > DENTRY_PER_GROUP) { + LOGE("name is too long"); + return ENAMETOOLONG; + } + + std::unique_lock lock(mtx_); + DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); + CloudDiskServiceDentry *de = FindDentry(&ctx); + if (de != nullptr) { + LOGE("this name dentry is exist"); + (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); + return EEXIST; + } + uint32_t bitPos = 0; + unsigned long bidx = 0; + CloudDiskServiceDentryGroup dentryBlk = {0}; + uint32_t namehash = 0; + auto ret = GetCreateInfo(base, bitPos, namehash, bidx, dentryBlk); + if (ret) { + return ret; + } + off_t pos = GetDentryGroupPos(bidx); + if (!CreateDentry(dentryBlk, base, namehash, bitPos, base.recordId)) { + LOGI("CreateDentry fail, stop write."); + (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + return EINVAL; + } + int size = FileUtils::WriteFile(fd_, &dentryBlk, pos, DENTRYGROUP_SIZE); + if (size != DENTRYGROUP_SIZE) { + LOGD("WriteFile failed, size %{public}d != %{public}d", size, DENTRYGROUP_SIZE); + (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + return EINVAL; + } + ret = FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + if (ret) { + return ret; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoRemove(const MetaBase &base, std::string &recordId) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + std::unique_lock lock(mtx_); + struct DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); + struct CloudDiskServiceDentry *de = FindDentry(&ctx); + if (de == nullptr) { + LOGD("find dentry failed"); + return ENOENT; + } + + de->revalidate = INVALIDATE; + recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); + + off_t ipos = GetDentryGroupPos(ctx.bidx); + ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(struct CloudDiskServiceDentryGroup)); + if (size != sizeof(struct CloudDiskServiceDentryGroup)) { + LOGE("write failed, ret = %{public}zd", size); + (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + return EIO; + } + auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + if (ret) { + return ret; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoFlush(const MetaBase &base, std::string &recordId) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + std::unique_lock lock(mtx_); + DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); + CloudDiskServiceDentry *de = FindDentry(&ctx, false, INVALIDATE); + if (de == nullptr) { + LOGE("find dentry failed"); + return ENOENT; + } + + uint32_t bitPos = (de - ctx.page->nsl); + uint32_t slots = GetDentrySlots(de->namelen); + for (uint32_t i = 0; i < slots; i++) { + BitOps::ClearBit(bitPos + i, ctx.page->bitmap); + } + + off_t ipos = GetDentryGroupPos(ctx.bidx); + ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(CloudDiskServiceDentryGroup)); + if (size != sizeof(CloudDiskServiceDentryGroup)) { + LOGE("WriteFile failed!, ret = %{public}zd", size); + (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + return EIO; + } + auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + if (ret) { + return ret; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoUpdate(const MetaBase &base, std::string &recordId) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + std::unique_lock lock(mtx_); + struct DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); + struct CloudDiskServiceDentry *de = FindDentry(&ctx); + if (de == nullptr) { + LOGD("find dentry failed"); + return ENOENT; + } + + de->atime = base.atime; + de->mtime = base.mtime; + de->size = base.size; + de->mode = base.mode; + recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); + + off_t ipos = GetDentryGroupPos(ctx.bidx); + ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(struct CloudDiskServiceDentryGroup)); + if (size != sizeof(struct CloudDiskServiceDentryGroup)) { + LOGE("write failed, ret = %{public}zd", size); + (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + return EIO; + } + auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + if (ret) { + return ret; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoRenameOld(const MetaBase &base, std::string &recordId) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + std::unique_lock lock(mtx_); + struct DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); + struct CloudDiskServiceDentry *de = FindDentry(&ctx); + if (de == nullptr) { + LOGD("find dentry failed"); + return ENOENT; + } + + de->revalidate = INVALIDATE; + recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); + + off_t ipos = GetDentryGroupPos(ctx.bidx); + ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(struct CloudDiskServiceDentryGroup)); + if (size != sizeof(struct CloudDiskServiceDentryGroup)) { + LOGE("write failed, ret = %{public}zd", size); + (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + return EIO; + } + auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); + if (ret) { + return ret; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoRenameNew(const MetaBase &base, std::string &recordId) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + // validate the length of name, maximum length is 52*8 byte + uint32_t slots = GetDentrySlots(base.name.length()); + if (slots > DENTRY_PER_GROUP) { + LOGE("name is too long"); + return ENAMETOOLONG; + } + + std::unique_lock lock(mtx_); + DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); + CloudDiskServiceDentry *de = FindDentry(&ctx); + if (de != nullptr) { + LOGE("this name dentry is exist"); + (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); + return EEXIST; + } + uint32_t bitPos = 0; + unsigned long bidx = 0; + CloudDiskServiceDentryGroup dentryBlk = {0}; + uint32_t namehash = 0; + auto ret = GetCreateInfo(base, bitPos, namehash, bidx, dentryBlk); + if (ret) { + return ret; + } + off_t pos = GetDentryGroupPos(bidx); + if (!CreateDentry(dentryBlk, base, namehash, bitPos, recordId)) { + LOGI("CreateDentry fail, stop write."); + (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + return EINVAL; + } + int size = FileUtils::WriteFile(fd_, &dentryBlk, pos, DENTRYGROUP_SIZE); + if (size != DENTRYGROUP_SIZE) { + LOGD("WriteFile failed, size %{public}d != %{public}d", size, DENTRYGROUP_SIZE); + (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + return EINVAL; + } + ret = FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + if (ret) { + return ret; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoRename(MetaBase &metaBase, const std::string &newName, + std::shared_ptr newMetaFile) +{ + std::string oldName = metaBase.name; + metaBase.name = newName; + auto ret = newMetaFile->DoCreate(metaBase); + if (ret != 0) { + LOGE("create dentry failed, ret = %{public}d", ret); + return ret; + } + metaBase.name = oldName; + std::string childRecordId; + ret = DoRemove(metaBase, childRecordId); + if (ret != 0) { + LOGE("remove dentry failed, ret = %{public}d", ret); + metaBase.name = newName; + (void)newMetaFile->DoFlush(metaBase, childRecordId); + return ret; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoLookupByName(MetaBase &base) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + std::unique_lock lock(mtx_); + struct DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); + struct CloudDiskServiceDentry *de = FindDentry(&ctx); + if (de == nullptr) { + LOGD("find dentry failed"); + return ENOENT; + } + (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); + + base.mode = de->mode; + base.hash = de->hash; + base.size = de->size; + base.atime = de->atime; + base.mtime = de->mtime; + base.recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::DoLookupByRecordId(MetaBase &base) +{ + if (fd_ < 0) { + LOGE("bad metafile fd"); + return EINVAL; + } + + std::unique_lock lock(mtx_); + struct DcacheLookupCtx ctx; + InitDcacheLookupCtx(&ctx, base, base.hash, fd_); + ctx.recordId = base.recordId; + struct CloudDiskServiceDentry *de = FindDentry(&ctx, true, FIND_BOTH); + if (de == nullptr) { + LOGD("find dentry failed"); + return ENOENT; + } + (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); + + base.mode = de->mode; + base.hash = de->hash; + base.size = de->size; + base.atime = de->atime; + base.mtime = de->mtime; + base.recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::GetCreateInfo(const MetaBase &base, uint32_t &bitPos, uint32_t &namehash, + unsigned long &bidx, struct CloudDiskServiceDentryGroup &dentryBlk) +{ + uint32_t level = 0; + namehash = CloudDisk::CloudFileUtils::DentryHash(base.name); + bool found = false; + while (!found) { + if (level == MAX_BUCKET_LEVEL) { + return ENOSPC; + } + bidx = GetBidxFromLevel(level, namehash); + unsigned long endBlock = bidx + BUCKET_BLOCKS; + int32_t ret = HandleFileByFd(endBlock, level); + if (ret != 0) { + return ret; + } + for (; bidx < endBlock; bidx++) { + off_t pos = GetDentryGroupPos(bidx); + ret = FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_WRLCK); + if (ret) { + return ret; + } + if (FileUtils::ReadFile(fd_, pos, DENTRYGROUP_SIZE, &dentryBlk) != DENTRYGROUP_SIZE) { + (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + return ENOENT; + } + bitPos = RoomForFilename(dentryBlk.bitmap, GetDentrySlots(base.name.length()), DENTRY_PER_GROUP); + if (bitPos < DENTRY_PER_GROUP) { + found = true; + break; + } + (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); + } + ++level; + } + return E_OK; +} + +int32_t CloudDiskServiceMetaFile::HandleFileByFd(unsigned long &endBlock, uint32_t level) +{ + struct stat fileStat; + int err = fstat(fd_, &fileStat); + if (err < 0) { + return EINVAL; + } + if ((endBlock > GetDentryGroupCnt(fileStat.st_size)) && + ftruncate(fd_, GetDcacheFileSize(level))) { + return ENOENT; + } + return E_OK; +} + +MetaFileMgr& MetaFileMgr::GetInstance() +{ + static MetaFileMgr instance_; + return instance_; +} + +std::shared_ptr MetaFileMgr::GetCloudDiskServiceMetaFile(uint32_t userId, + const uint32_t syncFolderIndex, const uint64_t inode) +{ + std::shared_ptr mFile = nullptr; + std::lock_guard lock(mtx_); + MetaFileKey key(userId, Converter::ConvertToHex(syncFolderIndex) + Converter::ConvertToHex(inode)); + auto it = metaFiles_.find(key); + if (it != metaFiles_.end()) { + metaFileList_.splice(metaFileList_.begin(), metaFileList_, it->second); + mFile = it->second->second; + } else { + if (metaFiles_.size() == MAX_META_FILE_NUM) { + auto deleteKey = metaFileList_.back().first; + metaFiles_.erase(deleteKey); + metaFileList_.pop_back(); + } + mFile = std::make_shared(userId, syncFolderIndex, inode); + metaFileList_.emplace_front(key, mFile); + metaFiles_[key] = metaFileList_.begin(); + } + return mFile; +} + +int32_t MetaFileMgr::GetRelativePath(const std::shared_ptr metaFile, std::string &path) +{ + auto pMetaFile = metaFile; + uint32_t userId = metaFile->userId_; + uint32_t syncFolderIndex = Converter::ConvertFromHex(metaFile->syncFolderIndex_); + while (pMetaFile->parentDentryFile_ != ROOT_PARENTDENTRYFILE) { + uint64_t inode = Converter::ConvertFromHex(pMetaFile->parentDentryFile_); + auto parentMetaFile = GetCloudDiskServiceMetaFile(userId, syncFolderIndex, inode); + + MetaBase mBase(pMetaFile->selfRecordId_, false); + int32_t ret = parentMetaFile->DoLookupByRecordId(mBase); + if (ret != 0) { + LOGE("lookup by id failed"); + return -1; + } + + path = mBase.name + path; + pMetaFile = parentMetaFile; + } + path = "/" + path; + return E_OK; +} + +} // namespace OHOS::FileManagement::CloudDiskService diff --git a/services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp b/services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp new file mode 100644 index 000000000..caa5f2ede --- /dev/null +++ b/services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp @@ -0,0 +1,62 @@ +/* + * 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 "cloud_disk_service_syncfolder.h" + +#include +#include +#include +#include +#include + +#include "cloud_disk_service_logfile.h" + +namespace OHOS::FileManagement::CloudDiskService { + +int32_t CloudDiskServiceSyncFolder::RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, + const std::string &path) +{ + return LogFileMgr::GetInstance().RegisterSyncFolder(userId, syncFolderIndex, path); +} + +int32_t CloudDiskServiceSyncFolder::UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) +{ + // todo 删除目录,清理map,待讨论 + return LogFileMgr::GetInstance().UnRegisterSyncFolder(userId, syncFolderIndex); +} + +void CloudDiskServiceSyncFolder::RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) +{ + LogFileMgr::GetInstance().RegisterSyncFolderChanges(userId, syncFolderIndex); +} + +void CloudDiskServiceSyncFolder::UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) +{ + LogFileMgr::GetInstance().UnRegisterSyncFolderChanges(userId, syncFolderIndex); +} + +int32_t CloudDiskServiceSyncFolder::GetSyncFolderChanges(const int32_t userId, const uint32_t syncFolderIndex, + const uint64_t start, const uint64_t count, + struct ChangesResult &changesResult) +{ + return LogFileMgr::GetInstance().PraseRequest(userId, syncFolderIndex, start, count, changesResult); +} + +int32_t CloudDiskServiceSyncFolder::SetSyncFolderChanges(std::vector &eventInfos) +{ + return LogFileMgr::GetInstance().ProduceRequest(eventInfos); +} + +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/src/converter.cpp b/services/clouddiskservice/sync_folder/src/converter.cpp new file mode 100644 index 000000000..74092e11c --- /dev/null +++ b/services/clouddiskservice/sync_folder/src/converter.cpp @@ -0,0 +1,57 @@ +/* + * 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 "converter.h" + +#include "utils_log.h" + +namespace OHOS::FileManagement::CloudDiskService { + +std::string Converter::ConvertToHex(uint64_t value) +{ + char buffer[17]; + char *p = buffer + 16; + *p = '\0'; + + for (int i = 0; i < 16; i++) { + --p; + int digit = value & 0xF; + *p = digit < 10 ? '0' + digit : 'a' + digit - 10; + value >>= 4; + } + + return std::string(buffer); +} + +uint64_t Converter::ConvertFromHex(const std::string &hex) +{ + uint64_t value = 0; + for (char c : hex) { + value <<= 4; + if (c >= '0' && c <= '9') { + value |= c - '0'; + } else if (c >= 'a' && c <= 'f') { + value |= c - 'a' + 10; + } else if (c >= 'A' && c <= 'F') { + value |= c - 'A' + 10; + } else { + LOGE("Invalid hex string: %{public}s", hex.c_str()); + return 0; + } + } + return value; +} + +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/src/uuid_helper.cpp b/services/clouddiskservice/sync_folder/src/uuid_helper.cpp new file mode 100644 index 000000000..d9fb602b8 --- /dev/null +++ b/services/clouddiskservice/sync_folder/src/uuid_helper.cpp @@ -0,0 +1,52 @@ +/* + * 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 "uuid_helper.h" + +namespace OHOS::FileManagement::CloudDiskService { + +std::string UuidHelper::GenerateUuid() +{ + std::string guid(""); + uuid_t uuid; + uuid_generate(uuid); + char str[50] = {}; + uuid_unparse(uuid, str); + guid.assign(str); + return guid; +} + +std::string UuidHelper::GenerateUuidWithoutDelim() +{ + std::string str = GenerateUuid(); + std::string uuid; + for (auto &ch : str) { + if (ch != '-') { + uuid += ch; + } + } + return uuid; +} + +std::string UuidHelper::GenerateUuidOnly() +{ + std::string guid(""); + uuid_t uuid; + uuid_generate(uuid); + guid.assign(reinterpret_cast(uuid), 16); + return guid; +} + +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/utils/include/cloud_disk_service_error.h b/services/clouddiskservice/utils/include/cloud_disk_service_error.h new file mode 100644 index 000000000..fac344e15 --- /dev/null +++ b/services/clouddiskservice/utils/include/cloud_disk_service_error.h @@ -0,0 +1,29 @@ +/* + * 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 CLOUD_DISK_SERVICE_ERROR_H +#define CLOUD_DISK_SERVICE_ERROR_H + +namespace OHOS::FileManagement::CloudDiskService { + +enum CloudDiskServiceErrCode : uint32_t { + E_OK = 0, + E_END_OF_LOGFILE, + E_OUT_OF_RANGE, +}; + +} // namespace OHOS::FileManagement::CloudDiskService + +#endif // CLOUD_DISK_SERVICE_ERROR_H diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp new file mode 100644 index 000000000..ed8ea97d9 --- /dev/null +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -0,0 +1,388 @@ +/* + * 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 + +#include "assistant.h" +#include "cloud_disk_service_metafile.cpp" + +namespace OHOS::FileManagement::::CloudDiskService::Test { +using namespace testing; +using namespace testing::ext; + +class CloudDiskServiceDentryGroupTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline std::shared_ptr insMock = nullptr; +}; + +void CloudDiskServiceDentryGroupTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CloudDiskServiceDentryGroupTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CloudDiskServiceDentryGroupTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; + AssistantMock::EnableMock(); + insMock = std::make_shared(); + Assistant::ins = insMock; +} + +void CloudDiskServiceDentryGroupTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; + AssistantMock::DisableMock(); + Assistant::ins = nullptr; + insMock = nullptr; +} + +/** + * @tc.name: GetBucketByLevelTest001 + * @tc.desc: Verify the GetBucketByLevel function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetOverallBucketTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetOverallBucketTest001 Start"; + try { + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t ret = GetOverallBucket(level); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetOverallBucketTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetOverallBucketTest001 End"; +} + +/** + * @tc.name: GetOverallBucketTest002 + * @tc.desc: Verify the GetOverallBucket function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetOverallBucketTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetOverallBucketTest002 Start"; + try { + uint32_t level = 1; + uint32_t ret = GetOverallBucket(level); + EXPECT_EQ(ret, static_cast((1ULL << level + 1)) - 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetOverallBucketTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GGetOverallBucketTest002 End"; +} + +/** + * @tc.name: GetDcacheFileSizeTest001 + * @tc.desc: Verify the GetDcacheFileSize function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetDcacheFileSizeTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 Start"; + try { + uint32_t level = 1; + size_t buckets = GetOverallBucket(level); + size_t ret = GetDcacheFileSize(level); + EXPECT_EQ(ret, buckets * DENTRYGROUP_SIZE * BUCKET_BLOCKS + DENTRYGROUP_HEADER); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 End"; +} + +/** + * @tc.name: GetBucketByLevelTest001 + * @tc.desc: Verify the GetBucketByLevel function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketByLevelTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBucketByLevelTest001 Start"; + try { + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t ret = GetBucketByLevel(level); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBucketByLevelTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetBucketByLevelTest001 End"; +} + +/** + * @tc.name: GetBucketByLevelTest002 + * @tc.desc: Verify the GetBucketByLevel function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketByLevelTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBucketByLevelTest002 Start"; + try { + uint32_t level = 1; + uint32_t ret = GetBucketByLevel(level); + EXPECT_EQ(ret, static_cast((1ULL << level))); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBucketByLevelTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GetBucketByLevelTest002 End"; +} + +/** + * @tc.name: GetBucketaddrTest001 + * @tc.desc: Verify the GetBucketaddr function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBucketaddrTest001 Start"; + try { + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t buckoffset = 0; + uint32_t ret = GetBucketaddr(level, buckoffset); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBucketaddrTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetBucketaddrTest001 End"; +} + +/** + * @tc.name: GetBucketaddrTest002 + * @tc.desc: Verify the GetBucketaddr function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBucketaddrTest002 Start"; + try { + uint32_t level = 1; + uint64_t curLevelMaxBucks = (1ULL << level); + uint32_t buckoffset = curLevelMaxBucks + 1; + uint32_t ret = GetBucketaddr(level, buckoffset); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBucketaddrTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GetBucketaddrTest002 End"; +} + +/** + * @tc.name: GetBucketaddrTest003 + * @tc.desc: Verify the GetBucketaddr function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBucketaddrTest003 Start"; + try { + uint32_t level = 1; + uint64_t curLevelMaxBucks = (1ULL << level); + uint32_t buckoffset = curLevelMaxBucks - 1; + uint32_t ret = GetBucketaddr(level, buckoffset); + EXPECT_EQ(ret, static_cast(curLevelMaxBucks) + buckoffset - 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBucketaddrTest003 ERROR"; + } + GTEST_LOG_(INFO) << "GetBucketaddrTest003 End"; +} + +/** + * @tc.name: GetBidxFromLevelTest001 + * @tc.desc: Verify the GetBidxFromLevel function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetBidxFromLevelTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 Start"; + try { + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t namehash = 1; + unsigned long ret = GetBidxFromLevel(level, namehash) + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 End"; +} + +/** + * @tc.name: GetBidxFromLevelTest002 + * @tc.desc: Verify the GetBidxFromLevel function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, GetBidxFromLevelTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 Start"; + try { + uint32_t level = 1; + uint32_t namehash = 1; + uint32_t bucket = GetBucketByLevel(level); + unsigned long ret = GetBidxFromLevel(level, namehash) + EXPECT_EQ(ret, BUCKET_BLOCKS * GetBucketaddr(level, namehash % bucket)); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 End"; +} + +/** + * @tc.name: FindDentryPageTest001 + * @tc.desc: Verify the FindDentryPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FindDentryPageTest001 Start"; + try { + uint64_t index; + off_t pos = index * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; + DcacheLookupCtx *ctx = std::make_shared(); + auto dentryBlk = std::make_unique(); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); + auto ret = FindDentryPage(index, ctx); + EXPECT_EQ(ret, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FindDentryPageTest001 ERROR"; + } + GTEST_LOG_(INFO) << "FindDentryPageTest001 End"; +} + +/** + * @tc.name: FindDentryPageTest002 + * @tc.desc: Verify the FindDentryPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FindDentryPageTest002 Start"; + try { + uint64_t index; + off_t pos = index * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; + DcacheLookupCtx *ctx = std::make_shared(); + auto dentryBlk = std::make_unique(); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); + auto ret = FindDentryPage(index, ctx); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FindDentryPageTest002 ERROR"; + } + GTEST_LOG_(INFO) << "FindDentryPageTest002 End"; +} + +/** + * @tc.name: FindDentryPageTest003 + * @tc.desc: Verify the FindDentryPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FindDentryPageTest003 Start"; + try { + uint64_t index; + off_t pos = index * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; + DcacheLookupCtx *ctx = std::make_shared(); + auto dentryBlk = std::make_unique(); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).Times(2).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); + auto ret = FindDentryPage(index, ctx); + EXPECT_EQ(ret, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FindDentryPageTest003 ERROR"; + } + GTEST_LOG_(INFO) << "FindDentryPageTest003 End"; +} + +/** + * @tc.name: FindInBlockByIdTest001 + * @tc.desc: Verify the FindInBlockById function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, FindInBlockByIdTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FindInBlockByIdTest001 Start"; + try { + CloudDiskServiceDentryGroup dentryBlk; + uint32_t namehash = 0; + std::string recordId = "test"; + uint8_t revalidate = 0; + auto ret = FindInBlockById(dentryBlk, namehash, recordId, revalidate); + EXPECT_EQ(ret, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FindInBlockByIdTest001 ERROR"; + } + GTEST_LOG_(INFO) << "FindInBlockByIdTest001 End"; +} + +/** + * @tc.name: FindInBlockTest001 + * @tc.desc: Verify the FindInBlock function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceDentryGroupTest, FindInBlockTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FindInBlockIdTest001 Start"; + try { + CloudDiskServiceDentryGroup dentryBlk; + uint32_t namehash = 0; + std::string name = "testName"; + uint8_t revalidate = 0; + auto ret = FindInBlock(dentryBlk, namehash, name, revalidate); + EXPECT_EQ(ret, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FindInBlockTest001 ERROR"; + } + GTEST_LOG_(INFO) << "FindInBlockTest001 End"; +} + +} \ No newline at end of file -- Gitee From 690c5f5674d7cb4c1513149bd9722193ca8c354a Mon Sep 17 00:00:00 2001 From: ADHDer <19857096219@163.com> Date: Sun, 7 Sep 2025 20:16:35 +0800 Subject: [PATCH 02/26] /sync_folder/metafileTDD+gn Signed-off-by: ADHDer <19857096219@163.com> --- test/unittests/clouddiskservice/BUILD.gn | 95 +++++++ .../clouddiskservice/mock/assistant.h | 50 ++++ .../mock/system_function_mock.cpp | 50 ++++ .../cloud_disk_service_metafile_test.cpp | 267 ++++++++++++++++-- .../sync_folder/convertor_test.cpp | 101 +++++++ 5 files changed, 536 insertions(+), 27 deletions(-) create mode 100644 test/unittests/clouddiskservice/BUILD.gn create mode 100644 test/unittests/clouddiskservice/mock/assistant.h create mode 100644 test/unittests/clouddiskservice/mock/system_function_mock.cpp create mode 100644 test/unittests/clouddiskservice/sync_folder/convertor_test.cpp diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn new file mode 100644 index 000000000..7ba8c6f3e --- /dev/null +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -0,0 +1,95 @@ +# 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. + +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") + +ohos_unittest("cloud_disk_service_metafile_test") { + module_out_path = "dfs_service/dfs_service" + + sources = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock/system_function_mock.cpp", + "${distributedfile_path}/utils/log/src/utils_log.cpp", + "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp", + "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + "sync_folder/loud_disk_service_metafile_test.cpp", + ] + + include_dirs = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock", + "${distributedfile_path}/utils/dentry/include", + "${services_path}/clouddiskservice/sync_folder/include", + "${services_path}/clouddiskservice/utils/include", + ] + + deps = [ + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "e2fsprogs:libext2_uuid", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "json:nlohmann_json_static", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + use_exceptions = true +} + +ohos_unittest("convertor_test") { + module_out_path = "dfs_service/dfs_service" + + sources = [ + "${distributedfile_path}/utils/log/src/utils_log.cpp", + "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + "sync_folder/convertor_test.cpp", + ] + + include_dirs = [ + "${services_path}/clouddiskservice/sync_folder/include", + "${services_path}/clouddiskservice/utils/include", + ] + + deps = [ + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + use_exceptions = true +} + +group("clouddisk_service_test") { + testonly = true + deps = [ + ":cloud_disk_service_metafile_test", + ":convertor_test", + ] +} diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h new file mode 100644 index 000000000..7c43634e5 --- /dev/null +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -0,0 +1,50 @@ +/* + * 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 TEST_UNITTEST_CLOUD_DISK_SERVICE_ASSISTANT_H +#define TEST_UNITTEST_CLOUD_DISK_SERVICE_ASSISTANT_H + +#include + +namespace OHOS::FileManagement::CloudDiskService { +class Assistant { +public: + static inline std::shared_ptr ins = nullptr; +public: + virtual ~Assistant() = default; + virtual ssize_t readlink(const char *pathname, char *buf, size_t bufsize) = 0; + virtual int fstat(int fd, struct stat *buf) = 0; + virtual int ftruncate(int fd, off_t length) = 0; +public: + // file_utils + virtual int64_t ReadFile(int fd, off_t offset, size_t size, void *data) = 0; + virtual int64_t WriteFile(int fd, const void *data, off_t offset, size_t size) = 0; + virtual int FilePosLock(int fd, off_t offset, size_t size, int type) = 0; +}; + +class AssistantMock : public Assistant { +public: + MOCK_METHOD3(readlink, ssize_t(const char *, char *, size_t)); + MOCK_METHOD2(fstat, int(int, struct stat *)); + MOCK_METHOD2(ftruncate, int(int, off_t)); + +public: + // file_utils + MOCK_METHOD4(ReadFile, int64_t(int fd, off_t offset, size_t size, void *data)); + MOCK_METHOD4(WriteFile, int64_t(int fd, const void *data, off_t offset, size_t size)); + MOCK_METHOD4(FilePosLock, int(int fd, off_t offset, size_t size, int type)); +}; +} + +#endif // TEST_UNITTEST_CLOUD_DISK_SERVICE_ASSISTANT_H \ No newline at end of file diff --git a/test/unittests/clouddiskservice/mock/system_function_mock.cpp b/test/unittests/clouddiskservice/mock/system_function_mock.cpp new file mode 100644 index 000000000..130d495e3 --- /dev/null +++ b/test/unittests/clouddiskservice/mock/system_function_mock.cpp @@ -0,0 +1,50 @@ +/* + * 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 "assistant.h" +#include "file_utils.h" + +using namespace OHOS::FileManagement::CloudDiskService; + +ssize_t readlink(const char *pathname, char *buf, size_t bufsize) +{ + return Assistant::ins->readlink(pathname, buf, bufsize); +} + +int fstat(int fd, struct stat *buf) +{ + return Assistant::ins->fstat(fd, buf); +} + +int ftruncate(int fd, off_t length) +{ + return Assistant::ins->ftruncate(fd, length); +} + +namespace OHOS::FileManagement { +int64_t FileUtils::ReadFile(int fd, off_t offset, size_t size, void *data) +{ + return Assistant::ins->ReadFile(fd, offset, size, data); +} + +int64_t FileUtils::WriteFile(int fd, const void *data, off_t offset, size_t size) +{ + return Assistant::ins->WriteFile(fd, data, offset, size); +} + +int FileRangeLock::FilePosLock(int fd, off_t offset, size_t size, int type) +{ + return Assistant::ins->FilePosLock(fd, offset, size, type); +} +} // namespace OHOS::FileManagement \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp index ed8ea97d9..8bb248992 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -16,43 +16,44 @@ #include #include "assistant.h" -#include "cloud_disk_service_metafile.cpp" +#include "dfs_error.h" -namespace OHOS::FileManagement::::CloudDiskService::Test { +#include "cloud_disk_service_metafile.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace std; using namespace testing; using namespace testing::ext; -class CloudDiskServiceDentryGroupTest : public testing::Test { +class CloudDiskServiceMetafileTest : public testing::Test { public: static void SetUpTestCase(void); static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline std::shared_ptr insMock = nullptr; + static inline shared_ptr insMock; }; -void CloudDiskServiceDentryGroupTest::SetUpTestCase(void) +void CloudDiskServiceMetafileTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; } -void CloudDiskServiceDentryGroupTest::TearDownTestCase(void) +void CloudDiskServiceMetafileTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; } -void CloudDiskServiceDentryGroupTest::SetUp(void) +void CloudDiskServiceMetafileTest::SetUp(void) { GTEST_LOG_(INFO) << "SetUp"; - AssistantMock::EnableMock(); - insMock = std::make_shared(); + insMock = make_shared(); Assistant::ins = insMock; } -void CloudDiskServiceDentryGroupTest::TearDown(void) +void CloudDiskServiceMetafileTest::TearDown(void) { GTEST_LOG_(INFO) << "TearDown"; - AssistantMock::DisableMock(); Assistant::ins = nullptr; insMock = nullptr; } @@ -63,7 +64,7 @@ void CloudDiskServiceDentryGroupTest::TearDown(void) * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetOverallBucketTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetOverallBucketTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "GetOverallBucketTest001 Start"; try { @@ -83,7 +84,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetOverallBucketTest001, TestSize.Leve * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetOverallBucketTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetOverallBucketTest002, TestSize.Level1) { GTEST_LOG_(INFO) << "GetOverallBucketTest002 Start"; try { @@ -103,7 +104,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetOverallBucketTest002, TestSize.Leve * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetDcacheFileSizeTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetDcacheFileSizeTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 Start"; try { @@ -124,7 +125,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetDcacheFileSizeTest001, TestSize.Lev * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketByLevelTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketByLevelTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "GetBucketByLevelTest001 Start"; try { @@ -144,7 +145,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketByLevelTest001, TestSize.Leve * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketByLevelTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketByLevelTest002, TestSize.Level1) { GTEST_LOG_(INFO) << "GetBucketByLevelTest002 Start"; try { @@ -164,7 +165,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketByLevelTest002, TestSize.Leve * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "GetBucketaddrTest001 Start"; try { @@ -185,7 +186,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest001, TestSize.Level1) * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest002, TestSize.Level1) { GTEST_LOG_(INFO) << "GetBucketaddrTest002 Start"; try { @@ -207,7 +208,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest003, TestSize.Level1) { GTEST_LOG_(INFO) << "GetBucketaddrTest003 Start"; try { @@ -229,7 +230,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetBucketaddrTest003, TestSize.Level1) * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetBidxFromLevelTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBidxFromLevelTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 Start"; try { @@ -250,7 +251,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetBidxFromLevelTest001, TestSize.Leve * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, GetBidxFromLevelTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBidxFromLevelTest002, TestSize.Level1) { GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 Start"; try { @@ -272,7 +273,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, GetBidxFromLevelTest002, TestSize.Leve * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "FindDentryPageTest001 Start"; try { @@ -296,7 +297,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest001, TestSize.Level1 * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest002, TestSize.Level1) { GTEST_LOG_(INFO) << "FindDentryPageTest002 Start"; try { @@ -320,7 +321,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest002, TestSize.Level1 * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest003, TestSize.Level1) { GTEST_LOG_(INFO) << "FindDentryPageTest003 Start"; try { @@ -345,7 +346,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, FindDentryPageTest003, TestSize.Level1 * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, FindInBlockByIdTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindInBlockByIdTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "FindInBlockByIdTest001 Start"; try { @@ -368,7 +369,7 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, FindInBlockByIdTest001, TestSize.Level * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceDentryGroupTest, FindInBlockTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindInBlockTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "FindInBlockIdTest001 Start"; try { @@ -385,4 +386,216 @@ HWTEST_F(CloudDiskServiceDentryGroupTest, FindInBlockTest001, TestSize.Level1) GTEST_LOG_(INFO) << "FindInBlockTest001 End"; } -} \ No newline at end of file +/** + * @tc.name: DecodeDentryHeaderTest001 + * @tc.desc: Verify the DecodeDentryHeader function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.DecodeDentryHeader(); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 ERROR"; + } + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 End"; +} + +/** + * @tc.name: DecodeDentryHeaderTest002 + * @tc.desc: Verify the DecodeDentryHeader function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.DecodeDentryHeader(); + EXPECT_EQ(ret, 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 ERROR"; + } + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 End"; +} + +/** + * @tc.name: GenericDentryHeaderTest001 + * @tc.desc: Verify the GenericDentryHeader function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 End"; +} + +/** + * @tc.name: GenericDentryHeaderTest002 + * @tc.desc: Verify the GenericDentryHeader function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + struct stat mockStat = { + .st_size = 1, + } + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 End"; +} + +/** + * @tc.name: GenericDentryHeaderTest003 + * @tc.desc: Verify the GenericDentryHeader function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + struct stat mockStat = { + .st_size = 1, + } + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 ERROR"; + } + GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 End"; +} + +/** + * @tc.name: GenericDentryHeaderTest004 + * @tc.desc: Verify the GenericDentryHeader function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.parentDentryFile_ = "parent_file"; + mFile.selfRecordId_ = "record_123"; + mFile.selfHash_ = 0x12345678; + struct stat mockStat = { + .st_size = DENTRYGROUP_HEADER, + } + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(123)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, 123); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 ERROR"; + } + GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 End"; +} + +/** + * @tc.name: GenericDentryHeaderTest005 + * @tc.desc: Verify the GenericDentryHeader function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + string parentDentryFileStr = "parent_file"; + string selfRecordIdStr = "record_123"; + auto selfHashNum = 0x12345678; + mFile.parentDentryFile_ = parentDentryFileStr; + mFile.selfRecordId_ = selfRecordIdStr; + mFile.selfHash_ = selfHashNum; + struct stat mockStat = { + .st_size = DENTRYGROUP_HEADER, + } + + CloudDiskServiceDcacheHeader header = {}; + copy(parentDentryFileStr.begin(), + min(parentDentryFileStr.end(), parentDentryFileStr.begin() + DENTRYFILE_NAME_LEN), + header.parentDentryfile); + copy(selfRecordIdStr.begin(), + min(selfRecordIdStr.end(), selfRecordIdStr.begin() + RECORD_ID_LEN), + header.selfRecordId); + header.selfHash = selfHashNum; + + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)) + .WillOnce(Return(static_cast(sizeof(CloudDiskServiceDcacheHeader)))); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, 123); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 ERROR"; + } + GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 End"; +} + +/** + * @tc.name: DoCreateTest001 + * @tc.desc: Verify the DoCreate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoCreateTest001 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + base.name = "test_name"; + base.recordId = "record_123"; + auto ret = mFile.DoCreate(base); + EXPECT_EQ(ret, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoCreateTest001 ERROR"; + } + GTEST_LOG_(INFO) << "DoCreateTest001 End"; +} +} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/convertor_test.cpp b/test/unittests/clouddiskservice/sync_folder/convertor_test.cpp new file mode 100644 index 000000000..8a7487bc8 --- /dev/null +++ b/test/unittests/clouddiskservice/sync_folder/convertor_test.cpp @@ -0,0 +1,101 @@ +/* + * 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 +#include + +#include "converter.h" +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement::CloudDiskService { +namespace Test { +using namespace testing::ext; +using namespace std; + +class ConvertorTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void ConvertorTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void ConvertorTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void ConvertorTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void ConvertorTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: ConvertToHexTest001 + * @tc.desc: Verify the ConvertToHex function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(ConvertorTest, ConvertToHexTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ConvertToHexTest001 start"; + try { + EXPECT_EQ(Converter::ConvertToHex(0), "0000000000000000"); + EXPECT_EQ(Converter::ConvertToHex(1), "0000000000000001"); + EXPECT_EQ(Converter::ConvertToHex(255), "00000000000000ff"); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ConvertToHexTest001 failed"; + } + GTEST_LOG_(INFO) << "ConvertToHexTest001 end"; +} + +/** + * @tc.name: ConvertFromHexTest001 + * @tc.desc: Verify the ConvertFromHex function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(ConvertorTest, ConvertFromHexTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ConvertFromHexTest001 start"; + try { + EXPECT_EQ(Converter::ConvertFromHex("0"), 0ULL); + EXPECT_EQ(Converter::ConvertFromHex("a"), 10ULL); + EXPECT_EQ(Converter::ConvertFromHex("F"), 15ULL); + EXPECT_EQ(Converter::ConvertFromHex("G"), 0ULL); + EXPECT_EQ(Converter::ConvertFromHex(""), 0ULL); + EXPECT_EQ(Converter::ConvertFromHex("123456789ABCDEF0"), 0x123456789ABCDEF0ULL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ConvertFromHexTest001 failed"; + } + GTEST_LOG_(INFO) << "ConvertFromHexTest001 end"; +} + +} // namespace Test +} // namespace FileManagement::CloudDiskService +} // namespace OHOS \ No newline at end of file -- Gitee From 42a288ff6707ce8c01d4a469c50ca0c4781196ae Mon Sep 17 00:00:00 2001 From: ADHDer <19857096219@163.com> Date: Mon, 8 Sep 2025 14:51:37 +0800 Subject: [PATCH 03/26] uuid_helper_TDD Signed-off-by: ADHDer <19857096219@163.com> --- test/unittests/clouddiskservice/BUILD.gn | 35 ++++++ .../cloud_disk_service_metafile_test.cpp | 25 ++++ .../sync_folder/uuid_helper_test.cpp | 116 ++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 test/unittests/clouddiskservice/sync_folder/uuid_helper_test.cpp diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index 7ba8c6f3e..6a269339b 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -86,10 +86,45 @@ ohos_unittest("convertor_test") { use_exceptions = true } +ohos_unittest("uuid_helper_test") { + module_out_path = "dfs_service/dfs_service" + + sources = [ + "${distributedfile_path}/utils/log/src/utils_log.cpp", + "${services_path}/clouddiskservice/sync_folder/src/uuid_helper.cpp", + "sync_folder/uuid_helper_test.cpp", + ] + + include_dirs = [ + "${services_path}/clouddiskservice/sync_folder/include", + "${services_path}/clouddiskservice/utils/include", + ] + + deps = [ + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "e2fsprogs:libext2_uuid", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + use_exceptions = true +} + group("clouddisk_service_test") { testonly = true deps = [ ":cloud_disk_service_metafile_test", ":convertor_test", + ":uuid_helper_test", ] } diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp index 8bb248992..b92576b57 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -598,4 +598,29 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest001, TestSize.Level1) } GTEST_LOG_(INFO) << "DoCreateTest001 End"; } + +/** + * @tc.name: DoCreateTest002 + * @tc.desc: Verify the DoCreate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoCreateTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + string longName(1000,'a'); + MetaBase base; + base.name = "test_name"; + base.recordId = "record_123"; + auto ret = mFile.DoCreate(base); + EXPECT_EQ(ret, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoCreateTest002 ERROR"; + } + GTEST_LOG_(INFO) << "DoCreateTest002 End"; +} } // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/uuid_helper_test.cpp b/test/unittests/clouddiskservice/sync_folder/uuid_helper_test.cpp new file mode 100644 index 000000000..46c8162d7 --- /dev/null +++ b/test/unittests/clouddiskservice/sync_folder/uuid_helper_test.cpp @@ -0,0 +1,116 @@ +/* + * 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 +#include + +#include "uuid_helper.h" +#include "utils_log.h" + +namespace OHOS { +namespace FileManagement::CloudDiskService { +namespace Test { +using namespace testing::ext; +using namespace std; + +class UuidHelperTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void UuidHelperTest::SetUpTestCase(void) +{ + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void UuidHelperTest::TearDownTestCase(void) +{ + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void UuidHelperTest::SetUp(void) +{ + GTEST_LOG_(INFO) << "SetUp"; +} + +void UuidHelperTest::TearDown(void) +{ + GTEST_LOG_(INFO) << "TearDown"; +} + +/** + * @tc.name: GenerateUuidTest001 + * @tc.desc: Verify the GenerateUuid function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(UuidHelperTest, GenerateUuidTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenerateUuidTest001 start"; + try { + string uuid = UuidHelper::GenerateUuid(); + EXPECT_EQ(uuid.length(), 36); + EXPECT_TRUE(uuid.find('-') != string::npos); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenerateUuidTest001 failed"; + } + GTEST_LOG_(INFO) << "GenerateUuidTest001 end"; +} + +/** + * @tc.name: GenerateUuidWithoutDelimTest001 + * @tc.desc: Verify the GenerateUuidWithoutDelim function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(UuidHelperTest, GenerateUuidWithoutDelimTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenerateUuidWithoutDelimTest001 start"; + try { + string uuid = UuidHelper::GenerateUuidWithoutDelim(); + EXPECT_EQ(uuid.length(), 32); + EXPECT_TRUE(uuid.find('-') == string::npos); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenerateUuidWithoutDelimTest001 failed"; + } + GTEST_LOG_(INFO) << "GenerateUuidWithoutDelimTest001 end"; +} + +/** + * @tc.name: GenerateUuidOnlyTest001 + * @tc.desc: Verify the GenerateUuidOnly function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(UuidHelperTest, GenerateUuidOnlyTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenerateUuidOnlyTest001 start"; + try { + string uuid = UuidHelper::GenerateUuidOnly(); + EXPECT_EQ(uuid.length(), 16); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenerateUuidOnlyTest001 failed"; + } + GTEST_LOG_(INFO) << "GenerateUuidOnlyTest001 end"; +} +} // namespace Test +} // namespace FileManagement::CloudDiskService +} // namespace OHOS \ No newline at end of file -- Gitee From dfb59fc6566736b30c264cf7d15cea07626d870c Mon Sep 17 00:00:00 2001 From: yujiahe Date: Mon, 8 Sep 2025 15:02:05 +0800 Subject: [PATCH 04/26] add cloud_disk_service_syncfolder_test Signed-off-by: yujiahe --- .../cloud_disk_service_syncfolder_test.cpp | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp new file mode 100644 index 000000000..25d0b43e2 --- /dev/null +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp @@ -0,0 +1,174 @@ +/* + * 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 "cloud_disk_service_syncfolder.h" + +#include +#include + +namespace OHOS::FileManagement::CloudDiskService::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CloudDiskServiceSyncFolderTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr syncFolder_; +}; + +void CloudDiskServiceSyncFolderTest::SetUpTestCase(void) { + GTEST_LOG_(INFO) << "SetUpTestCase"; + syncFolder_ = make_shared(); +} + +void CloudDiskServiceSyncFolderTest::TearDownTestCase(void) { + GTEST_LOG_(INFO) << "TearDownTestCase"; + syncFolder_ = nullptr; +} + +void CloudDiskServiceSyncFolderTest::SetUp() { + +} + +void CloudDiskServiceSyncFolderTest::TearDown() { + +} + +/** + * @tc.name: RegisterSyncFolderTest001 + * @tc.desc: Verify the RegisterSyncFolder function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceSyncFolderTest, RegisterSyncFolderTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 start"; + try { + int32_t userId = 1; + uint64_t syncFolderIndex = 1; + string path = "path"; + syncFolder_ ->RegisterSyncFolder(userId, syncFolderIndex, path); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 failed"; + } + GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 end"; +} + +/** + * @tc.name: UnRegisterSyncFolderTest001 + * @tc.desc: Verify the UnRegisterSyncFolder function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceSyncFolderTest, UnRegisterSyncFolderTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 start"; + try { + int32_t userId = 1; + uint64_t syncFolderIndex = 1; + syncFolder_ ->UnRegisterSyncFolder(userId, syncFolderIndex); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 failed"; + } + GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 end"; +} + +/** + * @tc.name: RegisterSyncFolderChangesTest001 + * @tc.desc: Verify the RegisterSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceSyncFolderTest, RegisterSyncFolderChangesTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 start"; + try { + int32_t userId = 1; + uint64_t syncFolderIndex = 1; + syncFolder_ ->RegisterSyncFolderChanges(userId, syncFolderIndex); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 failed"; + } + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 end"; +} + +/** + * @tc.name: UnRegisterSyncFolderChangesTest001 + * @tc.desc: Verify the RegisterSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceSyncFolderTest, UnRegisterSyncFolderChangesTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 start"; + try { + int32_t userId = 1; + uint64_t syncFolderIndex = 1; + syncFolder_ ->UnRegisterSyncFolderChanges(userId, syncFolderIndex); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 failed"; + } + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 end"; +} + +/** + * @tc.name: GetSyncFolderChangesTest001 + * @tc.desc: Verify the GetSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceSyncFolderTest, GetSyncFolderChangesTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetSyncFolderChangesTest001 start"; + try { + int32_t userId = 1; + uint64_t syncFolderIndex = 1; + uint64_t start = 1; + uint64_t count = 1; + ChangesResult changesResult; + syncFolder_ ->GetSyncFolderChanges(userId, syncFolderIndex, start, count, changesResult); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetSyncFolderChangesTest001 failed"; + } + GTEST_LOG_(INFO) << "GetSyncFolderChangesTest001 end"; +} + +/** + * @tc.name: SetSyncFolderChangesTest001 + * @tc.desc: Verify the SetSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceSyncFolderTest, SetSyncFolderChangesTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SetSyncFolderChangesTest001 start"; + try { + EventInfo eventInfo; + syncFolder_ ->SetSyncFolderChanges(eventInfo); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SetSyncFolderChangesTest001 failed"; + } + GTEST_LOG_(INFO) << "SetSyncFolderChangesTest001 end"; +} +} \ No newline at end of file -- Gitee From 496689bf495e2dee45bd692117ed1e4c18fcd734 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Mon, 8 Sep 2025 15:08:35 +0800 Subject: [PATCH 05/26] fix cloud_disk_service_syncfolder_test Signed-off-by: yujiahe --- .../cloud_disk_service_syncfolder_test.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp index 25d0b43e2..f0a29c3ea 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "cloud_disk_service_syncfolder.h" +#include "cloud_disk_service_syncfolder.cpp" #include #include @@ -28,17 +28,14 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); - static inline shared_ptr syncFolder_; }; void CloudDiskServiceSyncFolderTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - syncFolder_ = make_shared(); } void CloudDiskServiceSyncFolderTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; - syncFolder_ = nullptr; } void CloudDiskServiceSyncFolderTest::SetUp() { @@ -62,7 +59,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, RegisterSyncFolderTest001, TestSize.Lev int32_t userId = 1; uint64_t syncFolderIndex = 1; string path = "path"; - syncFolder_ ->RegisterSyncFolder(userId, syncFolderIndex, path); + RegisterSyncFolder(userId, syncFolderIndex, path); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 failed"; @@ -82,7 +79,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, UnRegisterSyncFolderTest001, TestSize.L try { int32_t userId = 1; uint64_t syncFolderIndex = 1; - syncFolder_ ->UnRegisterSyncFolder(userId, syncFolderIndex); + UnRegisterSyncFolder(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 failed"; @@ -102,7 +99,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, RegisterSyncFolderChangesTest001, TestS try { int32_t userId = 1; uint64_t syncFolderIndex = 1; - syncFolder_ ->RegisterSyncFolderChanges(userId, syncFolderIndex); + RegisterSyncFolderChanges(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 failed"; @@ -122,7 +119,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, UnRegisterSyncFolderChangesTest001, Tes try { int32_t userId = 1; uint64_t syncFolderIndex = 1; - syncFolder_ ->UnRegisterSyncFolderChanges(userId, syncFolderIndex); + UnRegisterSyncFolderChanges(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 failed"; @@ -145,7 +142,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, GetSyncFolderChangesTest001, TestSize.L uint64_t start = 1; uint64_t count = 1; ChangesResult changesResult; - syncFolder_ ->GetSyncFolderChanges(userId, syncFolderIndex, start, count, changesResult); + GetSyncFolderChanges(userId, syncFolderIndex, start, count, changesResult); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GetSyncFolderChangesTest001 failed"; @@ -164,7 +161,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, SetSyncFolderChangesTest001, TestSize.L GTEST_LOG_(INFO) << "SetSyncFolderChangesTest001 start"; try { EventInfo eventInfo; - syncFolder_ ->SetSyncFolderChanges(eventInfo); + SetSyncFolderChanges(eventInfo); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "SetSyncFolderChangesTest001 failed"; -- Gitee From a878ec249c14f3c7229e7810486e7a9a0aaf1168 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Mon, 8 Sep 2025 20:28:25 +0800 Subject: [PATCH 06/26] add cloud_disk_service_logfile_static_test Signed-off-by: yujiahe --- .../src/cloud_disk_service_logfile.cpp | 2 +- .../clouddiskservice/mock/assistant.h | 11 +- .../mock/system_function_mock.cpp | 10 + ...cloud_disk_service_logfile_static_test.cpp | 350 ++++++++++++++++++ .../cloud_disk_service_syncfolder_test.cpp | 12 +- 5 files changed, 375 insertions(+), 10 deletions(-) create mode 100644 test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp diff --git a/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp b/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp index 6d44cc369..eff7f04d7 100644 --- a/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp +++ b/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp @@ -34,7 +34,7 @@ namespace OHOS::FileManagement::CloudDiskService { -const std::string LOGFILENAME = "history.log"; +const std::string LOGFILENAME = "path为/data/service/el2/1/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/0000000000000001,第二个参数为0771时,运行完这个函数,path的值为多少"; const unsigned int STAT_MODE_DIR = 0771; const unsigned int MAX_CHANGEDATAS_SIZE = 20; diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h index 7c43634e5..d0183674e 100644 --- a/test/unittests/clouddiskservice/mock/assistant.h +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -21,16 +21,19 @@ namespace OHOS::FileManagement::CloudDiskService { class Assistant { public: static inline std::shared_ptr ins = nullptr; -public: + virtual ~Assistant() = default; virtual ssize_t readlink(const char *pathname, char *buf, size_t bufsize) = 0; virtual int fstat(int fd, struct stat *buf) = 0; virtual int ftruncate(int fd, off_t length) = 0; -public: + // file_utils virtual int64_t ReadFile(int fd, off_t offset, size_t size, void *data) = 0; virtual int64_t WriteFile(int fd, const void *data, off_t offset, size_t size) = 0; virtual int FilePosLock(int fd, off_t offset, size_t size, int type) = 0; + + virtual int fcntl(int fd, int op, ...) = 0; + virtual errno_t memcpy_s(void *dest, size_t destMax, int c, size_t count) = 0; }; class AssistantMock : public Assistant { @@ -39,11 +42,13 @@ public: MOCK_METHOD2(fstat, int(int, struct stat *)); MOCK_METHOD2(ftruncate, int(int, off_t)); -public: // file_utils MOCK_METHOD4(ReadFile, int64_t(int fd, off_t offset, size_t size, void *data)); MOCK_METHOD4(WriteFile, int64_t(int fd, const void *data, off_t offset, size_t size)); MOCK_METHOD4(FilePosLock, int(int fd, off_t offset, size_t size, int type)); + + MOCK_METHOD2(fcntl, int(int, int)); + MOCK_METHOD4(memcpy_s, errno_t(void *, size_t, int, size_t)); }; } diff --git a/test/unittests/clouddiskservice/mock/system_function_mock.cpp b/test/unittests/clouddiskservice/mock/system_function_mock.cpp index 130d495e3..5adc94a7d 100644 --- a/test/unittests/clouddiskservice/mock/system_function_mock.cpp +++ b/test/unittests/clouddiskservice/mock/system_function_mock.cpp @@ -32,6 +32,16 @@ int ftruncate(int fd, off_t length) return Assistant::ins->ftruncate(fd, length); } +int fcntl(int fd, int op, ...) +{ + return Assistant::ins->fcntl(fd, op); +} + +errno_t memcpy_s(void *dest, size_t destMax, int c, size_t count) +{ + return Assistant::ins->memcpy_s(dest, destMax, c, count); +} + namespace OHOS::FileManagement { int64_t FileUtils::ReadFile(int fd, off_t offset, size_t size, void *data) { diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp new file mode 100644 index 000000000..12852c8cc --- /dev/null +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp @@ -0,0 +1,350 @@ +/* + * 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 "cloud_disk_service_logfile.cpp" + +#include +#include + +namespace OHOS::FileManagement::CloudDiskService::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CloudDiskServiceLogFileStaticTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr insMock_; +}; + +void CloudDiskServiceLogFileStaticTest::SetUpTestCase(void) { + GTEST_LOG_(INFO) << "SetUpTestCase"; + insMock_ = make_shared(); + Assistant::ins = insMock_; +} + +void CloudDiskServiceLogFileStaticTest::TearDownTestCase(void) { + GTEST_LOG_(INFO) << "TearDownTestCase"; + insMock_ = nullptr; + Assistant::ins = nullptr; +} + +void CloudDiskServiceLogFileStaticTest::SetUp() { + +} + +void CloudDiskServiceLogFileStaticTest::TearDown() { + +} + +/** + * @tc.name: GetLogFileByPathTest001 + * @tc.desc: Verify the GetLogFileByPath function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, GetLogFileByPathTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetLogFileByPathTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 1; + string res = GetLogFileByPath(userId, syncFolderIndex); + EXPECT_EQ(res, + "/data/service/el2/1/hmdfs/cache/account_cache/dentry_cache" + + "/clouddisk_service_cache/0000000000000001/history.log"); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetLogFileByPathTest001 failed"; + } + GTEST_LOG_(INFO) << "GetLogFileByPathTest001 end"; +} + +/** + * @tc.name: GetBucketAndOffsetTest001 + * @tc.desc: Verify the GetBucketAndOffset function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, GetBucketAndOffsetTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBucketAndOffsetTest001 start"; + try { + uint64_t line = 1; + uint32_t bucket = 1; + uint32_t offset = 1; + GetBucketAndOffset(line, bucket, offset); + EXPECT_EQ(bucket, 0); + EXPECT_EQ(offset, 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBucketAndOffsetTest001 failed"; + } + GTEST_LOG_(INFO) << "GetBucketAndOffsetTest001 end"; +} + +/** + * @tc.name: GetBucketaddrTest001 + * @tc.desc: Verify the GetBucketaddr function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, GetBucketaddrTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetBucketaddrTest001 start"; + try { + uint32_t bucket = 1; + auto res = GetBucketaddr(bucket); + EXPECT_EQ(res, 8192); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetBucketaddrTest001 failed"; + } + GTEST_LOG_(INFO) << "GetBucketaddrTest001 end"; +} + +/** + * @tc.name: LoadCurrentPageTest001 + * @tc.desc: Verify the LoadCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LoadCurrentPageTest001 start"; + try { + int fd = 1; + uint32_t bucket = 1; + EXPECT_CALL(*insMock_, fcntl(_, _)).WillOnce(Return(-1)) + auto res = LoadCurrentPage(fd, bucket); + EXPECT_EQ(res, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "LoadCurrentPageTest001 failed"; + } + GTEST_LOG_(INFO) << "LoadCurrentPageTest001 end"; +} + +/** + * @tc.name: LoadCurrentPageTest002 + * @tc.desc: Verify the LoadCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LoadCurrentPageTest002 start"; + try { + int fd = 1; + uint32_t bucket = 1; + EXPECT_CALL(*insMock_, fcntl(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(1)); + auto res = LoadCurrentPage(fd, bucket); + EXPECT_EQ(res, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "LoadCurrentPageTest002 failed"; + } + GTEST_LOG_(INFO) << "LoadCurrentPageTest002 end"; +} + +/** + * @tc.name: LoadCurrentPageTest003 + * @tc.desc: Verify the LoadCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "LoadCurrentPageTest003 start"; + try { + int fd = 1; + uint32_t bucket = 1; + EXPECT_CALL(*insMock_, fcntl(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(4096)); + auto res = LoadCurrentPage(fd, bucket); + EXPECT_NE(res, nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "LoadCurrentPageTest003 failed"; + } + GTEST_LOG_(INFO) << "LoadCurrentPageTest003 end"; +} + +/** + * @tc.name: SyncCurrentPageTest001 + * @tc.desc: Verify the SyncCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SyncCurrentPageTest001 start"; + try { + LogGroup logGroup; + int fd = 1; + uint32_t line = 1; + EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(-1)); + auto res = SyncCurrentPage(logGroup, fd, line); + EXPECT_EQ(res, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SyncCurrentPageTest001 failed"; + } + GTEST_LOG_(INFO) << "SyncCurrentPageTest001 end"; +} + +/** + * @tc.name: SyncCurrentPageTest002 + * @tc.desc: Verify the SyncCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SyncCurrentPageTest002 start"; + try { + LogGroup logGroup; + int fd = 1; + uint32_t line = 1; + EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(1)); + auto res = SyncCurrentPage(logGroup, fd, line); + EXPECT_EQ(res, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SyncCurrentPageTest002 failed"; + } + GTEST_LOG_(INFO) << "SyncCurrentPageTest002 end"; +} + +/** + * @tc.name: SyncCurrentPageTest003 + * @tc.desc: Verify the SyncCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SyncCurrentPageTest003 start"; + try { + LogGroup logGroup; + int fd = 1; + uint32_t line = 1; + EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(4096)); + EXPECT_CALL(*insMock_, fcntl(_, _, _)).WillOnce(Return(-1)); + auto res = SyncCurrentPage(logGroup, fd, line); + EXPECT_NE(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SyncCurrentPageTest003 failed"; + } + GTEST_LOG_(INFO) << "SyncCurrentPageTest003 end"; +} + +/** + * @tc.name: SyncCurrentPageTest004 + * @tc.desc: Verify the SyncCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "SyncCurrentPageTest004 start"; + try { + LogGroup logGroup; + int fd = 1; + uint32_t line = 1; + EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(4096)); + EXPECT_CALL(*insMock_, fcntl(_, _, _)).WillOnce(Return(0)); + auto res = SyncCurrentPage(logGroup, fd, line); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "SyncCurrentPageTest004 failed"; + } + GTEST_LOG_(INFO) << "SyncCurrentPageTest004 end"; +} + +/** + * @tc.name: FillLogGroupTest001 + * @tc.desc: Verify the FillLogGroup function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, FillLogGroupTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FillLogGroupTest001 start"; + try { + LogGroup logGroup; + LogBlock logBlock; + uint32_t offset = 0; + logGroup.nsl[0].timestamp = 1; + EXPECT_CALL(*insMock_, memcpy(_, _, _, _)).WillOnce(Return(1)); + auto res = FillLogGroup(logGroup, logBlock, offset); + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FillLogGroupTest001 failed"; + } + GTEST_LOG_(INFO) << "FillLogGroupTest001 end"; +} + +/** + * @tc.name: FillLogGroupTest002 + * @tc.desc: Verify the FillLogGroup function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, FillLogGroupTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FillLogGroupTest002 start"; + try { + LogGroup logGroup; + LogBlock logBlock; + uint32_t offset = 0; + EXPECT_CALL(*insMock_, memcpy(_, _, _, _)).WillOnce(Return(0)); + auto res = FillLogGroup(logGroup, logBlock, offset); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FillLogGroupTest002 failed"; + } + GTEST_LOG_(INFO) << "FillLogGroupTest002 end"; +} + +/** + * @tc.name: GetCurrentLineTest001 + * @tc.desc: Verify the GetCurrentLine function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, GetCurrentLineTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCurrentLineTest001 start"; + try { + int fd = 1; + auto res = GetCurrentLine(fd); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCurrentLineTest001 failed"; + } + GTEST_LOG_(INFO) << "GetCurrentLineTest001 end"; +} +} \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp index f0a29c3ea..a6a8f60c9 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_syncfolder_test.cpp @@ -59,7 +59,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, RegisterSyncFolderTest001, TestSize.Lev int32_t userId = 1; uint64_t syncFolderIndex = 1; string path = "path"; - RegisterSyncFolder(userId, syncFolderIndex, path); + CloudDiskServiceSyncFolder::RegisterSyncFolder(userId, syncFolderIndex, path); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 failed"; @@ -79,7 +79,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, UnRegisterSyncFolderTest001, TestSize.L try { int32_t userId = 1; uint64_t syncFolderIndex = 1; - UnRegisterSyncFolder(userId, syncFolderIndex); + CloudDiskServiceSyncFolder::UnRegisterSyncFolder(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 failed"; @@ -99,7 +99,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, RegisterSyncFolderChangesTest001, TestS try { int32_t userId = 1; uint64_t syncFolderIndex = 1; - RegisterSyncFolderChanges(userId, syncFolderIndex); + CloudDiskServiceSyncFolder::RegisterSyncFolderChanges(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 failed"; @@ -119,7 +119,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, UnRegisterSyncFolderChangesTest001, Tes try { int32_t userId = 1; uint64_t syncFolderIndex = 1; - UnRegisterSyncFolderChanges(userId, syncFolderIndex); + CloudDiskServiceSyncFolder::UnRegisterSyncFolderChanges(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 failed"; @@ -142,7 +142,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, GetSyncFolderChangesTest001, TestSize.L uint64_t start = 1; uint64_t count = 1; ChangesResult changesResult; - GetSyncFolderChanges(userId, syncFolderIndex, start, count, changesResult); + CloudDiskServiceSyncFolder::GetSyncFolderChanges(userId, syncFolderIndex, start, count, changesResult); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GetSyncFolderChangesTest001 failed"; @@ -161,7 +161,7 @@ HWTEST_F(CloudDiskServiceSyncFolderTest, SetSyncFolderChangesTest001, TestSize.L GTEST_LOG_(INFO) << "SetSyncFolderChangesTest001 start"; try { EventInfo eventInfo; - SetSyncFolderChanges(eventInfo); + CloudDiskServiceSyncFolder::SetSyncFolderChanges(eventInfo); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "SetSyncFolderChangesTest001 failed"; -- Gitee From be531054c098e013fc21bb343a9e557229adf105 Mon Sep 17 00:00:00 2001 From: ADHDer <19857096219@163.com> Date: Mon, 8 Sep 2025 21:34:02 +0800 Subject: [PATCH 07/26] metafile_test_TDD Signed-off-by: ADHDer <19857096219@163.com> --- test/unittests/clouddiskservice/BUILD.gn | 2 +- .../cloud_disk_service_metafile_test.cpp | 598 +++++++++--------- 2 files changed, 291 insertions(+), 309 deletions(-) diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index 6a269339b..5fe16651f 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -22,7 +22,7 @@ ohos_unittest("cloud_disk_service_metafile_test") { "${distributedfile_path}/utils/log/src/utils_log.cpp", "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp", "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", - "sync_folder/loud_disk_service_metafile_test.cpp", + "sync_folder/cloud_disk_service_metafile_test.cpp", ] include_dirs = [ diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp index b92576b57..333503605 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -59,568 +59,550 @@ void CloudDiskServiceMetafileTest::TearDown(void) } /** - * @tc.name: GetBucketByLevelTest001 - * @tc.desc: Verify the GetBucketByLevel function + * @tc.name: DecodeDentryHeaderTest001 + * @tc.desc: Verify the DecodeDentryHeader function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetOverallBucketTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetOverallBucketTest001 Start"; + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 Start"; try { - uint32_t level = MAX_BUCKET_LEVEL + 1; - uint32_t ret = GetOverallBucket(level); + CloudDiskServiceMetaFile mFile(100, 1, 123); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.DecodeDentryHeader(); EXPECT_EQ(ret, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetOverallBucketTest001 ERROR"; + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetOverallBucketTest001 End"; + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 End"; } /** - * @tc.name: GetOverallBucketTest002 - * @tc.desc: Verify the GetOverallBucket function + * @tc.name: DecodeDentryHeaderTest002 + * @tc.desc: Verify the DecodeDentryHeader function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetOverallBucketTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetOverallBucketTest002 Start"; + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 Start"; try { - uint32_t level = 1; - uint32_t ret = GetOverallBucket(level); - EXPECT_EQ(ret, static_cast((1ULL << level + 1)) - 1); + CloudDiskServiceMetaFile mFile(100, 1, 123); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.DecodeDentryHeader(); + EXPECT_EQ(ret, 1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetOverallBucketTest002 ERROR"; + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 ERROR"; } - GTEST_LOG_(INFO) << "GGetOverallBucketTest002 End"; + GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 End"; } /** - * @tc.name: GetDcacheFileSizeTest001 - * @tc.desc: Verify the GetDcacheFileSize function + * @tc.name: GenericDentryHeaderTest001 + * @tc.desc: Verify the GenericDentryHeader function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetDcacheFileSizeTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 Start"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 Start"; try { - uint32_t level = 1; - size_t buckets = GetOverallBucket(level); - size_t ret = GetDcacheFileSize(level); - EXPECT_EQ(ret, buckets * DENTRYGROUP_SIZE * BUCKET_BLOCKS + DENTRYGROUP_HEADER); + CloudDiskServiceMetaFile mFile(100, 1, 123); + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 ERROR"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 End"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 End"; } /** - * @tc.name: GetBucketByLevelTest001 - * @tc.desc: Verify the GetBucketByLevel function + * @tc.name: GenericDentryHeaderTest002 + * @tc.desc: Verify the GenericDentryHeader function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetBucketByLevelTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetBucketByLevelTest001 Start"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 Start"; try { - uint32_t level = MAX_BUCKET_LEVEL + 1; - uint32_t ret = GetBucketByLevel(level); - EXPECT_EQ(ret, E_OK); + CloudDiskServiceMetaFile mFile(100, 1, 123); + struct stat mockStat = { + .st_size = 1, + } + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, ENOENT); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetBucketByLevelTest001 ERROR"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 ERROR"; } - GTEST_LOG_(INFO) << "GetBucketByLevelTest001 End"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 End"; } /** - * @tc.name: GetBucketByLevelTest002 - * @tc.desc: Verify the GetBucketByLevel function + * @tc.name: GenericDentryHeaderTest003 + * @tc.desc: Verify the GenericDentryHeader function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetBucketByLevelTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest003, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetBucketByLevelTest002 Start"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 Start"; try { - uint32_t level = 1; - uint32_t ret = GetBucketByLevel(level); - EXPECT_EQ(ret, static_cast((1ULL << level))); + CloudDiskServiceMetaFile mFile(100, 1, 123); + struct stat mockStat = { + .st_size = DENTRYGROUP_HEADER, + } + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, 1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetBucketByLevelTest002 ERROR"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 ERROR"; } - GTEST_LOG_(INFO) << "GetBucketByLevelTest002 End"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 End"; } /** - * @tc.name: GetBucketaddrTest001 - * @tc.desc: Verify the GetBucketaddr function + * @tc.name: GenericDentryHeaderTest004 + * @tc.desc: Verify the GenericDentryHeader function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest004, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetBucketaddrTest001 Start"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 Start"; try { - uint32_t level = MAX_BUCKET_LEVEL + 1; - uint32_t buckoffset = 0; - uint32_t ret = GetBucketaddr(level, buckoffset); - EXPECT_EQ(ret, E_OK); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.parentDentryFile_ = "parent_file"; + mFile.selfRecordId_ = "record_123"; + mFile.selfHash_ = 0x12345678; + struct stat mockStat = { + .st_size = 1, + } + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(123)); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, 123); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetBucketaddrTest001 ERROR"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 ERROR"; } - GTEST_LOG_(INFO) << "GetBucketaddrTest001 End"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 End"; } /** - * @tc.name: GetBucketaddrTest002 - * @tc.desc: Verify the GetBucketaddr function + * @tc.name: GenericDentryHeaderTest005 + * @tc.desc: Verify the GenericDentryHeader function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest005, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetBucketaddrTest002 Start"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 Start"; try { - uint32_t level = 1; - uint64_t curLevelMaxBucks = (1ULL << level); - uint32_t buckoffset = curLevelMaxBucks + 1; - uint32_t ret = GetBucketaddr(level, buckoffset); - EXPECT_EQ(ret, E_OK); + CloudDiskServiceMetaFile mFile(100, 1, 123); + string parentDentryFileStr = "parent_file"; + string selfRecordIdStr = "record_123"; + auto selfHashNum = 0x12345678; + mFile.parentDentryFile_ = parentDentryFileStr; + mFile.selfRecordId_ = selfRecordIdStr; + mFile.selfHash_ = selfHashNum; + struct stat mockStat = { + .st_size = DENTRYGROUP_HEADER, + } + + CloudDiskServiceDcacheHeader header = {}; + copy(parentDentryFileStr.begin(), + min(parentDentryFileStr.end(), parentDentryFileStr.begin() + DENTRYFILE_NAME_LEN), + header.parentDentryfile); + copy(selfRecordIdStr.begin(), + min(selfRecordIdStr.end(), selfRecordIdStr.begin() + RECORD_ID_LEN), + header.selfRecordId); + header.selfHash = selfHashNum; + + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)) + .WillOnce(Return(static_cast(sizeof(CloudDiskServiceDcacheHeader)))); + auto ret = mFile.GenericDentryHeader(); + EXPECT_EQ(ret, 123); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetBucketaddrTest002 ERROR"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 ERROR"; } - GTEST_LOG_(INFO) << "GetBucketaddrTest002 End"; + GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 End"; } /** - * @tc.name: GetBucketaddrTest003 - * @tc.desc: Verify the GetBucketaddr function + * @tc.name: DoCreateTest001 + * @tc.desc: Verify the DoCreate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetBucketaddrTest003 Start"; + GTEST_LOG_(INFO) << "DoCreateTest001 Start"; try { - uint32_t level = 1; - uint64_t curLevelMaxBucks = (1ULL << level); - uint32_t buckoffset = curLevelMaxBucks - 1; - uint32_t ret = GetBucketaddr(level, buckoffset); - EXPECT_EQ(ret, static_cast(curLevelMaxBucks) + buckoffset - 1); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + base.name = "test_name"; + base.recordId = "record_123"; + auto ret = mFile.DoCreate(base); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetBucketaddrTest003 ERROR"; + GTEST_LOG_(INFO) << "DoCreateTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetBucketaddrTest003 End"; + GTEST_LOG_(INFO) << "DoCreateTest001 End"; } /** - * @tc.name: GetBidxFromLevelTest001 - * @tc.desc: Verify the GetBidxFromLevel function + * @tc.name: DoCreateTest002 + * @tc.desc: Verify the DoCreate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetBidxFromLevelTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 Start"; + GTEST_LOG_(INFO) << "DoCreateTest002 Start"; try { - uint32_t level = MAX_BUCKET_LEVEL + 1; - uint32_t namehash = 1; - unsigned long ret = GetBidxFromLevel(level, namehash) - EXPECT_EQ(ret, E_OK); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + string longName(1000,'a'); + MetaBase base; + base.name = longName; + base.recordId = "record_123"; + auto ret = mFile.DoCreate(base); + EXPECT_EQ(ret, ENAMETOOLONG); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 ERROR"; + GTEST_LOG_(INFO) << "DoCreateTest002 ERROR"; } - GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 End"; + GTEST_LOG_(INFO) << "DoCreateTest002 End"; } /** - * @tc.name: GetBidxFromLevelTest002 - * @tc.desc: Verify the GetBidxFromLevel function + * @tc.name: DoRemoveTest001 + * @tc.desc: Verify the DoRemove function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetBidxFromLevelTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoRemoveTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 Start"; + GTEST_LOG_(INFO) << "DoRemoveTest001 Start"; try { - uint32_t level = 1; - uint32_t namehash = 1; - uint32_t bucket = GetBucketByLevel(level); - unsigned long ret = GetBidxFromLevel(level, namehash) - EXPECT_EQ(ret, BUCKET_BLOCKS * GetBucketaddr(level, namehash % bucket)); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoRemove(base, recordId); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 ERROR"; + GTEST_LOG_(INFO) << "DoRemoveTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 End"; + GTEST_LOG_(INFO) << "DoRemoveTest001 End"; } /** - * @tc.name: FindDentryPageTest001 - * @tc.desc: Verify the FindDentryPage function + * @tc.name: DoFlushTest001 + * @tc.desc: Verify the DoFlush function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoFlushTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "FindDentryPageTest001 Start"; + GTEST_LOG_(INFO) << "DoFlushTest001 Start"; try { - uint64_t index; - off_t pos = index * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; - DcacheLookupCtx *ctx = std::make_shared(); - auto dentryBlk = std::make_unique(); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); - auto ret = FindDentryPage(index, ctx); - EXPECT_EQ(ret, nullptr); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoRemove(base, recordId); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FindDentryPageTest001 ERROR"; + GTEST_LOG_(INFO) << "DoRemoveTest001 ERROR"; } - GTEST_LOG_(INFO) << "FindDentryPageTest001 End"; + GTEST_LOG_(INFO) << "DoRemoveTest001 End"; } /** - * @tc.name: FindDentryPageTest002 - * @tc.desc: Verify the FindDentryPage function + * @tc.name: DoUpdateTest001 + * @tc.desc: Verify the DoUpdate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoUpdateTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "FindDentryPageTest002 Start"; + GTEST_LOG_(INFO) << "DoUpdateTest001 Start"; try { - uint64_t index; - off_t pos = index * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; - DcacheLookupCtx *ctx = std::make_shared(); - auto dentryBlk = std::make_unique(); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); - auto ret = FindDentryPage(index, ctx); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoUpdate(base, recordId); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FindDentryPageTest002 ERROR"; + GTEST_LOG_(INFO) << "DoUpdateTest001 ERROR"; } - GTEST_LOG_(INFO) << "FindDentryPageTest002 End"; + GTEST_LOG_(INFO) << "DoUpdateTest001 End"; } /** - * @tc.name: FindDentryPageTest003 - * @tc.desc: Verify the FindDentryPage function + * @tc.name: DoRenameOldTest001 + * @tc.desc: Verify the DoRenameOld function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameOldTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "FindDentryPageTest003 Start"; + GTEST_LOG_(INFO) << "DoRenameOldTest001 Start"; try { - uint64_t index; - off_t pos = index * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; - DcacheLookupCtx *ctx = std::make_shared(); - auto dentryBlk = std::make_unique(); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).Times(2).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); - auto ret = FindDentryPage(index, ctx); - EXPECT_EQ(ret, nullptr); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoRenameOld(base, recordId); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FindDentryPageTest003 ERROR"; + GTEST_LOG_(INFO) << "DoRenameOldTest001 ERROR"; } - GTEST_LOG_(INFO) << "FindDentryPageTest003 End"; + GTEST_LOG_(INFO) << "DoRenameOldTest001 End"; } /** - * @tc.name: FindInBlockByIdTest001 - * @tc.desc: Verify the FindInBlockById function + * @tc.name: DoRenameNewTest001 + * @tc.desc: Verify the DoRenameNew function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, FindInBlockByIdTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "FindInBlockByIdTest001 Start"; + GTEST_LOG_(INFO) << "DoRenameNewTest001 Start"; try { - CloudDiskServiceDentryGroup dentryBlk; - uint32_t namehash = 0; - std::string recordId = "test"; - uint8_t revalidate = 0; - auto ret = FindInBlockById(dentryBlk, namehash, recordId, revalidate); - EXPECT_EQ(ret, nullptr); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoRenameNew(base, recordId); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FindInBlockByIdTest001 ERROR"; + GTEST_LOG_(INFO) << "DoRenameNewTest001 ERROR"; } - GTEST_LOG_(INFO) << "FindInBlockByIdTest001 End"; + GTEST_LOG_(INFO) << "DoRenameNewTest001 End"; } /** - * @tc.name: FindInBlockTest001 - * @tc.desc: Verify the FindInBlock function + * @tc.name: DoRenameNewTest002 + * @tc.desc: Verify the DoRenameNew function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, FindInBlockTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "FindInBlockIdTest001 Start"; + GTEST_LOG_(INFO) << "DoRenameNewTest002 Start"; try { - CloudDiskServiceDentryGroup dentryBlk; - uint32_t namehash = 0; - std::string name = "testName"; - uint8_t revalidate = 0; - auto ret = FindInBlock(dentryBlk, namehash, name, revalidate); - EXPECT_EQ(ret, nullptr); + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + string longName(1000, 'a'); + base.name = longName; + string recordId = "record_123"; + auto ret = mFile.DoRenameNew(base, recordId); + EXPECT_EQ(ret, ENAMETOOLONG); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FindInBlockTest001 ERROR"; + GTEST_LOG_(INFO) << "DoRenameNewTest002 ERROR"; } - GTEST_LOG_(INFO) << "FindInBlockTest001 End"; + GTEST_LOG_(INFO) << "DoRenameNewTest002 End"; } /** - * @tc.name: DecodeDentryHeaderTest001 - * @tc.desc: Verify the DecodeDentryHeader function + * @tc.name: DoRenameTest001 + * @tc.desc: Verify the DoRename function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 Start"; + GTEST_LOG_(INFO) << "DoRenameTest001 Start"; try { CloudDiskServiceMetaFile mFile(100, 1, 123); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.DecodeDentryHeader(); - EXPECT_EQ(ret, E_OK); + MetaBase base; + string recordId = "record_123"; + auto newMetaFile = make_shared(100, 2, 456); + auto ret = mFile.DoRename(base, recordId, newMetaFile); + EXPECT_EQ(ret, ENOENT); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 ERROR"; + GTEST_LOG_(INFO) << "DoRenameTest001 ERROR"; } - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 End"; + GTEST_LOG_(INFO) << "DoRenameTest001 End"; } /** - * @tc.name: DecodeDentryHeaderTest002 - * @tc.desc: Verify the DecodeDentryHeader function + * @tc.name: DoRenameTest002 + * @tc.desc: Verify the DoRename function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 Start"; + GTEST_LOG_(INFO) << "DoRenameTest002 Start"; try { CloudDiskServiceMetaFile mFile(100, 1, 123); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.DecodeDentryHeader(); - EXPECT_EQ(ret, 1); + MetaBase base; + string recordId = "record_123"; + auto newMetaFile = make_shared(100, 2, 456); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + auto ret = mFile.DoRename(base, recordId, newMetaFile); + EXPECT_EQ(ret, ENOENT); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 ERROR"; + GTEST_LOG_(INFO) << "DoRenameTest002 ERROR"; } - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 End"; + GTEST_LOG_(INFO) << "DoRenameTest002 End"; } /** - * @tc.name: GenericDentryHeaderTest001 - * @tc.desc: Verify the GenericDentryHeader function + * @tc.name: DoLookupByNameTest001 + * @tc.desc: Verify the DoLookupByName function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByNameTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 Start"; + GTEST_LOG_(INFO) << "DoLookupByNameTest001 Start"; try { CloudDiskServiceMetaFile mFile(100, 1, 123); - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); - auto ret = mFile.GenericDentryHeader(); + mFile.fd_.Reset(-1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoLookupByName(base); EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 ERROR"; + GTEST_LOG_(INFO) << "DoLookupByNameTest001 ERROR"; } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 End"; + GTEST_LOG_(INFO) << "DoLookupByNameTest001 End"; } /** - * @tc.name: GenericDentryHeaderTest002 - * @tc.desc: Verify the GenericDentryHeader function + * @tc.name: DoLookupByRecordIdTest001 + * @tc.desc: Verify the DoLookupByRecordId function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByRecordIdTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 Start"; + GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 Start"; try { CloudDiskServiceMetaFile mFile(100, 1, 123); - struct stat mockStat = { - .st_size = 1, - } - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, ENOENT); + mFile.fd_.Reset(-1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoLookupByRecordId(base); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 ERROR"; + GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 ERROR"; } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 End"; + GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 End"; } /** - * @tc.name: GenericDentryHeaderTest003 - * @tc.desc: Verify the GenericDentryHeader function + * @tc.name: HandleFileByFdTest001 + * @tc.desc: Verify the HandleFileByFd function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, HandleFileByFdTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 Start"; + GTEST_LOG_(INFO) << "HandleFileByFdTest001 Start"; try { CloudDiskServiceMetaFile mFile(100, 1, 123); - struct stat mockStat = { - .st_size = 1, - } - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, 1); + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); + unsigned long endBlock = 10; + uint32_t level = 0; + auto ret = mFile.HandleFileByFd(endBlock, level); + EXPECT_EQ(ret, EINVAL); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 ERROR"; + GTEST_LOG_(INFO) << "HandleFileByFdTest001 ERROR"; } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 End"; + GTEST_LOG_(INFO) << "HandleFileByFdTest001 End"; } /** - * @tc.name: GenericDentryHeaderTest004 - * @tc.desc: Verify the GenericDentryHeader function + * @tc.name: HandleFileByFdTest002 + * @tc.desc: Verify the HandleFileByFd function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest004, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, HandleFileByFdTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 Start"; + GTEST_LOG_(INFO) << "HandleFileByFdTest002 Start"; try { CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.parentDentryFile_ = "parent_file"; - mFile.selfRecordId_ = "record_123"; - mFile.selfHash_ = 0x12345678; struct stat mockStat = { - .st_size = DENTRYGROUP_HEADER, + .st_size = 1, } EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(123)); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, 123); + EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); + unsigned long endBlock = 10; + uint32_t level = 0; + auto ret = mFile.HandleFileByFd(endBlock, level); + EXPECT_EQ(ret, ENOENT); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 ERROR"; + GTEST_LOG_(INFO) << "HandleFileByFdTest002 ERROR"; } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 End"; + GTEST_LOG_(INFO) << "HandleFileByFdTest002 End"; } /** - * @tc.name: GenericDentryHeaderTest005 - * @tc.desc: Verify the GenericDentryHeader function + * @tc.name: HandleFileByFdTest003 + * @tc.desc: Verify the HandleFileByFd function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest005, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, HandleFileByFdTest003, TestSize.Level1) { - GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 Start"; + GTEST_LOG_(INFO) << "HandleFileByFdTest003 Start"; try { CloudDiskServiceMetaFile mFile(100, 1, 123); - string parentDentryFileStr = "parent_file"; - string selfRecordIdStr = "record_123"; - auto selfHashNum = 0x12345678; - mFile.parentDentryFile_ = parentDentryFileStr; - mFile.selfRecordId_ = selfRecordIdStr; - mFile.selfHash_ = selfHashNum; struct stat mockStat = { - .st_size = DENTRYGROUP_HEADER, + .st_size = 1, } - - CloudDiskServiceDcacheHeader header = {}; - copy(parentDentryFileStr.begin(), - min(parentDentryFileStr.end(), parentDentryFileStr.begin() + DENTRYFILE_NAME_LEN), - header.parentDentryfile); - copy(selfRecordIdStr.begin(), - min(selfRecordIdStr.end(), selfRecordIdStr.begin() + RECORD_ID_LEN), - header.selfRecordId); - header.selfHash = selfHashNum; - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)) - .WillOnce(Return(static_cast(sizeof(CloudDiskServiceDcacheHeader)))); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, 123); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 ERROR"; - } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 End"; -} - -/** - * @tc.name: DoCreateTest001 - * @tc.desc: Verify the DoCreate function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoCreateTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - base.name = "test_name"; - base.recordId = "record_123"; - auto ret = mFile.DoCreate(base); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoCreateTest001 ERROR"; - } - GTEST_LOG_(INFO) << "DoCreateTest001 End"; -} - -/** - * @tc.name: DoCreateTest002 - * @tc.desc: Verify the DoCreate function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoCreateTest002 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - string longName(1000,'a'); - MetaBase base; - base.name = "test_name"; - base.recordId = "record_123"; - auto ret = mFile.DoCreate(base); - EXPECT_EQ(ret, EINVAL); + unsigned long endBlock = 10; + uint32_t level = 0; + auto ret = mFile.HandleFileByFd(endBlock, level); + EXPECT_EQ(ret, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoCreateTest002 ERROR"; + GTEST_LOG_(INFO) << "HandleFileByFdTest003 ERROR"; } - GTEST_LOG_(INFO) << "DoCreateTest002 End"; + GTEST_LOG_(INFO) << "HandleFileByFdTest003 End"; } } // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file -- Gitee From 2ec6d105246b7902fccddf4c9b3281d314681ee3 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Tue, 9 Sep 2025 09:41:38 +0800 Subject: [PATCH 08/26] add gn Signed-off-by: yujiahe --- test/unittests/clouddiskservice/BUILD.gn | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index 7ba8c6f3e..a41145fd8 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -53,6 +53,58 @@ ohos_unittest("cloud_disk_service_metafile_test") { use_exceptions = true } +ohos_unittest("cloud_disk_service_syncfolder_test") { + module_out_path = "dfs_service/dfs_service" + + sources = [ + "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager.cpp", + "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp", + "${services_path}/clouddiskservice/sync_folder/src/uuid_helper.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp", + "${services_path}/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp", + "${services_path}/clouddiskservice/syncfolder/src/convertor.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager_impl.cpp", + "sync_folder/cloud_disk_service_syncfolder_test.cpp", + ] + + include_dirs = [ + "${services_path}/clouddiskservice/sync_folder/src", + "${services_path}/clouddiskservice/sync_folder/include", + "${services_path}/clouddiskservice/monitor/include", + "${innerkits_native_path}/clouddiskservice_kit_inner", + "${services_path}/clouddiskservice/ipc/include", + "${services_path}/clouddiskservice/utils/include", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/include", + ] + + deps = [ + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "e2fsprogs:libext2_uuid", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "c_utils:utils", + "ffrt:libffrt", + "json:nlohmann_json_static", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + public_configs = [ + "${services_path}/clouddiskservice:cloud_disk_service_public_config", + ] + + use_exceptions = true +} + ohos_unittest("convertor_test") { module_out_path = "dfs_service/dfs_service" @@ -90,6 +142,7 @@ group("clouddisk_service_test") { testonly = true deps = [ ":cloud_disk_service_metafile_test", + ":cloud_disk_service_syncfolder_test", ":convertor_test", ] } -- Gitee From fce7cc38911d51ab58017529ca6f3c3c2a04ade2 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Tue, 9 Sep 2025 15:41:07 +0800 Subject: [PATCH 09/26] fix cases Signed-off-by: yujiahe --- test/unittests/clouddiskservice/BUILD.gn | 4 +- .../clouddiskservice/mock/assistant.h | 11 ++-- .../mock/system_function_mock.cpp | 10 ---- ...cloud_disk_service_logfile_static_test.cpp | 59 ++++--------------- .../cloud_disk_service_logfile_test.cpp | 53 +++++++++++++++++ 5 files changed, 70 insertions(+), 67 deletions(-) create mode 100644 test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index a41145fd8..c879e9d22 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -57,13 +57,13 @@ ohos_unittest("cloud_disk_service_syncfolder_test") { module_out_path = "dfs_service/dfs_service" sources = [ - "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp", + "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp", "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager.cpp", "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp", "${services_path}/clouddiskservice/sync_folder/src/uuid_helper.cpp", "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp", "${services_path}/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp", - "${services_path}/clouddiskservice/syncfolder/src/convertor.cpp", + "${services_path}/clouddiskservice/sync_folder/src/convertor.cpp", "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_service_manager_impl.cpp", "sync_folder/cloud_disk_service_syncfolder_test.cpp", ] diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h index d0183674e..e8ffeda25 100644 --- a/test/unittests/clouddiskservice/mock/assistant.h +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -18,6 +18,11 @@ #include namespace OHOS::FileManagement::CloudDiskService { + +#ifndef errno_t +typedef int errno_t; +#endif + class Assistant { public: static inline std::shared_ptr ins = nullptr; @@ -31,9 +36,6 @@ public: virtual int64_t ReadFile(int fd, off_t offset, size_t size, void *data) = 0; virtual int64_t WriteFile(int fd, const void *data, off_t offset, size_t size) = 0; virtual int FilePosLock(int fd, off_t offset, size_t size, int type) = 0; - - virtual int fcntl(int fd, int op, ...) = 0; - virtual errno_t memcpy_s(void *dest, size_t destMax, int c, size_t count) = 0; }; class AssistantMock : public Assistant { @@ -46,9 +48,6 @@ public: MOCK_METHOD4(ReadFile, int64_t(int fd, off_t offset, size_t size, void *data)); MOCK_METHOD4(WriteFile, int64_t(int fd, const void *data, off_t offset, size_t size)); MOCK_METHOD4(FilePosLock, int(int fd, off_t offset, size_t size, int type)); - - MOCK_METHOD2(fcntl, int(int, int)); - MOCK_METHOD4(memcpy_s, errno_t(void *, size_t, int, size_t)); }; } diff --git a/test/unittests/clouddiskservice/mock/system_function_mock.cpp b/test/unittests/clouddiskservice/mock/system_function_mock.cpp index 5adc94a7d..130d495e3 100644 --- a/test/unittests/clouddiskservice/mock/system_function_mock.cpp +++ b/test/unittests/clouddiskservice/mock/system_function_mock.cpp @@ -32,16 +32,6 @@ int ftruncate(int fd, off_t length) return Assistant::ins->ftruncate(fd, length); } -int fcntl(int fd, int op, ...) -{ - return Assistant::ins->fcntl(fd, op); -} - -errno_t memcpy_s(void *dest, size_t destMax, int c, size_t count) -{ - return Assistant::ins->memcpy_s(dest, destMax, c, count); -} - namespace OHOS::FileManagement { int64_t FileUtils::ReadFile(int fd, off_t offset, size_t size, void *data) { diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp index 12852c8cc..f34166f12 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp @@ -17,6 +17,8 @@ #include #include +#include "assistant.h" + namespace OHOS::FileManagement::CloudDiskService::Test { using namespace testing; using namespace testing::ext; @@ -129,7 +131,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest001, TestSize.Lev try { int fd = 1; uint32_t bucket = 1; - EXPECT_CALL(*insMock_, fcntl(_, _)).WillOnce(Return(-1)) + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); auto res = LoadCurrentPage(fd, bucket); EXPECT_EQ(res, nullptr); } catch (...) { @@ -151,7 +153,8 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest002, TestSize.Lev try { int fd = 1; uint32_t bucket = 1; - EXPECT_CALL(*insMock_, fcntl(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)) + .WillOnce(Return(1)); EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(1)); auto res = LoadCurrentPage(fd, bucket); EXPECT_EQ(res, nullptr); @@ -174,7 +177,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest003, TestSize.Lev try { int fd = 1; uint32_t bucket = 1; - EXPECT_CALL(*insMock_, fcntl(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(4096)); auto res = LoadCurrentPage(fd, bucket); EXPECT_NE(res, nullptr); @@ -223,6 +226,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest002, TestSize.Lev uint32_t line = 1; EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); auto res = SyncCurrentPage(logGroup, fd, line); EXPECT_EQ(res, EINVAL); } catch (...) { @@ -247,7 +251,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest003, TestSize.Lev uint32_t line = 1; EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(4096)); - EXPECT_CALL(*insMock_, fcntl(_, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); auto res = SyncCurrentPage(logGroup, fd, line); EXPECT_NE(res, E_OK); } catch (...) { @@ -272,7 +276,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest004, TestSize.Lev uint32_t line = 1; EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(4096)); - EXPECT_CALL(*insMock_, fcntl(_, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); auto res = SyncCurrentPage(logGroup, fd, line); EXPECT_EQ(res, E_OK); } catch (...) { @@ -296,55 +300,12 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, FillLogGroupTest001, TestSize.Level1 LogBlock logBlock; uint32_t offset = 0; logGroup.nsl[0].timestamp = 1; - EXPECT_CALL(*insMock_, memcpy(_, _, _, _)).WillOnce(Return(1)); auto res = FillLogGroup(logGroup, logBlock, offset); - EXPECT_EQ(res, -1); + EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "FillLogGroupTest001 failed"; } GTEST_LOG_(INFO) << "FillLogGroupTest001 end"; } - -/** - * @tc.name: FillLogGroupTest002 - * @tc.desc: Verify the FillLogGroup function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileStaticTest, FillLogGroupTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FillLogGroupTest002 start"; - try { - LogGroup logGroup; - LogBlock logBlock; - uint32_t offset = 0; - EXPECT_CALL(*insMock_, memcpy(_, _, _, _)).WillOnce(Return(0)); - auto res = FillLogGroup(logGroup, logBlock, offset); - EXPECT_EQ(res, E_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FillLogGroupTest002 failed"; - } - GTEST_LOG_(INFO) << "FillLogGroupTest002 end"; -} - -/** - * @tc.name: GetCurrentLineTest001 - * @tc.desc: Verify the GetCurrentLine function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileStaticTest, GetCurrentLineTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "GetCurrentLineTest001 start"; - try { - int fd = 1; - auto res = GetCurrentLine(fd); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetCurrentLineTest001 failed"; - } - GTEST_LOG_(INFO) << "GetCurrentLineTest001 end"; -} } \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp new file mode 100644 index 000000000..b35d22bdb --- /dev/null +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -0,0 +1,53 @@ +/* + * 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 "cloud_disk_service_logfile.h" + +#include +#include + +#include "assistant.h" + +namespace OHOS::FileManagement::CloudDiskService::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CloudDiskServiceLogFileTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +} + +void CloudDiskServiceLogFileTest::SetUpTestCase(void) { + GTEST_LOG_(INFO) << "SetUpTestCase"; +} + +void CloudDiskServiceLogFileTest::TearDownTestCase(void) { + GTEST_LOG_(INFO) << "TearDownTestCase"; +} + +void CloudDiskServiceLogFileTest::SetUp() { + +} + +void CloudDiskServiceLogFileTest::TearDown() { + +} + + +} \ No newline at end of file -- Gitee From eb43c12c55229d004bfd87cae4d4bda917742e82 Mon Sep 17 00:00:00 2001 From: ADHDer <19857096219@163.com> Date: Tue, 9 Sep 2025 20:10:04 +0800 Subject: [PATCH 10/26] metafile_TDD Signed-off-by: ADHDer <19857096219@163.com> --- test/unittests/BUILD.gn | 1 + test/unittests/clouddiskservice/BUILD.gn | 4 +- .../cloud_disk_service_metafile_test.cpp | 267 ++++++++++++++++++ .../sync_folder/convertor_test.cpp | 20 +- 4 files changed, 280 insertions(+), 12 deletions(-) diff --git a/test/unittests/BUILD.gn b/test/unittests/BUILD.gn index 9647db0b4..7b3499eec 100644 --- a/test/unittests/BUILD.gn +++ b/test/unittests/BUILD.gn @@ -22,6 +22,7 @@ group("cloudsyncunittests") { "cloud_disk:cloud_disk_test", "cloud_file_kit_inner:cloud_file_kit_inner_test", "clouddisk_database:clouddisk_database_test", + "clouddiskservice:clouddisk_service_test", "cloudsync_api:cloudsync_api_test", "cloudsync_sa:cloudsync_sa_test", "services_daemon:services_daemon_test", diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index 5fe16651f..21023b01e 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -21,7 +21,7 @@ ohos_unittest("cloud_disk_service_metafile_test") { "${distributedfile_path}/test/unittests/clouddiskservice/mock/system_function_mock.cpp", "${distributedfile_path}/utils/log/src/utils_log.cpp", "${services_path}/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp", - "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + "${services_path}/clouddiskservice/sync_folder/src/convertor.cpp", "sync_folder/cloud_disk_service_metafile_test.cpp", ] @@ -58,7 +58,7 @@ ohos_unittest("convertor_test") { sources = [ "${distributedfile_path}/utils/log/src/utils_log.cpp", - "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + "${services_path}/clouddiskservice/sync_folder/src/convertor.cpp", "sync_folder/convertor_test.cpp", ] diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp index 333503605..5afaacac4 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -13,9 +13,11 @@ * limitations under the License. */ +#include #include #include "assistant.h" +#include "convertor.h" #include "dfs_error.h" #include "cloud_disk_service_metafile.h" @@ -295,6 +297,98 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest002, TestSize.Level1) GTEST_LOG_(INFO) << "DoCreateTest002 End"; } +/** + * @tc.name: DoCreateTest003 + * @tc.desc: Verify the DoCreate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoCreateTest003 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + base.name = "test_name"; + base.recordId = "record_123"; + base.atime = 0; + base.mtime = 0; + base.size = 1024; + base.mode = 0644; + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); + auto ret = mFile.DoCreate(base); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoCreateTest003 ERROR"; + } + GTEST_LOG_(INFO) << "DoCreateTest003 End"; +} + +/** + * @tc.name: DoCreateTest004 + * @tc.desc: Verify the DoCreate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoCreateTest004 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + base.name = "test_name"; + base.recordId = "record_123"; + base.atime = 0; + base.mtime = 0; + base.size = 1024; + base.mode = 0644; + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + auto ret = mFile.DoCreate(base); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoCreateTest004 ERROR"; + } + GTEST_LOG_(INFO) << "DoCreateTest004 End"; +} + +/** + * @tc.name: DoCreateTest005 + * @tc.desc: Verify the DoCreate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoCreateTest005 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + base.name = "test_name"; + base.recordId = "record_123"; + base.atime = 0; + base.mtime = 0; + base.size = 1024; + base.mode = 0644; + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.DoCreate(base); + EXPECT_EQ(ret, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoCreateTest005 ERROR"; + } + GTEST_LOG_(INFO) << "DoCreateTest005 End"; +} + /** * @tc.name: DoRemoveTest001 * @tc.desc: Verify the DoRemove function @@ -529,6 +623,36 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByRecordIdTest001, TestSize.Level GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 End"; } +/** + * @tc.name: GetCreateInfoTest001 + * @tc.desc: Verify the GetCreateInfo function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCreateInfoTest001 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + base.name = "test_name"; + uint32_t bitPos = 0; + uint32_t nameHash = 0; + unsigned long bidx = 0; + CloudDiskServiceDentryGroup dentryBlk; + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); + auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCreateInfoTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetCreateInfoTest001 End"; +} + /** * @tc.name: HandleFileByFdTest001 * @tc.desc: Verify the HandleFileByFd function @@ -605,4 +729,147 @@ HWTEST_F(CloudDiskServiceMetafileTest, HandleFileByFdTest003, TestSize.Level1) } GTEST_LOG_(INFO) << "HandleFileByFdTest003 End"; } + +/** + * @tc.name: GetMetaFileMgrInstanceTest001 + * @tc.desc: Verify the GetMetaFileMgrInstance function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetMetaFileMgrInstanceTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetMetaFileMgrInstanceTest001 Start"; + try { + MetaFileMgr& instance1 = MetaFileMgr::GetInstance(); + MetaFileMgr& instance2 = MetaFileMgr::GetInstance(); + EXPECT_EQ(instance1, instance2); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetMetaFileMgrInstanceTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetMetaFileMgrInstanceTest001 End"; +} + +/** + * @tc.name: GetCloudDiskServiceMetaFileTest001 + * @tc.desc: Verify the GetCloudDiskServiceMetaFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetCloudDiskServiceMetaFileTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest001 Start"; + try { + MetaFileMgr& mgr = MetaFileMgr::GetInstance(); + mgr.metaFiles_.clear(); + mgr.metaFileList_.clear(); + uint32_t userId = 1001; + uint32_t syncFolderIndex = 0; + uint64_t inode = 123456789; + auto metaFile = mgr.GetCloudDiskServiceMetaFile(userId, syncFolderIndex, inode); + EXPECT_TRUE(metafile != nullptr); + EXPECT_EQ(mgr.metaFiles_.size(), 1); + EXPECT_EQ(mgr.metaFileList_.size(), 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest001 End"; +} + +/** + * @tc.name: GetCloudDiskServiceMetaFileTest002 + * @tc.desc: Verify the GetCloudDiskServiceMetaFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetCloudDiskServiceMetaFileTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest002 Start"; + try { + MetaFileMgr& mgr = MetaFileMgr::GetInstance(); + mgr.metaFiles_.clear(); + mgr.metaFileList_.clear(); + uint32_t userId = 1001; + uint32_t syncFolderIndex = 0; + uint64_t inode = 123456789; + constexpr uint32_t MAX_META_FILE_NUM = 150; + for (int i = 0; i < MAX_META_FILE_NUM; i++) { + uint32_t tempUserId = 1000 + i; + uint32_t tempSyncFolderIndex = i; + uint64_t tempInode = 1000000000 + i; + shared_ptr mFile = + make_shared(tempUserId, tempSyncFolderIndex, tempInode); + MetaFileKey key(tempUserId, + Convertor::ConvertToHex(tempSyncFolderIndex) + Convertor::ConvertToHex(tempInode)); + mgr.metaFileList_.emplace_back(key, mFile); + auto it = prev(mgr.metaFileList_.end()); + mgr.metaFiles_[key] = it; + } + auto metaFile = mgr.GetCloudDiskServiceMetaFile(userId, syncFolderIndex, inode); + EXPECT_TRUE(metafile != nullptr); + EXPECT_EQ(mgr.metaFiles_.size(), MAX_META_FILE_NUM); + EXPECT_EQ(mgr.metaFileList_.size(), MAX_META_FILE_NUM); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest002 End"; +} + +/** + * @tc.name: GetRelativePathTest001 + * @tc.desc: Verify the GetRelativePath function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetRelativePathTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetRelativePathTest001 Start"; + try { + MetaFileMgr& mgr = MetaFileMgr::GetInstance(); + mgr.metaFiles_.clear(); + mgr.metaFileList_.clear(); + uint32_t userId = 1001; + uint32_t syncFolderIndex = 0; + uint64_t inode = 123456789; + auto metaFile = make_shared(userId, syncFolderIndex, inode); + metaFile->parentDentryFile_ = ROOT_PARENTDENTRYFILE; + string path = "/test"; + auto ret = mgr.GetRelativePath(metaFile, path); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetRelativePathTest001 ERROR"; + } + GTEST_LOG_(INFO) << "GetRelativePathTest001 End"; +} + +/** + * @tc.name: GetRelativePathTest002 + * @tc.desc: Verify the GetRelativePath function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetRelativePathTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetRelativePathTest002 Start"; + try { + MetaFileMgr& mgr = MetaFileMgr::GetInstance(); + mgr.metaFiles_.clear(); + mgr.metaFileList_.clear(); + uint32_t userId = 1001; + uint32_t syncFolderIndex = 0; + uint64_t inode = 123456789; + auto metaFile = make_shared(userId, syncFolderIndex, inode); + metaFile->parentDentryFile_ = "123"; + string path = "/test"; + auto ret = mgr.GetRelativePath(metaFile, path); + EXPECT_EQ(ret, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetRelativePathTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GetRelativePathTest002 End"; +} } // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/convertor_test.cpp b/test/unittests/clouddiskservice/sync_folder/convertor_test.cpp index 8a7487bc8..bd3140e76 100644 --- a/test/unittests/clouddiskservice/sync_folder/convertor_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/convertor_test.cpp @@ -16,7 +16,7 @@ #include #include -#include "converter.h" +#include "convertor.h" #include "utils_log.h" namespace OHOS { @@ -63,9 +63,9 @@ HWTEST_F(ConvertorTest, ConvertToHexTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "ConvertToHexTest001 start"; try { - EXPECT_EQ(Converter::ConvertToHex(0), "0000000000000000"); - EXPECT_EQ(Converter::ConvertToHex(1), "0000000000000001"); - EXPECT_EQ(Converter::ConvertToHex(255), "00000000000000ff"); + EXPECT_EQ(Convertor::ConvertToHex(0), "0000000000000000"); + EXPECT_EQ(Convertor::ConvertToHex(1), "0000000000000001"); + EXPECT_EQ(Convertor::ConvertToHex(255), "00000000000000ff"); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ConvertToHexTest001 failed"; @@ -83,12 +83,12 @@ HWTEST_F(ConvertorTest, ConvertFromHexTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "ConvertFromHexTest001 start"; try { - EXPECT_EQ(Converter::ConvertFromHex("0"), 0ULL); - EXPECT_EQ(Converter::ConvertFromHex("a"), 10ULL); - EXPECT_EQ(Converter::ConvertFromHex("F"), 15ULL); - EXPECT_EQ(Converter::ConvertFromHex("G"), 0ULL); - EXPECT_EQ(Converter::ConvertFromHex(""), 0ULL); - EXPECT_EQ(Converter::ConvertFromHex("123456789ABCDEF0"), 0x123456789ABCDEF0ULL); + EXPECT_EQ(Convertor::ConvertFromHex("0"), 0ULL); + EXPECT_EQ(Convertor::ConvertFromHex("a"), 10ULL); + EXPECT_EQ(Convertor::ConvertFromHex("F"), 15ULL); + EXPECT_EQ(Convertor::ConvertFromHex("G"), 0ULL); + EXPECT_EQ(Convertor::ConvertFromHex(""), 0ULL); + EXPECT_EQ(Convertor::ConvertFromHex("123456789ABCDEF0"), 0x123456789ABCDEF0ULL); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ConvertFromHexTest001 failed"; -- Gitee From 49c67421ea8ba04193d4d987c3b08842e1712887 Mon Sep 17 00:00:00 2001 From: ADHDer <19857096219@163.com> Date: Tue, 9 Sep 2025 20:30:35 +0800 Subject: [PATCH 11/26] metafile Signed-off-by: ADHDer <19857096219@163.com> --- .../cloud_disk_service_metafile_test.cpp | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp index 5afaacac4..5cd8b4860 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -641,6 +641,7 @@ HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest001, TestSize.Level1) uint32_t nameHash = 0; unsigned long bidx = 0; CloudDiskServiceDentryGroup dentryBlk; + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(0)); EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); @@ -653,6 +654,93 @@ HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest001, TestSize.Level1) GTEST_LOG_(INFO) << "GetCreateInfoTest001 End"; } +/** + * @tc.name: GetCreateInfoTest002 + * @tc.desc: Verify the GetCreateInfo function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCreateInfoTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + base.name = "test_name"; + uint32_t bitPos = 0; + uint32_t nameHash = 0; + unsigned long bidx = 0; + CloudDiskServiceDentryGroup dentryBlk; + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); + auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); + EXPECT_EQ(ret, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCreateInfoTest002 ERROR"; + } + GTEST_LOG_(INFO) << "GetCreateInfoTest002 End"; +} + +/** + * @tc.name: GetCreateInfoTest003 + * @tc.desc: Verify the GetCreateInfo function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCreateInfoTest003 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + base.name = "test_name"; + uint32_t bitPos = 0; + uint32_t nameHash = 0; + unsigned long bidx = 0; + CloudDiskServiceDentryGroup dentryBlk; + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(-1)); + auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); + EXPECT_EQ(ret, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCreateInfoTest003 ERROR"; + } + GTEST_LOG_(INFO) << "GetCreateInfoTest003 End"; +} + +/** + * @tc.name: GetCreateInfoTest004 + * @tc.desc: Verify the GetCreateInfo function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCreateInfoTest003 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(-1); + MetaBase base; + base.name = "test_name"; + uint32_t bitPos = 0; + uint32_t nameHash = 0; + unsigned long bidx = 0; + CloudDiskServiceDentryGroup dentryBlk; + EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(0)); + auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCreateInfoTest004 ERROR"; + } + GTEST_LOG_(INFO) << "GetCreateInfoTest004 End"; +} + /** * @tc.name: HandleFileByFdTest001 * @tc.desc: Verify the HandleFileByFd function -- Gitee From a32bf07d06abb916a466f79e78f2e1944b9c6b7e Mon Sep 17 00:00:00 2001 From: yujiahe Date: Tue, 9 Sep 2025 22:28:27 +0800 Subject: [PATCH 12/26] add metafile_test Signed-off-by: yujiahe --- .../clouddiskservice/mock/assistant.h | 15 +- .../mock/cloud_disk_service_metafile_mock.cpp | 46 + .../mock/system_function_mock.cpp | 20 + ...cloud_disk_service_logfile_static_test.cpp | 7 +- .../cloud_disk_service_logfile_test.cpp | 922 ++++++++++++++++++ 5 files changed, 1003 insertions(+), 7 deletions(-) create mode 100644 test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h index e8ffeda25..8122206b3 100644 --- a/test/unittests/clouddiskservice/mock/assistant.h +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -15,14 +15,11 @@ #ifndef TEST_UNITTEST_CLOUD_DISK_SERVICE_ASSISTANT_H #define TEST_UNITTEST_CLOUD_DISK_SERVICE_ASSISTANT_H +#include #include namespace OHOS::FileManagement::CloudDiskService { -#ifndef errno_t -typedef int errno_t; -#endif - class Assistant { public: static inline std::shared_ptr ins = nullptr; @@ -36,6 +33,11 @@ public: virtual int64_t ReadFile(int fd, off_t offset, size_t size, void *data) = 0; virtual int64_t WriteFile(int fd, const void *data, off_t offset, size_t size) = 0; virtual int FilePosLock(int fd, off_t offset, size_t size, int type) = 0; + + virtual int access(const char *name, int type) = 0; + virtual DIR* opendir(const char* path) = 0; + virtual struct dirent* readdir(DIR* d) = 0; + virtual int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) = 0; }; class AssistantMock : public Assistant { @@ -48,6 +50,11 @@ public: MOCK_METHOD4(ReadFile, int64_t(int fd, off_t offset, size_t size, void *data)); MOCK_METHOD4(WriteFile, int64_t(int fd, const void *data, off_t offset, size_t size)); MOCK_METHOD4(FilePosLock, int(int fd, off_t offset, size_t size, int type)); + + MOCK_METHOD2(access, int(const char *, int)); + MOCK_METHOD1(opendir, DIR*(const char*)); + MOCK_METHOD1(readdir, struct dirent*(DIR*)); + MOCK_METHOD2(UnRegisterSyncFolder, int32_t(const int32_t, const uint64_t)) }; } diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp new file mode 100644 index 000000000..809229975 --- /dev/null +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp @@ -0,0 +1,46 @@ +/* + * 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 "cloud_disk_service_metafile.h" + +namespace OHOS::FileManagement::CloudDiskService { +using namespace OHOS::FileManagement; + +int32_t CloudDiskServiceMetaFile::DoRemove(const MetaBase &base, std::string &recordId) +{ + if (recordId == "") { + return E_OK; + } else { + return EINVAL; + } +} + +int32_t CloudDiskServiceMetaFile::DoRenameOld(const MetaBase &base, std::string &recordId) +{ + if (recordId == "") { + return E_OK; + } else { + return EINVAL; + } +} + +int32_t CloudDiskServiceMetaFile::DoRenameNew(const MetaBase &base, std::string &recordId) +{ + if (recordId == "") { + return E_OK; + } else { + return EINVAL; + } +} +} \ No newline at end of file diff --git a/test/unittests/clouddiskservice/mock/system_function_mock.cpp b/test/unittests/clouddiskservice/mock/system_function_mock.cpp index 130d495e3..76f0eb545 100644 --- a/test/unittests/clouddiskservice/mock/system_function_mock.cpp +++ b/test/unittests/clouddiskservice/mock/system_function_mock.cpp @@ -32,6 +32,26 @@ int ftruncate(int fd, off_t length) return Assistant::ins->ftruncate(fd, length); } +int access(const char *name, int type) +{ + return Assistant::ins->access(name, type); +} + +DIR* opendir(const char* path) +{ + return Assistant::ins->opendir(path); +} + +struct dirent* readdir(DIR* d) +{ + return Assistant::ins->readdir(d); +} + +int32_t LogFileMgr::UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) +{ + return Assistant::ins->UnRegisterSyncFolder(userId, syncFolderIndex); +} + namespace OHOS::FileManagement { int64_t FileUtils::ReadFile(int fd, off_t offset, size_t size, void *data) { diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp index f34166f12..50ce2e173 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp @@ -66,9 +66,10 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, GetLogFileByPathTest001, TestSize.Le uint32_t userId = 1; uint32_t syncFolderIndex = 1; string res = GetLogFileByPath(userId, syncFolderIndex); - EXPECT_EQ(res, - "/data/service/el2/1/hmdfs/cache/account_cache/dentry_cache" + - "/clouddisk_service_cache/0000000000000001/history.log"); + string str = + "/data/service/el2/1/hmdfs/cache/account_cache/dentry_cache/" + "clouddisk_service_cache/0000000000000001/history.log"; + EXPECT_EQ(res, str); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GetLogFileByPathTest001 failed"; diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp index b35d22bdb..78cc65c55 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -31,14 +31,25 @@ public: static void TearDownTestCase(void); void SetUp(); void TearDown(); + static inline shared_ptr logFile_; + static inline shared_ptr insMock_; + static inline shared_ptr managerMock_; } void CloudDiskServiceLogFileTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + logFile_ = make_shared(0, 0); + insMock_ = make_shared(); + managerMock_ = make_shared(); + Assistant::ins = insMock_; } void CloudDiskServiceLogFileTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; + logFile_ = nullptr; + Assistant::ins = nullptr; + insMock_ = nullptr; + managerMock_ = nullptr; } void CloudDiskServiceLogFileTest::SetUp() { @@ -49,5 +60,916 @@ void CloudDiskServiceLogFileTest::TearDown() { } +/** + * @tc.name: WriteLogFileTest001 + * @tc.desc: Verify the WriteLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, WriteLogFileTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WriteLogFileTest001 start"; + try { + LogBlock logBlock; + int32_t res = logFile_->WriteLogFile(logBlock); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "WriteLogFileTest001 failed"; + } + GTEST_LOG_(INFO) << "WriteLogFileTest001 end"; +} + +/** + * @tc.name: ReadLogFileTest001 + * @tc.desc: Verify the ReadLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ReadLogFileTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadLogFileTest001 start"; + try { + uint64_t line = 1; + LogBlock logBlock; + int32_t res = logFile_->ReadLogFile(logBlock); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadLogFileTest001 failed"; + } + GTEST_LOG_(INFO) << "ReadLogFileTest001 end"; +} + +/** + * @tc.name: OnDataChangeTest001 + * @tc.desc: Verify the OnDataChange function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, OnDataChangeTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnDataChangeTest001 start"; + try { + logFile_->changeDatas_.clear(); + int32_t res = logFile_->OnDataChange(); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnDataChangeTest001 failed"; + } + GTEST_LOG_(INFO) << "OnDataChangeTest001 end"; +} + +/** + * @tc.name: OnDataChangeTest002 + * @tc.desc: Verify the OnDataChange function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, OnDataChangeTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnDataChangeTest002 start"; + try { + ChangeData changeData; + logFile_->changeDatas_.push_back(changeData); + int32_t res = logFile_->OnDataChange(); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnDataChangeTest002 failed"; + } + GTEST_LOG_(INFO) << "OnDataChangeTest002 end"; +} + +/** + * @tc.name: CloudDiskServiceLogFileTest001 + * @tc.desc: Verify the CloudDiskServiceLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, CloudDiskServiceLogFileTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + EXPECT_CALL(*insMock_, access(_, _)).WillOnce(Return(0)); + shared_ptr logFile = + make_shared(userId, syncsyncFolderIndex); + EXPECT_EQ(logFile.userId_, userId); + EXPECT_EQ(logFile.syncFolderIndex_, syncFolderIndex); + EXPECT_NE(logFile.needCallback_); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest001 failed"; + } + GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest001 end"; +} + +/** + * @tc.name: CloudDiskServiceLogFileTest002 + * @tc.desc: Verify the CloudDiskServiceLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, CloudDiskServiceLogFileTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest001 start"; + try { + uint32_t userId = 3; + uint32_t syncFolderIndex = 4; + EXPECT_CALL(*insMock_, access(_, _)).WillOnce(Return(1)); + shared_ptr logFile = + make_shared(userId, syncsyncFolderIndex); + EXPECT_EQ(logFile.userId_, userId); + EXPECT_EQ(logFile.syncFolderIndex_, syncFolderIndex); + EXPECT_NE(logFile.needCallback_); + EXPECT_EQ(logFile.currentLine_, 0); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest002 failed"; + } + GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest002 end"; +} + +/** + * @tc.name: GenerateLogBlockTest001 + * @tc.desc: Verify the GenerateLogBlock function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, GenerateLogBlockTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenerateLogBlockTest001 start"; + try { + EventInfo eventInfo; + uint64_t parentInode = 1; + string childRecordId = "childRecordId"; + string parentRecordId = "parentRecordId"; + uint64_t line = 1; + int32_t res = + logFile_->GenerateLogBlock(eventInfo, parentInode, childRecordId, parentRecordId, line); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenerateLogBlockTest001 failed"; + } + GTEST_LOG_(INFO) << "GenerateLogBlockTest001 end"; +} + +/** + * @tc.name: GenerateChangeDataTest001 + * @tc.desc: Verify the GenerateChangeData function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenerateChangeDataTest001 start"; + try { + EventInfo eventInfo; + uint64_t line = 1; + string childRecordId = "childRecordId"; + string parentRecordId = "parentRecordId"; + logFile_->changeDatas_.clear(); + int32_t res = + logFile_->GenerateChangeData(eventInfo, line, childRecordId, parentRecordId); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenerateChangeDataTest001 failed"; + } + GTEST_LOG_(INFO) << "GenerateChangeDataTest001 end"; +} + +/** + * @tc.name: GenerateChangeDataTest001 + * @tc.desc: Verify the GenerateChangeData function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenerateChangeDataTest001 start"; + try { + EventInfo eventInfo; + uint64_t line = 1; + string childRecordId = "childRecordId"; + string parentRecordId = "parentRecordId"; + logFile_->changeDatas_.clear(); + int32_t res = + logFile_->GenerateChangeData(eventInfo, line, childRecordId, parentRecordId); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenerateChangeDataTest001 failed"; + } + GTEST_LOG_(INFO) << "GenerateChangeDataTest001 end"; +} + +/** + * @tc.name: GenerateChangeDataTest002 + * @tc.desc: Verify the GenerateChangeData function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GenerateChangeDataTest002 start"; + try { + EventInfo eventInfo; + uint64_t line = 1; + string childRecordId = "childRecordId"; + string parentRecordId = "parentRecordId"; + logFile_->changeDatas_.clear(); + for (int i = 0; i < MAX_CHANGEDATAS_SIZE; i++) { + ChangeData changeData; + logFile_->changeDatas_.push_back(changeData); + } + int32_t res = + logFile_->GenerateChangeData(eventInfo, line, childRecordId, parentRecordId); + EXPECT_EQ(res, E_OK); + EXPECT_TRUE(logFile_->changeData_.empty()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GenerateChangeDataTest002 failed"; + } + GTEST_LOG_(INFO) << "GenerateChangeDataTest002 end"; +} + +#undef S_ISDIR +#define S_ISDIR(mode) (0) + +/** + * @tc.name: FillChildForDirTest001 + * @tc.desc: Verify the FillChildForDir function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FillChildForDirTest001 start"; + try { + string path = "path"; + uint64_t timestamp = 1; + int32_t res = + logFile_->FillChildForDir(path, timestamp); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FillChildForDirTest001 failed"; + } + GTEST_LOG_(INFO) << "FillChildForDirTest001 end"; +} + +#undef S_ISDIR +#define S_ISDIR(mode) (1) + +/** + * @tc.name: FillChildForDirTest002 + * @tc.desc: Verify the FillChildForDir function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FillChildForDirTest002 start"; + try { + string path = "path"; + uint64_t timestamp = 1; + EXPECT_CALL(*insMock_, opendir(_)).WillOnce([&]() { + errno = ENOENT; + return nullptr; + }); + int32_t res = + logFile_->FillChildForDir(path, timestamp); + EXPECT_EQ(res, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FillChildForDirTest002 failed"; + } + GTEST_LOG_(INFO) << "FillChildForDirTest002 end"; +} + +/** + * @tc.name: FillChildForDirTest003 + * @tc.desc: Verify the FillChildForDir function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FillChildForDirTest003 start"; + try { + string path = "path"; + uint64_t timestamp = 1; + DIR* dir = reinterpret_cast(UINT64_MAX); + EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); + EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(nullptr)); + int32_t res = + logFile_->FillChildForDir(path, timestamp); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FillChildForDirTest003 failed"; + } + GTEST_LOG_(INFO) << "FillChildForDirTest003 end"; +} + +/** + * @tc.name: FillChildForDirTest004 + * @tc.desc: Verify the FillChildForDir function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FillChildForDirTest004 start"; + try { + string path = "path"; + uint64_t timestamp = 1; + DIR* dir = reinterpret_cast(UINT64_MAX); + EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); + dirent *entry1; + entry1->d_name = "."; + dirent *entry2; + entry2->d_name = ".."; + dirent *entry; + entry->d_name = "name"; + EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(entry1) + .WillOnce(Return(entry2)) + .WillOnce(Return(entry))); + int32_t res = + logFile_->FillChildForDir(path, timestamp); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FillChildForDirTest004 failed"; + } + GTEST_LOG_(INFO) << "FillChildForDirTest004 end"; +} + +/** + * @tc.name: StartCallbackTest001 + * @tc.desc: Verify the StartCallback function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, StartCallbackTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StartCallbackTest001 start"; + try { + logFile_->StartCallback(); + EXPECT_TRUE(logFile_->needCallback_); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "StartCallbackTest001 failed"; + } + GTEST_LOG_(INFO) << "StartCallbackTest001 end"; +} + +/** + * @tc.name: StopCallbackTest001 + * @tc.desc: Verify the StopCallback function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, StopCallbackTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "StopCallbackTest001 start"; + try { + logFile_->StopCallback(); + EXPECT_NE(logFile_->needCallback_); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "StopCallbackTest001 failed"; + } + GTEST_LOG_(INFO) << "StopCallbackTest001 end"; +} + +/** + * @tc.name: ProduceLogTest001 + * @tc.desc: Verify the ProduceLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogTest001 start"; + try { + EventInfo eventInfo; + eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); + EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(1)); + auto res = logFile_->ProduceLog(eventInfo); + EXPECT_EQ(res, 1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogTest001 end"; +} + +/** + * @tc.name: ProduceLogTest002 + * @tc.desc: Verify the ProduceLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogTest002 start"; + try { + EventInfo eventInfo; + eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); + EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(0)); + auto res = logFile_->ProduceLog(eventInfo); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogTest002 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogTest002 end"; +} + +/** + * @tc.name: ProduceLogTest003 + * @tc.desc: Verify the ProduceLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogTest003 start"; + try { + EventInfo eventInfo; + eventInfo.operateType = static_cast(OperationType::CREATE); + auto res = logFile_->ProduceLog(eventInfo); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogTest003 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogTest003 end"; +} + + + + + + + + + +/** + * @tc.name: PraseLogTest001 + * @tc.desc: Verify the PraseLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "PraseLogTest001 start"; + try { + logFile_.currentLine_ = 0; + uint64_t line = 1; + ChangeData changeData; + auto res = logFile_->PraseLog(line, changeData); + EXPECT_EQ(res, E_OUT_OF_RANGE); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "PraseLogTest001 failed"; + } + GTEST_LOG_(INFO) << "PraseLogTest001 end"; +} + + + + + + + + +/** + * @tc.name: ProduceUnlinkLogTest001 + * @tc.desc: Verify the ProduceUnlinkLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceUnlinkLogTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceUnlinkLogTest001 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string name = "name"; + string childRecordId = "childRecordId"; + auto res = logFile_->ProduceUnlinkLog(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceUnlinkLogTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceUnlinkLogTest001 end"; +} +/** + * @tc.name: ProduceUnlinkLogTest002 + * @tc.desc: Verify the ProduceUnlinkLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceUnlinkLogTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceUnlinkLogTest001 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string name = "name"; + string childRecordId = ""; + auto res = logFile_->ProduceUnlinkLog(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceUnlinkLogTest002 failed"; + } + GTEST_LOG_(INFO) << "ProduceUnlinkLogTest002 end"; +} + +/** + * @tc.name: ProduceRenameOldLogTest001 + * @tc.desc: Verify the ProduceRenameOldLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameOldLogTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceRenameOldLogTest001 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string name = "name"; + string childRecordId = "childRecordId"; + auto res = logFile_->ProduceRenameOldLog(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceRenameOldLogTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceRenameOldLogTest001 end"; +} + +/** + * @tc.name: ProduceRenameOldLogTest002 + * @tc.desc: Verify the ProduceRenameOldLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameOldLogTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceRenameOldLogTest002 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string name = "name"; + string childRecordId = ""; + auto res = logFile_->ProduceRenameOldLog(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceRenameOldLogTest002 failed"; + } + GTEST_LOG_(INFO) << "ProduceRenameOldLogTest002 end"; +} + +/** + * @tc.name: ProduceRenameNewLogTest001 + * @tc.desc: Verify the ProduceRenameNewLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest001 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + auto res = logFile_->ProduceRenameNewLog(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest001 end"; +} + +#undef S_ISDIR +#define S_ISDIR(mode) (0) + +/** + * @tc.name: ProduceRenameNewLogTest002 + * @tc.desc: Verify the ProduceRenameNewLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest002 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string path = "path"; + string name = "name"; + string childRecordId = ""; + auto res = logFile_->ProduceRenameNewLog(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest002 failed"; + } + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest002 end"; +} + +#undef S_ISDIR +#define S_ISDIR(mode) (1) + +/** + * @tc.name: ProduceRenameNewLogTest003 + * @tc.desc: Verify the ProduceRenameNewLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest003 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string path = "path"; + string name = "name"; + string childRecordId = ""; + auto res = logFile_->ProduceRenameNewLog(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest003 failed"; + } + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest003 end"; +} + +/** + * @tc.name: ProduceCloseAndWriteTest001 + * @tc.desc: Verify the ProduceCloseAndWrite function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 end"; +} + +/** + * @tc.name: ProduceCloseAndWriteTest002 + * @tc.desc: Verify the ProduceCloseAndWrite function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string path = "path"; + string name = "name"; + string childRecordId = ""; + auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, name, childRecordId) + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 failed"; + } + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 end"; +} + +class LogFileMgrTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + static inline shared_ptr logFileMgr_; +} + +void LogFileMgrTest::SetUpTestCase(void) { + GTEST_LOG_(INFO) << "SetUpTestCase"; + logFileMgr_ = make_shared(); +} + +void LogFileMgrTest::TearDownTestCase(void) { + GTEST_LOG_(INFO) << "TearDownTestCase"; + logFileMgr_ = nullptr; +} + +void LogFileMgrTest::SetUp() { + +} + +void LogFileMgrTest::TearDown() { + +} + +/** + * @tc.name: GetInstanceTest001 + * @tc.desc: Verify the GetInstance function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, GetInstanceTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetInstanceTest001 start"; + try { + LogFileMgr& instance1 = LogFileMgr::GetInstance(); + LogFileMgr& instance2 = LogFileMgr::GetInstance(); + EXPECT_EQ(instance1, instance2); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetInstanceTest001 failed"; + } + GTEST_LOG_(INFO) << "GetInstanceTest001 end"; +} + +/** + * @tc.name: ProduceRequestTest001 + * @tc.desc: Verify the ProduceRequest function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, ProduceRequestTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceRequestTest001 start"; + try { + EventInfo eventInfo; + auto res = logFileMgr_->ProduceRequest(eventInfo); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceRequestTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceRequestTest001 end"; +} + + + + + + + + + + + +/** + * @tc.name: UnRegisterSyncFolderTest001 + * @tc.desc: Verify the UnRegisterSyncFolder function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + auto res = logFileMgr_->UnRegisterSyncFolder(userId, syncFolderIndex); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 failed"; + } + GTEST_LOG_(INFO) << "UnRegisterSyncFolderTest001 end"; +} + +/** + * @tc.name: RegisterSyncFolderChangesTest001 + * @tc.desc: Verify the RegisterSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, RegisterSyncFolderChangesTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + logFileMgr_->RegisterSyncFolderChanges(userId, syncFolderIndex); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 failed"; + } + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 end"; +} + +/** + * @tc.name: UnRegisterSyncFolderChangesTest001 + * @tc.desc: Verify the UnRegisterSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderChangesTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + logFileMgr_->UnRegisterSyncFolderChanges(userId, syncFolderIndex); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 failed"; + } + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 end"; +} + +/** + * @tc.name: GetCloudDiskServiceLogFileTest001 + * @tc.desc: Verify the GetCloudDiskServiceLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, GetCloudDiskServiceLogFileTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest001 start"; + try { + logFileMgr_->logFiles_.clear(); + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + auto res = logFileMgr_->GetCloudDiskServiceLogFile(userId, syncFolderIndex); + EXPECT_EQ(res->userId_, userId); + EXPECT_EQ(res->syncFolderIndex_, syncFolderIndex); + EXPECT_NE(logFileMgr_->logFiles_.empty()); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest001 failed"; + } + GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest001 end"; +} + +/** + * @tc.name: GetCloudDiskServiceLogFileTest002 + * @tc.desc: Verify the GetCloudDiskServiceLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, GetCloudDiskServiceLogFileTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest002 start"; + try { + logFileMgr_->logFiles_.clear(); + uint32_t userId = 3; + uint32_t syncFolderIndex = 4; + LogFileKey key(userId, syncFolderIndex); + shared_ptr newLogFile = make_shared(); + logFiles_.insert(key, newLogFile); + auto res = logFileMgr_->GetCloudDiskServiceLogFile(userId, syncFolderIndex); + EXPECT_EQ(res, newLogFile) + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest002 failed"; + } + GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest002 end"; +} + +/** + * @tc.name: OnDataChangeTest001 + * @tc.desc: Verify the OnDataChange function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, OnDataChangeTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "OnDataChangeTest001 start"; + try { + auto res = logFileMgr_->OnDataChange(); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "OnDataChangeTest001 failed"; + } + GTEST_LOG_(INFO) << "OnDataChangeTest001 end"; +} } \ No newline at end of file -- Gitee From 62764a06f318f800a2f1f880049237e4a3bbc07a Mon Sep 17 00:00:00 2001 From: ADHDer <19857096219@163.com> Date: Wed, 10 Sep 2025 10:36:19 +0800 Subject: [PATCH 13/26] test Signed-off-by: ADHDer <19857096219@163.com> --- .../cloud_disk_service_metafile_test.cpp | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp index 5cd8b4860..cd9fc45a2 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -412,6 +412,29 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoRemoveTest001, TestSize.Level1) GTEST_LOG_(INFO) << "DoRemoveTest001 End"; } +/** + * @tc.name: DoRemoveTest002 + * @tc.desc: Verify the DoRemove function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoRemoveTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoRemoveTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoRemove(base, recordId); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoRemoveTest002Q ERROR"; + } + GTEST_LOG_(INFO) << "DoRemoveTest002 End"; +} + /** * @tc.name: DoFlushTest001 * @tc.desc: Verify the DoFlush function @@ -435,6 +458,29 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoFlushTest001, TestSize.Level1) GTEST_LOG_(INFO) << "DoRemoveTest001 End"; } +/** + * @tc.name: DoFlushTest002 + * @tc.desc: Verify the DoFlush function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoFlushTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoFlushTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoRemove(base, recordId); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoRemoveTest002 ERROR"; + } + GTEST_LOG_(INFO) << "DoRemoveTest002 End"; +} + /** * @tc.name: DoUpdateTest001 * @tc.desc: Verify the DoUpdate function @@ -458,6 +504,29 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoUpdateTest001, TestSize.Level1) GTEST_LOG_(INFO) << "DoUpdateTest001 End"; } +/** + * @tc.name: DoUpdateTest002 + * @tc.desc: Verify the DoUpdate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoUpdateTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoUpdateTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoUpdate(base, recordId); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoUpdateTest002 ERROR"; + } + GTEST_LOG_(INFO) << "DoUpdateTest002 End"; +} + /** * @tc.name: DoRenameOldTest001 * @tc.desc: Verify the DoRenameOld function @@ -481,6 +550,29 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoRenameOldTest001, TestSize.Level1) GTEST_LOG_(INFO) << "DoRenameOldTest001 End"; } +/** + * @tc.name: DoRenameOldTest002 + * @tc.desc: Verify the DoRenameOld function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameOldTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoRenameOldTest001 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoRenameOld(base, recordId); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoRenameOldTest002 ERROR"; + } + GTEST_LOG_(INFO) << "DoRenameOldTest002 End"; +} + /** * @tc.name: DoRenameNewTest001 * @tc.desc: Verify the DoRenameNew function @@ -529,6 +621,86 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest002, TestSize.Level1) GTEST_LOG_(INFO) << "DoRenameNewTest002 End"; } +/** + * @tc.name: DoRenameNewTest003 + * @tc.desc: Verify the DoRenameNew function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoRenameNewTest003 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + base.name = "test_name"; + string recordId = "record_123"; + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); + auto ret = mFile.DoRenameNew(base, recordId); + EXPECT_EQ(ret, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoRenameNewTest003 ERROR"; + } + GTEST_LOG_(INFO) << "DoRenameNewTest003 End"; +} + +/** + * @tc.name: DoRenameNewTest004 + * @tc.desc: Verify the DoRenameNew function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoRenameNewTest004 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + base.name = "test_name"; + string recordId = "record_123"; + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(1)); + auto ret = mFile.DoRenameNew(base, recordId); + EXPECT_EQ(ret, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoRenameNewTest004 ERROR"; + } + GTEST_LOG_(INFO) << "DoRenameNewTest004 End"; +} + +/** + * @tc.name: DoRenameNewTest005 + * @tc.desc: Verify the DoRenameNew function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoRenameNewTest005 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + base.name = "test_name"; + string recordId = "record_123"; + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(1)); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); + auto ret = mFile.DoRenameNew(base, recordId); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoRenameNewTest005 ERROR"; + } + GTEST_LOG_(INFO) << "DoRenameNewTest005 End"; +} + /** * @tc.name: DoRenameTest001 * @tc.desc: Verify the DoRename function @@ -600,6 +772,30 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByNameTest001, TestSize.Level1) GTEST_LOG_(INFO) << "DoLookupByNameTest001 End"; } +/** + * @tc.name: DoLookupByNameTest002 + * @tc.desc: Verify the DoLookupByName function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByNameTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoLookupByNameTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoLookupByName(base); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoLookupByNameTest002 ERROR"; + } + GTEST_LOG_(INFO) << "DoLookupByNameTest002 End"; +} + + /** * @tc.name: DoLookupByRecordIdTest001 * @tc.desc: Verify the DoLookupByRecordId function @@ -623,6 +819,29 @@ HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByRecordIdTest001, TestSize.Level GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 End"; } +/** + * @tc.name: DoLookupByRecordIdTest002 + * @tc.desc: Verify the DoLookupByRecordId function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByRecordIdTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "DoLookupByRecordIdTest002 Start"; + try { + CloudDiskServiceMetaFile mFile(100, 1, 123); + mFile.fd_.Reset(1); + MetaBase base; + string recordId = "record_123"; + auto ret = mFile.DoLookupByRecordId(base); + EXPECT_EQ(ret, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "DoLookupByRecordIdTest002 ERROR"; + } + GTEST_LOG_(INFO) << "DoLookupByRecordIdTest002 End"; +} + /** * @tc.name: GetCreateInfoTest001 * @tc.desc: Verify the GetCreateInfo function -- Gitee From 6885ce3a90c68e4f603613e9614267a92aaa5a2b Mon Sep 17 00:00:00 2001 From: yujiahe Date: Wed, 10 Sep 2025 14:46:08 +0800 Subject: [PATCH 14/26] add metafile_test Signed-off-by: yujiahe --- .../clouddiskservice/mock/assistant.h | 2 + .../mock/cloud_disk_service_manager_mock.h | 34 +++ .../mock/cloud_disk_service_metafile_mock.cpp | 10 +- .../cloud_disk_service_logfile_test.cpp | 233 ++++++++++++++---- 4 files changed, 227 insertions(+), 52 deletions(-) create mode 100644 test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h index 8122206b3..ddac6d999 100644 --- a/test/unittests/clouddiskservice/mock/assistant.h +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -38,6 +38,7 @@ public: virtual DIR* opendir(const char* path) = 0; virtual struct dirent* readdir(DIR* d) = 0; virtual int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) = 0; + vitual int MockStat(const char* path, struct stat* buf); }; class AssistantMock : public Assistant { @@ -55,6 +56,7 @@ public: MOCK_METHOD1(opendir, DIR*(const char*)); MOCK_METHOD1(readdir, struct dirent*(DIR*)); MOCK_METHOD2(UnRegisterSyncFolder, int32_t(const int32_t, const uint64_t)) + MOCK_METHOD2(MockStat, int(const char*, struct stat*)); }; } diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h new file mode 100644 index 000000000..bcb2d1802 --- /dev/null +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h @@ -0,0 +1,34 @@ +/* + * 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 TEST_UNITTEST_CLOUD_DISK_SERVICE_MANAGER_MOCK_H +#define TEST_UNITTEST_CLOUD_DISK_SERVICE_MANAGER_MOCK_H + +#include "cloud_disk_service_manager.h" + +#include +#include + +namespace OHOS::FileManagement::CloudDiskService::Test { +using namespace testing; +using namespace testing::ext; +using namespace std; + +class CloudDiskServiceManagerMock final : public CloudDiskServiceManager { +public: + MOCK_METHOD1(UnregisterForSa, int32_t(const string)); +}; +} + +#endif \ No newline at end of file diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp index 809229975..bd0a5e385 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp @@ -15,7 +15,6 @@ #include "cloud_disk_service_metafile.h" namespace OHOS::FileManagement::CloudDiskService { -using namespace OHOS::FileManagement; int32_t CloudDiskServiceMetaFile::DoRemove(const MetaBase &base, std::string &recordId) { @@ -43,4 +42,13 @@ int32_t CloudDiskServiceMetaFile::DoRenameNew(const MetaBase &base, std::string return EINVAL; } } + +int32_t MetaFileMgr::GetRelativePath(const std::shared_ptr metaFile, std::string &path) +{ + if (metaFile->userId_ = 1) { + return E_OK; + } else { + return -1; + } +} } \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp index 78cc65c55..e3d340d7b 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -19,6 +19,11 @@ #include #include "assistant.h" +#include "cloud_disk_service_manager_mock.h" + +#define stat(path, buf) OHOS::FileManagement::CloudDiskService::Assistant::ins->MockStat(path, buf) +#include "cloud_disk_service_logfile.cpp" +#undef stat namespace OHOS::FileManagement::CloudDiskService::Test { using namespace testing; @@ -34,14 +39,14 @@ public: static inline shared_ptr logFile_; static inline shared_ptr insMock_; static inline shared_ptr managerMock_; -} +}; void CloudDiskServiceLogFileTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; logFile_ = make_shared(0, 0); insMock_ = make_shared(); - managerMock_ = make_shared(); Assistant::ins = insMock_; + managerMock_ = make_shared(); } void CloudDiskServiceLogFileTest::TearDownTestCase(void) { @@ -233,6 +238,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest001, TestSize.Level1 string childRecordId = "childRecordId"; string parentRecordId = "parentRecordId"; logFile_->changeDatas_.clear(); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(1)); int32_t res = logFile_->GenerateChangeData(eventInfo, line, childRecordId, parentRecordId); EXPECT_EQ(res, E_OK); @@ -243,30 +249,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest001, TestSize.Level1 GTEST_LOG_(INFO) << "GenerateChangeDataTest001 end"; } -/** - * @tc.name: GenerateChangeDataTest001 - * @tc.desc: Verify the GenerateChangeData function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "GenerateChangeDataTest001 start"; - try { - EventInfo eventInfo; - uint64_t line = 1; - string childRecordId = "childRecordId"; - string parentRecordId = "parentRecordId"; - logFile_->changeDatas_.clear(); - int32_t res = - logFile_->GenerateChangeData(eventInfo, line, childRecordId, parentRecordId); - EXPECT_EQ(res, E_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenerateChangeDataTest001 failed"; - } - GTEST_LOG_(INFO) << "GenerateChangeDataTest001 end"; -} /** * @tc.name: GenerateChangeDataTest002 @@ -287,6 +269,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest002, TestSize.Level1 ChangeData changeData; logFile_->changeDatas_.push_back(changeData); } + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); int32_t res = logFile_->GenerateChangeData(eventInfo, line, childRecordId, parentRecordId); EXPECT_EQ(res, E_OK); @@ -298,9 +281,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest002, TestSize.Level1 GTEST_LOG_(INFO) << "GenerateChangeDataTest002 end"; } -#undef S_ISDIR -#define S_ISDIR(mode) (0) - /** * @tc.name: FillChildForDirTest001 * @tc.desc: Verify the FillChildForDir function @@ -309,13 +289,17 @@ HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest002, TestSize.Level1 */ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "FillChildForDirTest001 start"; + GTEST_LOG_(INFO) << "FillChildForDirTest002 start"; try { string path = "path"; uint64_t timestamp = 1; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { + errno = ENOENT; + return 1; + }); int32_t res = logFile_->FillChildForDir(path, timestamp); - EXPECT_EQ(res, E_OK); + EXPECT_EQ(res, ENOENT); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "FillChildForDirTest001 failed"; @@ -324,7 +308,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest001, TestSize.Level1) } #undef S_ISDIR -#define S_ISDIR(mode) (1) +#define S_ISDIR(mode) (0) /** * @tc.name: FillChildForDirTest002 @@ -338,13 +322,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest002, TestSize.Level1) try { string path = "path"; uint64_t timestamp = 1; - EXPECT_CALL(*insMock_, opendir(_)).WillOnce([&]() { - errno = ENOENT; - return nullptr; - }); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); int32_t res = logFile_->FillChildForDir(path, timestamp); - EXPECT_EQ(res, ENOENT); + EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "FillChildForDirTest002 failed"; @@ -352,6 +333,9 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest002, TestSize.Level1) GTEST_LOG_(INFO) << "FillChildForDirTest002 end"; } +#undef S_ISDIR +#define S_ISDIR(mode) (1) + /** * @tc.name: FillChildForDirTest003 * @tc.desc: Verify the FillChildForDir function @@ -364,12 +348,14 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest003, TestSize.Level1) try { string path = "path"; uint64_t timestamp = 1; - DIR* dir = reinterpret_cast(UINT64_MAX); - EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); - EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(nullptr)); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, opendir(_)).WillOnce([&]() { + errno = ENOENT; + return nullptr; + }); int32_t res = logFile_->FillChildForDir(path, timestamp); - EXPECT_EQ(res, E_OK); + EXPECT_EQ(res, ENOENT); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "FillChildForDirTest003 failed"; @@ -389,6 +375,33 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest004, TestSize.Level1) try { string path = "path"; uint64_t timestamp = 1; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + DIR* dir = reinterpret_cast(UINT64_MAX); + EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); + EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(nullptr)); + int32_t res = + logFile_->FillChildForDir(path, timestamp); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "FillChildForDirTest004 failed"; + } + GTEST_LOG_(INFO) << "FillChildForDirTest004 end"; +} + +/** + * @tc.name: FillChildForDirTest005 + * @tc.desc: Verify the FillChildForDir function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "FillChildForDirTest005 start"; + try { + string path = "path"; + uint64_t timestamp = 1; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); DIR* dir = reinterpret_cast(UINT64_MAX); EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); dirent *entry1; @@ -405,9 +418,9 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest004, TestSize.Level1) EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FillChildForDirTest004 failed"; + GTEST_LOG_(INFO) << "FillChildForDirTest005 failed"; } - GTEST_LOG_(INFO) << "FillChildForDirTest004 end"; + GTEST_LOG_(INFO) << "FillChildForDirTest005 end"; } /** @@ -459,10 +472,12 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest001, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceLogTest001 start"; try { EventInfo eventInfo; - eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); - EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(1)); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { + errno = ENOENT; + return 1; + }); auto res = logFile_->ProduceLog(eventInfo); - EXPECT_EQ(res, 1); + EXPECT_EQ(res, ENOENT); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ProduceLogTest001 failed"; @@ -482,9 +497,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest002, TestSize.Level1) try { EventInfo eventInfo; eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); - EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(0); + EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(1)); auto res = logFile_->ProduceLog(eventInfo); - EXPECT_EQ(res, E_OK); + EXPECT_EQ(res, 1); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ProduceLogTest002 failed"; @@ -503,8 +519,11 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest003, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceLogTest003 start"; try { EventInfo eventInfo; - eventInfo.operateType = static_cast(OperationType::CREATE); + eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(0); + EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(0)); auto res = logFile_->ProduceLog(eventInfo); + EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ProduceLogTest003 failed"; @@ -512,6 +531,29 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest003, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceLogTest003 end"; } +/** + * @tc.name: ProduceLogTest004 + * @tc.desc: Verify the ProduceLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogTest004 start"; + try { + EventInfo eventInfo; + eventInfo.operateType = static_cast(OperationType::CREATE); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(0); + // productlogforoperation actions + auto res = logFile_->ProduceLog(eventInfo); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogTest004 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogTest004 end"; +} + @@ -530,7 +572,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "PraseLogTest001 start"; try { - logFile_.currentLine_ = 0; + logFile_->currentLine_ = 0; uint64_t line = 1; ChangeData changeData; auto res = logFile_->PraseLog(line, changeData); @@ -542,6 +584,75 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest001, TestSize.Level1) GTEST_LOG_(INFO) << "PraseLogTest001 end"; } +/** + * @tc.name: PraseLogTest002 + * @tc.desc: Verify the PraseLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002 TestSize.Level1) +{ + GTEST_LOG_(INFO) << "PraseLogTest002 start"; + try { + logFile_->userId_ = 0; + logFile_->currentLine_ = 2; + uint64_t line = 1; + ChangeData changeData; + auto res = logFile_->PraseLog(line, changeData); + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "PraseLogTest002 failed"; + } + GTEST_LOG_(INFO) << "PraseLogTest002 end"; +} + +/** + * @tc.name: PraseLogTest003 + * @tc.desc: Verify the PraseLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) +{ + GTEST_LOG_(INFO) << "PraseLogTest003 start"; + try { + logFile_->userId_ = 1; + logFile_->currentLine_ = 1; + uint64_t line = 1; + ChangeData changeData; + auto res = logFile_->PraseLog(line, changeData); + EXPECT_EQ(res, E_END_OF_LOGFILE); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "PraseLogTest003 failed"; + } + GTEST_LOG_(INFO) << "PraseLogTest003 end"; +} + +/** + * @tc.name: PraseLogTest004 + * @tc.desc: Verify the PraseLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest004 TestSize.Level1) +{ + GTEST_LOG_(INFO) << "PraseLogTest004 start"; + try { + logFile_->userId_ = 1; + logFile_->currentLine_ = 2; + uint64_t line = 1; + ChangeData changeData; + auto res = logFile_->PraseLog(line, changeData); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "PraseLogTest004 failed"; + } + GTEST_LOG_(INFO) << "PraseLogTest004 end"; +} + @@ -842,7 +953,27 @@ HWTEST_F(LogFileMgrTest, ProduceRequestTest001, TestSize.Level1) - +/** + * @tc.name: RegisterSyncFolderTest001 + * @tc.desc: Verify the RegisterSyncFolder function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, RegisterSyncFolderTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + string path = "path"; + auto res = logFileMgr_->RegisterSyncFolder(userId, syncFolderIndex, path); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 failed"; + } + GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 end"; +} /** * @tc.name: UnRegisterSyncFolderTest001 -- Gitee From 9f6fde91b175c7f010a8122af9bdf0fdba01d72e Mon Sep 17 00:00:00 2001 From: yujiahe Date: Wed, 10 Sep 2025 15:18:41 +0800 Subject: [PATCH 15/26] add metafile_test Signed-off-by: yujiahe --- test/unittests/clouddiskservice/mock/assistant.h | 4 ++-- .../mock/cloud_disk_service_manager_mock.h | 11 ++++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h index ddac6d999..b8d9a1e0e 100644 --- a/test/unittests/clouddiskservice/mock/assistant.h +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -38,7 +38,7 @@ public: virtual DIR* opendir(const char* path) = 0; virtual struct dirent* readdir(DIR* d) = 0; virtual int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) = 0; - vitual int MockStat(const char* path, struct stat* buf); + virtual int MockStat(const char* path, struct stat* buf); }; class AssistantMock : public Assistant { @@ -55,7 +55,7 @@ public: MOCK_METHOD2(access, int(const char *, int)); MOCK_METHOD1(opendir, DIR*(const char*)); MOCK_METHOD1(readdir, struct dirent*(DIR*)); - MOCK_METHOD2(UnRegisterSyncFolder, int32_t(const int32_t, const uint64_t)) + MOCK_METHOD2(UnRegisterSyncFolder, int32_t(const int32_t, const uint64_t)); MOCK_METHOD2(MockStat, int(const char*, struct stat*)); }; } diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h index bcb2d1802..4d37a5cb0 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h @@ -27,7 +27,16 @@ using namespace std; class CloudDiskServiceManagerMock final : public CloudDiskServiceManager { public: - MOCK_METHOD1(UnregisterForSa, int32_t(const string)); + MOCK_METHOD2(RegisterSyncFolderChanges, int32_t(const std::string &, + const std::shared_ptr)); + MOCK_METHOD1(UnregisterSyncFolderChanges, int32_t(const std::string &)); + MOCK_METHOD4(GetSyncFolderChanges, int32_t(const std::string &, uint64_t, uint64_t, ChangesResult &)); + MOCK_METHOD3(SetFileSyncStates, int32_t(const std::string &, + const std::vector &, std::vetor &)); + MOCK_METHOD3(GetFileSyncStates, int32_t(const std::string &, const std::vector &, std::vector &)); + MOCK_METHOD3(RegisterSyncFolder, int32_t(int32_t, const std::string &, const std::string &)); + MOCK_METHOD3(UnregisterSyncFolder, int32_t(int32_t, const std::string &, const std::string &)); + MOCK_METHOD1(UnregisterForSa, int32_t(const string&)); }; } -- Gitee From d138b529f9f92630f2af8e7b1a68291b031a2abb Mon Sep 17 00:00:00 2001 From: yujiahe Date: Wed, 10 Sep 2025 19:39:51 +0800 Subject: [PATCH 16/26] add metafile_test Signed-off-by: yujiahe --- .../mock/cloud_disk_service_manager_mock.h | 2 +- .../mock/cloud_disk_service_metafile_mock.cpp | 11 ++-- .../cloud_disk_service_logfile_test.cpp | 63 +++++++++++++------ 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h index 4d37a5cb0..1d0b1f645 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h @@ -32,7 +32,7 @@ public: MOCK_METHOD1(UnregisterSyncFolderChanges, int32_t(const std::string &)); MOCK_METHOD4(GetSyncFolderChanges, int32_t(const std::string &, uint64_t, uint64_t, ChangesResult &)); MOCK_METHOD3(SetFileSyncStates, int32_t(const std::string &, - const std::vector &, std::vetor &)); + const std::vector &, std::vector &)); MOCK_METHOD3(GetFileSyncStates, int32_t(const std::string &, const std::vector &, std::vector &)); MOCK_METHOD3(RegisterSyncFolder, int32_t(int32_t, const std::string &, const std::string &)); MOCK_METHOD3(UnregisterSyncFolder, int32_t(int32_t, const std::string &, const std::string &)); diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp index bd0a5e385..08b08647f 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp @@ -15,13 +15,16 @@ #include "cloud_disk_service_metafile.h" namespace OHOS::FileManagement::CloudDiskService { +#ifndef E_OK +#define E_OK 0 +#endif int32_t CloudDiskServiceMetaFile::DoRemove(const MetaBase &base, std::string &recordId) { if (recordId == "") { return E_OK; } else { - return EINVAL; + return 1; } } @@ -30,7 +33,7 @@ int32_t CloudDiskServiceMetaFile::DoRenameOld(const MetaBase &base, std::string if (recordId == "") { return E_OK; } else { - return EINVAL; + return 1; } } @@ -39,13 +42,13 @@ int32_t CloudDiskServiceMetaFile::DoRenameNew(const MetaBase &base, std::string if (recordId == "") { return E_OK; } else { - return EINVAL; + return 1; } } int32_t MetaFileMgr::GetRelativePath(const std::shared_ptr metaFile, std::string &path) { - if (metaFile->userId_ = 1) { + if (metaFile->userId_ == 1) { return E_OK; } else { return -1; diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp index e3d340d7b..cdc94d0ef 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -43,9 +43,9 @@ public: void CloudDiskServiceLogFileTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; - logFile_ = make_shared(0, 0); insMock_ = make_shared(); Assistant::ins = insMock_; + logFile_ = make_shared(0, 0); managerMock_ = make_shared(); } @@ -76,8 +76,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, WriteLogFileTest001, TestSize.Level1) GTEST_LOG_(INFO) << "WriteLogFileTest001 start"; try { LogBlock logBlock; + logFile_->fd_ = 1; + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); int32_t res = logFile_->WriteLogFile(logBlock); - EXPECT_EQ(res, E_OK); + EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "WriteLogFileTest001 failed"; @@ -85,6 +87,29 @@ HWTEST_F(CloudDiskServiceLogFileTest, WriteLogFileTest001, TestSize.Level1) GTEST_LOG_(INFO) << "WriteLogFileTest001 end"; } +/** + * @tc.name: WriteLogFileTest002 + * @tc.desc: Verify the WriteLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, WriteLogFileTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "WriteLogFileTest002 start"; + try { + LogBlock logBlock; + logFile_->fd_ = 1; + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(4096)); + int32_t res = logFile_->WriteLogFile(logBlock); + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "WriteLogFileTest002 failed"; + } + GTEST_LOG_(INFO) << "WriteLogFileTest002 end"; +} + /** * @tc.name: ReadLogFileTest001 * @tc.desc: Verify the ReadLogFile function @@ -97,7 +122,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ReadLogFileTest001, TestSize.Level1) try { uint64_t line = 1; LogBlock logBlock; - int32_t res = logFile_->ReadLogFile(logBlock); + int32_t res = logFile_->ReadLogFile(line, logBlock); EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); @@ -161,10 +186,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, CloudDiskServiceLogFileTest001, TestSize.L uint32_t syncFolderIndex = 2; EXPECT_CALL(*insMock_, access(_, _)).WillOnce(Return(0)); shared_ptr logFile = - make_shared(userId, syncsyncFolderIndex); - EXPECT_EQ(logFile.userId_, userId); - EXPECT_EQ(logFile.syncFolderIndex_, syncFolderIndex); - EXPECT_NE(logFile.needCallback_); + make_shared(userId, syncFolderIndex); + EXPECT_EQ(logFile->userId_, userId); + EXPECT_EQ(logFile->syncFolderIndex_, syncFolderIndex); + EXPECT_FALSE(logFile->needCallback_); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest001 failed"; @@ -186,11 +211,11 @@ HWTEST_F(CloudDiskServiceLogFileTest, CloudDiskServiceLogFileTest002, TestSize.L uint32_t syncFolderIndex = 4; EXPECT_CALL(*insMock_, access(_, _)).WillOnce(Return(1)); shared_ptr logFile = - make_shared(userId, syncsyncFolderIndex); - EXPECT_EQ(logFile.userId_, userId); - EXPECT_EQ(logFile.syncFolderIndex_, syncFolderIndex); - EXPECT_NE(logFile.needCallback_); - EXPECT_EQ(logFile.currentLine_, 0); + make_shared(userId, syncFolderIndex); + EXPECT_EQ(logFile->userId_, userId); + EXPECT_EQ(logFile->syncFolderIndex_, syncFolderIndex); + EXPECT_FALSE(logFile->needCallback_); + EXPECT_EQ(logFile->currentLine_, 0); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "CloudDiskServiceLogFileTest002 failed"; @@ -273,7 +298,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, GenerateChangeDataTest002, TestSize.Level1 int32_t res = logFile_->GenerateChangeData(eventInfo, line, childRecordId, parentRecordId); EXPECT_EQ(res, E_OK); - EXPECT_TRUE(logFile_->changeData_.empty()); + EXPECT_TRUE(logFile_->changeDatas_.empty()); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GenerateChangeDataTest002 failed"; @@ -410,9 +435,9 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest005, TestSize.Level1) entry2->d_name = ".."; dirent *entry; entry->d_name = "name"; - EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(entry1) + EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(entry1)) .WillOnce(Return(entry2)) - .WillOnce(Return(entry))); + .WillOnce(Return(entry)); int32_t res = logFile_->FillChildForDir(path, timestamp); EXPECT_EQ(res, E_OK); @@ -453,7 +478,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, StopCallbackTest001, TestSize.Level1) GTEST_LOG_(INFO) << "StopCallbackTest001 start"; try { logFile_->StopCallback(); - EXPECT_NE(logFile_->needCallback_); + EXPECT_FALSE(logFile_->needCallback_); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "StopCallbackTest001 failed"; @@ -497,7 +522,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest002, TestSize.Level1) try { EventInfo eventInfo; eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(0); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(1)); auto res = logFile_->ProduceLog(eventInfo); EXPECT_EQ(res, 1); @@ -520,7 +545,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest003, TestSize.Level1) try { EventInfo eventInfo; eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(0); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(0)); auto res = logFile_->ProduceLog(eventInfo); EXPECT_EQ(res, E_OK); @@ -543,7 +568,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest004, TestSize.Level1) try { EventInfo eventInfo; eventInfo.operateType = static_cast(OperationType::CREATE); - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(0); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); // productlogforoperation actions auto res = logFile_->ProduceLog(eventInfo); EXPECT_EQ(res, E_OK); -- Gitee From cf03529901a33c84ed04d2c848a63dd6b76a69f7 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Thu, 11 Sep 2025 20:56:15 +0800 Subject: [PATCH 17/26] fix Signed-off-by: yujiahe --- test/unittests/clouddiskservice/BUILD.gn | 116 ++++ .../clouddiskservice/mock/assistant.h | 2 - .../mock/cloud_disk_service_manager_mock.cpp | 24 + .../mock/cloud_disk_service_manager_mock.h | 6 + .../mock/cloud_disk_service_metafile_mock.cpp | 39 ++ .../mock/system_function_mock.cpp | 10 - ...cloud_disk_service_logfile_static_test.cpp | 41 +- .../cloud_disk_service_logfile_test.cpp | 581 ++++++++++++------ 8 files changed, 571 insertions(+), 248 deletions(-) create mode 100644 test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.cpp diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index c879e9d22..2c832f112 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -14,6 +14,122 @@ import("//build/test.gni") import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_unittest("cloud_disk_service_logfile_static_test") { + module_out_path = "dfs_service/dfs_service" + + source = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock/system_function_mock.cpp", + "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + "${distributedfile_path}/utils/log/src/utils_log.cpp", + "${distributedfile_path}/utils/system/src/utils_directory.cpp", + "${distributedfile_path}/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp", + "sync_folder/cloud_disk_service_logfile_static_test.cpp", + ] + + include_dirs = [ + "${services_path}/clouddiskservice/sync_folder/src", + "${services_path}/clouddiskservice/sync_folder/include", + "${distributedfile_path}/test/unittests/clouddiskservice/mock", + "${innerkits_native_path}/clouddiskservice_kit_inner", + "${services_path}/clouddiskservice/monitor/include", + "${services_path}/clouddiskservice/ipc/include", + "${services_path}/clouddiskservice/utils/include", + "${distributedfile_path}/utils/inner_api", + "${distributedfile_path}/utils/system/include", + "${distributedfile_path}/interfaces/inner_api/native/clouddiskservice_kit_inner", + "${distributedfile_path}/utils/dentry/include", + "${distributedfile_path}/utils/log/include", + ] + + deps = [ + + ] + + external_deps = [ + "e2fsprogs:libext2_uuid", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "c_utils:utils", + "ffrt:libffrt", + "json:nlohmann_json_static", + "libfuse:libfuse", + "hisysevent:libsysevent", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + public_configs = [ + "${services_path}/clouddiskservice:cloud_disk_service_public_config", + ] + + if (cloudsync_service_hicollie_enable) { + external_deps += [ "hicollie:libhicollie" ] + defines += [ "HICOLLIE_ENABLE" ] + } + + use_exceptions = true +} + +ohos_unittest("cloud_disk_service_logfile_test") { + module_out_path = "dfs_service/dfs_service" + + source = [ + "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + "${services_path}/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp", + "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp", + "${distributedfile_path}/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.cpp", + "${distributedfile_path}/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp", + "${services_path}/clouddiskservice/sync_folder/src/uuid_helper.cpp", + "${distributedfile_path}/test/unittests/clouddiskservice/mock/system_function_mock.cpp", + "sync_folder/cloud_disk_service_logfile_test.cpp", + ] + + include_dirs = [ + "${services_path}/clouddiskservice/sync_folder/src", + "${services_path}/clouddiskservice/sync_folder/include", + "${distributedfile_path}/test/unittests/clouddiskservice/mock", + "${innerkits_native_path}/clouddiskservice_kit_inner", + "${services_path}/clouddiskservice/monitor/include", + "${services_path}/clouddiskservice/ipc/include", + "${services_path}/clouddiskservice/utils/include", + "${distributedfile_path}/utils/inner_api", + "${distributedfile_path}/utils/system/include", + "${distributedfile_path}/utils/dentry/include", + "${distributedfile_path}/utils/log/include", + ] + + deps = [ + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "e2fsprogs:libext2_uuid", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "c_utils:utils", + "ffrt:libffrt", + "json:nlohmann_json_static", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + public_configs = [ + "${services_path}/clouddiskservice:cloud_disk_service_public_config", + ] + + use_exceptions = true +} + ohos_unittest("cloud_disk_service_metafile_test") { module_out_path = "dfs_service/dfs_service" diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h index b8d9a1e0e..4239abc0d 100644 --- a/test/unittests/clouddiskservice/mock/assistant.h +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -37,7 +37,6 @@ public: virtual int access(const char *name, int type) = 0; virtual DIR* opendir(const char* path) = 0; virtual struct dirent* readdir(DIR* d) = 0; - virtual int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) = 0; virtual int MockStat(const char* path, struct stat* buf); }; @@ -55,7 +54,6 @@ public: MOCK_METHOD2(access, int(const char *, int)); MOCK_METHOD1(opendir, DIR*(const char*)); MOCK_METHOD1(readdir, struct dirent*(DIR*)); - MOCK_METHOD2(UnRegisterSyncFolder, int32_t(const int32_t, const uint64_t)); MOCK_METHOD2(MockStat, int(const char*, struct stat*)); }; } diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.cpp b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.cpp new file mode 100644 index 000000000..6f41144f0 --- /dev/null +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.cpp @@ -0,0 +1,24 @@ +/* + * 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 "cloud_disk_service_manager_mock.h" + +namespace OHOS::FileManagement::CloudDiskService { + +CloudDiskServiceManager &CloudDiskServiceManager::GetInstance() +{ + return Test::CloudDiskServiceManagerMock::GetInstance(); +} +} \ No newline at end of file diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h index 1d0b1f645..cd5b8e283 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h @@ -27,6 +27,12 @@ using namespace std; class CloudDiskServiceManagerMock final : public CloudDiskServiceManager { public: + static CloudDiskServiceManagerMock &GetInstance() + { + static CloudDiskServiceMock instance; + return instance; + } + MOCK_METHOD2(RegisterSyncFolderChanges, int32_t(const std::string &, const std::shared_ptr)); MOCK_METHOD1(UnregisterSyncFolderChanges, int32_t(const std::string &)); diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp index 08b08647f..6fbdb21a0 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp @@ -19,6 +19,26 @@ namespace OHOS::FileManagement::CloudDiskService { #define E_OK 0 #endif +CloudDiskServiceMetaFile::CloudDiskServiceMetaFile(const uint32_t userId, const uint32_t syncFolderIndex, + const uint64_t inode) +{ + userId_ = userId; +} + +MetaFileMgr& MetaFile::GetInstance() +{ + static MetaFileMgr instance_; + return instance_; +} + +std::shared_ptr MetaFileMgr::GetCloudDiskServiceMetaFile(uint32_t userId, + const uint32_t syncFolderIndex, const uint64_t inode) +{ + std::shared_ptr metaFile = + std::make_shared(userId, syncFolderIndex, inode); + return metaFile; +} + int32_t CloudDiskServiceMetaFile::DoRemove(const MetaBase &base, std::string &recordId) { if (recordId == "") { @@ -54,4 +74,23 @@ int32_t MetaFileMgr::GetRelativePath(const std::shared_ptrftruncate(fd, length); } -int access(const char *name, int type) -{ - return Assistant::ins->access(name, type); -} - DIR* opendir(const char* path) { return Assistant::ins->opendir(path); @@ -47,11 +42,6 @@ struct dirent* readdir(DIR* d) return Assistant::ins->readdir(d); } -int32_t LogFileMgr::UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) -{ - return Assistant::ins->UnRegisterSyncFolder(userId, syncFolderIndex); -} - namespace OHOS::FileManagement { int64_t FileUtils::ReadFile(int fd, off_t offset, size_t size, void *data) { diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp index 50ce2e173..d7bd53c01 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp @@ -178,8 +178,8 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest003, TestSize.Lev try { int fd = 1; uint32_t bucket = 1; - EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(4096)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).Time(2).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(LOGGROUP_SIZE)); auto res = LoadCurrentPage(fd, bucket); EXPECT_NE(res, nullptr); } catch (...) { @@ -202,7 +202,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest001, TestSize.Lev LogGroup logGroup; int fd = 1; uint32_t line = 1; - EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(-1)); + EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(1)); auto res = SyncCurrentPage(logGroup, fd, line); EXPECT_EQ(res, EINVAL); } catch (...) { @@ -225,11 +225,10 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest002, TestSize.Lev LogGroup logGroup; int fd = 1; uint32_t line = 1; - EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(LOGGROUP_SIZE)); EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); auto res = SyncCurrentPage(logGroup, fd, line); - EXPECT_EQ(res, EINVAL); + EXPECT_EQ(res, 1); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "SyncCurrentPageTest002 failed"; @@ -250,9 +249,8 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest003, TestSize.Lev LogGroup logGroup; int fd = 1; uint32_t line = 1; - EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(4096)); - EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); + EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(LOGGROUP_SIZE)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); auto res = SyncCurrentPage(logGroup, fd, line); EXPECT_NE(res, E_OK); } catch (...) { @@ -262,31 +260,6 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest003, TestSize.Lev GTEST_LOG_(INFO) << "SyncCurrentPageTest003 end"; } -/** - * @tc.name: SyncCurrentPageTest004 - * @tc.desc: Verify the SyncCurrentPage function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest004, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "SyncCurrentPageTest004 start"; - try { - LogGroup logGroup; - int fd = 1; - uint32_t line = 1; - EXPECT_CALL(*insMock_, fstat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(4096)); - EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); - auto res = SyncCurrentPage(logGroup, fd, line); - EXPECT_EQ(res, E_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "SyncCurrentPageTest004 failed"; - } - GTEST_LOG_(INFO) << "SyncCurrentPageTest004 end"; -} - /** * @tc.name: FillLogGroupTest001 * @tc.desc: Verify the FillLogGroup function diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp index cdc94d0ef..fb1a55fbe 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -22,8 +22,10 @@ #include "cloud_disk_service_manager_mock.h" #define stat(path, buf) OHOS::FileManagement::CloudDiskService::Assistant::ins->MockStat(path, buf) +#define access OHOS::FileManagement::CloudDiskService::Assistant::ins->access #include "cloud_disk_service_logfile.cpp" #undef stat +#undef access namespace OHOS::FileManagement::CloudDiskService::Test { using namespace testing; @@ -38,7 +40,6 @@ public: void TearDown(); static inline shared_ptr logFile_; static inline shared_ptr insMock_; - static inline shared_ptr managerMock_; }; void CloudDiskServiceLogFileTest::SetUpTestCase(void) { @@ -46,7 +47,6 @@ void CloudDiskServiceLogFileTest::SetUpTestCase(void) { insMock_ = make_shared(); Assistant::ins = insMock_; logFile_ = make_shared(0, 0); - managerMock_ = make_shared(); } void CloudDiskServiceLogFileTest::TearDownTestCase(void) { @@ -54,7 +54,6 @@ void CloudDiskServiceLogFileTest::TearDownTestCase(void) { logFile_ = nullptr; Assistant::ins = nullptr; insMock_ = nullptr; - managerMock_ = nullptr; } void CloudDiskServiceLogFileTest::SetUp() { @@ -76,7 +75,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, WriteLogFileTest001, TestSize.Level1) GTEST_LOG_(INFO) << "WriteLogFileTest001 start"; try { LogBlock logBlock; - logFile_->fd_ = 1; EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); int32_t res = logFile_->WriteLogFile(logBlock); EXPECT_EQ(res, -1); @@ -98,7 +96,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, WriteLogFileTest002, TestSize.Level1) GTEST_LOG_(INFO) << "WriteLogFileTest002 start"; try { LogBlock logBlock; - logFile_->fd_ = 1; EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(4096)); int32_t res = logFile_->WriteLogFile(logBlock); @@ -122,8 +119,9 @@ HWTEST_F(CloudDiskServiceLogFileTest, ReadLogFileTest001, TestSize.Level1) try { uint64_t line = 1; LogBlock logBlock; + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)); int32_t res = logFile_->ReadLogFile(line, logBlock); - EXPECT_EQ(res, E_OK); + EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ReadLogFileTest001 failed"; @@ -131,6 +129,29 @@ HWTEST_F(CloudDiskServiceLogFileTest, ReadLogFileTest001, TestSize.Level1) GTEST_LOG_(INFO) << "ReadLogFileTest001 end"; } +/** + * @tc.name: ReadLogFileTest002 + * @tc.desc: Verify the ReadLogFile function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ReadLogFileTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ReadLogFileTest002 start"; + try { + uint64_t line = 1; + LogBlock logBlock; + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillRepeatedly(Return(1)); + EXPECT_CALL(*insMock_, ReadFile(_, _, _,_)).WillRepeatedly(Return(4096)); + int32_t res = logFile_->ReadLogFile(line, logBlock); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ReadLogFileTest002 failed"; + } + GTEST_LOG_(INFO) << "ReadLogFileTest002 end"; +} + /** * @tc.name: OnDataChangeTest001 * @tc.desc: Verify the OnDataChange function @@ -332,9 +353,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest001, TestSize.Level1) GTEST_LOG_(INFO) << "FillChildForDirTest001 end"; } -#undef S_ISDIR -#define S_ISDIR(mode) (0) - /** * @tc.name: FillChildForDirTest002 * @tc.desc: Verify the FillChildForDir function @@ -347,7 +365,9 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest002, TestSize.Level1) try { string path = "path"; uint64_t timestamp = 1; - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + struct stat statInfo; + statInfo.st_mode = S_IFREG; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(SetArgPointee<1>(statInfo), Return(0)); int32_t res = logFile_->FillChildForDir(path, timestamp); EXPECT_EQ(res, E_OK); @@ -358,9 +378,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest002, TestSize.Level1) GTEST_LOG_(INFO) << "FillChildForDirTest002 end"; } -#undef S_ISDIR -#define S_ISDIR(mode) (1) - /** * @tc.name: FillChildForDirTest003 * @tc.desc: Verify the FillChildForDir function @@ -373,8 +390,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest003, TestSize.Level1) try { string path = "path"; uint64_t timestamp = 1; - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock_, opendir(_)).WillOnce([&]() { + struct stat statInfo; + statInfo.st_mode = S_IFDIR; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); + EXPECT_CALL(*insMock_, opendir(_)).WillRepeatedly([&]() { errno = ENOENT; return nullptr; }); @@ -388,65 +407,69 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest003, TestSize.Level1) GTEST_LOG_(INFO) << "FillChildForDirTest003 end"; } -/** - * @tc.name: FillChildForDirTest004 - * @tc.desc: Verify the FillChildForDir function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest004, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FillChildForDirTest004 start"; - try { - string path = "path"; - uint64_t timestamp = 1; - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - DIR* dir = reinterpret_cast(UINT64_MAX); - EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); - EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(nullptr)); - int32_t res = - logFile_->FillChildForDir(path, timestamp); - EXPECT_EQ(res, E_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FillChildForDirTest004 failed"; - } - GTEST_LOG_(INFO) << "FillChildForDirTest004 end"; -} - -/** - * @tc.name: FillChildForDirTest005 - * @tc.desc: Verify the FillChildForDir function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest005, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "FillChildForDirTest005 start"; - try { - string path = "path"; - uint64_t timestamp = 1; - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - DIR* dir = reinterpret_cast(UINT64_MAX); - EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); - dirent *entry1; - entry1->d_name = "."; - dirent *entry2; - entry2->d_name = ".."; - dirent *entry; - entry->d_name = "name"; - EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(entry1)) - .WillOnce(Return(entry2)) - .WillOnce(Return(entry)); - int32_t res = - logFile_->FillChildForDir(path, timestamp); - EXPECT_EQ(res, E_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "FillChildForDirTest005 failed"; - } - GTEST_LOG_(INFO) << "FillChildForDirTest005 end"; -} +// /** +// * @tc.name: FillChildForDirTest004 +// * @tc.desc: Verify the FillChildForDir function +// * @tc.type: FUNC +// * @tc.require: NA +// */ +// HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest004, TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "FillChildForDirTest004 start"; +// try { +// string path = "path"; +// uint64_t timestamp = 1; +// struct stat statInfo; +// statInfo.st_mode = S_IFDIR; +// EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); +// DIR* dir = reinterpret_cast(UINT64_MAX); +// EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); +// EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(nullptr)); +// int32_t res = +// logFile_->FillChildForDir(path, timestamp); +// EXPECT_EQ(res, E_OK); +// } catch (...) { +// EXPECT_TRUE(false); +// GTEST_LOG_(INFO) << "FillChildForDirTest004 failed"; +// } +// GTEST_LOG_(INFO) << "FillChildForDirTest004 end"; +// } + +// /** +// * @tc.name: FillChildForDirTest005 +// * @tc.desc: Verify the FillChildForDir function +// * @tc.type: FUNC +// * @tc.require: NA +// */ +// HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest005, TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "FillChildForDirTest005 start"; +// try { +// string path = "path"; +// uint64_t timestamp = 1; +// struct stat statInfo; +// statInfo.st_mode = S_IFDIR; +// EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); +// DIR* dir = reinterpret_cast(UINT64_MAX); +// EXPECT_CALL(*insMock_, opendir(_)).WillOnce(Return(dir)); +// dirent *entry1 = new dirent; +// strcpy(entry1->d_name, "."); +// dirent *entry2 = new dirent; +// strcpy(entry2->d_name, ".."); +// dirent *entry = new dirent; +// strcpy(entry->d_name, "name"); +// EXPECT_CALL(*insMock_, readdir(_)).WillOnce(Return(entry1)) +// .WillOnce(Return(entry2)) +// .WillOnce(Return(entry)); +// int32_t res = +// logFile_->FillChildForDir(path, timestamp); +// EXPECT_EQ(res, E_OK); +// } catch (...) { +// EXPECT_TRUE(false); +// GTEST_LOG_(INFO) << "FillChildForDirTest005 failed"; +// } +// GTEST_LOG_(INFO) << "FillChildForDirTest005 end"; +// } /** * @tc.name: StartCallbackTest001 @@ -523,7 +546,8 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest002, TestSize.Level1) EventInfo eventInfo; eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(1)); + CloudDiskServiceManagerMock& mock = CloudDiskServiceManagerMock::GetInstance(); + EXPECT_CALL(mock, UnregisterForSa(_)).WillOnce(Return(1)); auto res = logFile_->ProduceLog(eventInfo); EXPECT_EQ(res, 1); } catch (...) { @@ -546,7 +570,8 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest003, TestSize.Level1) EventInfo eventInfo; eventInfo.operateType = static_cast(OperationType::SYNC_FOLDER_INVALID); EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*managerMock_, UnregisterForSa(_)).WillOnce(Return(0)); + CloudDiskServiceManagerMock& mock = CloudDiskServiceManagerMock::GetInstance(); + EXPECT_CALL(mock, UnregisterForSa(_)).WillOnce(Return(0)); auto res = logFile_->ProduceLog(eventInfo); EXPECT_EQ(res, E_OK); } catch (...) { @@ -556,27 +581,27 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest003, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceLogTest003 end"; } -/** - * @tc.name: ProduceLogTest004 - * @tc.desc: Verify the ProduceLog function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest004, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "ProduceLogTest004 start"; - try { - EventInfo eventInfo; - eventInfo.operateType = static_cast(OperationType::CREATE); - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - // productlogforoperation actions - auto res = logFile_->ProduceLog(eventInfo); - EXPECT_EQ(res, E_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogTest004 failed"; - } - GTEST_LOG_(INFO) << "ProduceLogTest004 end"; +// /** +// * @tc.name: ProduceLogTest004 +// * @tc.desc: Verify the ProduceLog function +// * @tc.type: FUNC +// * @tc.require: NA +// */ +// HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest004, TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "ProduceLogTest004 start"; +// try { +// EventInfo eventInfo; +// eventInfo.operateType = static_cast(OperationType::CREATE); +// EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); +// // productlogforoperation actions +// auto res = logFile_->ProduceLog(eventInfo); +// EXPECT_EQ(res, E_OK); +// } catch (...) { +// EXPECT_TRUE(false); +// GTEST_LOG_(INFO) << "ProduceLogTest004 failed"; +// } +// GTEST_LOG_(INFO) << "ProduceLogTest004 end"; } @@ -609,74 +634,74 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest001, TestSize.Level1) GTEST_LOG_(INFO) << "PraseLogTest001 end"; } -/** - * @tc.name: PraseLogTest002 - * @tc.desc: Verify the PraseLog function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002 TestSize.Level1) -{ - GTEST_LOG_(INFO) << "PraseLogTest002 start"; - try { - logFile_->userId_ = 0; - logFile_->currentLine_ = 2; - uint64_t line = 1; - ChangeData changeData; - auto res = logFile_->PraseLog(line, changeData); - EXPECT_EQ(res, -1); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "PraseLogTest002 failed"; - } - GTEST_LOG_(INFO) << "PraseLogTest002 end"; -} - -/** - * @tc.name: PraseLogTest003 - * @tc.desc: Verify the PraseLog function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) -{ - GTEST_LOG_(INFO) << "PraseLogTest003 start"; - try { - logFile_->userId_ = 1; - logFile_->currentLine_ = 1; - uint64_t line = 1; - ChangeData changeData; - auto res = logFile_->PraseLog(line, changeData); - EXPECT_EQ(res, E_END_OF_LOGFILE); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "PraseLogTest003 failed"; - } - GTEST_LOG_(INFO) << "PraseLogTest003 end"; -} - -/** - * @tc.name: PraseLogTest004 - * @tc.desc: Verify the PraseLog function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest004 TestSize.Level1) -{ - GTEST_LOG_(INFO) << "PraseLogTest004 start"; - try { - logFile_->userId_ = 1; - logFile_->currentLine_ = 2; - uint64_t line = 1; - ChangeData changeData; - auto res = logFile_->PraseLog(line, changeData); - EXPECT_EQ(res, E_OK); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "PraseLogTest004 failed"; - } - GTEST_LOG_(INFO) << "PraseLogTest004 end"; -} +// /** +// * @tc.name: PraseLogTest002 +// * @tc.desc: Verify the PraseLog function +// * @tc.type: FUNC +// * @tc.require: NA +// */ +// HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002 TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "PraseLogTest002 start"; +// try { +// logFile_->userId_ = 0; +// logFile_->currentLine_ = 2; +// uint64_t line = 1; +// ChangeData changeData; +// auto res = logFile_->PraseLog(line, changeData); +// EXPECT_EQ(res, -1); +// } catch (...) { +// EXPECT_TRUE(false); +// GTEST_LOG_(INFO) << "PraseLogTest002 failed"; +// } +// GTEST_LOG_(INFO) << "PraseLogTest002 end"; +// } + +// /** +// * @tc.name: PraseLogTest003 +// * @tc.desc: Verify the PraseLog function +// * @tc.type: FUNC +// * @tc.require: NA +// */ +// HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "PraseLogTest003 start"; +// try { +// logFile_->userId_ = 1; +// logFile_->currentLine_ = 1; +// uint64_t line = 1; +// ChangeData changeData; +// auto res = logFile_->PraseLog(line, changeData); +// EXPECT_EQ(res, E_END_OF_LOGFILE); +// } catch (...) { +// EXPECT_TRUE(false); +// GTEST_LOG_(INFO) << "PraseLogTest003 failed"; +// } +// GTEST_LOG_(INFO) << "PraseLogTest003 end"; +// } + +// /** +// * @tc.name: PraseLogTest004 +// * @tc.desc: Verify the PraseLog function +// * @tc.type: FUNC +// * @tc.require: NA +// */ +// HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest004 TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "PraseLogTest004 start"; +// try { +// logFile_->userId_ = 1; +// logFile_->currentLine_ = 2; +// uint64_t line = 1; +// ChangeData changeData; +// auto res = logFile_->PraseLog(line, changeData); +// EXPECT_EQ(res, E_OK); +// } catch (...) { +// EXPECT_TRUE(false); +// GTEST_LOG_(INFO) << "PraseLogTest004 failed"; +// } +// GTEST_LOG_(INFO) << "PraseLogTest004 end"; +// } @@ -696,10 +721,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceUnlinkLogTest001, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceUnlinkLogTest001 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string name = "name"; string childRecordId = "childRecordId"; - auto res = logFile_->ProduceUnlinkLog(parentMetaFile, name, childRecordId) + auto res = logFile_->ProduceUnlinkLog(parentMetaFile, name, childRecordId); EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); @@ -719,10 +744,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceUnlinkLogTest002, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceUnlinkLogTest001 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string name = "name"; string childRecordId = ""; - auto res = logFile_->ProduceUnlinkLog(parentMetaFile, name, childRecordId) + auto res = logFile_->ProduceUnlinkLog(parentMetaFile, name, childRecordId); EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); @@ -742,10 +767,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameOldLogTest001, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameOldLogTest001 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string name = "name"; string childRecordId = "childRecordId"; - auto res = logFile_->ProduceRenameOldLog(parentMetaFile, name, childRecordId) + auto res = logFile_->ProduceRenameOldLog(parentMetaFile, name, childRecordId); EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); @@ -765,10 +790,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameOldLogTest002, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameOldLogTest002 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string name = "name"; string childRecordId = ""; - auto res = logFile_->ProduceRenameOldLog(parentMetaFile, name, childRecordId) + auto res = logFile_->ProduceRenameOldLog(parentMetaFile, name, childRecordId); EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); @@ -788,12 +813,16 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest001, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameNewLogTest001 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - auto res = logFile_->ProduceRenameNewLog(parentMetaFile, name, childRecordId) - EXPECT_EQ(res, -1); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { + errno = ENOENT; + return 1; + }) + auto res = logFile_->ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, ENOENT); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ProduceRenameNewLogTest001 failed"; @@ -801,9 +830,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest001, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameNewLogTest001 end"; } -#undef S_ISDIR -#define S_ISDIR(mode) (0) - /** * @tc.name: ProduceRenameNewLogTest002 * @tc.desc: Verify the ProduceRenameNewLog function @@ -815,12 +841,13 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest002, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameNewLogTest002 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = ""; - auto res = logFile_->ProduceRenameNewLog(parentMetaFile, name, childRecordId) - EXPECT_EQ(res, E_OK); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + auto res = logFile_->ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ProduceRenameNewLogTest002 failed"; @@ -828,9 +855,6 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest002, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameNewLogTest002 end"; } -#undef S_ISDIR -#define S_ISDIR(mode) (1) - /** * @tc.name: ProduceRenameNewLogTest003 * @tc.desc: Verify the ProduceRenameNewLog function @@ -842,11 +866,14 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest003, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameNewLogTest003 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = ""; - auto res = logFile_->ProduceRenameNewLog(parentMetaFile, name, childRecordId) + struct stat statInfo; + statInfo.st_ino = S_IFREG; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); + auto res = logFile_->ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); @@ -855,6 +882,33 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest003, TestSize.Level GTEST_LOG_(INFO) << "ProduceRenameNewLogTest003 end"; } +/** + * @tc.name: ProduceRenameNewLogTest004 + * @tc.desc: Verify the ProduceRenameNewLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest004 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = ""; + struct stat statInfo; + statInfo.st_ino = S_IFDIR; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); + auto res = logFile_->ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest004 failed"; + } + GTEST_LOG_(INFO) << "ProduceRenameNewLogTest004 end"; +} + /** * @tc.name: ProduceCloseAndWriteTest001 * @tc.desc: Verify the ProduceCloseAndWrite function @@ -866,12 +920,16 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest001, TestSize.Leve GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, name, childRecordId) - EXPECT_EQ(res, -1); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { + errno = ENOENT; + return 1; + }) + auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, ENOENT); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 failed"; @@ -893,9 +951,10 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest002, TestSize.Leve make_shared(); string path = "path"; string name = "name"; - string childRecordId = ""; - auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, name, childRecordId) - EXPECT_EQ(res, E_OK); + string childRecordId = "childRecordId"; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, path, name, childRecordId) + EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 failed"; @@ -903,6 +962,31 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest002, TestSize.Leve GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 end"; } +/** + * @tc.name: ProduceCloseAndWriteTest003 + * @tc.desc: Verify the ProduceCloseAndWrite function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest003 start"; + try { + shared_ptr parentMetaFile = + make_shared(); + string path = "path"; + string name = "name"; + string childRecordId = ""; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, path, name, childRecordId) + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest003 failed"; + } + GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest003 end"; +} + class LogFileMgrTest : public testing::Test { public: static void SetUpTestCase(void); @@ -910,16 +994,21 @@ public: void SetUp(); void TearDown(); static inline shared_ptr logFileMgr_; -} + static inline shared_ptr insMock_; +}; void LogFileMgrTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; + insMock_ = make_shared(); + Assistant->ins = insMock_; logFileMgr_ = make_shared(); } void LogFileMgrTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; logFileMgr_ = nullptr; + Assistant->ins = nullptr; + insMock_ = nullptr; } void LogFileMgrTest::SetUp() { @@ -941,8 +1030,6 @@ HWTEST_F(LogFileMgrTest, GetInstanceTest001, TestSize.Level1) GTEST_LOG_(INFO) << "GetInstanceTest001 start"; try { LogFileMgr& instance1 = LogFileMgr::GetInstance(); - LogFileMgr& instance2 = LogFileMgr::GetInstance(); - EXPECT_EQ(instance1, instance2); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GetInstanceTest001 failed"; @@ -991,8 +1078,12 @@ HWTEST_F(LogFileMgrTest, RegisterSyncFolderTest001, TestSize.Level1) uint32_t userId = 1; uint32_t syncFolderIndex = 2; string path = "path"; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { + errno = ENOENT; + return 1; + }); auto res = logFileMgr_->RegisterSyncFolder(userId, syncFolderIndex, path); - EXPECT_EQ(res, E_OK); + EXPECT_EQ(res, ENOENT); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 failed"; @@ -1000,6 +1091,30 @@ HWTEST_F(LogFileMgrTest, RegisterSyncFolderTest001, TestSize.Level1) GTEST_LOG_(INFO) << "RegisterSyncFolderTest001 end"; } +// /** +// * @tc.name: RegisterSyncFolderTest002 +// * @tc.desc: Verify the RegisterSyncFolder function +// * @tc.type: FUNC +// * @tc.require: NA +// */ +// HWTEST_F(LogFileMgrTest, RegisterSyncFolderTest002, TestSize.Level1) +// { +// GTEST_LOG_(INFO) << "RegisterSyncFolderTest002 start"; +// try { +// uint32_t userId = 1; +// uint32_t syncFolderIndex = 2; +// string path = "path"; +// EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)) +// .WillOnce(Return(1)); +// auto res = logFileMgr_->RegisterSyncFolder(userId, syncFolderIndex, path); +// EXPECT_EQ(res, E_OK); +// } catch (...) { +// EXPECT_TRUE(false); +// GTEST_LOG_(INFO) << "RegisterSyncFolderTest002 failed"; +// } +// GTEST_LOG_(INFO) << "RegisterSyncFolderTest002 end"; +// } + /** * @tc.name: UnRegisterSyncFolderTest001 * @tc.desc: Verify the UnRegisterSyncFolder function @@ -1033,6 +1148,7 @@ HWTEST_F(LogFileMgrTest, RegisterSyncFolderChangesTest001, TestSize.Level1) try { uint32_t userId = 1; uint32_t syncFolderIndex = 2; + logFileMgr_->registerChangeCount_ = 0; logFileMgr_->RegisterSyncFolderChanges(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); @@ -1041,6 +1157,48 @@ HWTEST_F(LogFileMgrTest, RegisterSyncFolderChangesTest001, TestSize.Level1) GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest001 end"; } +/** + * @tc.name: RegisterSyncFolderChangesTest002 + * @tc.desc: Verify the RegisterSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, RegisterSyncFolderChangesTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest002 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + logFileMgr_->registerChangeCount_ = 1; + logFileMgr_->RegisterSyncFolderChanges(userId, syncFolderIndex); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest002 failed"; + } + GTEST_LOG_(INFO) << "RegisterSyncFolderChangesTest002 end"; +} + +/** + * @tc.name: UnRegisterSyncFolderChangesTest001 + * @tc.desc: Verify the UnRegisterSyncFolderChanges function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderChangesTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 2; + logFileMgr_->registerChangeCount_ = 1; + logFileMgr_->UnRegisterSyncFolderChanges(userId, syncFolderIndex); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 failed"; + } + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 end"; +} + /** * @tc.name: UnRegisterSyncFolderChangesTest001 * @tc.desc: Verify the UnRegisterSyncFolderChanges function @@ -1053,6 +1211,7 @@ HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderChangesTest001, TestSize.Level1) try { uint32_t userId = 1; uint32_t syncFolderIndex = 2; + logFileMgr_->registerChangeCount_ = 2; logFileMgr_->UnRegisterSyncFolderChanges(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); @@ -1071,13 +1230,13 @@ HWTEST_F(LogFileMgrTest, GetCloudDiskServiceLogFileTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest001 start"; try { - logFileMgr_->logFiles_.clear(); + logFileMgr_->LogFiles_.clear(); uint32_t userId = 1; uint32_t syncFolderIndex = 2; auto res = logFileMgr_->GetCloudDiskServiceLogFile(userId, syncFolderIndex); EXPECT_EQ(res->userId_, userId); EXPECT_EQ(res->syncFolderIndex_, syncFolderIndex); - EXPECT_NE(logFileMgr_->logFiles_.empty()); + EXPECT_FALSE(logFileMgr_->LogFiles_.empty()); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest001 failed"; @@ -1095,12 +1254,12 @@ HWTEST_F(LogFileMgrTest, GetCloudDiskServiceLogFileTest002, TestSize.Level1) { GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest002 start"; try { - logFileMgr_->logFiles_.clear(); + logFileMgr_->LogFiles_.clear(); uint32_t userId = 3; uint32_t syncFolderIndex = 4; LogFileKey key(userId, syncFolderIndex); - shared_ptr newLogFile = make_shared(); - logFiles_.insert(key, newLogFile); + shared_ptr newLogFile = make_shared(0, 0); + logFileMgr_->LogFiles_[key] = newLogFile; auto res = logFileMgr_->GetCloudDiskServiceLogFile(userId, syncFolderIndex); EXPECT_EQ(res, newLogFile) } catch (...) { @@ -1110,6 +1269,24 @@ HWTEST_F(LogFileMgrTest, GetCloudDiskServiceLogFileTest002, TestSize.Level1) GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest002 end"; } +/** + * @tc.name: CallBackTest001 + * @tc.desc: Verify the CallBack function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, CallBackTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "CallBackTest001 start"; + try { + logFileMgr_->CallBack(nullptr); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "CallBackTest001 failed"; + } + GTEST_LOG_(INFO) << "CallBackTest001 end"; +} + /** * @tc.name: OnDataChangeTest001 * @tc.desc: Verify the OnDataChange function -- Gitee From 254f3a874a40bd9d91c55f21282846a4fa7a3e8f Mon Sep 17 00:00:00 2001 From: yujiahe Date: Thu, 11 Sep 2025 21:14:39 +0800 Subject: [PATCH 18/26] fix Signed-off-by: yujiahe --- ...cloud_disk_service_logfile_static_test.cpp | 46 ++++++++++++++++++- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp index d7bd53c01..5ed555187 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp @@ -178,7 +178,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest003, TestSize.Lev try { int fd = 1; uint32_t bucket = 1; - EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).Time(2).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).Times(2).WillOnce(Return(0)); EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(LOGGROUP_SIZE)); auto res = LoadCurrentPage(fd, bucket); EXPECT_NE(res, nullptr); @@ -189,6 +189,26 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest003, TestSize.Lev GTEST_LOG_(INFO) << "LoadCurrentPageTest003 end"; } +/** + * @tc.name: UnlockCurrentPageTest001 + * @tc.desc: Verify the UnlockCurrentPage function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, UnlockCurrentPageTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "UnlockCurrentPageTest001 start"; + try { + int fd = 1; + uint32_t bucket = 1; + UnlockCurrentPage(fd, bucket); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "UnlockCurrentPageTest001 failed"; + } + GTEST_LOG_(INFO) << "UnlockCurrentPageTest001 end"; +} + /** * @tc.name: SyncCurrentPageTest001 * @tc.desc: Verify the SyncCurrentPage function @@ -252,7 +272,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, SyncCurrentPageTest003, TestSize.Lev EXPECT_CALL(*insMock_, WriteFile(_, _, _, _)).WillOnce(Return(LOGGROUP_SIZE)); EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(0)); auto res = SyncCurrentPage(logGroup, fd, line); - EXPECT_NE(res, E_OK); + EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "SyncCurrentPageTest003 failed"; @@ -282,4 +302,26 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, FillLogGroupTest001, TestSize.Level1 } GTEST_LOG_(INFO) << "FillLogGroupTest001 end"; } + +/** + * @tc.name: GetCurrentLineTest001 + * @tc.desc: Verify the GetCurrentLine function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileStaticTest, GetCurrentLineTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "GetCurrentLineTest001 start"; + try { + int fd = 1; + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillOnce(Return(1)) + .WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillRepeatedly(Return(4096)); + GetCurrentLine(fd); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "GetCurrentLineTest001 failed"; + } + GTEST_LOG_(INFO) << "GetCurrentLineTest001 end"; +} } \ No newline at end of file -- Gitee From e5456dd4a061a678e62c8152596ecb6d9b204005 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Thu, 11 Sep 2025 21:59:07 +0800 Subject: [PATCH 19/26] fix Signed-off-by: yujiahe --- test/unittests/clouddiskservice/BUILD.gn | 21 +- .../mock/cloud_disk_service_metafile_mock.cpp | 6 +- ...cloud_disk_service_logfile_static_test.cpp | 2 +- .../cloud_disk_service_logfile_test.cpp | 435 ++++++++++++++---- 4 files changed, 352 insertions(+), 112 deletions(-) diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index 2c832f112..4977a7ef6 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -17,9 +17,9 @@ import("//foundation/filemanagement/dfs_service/distributedfile.gni") ohos_unittest("cloud_disk_service_logfile_static_test") { module_out_path = "dfs_service/dfs_service" - source = [ + sources = [ "${distributedfile_path}/test/unittests/clouddiskservice/mock/system_function_mock.cpp", - "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + "${services_path}/clouddiskservice/sync_folder/src/convertor.cpp", "${distributedfile_path}/utils/log/src/utils_log.cpp", "${distributedfile_path}/utils/system/src/utils_directory.cpp", "${distributedfile_path}/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp", @@ -54,7 +54,7 @@ ohos_unittest("cloud_disk_service_logfile_static_test") { "ffrt:libffrt", "json:nlohmann_json_static", "libfuse:libfuse", - "hisysevent:libsysevent", + "hisysevent:libhisysevent", ] defines = [ @@ -78,8 +78,8 @@ ohos_unittest("cloud_disk_service_logfile_static_test") { ohos_unittest("cloud_disk_service_logfile_test") { module_out_path = "dfs_service/dfs_service" - source = [ - "${services_path}/clouddiskservice/sync_folder/src/converter.cpp", + sources = [ + "${services_path}/clouddiskservice/sync_folder/src/convertor.cpp", "${services_path}/clouddiskservice/ipc/src/cloud_disk_service_callback_manager.cpp", "${distributedfile_path}/frameworks/native/clouddiskservice_kit_inner/src/cloud_disk_common.cpp", "${distributedfile_path}/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.cpp", @@ -90,17 +90,16 @@ ohos_unittest("cloud_disk_service_logfile_test") { ] include_dirs = [ - "${services_path}/clouddiskservice/sync_folder/src", "${services_path}/clouddiskservice/sync_folder/include", - "${distributedfile_path}/test/unittests/clouddiskservice/mock", + "${services_path}/clouddiskservice/sync_folder/src", "${innerkits_native_path}/clouddiskservice_kit_inner", "${services_path}/clouddiskservice/monitor/include", "${services_path}/clouddiskservice/ipc/include", "${services_path}/clouddiskservice/utils/include", "${distributedfile_path}/utils/inner_api", - "${distributedfile_path}/utils/system/include", "${distributedfile_path}/utils/dentry/include", - "${distributedfile_path}/utils/log/include", + "${distributedfile_path}/utils/system/include", + "${distributedfile_path}/test/unittests/clouddiskservice/mock", ] deps = [ @@ -126,7 +125,7 @@ ohos_unittest("cloud_disk_service_logfile_test") { public_configs = [ "${services_path}/clouddiskservice:cloud_disk_service_public_config", ] - + use_exceptions = true } @@ -257,6 +256,8 @@ ohos_unittest("convertor_test") { group("clouddisk_service_test") { testonly = true deps = [ + ":cloud_disk_service_logfile_static_test", + ":cloud_disk_service_logfile_test", ":cloud_disk_service_metafile_test", ":cloud_disk_service_syncfolder_test", ":convertor_test", diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp index 6fbdb21a0..b49031fc2 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp @@ -91,6 +91,10 @@ int32_t CloudDiskServiceMetaFile::GenericDentryHeader() int32_t CloudDiskServiceMetaFile::DoCreate(const MetaBase &base) { - return E_OK; + if (base.name == "") { + return E_OK; + } else { + return 1; + } } } \ No newline at end of file diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp index 5ed555187..ec700847d 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_static_test.cpp @@ -178,7 +178,7 @@ HWTEST_F(CloudDiskServiceLogFileStaticTest, LoadCurrentPageTest003, TestSize.Lev try { int fd = 1; uint32_t bucket = 1; - EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).Times(2).WillOnce(Return(0)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillRepetedly(Return(0)); EXPECT_CALL(*insMock_, ReadFile(_, _, _, _)).WillOnce(Return(LOGGROUP_SIZE)); auto res = LoadCurrentPage(fd, bucket); EXPECT_NE(res, nullptr); diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp index fb1a55fbe..b4e204e0a 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -581,37 +581,29 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest003, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceLogTest003 end"; } -// /** -// * @tc.name: ProduceLogTest004 -// * @tc.desc: Verify the ProduceLog function -// * @tc.type: FUNC -// * @tc.require: NA -// */ -// HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest004, TestSize.Level1) -// { -// GTEST_LOG_(INFO) << "ProduceLogTest004 start"; -// try { -// EventInfo eventInfo; -// eventInfo.operateType = static_cast(OperationType::CREATE); -// EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); -// // productlogforoperation actions -// auto res = logFile_->ProduceLog(eventInfo); -// EXPECT_EQ(res, E_OK); -// } catch (...) { -// EXPECT_TRUE(false); -// GTEST_LOG_(INFO) << "ProduceLogTest004 failed"; -// } -// GTEST_LOG_(INFO) << "ProduceLogTest004 end"; +/** + * @tc.name: ProduceLogTest004 + * @tc.desc: Verify the ProduceLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogTest004 start"; + try { + EventInfo eventInfo; + eventInfo.operateType = static_cast(OperationType::OPERATION_MAX); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + // productlogforoperation actions + auto res = logFile_->ProduceLog(eventInfo); + EXPECT_EQ(res, EINVAL); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogTest004 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogTest004 end"; } - - - - - - - - /** * @tc.name: PraseLogTest001 * @tc.desc: Verify the PraseLog function @@ -622,11 +614,11 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest001, TestSize.Level1) { GTEST_LOG_(INFO) << "PraseLogTest001 start"; try { - logFile_->currentLine_ = 0; + logFile_->currentLine_ = 1; uint64_t line = 1; - ChangeData changeData; - auto res = logFile_->PraseLog(line, changeData); - EXPECT_EQ(res, E_OUT_OF_RANGE); + ChangeData data; + auto res = logFile_->PraseLog(line, data); + EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "PraseLogTest001 failed"; @@ -634,81 +626,324 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest001, TestSize.Level1) GTEST_LOG_(INFO) << "PraseLogTest001 end"; } -// /** -// * @tc.name: PraseLogTest002 -// * @tc.desc: Verify the PraseLog function -// * @tc.type: FUNC -// * @tc.require: NA -// */ -// HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002 TestSize.Level1) -// { -// GTEST_LOG_(INFO) << "PraseLogTest002 start"; -// try { -// logFile_->userId_ = 0; -// logFile_->currentLine_ = 2; -// uint64_t line = 1; -// ChangeData changeData; -// auto res = logFile_->PraseLog(line, changeData); -// EXPECT_EQ(res, -1); -// } catch (...) { -// EXPECT_TRUE(false); -// GTEST_LOG_(INFO) << "PraseLogTest002 failed"; -// } -// GTEST_LOG_(INFO) << "PraseLogTest002 end"; -// } +/** + * @tc.name: PraseLogTest002 + * @tc.desc: Verify the PraseLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "PraseLogTest002 start"; + try { + logFile_->currentLine_ = 0; + uint64_t line = 1; + ChangeData data; + auto res = logFile_->PraseLog(line, data); + EXPECT_EQ(res, E_OUT_OF_RANGE); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "PraseLogTest002 failed"; + } + GTEST_LOG_(INFO) << "PraseLogTest002 end"; +} -// /** -// * @tc.name: PraseLogTest003 -// * @tc.desc: Verify the PraseLog function -// * @tc.type: FUNC -// * @tc.require: NA -// */ -// HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) -// { -// GTEST_LOG_(INFO) << "PraseLogTest003 start"; -// try { -// logFile_->userId_ = 1; -// logFile_->currentLine_ = 1; -// uint64_t line = 1; -// ChangeData changeData; -// auto res = logFile_->PraseLog(line, changeData); -// EXPECT_EQ(res, E_END_OF_LOGFILE); -// } catch (...) { -// EXPECT_TRUE(false); -// GTEST_LOG_(INFO) << "PraseLogTest003 failed"; -// } -// GTEST_LOG_(INFO) << "PraseLogTest003 end"; -// } +/** + * @tc.name: PraseLogTest003 + * @tc.desc: Verify the PraseLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) +{ + GTEST_LOG_(INFO) << "PraseLogTest003 start"; + try { + logFile_->userId_ = 1; + logFile_->currentLine_ = 1; + uint64_t line = 1; + ChangeData data; + auto res = logFile_->PraseLog(line, data); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "PraseLogTest003 failed"; + } + GTEST_LOG_(INFO) << "PraseLogTest003 end"; +} -// /** -// * @tc.name: PraseLogTest004 -// * @tc.desc: Verify the PraseLog function -// * @tc.type: FUNC -// * @tc.require: NA -// */ -// HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest004 TestSize.Level1) -// { -// GTEST_LOG_(INFO) << "PraseLogTest004 start"; -// try { -// logFile_->userId_ = 1; -// logFile_->currentLine_ = 2; -// uint64_t line = 1; -// ChangeData changeData; -// auto res = logFile_->PraseLog(line, changeData); -// EXPECT_EQ(res, E_OK); -// } catch (...) { -// EXPECT_TRUE(false); -// GTEST_LOG_(INFO) << "PraseLogTest004 failed"; -// } -// GTEST_LOG_(INFO) << "PraseLogTest004 end"; -// } +/** + * @tc.name: ProduceLogForOperateTest001 + * @tc.desc: Verify the ProduceLogForOperate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogForOperateTest001 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + uint8_t operator = static_cast(OperationType::Create); + logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogForOperateTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogForOperateTest001 end"; +} +/** + * @tc.name: ProduceLogForOperateTest002 + * @tc.desc: Verify the ProduceLogForOperate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogForOperateTest002 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + uint8_t operator = static_cast(OperationType::DELETE); + logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogForOperateTest002 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogForOperateTest002 end"; +} +/** + * @tc.name: ProduceLogForOperateTest003 + * @tc.desc: Verify the ProduceLogForOperate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogForOperateTest003 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + uint8_t operator = static_cast(OperationType::MOVE_FROM); + logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogForOperateTest003 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogForOperateTest003 end"; +} +/** + * @tc.name: ProduceLogForOperateTest004 + * @tc.desc: Verify the ProduceLogForOperate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogForOperateTest004 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + uint8_t operator = static_cast(OperationType::MOVE_TO); + logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogForOperateTest004 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogForOperateTest004 end"; +} +/** + * @tc.name: ProduceLogForOperateTest005 + * @tc.desc: Verify the ProduceLogForOperate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest005, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogForOperateTest005 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + uint8_t operator = static_cast(OperationType::CLOSE_WRITE); + logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogForOperateTest005 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogForOperateTest005 end"; +} +/** + * @tc.name: ProduceLogForOperateTest006 + * @tc.desc: Verify the ProduceLogForOperate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest006, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogForOperateTest006 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + uint8_t operator = static_cast(OperationType::SYNC_FOLDER_INVALID); + logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogForOperateTest006 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogForOperateTest006 end"; +} +/** + * @tc.name: ProduceLogForOperateTest007 + * @tc.desc: Verify the ProduceLogForOperate function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest007, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceLogForOperateTest007 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + uint8_t operator = static_cast(OperationType::OPERATION_MAX); + logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceLogForOperateTest007 failed"; + } + GTEST_LOG_(INFO) << "ProduceLogForOperateTest007 end"; +} +/** + * @tc.name: ProduceCreateLogTest001 + * @tc.desc: Verify the ProduceCreateLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCreateLogTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceCreateLogTest001 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { + errno = ENOENT; + return 1; + }); + auto res = logFile_->ProduceCreateLog(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, ENOENT); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceCreateLogTest001 failed"; + } + GTEST_LOG_(INFO) << "ProduceCreateLogTest001 end"; +} + +/** + * @tc.name: ProduceCreateLogTest002 + * @tc.desc: Verify the ProduceCreateLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCreateLogTest002, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceCreateLogTest002 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = "name"; + string childRecordId = "childRecordId"; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); + auto res = logFile_->ProduceCreateLog(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, -1); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceCreateLogTest002 failed"; + } + GTEST_LOG_(INFO) << "ProduceCreateLogTest002 end"; +} + +/** + * @tc.name: ProduceCreateLogTest003 + * @tc.desc: Verify the ProduceCreateLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCreateLogTest003, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceCreateLogTest003 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = ""; + string childRecordId = "childRecordId"; + struct stat statInfo; + statInfo.st_ino = S_IFREG; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); + auto res = logFile_->ProduceCreateLog(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceCreateLogTest003 failed"; + } + GTEST_LOG_(INFO) << "ProduceCreateLogTest003 end"; +} + +/** + * @tc.name: ProduceCreateLogTest004 + * @tc.desc: Verify the ProduceCreateLog function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCreateLogTest004, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "ProduceCreateLogTest004 start"; + try { + shared_ptr parentMetaFile = + make_shared(0, 0, 0); + string path = "path"; + string name = ""; + string childRecordId = "childRecordId"; + struct stat statInfo; + statInfo.st_ino = S_IFDIR; + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); + auto res = logFile_->ProduceCreateLog(parentMetaFile, path, name, childRecordId); + EXPECT_EQ(res, E_OK); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "ProduceCreateLogTest004 failed"; + } + GTEST_LOG_(INFO) << "ProduceCreateLogTest004 end"; +} /** * @tc.name: ProduceUnlinkLogTest001 @@ -927,7 +1162,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest001, TestSize.Leve EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { errno = ENOENT; return 1; - }) + }); auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, ENOENT); } catch (...) { -- Gitee From bec3f7b4eb74c15bb994ad6dca0ea8bf25ffcbfe Mon Sep 17 00:00:00 2001 From: yujiahe Date: Thu, 11 Sep 2025 22:46:43 +0800 Subject: [PATCH 20/26] fix Signed-off-by: yujiahe --- .../mock/cloud_disk_service_metafile_mock.cpp | 9 +- .../cloud_disk_service_logfile_test.cpp | 229 ++++++++++-------- 2 files changed, 130 insertions(+), 108 deletions(-) diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp index b49031fc2..162041225 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_metafile_mock.cpp @@ -25,7 +25,7 @@ CloudDiskServiceMetaFile::CloudDiskServiceMetaFile(const uint32_t userId, const userId_ = userId; } -MetaFileMgr& MetaFile::GetInstance() +MetaFileMgr& MetaFileMgr::GetInstance() { static MetaFileMgr instance_; return instance_; @@ -75,7 +75,7 @@ int32_t MetaFileMgr::GetRelativePath(const std::shared_ptrWriteLogFile(logBlock); - EXPECT_EQ(res, -1); + EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "WriteLogFileTest002 failed"; @@ -141,7 +141,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ReadLogFileTest002, TestSize.Level1) try { uint64_t line = 1; LogBlock logBlock; - EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillRepeatedly(Return(1)); + EXPECT_CALL(*insMock_, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); EXPECT_CALL(*insMock_, ReadFile(_, _, _,_)).WillRepeatedly(Return(4096)); int32_t res = logFile_->ReadLogFile(line, logBlock); EXPECT_EQ(res, E_OK); @@ -367,7 +367,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, FillChildForDirTest002, TestSize.Level1) uint64_t timestamp = 1; struct stat statInfo; statInfo.st_mode = S_IFREG; - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(SetArgPointee<1>(statInfo), Return(0)); + EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); int32_t res = logFile_->FillChildForDir(path, timestamp); EXPECT_EQ(res, E_OK); @@ -593,8 +593,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogTest004, TestSize.Level1) try { EventInfo eventInfo; eventInfo.operateType = static_cast(OperationType::OPERATION_MAX); - EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - // productlogforoperation actions + EXPECT_CALL(*insMock_, MockStat(_, _)).WillRepeatedly(Return(0)); auto res = logFile_->ProduceLog(eventInfo); EXPECT_EQ(res, EINVAL); } catch (...) { @@ -617,7 +616,8 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest001, TestSize.Level1) logFile_->currentLine_ = 1; uint64_t line = 1; ChangeData data; - auto res = logFile_->PraseLog(line, data); + bool isEof = true; + auto res = logFile_->PraseLog(line, data, isEof); EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); @@ -639,7 +639,8 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002, TestSize.Level1) logFile_->currentLine_ = 0; uint64_t line = 1; ChangeData data; - auto res = logFile_->PraseLog(line, data); + bool isEof = true; + auto res = logFile_->PraseLog(line, data, isEof); EXPECT_EQ(res, E_OUT_OF_RANGE); } catch (...) { EXPECT_TRUE(false); @@ -654,7 +655,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002, TestSize.Level1) * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003, TestSize.Level1) { GTEST_LOG_(INFO) << "PraseLogTest003 start"; try { @@ -662,7 +663,8 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) logFile_->currentLine_ = 1; uint64_t line = 1; ChangeData data; - auto res = logFile_->PraseLog(line, data); + bool isEof = true; + auto res = logFile_->PraseLog(line, data, isEof); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "PraseLogTest003 failed"; @@ -671,171 +673,171 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest003 TestSize.Level1) } /** - * @tc.name: ProduceLogForOperateTest001 - * @tc.desc: Verify the ProduceLogForOperate function + * @tc.name: ProductLogForOperateTest001 + * @tc.desc: Verify the ProductLogForOperate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProductLogForOperateTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceLogForOperateTest001 start"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest001 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - uint8_t operator = static_cast(OperationType::Create); - logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + uint8_t operator1 = static_cast(OperationType::CREATE); + logFile_->ProductLogForOperate(parentMetaFile, path, name, childRecordId, operator1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogForOperateTest001 failed"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest001 failed"; } - GTEST_LOG_(INFO) << "ProduceLogForOperateTest001 end"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest001 end"; } /** - * @tc.name: ProduceLogForOperateTest002 - * @tc.desc: Verify the ProduceLogForOperate function + * @tc.name: ProductLogForOperateTest002 + * @tc.desc: Verify the ProductLogForOperate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProductLogForOperateTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceLogForOperateTest002 start"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest002 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - uint8_t operator = static_cast(OperationType::DELETE); - logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + uint8_t operator1 = static_cast(OperationType::DELETE); + logFile_->ProductLogForOperate(parentMetaFile, path, name, childRecordId, operator1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogForOperateTest002 failed"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest002 failed"; } - GTEST_LOG_(INFO) << "ProduceLogForOperateTest002 end"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest002 end"; } /** - * @tc.name: ProduceLogForOperateTest003 - * @tc.desc: Verify the ProduceLogForOperate function + * @tc.name: ProductLogForOperateTest003 + * @tc.desc: Verify the ProductLogForOperate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProductLogForOperateTest003, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceLogForOperateTest003 start"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest003 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - uint8_t operator = static_cast(OperationType::MOVE_FROM); - logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + uint8_t operator1 = static_cast(OperationType::MOVE_FROM); + logFile_->ProductLogForOperate(parentMetaFile, path, name, childRecordId, operator1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogForOperateTest003 failed"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest003 failed"; } - GTEST_LOG_(INFO) << "ProduceLogForOperateTest003 end"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest003 end"; } /** - * @tc.name: ProduceLogForOperateTest004 - * @tc.desc: Verify the ProduceLogForOperate function + * @tc.name: ProductLogForOperateTest004 + * @tc.desc: Verify the ProductLogForOperate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest004, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProductLogForOperateTest004, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceLogForOperateTest004 start"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest004 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - uint8_t operator = static_cast(OperationType::MOVE_TO); - logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + uint8_t operator1 = static_cast(OperationType::MOVE_TO); + logFile_->ProductLogForOperate(parentMetaFile, path, name, childRecordId, operator1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogForOperateTest004 failed"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest004 failed"; } - GTEST_LOG_(INFO) << "ProduceLogForOperateTest004 end"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest004 end"; } /** - * @tc.name: ProduceLogForOperateTest005 - * @tc.desc: Verify the ProduceLogForOperate function + * @tc.name: ProductLogForOperateTest005 + * @tc.desc: Verify the ProductLogForOperate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest005, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProductLogForOperateTest005, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceLogForOperateTest005 start"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest005 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - uint8_t operator = static_cast(OperationType::CLOSE_WRITE); - logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + uint8_t operator1 = static_cast(OperationType::CLOSE_WRITE); + logFile_->ProductLogForOperate(parentMetaFile, path, name, childRecordId, operator1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogForOperateTest005 failed"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest005 failed"; } - GTEST_LOG_(INFO) << "ProduceLogForOperateTest005 end"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest005 end"; } /** - * @tc.name: ProduceLogForOperateTest006 - * @tc.desc: Verify the ProduceLogForOperate function + * @tc.name: ProductLogForOperateTest006 + * @tc.desc: Verify the ProductLogForOperate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest006, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProductLogForOperateTest006, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceLogForOperateTest006 start"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest006 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - uint8_t operator = static_cast(OperationType::SYNC_FOLDER_INVALID); - logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + uint8_t operator1 = static_cast(OperationType::SYNC_FOLDER_INVALID); + logFile_->ProductLogForOperate(parentMetaFile, path, name, childRecordId, operator1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogForOperateTest006 failed"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest006 failed"; } - GTEST_LOG_(INFO) << "ProduceLogForOperateTest006 end"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest006 end"; } /** - * @tc.name: ProduceLogForOperateTest007 - * @tc.desc: Verify the ProduceLogForOperate function + * @tc.name: ProductLogForOperateTest007 + * @tc.desc: Verify the ProductLogForOperate function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceLogForOperateTest007, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProductLogForOperateTest007, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceLogForOperateTest007 start"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest007 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; - uint8_t operator = static_cast(OperationType::OPERATION_MAX); - logFile_->ProduceLogForOperate(parentMetaFile, path, name, childRecordId, operator); + uint8_t operator1 = static_cast(OperationType::OPERATION_MAX); + logFile_->ProductLogForOperate(parentMetaFile, path, name, childRecordId, operator1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceLogForOperateTest007 failed"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest007 failed"; } - GTEST_LOG_(INFO) << "ProduceLogForOperateTest007 end"; + GTEST_LOG_(INFO) << "ProductLogForOperateTest007 end"; } /** @@ -1055,7 +1057,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest001, TestSize.Level EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce([&]() { errno = ENOENT; return 1; - }) + }); auto res = logFile_->ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, ENOENT); } catch (...) { @@ -1079,7 +1081,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest002, TestSize.Level make_shared(0, 0, 0); string path = "path"; string name = "name"; - string childRecordId = ""; + string childRecordId = "childRecordId"; EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); auto res = logFile_->ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, -1); @@ -1145,14 +1147,14 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest004, TestSize.Level } /** - * @tc.name: ProduceCloseAndWriteTest001 - * @tc.desc: Verify the ProduceCloseAndWrite function + * @tc.name: ProduceCloseAndWriteLogTest001 + * @tc.desc: Verify the ProduceCloseAndWriteLog function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteLogTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 start"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest001 start"; try { shared_ptr parentMetaFile = make_shared(0, 0, 0); @@ -1163,63 +1165,63 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest001, TestSize.Leve errno = ENOENT; return 1; }); - auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, path, name, childRecordId); + auto res = logFile_->ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, ENOENT); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 failed"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest001 failed"; } - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest001 end"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest001 end"; } /** - * @tc.name: ProduceCloseAndWriteTest002 - * @tc.desc: Verify the ProduceCloseAndWrite function + * @tc.name: ProduceCloseAndWriteLogTest002 + * @tc.desc: Verify the ProduceCloseAndWriteLog function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteLogTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 start"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest002 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = "childRecordId"; EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, path, name, childRecordId) + auto res = logFile_->ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId) EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 failed"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest002 failed"; } - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest002 end"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest002 end"; } /** - * @tc.name: ProduceCloseAndWriteTest003 - * @tc.desc: Verify the ProduceCloseAndWrite function + * @tc.name: ProduceCloseAndWriteLogTest003 + * @tc.desc: Verify the ProduceCloseAndWriteLog function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteLogTest003, TestSize.Level1) { - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest003 start"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest003 start"; try { shared_ptr parentMetaFile = - make_shared(); + make_shared(0, 0, 0); string path = "path"; string name = "name"; string childRecordId = ""; EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - auto res = logFile_->ProduceCloseAndWrite(parentMetaFile, path, name, childRecordId) + auto res = logFile_->ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId) EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest003 failed"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest003 failed"; } - GTEST_LOG_(INFO) << "ProduceCloseAndWriteTest003 end"; + GTEST_LOG_(INFO) << "ProduceCloseAndWriteLogTest003 end"; } class LogFileMgrTest : public testing::Test { @@ -1235,14 +1237,14 @@ public: void LogFileMgrTest::SetUpTestCase(void) { GTEST_LOG_(INFO) << "SetUpTestCase"; insMock_ = make_shared(); - Assistant->ins = insMock_; + Assistant::ins = insMock_; logFileMgr_ = make_shared(); } void LogFileMgrTest::TearDownTestCase(void) { GTEST_LOG_(INFO) << "TearDownTestCase"; logFileMgr_ = nullptr; - Assistant->ins = nullptr; + Assistant::ins = nullptr; insMock_ = nullptr; } @@ -1292,13 +1294,28 @@ HWTEST_F(LogFileMgrTest, ProduceRequestTest001, TestSize.Level1) GTEST_LOG_(INFO) << "ProduceRequestTest001 end"; } - - - - - - - +/** + * @tc.name: PraseRequestTest001 + * @tc.desc: Verify the PraseRequest function + * @tc.type: FUNC + * @tc.require: NA + */ +HWTEST_F(LogFileMgrTest, PraseRequestTest001, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "PraseRequestTest001 start"; + try { + uint32_t userId = 1; + uint32_t syncFolderIndex = 1; + uint64_t start = 1; + uint64_t count = 1; + ChangeResult changeResult; + logFileMgr_->PraseRequest(userId, syncFolderIndex, start, count, changeResult); + } catch (...) { + EXPECT_TRUE(false); + GTEST_LOG_(INFO) << "PraseRequestTest001 failed"; + } + GTEST_LOG_(INFO) << "PraseRequestTest001 end"; +} /** * @tc.name: RegisterSyncFolderTest001 @@ -1435,14 +1452,14 @@ HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderChangesTest001, TestSize.Level1) } /** - * @tc.name: UnRegisterSyncFolderChangesTest001 + * @tc.name: UnRegisterSyncFolderChangesTest002 * @tc.desc: Verify the UnRegisterSyncFolderChanges function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderChangesTest001, TestSize.Level1) +HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderChangesTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 start"; + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest002 start"; try { uint32_t userId = 1; uint32_t syncFolderIndex = 2; @@ -1450,9 +1467,9 @@ HWTEST_F(LogFileMgrTest, UnRegisterSyncFolderChangesTest001, TestSize.Level1) logFileMgr_->UnRegisterSyncFolderChanges(userId, syncFolderIndex); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 failed"; + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest002 failed"; } - GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest001 end"; + GTEST_LOG_(INFO) << "UnRegisterSyncFolderChangesTest002 end"; } /** @@ -1496,7 +1513,7 @@ HWTEST_F(LogFileMgrTest, GetCloudDiskServiceLogFileTest002, TestSize.Level1) shared_ptr newLogFile = make_shared(0, 0); logFileMgr_->LogFiles_[key] = newLogFile; auto res = logFileMgr_->GetCloudDiskServiceLogFile(userId, syncFolderIndex); - EXPECT_EQ(res, newLogFile) + EXPECT_EQ(res, newLogFile); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "GetCloudDiskServiceLogFileTest002 failed"; -- Gitee From 5e14e550dedbac9c1527f3e42539660ff99f3be0 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Thu, 11 Sep 2025 22:56:55 +0800 Subject: [PATCH 21/26] fix Signed-off-by: yujiahe --- .../mock/cloud_disk_service_manager_mock.h | 2 +- .../sync_folder/cloud_disk_service_logfile_test.cpp | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h index cd5b8e283..e4db1bf35 100644 --- a/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h +++ b/test/unittests/clouddiskservice/mock/cloud_disk_service_manager_mock.h @@ -29,7 +29,7 @@ class CloudDiskServiceManagerMock final : public CloudDiskServiceManager { public: static CloudDiskServiceManagerMock &GetInstance() { - static CloudDiskServiceMock instance; + static CloudDiskServiceManageMock instance; return instance; } diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp index 6ee0e2358..478259fdc 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -1082,6 +1082,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest002, TestSize.Level string path = "path"; string name = "name"; string childRecordId = "childRecordId"; + logFile_->renameRecordId_ = "renameRecordId"; EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); auto res = logFile_->ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, -1); @@ -1107,6 +1108,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceRenameNewLogTest003, TestSize.Level string path = "path"; string name = "name"; string childRecordId = ""; + logFile_->renameRecordId_ = ""; struct stat statInfo; statInfo.st_ino = S_IFREG; EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(DoAll(SetArgPointee<1>(statInfo), Return(0))); @@ -1190,7 +1192,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteLogTest002, TestSize.L string name = "name"; string childRecordId = "childRecordId"; EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - auto res = logFile_->ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId) + auto res = logFile_->ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, -1); } catch (...) { EXPECT_TRUE(false); @@ -1215,7 +1217,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, ProduceCloseAndWriteLogTest003, TestSize.L string name = "name"; string childRecordId = ""; EXPECT_CALL(*insMock_, MockStat(_, _)).WillOnce(Return(0)); - auto res = logFile_->ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId) + auto res = logFile_->ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId); EXPECT_EQ(res, E_OK); } catch (...) { EXPECT_TRUE(false); @@ -1308,7 +1310,7 @@ HWTEST_F(LogFileMgrTest, PraseRequestTest001, TestSize.Level1) uint32_t syncFolderIndex = 1; uint64_t start = 1; uint64_t count = 1; - ChangeResult changeResult; + ChangesResult changeResult; logFileMgr_->PraseRequest(userId, syncFolderIndex, start, count, changeResult); } catch (...) { EXPECT_TRUE(false); -- Gitee From 234085dd3a62047f1f2a86b5c753bd54bf0916b8 Mon Sep 17 00:00:00 2001 From: yujiahe Date: Thu, 11 Sep 2025 23:00:42 +0800 Subject: [PATCH 22/26] fix Signed-off-by: yujiahe --- .../sync_folder/cloud_disk_service_logfile_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp index 478259fdc..52bc7a99c 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_logfile_test.cpp @@ -641,7 +641,7 @@ HWTEST_F(CloudDiskServiceLogFileTest, PraseLogTest002, TestSize.Level1) ChangeData data; bool isEof = true; auto res = logFile_->PraseLog(line, data, isEof); - EXPECT_EQ(res, E_OUT_OF_RANGE); + EXPECT_EQ(res, E_INVALID_CHANGE_SEQUENCE); } catch (...) { EXPECT_TRUE(false); GTEST_LOG_(INFO) << "PraseLogTest002 failed"; -- Gitee From 1c7afe145cc712e773d5064050c556525fca290d Mon Sep 17 00:00:00 2001 From: fuletian Date: Sat, 13 Sep 2025 20:42:16 +0800 Subject: [PATCH 23/26] add_tdd Signed-off-by: fuletian --- test/unittests/clouddiskservice/BUILD.gn | 40 + .../cloud_disk_service_metafile_test.cpp | 1115 ++++------------- 2 files changed, 289 insertions(+), 866 deletions(-) diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index 91beeedd0..65d4ca7f9 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -168,6 +168,45 @@ ohos_unittest("cloud_disk_service_metafile_test") { use_exceptions = true } +ohos_unittest("cloud_disk_service_metafile_static_test") { + module_out_path = "dfs_service/dfs_service" + + sources = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock/system_function_mock.cpp", + "${distributedfile_path}/utils/log/src/utils_log.cpp", + "${services_path}/clouddiskservice/sync_folder/src/convertor.cpp", + "sync_folder/cloud_disk_service_metafile_static_test.cpp", + ] + + include_dirs = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock", + "${distributedfile_path}/utils/dentry/include", + "${services_path}/clouddiskservice/sync_folder/include", + "${services_path}/clouddiskservice/sync_folder/src", + "${services_path}/clouddiskservice/utils/include", + ] + + deps = [ + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "e2fsprogs:libext2_uuid", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "json:nlohmann_json_static", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD004308", + "LOG_TAG=\"CloudDiskService\"", + ] + + use_exceptions = true +} + ohos_unittest("cloud_disk_service_syncfolder_test") { module_out_path = "dfs_service/dfs_service" @@ -293,6 +332,7 @@ group("clouddisk_service_test") { ":cloud_disk_service_logfile_static_test", ":cloud_disk_service_logfile_test", ":cloud_disk_service_metafile_test", + ":cloud_disk_service_metafile_static_test", ":cloud_disk_service_syncfolder_test", ":convertor_test", ":uuid_helper_test", diff --git a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp index cd9fc45a2..598b44369 100644 --- a/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp +++ b/test/unittests/clouddiskservice/sync_folder/cloud_disk_service_metafile_test.cpp @@ -13,14 +13,12 @@ * limitations under the License. */ -#include #include #include "assistant.h" -#include "convertor.h" #include "dfs_error.h" -#include "cloud_disk_service_metafile.h" +#include "cloud_disk_service_metafile.cpp" namespace OHOS::FileManagement::CloudDiskService { using namespace std; @@ -61,1122 +59,507 @@ void CloudDiskServiceMetafileTest::TearDown(void) } /** - * @tc.name: DecodeDentryHeaderTest001 - * @tc.desc: Verify the DecodeDentryHeader function + * @tc.name: GetBucketByLevelTest001 + * @tc.desc: Verify the GetBucketByLevel function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetOverallBucketTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 Start"; + GTEST_LOG_(INFO) << "GetOverallBucketTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.DecodeDentryHeader(); + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t ret = GetOverallBucket(level); EXPECT_EQ(ret, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 ERROR"; - } - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest001 End"; -} - -/** - * @tc.name: DecodeDentryHeaderTest002 - * @tc.desc: Verify the DecodeDentryHeader function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DecodeDentryHeaderTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.DecodeDentryHeader(); - EXPECT_EQ(ret, 1); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 ERROR"; - } - GTEST_LOG_(INFO) << "DecodeDentryHeaderTest002 End"; -} - -/** - * @tc.name: GenericDentryHeaderTest001 - * @tc.desc: Verify the GenericDentryHeader function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 ERROR"; - } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest001 End"; -} - -/** - * @tc.name: GenericDentryHeaderTest002 - * @tc.desc: Verify the GenericDentryHeader function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - struct stat mockStat = { - .st_size = 1, - } - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 ERROR"; - } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest002 End"; -} - -/** - * @tc.name: GenericDentryHeaderTest003 - * @tc.desc: Verify the GenericDentryHeader function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest003, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - struct stat mockStat = { - .st_size = DENTRYGROUP_HEADER, - } - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, 1); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 ERROR"; - } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest003 End"; -} - -/** - * @tc.name: GenericDentryHeaderTest004 - * @tc.desc: Verify the GenericDentryHeader function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest004, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.parentDentryFile_ = "parent_file"; - mFile.selfRecordId_ = "record_123"; - mFile.selfHash_ = 0x12345678; - struct stat mockStat = { - .st_size = 1, - } - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(123)); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, 123); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 ERROR"; - } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest004 End"; -} - -/** - * @tc.name: GenericDentryHeaderTest005 - * @tc.desc: Verify the GenericDentryHeader function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, GenericDentryHeaderTest005, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - string parentDentryFileStr = "parent_file"; - string selfRecordIdStr = "record_123"; - auto selfHashNum = 0x12345678; - mFile.parentDentryFile_ = parentDentryFileStr; - mFile.selfRecordId_ = selfRecordIdStr; - mFile.selfHash_ = selfHashNum; - struct stat mockStat = { - .st_size = DENTRYGROUP_HEADER, - } - - CloudDiskServiceDcacheHeader header = {}; - copy(parentDentryFileStr.begin(), - min(parentDentryFileStr.end(), parentDentryFileStr.begin() + DENTRYFILE_NAME_LEN), - header.parentDentryfile); - copy(selfRecordIdStr.begin(), - min(selfRecordIdStr.end(), selfRecordIdStr.begin() + RECORD_ID_LEN), - header.selfRecordId); - header.selfHash = selfHashNum; - - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)) - .WillOnce(Return(static_cast(sizeof(CloudDiskServiceDcacheHeader)))); - auto ret = mFile.GenericDentryHeader(); - EXPECT_EQ(ret, 123); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 ERROR"; + GTEST_LOG_(INFO) << "GetOverallBucketTest001 ERROR"; } - GTEST_LOG_(INFO) << "GenericDentryHeaderTest005 End"; + GTEST_LOG_(INFO) << "GetOverallBucketTest001 End"; } /** - * @tc.name: DoCreateTest001 - * @tc.desc: Verify the DoCreate function + * @tc.name: GetOverallBucketTest002 + * @tc.desc: Verify the GetOverallBucket function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetOverallBucketTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoCreateTest001 Start"; + GTEST_LOG_(INFO) << "GetOverallBucketTest002 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - base.name = "test_name"; - base.recordId = "record_123"; - auto ret = mFile.DoCreate(base); - EXPECT_EQ(ret, EINVAL); + uint32_t level = 1; + uint32_t ret = GetOverallBucket(level); + EXPECT_EQ(ret, static_cast((1ULL << (level + 1))) - 1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoCreateTest001 ERROR"; + GTEST_LOG_(INFO) << "GetOverallBucketTest002 ERROR"; } - GTEST_LOG_(INFO) << "DoCreateTest001 End"; + GTEST_LOG_(INFO) << "GGetOverallBucketTest002 End"; } /** - * @tc.name: DoCreateTest002 - * @tc.desc: Verify the DoCreate function + * @tc.name: GetDcacheFileSizeTest001 + * @tc.desc: Verify the GetDcacheFileSize function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetDcacheFileSizeTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoCreateTest002 Start"; + GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - string longName(1000,'a'); - MetaBase base; - base.name = longName; - base.recordId = "record_123"; - auto ret = mFile.DoCreate(base); - EXPECT_EQ(ret, ENAMETOOLONG); + uint32_t level = 1; + size_t buckets = GetOverallBucket(level); + size_t ret = GetDcacheFileSize(level); + EXPECT_EQ(ret, buckets * DENTRYGROUP_SIZE * BUCKET_BLOCKS + DENTRYGROUP_HEADER); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoCreateTest002 ERROR"; + GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 ERROR"; } - GTEST_LOG_(INFO) << "DoCreateTest002 End"; + GTEST_LOG_(INFO) << "GetDcacheFileSizeTest001 End"; } /** - * @tc.name: DoCreateTest003 - * @tc.desc: Verify the DoCreate function + * @tc.name: GetBucketByLevelTest001 + * @tc.desc: Verify the GetBucketByLevel function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketByLevelTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoCreateTest003 Start"; + GTEST_LOG_(INFO) << "GetBucketByLevelTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - base.name = "test_name"; - base.recordId = "record_123"; - base.atime = 0; - base.mtime = 0; - base.size = 1024; - base.mode = 0644; - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); - auto ret = mFile.DoCreate(base); + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t ret = GetBucketByLevel(level); EXPECT_EQ(ret, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoCreateTest003 ERROR"; - } - GTEST_LOG_(INFO) << "DoCreateTest003 End"; -} - -/** - * @tc.name: DoCreateTest004 - * @tc.desc: Verify the DoCreate function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest004, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoCreateTest004 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - base.name = "test_name"; - base.recordId = "record_123"; - base.atime = 0; - base.mtime = 0; - base.size = 1024; - base.mode = 0644; - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - auto ret = mFile.DoCreate(base); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoCreateTest004 ERROR"; - } - GTEST_LOG_(INFO) << "DoCreateTest004 End"; -} - -/** - * @tc.name: DoCreateTest005 - * @tc.desc: Verify the DoCreate function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoCreateTest005, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoCreateTest005 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - base.name = "test_name"; - base.recordId = "record_123"; - base.atime = 0; - base.mtime = 0; - base.size = 1024; - base.mode = 0644; - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.DoCreate(base); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoCreateTest005 ERROR"; - } - GTEST_LOG_(INFO) << "DoCreateTest005 End"; -} - -/** - * @tc.name: DoRemoveTest001 - * @tc.desc: Verify the DoRemove function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRemoveTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRemoveTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoRemove(base, recordId); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRemoveTest001 ERROR"; - } - GTEST_LOG_(INFO) << "DoRemoveTest001 End"; -} - -/** - * @tc.name: DoRemoveTest002 - * @tc.desc: Verify the DoRemove function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRemoveTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRemoveTest002 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoRemove(base, recordId); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRemoveTest002Q ERROR"; - } - GTEST_LOG_(INFO) << "DoRemoveTest002 End"; -} - -/** - * @tc.name: DoFlushTest001 - * @tc.desc: Verify the DoFlush function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoFlushTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoFlushTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoRemove(base, recordId); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRemoveTest001 ERROR"; - } - GTEST_LOG_(INFO) << "DoRemoveTest001 End"; -} - -/** - * @tc.name: DoFlushTest002 - * @tc.desc: Verify the DoFlush function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoFlushTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoFlushTest002 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoRemove(base, recordId); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRemoveTest002 ERROR"; - } - GTEST_LOG_(INFO) << "DoRemoveTest002 End"; -} - -/** - * @tc.name: DoUpdateTest001 - * @tc.desc: Verify the DoUpdate function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoUpdateTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoUpdateTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoUpdate(base, recordId); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoUpdateTest001 ERROR"; - } - GTEST_LOG_(INFO) << "DoUpdateTest001 End"; -} - -/** - * @tc.name: DoUpdateTest002 - * @tc.desc: Verify the DoUpdate function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoUpdateTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoUpdateTest002 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoUpdate(base, recordId); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoUpdateTest002 ERROR"; - } - GTEST_LOG_(INFO) << "DoUpdateTest002 End"; -} - -/** - * @tc.name: DoRenameOldTest001 - * @tc.desc: Verify the DoRenameOld function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameOldTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRenameOldTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoRenameOld(base, recordId); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameOldTest001 ERROR"; - } - GTEST_LOG_(INFO) << "DoRenameOldTest001 End"; -} - -/** - * @tc.name: DoRenameOldTest002 - * @tc.desc: Verify the DoRenameOld function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameOldTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRenameOldTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoRenameOld(base, recordId); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameOldTest002 ERROR"; - } - GTEST_LOG_(INFO) << "DoRenameOldTest002 End"; -} - -/** - * @tc.name: DoRenameNewTest001 - * @tc.desc: Verify the DoRenameNew function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRenameNewTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoRenameNew(base, recordId); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameNewTest001 ERROR"; + GTEST_LOG_(INFO) << "GetBucketByLevelTest001 ERROR"; } - GTEST_LOG_(INFO) << "DoRenameNewTest001 End"; + GTEST_LOG_(INFO) << "GetBucketByLevelTest001 End"; } /** - * @tc.name: DoRenameNewTest002 - * @tc.desc: Verify the DoRenameNew function + * @tc.name: GetBucketByLevelTest002 + * @tc.desc: Verify the GetBucketByLevel function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketByLevelTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoRenameNewTest002 Start"; + GTEST_LOG_(INFO) << "GetBucketByLevelTest002 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - string longName(1000, 'a'); - base.name = longName; - string recordId = "record_123"; - auto ret = mFile.DoRenameNew(base, recordId); - EXPECT_EQ(ret, ENAMETOOLONG); + uint32_t level = 1; + uint32_t ret = GetBucketByLevel(level); + EXPECT_EQ(ret, static_cast((1ULL << level))); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameNewTest002 ERROR"; + GTEST_LOG_(INFO) << "GetBucketByLevelTest002 ERROR"; } - GTEST_LOG_(INFO) << "DoRenameNewTest002 End"; + GTEST_LOG_(INFO) << "GetBucketByLevelTest002 End"; } /** - * @tc.name: DoRenameNewTest003 - * @tc.desc: Verify the DoRenameNew function + * @tc.name: GetBucketaddrTest001 + * @tc.desc: Verify the GetBucketaddr function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoRenameNewTest003 Start"; + GTEST_LOG_(INFO) << "GetBucketaddrTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - base.name = "test_name"; - string recordId = "record_123"; - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); - auto ret = mFile.DoRenameNew(base, recordId); + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t buckoffset = 0; + uint32_t ret = GetBucketaddr(level, buckoffset); EXPECT_EQ(ret, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameNewTest003 ERROR"; - } - GTEST_LOG_(INFO) << "DoRenameNewTest003 End"; -} - -/** - * @tc.name: DoRenameNewTest004 - * @tc.desc: Verify the DoRenameNew function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest004, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRenameNewTest004 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - base.name = "test_name"; - string recordId = "record_123"; - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(1)); - auto ret = mFile.DoRenameNew(base, recordId); - EXPECT_EQ(ret, EINVAL); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameNewTest004 ERROR"; + GTEST_LOG_(INFO) << "GetBucketaddrTest001 ERROR"; } - GTEST_LOG_(INFO) << "DoRenameNewTest004 End"; + GTEST_LOG_(INFO) << "GetBucketaddrTest001 End"; } /** - * @tc.name: DoRenameNewTest005 - * @tc.desc: Verify the DoRenameNew function + * @tc.name: GetBucketaddrTest002 + * @tc.desc: Verify the GetBucketaddr function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameNewTest005, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoRenameNewTest005 Start"; + GTEST_LOG_(INFO) << "GetBucketaddrTest002 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - base.name = "test_name"; - string recordId = "record_123"; - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(1)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - auto ret = mFile.DoRenameNew(base, recordId); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameNewTest005 ERROR"; - } - GTEST_LOG_(INFO) << "DoRenameNewTest005 End"; -} - -/** - * @tc.name: DoRenameTest001 - * @tc.desc: Verify the DoRename function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameTest001, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRenameTest001 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - MetaBase base; - string recordId = "record_123"; - auto newMetaFile = make_shared(100, 2, 456); - auto ret = mFile.DoRename(base, recordId, newMetaFile); - EXPECT_EQ(ret, ENOENT); - } catch (...) { - EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameTest001 ERROR"; - } - GTEST_LOG_(INFO) << "DoRenameTest001 End"; -} - -/** - * @tc.name: DoRenameTest002 - * @tc.desc: Verify the DoRename function - * @tc.type: FUNC - * @tc.require: NA - */ -HWTEST_F(CloudDiskServiceMetafileTest, DoRenameTest002, TestSize.Level1) -{ - GTEST_LOG_(INFO) << "DoRenameTest002 Start"; - try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - MetaBase base; - string recordId = "record_123"; - auto newMetaFile = make_shared(100, 2, 456); - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - auto ret = mFile.DoRename(base, recordId, newMetaFile); - EXPECT_EQ(ret, ENOENT); + uint32_t level = 1; + uint64_t curLevelMaxBucks = (1ULL << level); + uint32_t buckoffset = curLevelMaxBucks + 1; + uint32_t ret = GetBucketaddr(level, buckoffset); + EXPECT_EQ(ret, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoRenameTest002 ERROR"; + GTEST_LOG_(INFO) << "GetBucketaddrTest002 ERROR"; } - GTEST_LOG_(INFO) << "DoRenameTest002 End"; + GTEST_LOG_(INFO) << "GetBucketaddrTest002 End"; } /** - * @tc.name: DoLookupByNameTest001 - * @tc.desc: Verify the DoLookupByName function + * @tc.name: GetBucketaddrTest003 + * @tc.desc: Verify the GetBucketaddr function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByNameTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBucketaddrTest003, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoLookupByNameTest001 Start"; + GTEST_LOG_(INFO) << "GetBucketaddrTest003 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoLookupByName(base); - EXPECT_EQ(ret, EINVAL); + uint32_t level = 1; + uint64_t curLevelMaxBucks = (1ULL << level); + uint32_t buckoffset = curLevelMaxBucks - 1; + uint32_t ret = GetBucketaddr(level, buckoffset); + EXPECT_EQ(ret, static_cast(curLevelMaxBucks) + buckoffset - 1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoLookupByNameTest001 ERROR"; + GTEST_LOG_(INFO) << "GetBucketaddrTest003 ERROR"; } - GTEST_LOG_(INFO) << "DoLookupByNameTest001 End"; + GTEST_LOG_(INFO) << "GetBucketaddrTest003 End"; } /** - * @tc.name: DoLookupByNameTest002 - * @tc.desc: Verify the DoLookupByName function + * @tc.name: GetBidxFromLevelTest001 + * @tc.desc: Verify the GetBidxFromLevel function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByNameTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBidxFromLevelTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoLookupByNameTest002 Start"; + GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoLookupByName(base); - EXPECT_EQ(ret, ENOENT); + uint32_t level = MAX_BUCKET_LEVEL + 1; + uint32_t namehash = 1; + unsigned long ret = GetBidxFromLevel(level, namehash); + EXPECT_EQ(ret, E_OK); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoLookupByNameTest002 ERROR"; + GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 ERROR"; } - GTEST_LOG_(INFO) << "DoLookupByNameTest002 End"; + GTEST_LOG_(INFO) << "GetBidxFromLevelTest001 End"; } - /** - * @tc.name: DoLookupByRecordIdTest001 - * @tc.desc: Verify the DoLookupByRecordId function + * @tc.name: GetBidxFromLevelTest002 + * @tc.desc: Verify the GetBidxFromLevel function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByRecordIdTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetBidxFromLevelTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 Start"; + GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoLookupByRecordId(base); - EXPECT_EQ(ret, EINVAL); + uint32_t level = 1; + uint32_t namehash = 1; + uint32_t bucket = GetBucketByLevel(level); + unsigned long ret = GetBidxFromLevel(level, namehash); + EXPECT_EQ(ret, BUCKET_BLOCKS * GetBucketaddr(level, namehash % bucket)); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 ERROR"; + GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 ERROR"; } - GTEST_LOG_(INFO) << "DoLookupByRecordIdTest001 End"; + GTEST_LOG_(INFO) << "GetBidxFromLevelTest002 End"; } /** - * @tc.name: DoLookupByRecordIdTest002 - * @tc.desc: Verify the DoLookupByRecordId function + * @tc.name: FindDentryPageTest001 + * @tc.desc: Verify the FindDentryPage function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, DoLookupByRecordIdTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "DoLookupByRecordIdTest002 Start"; + GTEST_LOG_(INFO) << "FindDentryPageTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(1); - MetaBase base; - string recordId = "record_123"; - auto ret = mFile.DoLookupByRecordId(base); - EXPECT_EQ(ret, ENOENT); + uint64_t index = 0; + auto ctx = std::make_shared(); + auto dentryBlk = std::make_unique(); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(1)); + auto ret = FindDentryPage(index, ctx.get()); + EXPECT_EQ(ret, nullptr); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "DoLookupByRecordIdTest002 ERROR"; + GTEST_LOG_(INFO) << "FindDentryPageTest001 ERROR"; } - GTEST_LOG_(INFO) << "DoLookupByRecordIdTest002 End"; + GTEST_LOG_(INFO) << "FindDentryPageTest001 End"; } /** - * @tc.name: GetCreateInfoTest001 - * @tc.desc: Verify the GetCreateInfo function + * @tc.name: FindDentryPageTest002 + * @tc.desc: Verify the FindDentryPage function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetCreateInfoTest001 Start"; + GTEST_LOG_(INFO) << "FindDentryPageTest002 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - base.name = "test_name"; - uint32_t bitPos = 0; - uint32_t nameHash = 0; - unsigned long bidx = 0; - CloudDiskServiceDentryGroup dentryBlk; - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(DENTRYGROUP_SIZE)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, WriteFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); - auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); - EXPECT_EQ(ret, E_OK); + uint64_t index = 1; + auto ctx = std::make_shared(); + auto dentryBlk = std::make_unique(); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillOnce(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(DENTRYGROUP_SIZE)); + auto ret = FindDentryPage(index, ctx.get()); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetCreateInfoTest001 ERROR"; + GTEST_LOG_(INFO) << "FindDentryPageTest002 ERROR"; } - GTEST_LOG_(INFO) << "GetCreateInfoTest001 End"; + GTEST_LOG_(INFO) << "FindDentryPageTest002 End"; } /** - * @tc.name: GetCreateInfoTest002 - * @tc.desc: Verify the GetCreateInfo function + * @tc.name: FindDentryPageTest003 + * @tc.desc: Verify the FindDentryPage function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindDentryPageTest003, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetCreateInfoTest002 Start"; + GTEST_LOG_(INFO) << "FindDentryPageTest003 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - base.name = "test_name"; - uint32_t bitPos = 0; - uint32_t nameHash = 0; - unsigned long bidx = 0; - CloudDiskServiceDentryGroup dentryBlk; - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); - auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); - EXPECT_EQ(ret, EINVAL); + uint64_t index = 1; + auto ctx = std::make_shared(); + auto dentryBlk = std::make_unique(); + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).Times(2).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); + auto ret = FindDentryPage(index, ctx.get()); + EXPECT_EQ(ret, nullptr); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetCreateInfoTest002 ERROR"; + GTEST_LOG_(INFO) << "FindDentryPageTest003 ERROR"; } - GTEST_LOG_(INFO) << "GetCreateInfoTest002 End"; + GTEST_LOG_(INFO) << "FindDentryPageTest003 End"; } /** - * @tc.name: GetCreateInfoTest003 - * @tc.desc: Verify the GetCreateInfo function + * @tc.name: FindInBlockByIdTest001 + * @tc.desc: Verify the FindInBlockById function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindInBlockByIdTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetCreateInfoTest003 Start"; + GTEST_LOG_(INFO) << "FindInBlockByIdTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - base.name = "test_name"; - uint32_t bitPos = 0; - uint32_t nameHash = 0; - unsigned long bidx = 0; CloudDiskServiceDentryGroup dentryBlk; - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(-1)); - auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); - EXPECT_EQ(ret, -1); + uint32_t namehash = 0; + std::string recordId = "test"; + uint8_t revalidate = 0; + auto ret = FindInBlockById(dentryBlk, namehash, recordId, revalidate); + EXPECT_EQ(ret, nullptr); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetCreateInfoTest003 ERROR"; + GTEST_LOG_(INFO) << "FindInBlockByIdTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetCreateInfoTest003 End"; + GTEST_LOG_(INFO) << "FindInBlockByIdTest001 End"; } /** - * @tc.name: GetCreateInfoTest004 - * @tc.desc: Verify the GetCreateInfo function + * @tc.name: FindInBlockTest001 + * @tc.desc: Verify the FindInBlock function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetCreateInfoTest004, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindInBlockTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetCreateInfoTest003 Start"; + GTEST_LOG_(INFO) << "FindInBlockIdTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - mFile.fd_.Reset(-1); - MetaBase base; - base.name = "test_name"; - uint32_t bitPos = 0; - uint32_t nameHash = 0; - unsigned long bidx = 0; CloudDiskServiceDentryGroup dentryBlk; - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(0)); - EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).WillRepeatedly(Return(0)); - EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillRepeatedly(Return(0)); - auto ret = mFile.GetCreateInfo(base, bitPos, nameHash, bidx, dentryBlk); - EXPECT_EQ(ret, ENOENT); + uint32_t namehash = 0; + std::string name = "testName"; + uint8_t revalidate = 0; + auto ret = FindInBlock(dentryBlk, namehash, name, revalidate); + EXPECT_EQ(ret, nullptr); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetCreateInfoTest004 ERROR"; + GTEST_LOG_(INFO) << "FindInBlockTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetCreateInfoTest004 End"; + GTEST_LOG_(INFO) << "FindInBlockTest001 End"; } /** - * @tc.name: HandleFileByFdTest001 - * @tc.desc: Verify the HandleFileByFd function + * @tc.name: InLevelTest001 + * @tc.desc: Verify the InLevel function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, HandleFileByFdTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, InLevelTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "HandleFileByFdTest001 Start"; + GTEST_LOG_(INFO) << "InLevelTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(Return(-1)); - unsigned long endBlock = 10; - uint32_t level = 0; - auto ret = mFile.HandleFileByFd(endBlock, level); - EXPECT_EQ(ret, EINVAL); + uint32_t level = MAX_BUCKET_LEVEL + 1; + DcacheLookupCtx *ctx = nullptr; + bool byId = true; + uint8_t revalidate = 0; + auto ret = InLevel(level, ctx, byId, revalidate); + EXPECT_EQ(ret, nullptr); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "HandleFileByFdTest001 ERROR"; + GTEST_LOG_(INFO) << "InLevelTest001 ERROR"; } - GTEST_LOG_(INFO) << "HandleFileByFdTest001 End"; + GTEST_LOG_(INFO) << "InLevelTest001 End"; } /** - * @tc.name: HandleFileByFdTest002 - * @tc.desc: Verify the HandleFileByFd function + * @tc.name: InLevelTest002 + * @tc.desc: Verify the InLevel function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, HandleFileByFdTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, InLevelTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "HandleFileByFdTest002 Start"; + GTEST_LOG_(INFO) << "InLevelTest002 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - struct stat mockStat = { - .st_size = 1, - } - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(1)); - unsigned long endBlock = 10; - uint32_t level = 0; - auto ret = mFile.HandleFileByFd(endBlock, level); - EXPECT_EQ(ret, ENOENT); + uint32_t level = 1; + shared_ptr ctx = make_shared(); + bool byId = true; + uint8_t revalidate = 0; + EXPECT_CALL(*insMock, FilePosLock(_, _, _, _)).Times(2).WillRepeatedly(Return(0)); + EXPECT_CALL(*insMock, ReadFile(_, _, _, _)).WillOnce(Return(1)); + auto ret = InLevel(level, ctx.get(), byId, revalidate); + EXPECT_EQ(ret, nullptr); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "HandleFileByFdTest002 ERROR"; + GTEST_LOG_(INFO) << "InLevelTest002 ERROR"; } - GTEST_LOG_(INFO) << "HandleFileByFdTest002 End"; + GTEST_LOG_(INFO) << "InLevelTest002 End"; } /** - * @tc.name: HandleFileByFdTest003 - * @tc.desc: Verify the HandleFileByFd function + * @tc.name: FindDentryTest001 + * @tc.desc: Verify the FindDentry function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, HandleFileByFdTest003, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, FindDentryTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "HandleFileByFdTest003 Start"; + GTEST_LOG_(INFO) << "FindDentryTest001 Start"; try { - CloudDiskServiceMetaFile mFile(100, 1, 123); - struct stat mockStat = { - .st_size = 1, - } - EXPECT_CALL(*insMock, fstat(_, _)).WillOnce(DoAll(SetArgPointee<1>(mockStat), Return(0))); - EXPECT_CALL(*insMock, ftruncate(_, _)).WillOnce(Return(0)); - unsigned long endBlock = 10; - uint32_t level = 0; - auto ret = mFile.HandleFileByFd(endBlock, level); - EXPECT_EQ(ret, E_OK); + shared_ptr ctx = make_shared(); + FindDentry(ctx.get()); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "HandleFileByFdTest003 ERROR"; + GTEST_LOG_(INFO) << "FindDentryTest002 ERROR"; } - GTEST_LOG_(INFO) << "HandleFileByFdTest003 End"; + GTEST_LOG_(INFO) << "FindDentryTest002 End"; } /** - * @tc.name: GetMetaFileMgrInstanceTest001 - * @tc.desc: Verify the GetMetaFileMgrInstance function + * @tc.name: InitDcacheLookupCtxTest001 + * @tc.desc: Verify the InitDcacheLookupCtx function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetMetaFileMgrInstanceTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, InitDcacheLookupCtxTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetMetaFileMgrInstanceTest001 Start"; + GTEST_LOG_(INFO) << "InitDcacheLookupCtxTest001 Start"; try { - MetaFileMgr& instance1 = MetaFileMgr::GetInstance(); - MetaFileMgr& instance2 = MetaFileMgr::GetInstance(); - EXPECT_EQ(instance1, instance2); + shared_ptr ctx = make_shared(); + MetaBase base; + base.name = "MetaBase_name"; + InitDcacheLookupCtx(ctx.get(), base, 0, 0); + EXPECT_EQ(ctx->fd, 0); + EXPECT_EQ(ctx->name, base.name); + EXPECT_EQ(ctx->bidx, 0); + EXPECT_EQ(ctx->page, nullptr); + EXPECT_EQ(ctx->hash, 0); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetMetaFileMgrInstanceTest001 ERROR"; + GTEST_LOG_(INFO) << "InitDcacheLookupCtxTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetMetaFileMgrInstanceTest001 End"; + GTEST_LOG_(INFO) << "InitDcacheLookupCtxTest001 End"; } /** - * @tc.name: GetCloudDiskServiceMetaFileTest001 - * @tc.desc: Verify the GetCloudDiskServiceMetaFile function + * @tc.name: RoomForFilenameTest001 + * @tc.desc: Verify the RoomForFilename function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetCloudDiskServiceMetaFileTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, RoomForFilenameTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest001 Start"; + GTEST_LOG_(INFO) << "RoomForFilenameTest001 Start"; try { - MetaFileMgr& mgr = MetaFileMgr::GetInstance(); - mgr.metaFiles_.clear(); - mgr.metaFileList_.clear(); - uint32_t userId = 1001; - uint32_t syncFolderIndex = 0; - uint64_t inode = 123456789; - auto metaFile = mgr.GetCloudDiskServiceMetaFile(userId, syncFolderIndex, inode); - EXPECT_TRUE(metafile != nullptr); - EXPECT_EQ(mgr.metaFiles_.size(), 1); - EXPECT_EQ(mgr.metaFileList_.size(), 1); + size_t slots = 2; + uint32_t maxSlots = 5; + uint8_t bitmap[] = {1, 1, 1, 1, 1}; + uint32_t ret = RoomForFilename(bitmap, slots, maxSlots); + EXPECT_EQ(ret, 1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest001 ERROR"; + GTEST_LOG_(INFO) << "RoomForFilenameTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest001 End"; + GTEST_LOG_(INFO) << "RoomForFilenameTest001 End"; } /** - * @tc.name: GetCloudDiskServiceMetaFileTest002 - * @tc.desc: Verify the GetCloudDiskServiceMetaFile function + * @tc.name: RoomForFilenameTest002 + * @tc.desc: Verify the RoomForFilename function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetCloudDiskServiceMetaFileTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, RoomForFilenameTest002, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest002 Start"; + GTEST_LOG_(INFO) << "RoomForFilenameTest002 Start"; try { - MetaFileMgr& mgr = MetaFileMgr::GetInstance(); - mgr.metaFiles_.clear(); - mgr.metaFileList_.clear(); - uint32_t userId = 1001; - uint32_t syncFolderIndex = 0; - uint64_t inode = 123456789; - constexpr uint32_t MAX_META_FILE_NUM = 150; - for (int i = 0; i < MAX_META_FILE_NUM; i++) { - uint32_t tempUserId = 1000 + i; - uint32_t tempSyncFolderIndex = i; - uint64_t tempInode = 1000000000 + i; - shared_ptr mFile = - make_shared(tempUserId, tempSyncFolderIndex, tempInode); - MetaFileKey key(tempUserId, - Convertor::ConvertToHex(tempSyncFolderIndex) + Convertor::ConvertToHex(tempInode)); - mgr.metaFileList_.emplace_back(key, mFile); - auto it = prev(mgr.metaFileList_.end()); - mgr.metaFiles_[key] = it; - } - auto metaFile = mgr.GetCloudDiskServiceMetaFile(userId, syncFolderIndex, inode); - EXPECT_TRUE(metafile != nullptr); - EXPECT_EQ(mgr.metaFiles_.size(), MAX_META_FILE_NUM); - EXPECT_EQ(mgr.metaFileList_.size(), MAX_META_FILE_NUM); + size_t slots = 2; + uint32_t maxSlots = 5; + uint8_t bitmap[] = {0, 0, 0, 0, 0}; + uint32_t ret = RoomForFilename(bitmap, slots, maxSlots); + EXPECT_EQ(ret, 0); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest002 ERROR"; + GTEST_LOG_(INFO) << "RoomForFilenameTest002 ERROR"; } - GTEST_LOG_(INFO) << "GetCloudDiskServiceMetaFileTest002 End"; + GTEST_LOG_(INFO) << "RoomForFilenameTest002 End"; } /** - * @tc.name: GetRelativePathTest001 - * @tc.desc: Verify the GetRelativePath function + * @tc.name: RoomForFilenameTest003 + * @tc.desc: Verify the RoomForFilename function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetRelativePathTest001, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, RoomForFilenameTest003, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetRelativePathTest001 Start"; + GTEST_LOG_(INFO) << "RoomForFilenameTest003 Start"; try { - MetaFileMgr& mgr = MetaFileMgr::GetInstance(); - mgr.metaFiles_.clear(); - mgr.metaFileList_.clear(); - uint32_t userId = 1001; - uint32_t syncFolderIndex = 0; - uint64_t inode = 123456789; - auto metaFile = make_shared(userId, syncFolderIndex, inode); - metaFile->parentDentryFile_ = ROOT_PARENTDENTRYFILE; - string path = "/test"; - auto ret = mgr.GetRelativePath(metaFile, path); - EXPECT_EQ(ret, E_OK); + size_t slots = 2; + uint32_t maxSlots = 5; + uint8_t bitmap[] = {1, 1, 1, 1, 0}; + uint32_t ret = RoomForFilename(bitmap, slots, maxSlots); + EXPECT_EQ(ret, 1); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetRelativePathTest001 ERROR"; + GTEST_LOG_(INFO) << "RoomForFilenameTest003 ERROR"; } - GTEST_LOG_(INFO) << "GetRelativePathTest001 End"; + GTEST_LOG_(INFO) << "RoomForFilenameTest003 End"; } /** - * @tc.name: GetRelativePathTest002 - * @tc.desc: Verify the GetRelativePath function + * @tc.name: GetDentryFileByPathTest001 + * @tc.desc: Verify the GetDentryFileByPath function * @tc.type: FUNC * @tc.require: NA */ -HWTEST_F(CloudDiskServiceMetafileTest, GetRelativePathTest002, TestSize.Level1) +HWTEST_F(CloudDiskServiceMetafileTest, GetDentryFileByPathTest001, TestSize.Level1) { - GTEST_LOG_(INFO) << "GetRelativePathTest002 Start"; + GTEST_LOG_(INFO) << "GetDentryFileByPathTest001 Start"; try { - MetaFileMgr& mgr = MetaFileMgr::GetInstance(); - mgr.metaFiles_.clear(); - mgr.metaFileList_.clear(); - uint32_t userId = 1001; - uint32_t syncFolderIndex = 0; - uint64_t inode = 123456789; - auto metaFile = make_shared(userId, syncFolderIndex, inode); - metaFile->parentDentryFile_ = "123"; - string path = "/test"; - auto ret = mgr.GetRelativePath(metaFile, path); - EXPECT_EQ(ret, -1); + uint32_t userId = 100; + std::string index = "index"; + std::string inode = "inode"; + GetDentryFileByPath(userId, index, inode); } catch (...) { EXPECT_TRUE(false); - GTEST_LOG_(INFO) << "GetRelativePathTest002 ERROR"; + GTEST_LOG_(INFO) << "GetDentryFileByPathTest001 ERROR"; } - GTEST_LOG_(INFO) << "GetRelativePathTest002 End"; + GTEST_LOG_(INFO) << "GetDentryFileByPathTest001 End"; } -} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file +} // namespace OHOS::FileManagement::CloudDiskService -- Gitee From 26f924dfb9a7c70057233766d7f2cec0fe7dcd80 Mon Sep 17 00:00:00 2001 From: fuletian Date: Sat, 13 Sep 2025 20:44:44 +0800 Subject: [PATCH 24/26] updata_tdd Signed-off-by: fuletian --- .../sync_folder/include/bit_ops.h | 63 -- .../sync_folder/include/block_queue.h | 97 --- .../include/cloud_disk_service_logfile.h | 166 ---- .../include/cloud_disk_service_metafile.h | 156 ---- .../include/cloud_disk_service_syncfolder.h | 39 - .../sync_folder/include/converter.h | 31 - .../sync_folder/include/uuid_helper.h | 34 - .../src/cloud_disk_service_logfile.cpp | 654 -------------- .../src/cloud_disk_service_metafile.cpp | 796 ------------------ .../src/cloud_disk_service_syncfolder.cpp | 62 -- .../sync_folder/src/converter.cpp | 57 -- .../sync_folder/src/uuid_helper.cpp | 52 -- .../utils/include/cloud_disk_service_error.h | 29 - 13 files changed, 2236 deletions(-) delete mode 100644 services/clouddiskservice/sync_folder/include/bit_ops.h delete mode 100644 services/clouddiskservice/sync_folder/include/block_queue.h delete mode 100644 services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h delete mode 100644 services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h delete mode 100644 services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h delete mode 100644 services/clouddiskservice/sync_folder/include/converter.h delete mode 100644 services/clouddiskservice/sync_folder/include/uuid_helper.h delete mode 100644 services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp delete mode 100644 services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp delete mode 100644 services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp delete mode 100644 services/clouddiskservice/sync_folder/src/converter.cpp delete mode 100644 services/clouddiskservice/sync_folder/src/uuid_helper.cpp delete mode 100644 services/clouddiskservice/utils/include/cloud_disk_service_error.h diff --git a/services/clouddiskservice/sync_folder/include/bit_ops.h b/services/clouddiskservice/sync_folder/include/bit_ops.h deleted file mode 100644 index 0dc6c4e5f..000000000 --- a/services/clouddiskservice/sync_folder/include/bit_ops.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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 BIT_OPS_H -#define BIT_OPS_H - -namespace OHOS::FileManagement::CloudDiskService { - -struct BitOps { - static const uint8_t BIT_PER_BYTE = 8; - static int TestBit(uint32_t nr, const uint8_t addr[]) - { - return 1 & (addr[nr / BIT_PER_BYTE] >> (nr & (BIT_PER_BYTE - 1))); - } - - static void ClearBit(uint32_t nr, uint8_t addr[]) - { - addr[nr / BIT_PER_BYTE] &= ~(1UL << ((nr) % BIT_PER_BYTE)); - } - - static void SetBit(uint32_t nr, uint8_t addr[]) - { - addr[nr / BIT_PER_BYTE] |= (1UL << ((nr) % BIT_PER_BYTE)); - } - - static uint32_t FindNextBit(const uint8_t addr[], uint32_t maxSlots, uint32_t start) - { - while (start < maxSlots) { - if (BitOps::TestBit(start, addr)) { - return start; - } - start++; - } - return maxSlots; - } - - static uint32_t FindNextZeroBit(const uint8_t addr[], uint32_t maxSlots, uint32_t start) - { - while (start < maxSlots) { - if (!BitOps::TestBit(start, addr)) { - return start; - } - start++; - } - return maxSlots; - } -}; - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // BIT_OPS_H diff --git a/services/clouddiskservice/sync_folder/include/block_queue.h b/services/clouddiskservice/sync_folder/include/block_queue.h deleted file mode 100644 index 52019bc31..000000000 --- a/services/clouddiskservice/sync_folder/include/block_queue.h +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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 BLOCK_QUEUE_H -#define BLOCK_QUEUE_H - -#include -#include - -#include "ffrt_inner.h" - -namespace OHOS::FileManagement::CloudDiskService { - -template -class BlockQueue { -public: - BlockQueue() - { - std::lock_guard lock(mutex_); - running_ = true; - } - - ~BlockQueue() - { - std::lock_guard lock(mutex_); - running_ = false; - } - - void push(T item) - { - std::lock_guard lock(mutex_); - if (!running_) { - return; - } - queue_.push(std::move(item)); - cv_.notify_one(); - } - - void pushBatch(std::vector &items) - { - std::lock_guard lock(mutex_); - if (!running_ || items.empty()) { - return; - } - for (const auto &item : items) { - queue_.push(std::move(item)); - } - cv_.notify_one(); - } - - std::optional pop() - { - std::unique_lock lock(mutex_); - cv_.wait(lock, [this] {return !running_ || !queue_.empty(); }); // run = false !run = true empty = true !empty = false - if (!running_ || queue_.empty()) { - return std::nullopt; - } - T item = std::move(queue_.front()); - queue_.pop(); - return item; - } - - bool empty() - { - std::lock_guard lock(mutex_); - return queue_.empty(); - } - - void shutdown() - { - std::lock_guard lock(mutex_); - running_ = false; - } - -private: - bool running_; - std::queue queue_; - ffrt::mutex mutex_; - ffrt::condition_variable cv_; -}; - - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // BLOCK_QUEUE_H diff --git a/services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h b/services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h deleted file mode 100644 index b88958e3c..000000000 --- a/services/clouddiskservice/sync_folder/include/cloud_disk_service_logfile.h +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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 CLOUD_DISK_SERVICE_LOGFILE_H -#define CLOUD_DISK_SERVICE_LOGFILE_H - -#include -#include -#include -#include -#include -#include - -#include "block_queue.h" -#include "cloud_disk_common.h" -#include "cloud_disk_service_metafile.h" -#include "disk_types.h" -#include "unique_fd.h" - -namespace OHOS::FileManagement::CloudDiskService { - -constexpr uint32_t LOGBLOCK_RESERVED_LENGTH = 2; -constexpr uint32_t LOGGROUP_RESERVED_LENGTH = 62; -constexpr uint32_t LOGBLOCK_PER_GROUP = 64; -constexpr uint32_t LOGGROUP_SHIFT = 0X6; -constexpr uint32_t LOGGROUP_SIZE = 4096; -constexpr uint32_t LOGGROUP_SIZE_SHIFT = 0XC; -constexpr uint32_t LOGGROUP_MAX = 1024; -constexpr uint32_t LOGGROUP_INDEX_MASK = 0XFFFF; -constexpr uint32_t LOGGROUP_HEADER = 4096; -constexpr uint32_t LOG_INDEX_MASK = 0X3F; -constexpr uint32_t LOG_COUNT_MAX = 65536; - -#pragma pack(push, 1) -struct LogBlock { - uint64_t line; - uint64_t timestamp; - uint64_t parentInode; - uint32_t hash; - uint8_t operationType; - uint8_t recordId[RECORD_ID_LEN]; - uint8_t parentRecordId[RECORD_ID_LEN]; - uint8_t reserved[LOGBLOCK_RESERVED_LENGTH]; -}; - -struct LogGroup { - uint8_t logVersion; - uint8_t logBlockCnt; - struct LogBlock nsl[LOGBLOCK_PER_GROUP]; - uint8_t reserved[LOGGROUP_RESERVED_LENGTH]; -}; -static_assert(sizeof(LogGroup) == LOGGROUP_SIZE); -#pragma pack(pop) - -class CloudDiskServiceLogFile { -public: - CloudDiskServiceLogFile() = delete; - ~CloudDiskServiceLogFile() = default; - explicit CloudDiskServiceLogFile(const uint32_t userId, const uint32_t syncFolderIndex); - - int32_t ProduceLog(struct EventInfo &eventInfo); - int32_t PraseLog(const uint64_t line, ChangeData &data); - int32_t FillChildForDir(const std::string &path, const uint64_t timestamp); - // int32_t PraseLogs(const uint64_t start, const uint64_t count, struct ChangesResult &changesResult); - void StartCallback(); - void StopCallback(); - - int32_t OnDataChange(); -private: - // int32_t DecodeLogHeader(); - - int32_t ProductLogForOperate(const std::shared_ptr parentMetaFile, - const std::string &path, const std::string &name, std::string &childRecordId, - uint8_t operationType); - int32_t ProduceCreateLog(const std::shared_ptr parentMetaFile, const std::string &path, - const std::string &name, std::string &childRecordId); - int32_t ProduceUnlinkLog(const std::shared_ptr parentMetaFile, const std::string &name, - std::string &childRecordId); - int32_t ProduceRenameOldLog(const std::shared_ptr parentMetaFile, const std::string &name, - std::string &childRecordId); - int32_t ProduceRenameNewLog(const std::shared_ptr parentMetaFile, const std::string &path, - const std::string &name, std::string &childRecordId); - int32_t ProduceCloseAndWriteLog(const std::shared_ptr parentMetaFile, - const std::string &path, const std::string &name, std::string &childRecordId); - - - // int32_t ConsumeCreateLog(const LogStruct log, ChangeData &data); - // int32_t ConsumeUnlinkLog(const LogStruct log, ChangeData &data); - // int32_t ConsumeRenameOldLog(const LogStruct log, ChangeData &data); - // int32_t ConsumeRenameNewLog(const LogStruct log, ChangeData &data); - // int32_t ConsumeCloseAndWriteLog(const LogStruct log, ChangeData &data); - int32_t WriteLogFile(const struct LogBlock &logBlock); - int32_t ReadLogFile(const uint64_t line, struct LogBlock &logBlock); - int32_t GenerateLogBlock(struct EventInfo &eventInfo, const uint64_t parentInode, - const std::string &childRecordId, const std::string &parentRecordId, uint64_t &line); - int32_t GenerateChangeData(struct EventInfo &eventInfo, uint64_t line, - const std::string &childRecordId, const std::string &parentRecordId); - - uint32_t userId_; - uint32_t syncFolderIndex_; - bool needCallback_; - std::string syncFolderPath_; - std::string logFilePath_; - std::string renameRecordId_; - UniqueFd fd_{}; - - std::atomic currentLine_; - - // std::mutex queueMtx_{}; - // std::queue> logQueue_; - // std::mutex syncMtx_{}; - // std::queue> syncQueue_; - std::mutex vectorMtx_{}; - std::vector changeDatas_; -}; - -typedef std::pair LogFileKey; - -class LogFileMgr { -public: - static LogFileMgr& GetInstance(); - std::shared_ptr GetCloudDiskServiceLogFile(const uint32_t userId, - const uint32_t syncFolderIndex); - - int32_t ProduceRequest(std::vector &eventInfos); - int32_t ProduceRequest(struct EventInfo &eventInfo); - int32_t PraseRequest(const int32_t userId, const uint32_t syncFolderIndex, const uint64_t start, - const uint64_t count, struct ChangesResult &changesResult); - - // int32_t ConsumeLog(); - int32_t RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, const std::string &path); - int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex); - void RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); - void UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); - -private: - LogFileMgr(); // 启动线程 - ~LogFileMgr(); - LogFileMgr(const LogFileMgr &m) = delete; - const LogFileMgr &operator=(const LogFileMgr &m) = delete; - - int32_t ConsumeRequest(); - // int32_t WriteLogFile(); - int32_t OnDataChange(); - - bool running_; - std::mutex mtx_{}; - std::map> LogFiles_; - BlockQueue eventBlockQueue_; -}; - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // CLOUD_DISK_SERVICE_LOGFILE_H \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h b/services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h deleted file mode 100644 index 18f15c635..000000000 --- a/services/clouddiskservice/sync_folder/include/cloud_disk_service_metafile.h +++ /dev/null @@ -1,156 +0,0 @@ -/* - * 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 CLOUD_DISK_SERVICE_METAFILE_H -#define CLOUD_DISK_SERVICE_METAFILE_H - -#include -#include -#include -#include -#include - -#include "unique_fd.h" - -namespace OHOS::FileManagement::CloudDiskService { - -constexpr uint32_t DENTRYGROUP_SIZE = 4096; -constexpr uint32_t DENTRY_NAME_LEN = 16; -constexpr uint32_t DENTRY_RESERVED_LENGTH = 3; -constexpr uint32_t DENTRY_PER_GROUP = 60; -constexpr uint32_t DENTRY_BITMAP_LENGTH = 8; -constexpr uint32_t DENTRY_GROUP_RESERVED = 7; -constexpr uint32_t DENTRYGROUP_HEADER = 4096; -constexpr uint32_t MAX_BUCKET_LEVEL = 63; -constexpr uint32_t BUCKET_BLOCKS = 2; -constexpr uint32_t BYTE_PER_SLOT = 16; -constexpr uint32_t HMDFS_SLOT_LEN_BITS = 4; -constexpr uint32_t RECORD_ID_LEN = 16; -constexpr uint32_t DENTRYFILE_NAME_LEN = 16; -const std::string ROOT_PARENTDENTRYFILE = "."; - -#pragma pack(push, 1) -struct CloudDiskServiceDentry { - uint8_t revalidate; - uint8_t recordId[RECORD_ID_LEN]; - uint32_t hash; - uint16_t mode; - uint16_t namelen; - uint64_t size; - uint64_t mtime; - uint64_t atime; - /* reserved bytes for long term extend, total 52 bytes */ - uint8_t reserved[DENTRY_RESERVED_LENGTH]; -}; - -struct CloudDiskServiceDentryGroup { - uint8_t dentryVersion; - uint8_t bitmap[DENTRY_BITMAP_LENGTH]; - struct CloudDiskServiceDentry nsl[DENTRY_PER_GROUP]; - uint8_t fileName[DENTRY_PER_GROUP][DENTRY_NAME_LEN]; - uint8_t reserved[DENTRY_GROUP_RESERVED]; -}; -static_assert(sizeof(CloudDiskServiceDentryGroup) == DENTRYGROUP_SIZE); - -struct CloudDiskServiceDcacheHeader { - uint8_t parentDentryfile[DENTRYFILE_NAME_LEN]; - uint8_t selfRecordId[RECORD_ID_LEN]; - uint32_t selfHash; -}; -#pragma pack(pop) - -struct MetaBase { - MetaBase(const std::string &nameOrRecordId, const bool isName) - { - if (isName) { - name = nameOrRecordId; - } else { - recordId = nameOrRecordId; - } - } - MetaBase(const std::string &name, const std::string &recordId) : name(name), recordId(recordId) {} - MetaBase() = default; - - uint32_t mode{0}; - uint32_t hash{0}; - uint64_t atime{0}; - uint64_t mtime{0}; - uint64_t size{0}; - std::string name{}; - std::string recordId{}; -}; - -class CloudDiskServiceMetaFile { -public: - CloudDiskServiceMetaFile() = delete; - ~CloudDiskServiceMetaFile() = default; - using CloudDiskServiceMetaFileCallBack = std::function; - explicit CloudDiskServiceMetaFile(const uint32_t userId, const uint32_t syncFolderIndex, const uint64_t inode); - - int32_t DoCreate(const MetaBase &base); - int32_t DoRemove(const MetaBase &base, std::string &recordId); - int32_t DoFlush(const MetaBase &base, std::string &recordId); - int32_t DoUpdate(const MetaBase &base, std::string &recordId); - int32_t DoRenameOld(const MetaBase &base, std::string &recordId); - int32_t DoRenameNew(const MetaBase &base, std::string &recordId); - int32_t DoRename(MetaBase &metaBase, const std::string &newName, - std::shared_ptr newMetaFile); - int32_t DoLookupByName(MetaBase &base); - int32_t DoLookupByRecordId(MetaBase &base); - - int32_t DecodeDentryHeader(); - int32_t GenericDentryHeader(); - - std::string parentDentryFile_{}; - std::string selfRecordId_{}; - std::string selfInode_{}; - uint32_t selfHash_{0}; - - std::string syncFolderIndex_{}; - uint32_t userId_{0}; - -private: - int32_t GetCreateInfo(const MetaBase &base, uint32_t &bitPos, uint32_t &namehash, unsigned long &bidx, - struct CloudDiskServiceDentryGroup &dentryBlk); - int32_t HandleFileByFd(unsigned long &endBlock, uint32_t level); - std::mutex mtx_{}; - UniqueFd fd_{}; - std::string cacheFile_{}; -}; - -typedef std::pair MetaFileKey; -typedef std::pair> MetaFileListEle; - -class MetaFileMgr { -public: - static MetaFileMgr& GetInstance(); - std::shared_ptr GetCloudDiskServiceMetaFile(const uint32_t userId, - const uint32_t syncFolderIndex, const uint64_t inode); - - int32_t GetRelativePath(const std::shared_ptr metaFile, std::string &path); -private: - MetaFileMgr() = default; - ~MetaFileMgr() = default; - MetaFileMgr(const MetaFileMgr &m) = delete; - const MetaFileMgr &operator=(const MetaFileMgr &m) = delete; - - std::mutex mtx_{}; - std::list metaFileList_; - std::map::iterator> metaFiles_; -}; - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // CLOUD_DISK_SERVICE_METAFILE_H \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h b/services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h deleted file mode 100644 index 5210a5a98..000000000 --- a/services/clouddiskservice/sync_folder/include/cloud_disk_service_syncfolder.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 CLOUD_DISK_SERVICE_SYNCFOLDER_H -#define CLOUD_DISK_SERVICE_SYNCFOLDER_H - -#include -#include - -#include "disk_types.h" - -namespace OHOS::FileManagement::CloudDiskService { - -class CloudDiskServiceSyncFolder { -public: - static int32_t RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, const std::string &path); - static int32_t UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex); - static void RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); - static void UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex); - static int32_t GetSyncFolderChanges(const int32_t userId, const uint32_t syncFolderIndex, const uint64_t start, - const uint64_t count, struct ChangesResult &changesResult); - static int32_t SetSyncFolderChanges(std::vector &eventInfos); -}; - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // CLOUD_DISK_SERVICE_SYNCFOLDER_H \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/include/converter.h b/services/clouddiskservice/sync_folder/include/converter.h deleted file mode 100644 index 2925d57a1..000000000 --- a/services/clouddiskservice/sync_folder/include/converter.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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 CONVERTER_H -#define CONVERTER_H - -#include - -namespace OHOS::FileManagement::CloudDiskService { - -class Converter { -public: - static std::string ConvertToHex(uint64_t value); - static uint64_t ConvertFromHex(const std::string &hex); -}; - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // CONVERTER_H diff --git a/services/clouddiskservice/sync_folder/include/uuid_helper.h b/services/clouddiskservice/sync_folder/include/uuid_helper.h deleted file mode 100644 index 6247e8874..000000000 --- a/services/clouddiskservice/sync_folder/include/uuid_helper.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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 UUID_HELPER_H -#define UUID_HELPER_H - -#include - -#include "uuid.h" // e2fsprogs:libext2_uuid - -namespace OHOS::FileManagement::CloudDiskService { - -class UuidHelper { -public: - static std::string GenerateUuid(); - static std::string GenerateUuidWithoutDelim(); - static std::string GenerateUuidOnly(); -}; - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // UUID_HELPER_H diff --git a/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp b/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp deleted file mode 100644 index eff7f04d7..000000000 --- a/services/clouddiskservice/sync_folder/src/cloud_disk_service_logfile.cpp +++ /dev/null @@ -1,654 +0,0 @@ -/* - * 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 "cloud_disk_service_logfile.h" - -#include -#include -#include -#include -#include - -#include "cloud_disk_service_callback_manager.h" -#include "cloud_disk_service_error.h" -#include "cloud_disk_service_manager.h" -#include "cloud_file_utils.h" -#include "converter.h" -#include "ffrt_inner.h" -#include "file_utils.h" -#include "utils_directory.h" -#include "utils_log.h" -#include "uuid_helper.h" - -namespace OHOS::FileManagement::CloudDiskService { - -const std::string LOGFILENAME = "path为/data/service/el2/1/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/0000000000000001,第二个参数为0771时,运行完这个函数,path的值为多少"; -const unsigned int STAT_MODE_DIR = 0771; -const unsigned int MAX_CHANGEDATAS_SIZE = 20; - -static std::string GetLogFileByPath(const uint32_t userId, const uint32_t syncFolderIndex) -{ - std::string rootDir = - "/data/service/el2/" + std::to_string(userId) + - "/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/" + - Converter::ConvertToHex(syncFolderIndex) + "/"; - Storage::DistributedFile::Utils::ForceCreateDirectory(rootDir, STAT_MODE_DIR); - return rootDir + LOGFILENAME; -} - -static void GetBucketAndOffset(const uint64_t line, uint32_t &bucket, uint32_t &offset) -{ - uint32_t temp = line & LOGGROUP_INDEX_MASK; - bucket = temp >> LOGGROUP_SHIFT; - offset = temp & LOG_INDEX_MASK; -} - -static off_t GetBucketaddr(const uint32_t bucket) -{ - return (static_cast(bucket) << LOGGROUP_SIZE_SHIFT) + LOGGROUP_HEADER; -} - -static std::unique_ptr LoadCurrentPage(int fd, uint32_t bucket) -{ - auto logGroup = std::make_unique(); - off_t pos = GetBucketaddr(bucket); - auto ret = FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_WRLCK); - if (ret) { - return nullptr; - } - ssize_t size = FileUtils::ReadFile(fd, pos, LOGGROUP_SIZE, logGroup.get()); - if (size != LOGGROUP_SIZE) { - (void)FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_UNLCK); - return nullptr; - } - return logGroup; -} - -static int32_t SyncCurrentPage(LogGroup &logGroup, int fd, uint32_t line) -{ - struct stat fileStat; - int err = fstat(fd, &fileStat); - if (err < 0) { - return EINVAL; - } - - off_t pos = fileStat.st_size - LOGGROUP_SIZE; - int size = FileUtils::WriteFile(fd, &logGroup, pos, LOGGROUP_SIZE); - if (size != LOGGROUP_SIZE) { - LOGD("WriteFile failed, size %{public}d != %{public}d", size, LOGGROUP_SIZE); - (void)FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_UNLCK); - return EINVAL; - } - auto ret = FileRangeLock::FilePosLock(fd, pos, LOGGROUP_SIZE, F_UNLCK); - if (ret) { - return ret; - } - return E_OK; -} - -static int32_t FillLogGroup(struct LogGroup &logGroup, const struct LogBlock &logBlock, uint32_t offset) -{ - if (logGroup.nsl[offset].timestamp != 0) { - (void) memset_s(&logGroup, LOGGROUP_SIZE, 0, LOGGROUP_SIZE); - // todo 完全消费日志 - } - auto ret = memcpy_s(&logGroup.nsl[offset], sizeof(struct LogBlock), &logBlock, sizeof(struct LogBlock)); - if (ret != 0) { - LOGE("memcpy_s struct LogBlock failed, errno = %{public}d", errno); - return -1; - } - logGroup.logBlockCnt++; - return E_OK; -} - -static uint32_t GetCurrentLine(int fd) -{ - uint32_t startLine = 0; - uint32_t offset = 0; - for (uint32_t i = 0; i < LOGGROUP_MAX; i++) { - auto logGroup = LoadCurrentPage(fd, i); - if (logGroup->nsl[0].timestamp != 0) { - break; - } else if (logGroup->nsl[0].line != startLine + offset) { - break; - } else { - startLine = logGroup->nsl[0].line; - offset = logGroup->logBlockCnt; - } - } - return startLine + offset; -} - -int32_t CloudDiskServiceLogFile::WriteLogFile(const struct LogBlock &logBlock) -{ - uint32_t bucket; - uint32_t offset; - GetBucketAndOffset(logBlock.line, bucket, offset); - - auto logGroup = LoadCurrentPage(fd_, bucket); - FillLogGroup(*logGroup, logBlock, offset); - SyncCurrentPage(*logGroup, fd_, bucket); - return E_OK; -} - -int32_t CloudDiskServiceLogFile::ReadLogFile(const uint64_t line, struct LogBlock &logBlock) -{ - uint32_t bucket; - uint32_t offset; - GetBucketAndOffset(logBlock.line, bucket, offset); - - auto logGroup = LoadCurrentPage(fd_, bucket); - logBlock = logGroup->nsl[offset]; - return E_OK; -} - -int32_t CloudDiskServiceLogFile::OnDataChange() -{ - std::lock_guard lock(vectorMtx_); - - if (changeDatas_.empty()) { - return E_OK; - } - CloudDiskServiceCallbackManager::GetInstance().OnChangeData(syncFolderIndex_, changeDatas_); - changeDatas_.clear(); - - return E_OK; -} - -CloudDiskServiceLogFile::CloudDiskServiceLogFile(const uint32_t userId, const uint32_t syncFolderIndex) -{ - userId_ = userId; - syncFolderIndex_ = syncFolderIndex; - needCallback_ = false; - // syncFolderPath_ = CloudDiskSyncFolder::GetInstance().syncFolderMap[syncFolderIndex].path; - logFilePath_ = GetLogFileByPath(userId_, syncFolderIndex_); - - if (access(logFilePath_.c_str(), F_OK) == 0) { - fd_ = UniqueFd{open(logFilePath_.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; - currentLine_ = GetCurrentLine(fd_); - } else { - fd_ = UniqueFd{open(logFilePath_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; - ftruncate(fd_, LOGGROUP_SIZE * LOGGROUP_MAX + LOGGROUP_HEADER); - currentLine_ = 0; - } -} - -int32_t CloudDiskServiceLogFile::GenerateLogBlock(struct EventInfo &eventInfo, const uint64_t parentInode, - const std::string &childRecordId, const std::string &parentRecordId, - uint64_t &line) -{ - // std::lock_guard lock(queueMtx_); - struct LogBlock logBlock; - logBlock.line = currentLine_++; - logBlock.timestamp = eventInfo.timestamp; - logBlock.parentInode = parentInode; - logBlock.hash = CloudDisk::CloudFileUtils::DentryHash(eventInfo.name); - logBlock.operationType = eventInfo.operateType; - auto ret = memcpy_s(logBlock.recordId, RECORD_ID_LEN, childRecordId.c_str(), RECORD_ID_LEN); - if (ret != 0) { - LOGE("memcpy_s recordId failed, errno = %{public}d", errno); - return -1; - } - ret = memcpy_s(logBlock.parentRecordId, RECORD_ID_LEN, parentRecordId.c_str(), RECORD_ID_LEN); - if (ret != 0) { - LOGE("memcpy_s parentRecordId failed, errno = %{public}d", errno); - return -1; - } - // logQueue_.push(logBlock); - WriteLogFile(logBlock); - line = logBlock.line; - return E_OK; -} - -int32_t CloudDiskServiceLogFile::GenerateChangeData(struct EventInfo &eventInfo, uint64_t line, - const std::string &childRecordId, const std::string &parentRecordId) -{ - std::lock_guard lock(vectorMtx_); - struct ChangeData changeData{}; - changeData.updateSequenceNumber = line; - changeData.fileId = childRecordId; - changeData.parentFileId = parentRecordId; - changeData.relativePath = eventInfo.path + "/" + eventInfo.name; - changeData.operationType = static_cast(eventInfo.operateType); - - struct stat childStat; - if (stat(changeData.relativePath.c_str(), &childStat) != 0) { - changeData.size = 0; - changeData.mtime = 0; - } else { - changeData.size = childStat.st_size; - changeData.mtime = childStat.st_mtime; - } - - changeData.timeStamp = eventInfo.timestamp; - changeDatas_.push_back(changeData); - if (changeDatas_.size() >= MAX_CHANGEDATAS_SIZE) { - CloudDiskServiceCallbackManager::GetInstance().OnChangeData(syncFolderIndex_, changeDatas_); - changeDatas_.clear(); - } - return E_OK; -} - -int32_t CloudDiskServiceLogFile::FillChildForDir(const std::string &path, const uint64_t timestamp) -{ - struct stat statInfo; - if (stat(path.c_str(), &statInfo) != 0) { - LOGE("stat parent failed for %{public}d", errno); - return errno; - } - - if (!S_ISDIR(statInfo.st_mode)) { - return E_OK; - } - DIR *dir = opendir(path.c_str()); - if (!dir) { - LOGE("opendir failed errno = %{public}d", errno); - return errno; - } - - struct dirent *entry; - while ((entry = readdir(dir)) != NULL) { - if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { - continue; - } - struct EventInfo eventInfo(userId_, syncFolderIndex_, OperationType::CREATE, path + "/" + entry->d_name); - LogFileMgr::GetInstance().ProduceRequest(eventInfo); - } - - closedir(dir); - return E_OK; -} - - -void CloudDiskServiceLogFile::StartCallback() -{ - needCallback_ = true; -} - -void CloudDiskServiceLogFile::StopCallback() -{ - needCallback_ = false; -} - -int32_t CloudDiskServiceLogFile::ProduceLog(struct EventInfo &eventInfo) -{ - struct stat parentStat; - if (stat(eventInfo.path.c_str(), &parentStat) != 0) { - LOGE("stat parent failed for %{public}d", errno); - return errno; - } - - int32_t ret = 0; - if (eventInfo.operateType == static_cast(OperationType::SYNC_FOLDER_INVALID)) - { - ret = CloudDiskServiceManager::GetInstance().UnregisterForSa(eventInfo.path + "/" + eventInfo.name); - if (ret != 0) { - return ret; - } else { - return LogFileMgr::GetInstance().UnRegisterSyncFolder(userId_, syncFolderIndex_); - } - } - - auto parentMetaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, - parentStat.st_ino); - - std::string childRecordId; - ret = ProductLogForOperate(parentMetaFile, eventInfo.path, eventInfo.name, childRecordId, - eventInfo.operateType); - if (ret != 0) { - LOGE("create log failed"); - return ret; - } - - if (eventInfo.operateType == static_cast(OperationType::CREATE)) { - ffrt::submit( - [eventInfo, this] { FillChildForDir(eventInfo.path + "/" + eventInfo.name, eventInfo.timestamp); }); - } - - uint64_t line = 0; - GenerateLogBlock(eventInfo, parentStat.st_ino, childRecordId, parentMetaFile->selfRecordId_, line); - if (needCallback_) { - GenerateChangeData(eventInfo, line, childRecordId, parentMetaFile->selfRecordId_); - } - return E_OK; -} - -int32_t CloudDiskServiceLogFile::PraseLog(const uint64_t line, ChangeData &data) -{ - // 可校验下父亲是否是父亲,存不存在inode冲突的情况,根据parentRecordId - if (currentLine_.load() - line >= LOG_COUNT_MAX) { - return E_OUT_OF_RANGE; - } - - struct LogBlock logBlock; - auto ret = ReadLogFile(line, logBlock); - if (ret != 0) { - return ret; - } - - auto parentMetaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, - logBlock.parentInode); - std::string relativePath; - ret = MetaFileMgr::GetInstance().GetRelativePath(parentMetaFile, relativePath); - if (ret != 0) { - LOGE("get relative path failed"); - return ret; - } - - std::string recordId = std::string(reinterpret_cast(logBlock.recordId), RECORD_ID_LEN); - std::string parentRecordId = std::string(reinterpret_cast(logBlock.parentRecordId), RECORD_ID_LEN); - MetaBase mBase(recordId, false); - auto metaFile = parentMetaFile->DoLookupByRecordId(mBase); - - data.updateSequenceNumber = logBlock.line; - data.fileId = recordId; - data.parentFileId = parentRecordId; - //data.syncFolderIndex = syncFolderIndex_; - data.relativePath = relativePath + "/" + mBase.name; - data.operationType = static_cast(logBlock.operationType); - data.size = mBase.size; - data.mtime = mBase.mtime; - data.timeStamp = logBlock.timestamp; - - if (line == currentLine_.load()) { - return E_END_OF_LOGFILE; - } - return E_OK; -} - -int32_t CloudDiskServiceLogFile::ProductLogForOperate(const std::shared_ptr parentMetaFile, - const std::string &path, const std::string &name, - std::string &childRecordId, uint8_t operationType) -{ - OperationType type = static_cast(operationType); - switch (type) { - case OperationType::CREATE: - return ProduceCreateLog(parentMetaFile, path, name, childRecordId); - case OperationType::DELETE: - return ProduceUnlinkLog(parentMetaFile, name, childRecordId); - case OperationType::MOVE_FROM: - return ProduceRenameOldLog(parentMetaFile, name, childRecordId); - case OperationType::MOVE_TO: - return ProduceRenameNewLog(parentMetaFile, path, name, childRecordId); - case OperationType::CLOSE_WRITE: - return ProduceCloseAndWriteLog(parentMetaFile, path, name, childRecordId); - case OperationType::SYNC_FOLDER_INVALID: - case OperationType::OPERATION_MAX: - return EINVAL; - } -} - -int32_t CloudDiskServiceLogFile::ProduceCreateLog(const std::shared_ptr parentMetaFile, - const std::string &path, const std::string &name, - std::string &childRecordId) -{ - childRecordId = UuidHelper::GenerateUuidOnly(); - MetaBase mBase(name, childRecordId); - - struct stat childStat; - if (stat((path + "/" + name).c_str(), &childStat) != 0) { - LOGE("stat child failed for %{public}d", errno); - return errno; - } - mBase.mode = childStat.st_mode; - mBase.atime = childStat.st_atime; - mBase.mtime = childStat.st_mtime; - mBase.size = childStat.st_size; - auto ret = parentMetaFile->DoCreate(mBase); - if (ret != 0) { - LOGE("create failed"); - return -1; - } - - if (!S_ISDIR(childStat.st_ino)) { - return E_OK; - } - auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, childStat.st_ino); - metaFile->parentDentryFile_ = parentMetaFile->selfInode_; - metaFile->selfRecordId_ = childRecordId; - metaFile->selfHash_ = CloudDisk::CloudFileUtils::DentryHash(name); - metaFile->GenericDentryHeader(); - return E_OK; -} - -int32_t CloudDiskServiceLogFile::ProduceUnlinkLog(const std::shared_ptr parentMetaFile, - const std::string &name, std::string &childRecordId) -{ - MetaBase mBase(name, true); - auto ret = parentMetaFile->DoRemove(mBase, childRecordId); - if (ret != 0) { - LOGE("remove failed"); - return -1; - } - return E_OK; -} - -int32_t CloudDiskServiceLogFile::ProduceRenameOldLog(const std::shared_ptr parentMetaFile, - const std::string &name, std::string &childRecordId) -{ - MetaBase mBase(name, true); - auto ret = parentMetaFile->DoRenameOld(mBase, childRecordId); - if (ret != 0) { - LOGE("renameold failed"); - return -1; - } - renameRecordId_ = childRecordId; - return E_OK; -} - -int32_t CloudDiskServiceLogFile::ProduceRenameNewLog(const std::shared_ptr parentMetaFile, - const std::string &path, const std::string &name, - std::string &childRecordId) -{ - MetaBase mBase(name, true); - struct stat childStat; - if (stat((path + "/" + name).c_str(), &childStat) != 0) { - LOGE("stat child failed for %{public}d", errno); - return errno; - } - mBase.mode = childStat.st_mode; - mBase.atime = childStat.st_atime; - mBase.mtime = childStat.st_mtime; - mBase.size = childStat.st_size; - auto ret = parentMetaFile->DoRenameNew(mBase, renameRecordId_); - if (ret != 0) { - LOGE("renamenew failed"); - return -1; - } - childRecordId = renameRecordId_; - - if (!S_ISDIR(childStat.st_ino)) { - return E_OK; - } - auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId_, syncFolderIndex_, childStat.st_ino); - metaFile->parentDentryFile_ = parentMetaFile->selfInode_; - metaFile->selfRecordId_ = childRecordId; - metaFile->selfHash_ = CloudDisk::CloudFileUtils::DentryHash(name); - metaFile->GenericDentryHeader(); - return E_OK; -} - -int32_t CloudDiskServiceLogFile::ProduceCloseAndWriteLog(const std::shared_ptr parentMetaFile, - const std::string &path, const std::string &name, - std::string &childRecordId) -{ - MetaBase mBase(name, true); - struct stat childStat; - if (stat((path + "/" + name).c_str(), &childStat) != 0) { - LOGE("stat child failed for %{public}d", errno); - return errno; - } - mBase.mode = childStat.st_mode; - mBase.atime = childStat.st_atime; - mBase.mtime = childStat.st_mtime; - mBase.size = childStat.st_size; - auto ret = parentMetaFile->DoUpdate(mBase, childRecordId); - if (ret != 0) { - LOGE("update failed"); - return -1; - } - return E_OK; -} - -LogFileMgr& LogFileMgr::GetInstance() -{ - static LogFileMgr instance_; - return instance_; -} - -LogFileMgr::LogFileMgr() -{ - running_ = true; - ffrt::submit([this] { ConsumeRequest(); }); -} - -LogFileMgr::~LogFileMgr() -{ - running_ = false; - eventBlockQueue_.shutdown(); -} - -int32_t LogFileMgr::ProduceRequest(std::vector &eventInfos) -{ - if (!running_) { - return -1; - } - eventBlockQueue_.pushBatch(eventInfos); - return E_OK; -} - -int32_t LogFileMgr::ProduceRequest(struct EventInfo &eventInfo) -{ - if (!running_) { - return -1; - } - eventBlockQueue_.push(eventInfo); - return E_OK; -} - -int32_t LogFileMgr::PraseRequest(const int32_t userId, const uint32_t syncFolderIndex, const uint64_t start, - const uint64_t count, struct ChangesResult &changesResult) -{ - int32_t ret; - auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); - for (uint64_t line = start; line < start + count; line++) { - struct ChangeData changeData; - ret = logFile->PraseLog(line, changeData); - if (ret == E_OK) { - changesResult.changesData.push_back(changeData); - continue; - } else if (ret == E_END_OF_LOGFILE) { - changesResult.nextUsn = start + line; - changesResult.isEof = true; - break; - } else { - break; - } - } - if (ret == E_OK) { - changesResult.nextUsn = start + count; - changesResult.isEof = false; - } - return ret; -} - -int32_t LogFileMgr::RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, const std::string &path) -{ - auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); - - struct stat st; - if (stat(path.c_str(), &st) != 0) { - LOGE("stat failed for %{public}d", errno); - return errno; - } - auto metaFile = MetaFileMgr::GetInstance().GetCloudDiskServiceMetaFile(userId, syncFolderIndex, st.st_ino); - metaFile->parentDentryFile_ = ROOT_PARENTDENTRYFILE; - metaFile->selfRecordId_ = UuidHelper::GenerateUuidOnly(); - metaFile->selfHash_ = CloudDisk::CloudFileUtils::DentryHash(ROOT_PARENTDENTRYFILE); - - metaFile->GenericDentryHeader(); - - ffrt::submit([logFile, path] { logFile->FillChildForDir(path, UTCTimeMilliSeconds()); }); - return E_OK; -} - -int32_t LogFileMgr::UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) -{ - std::lock_guard lock(mtx_); - LogFileKey key(userId, syncFolderIndex); - LogFiles_.erase(key); - - std::string rootDir = - "/data/service/el2/" + std::to_string(userId) + - "/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/" + - Converter::ConvertToHex(syncFolderIndex) + "/"; - if (!OHOS::Storage::DistributedFile::Utils::ForceRemoveDirectoryDeepFirst(rootDir)) { - LOGW("remove photo dentry dir failed, errno: %{public}d", errno); - } - return E_OK; -} - -void LogFileMgr::RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) -{ - auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); - logFile->StartCallback(); -} - -void LogFileMgr::UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) -{ - auto logFile = GetCloudDiskServiceLogFile(userId, syncFolderIndex); - logFile->StopCallback(); -} - -std::shared_ptr LogFileMgr::GetCloudDiskServiceLogFile(const uint32_t userId, - const uint32_t syncFolderIndex) -{ - std::shared_ptr lFile = nullptr; - std::lock_guard lock(mtx_); - LogFileKey key(userId, syncFolderIndex); - auto it = LogFiles_.find(key); - if (it != LogFiles_.end()) { - lFile = it->second; - } else { - lFile = std::make_shared(userId, syncFolderIndex); - LogFiles_[key] = lFile; - } - return lFile; -} - -int32_t LogFileMgr::ConsumeRequest() -{ - while (running_) { - auto front = eventBlockQueue_.pop(); - if (!front) { - continue; - } - - auto eventInfo = std::move(front.value()); - auto logFile = GetCloudDiskServiceLogFile(eventInfo.userId, eventInfo.syncFolderIndex); - logFile->ProduceLog(eventInfo); - } - return E_OK; -} - -int32_t LogFileMgr::OnDataChange() -{ - for (const auto &it : LogFiles_) { - it.second->OnDataChange(); - } - return E_OK; -} - -} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp b/services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp deleted file mode 100644 index 29736cd1b..000000000 --- a/services/clouddiskservice/sync_folder/src/cloud_disk_service_metafile.cpp +++ /dev/null @@ -1,796 +0,0 @@ -/* - * 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 "cloud_disk_service_metafile.h" - -#include -#include -#include -#include -#include // e2fsprogs:libext2_uuid - -#include "bit_ops.h" -#include "cloud_disk_service_error.h" -#include "cloud_file_utils.h" -#include "converter.h" -#include "file_utils.h" -#include "utils_directory.h" -#include "utils_log.h" - -namespace OHOS::FileManagement::CloudDiskService { -using namespace OHOS::FileManagement; - -constexpr uint8_t VALIDATE = 0x01; -constexpr uint8_t INVALIDATE = 0x02; -constexpr uint8_t FIND_BOTH = 0x03; -constexpr uint32_t MAX_META_FILE_NUM = 150; -const unsigned int STAT_MODE_DIR = 0771; - -struct DcacheLookupCtx { - int fd{-1}; - std::string name{}; - std::string recordId{}; - uint32_t hash{0}; - uint32_t bidx{0}; - std::unique_ptr page{nullptr}; -}; - -static inline uint32_t GetDentrySlots(size_t namelen) -{ - return static_cast((namelen + BYTE_PER_SLOT - 1) >> HMDFS_SLOT_LEN_BITS); -} - -static inline off_t GetDentryGroupPos(size_t bidx) -{ - return bidx * DENTRYGROUP_SIZE + DENTRYGROUP_HEADER; -} - -static inline uint64_t GetDentryGroupCnt(uint64_t size) -{ - return (size >= DENTRYGROUP_HEADER) ? ((size - DENTRYGROUP_HEADER) / DENTRYGROUP_SIZE) : 0; -} - -static uint32_t GetOverallBucket(uint32_t level) -{ - if (level >= MAX_BUCKET_LEVEL) { - LOGD("level = %{public}d overflow", level); - return E_OK; - } - uint64_t buckets = (1ULL << (level + 1)) - 1; - return static_cast(buckets); -} - -static size_t GetDcacheFileSize(uint32_t level) -{ - size_t buckets = GetOverallBucket(level); - return buckets * DENTRYGROUP_SIZE * BUCKET_BLOCKS + DENTRYGROUP_HEADER; -} - -static uint32_t GetBucketByLevel(uint32_t level) -{ - if (level >= MAX_BUCKET_LEVEL) { - LOGD("level = %{public}d overflow", level); - return E_OK; - } - - uint64_t buckets = (1ULL << level); - return static_cast(buckets); -} - -static uint32_t GetBucketaddr(uint32_t level, uint32_t buckoffset) -{ - if (level >= MAX_BUCKET_LEVEL) { - return E_OK; - } - - uint64_t curLevelMaxBucks = (1ULL << level); - if (buckoffset >= curLevelMaxBucks) { - return E_OK; - } - - return static_cast(curLevelMaxBucks) + buckoffset - 1; -} - -static unsigned long GetBidxFromLevel(uint32_t level, uint32_t namehash) -{ - uint32_t bucket = GetBucketByLevel(level); - if (bucket == 0) { - return E_OK; - } - return BUCKET_BLOCKS * GetBucketaddr(level, namehash % bucket); -} - -static std::unique_ptr FindDentryPage(uint64_t index, DcacheLookupCtx *ctx) -{ - auto dentryBlk = std::make_unique(); - - off_t pos = GetDentryGroupPos(index); - auto ret = FileRangeLock::FilePosLock(ctx->fd, pos, DENTRYGROUP_SIZE, F_WRLCK); - if (ret) { - return nullptr; - } - ssize_t size = FileUtils::ReadFile(ctx->fd, pos, DENTRYGROUP_SIZE, dentryBlk.get()); - if (size != DENTRYGROUP_SIZE) { - (void)FileRangeLock::FilePosLock(ctx->fd, pos, DENTRYGROUP_SIZE, F_UNLCK); - return nullptr; - } - return dentryBlk; -} - -static CloudDiskServiceDentry *FindInBlockById(CloudDiskServiceDentryGroup &dentryBlk, uint32_t namehash, - const std::string &recordId, uint8_t revalidate) -{ - uint32_t bitPos = 0; - CloudDiskServiceDentry *de = nullptr; - - while (bitPos < DENTRY_PER_GROUP) { - if (!BitOps::TestBit(bitPos, dentryBlk.bitmap)) { - bitPos++; - continue; - } - de = &dentryBlk.nsl[bitPos]; - if (!de->namelen) { - bitPos++; - continue; - } - - if ((de->revalidate & revalidate) != 0 && !memcmp(recordId.c_str(), de->recordId, recordId.length())) { - return de; - } - bitPos += GetDentrySlots(de->namelen); - } - - return nullptr; -} - -static CloudDiskServiceDentry *FindInBlock(CloudDiskServiceDentryGroup &dentryBlk, uint32_t namehash, - const std::string &name, uint8_t revalidate) -{ - uint32_t bitPos = 0; - CloudDiskServiceDentry *de = nullptr; - - while (bitPos < DENTRY_PER_GROUP) { - if (!BitOps::TestBit(bitPos, dentryBlk.bitmap)) { - bitPos++; - continue; - } - de = &dentryBlk.nsl[bitPos]; - if (!de->namelen) { - bitPos++; - continue; - } - - if ((de->revalidate & revalidate) != 0 && de->hash == namehash && de->namelen == name.length() && - !memcmp(name.c_str(), dentryBlk.fileName[bitPos], de->namelen)) { - return de; - } - bitPos += GetDentrySlots(de->namelen); - } - - return nullptr; -} - -static CloudDiskServiceDentry *InLevel(uint32_t level, DcacheLookupCtx *ctx, bool byId, uint8_t revalidate) - __attribute__((no_sanitize("unsigned-integer-overflow"))) -{ - CloudDiskServiceDentry *de = nullptr; - - uint32_t nbucket = GetBucketByLevel(level); - if (nbucket == 0) { - return de; - } - - uint32_t bidx = GetBucketaddr(level, ctx->hash % nbucket) * BUCKET_BLOCKS; - uint32_t endBlock = bidx + BUCKET_BLOCKS; - - for (; bidx < endBlock; bidx++) { - auto dentryBlk = FindDentryPage(bidx, ctx); - if (dentryBlk == nullptr) { - break; - } - - if (byId) { - de = FindInBlockById(*dentryBlk, ctx->hash, ctx->recordId, revalidate); - } else { - de = FindInBlock(*dentryBlk, ctx->hash, ctx->name, revalidate); - } - if (de != nullptr) { - ctx->page = std::move(dentryBlk); - break; - } - off_t pos = GetDentryGroupPos(bidx); - (void)FileRangeLock::FilePosLock(ctx->fd, pos, DENTRYGROUP_SIZE, F_UNLCK); - } - ctx->bidx = bidx; - return de; -} - -static CloudDiskServiceDentry *FindDentry(DcacheLookupCtx *ctx, bool byId = false, uint8_t revalidate = VALIDATE) -{ - for (uint32_t level = 0; level < MAX_BUCKET_LEVEL; level++) { - CloudDiskServiceDentry *de = InLevel(level, ctx, byId, revalidate); - if (de != nullptr) { - return de; - } - } - return nullptr; -} - -static void InitDcacheLookupCtx(DcacheLookupCtx *ctx, const MetaBase &base, uint32_t hash, int fd) -{ - ctx->fd = fd; - ctx->name = base.name; - ctx->bidx = 0; - ctx->page = nullptr; - ctx->hash = hash; -} - -static uint32_t RoomForFilename(const uint8_t bitmap[], size_t slots, uint32_t maxSlots) -{ - uint32_t bitStart = 0; - bool loopFlag = true; - while (loopFlag) { - uint32_t zeroStart = BitOps::FindNextZeroBit(bitmap, maxSlots, bitStart); - if (zeroStart >= maxSlots) { - return maxSlots; - } - - uint32_t zeroEnd = BitOps::FindNextBit(bitmap, maxSlots, zeroStart); - if (zeroEnd - zeroStart >= slots) { - return zeroStart; - } - - bitStart = zeroEnd + 1; - if (zeroEnd + 1 >= maxSlots) { - return maxSlots; - } - } - return E_OK; -} - -static bool CreateDentry(CloudDiskServiceDentryGroup &d, const MetaBase &base, uint32_t nameHash, uint32_t bitPos, - std::string recordId) -{ - CloudDiskServiceDentry *de; - const std::string name = base.name; - uint32_t slots = GetDentrySlots(name.length()); - - de = &d.nsl[bitPos]; - de->hash = nameHash; - de->namelen = name.length(); - auto ret = memcpy_s(d.fileName[bitPos], slots * DENTRY_NAME_LEN, name.c_str(), name.length()); - if (ret != 0) { - LOGE("memcpy_s failed, dstLen = %{public}d, srcLen = %{public}zu", slots * DENTRY_NAME_LEN, name.length()); - return false; - } - de->revalidate = VALIDATE; - de->atime = base.atime; - de->mtime = base.mtime; - de->size = base.size; - de->mode = base.mode; - (void) memset_s(de->recordId, RECORD_ID_LEN, 0, RECORD_ID_LEN); - ret = memcpy_s(de->recordId, RECORD_ID_LEN, recordId.c_str(), recordId.length()); - if (ret != 0) { - LOGE("memcpy_s failed, dstLen = %{public}d, srcLen = %{public}zu", RECORD_ID_LEN, recordId.length()); - return false; - } - - for (uint32_t i = 0; i < slots; i++) { - BitOps::SetBit(bitPos + i, d.bitmap); - if (i) { - (de + i)->namelen = 0; - } - } - return true; -} - -static std::string GetDentryFileByPath(const uint32_t userId, const std::string &syncFolderIndex, - const std::string &inode) -{ - std::string cacheDir = - "/data/service/el2/" + std::to_string(userId) + - "/hmdfs/cache/account_cache/dentry_cache/clouddisk_service_cache/" + syncFolderIndex + "/" + - std::to_string(CloudDisk::CloudFileUtils::GetBucketId(inode)) + "/"; - Storage::DistributedFile::Utils::ForceCreateDirectory(cacheDir, STAT_MODE_DIR); - return cacheDir + inode; -} - -CloudDiskServiceMetaFile::CloudDiskServiceMetaFile(const uint32_t userId, const uint32_t syncFolderIndex, - const uint64_t inode) -{ - userId_ = userId; - syncFolderIndex_ = Converter::ConvertToHex(syncFolderIndex); - selfInode_ = Converter::ConvertToHex(inode); - cacheFile_ = GetDentryFileByPath(userId_, syncFolderIndex_, selfInode_); - - if (access(cacheFile_.c_str(), F_OK) == 0) { - fd_ = UniqueFd{open(cacheFile_.c_str(), O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; - DecodeDentryHeader(); - } else { - fd_ = UniqueFd{open(cacheFile_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)}; - } -} - -int32_t CloudDiskServiceMetaFile::DecodeDentryHeader() -{ - auto ret = FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_WRLCK); - if (ret) { - return ret; - } - struct CloudDiskServiceDcacheHeader header; - ssize_t size = FileUtils::ReadFile(fd_, 0, sizeof(CloudDiskServiceDcacheHeader), &header); - (void)FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_UNLCK); - if (size != sizeof(CloudDiskServiceDcacheHeader)) { - return size; - } - - parentDentryFile_ = std::string(reinterpret_cast(header.parentDentryfile), DENTRYFILE_NAME_LEN); - selfRecordId_ = std::string(reinterpret_cast(header.selfRecordId), RECORD_ID_LEN); - selfHash_ = header.selfHash; - - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::GenericDentryHeader() -{ - struct stat fileStat; - int err = fstat(fd_, &fileStat); - if (err < 0) { - return EINVAL; - } - if (fileStat.st_size < DENTRYGROUP_HEADER && ftruncate(fd_, DENTRYGROUP_HEADER)) { - return ENOENT; - } - - struct CloudDiskServiceDcacheHeader header; - auto ret = memcpy_s(header.parentDentryfile, DENTRYFILE_NAME_LEN, parentDentryFile_.c_str(), DENTRYFILE_NAME_LEN); - ret += memcpy_s(header.selfRecordId, RECORD_ID_LEN, selfRecordId_.c_str(), RECORD_ID_LEN); - if (ret != 0) { - LOGE("memcpy_s failed errno=%{public}d", errno); - return errno; - } - header.selfHash = selfHash_; - - ret = FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_WRLCK); - if (ret) { - return ret; - } - ssize_t size = FileUtils::WriteFile(fd_, &header, 0, sizeof(CloudDiskServiceDcacheHeader)); - (void)FileRangeLock::FilePosLock(fd_, 0, DENTRYGROUP_HEADER, F_UNLCK); - if (size != sizeof(CloudDiskServiceDcacheHeader)) { - return size; - } - - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoCreate(const MetaBase &base) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - // validate the length of name, maximum length is 52*8 byte - uint32_t slots = GetDentrySlots(base.name.length()); - if (slots > DENTRY_PER_GROUP) { - LOGE("name is too long"); - return ENAMETOOLONG; - } - - std::unique_lock lock(mtx_); - DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); - CloudDiskServiceDentry *de = FindDentry(&ctx); - if (de != nullptr) { - LOGE("this name dentry is exist"); - (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); - return EEXIST; - } - uint32_t bitPos = 0; - unsigned long bidx = 0; - CloudDiskServiceDentryGroup dentryBlk = {0}; - uint32_t namehash = 0; - auto ret = GetCreateInfo(base, bitPos, namehash, bidx, dentryBlk); - if (ret) { - return ret; - } - off_t pos = GetDentryGroupPos(bidx); - if (!CreateDentry(dentryBlk, base, namehash, bitPos, base.recordId)) { - LOGI("CreateDentry fail, stop write."); - (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - return EINVAL; - } - int size = FileUtils::WriteFile(fd_, &dentryBlk, pos, DENTRYGROUP_SIZE); - if (size != DENTRYGROUP_SIZE) { - LOGD("WriteFile failed, size %{public}d != %{public}d", size, DENTRYGROUP_SIZE); - (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - return EINVAL; - } - ret = FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - if (ret) { - return ret; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoRemove(const MetaBase &base, std::string &recordId) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - std::unique_lock lock(mtx_); - struct DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); - struct CloudDiskServiceDentry *de = FindDentry(&ctx); - if (de == nullptr) { - LOGD("find dentry failed"); - return ENOENT; - } - - de->revalidate = INVALIDATE; - recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); - - off_t ipos = GetDentryGroupPos(ctx.bidx); - ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(struct CloudDiskServiceDentryGroup)); - if (size != sizeof(struct CloudDiskServiceDentryGroup)) { - LOGE("write failed, ret = %{public}zd", size); - (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - return EIO; - } - auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - if (ret) { - return ret; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoFlush(const MetaBase &base, std::string &recordId) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - std::unique_lock lock(mtx_); - DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); - CloudDiskServiceDentry *de = FindDentry(&ctx, false, INVALIDATE); - if (de == nullptr) { - LOGE("find dentry failed"); - return ENOENT; - } - - uint32_t bitPos = (de - ctx.page->nsl); - uint32_t slots = GetDentrySlots(de->namelen); - for (uint32_t i = 0; i < slots; i++) { - BitOps::ClearBit(bitPos + i, ctx.page->bitmap); - } - - off_t ipos = GetDentryGroupPos(ctx.bidx); - ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(CloudDiskServiceDentryGroup)); - if (size != sizeof(CloudDiskServiceDentryGroup)) { - LOGE("WriteFile failed!, ret = %{public}zd", size); - (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - return EIO; - } - auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - if (ret) { - return ret; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoUpdate(const MetaBase &base, std::string &recordId) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - std::unique_lock lock(mtx_); - struct DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); - struct CloudDiskServiceDentry *de = FindDentry(&ctx); - if (de == nullptr) { - LOGD("find dentry failed"); - return ENOENT; - } - - de->atime = base.atime; - de->mtime = base.mtime; - de->size = base.size; - de->mode = base.mode; - recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); - - off_t ipos = GetDentryGroupPos(ctx.bidx); - ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(struct CloudDiskServiceDentryGroup)); - if (size != sizeof(struct CloudDiskServiceDentryGroup)) { - LOGE("write failed, ret = %{public}zd", size); - (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - return EIO; - } - auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - if (ret) { - return ret; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoRenameOld(const MetaBase &base, std::string &recordId) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - std::unique_lock lock(mtx_); - struct DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); - struct CloudDiskServiceDentry *de = FindDentry(&ctx); - if (de == nullptr) { - LOGD("find dentry failed"); - return ENOENT; - } - - de->revalidate = INVALIDATE; - recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); - - off_t ipos = GetDentryGroupPos(ctx.bidx); - ssize_t size = FileUtils::WriteFile(fd_, ctx.page.get(), ipos, sizeof(struct CloudDiskServiceDentryGroup)); - if (size != sizeof(struct CloudDiskServiceDentryGroup)) { - LOGE("write failed, ret = %{public}zd", size); - (void)FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - return EIO; - } - auto ret = FileRangeLock::FilePosLock(fd_, ipos, DENTRYGROUP_SIZE, F_UNLCK); - if (ret) { - return ret; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoRenameNew(const MetaBase &base, std::string &recordId) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - // validate the length of name, maximum length is 52*8 byte - uint32_t slots = GetDentrySlots(base.name.length()); - if (slots > DENTRY_PER_GROUP) { - LOGE("name is too long"); - return ENAMETOOLONG; - } - - std::unique_lock lock(mtx_); - DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); - CloudDiskServiceDentry *de = FindDentry(&ctx); - if (de != nullptr) { - LOGE("this name dentry is exist"); - (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); - return EEXIST; - } - uint32_t bitPos = 0; - unsigned long bidx = 0; - CloudDiskServiceDentryGroup dentryBlk = {0}; - uint32_t namehash = 0; - auto ret = GetCreateInfo(base, bitPos, namehash, bidx, dentryBlk); - if (ret) { - return ret; - } - off_t pos = GetDentryGroupPos(bidx); - if (!CreateDentry(dentryBlk, base, namehash, bitPos, recordId)) { - LOGI("CreateDentry fail, stop write."); - (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - return EINVAL; - } - int size = FileUtils::WriteFile(fd_, &dentryBlk, pos, DENTRYGROUP_SIZE); - if (size != DENTRYGROUP_SIZE) { - LOGD("WriteFile failed, size %{public}d != %{public}d", size, DENTRYGROUP_SIZE); - (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - return EINVAL; - } - ret = FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - if (ret) { - return ret; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoRename(MetaBase &metaBase, const std::string &newName, - std::shared_ptr newMetaFile) -{ - std::string oldName = metaBase.name; - metaBase.name = newName; - auto ret = newMetaFile->DoCreate(metaBase); - if (ret != 0) { - LOGE("create dentry failed, ret = %{public}d", ret); - return ret; - } - metaBase.name = oldName; - std::string childRecordId; - ret = DoRemove(metaBase, childRecordId); - if (ret != 0) { - LOGE("remove dentry failed, ret = %{public}d", ret); - metaBase.name = newName; - (void)newMetaFile->DoFlush(metaBase, childRecordId); - return ret; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoLookupByName(MetaBase &base) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - std::unique_lock lock(mtx_); - struct DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, CloudDisk::CloudFileUtils::DentryHash(base.name), fd_); - struct CloudDiskServiceDentry *de = FindDentry(&ctx); - if (de == nullptr) { - LOGD("find dentry failed"); - return ENOENT; - } - (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); - - base.mode = de->mode; - base.hash = de->hash; - base.size = de->size; - base.atime = de->atime; - base.mtime = de->mtime; - base.recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::DoLookupByRecordId(MetaBase &base) -{ - if (fd_ < 0) { - LOGE("bad metafile fd"); - return EINVAL; - } - - std::unique_lock lock(mtx_); - struct DcacheLookupCtx ctx; - InitDcacheLookupCtx(&ctx, base, base.hash, fd_); - ctx.recordId = base.recordId; - struct CloudDiskServiceDentry *de = FindDentry(&ctx, true, FIND_BOTH); - if (de == nullptr) { - LOGD("find dentry failed"); - return ENOENT; - } - (void)FileRangeLock::FilePosLock(fd_, GetDentryGroupPos(ctx.bidx), DENTRYGROUP_SIZE, F_UNLCK); - - base.mode = de->mode; - base.hash = de->hash; - base.size = de->size; - base.atime = de->atime; - base.mtime = de->mtime; - base.recordId = std::string(reinterpret_cast(de->recordId), RECORD_ID_LEN); - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::GetCreateInfo(const MetaBase &base, uint32_t &bitPos, uint32_t &namehash, - unsigned long &bidx, struct CloudDiskServiceDentryGroup &dentryBlk) -{ - uint32_t level = 0; - namehash = CloudDisk::CloudFileUtils::DentryHash(base.name); - bool found = false; - while (!found) { - if (level == MAX_BUCKET_LEVEL) { - return ENOSPC; - } - bidx = GetBidxFromLevel(level, namehash); - unsigned long endBlock = bidx + BUCKET_BLOCKS; - int32_t ret = HandleFileByFd(endBlock, level); - if (ret != 0) { - return ret; - } - for (; bidx < endBlock; bidx++) { - off_t pos = GetDentryGroupPos(bidx); - ret = FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_WRLCK); - if (ret) { - return ret; - } - if (FileUtils::ReadFile(fd_, pos, DENTRYGROUP_SIZE, &dentryBlk) != DENTRYGROUP_SIZE) { - (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - return ENOENT; - } - bitPos = RoomForFilename(dentryBlk.bitmap, GetDentrySlots(base.name.length()), DENTRY_PER_GROUP); - if (bitPos < DENTRY_PER_GROUP) { - found = true; - break; - } - (void)FileRangeLock::FilePosLock(fd_, pos, DENTRYGROUP_SIZE, F_UNLCK); - } - ++level; - } - return E_OK; -} - -int32_t CloudDiskServiceMetaFile::HandleFileByFd(unsigned long &endBlock, uint32_t level) -{ - struct stat fileStat; - int err = fstat(fd_, &fileStat); - if (err < 0) { - return EINVAL; - } - if ((endBlock > GetDentryGroupCnt(fileStat.st_size)) && - ftruncate(fd_, GetDcacheFileSize(level))) { - return ENOENT; - } - return E_OK; -} - -MetaFileMgr& MetaFileMgr::GetInstance() -{ - static MetaFileMgr instance_; - return instance_; -} - -std::shared_ptr MetaFileMgr::GetCloudDiskServiceMetaFile(uint32_t userId, - const uint32_t syncFolderIndex, const uint64_t inode) -{ - std::shared_ptr mFile = nullptr; - std::lock_guard lock(mtx_); - MetaFileKey key(userId, Converter::ConvertToHex(syncFolderIndex) + Converter::ConvertToHex(inode)); - auto it = metaFiles_.find(key); - if (it != metaFiles_.end()) { - metaFileList_.splice(metaFileList_.begin(), metaFileList_, it->second); - mFile = it->second->second; - } else { - if (metaFiles_.size() == MAX_META_FILE_NUM) { - auto deleteKey = metaFileList_.back().first; - metaFiles_.erase(deleteKey); - metaFileList_.pop_back(); - } - mFile = std::make_shared(userId, syncFolderIndex, inode); - metaFileList_.emplace_front(key, mFile); - metaFiles_[key] = metaFileList_.begin(); - } - return mFile; -} - -int32_t MetaFileMgr::GetRelativePath(const std::shared_ptr metaFile, std::string &path) -{ - auto pMetaFile = metaFile; - uint32_t userId = metaFile->userId_; - uint32_t syncFolderIndex = Converter::ConvertFromHex(metaFile->syncFolderIndex_); - while (pMetaFile->parentDentryFile_ != ROOT_PARENTDENTRYFILE) { - uint64_t inode = Converter::ConvertFromHex(pMetaFile->parentDentryFile_); - auto parentMetaFile = GetCloudDiskServiceMetaFile(userId, syncFolderIndex, inode); - - MetaBase mBase(pMetaFile->selfRecordId_, false); - int32_t ret = parentMetaFile->DoLookupByRecordId(mBase); - if (ret != 0) { - LOGE("lookup by id failed"); - return -1; - } - - path = mBase.name + path; - pMetaFile = parentMetaFile; - } - path = "/" + path; - return E_OK; -} - -} // namespace OHOS::FileManagement::CloudDiskService diff --git a/services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp b/services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp deleted file mode 100644 index caa5f2ede..000000000 --- a/services/clouddiskservice/sync_folder/src/cloud_disk_service_syncfolder.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/* - * 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 "cloud_disk_service_syncfolder.h" - -#include -#include -#include -#include -#include - -#include "cloud_disk_service_logfile.h" - -namespace OHOS::FileManagement::CloudDiskService { - -int32_t CloudDiskServiceSyncFolder::RegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex, - const std::string &path) -{ - return LogFileMgr::GetInstance().RegisterSyncFolder(userId, syncFolderIndex, path); -} - -int32_t CloudDiskServiceSyncFolder::UnRegisterSyncFolder(const int32_t userId, const uint64_t syncFolderIndex) -{ - // todo 删除目录,清理map,待讨论 - return LogFileMgr::GetInstance().UnRegisterSyncFolder(userId, syncFolderIndex); -} - -void CloudDiskServiceSyncFolder::RegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) -{ - LogFileMgr::GetInstance().RegisterSyncFolderChanges(userId, syncFolderIndex); -} - -void CloudDiskServiceSyncFolder::UnRegisterSyncFolderChanges(const int32_t userId, const uint64_t syncFolderIndex) -{ - LogFileMgr::GetInstance().UnRegisterSyncFolderChanges(userId, syncFolderIndex); -} - -int32_t CloudDiskServiceSyncFolder::GetSyncFolderChanges(const int32_t userId, const uint32_t syncFolderIndex, - const uint64_t start, const uint64_t count, - struct ChangesResult &changesResult) -{ - return LogFileMgr::GetInstance().PraseRequest(userId, syncFolderIndex, start, count, changesResult); -} - -int32_t CloudDiskServiceSyncFolder::SetSyncFolderChanges(std::vector &eventInfos) -{ - return LogFileMgr::GetInstance().ProduceRequest(eventInfos); -} - -} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/src/converter.cpp b/services/clouddiskservice/sync_folder/src/converter.cpp deleted file mode 100644 index 74092e11c..000000000 --- a/services/clouddiskservice/sync_folder/src/converter.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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 "converter.h" - -#include "utils_log.h" - -namespace OHOS::FileManagement::CloudDiskService { - -std::string Converter::ConvertToHex(uint64_t value) -{ - char buffer[17]; - char *p = buffer + 16; - *p = '\0'; - - for (int i = 0; i < 16; i++) { - --p; - int digit = value & 0xF; - *p = digit < 10 ? '0' + digit : 'a' + digit - 10; - value >>= 4; - } - - return std::string(buffer); -} - -uint64_t Converter::ConvertFromHex(const std::string &hex) -{ - uint64_t value = 0; - for (char c : hex) { - value <<= 4; - if (c >= '0' && c <= '9') { - value |= c - '0'; - } else if (c >= 'a' && c <= 'f') { - value |= c - 'a' + 10; - } else if (c >= 'A' && c <= 'F') { - value |= c - 'A' + 10; - } else { - LOGE("Invalid hex string: %{public}s", hex.c_str()); - return 0; - } - } - return value; -} - -} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/sync_folder/src/uuid_helper.cpp b/services/clouddiskservice/sync_folder/src/uuid_helper.cpp deleted file mode 100644 index d9fb602b8..000000000 --- a/services/clouddiskservice/sync_folder/src/uuid_helper.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * 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 "uuid_helper.h" - -namespace OHOS::FileManagement::CloudDiskService { - -std::string UuidHelper::GenerateUuid() -{ - std::string guid(""); - uuid_t uuid; - uuid_generate(uuid); - char str[50] = {}; - uuid_unparse(uuid, str); - guid.assign(str); - return guid; -} - -std::string UuidHelper::GenerateUuidWithoutDelim() -{ - std::string str = GenerateUuid(); - std::string uuid; - for (auto &ch : str) { - if (ch != '-') { - uuid += ch; - } - } - return uuid; -} - -std::string UuidHelper::GenerateUuidOnly() -{ - std::string guid(""); - uuid_t uuid; - uuid_generate(uuid); - guid.assign(reinterpret_cast(uuid), 16); - return guid; -} - -} // namespace OHOS::FileManagement::CloudDiskService \ No newline at end of file diff --git a/services/clouddiskservice/utils/include/cloud_disk_service_error.h b/services/clouddiskservice/utils/include/cloud_disk_service_error.h deleted file mode 100644 index fac344e15..000000000 --- a/services/clouddiskservice/utils/include/cloud_disk_service_error.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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 CLOUD_DISK_SERVICE_ERROR_H -#define CLOUD_DISK_SERVICE_ERROR_H - -namespace OHOS::FileManagement::CloudDiskService { - -enum CloudDiskServiceErrCode : uint32_t { - E_OK = 0, - E_END_OF_LOGFILE, - E_OUT_OF_RANGE, -}; - -} // namespace OHOS::FileManagement::CloudDiskService - -#endif // CLOUD_DISK_SERVICE_ERROR_H -- Gitee From aaebf1eefae119aee2f6f5dae606049e7e6b722c Mon Sep 17 00:00:00 2001 From: fuletian Date: Sat, 13 Sep 2025 21:19:01 +0800 Subject: [PATCH 25/26] updata Signed-off-by: fuletian --- test/unittests/clouddiskservice/BUILD.gn | 72 +++++++++++++++++++ .../clouddiskservice/mock/assistant.h | 11 ++- .../mock/system_function_mock.cpp | 19 +++++ 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/test/unittests/clouddiskservice/BUILD.gn b/test/unittests/clouddiskservice/BUILD.gn index 65d4ca7f9..795ab3697 100644 --- a/test/unittests/clouddiskservice/BUILD.gn +++ b/test/unittests/clouddiskservice/BUILD.gn @@ -326,9 +326,81 @@ ohos_unittest("uuid_helper_test") { use_exceptions = true } +ohos_unittest("disk_monitor_test") { + module_out_path = "dfs_service/dfs_service" + + sources = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock/system_function_mock.cpp", + "${distributedfile_path}/test/unittests/clouddiskservice/monitor/mock_monitor/disk_utils_mock.cpp", + "${services_path}/clouddiskservice/monitor/src/disk_monitor.cpp", + "${utils_path}/log/src/utils_log.cpp", + "monitor/disk_monitor_test.cpp", + ] + + include_dirs = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock", + "${distributedfile_path}/test/unittests/clouddiskservice/monitor/mock_monitor", + "${services_path}/clouddiskservice/monitor/include", + "${utils_path}/log/include", + ] + + deps = [] + + external_deps = [ + "c_utils:utils", + "ffrt:libffrt", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + use_exceptions = true +} + +ohos_unittest("disk_utils_test") { + module_out_path = "dfs_service/dfs_service" + + sources = [ + "${distributedfile_path}/test/unittests/clouddiskservice/monitor/mock_utils/system_function_mock.cpp", + "${services_path}/clouddiskservice/monitor/src/disk_utils.cpp", + "${utils_path}/log/src/utils_log.cpp", + "monitor/disk_utils_test.cpp" + ] + + include_dirs = [ + "${distributedfile_path}/test/unittests/clouddiskservice/mock", + "${services_path}/clouddiskservice/monitor/include", + "${utils_path}/log/include", + ] + + deps = [] + + external_deps = [ + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + ] + + defines = [ + "private=public", + "LOG_DOMAIN=0xD003900", + "LOG_TAG=\"CloudDiskService\"", + ] + + use_exceptions = true +} + group("clouddisk_service_test") { testonly = true deps = [ + ":disk_monitor_test", + ":disk_utils_test", ":cloud_disk_service_logfile_static_test", ":cloud_disk_service_logfile_test", ":cloud_disk_service_metafile_test", diff --git a/test/unittests/clouddiskservice/mock/assistant.h b/test/unittests/clouddiskservice/mock/assistant.h index 4239abc0d..ae0f8aaf4 100644 --- a/test/unittests/clouddiskservice/mock/assistant.h +++ b/test/unittests/clouddiskservice/mock/assistant.h @@ -35,9 +35,14 @@ public: virtual int FilePosLock(int fd, off_t offset, size_t size, int type) = 0; virtual int access(const char *name, int type) = 0; - virtual DIR* opendir(const char* path) = 0; virtual struct dirent* readdir(DIR* d) = 0; virtual int MockStat(const char* path, struct stat* buf); + + virtual int fanotify_init(unsigned flags, unsigned event_f_flags) = 0; + virtual int fanotify_mark(int fanotify_fd, unsigned flags, unsigned long long mask, int dfd, + const char *pathname) = 0; + virtual DIR *opendir(const char *path) = 0; + virtual int dirfd(DIR *d) = 0; }; class AssistantMock : public Assistant { @@ -55,6 +60,10 @@ public: MOCK_METHOD1(opendir, DIR*(const char*)); MOCK_METHOD1(readdir, struct dirent*(DIR*)); MOCK_METHOD2(MockStat, int(const char*, struct stat*)); + + MOCK_METHOD2(fanotify_init, int(unsigned, unsigned)); + MOCK_METHOD5(fanotify_mark, int(int, unsigned, unsigned long long, int, const char *)); + MOCK_METHOD1(dirfd, int(DIR *)); }; } diff --git a/test/unittests/clouddiskservice/mock/system_function_mock.cpp b/test/unittests/clouddiskservice/mock/system_function_mock.cpp index 4e984e48e..335a672aa 100644 --- a/test/unittests/clouddiskservice/mock/system_function_mock.cpp +++ b/test/unittests/clouddiskservice/mock/system_function_mock.cpp @@ -17,6 +17,25 @@ using namespace OHOS::FileManagement::CloudDiskService; +int fanotify_init(unsigned flags, unsigned event_f_flags) +{ + return Assistant::ins->fanotify_init(flags, event_f_flags); +} + +int fanotify_mark(int fanotify_fd, + unsigned flags, + unsigned long long mask, + int dfd, + const char *pathname) +{ + return Assistant::ins->fanotify_mark(fanotify_fd, flags, mask, dfd, pathname); +} + +int dirfd(DIR *d) +{ + return Assistant::ins->dirfd(d); +} + ssize_t readlink(const char *pathname, char *buf, size_t bufsize) { return Assistant::ins->readlink(pathname, buf, bufsize); -- Gitee From b6f0f2c9dfdcae03b44119d5123151922f02ac99 Mon Sep 17 00:00:00 2001 From: fuletian Date: Sat, 13 Sep 2025 21:24:40 +0800 Subject: [PATCH 26/26] updata Signed-off-by: fuletian --- test/unittests/clouddiskservice/mock/system_function_mock.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/test/unittests/clouddiskservice/mock/system_function_mock.cpp b/test/unittests/clouddiskservice/mock/system_function_mock.cpp index 335a672aa..09e64b036 100644 --- a/test/unittests/clouddiskservice/mock/system_function_mock.cpp +++ b/test/unittests/clouddiskservice/mock/system_function_mock.cpp @@ -14,6 +14,7 @@ */ #include "assistant.h" #include "file_utils.h" +#include using namespace OHOS::FileManagement::CloudDiskService; -- Gitee