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 
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>
23 #include <brillo/secure_blob.h>
24 
25 #include "update_engine/payload_consumer/file_writer.h"
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:
FakeFileWriter()35   FakeFileWriter() : was_opened_(false), was_closed_(false) {}
36 
Open(const char * path,int flags,mode_t mode)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 
Write(const void * bytes,size_t count)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 
Close()52   virtual int Close() {
53     CHECK(was_opened_);
54     CHECK(!was_closed_);
55     was_closed_ = true;
56     return 0;
57   }
58 
bytes()59   const brillo::Blob& bytes() {
60     return bytes_;
61   }
62 
63  private:
64   // The internal store of all bytes that have been written
65   brillo::Blob bytes_;
66 
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_
77