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 #include "fake_io_delegate.h"
18
19 #include <android-base/stringprintf.h>
20 #include <android-base/strings.h>
21
22 #include "logging.h"
23 #include "os.h"
24 #include "tests/test_util.h"
25
26 using android::base::Result;
27 using android::base::StringAppendF;
28 using android::base::StringPrintf;
29 using std::string;
30 using std::unique_ptr;
31 using std::vector;
32
33 namespace android {
34 namespace aidl {
35 namespace test {
36
37 // Claims to always write successfully, but can't close the file.
38 class BrokenCodeWriter : public CodeWriter {
WriteString(const std::string &)39 bool WriteString(const std::string& /* str */) override { return true; }
Close()40 bool Close() override { return false; }
41 ~BrokenCodeWriter() override = default;
42 }; // class BrokenCodeWriter
43
GetFileContents(const string & relative_filename,const string & content_suffix) const44 unique_ptr<string> FakeIoDelegate::GetFileContents(
45 const string& relative_filename,
46 const string& content_suffix) const {
47 string filename = CleanPath(relative_filename);
48 unique_ptr<string> contents;
49 auto it = file_contents_.find(filename);
50 if (it == file_contents_.end()) {
51 return contents;
52 }
53 contents.reset(new string);
54 *contents = it->second;
55 contents->append(content_suffix);
56
57 return contents;
58 }
59
FileIsReadable(const string & path) const60 bool FakeIoDelegate::FileIsReadable(const string& path) const {
61 return file_contents_.find(CleanPath(path)) != file_contents_.end();
62 }
63
GetCodeWriter(const std::string & file_path) const64 std::unique_ptr<CodeWriter> FakeIoDelegate::GetCodeWriter(
65 const std::string& file_path) const {
66 if (broken_files_.count(file_path) > 0) {
67 return unique_ptr<CodeWriter>(new BrokenCodeWriter);
68 }
69 written_file_contents_[file_path] = "";
70 return CodeWriter::ForString(&written_file_contents_[file_path]);
71 }
72
SetFileContents(const string & filename,const string & contents)73 void FakeIoDelegate::SetFileContents(const string& filename,
74 const string& contents) {
75 file_contents_[filename] = contents;
76 }
77
ListFiles(const string & dir) const78 Result<vector<string>> FakeIoDelegate::ListFiles(const string& dir) const {
79 const string dir_name = dir.back() == OS_PATH_SEPARATOR ? dir : dir + OS_PATH_SEPARATOR;
80 vector<string> files;
81 for (auto it = file_contents_.begin(); it != file_contents_.end(); it++) {
82 if (android::base::StartsWith(it->first, dir_name)) {
83 files.emplace_back(it->first);
84 }
85 }
86 return files;
87 }
88
AddBrokenFilePath(const std::string & path)89 void FakeIoDelegate::AddBrokenFilePath(const std::string& path) {
90 broken_files_.insert(path);
91 }
92
GetWrittenContents(const string & path,string * content) const93 bool FakeIoDelegate::GetWrittenContents(const string& path, string* content) const {
94 const auto it = written_file_contents_.find(path);
95 if (it == written_file_contents_.end()) {
96 return false;
97 }
98 if (content) {
99 *content = it->second;
100 }
101 return true;
102 }
103
InputFiles() const104 const std::map<std::string, std::string>& FakeIoDelegate::InputFiles() const {
105 return file_contents_;
106 }
107
OutputFiles() const108 const std::map<std::string, std::string>& FakeIoDelegate::OutputFiles() const {
109 return written_file_contents_;
110 }
111
112 } // namespace test
113 } // namespace aidl
114 } // namespace android
115