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 #pragma once 17 18 #include <map> 19 #include <memory> 20 #include <set> 21 #include <string> 22 #include <vector> 23 24 #include "io_delegate.h" 25 26 namespace android { 27 namespace aidl { 28 namespace test { 29 30 class FakeIoDelegate : public IoDelegate { 31 public: 32 FakeIoDelegate() = default; 33 virtual ~FakeIoDelegate() = default; 34 35 // non-copyable, non-movable 36 FakeIoDelegate(const FakeIoDelegate&) = delete; 37 FakeIoDelegate(FakeIoDelegate&&) = delete; 38 FakeIoDelegate& operator=(const FakeIoDelegate&) = delete; 39 FakeIoDelegate& operator=(FakeIoDelegate&&) = delete; 40 41 // Overrides from the real IoDelegate 42 std::unique_ptr<std::string> GetFileContents( 43 const std::string& filename, 44 const std::string& append_content_suffix = "") const override; 45 std::unique_ptr<LineReader> GetLineReader( 46 const std::string& file_path) const override; 47 bool FileIsReadable(const std::string& path) const override; 48 std::unique_ptr<CodeWriter> GetCodeWriter( 49 const std::string& file_path) const override; 50 void RemovePath(const std::string& file_path) const override; 51 android::base::Result<std::vector<std::string>> ListFiles(const std::string& dir) 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) const; 65 const std::map<std::string, std::string>& InputFiles() const; 66 const std::map<std::string, std::string>& OutputFiles() const; 67 68 bool PathWasRemoved(const std::string& path); 69 70 private: 71 // Remove leading "./" from |path|. 72 std::string CleanPath(const std::string& path) const; 73 74 std::map<std::string, std::string> file_contents_; 75 // Normally, writing to files leaves the IoDelegate unchanged, so 76 // GetCodeWriter is a const method. However, for tests, we break this 77 // intentionally by storing the written strings. 78 mutable std::map<std::string, std::string> written_file_contents_; 79 80 // We normally just write to strings in |written_file_contents_| but for 81 // files in this list, we simulate I/O errors. 82 std::set<std::string> broken_files_; 83 mutable std::set<std::string> removed_files_; 84 }; // class FakeIoDelegate 85 86 } // namespace test 87 } // namespace aidl 88 } // namespace android 89