• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015, 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 AIDL_TESTS_FAKE_IO_DELEGATE_H_
18 #define AIDL_TESTS_FAKE_IO_DELEGATE_H_
19 
20 #include <android-base/macros.h>
21 
22 #include <map>
23 #include <memory>
24 #include <set>
25 #include <string>
26 #include <vector>
27 
28 #include "io_delegate.h"
29 
30 namespace android {
31 namespace aidl {
32 namespace test {
33 
34 class FakeIoDelegate : public IoDelegate {
35  public:
36   FakeIoDelegate() = default;
37   virtual ~FakeIoDelegate() = default;
38 
39   // Overrides from the real IoDelegate
40   std::unique_ptr<std::string> GetFileContents(
41       const std::string& filename,
42       const std::string& append_content_suffix = "") const override;
43   std::unique_ptr<LineReader> GetLineReader(
44       const std::string& file_path) const override;
45   bool FileIsReadable(const std::string& path) const override;
46   std::unique_ptr<CodeWriter> GetCodeWriter(
47       const std::string& file_path) const override;
48   void RemovePath(const std::string& file_path) const override;
49   std::vector<std::string> ListFiles(const std::string& dir) const override;
50 
51   // Methods added to facilitate testing.
52   void SetFileContents(const std::string& filename,
53                        const std::string& contents);
54   void AddStubParcelable(const std::string& canonical_name,
55                          const std::string& cpp_header);
56   void AddStubInterface(const std::string& canonical_name);
57   void AddCompoundParcelable(const std::string& canonical_name,
58                              const std::vector<std::string>& subclasses);
59   void AddBrokenFilePath(const std::string& path);
60   // Returns true iff we've previously written to |path|.
61   // When we return true, we'll set *contents to the written string.
62   bool GetWrittenContents(const std::string& path, std::string* content);
63 
64   bool PathWasRemoved(const std::string& path);
65 
66  private:
67   // Remove leading "./" from |path|.
68   std::string CleanPath(const std::string& path) const;
69 
70   std::map<std::string, std::string> file_contents_;
71   // Normally, writing to files leaves the IoDelegate unchanged, so
72   // GetCodeWriter is a const method.  However, for tests, we break this
73   // intentionally by storing the written strings.
74   mutable std::map<std::string, std::string> written_file_contents_;
75 
76   // We normally just write to strings in |written_file_contents_| but for
77   // files in this list, we simulate I/O errors.
78   std::set<std::string> broken_files_;
79   mutable std::set<std::string> removed_files_;
80 
81   DISALLOW_COPY_AND_ASSIGN(FakeIoDelegate);
82 };  // class FakeIoDelegate
83 
84 }  // namespace test
85 }  // namespace android
86 }  // namespace aidl
87 
88 #endif // AIDL_TESTS_FAKE_IO_DELEGATE_H_
89