blob: 5231e402caf63824b0af9a22348b42f30633eef2 [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
Ben Chan05735a12014-09-03 07:48:22 -070010#include <base/macros.h>
11
rspangler@google.com49fdf182009-10-10 00:57:34 +000012#include "update_engine/file_writer.h"
13
14// MockFileWriter 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 MockFileWriter : public FileWriter {
21 public:
22 MockFileWriter() : 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
Andrew de los Reyes0cca4212010-04-29 14:00:58 -070031 virtual ssize_t Write(const void* bytes, size_t count) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000032 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 }
Alex Vakulenkod2779df2014-06-16 13:19:00 -070049
rspangler@google.com49fdf182009-10-10 00:57:34 +000050 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(MockFileWriter);
59};
60
61} // namespace chromeos_update_engine
62
Gilad Arnoldcf175a02014-07-10 16:48:47 -070063#endif // UPDATE_ENGINE_MOCK_FILE_WRITER_H_