14a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley/*
24a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * Copyright (C) 2015, The Android Open Source Project
34a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley *
44a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
54a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * you may not use this file except in compliance with the License.
64a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * You may obtain a copy of the License at
74a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley *
84a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley *     http://www.apache.org/licenses/LICENSE-2.0
94a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley *
104a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * Unless required by applicable law or agreed to in writing, software
114a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
124a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * See the License for the specific language governing permissions and
144a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley * limitations under the License.
154a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley */
164a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
174a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley#ifndef AIDL_TESTS_FAKE_IO_DELEGATE_H_
184a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley#define AIDL_TESTS_FAKE_IO_DELEGATE_H_
194a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
200a62067f35e957493bc37c4b42dfdcfc16353831Elliott Hughes#include <android-base/macros.h>
214a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
224a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley#include <map>
234a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley#include <memory>
249d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley#include <set>
254a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley#include <string>
26ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley#include <vector>
274a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
284a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley#include "io_delegate.h"
294a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
304a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wileynamespace android {
314a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wileynamespace aidl {
324a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wileynamespace test {
334a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
344a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wileyclass FakeIoDelegate : public IoDelegate {
354a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley public:
364a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley  FakeIoDelegate() = default;
374a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley  virtual ~FakeIoDelegate() = default;
384a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
39a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  // Overrides from the real IoDelegate
404a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley  std::unique_ptr<std::string> GetFileContents(
414a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley      const std::string& filename,
424a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley      const std::string& append_content_suffix = "") const override;
43ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley  std::unique_ptr<LineReader> GetLineReader(
44ef1409302aa97b1807e55c63588ac0a428f41a06Christopher Wiley      const std::string& file_path) const override;
4572877acd3b57462fcd452bff4a9d7ff936936a8eChristopher Wiley  bool FileIsReadable(const std::string& path) const override;
46a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  bool CreatedNestedDirs(
47a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      const std::string& base_dir,
48a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      const std::vector<std::string>& nested_subdirs) const override;
49a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  std::unique_ptr<CodeWriter> GetCodeWriter(
50a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley      const std::string& file_path) const override;
519d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley  void RemovePath(const std::string& file_path) const override;
5272877acd3b57462fcd452bff4a9d7ff936936a8eChristopher Wiley
53a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  // Methods added to facilitate testing.
544a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley  void SetFileContents(const std::string& filename,
554a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley                       const std::string& contents);
56e9351ccad61f2f7fc4a0721480119112c97f049eChristopher Wiley  void AddStubParcelable(const std::string& canonical_name,
57e9351ccad61f2f7fc4a0721480119112c97f049eChristopher Wiley                         const std::string& cpp_header);
58ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  void AddStubInterface(const std::string& canonical_name);
59ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  void AddCompoundParcelable(const std::string& canonical_name,
60ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley                             const std::vector<std::string>& subclasses);
619d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley  void AddBrokenFilePath(const std::string& path);
62a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  // Returns true iff we've previously written to |path|.
63a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  // When we return true, we'll set *contents to the written string.
64a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  bool GetWrittenContents(const std::string& path, std::string* content);
654a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
669d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley  bool PathWasRemoved(const std::string& path);
679d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley
684a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley private:
69ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  // Remove leading "./" from |path|.
70ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley  std::string CleanPath(const std::string& path) const;
71ea6e382143e4a8df894d47db3a6cb79a38ad4530Christopher Wiley
724a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley  std::map<std::string, std::string> file_contents_;
73a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  // Normally, writing to files leaves the IoDelegate unchanged, so
74a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  // GetCodeWriter is a const method.  However, for tests, we break this
75a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  // intentionally by storing the written strings.
76a30a45e7b7cdd3c4b5b7c5dcc6ce005abfe7c90fChristopher Wiley  mutable std::map<std::string, std::string> written_file_contents_;
774a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
789d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley  // We normally just write to strings in |written_file_contents_| but for
799d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley  // files in this list, we simulate I/O errors.
809d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley  std::set<std::string> broken_files_;
819d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley  mutable std::set<std::string> removed_files_;
829d6e0b29add607669e440085f1fc60cd434dc987Christopher Wiley
834a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley  DISALLOW_COPY_AND_ASSIGN(FakeIoDelegate);
844a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley};  // class FakeIoDelegate
854a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
864a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley}  // namespace test
874a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley}  // namespace android
884a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley}  // namespace aidl
894a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley
904a2884b0e9414ed9dfee4e141e8bd462c07b442bChristopher Wiley#endif // AIDL_TESTS_FAKE_IO_DELEGATE_H_
91