blob: 6ba550d17d8a3e34b84a5a90812338106b08c0bc [file] [log] [blame]
Alex Deymo8427b4a2014-11-05 14:00:32 -08001// Copyright (c) 2009 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 UPDATE_ENGINE_FAKE_FILE_WRITER_H_
6#define UPDATE_ENGINE_FAKE_FILE_WRITER_H_
7
8#include <vector>
9
10#include <base/macros.h>
11
12#include "update_engine/file_writer.h"
13
14// FakeFileWriter is an implementation of FileWriter. It will succeed
15// calls to Open(), Close(), but not do any work. All calls to Write()
16// will append the passed data to an internal vector.
17
18namespace chromeos_update_engine {
19
20class FakeFileWriter : public FileWriter {
21 public:
22 FakeFileWriter() : was_opened_(false), was_closed_(false) {}
23
24 virtual int Open(const char* path, int flags, mode_t mode) {
25 CHECK(!was_opened_);
26 CHECK(!was_closed_);
27 was_opened_ = true;
28 return 0;
29 }
30
31 virtual ssize_t Write(const void* bytes, size_t count) {
32 CHECK(was_opened_);
33 CHECK(!was_closed_);
34 const char* char_bytes = reinterpret_cast<const char*>(bytes);
35 bytes_.insert(bytes_.end(), char_bytes, char_bytes + count);
36 return count;
37 }
38
39 virtual int Close() {
40 CHECK(was_opened_);
41 CHECK(!was_closed_);
42 was_closed_ = true;
43 return 0;
44 }
45
46 const std::vector<char>& bytes() {
47 return bytes_;
48 }
49
50 private:
51 // The internal store of all bytes that have been written
52 std::vector<char> bytes_;
53
54 // These are just to ensure FileWriter methods are called properly.
55 bool was_opened_;
56 bool was_closed_;
57
58 DISALLOW_COPY_AND_ASSIGN(FakeFileWriter);
59};
60
61} // namespace chromeos_update_engine
62
63#endif // UPDATE_ENGINE_FAKE_FILE_WRITER_H_