Home | History | Annotate | Download | only in payload_consumer
      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 "update_engine/payload_consumer/file_descriptor.h"
     26 
     27 namespace chromeos_update_engine {
     28 
     29 // A fake file descriptor with configurable errors. The file descriptor always
     30 // reads a fixed sequence of bytes, consisting of the concatenation of the
     31 // numbers 0, 1, 2... each one encoded in 4 bytes as the big-endian 16-bit
     32 // number encoded in hexadecimal. For example, the beginning of the stream in
     33 // ASCII is 0000000100020003... which corresponds to the numbers 0, 1, 2 and 3.
     34 class FakeFileDescriptor : public FileDescriptor {
     35  public:
     36   FakeFileDescriptor() = default;
     37   ~FakeFileDescriptor() override = default;
     38 
     39   // FileDescriptor override methods.
     40   bool Open(const char* path, int flags, mode_t mode) override {
     41     if (open_)
     42       return false;
     43     open_ = true;
     44     return true;
     45   }
     46 
     47   bool Open(const char* path, int flags) override {
     48     return Open(path, flags, 0);
     49   }
     50 
     51   ssize_t Read(void* buf, size_t count) override;
     52 
     53   ssize_t Write(const void* buf, size_t count) override {
     54     // Read-only block device.
     55     errno = EROFS;
     56     return -1;
     57   }
     58 
     59   off64_t Seek(off64_t offset, int whence) override;
     60 
     61   uint64_t BlockDevSize() override { return size_; }
     62 
     63   bool BlkIoctl(int request,
     64                 uint64_t start,
     65                 uint64_t length,
     66                 int* result) override {
     67     return false;
     68   }
     69 
     70   bool Flush() override {
     71     return open_;
     72   }
     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 }  // namespace chromeos_update_engine
    125 
    126 #endif  // UPDATE_ENGINE_PAYLOAD_CONSUMER_FAKE_FILE_DESCRIPTOR_H_
    127