Home | History | Annotate | Download | only in tests
      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   bool CreatedNestedDirs(
     47       const std::string& base_dir,
     48       const std::vector<std::string>& nested_subdirs) const override;
     49   std::unique_ptr<CodeWriter> GetCodeWriter(
     50       const std::string& file_path) const override;
     51   void RemovePath(const std::string& file_path) const override;
     52 
     53   // Methods added to facilitate testing.
     54   void SetFileContents(const std::string& filename,
     55                        const std::string& contents);
     56   void AddStubParcelable(const std::string& canonical_name,
     57                          const std::string& cpp_header);
     58   void AddStubInterface(const std::string& canonical_name);
     59   void AddCompoundParcelable(const std::string& canonical_name,
     60                              const std::vector<std::string>& subclasses);
     61   void AddBrokenFilePath(const std::string& path);
     62   // Returns true iff we've previously written to |path|.
     63   // When we return true, we'll set *contents to the written string.
     64   bool GetWrittenContents(const std::string& path, std::string* content);
     65 
     66   bool PathWasRemoved(const std::string& path);
     67 
     68  private:
     69   // Remove leading "./" from |path|.
     70   std::string CleanPath(const std::string& path) const;
     71 
     72   std::map<std::string, std::string> file_contents_;
     73   // Normally, writing to files leaves the IoDelegate unchanged, so
     74   // GetCodeWriter is a const method.  However, for tests, we break this
     75   // intentionally by storing the written strings.
     76   mutable std::map<std::string, std::string> written_file_contents_;
     77 
     78   // We normally just write to strings in |written_file_contents_| but for
     79   // files in this list, we simulate I/O errors.
     80   std::set<std::string> broken_files_;
     81   mutable std::set<std::string> removed_files_;
     82 
     83   DISALLOW_COPY_AND_ASSIGN(FakeIoDelegate);
     84 };  // class FakeIoDelegate
     85 
     86 }  // namespace test
     87 }  // namespace android
     88 }  // namespace aidl
     89 
     90 #endif // AIDL_TESTS_FAKE_IO_DELEGATE_H_
     91