rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1 | // 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_FILE_WRITER_H__ |
| 6 | #define CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_FILE_WRITER_H__ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
| 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 | |
| 15 | namespace chromeos_update_engine { |
| 16 | |
| 17 | class 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 Reyes | 0cca421 | 2010-04-29 14:00:58 -0700 | [diff] [blame] | 28 | virtual ssize_t Write(const void* bytes, size_t count) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 | 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.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 59 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_MOCK_FILE_WRITER_H__ |