1 /* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MOCK_EXTENSION_MOCK_H 17 #define MOCK_EXTENSION_MOCK_H 18 19 #include <fcntl.h> 20 #include <gmock/gmock.h> 21 #include <sys/stat.h> 22 #include <sys/types.h> 23 24 #include "b_error/b_error.h" 25 #include "i_extension.h" 26 #include "iremote_stub.h" 27 #include "test_manager.h" 28 29 namespace OHOS::FileManagement::Backup { 30 class BackupExtExtensionMock : public IRemoteStub<IExtension> { 31 public: 32 int code_ = 0; BackupExtExtensionMock()33 BackupExtExtensionMock() : code_(0) {} ~BackupExtExtensionMock()34 virtual ~BackupExtExtensionMock() {} 35 36 MOCK_METHOD4(SendRequest, int(uint32_t, MessageParcel &, MessageParcel &, MessageOption &)); 37 InvokeSendRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int32_t InvokeSendRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 39 { 40 reply.WriteInt32(BError(BError::Codes::OK)); 41 return BError(BError::Codes::OK); 42 } 43 InvokeGetFileHandleRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t InvokeGetFileHandleRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) 45 { 46 std::string fileName = "1.tar"; 47 TestManager tm("GetFileHand_GetFd_0100"); 48 std::string filePath = tm.GetRootDirCurTest().append(fileName); 49 UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)); 50 reply.WriteBool(true); 51 reply.WriteInt32(0); 52 reply.WriteFileDescriptor(fd); 53 return BError(BError::Codes::OK); 54 } 55 GetFileHandle(const std::string & fileName,int32_t & errCode)56 UniqueFd GetFileHandle(const std::string &fileName, int32_t &errCode) override 57 { 58 GTEST_LOG_(INFO) << "GetFileHandle" << fileName; 59 if (fileName == "testName") { 60 errCode = BError::BackupErrorCode::E_UKERR; 61 return UniqueFd(-1); 62 } 63 64 if (fileName.empty()) { 65 errCode = BError::BackupErrorCode::E_UKERR; 66 return UniqueFd(-1); 67 } 68 TestManager tm("GetFileHand_GetFd_0200"); 69 std::string filePath = tm.GetRootDirCurTest().append(fileName); 70 UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)); 71 if (fd < 0) { 72 errCode = BError::GetCodeByErrno(errno); 73 } 74 return fd; 75 }; 76 HandleClear()77 ErrCode HandleClear() override 78 { 79 return BError(BError::Codes::OK); 80 }; 81 HandleBackup(bool isClearData)82 ErrCode HandleBackup(bool isClearData) override 83 { 84 GTEST_LOG_(INFO) << "HandleBackup"; 85 if (nHandleBackupNum_ == 1) { 86 GTEST_LOG_(INFO) << "HandleBackup is false"; 87 return 1; 88 } 89 nHandleBackupNum_++; 90 return BError(BError::Codes::OK); 91 }; 92 PublishFile(const std::string & fileName)93 ErrCode PublishFile(const std::string &fileName) override 94 { 95 GTEST_LOG_(INFO) << "PublishFile " << fileName; 96 if (fileName == "test") { 97 return 1; 98 } 99 return BError(BError::Codes::OK); 100 }; 101 HandleRestore(bool isClearData)102 ErrCode HandleRestore(bool isClearData) override 103 { 104 return BError(BError::Codes::OK); 105 }; 106 GetIncrementalFileHandle(const std::string & fileName)107 ErrCode GetIncrementalFileHandle(const std::string &fileName) override 108 { 109 return BError(BError::Codes::OK); 110 }; 111 PublishIncrementalFile(const std::string & fileName)112 ErrCode PublishIncrementalFile(const std::string &fileName) override 113 { 114 return BError(BError::Codes::OK); 115 }; 116 HandleIncrementalBackup(UniqueFd incrementalFd,UniqueFd manifestFd)117 ErrCode HandleIncrementalBackup(UniqueFd incrementalFd, UniqueFd manifestFd) override 118 { 119 return BError(BError::Codes::OK); 120 }; 121 IncrementalOnBackup(bool isClearData)122 ErrCode IncrementalOnBackup(bool isClearData) override 123 { 124 return BError(BError::Codes::OK); 125 }; 126 GetIncrementalBackupFileHandle()127 std::tuple<UniqueFd, UniqueFd> GetIncrementalBackupFileHandle() override 128 { 129 return {UniqueFd(-1), UniqueFd(-1)}; 130 }; 131 GetBackupInfo(std::string & result)132 ErrCode GetBackupInfo(std::string &result) override 133 { 134 return BError(BError::Codes::OK); 135 }; 136 UpdateFdSendRate(std::string & bundleName,int32_t sendRate)137 ErrCode UpdateFdSendRate(std::string &bundleName, int32_t sendRate) override 138 { 139 return BError(BError::Codes::OK); 140 }; 141 User0OnBackup()142 ErrCode User0OnBackup() override 143 { 144 return BError(BError::Codes::OK); 145 }; 146 147 private: 148 int32_t nHandleBackupNum_ = 0; 149 }; 150 } // namespace OHOS::FileManagement::Backup 151 #endif // MOCK_EXTENSION_MOCK_H