Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [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 | |
| 5 | #ifndef UPDATE_ENGINE_FAKE_FILE_WRITER_H_ |
| 6 | #define UPDATE_ENGINE_FAKE_FILE_WRITER_H_ |
| 7 | |
| 8 | #include <vector> |
| 9 | |
| 10 | #include <base/macros.h> |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 11 | #include <chromeos/secure_blob.h> |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 12 | |
| 13 | #include "update_engine/file_writer.h" |
| 14 | |
| 15 | // FakeFileWriter is an implementation of FileWriter. It will succeed |
| 16 | // calls to Open(), Close(), but not do any work. All calls to Write() |
| 17 | // will append the passed data to an internal vector. |
| 18 | |
| 19 | namespace chromeos_update_engine { |
| 20 | |
| 21 | class FakeFileWriter : public FileWriter { |
| 22 | public: |
| 23 | FakeFileWriter() : was_opened_(false), was_closed_(false) {} |
| 24 | |
| 25 | virtual int Open(const char* path, int flags, mode_t mode) { |
| 26 | CHECK(!was_opened_); |
| 27 | CHECK(!was_closed_); |
| 28 | was_opened_ = true; |
| 29 | return 0; |
| 30 | } |
| 31 | |
| 32 | virtual ssize_t Write(const void* bytes, size_t count) { |
| 33 | CHECK(was_opened_); |
| 34 | CHECK(!was_closed_); |
| 35 | const char* char_bytes = reinterpret_cast<const char*>(bytes); |
| 36 | bytes_.insert(bytes_.end(), char_bytes, char_bytes + count); |
| 37 | return count; |
| 38 | } |
| 39 | |
| 40 | virtual int Close() { |
| 41 | CHECK(was_opened_); |
| 42 | CHECK(!was_closed_); |
| 43 | was_closed_ = true; |
| 44 | return 0; |
| 45 | } |
| 46 | |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 47 | const chromeos::Blob& bytes() { |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 48 | return bytes_; |
| 49 | } |
| 50 | |
| 51 | private: |
| 52 | // The internal store of all bytes that have been written |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame^] | 53 | chromeos::Blob bytes_; |
Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 54 | |
| 55 | // These are just to ensure FileWriter methods are called properly. |
| 56 | bool was_opened_; |
| 57 | bool was_closed_; |
| 58 | |
| 59 | DISALLOW_COPY_AND_ASSIGN(FakeFileWriter); |
| 60 | }; |
| 61 | |
| 62 | } // namespace chromeos_update_engine |
| 63 | |
| 64 | #endif // UPDATE_ENGINE_FAKE_FILE_WRITER_H_ |