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 | |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_MOCK_FILE_WRITER_H_ |
| 6 | #define UPDATE_ENGINE_MOCK_FILE_WRITER_H_ |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
Ben Chan | 05735a1 | 2014-09-03 07:48:22 -0700 | [diff] [blame^] | 10 | #include <base/macros.h> |
| 11 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 12 | #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 | |
| 18 | namespace chromeos_update_engine { |
| 19 | |
| 20 | class 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 Reyes | 0cca421 | 2010-04-29 14:00:58 -0700 | [diff] [blame] | 31 | virtual ssize_t Write(const void* bytes, size_t count) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 32 | 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 Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 49 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 50 | 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 Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 63 | #endif // UPDATE_ENGINE_MOCK_FILE_WRITER_H_ |