1 // 2 // Copyright (C) 2017 The Android Open Source Project 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #ifndef UPDATE_ENGINE_PAYLOAD_CONSUMER_FAKE_FILE_DESCRIPTOR_H_ 18 #define UPDATE_ENGINE_PAYLOAD_CONSUMER_FAKE_FILE_DESCRIPTOR_H_ 19 20 #include <algorithm> 21 #include <limits> 22 #include <utility> 23 #include <vector> 24 25 #include <brillo/secure_blob.h> 26 27 #include "update_engine/payload_consumer/file_descriptor.h" 28 29 namespace chromeos_update_engine { 30 31 // A fake file descriptor with configurable errors. The file descriptor always 32 // reads a fixed sequence of bytes, consisting of the concatenation of the 33 // numbers 0, 1, 2... each one encoded in 4 bytes as the big-endian 16-bit 34 // number encoded in hexadecimal. For example, the beginning of the stream in 35 // ASCII is 0000000100020003... which corresponds to the numbers 0, 1, 2 and 3. 36 class FakeFileDescriptor : public FileDescriptor { 37 public: 38 FakeFileDescriptor() = default; 39 ~FakeFileDescriptor() override = default; 40 41 // FileDescriptor override methods. 42 bool Open(const char* path, int flags, mode_t mode) override { 43 if (open_) 44 return false; 45 open_ = true; 46 return true; 47 } 48 49 bool Open(const char* path, int flags) override { 50 return Open(path, flags, 0); 51 } 52 53 ssize_t Read(void* buf, size_t count) override; 54 55 ssize_t Write(const void* buf, size_t count) override { 56 // Read-only block device. 57 errno = EROFS; 58 return -1; 59 } 60 61 off64_t Seek(off64_t offset, int whence) override; 62 63 uint64_t BlockDevSize() override { return size_; } 64 65 bool BlkIoctl(int request, 66 uint64_t start, 67 uint64_t length, 68 int* result) override { 69 return false; 70 } 71 72 bool Flush() override { return open_; } 73 74 bool Close() override { 75 if (!open_) 76 return false; 77 open_ = false; 78 return true; 79 } 80 81 bool IsSettingErrno() override { return true; } 82 83 bool IsOpen() override { return open_; } 84 85 // Fake class configuration methods. 86 87 // Set the size of the file. 88 void SetFileSize(uint64_t size) { size_ = size; } 89 90 // Marks the range starting from |offset| bytes into the file and |length| 91 // size as a failure range. Reads from this range will always fail. 92 void AddFailureRange(uint64_t offset, uint64_t length) { 93 if (length == 0) 94 return; 95 failure_ranges_.emplace_back(offset, length); 96 } 97 98 // Return the list of ranges of bytes requested with a Read() as (offset, 99 // length), regardless of the Read() return value. 100 std::vector<std::pair<uint64_t, uint64_t>> GetReadOps() const { 101 return read_ops_; 102 } 103 104 private: 105 // Whether the fake file is open. 106 bool open_{false}; 107 108 // The current file pointer offset into the fake file. 109 uint64_t offset_{0}; 110 111 // The size of the file. Reads beyond |max_size_| will an EOF condition. 112 off64_t size_{std::numeric_limits<off64_t>::max()}; 113 114 // The list of ranges represented as (start, length) in bytes where reads will 115 // always fail. 116 std::vector<std::pair<uint64_t, uint64_t>> failure_ranges_; 117 118 // List of reads performed as (offset, length) of the read request. 119 std::vector<std::pair<uint64_t, uint64_t>> read_ops_; 120 121 DISALLOW_COPY_AND_ASSIGN(FakeFileDescriptor); 122 }; 123 124 // Return a blob with the first |size| bytes of a FakeFileDescriptor stream. 125 brillo::Blob FakeFileDescriptorData(size_t size); 126 127 } // namespace chromeos_update_engine 128 129 #endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_FAKE_FILE_DESCRIPTOR_H_ 130