blob: 43b71c7e7d67687fa088cd77c6b27b4bfeb5683c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymo8427b4a2014-11-05 14:00:32 -080016
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 Vakulenko3f39d5c2015-10-13 09:27:13 -070023#include <brillo/secure_blob.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080024
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/payload_consumer/file_writer.h"
Alex Deymo8427b4a2014-11-05 14:00:32 -080026
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
31namespace chromeos_update_engine {
32
33class 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
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070059 const brillo::Blob& bytes() {
Alex Deymo8427b4a2014-11-05 14:00:32 -080060 return bytes_;
61 }
62
63 private:
64 // The internal store of all bytes that have been written
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070065 brillo::Blob bytes_;
Alex Deymo8427b4a2014-11-05 14:00:32 -080066
67 // These are just to ensure FileWriter methods are called properly.
68 bool was_opened_;
69 bool was_closed_;
70
71 DISALLOW_COPY_AND_ASSIGN(FakeFileWriter);
72};
73
74} // namespace chromeos_update_engine
75
76#endif // UPDATE_ENGINE_FAKE_FILE_WRITER_H_