blob: f14b6ff5482cf88a2abae3a1c59f1c7d1d5e9264 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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
Gilad Arnoldcf175a02014-07-10 16:48:47 -07005#ifndef UPDATE_ENGINE_MOCK_FILE_WRITER_H_
6#define UPDATE_ENGINE_MOCK_FILE_WRITER_H_
rspangler@google.com49fdf182009-10-10 00:57:34 +00007
Alex Vakulenkod2779df2014-06-16 13:19:00 -07008#include <vector>
9
rspangler@google.com49fdf182009-10-10 00:57:34 +000010#include "base/basictypes.h"
11#include "update_engine/file_writer.h"
12
13// MockFileWriter is an implementation of FileWriter. It will succeed
14// calls to Open(), Close(), but not do any work. All calls to Write()
15// will append the passed data to an internal vector.
16
17namespace chromeos_update_engine {
18
19class MockFileWriter : public FileWriter {
20 public:
21 MockFileWriter() : was_opened_(false), was_closed_(false) {}
22
23 virtual int Open(const char* path, int flags, mode_t mode) {
24 CHECK(!was_opened_);
25 CHECK(!was_closed_);
26 was_opened_ = true;
27 return 0;
28 }
29
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070030 virtual ssize_t Write(const void* bytes, size_t count) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000031 CHECK(was_opened_);
32 CHECK(!was_closed_);
33 const char* char_bytes = reinterpret_cast<const char*>(bytes);
34 bytes_.insert(bytes_.end(), char_bytes, char_bytes + count);
35 return count;
36 }
37
38 virtual int Close() {
39 CHECK(was_opened_);
40 CHECK(!was_closed_);
41 was_closed_ = true;
42 return 0;
43 }
44
45 const std::vector<char>& bytes() {
46 return bytes_;
47 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -070048
rspangler@google.com49fdf182009-10-10 00:57:34 +000049 private:
50 // The internal store of all bytes that have been written
51 std::vector<char> bytes_;
52
53 // These are just to ensure FileWriter methods are called properly.
54 bool was_opened_;
55 bool was_closed_;
56
57 DISALLOW_COPY_AND_ASSIGN(MockFileWriter);
58};
59
60} // namespace chromeos_update_engine
61
Gilad Arnoldcf175a02014-07-10 16:48:47 -070062#endif // UPDATE_ENGINE_MOCK_FILE_WRITER_H_