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