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