blob: 7ac50632ca4a0fe0d24cb95a7a21ce2cc53c8723 [file] [log] [blame]
Alex Deymofb3b6322017-09-27 14:28:54 +02001// 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
13namespace bsdiff {
14
15// A fake PatchWriterInterface derived class with easy access to the data passed
16// to it.
17class FakePatchWriter : public PatchWriterInterface {
18 public:
19 FakePatchWriter() = default;
20 ~FakePatchWriter() override = default;
21
22 // PatchWriterInterface overrides.
Alex Deymo4dadd8b2017-10-26 16:19:33 +020023 bool Init(size_t new_size) override {
Alex Deymofb3b6322017-09-27 14:28:54 +020024 EXPECT_FALSE(initialized_);
25 initialized_ = true;
Alex Deymo4dadd8b2017-10-26 16:19:33 +020026 new_size_ = new_size;
Alex Deymofb3b6322017-09-27 14:28:54 +020027 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 Deymo68c0e7f2017-10-02 20:38:12 +020037 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 Deymofb3b6322017-09-27 14:28:54 +020047 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 Deymo68c0e7f2017-10-02 20:38:12 +020055 const std::vector<uint8_t>& diff_stream() const { return diff_stream_; }
56 const std::vector<uint8_t>& extra_stream() const { return extra_stream_; }
Alex Deymo4dadd8b2017-10-26 16:19:33 +020057 size_t new_size() const { return new_size_; }
Alex Deymofb3b6322017-09-27 14:28:54 +020058
59 private:
60 // The list of ControlEntry passed to this class.
61 std::vector<ControlEntry> entries_;
Alex Deymo68c0e7f2017-10-02 20:38:12 +020062 std::vector<uint8_t> diff_stream_;
63 std::vector<uint8_t> extra_stream_;
Alex Deymofb3b6322017-09-27 14:28:54 +020064
Alex Deymo4dadd8b2017-10-26 16:19:33 +020065 // The size of the new file for the patch we are writing.
66 size_t new_size_{0};
67
Alex Deymofb3b6322017-09-27 14:28:54 +020068 // Whether this class was initialized.
69 bool initialized_{false};
70
71 // Whether the patch was closed.
72 bool closed_{false};
Alex Deymofb3b6322017-09-27 14:28:54 +020073};
74
75} // namespace bsdiff
76
77#endif // _BSDIFF_FAKE_PATCH_WRITER_H_