1 // Copyright 2017 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef _BSDIFF_FAKE_PATCH_WRITER_H_ 6 #define _BSDIFF_FAKE_PATCH_WRITER_H_ 7 8 #include <gtest/gtest.h> 9 #include <vector> 10 11 #include "bsdiff/patch_writer_interface.h" 12 13 namespace bsdiff { 14 15 // A fake PatchWriterInterface derived class with easy access to the data passed 16 // to it. 17 class FakePatchWriter : public PatchWriterInterface { 18 public: 19 FakePatchWriter() = default; 20 ~FakePatchWriter() override = default; 21 22 // PatchWriterInterface overrides. Init(size_t new_size)23 bool Init(size_t new_size) override { 24 EXPECT_FALSE(initialized_); 25 initialized_ = true; 26 new_size_ = new_size; 27 return true; 28 } 29 AddControlEntry(const ControlEntry & entry)30 bool AddControlEntry(const ControlEntry& entry) override { 31 EXPECT_TRUE(initialized_); 32 EXPECT_FALSE(closed_); 33 entries_.push_back(entry); 34 return true; 35 } 36 WriteDiffStream(const uint8_t * data,size_t size)37 bool WriteDiffStream(const uint8_t* data, size_t size) override { 38 diff_stream_.insert(diff_stream_.end(), data, data + size); 39 return true; 40 } 41 WriteExtraStream(const uint8_t * data,size_t size)42 bool WriteExtraStream(const uint8_t* data, size_t size) override { 43 extra_stream_.insert(extra_stream_.end(), data, data + size); 44 return true; 45 } 46 Close()47 bool Close() override { 48 EXPECT_FALSE(closed_) << "Close() already called"; 49 closed_ = true; 50 return true; 51 } 52 53 // Fake getter methods. entries()54 const std::vector<ControlEntry>& entries() const { return entries_; } diff_stream()55 const std::vector<uint8_t>& diff_stream() const { return diff_stream_; } extra_stream()56 const std::vector<uint8_t>& extra_stream() const { return extra_stream_; } new_size()57 size_t new_size() const { return new_size_; } 58 59 private: 60 // The list of ControlEntry passed to this class. 61 std::vector<ControlEntry> entries_; 62 std::vector<uint8_t> diff_stream_; 63 std::vector<uint8_t> extra_stream_; 64 65 // The size of the new file for the patch we are writing. 66 size_t new_size_{0}; 67 68 // Whether this class was initialized. 69 bool initialized_{false}; 70 71 // Whether the patch was closed. 72 bool closed_{false}; 73 }; 74 75 } // namespace bsdiff 76 77 #endif // _BSDIFF_FAKE_PATCH_WRITER_H_ 78