fake_file_writer.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. //
  16. #ifndef UPDATE_ENGINE_FAKE_FILE_WRITER_H_
  17. #define UPDATE_ENGINE_FAKE_FILE_WRITER_H_
  18. #include <vector>
  19. #include <base/macros.h>
  20. #include <brillo/secure_blob.h>
  21. #include "update_engine/payload_consumer/file_writer.h"
  22. // FakeFileWriter is an implementation of FileWriter. It will succeed
  23. // calls to Open(), Close(), but not do any work. All calls to Write()
  24. // will append the passed data to an internal vector.
  25. namespace chromeos_update_engine {
  26. class FakeFileWriter : public FileWriter {
  27. public:
  28. FakeFileWriter() : was_opened_(false), was_closed_(false) {}
  29. virtual int Open(const char* path, int flags, mode_t mode) {
  30. CHECK(!was_opened_);
  31. CHECK(!was_closed_);
  32. was_opened_ = true;
  33. return 0;
  34. }
  35. virtual ssize_t Write(const void* bytes, size_t count) {
  36. CHECK(was_opened_);
  37. CHECK(!was_closed_);
  38. const char* char_bytes = reinterpret_cast<const char*>(bytes);
  39. bytes_.insert(bytes_.end(), char_bytes, char_bytes + count);
  40. return count;
  41. }
  42. virtual int Close() {
  43. CHECK(was_opened_);
  44. CHECK(!was_closed_);
  45. was_closed_ = true;
  46. return 0;
  47. }
  48. const brillo::Blob& bytes() { return bytes_; }
  49. private:
  50. // The internal store of all bytes that have been written
  51. brillo::Blob bytes_;
  52. // These are just to ensure FileWriter methods are called properly.
  53. bool was_opened_;
  54. bool was_closed_;
  55. DISALLOW_COPY_AND_ASSIGN(FakeFileWriter);
  56. };
  57. } // namespace chromeos_update_engine
  58. #endif // UPDATE_ENGINE_FAKE_FILE_WRITER_H_