Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 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. |
Alex Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 23 | bool Init(size_t new_size) override { |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 24 | EXPECT_FALSE(initialized_); |
| 25 | initialized_ = true; |
Alex Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 26 | new_size_ = new_size; |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 27 | return true; |
| 28 | } |
| 29 | |
| 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 | |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 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 | |
| 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 | |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 47 | bool Close() override { |
| 48 | EXPECT_FALSE(closed_) << "Close() already called"; |
| 49 | closed_ = true; |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | // Fake getter methods. |
| 54 | const std::vector<ControlEntry>& entries() const { return entries_; } |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 55 | const std::vector<uint8_t>& diff_stream() const { return diff_stream_; } |
| 56 | const std::vector<uint8_t>& extra_stream() const { return extra_stream_; } |
Alex Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 57 | size_t new_size() const { return new_size_; } |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | // The list of ControlEntry passed to this class. |
| 61 | std::vector<ControlEntry> entries_; |
Alex Deymo | 68c0e7f | 2017-10-02 20:38:12 +0200 | [diff] [blame] | 62 | std::vector<uint8_t> diff_stream_; |
| 63 | std::vector<uint8_t> extra_stream_; |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 64 | |
Alex Deymo | 4dadd8b | 2017-10-26 16:19:33 +0200 | [diff] [blame] | 65 | // The size of the new file for the patch we are writing. |
| 66 | size_t new_size_{0}; |
| 67 | |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 68 | // Whether this class was initialized. |
| 69 | bool initialized_{false}; |
| 70 | |
| 71 | // Whether the patch was closed. |
| 72 | bool closed_{false}; |
Alex Deymo | fb3b632 | 2017-09-27 14:28:54 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | } // namespace bsdiff |
| 76 | |
| 77 | #endif // _BSDIFF_FAKE_PATCH_WRITER_H_ |