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 bool FileIsReadable(const std::string& path) const override; 46 std::unique_ptr<CodeWriter> GetCodeWriter( 47 const std::string& file_path) const override; 48 android::base::Result<std::vector<std::string>> ListFiles(const std::string& dir) const override; 49 50 // Methods added to facilitate testing. 51 void SetFileContents(const std::string& filename, 52 const std::string& contents); 53 void AddStubParcelable(const std::string& canonical_name, 54 const std::string& cpp_header); 55 void AddStubInterface(const std::string& canonical_name); 56 void AddCompoundParcelable(const std::string& canonical_name, 57 const std::vector<std::string>& subclasses); 58 void AddBrokenFilePath(const std::string& path); 59 // Returns true iff we've previously written to |path|. 60 // When we return true, we'll set *contents to the written string. 61 bool GetWrittenContents(const std::string& path, std::string* content) const; 62 const std::map<std::string, std::string>& InputFiles() const; 63 const std::map<std::string, std::string>& OutputFiles() const; 64 65 private: 66 std::map<std::string, std::string> file_contents_; 67 // Normally, writing to files leaves the IoDelegate unchanged, so 68 // GetCodeWriter is a const method. However, for tests, we break this 69 // intentionally by storing the written strings. 70 mutable std::map<std::string, std::string> written_file_contents_; 71 72 // We normally just write to strings in |written_file_contents_| but for 73 // files in this list, we simulate I/O errors. 74 std::set<std::string> broken_files_; 75 }; // class FakeIoDelegate 76 77 } // namespace test 78 } // namespace aidl 79 } // namespace android 80