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