1 /* 2 * Copyright (c) 2022-2023 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.WriteFileDescriptor(fd); 51 return BError(BError::Codes::OK); 52 } 53 GetFileHandle(const std::string & fileName)54 UniqueFd GetFileHandle(const std::string &fileName) override 55 { 56 GTEST_LOG_(INFO) << "GetFileHandle" << fileName; 57 if (fileName == "testName") { 58 return UniqueFd(-1); 59 } 60 61 if (fileName.empty()) { 62 return UniqueFd(-1); 63 } 64 TestManager tm("GetFileHand_GetFd_0200"); 65 std::string filePath = tm.GetRootDirCurTest().append(fileName); 66 UniqueFd fd(open(filePath.data(), O_RDONLY | O_CREAT, S_IRUSR | S_IWUSR)); 67 return fd; 68 }; 69 HandleClear()70 ErrCode HandleClear() override 71 { 72 return BError(BError::Codes::OK); 73 }; 74 HandleBackup()75 ErrCode HandleBackup() override 76 { 77 GTEST_LOG_(INFO) << "HandleBackup"; 78 if (nHandleBackupNum_ == 1) { 79 GTEST_LOG_(INFO) << "HandleBackup is false"; 80 return 1; 81 } 82 nHandleBackupNum_++; 83 return BError(BError::Codes::OK); 84 }; 85 PublishFile(const std::string & fileName)86 ErrCode PublishFile(const std::string &fileName) override 87 { 88 GTEST_LOG_(INFO) << "PublishFile " << fileName; 89 if (fileName == "test") { 90 return 1; 91 } 92 return BError(BError::Codes::OK); 93 }; 94 95 private: 96 int32_t nHandleBackupNum_ = 0; 97 }; 98 } // namespace OHOS::FileManagement::Backup 99 #endif // MOCK_EXTENSION_MOCK_H