| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
 | 2 | // Copyright (C) 2009 The Android Open Source Project | 
 | 3 | // | 
 | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 | // you may not use this file except in compliance with the License. | 
 | 6 | // You may obtain a copy of the License at | 
 | 7 | // | 
 | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 | // | 
 | 10 | // Unless required by applicable law or agreed to in writing, software | 
 | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 | // See the License for the specific language governing permissions and | 
 | 14 | // limitations under the License. | 
 | 15 | // | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 16 |  | 
 | 17 | #ifndef UPDATE_ENGINE_FAKE_FILE_WRITER_H_ | 
 | 18 | #define UPDATE_ENGINE_FAKE_FILE_WRITER_H_ | 
 | 19 |  | 
 | 20 | #include <vector> | 
 | 21 |  | 
 | 22 | #include <base/macros.h> | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 23 | #include <brillo/secure_blob.h> | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 24 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 25 | #include "update_engine/payload_consumer/file_writer.h" | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 26 |  | 
 | 27 | // FakeFileWriter is an implementation of FileWriter. It will succeed | 
 | 28 | // calls to Open(), Close(), but not do any work. All calls to Write() | 
 | 29 | // will append the passed data to an internal vector. | 
 | 30 |  | 
 | 31 | namespace chromeos_update_engine { | 
 | 32 |  | 
 | 33 | class FakeFileWriter : public FileWriter { | 
 | 34 |  public: | 
 | 35 |   FakeFileWriter() : was_opened_(false), was_closed_(false) {} | 
 | 36 |  | 
 | 37 |   virtual int Open(const char* path, int flags, mode_t mode) { | 
 | 38 |     CHECK(!was_opened_); | 
 | 39 |     CHECK(!was_closed_); | 
 | 40 |     was_opened_ = true; | 
 | 41 |     return 0; | 
 | 42 |   } | 
 | 43 |  | 
 | 44 |   virtual ssize_t Write(const void* bytes, size_t count) { | 
 | 45 |     CHECK(was_opened_); | 
 | 46 |     CHECK(!was_closed_); | 
 | 47 |     const char* char_bytes = reinterpret_cast<const char*>(bytes); | 
 | 48 |     bytes_.insert(bytes_.end(), char_bytes, char_bytes + count); | 
 | 49 |     return count; | 
 | 50 |   } | 
 | 51 |  | 
 | 52 |   virtual int Close() { | 
 | 53 |     CHECK(was_opened_); | 
 | 54 |     CHECK(!was_closed_); | 
 | 55 |     was_closed_ = true; | 
 | 56 |     return 0; | 
 | 57 |   } | 
 | 58 |  | 
| Amin Hassani | 7cc8bb0 | 2019-01-14 16:29:47 -0800 | [diff] [blame] | 59 |   const brillo::Blob& bytes() { return bytes_; } | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 60 |  | 
 | 61 |  private: | 
 | 62 |   // The internal store of all bytes that have been written | 
| Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 63 |   brillo::Blob bytes_; | 
| Alex Deymo | 8427b4a | 2014-11-05 14:00:32 -0800 | [diff] [blame] | 64 |  | 
 | 65 |   // These are just to ensure FileWriter methods are called properly. | 
 | 66 |   bool was_opened_; | 
 | 67 |   bool was_closed_; | 
 | 68 |  | 
 | 69 |   DISALLOW_COPY_AND_ASSIGN(FakeFileWriter); | 
 | 70 | }; | 
 | 71 |  | 
 | 72 | }  // namespace chromeos_update_engine | 
 | 73 |  | 
 | 74 | #endif  // UPDATE_ENGINE_FAKE_FILE_WRITER_H_ |