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, const std::string& contents); 52 void AddBrokenFilePath(const std::string& path); 53 // Returns true iff we've previously written to |path|. 54 // When we return true, we'll set *contents to the written string. 55 bool GetWrittenContents(const std::string& path, std::string* content) const; 56 const std::map<std::string, std::string>& InputFiles() const; 57 const std::map<std::string, std::string>& OutputFiles() const; 58 59 private: 60 std::map<std::string, std::string> file_contents_; 61 // Normally, writing to files leaves the IoDelegate unchanged, so 62 // GetCodeWriter is a const method. However, for tests, we break this 63 // intentionally by storing the written strings. 64 mutable std::map<std::string, std::string> written_file_contents_; 65 66 // We normally just write to strings in |written_file_contents_| but for 67 // files in this list, we simulate I/O errors. 68 std::set<std::string> broken_files_; 69 }; // class FakeIoDelegate 70 71 } // namespace test 72 } // namespace aidl 73 } // namespace android 74