• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
Write(const char *,...)39   bool Write(const char* /* format */, ...) 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 
GetLineReader(const string & file_path) const60 unique_ptr<LineReader> FakeIoDelegate::GetLineReader(
61     const string& file_path) const {
62   unique_ptr<LineReader> ret;
63   const auto& it = file_contents_.find(CleanPath(file_path));
64   if (it != file_contents_.cend()) {
65     ret = LineReader::ReadFromMemory(it->second);
66   }
67   return ret;
68 }
69 
FileIsReadable(const string & path) const70 bool FakeIoDelegate::FileIsReadable(const string& path) const {
71   return file_contents_.find(CleanPath(path)) != file_contents_.end();
72 }
73 
GetCodeWriter(const std::string & file_path) const74 std::unique_ptr<CodeWriter> FakeIoDelegate::GetCodeWriter(
75     const std::string& file_path) const {
76   if (broken_files_.count(file_path) > 0) {
77     return unique_ptr<CodeWriter>(new BrokenCodeWriter);
78   }
79   removed_files_.erase(file_path);
80   written_file_contents_[file_path] = "";
81   return CodeWriter::ForString(&written_file_contents_[file_path]);
82 }
83 
RemovePath(const std::string & file_path) const84 void FakeIoDelegate::RemovePath(const std::string& file_path) const {
85   removed_files_.insert(file_path);
86 }
87 
SetFileContents(const string & filename,const string & contents)88 void FakeIoDelegate::SetFileContents(const string& filename,
89                                      const string& contents) {
90   file_contents_[filename] = contents;
91 }
92 
ListFiles(const string & dir) const93 Result<vector<string>> FakeIoDelegate::ListFiles(const string& dir) const {
94   const string dir_name = dir.back() == OS_PATH_SEPARATOR ? dir : dir + OS_PATH_SEPARATOR;
95   vector<string> files;
96   for (auto it = file_contents_.begin(); it != file_contents_.end(); it++) {
97     if (android::base::StartsWith(it->first, dir_name)) {
98       files.emplace_back(it->first);
99     }
100   }
101   return files;
102 }
103 
AddStubParcelable(const string & canonical_name,const string & cpp_header)104 void FakeIoDelegate::AddStubParcelable(const string& canonical_name,
105                                        const string& cpp_header) {
106   string package, class_name, rel_path;
107   SplitPackageClass(canonical_name, &rel_path, &package, &class_name);
108   string contents;
109   if (cpp_header.empty()) {
110     contents = StringPrintf("package %s;\nparcelable %s;",
111                             package.c_str(), class_name.c_str());
112   } else {
113     contents = StringPrintf("package %s;\nparcelable %s cpp_header \"%s\";",
114                             package.c_str(), class_name.c_str(),
115                             cpp_header.c_str());
116   }
117   SetFileContents(rel_path, contents);
118 }
119 
AddStubInterface(const string & canonical_name)120 void FakeIoDelegate::AddStubInterface(const string& canonical_name) {
121   string package, class_name, rel_path;
122   SplitPackageClass(canonical_name, &rel_path, &package, &class_name);
123   string contents = StringPrintf("package %s;\ninterface %s { }",
124                                  package.c_str(), class_name.c_str());
125   SetFileContents(rel_path, contents);
126 }
127 
AddCompoundParcelable(const string & canonical_name,const vector<string> & subclasses)128 void FakeIoDelegate::AddCompoundParcelable(const string& canonical_name,
129                                            const vector<string>& subclasses) {
130   string package, class_name, rel_path;
131   SplitPackageClass(canonical_name, &rel_path, &package, &class_name);
132   string contents = StringPrintf("package %s;\n", package.c_str());
133   for (const string& subclass : subclasses) {
134     StringAppendF(&contents, "parcelable %s.%s;\n",
135                   class_name.c_str(), subclass.c_str());
136   }
137   SetFileContents(rel_path, contents);
138 }
139 
AddBrokenFilePath(const std::string & path)140 void FakeIoDelegate::AddBrokenFilePath(const std::string& path) {
141   broken_files_.insert(path);
142 }
143 
GetWrittenContents(const string & path,string * content) const144 bool FakeIoDelegate::GetWrittenContents(const string& path, string* content) const {
145   const auto it = written_file_contents_.find(path);
146   if (it == written_file_contents_.end()) {
147     return false;
148   }
149   if (content) {
150     *content = it->second;
151   }
152   return true;
153 }
154 
InputFiles() const155 const std::map<std::string, std::string>& FakeIoDelegate::InputFiles() const {
156   return file_contents_;
157 }
158 
OutputFiles() const159 const std::map<std::string, std::string>& FakeIoDelegate::OutputFiles() const {
160   return written_file_contents_;
161 }
162 
PathWasRemoved(const std::string & path)163 bool FakeIoDelegate::PathWasRemoved(const std::string& path) {
164   if (removed_files_.count(path) > 0) {
165     return true;
166   }
167   return false;
168 }
169 
CleanPath(const string & path) const170 string FakeIoDelegate::CleanPath(const string& path) const {
171   string clean_path = path;
172   while (clean_path.length() >= 2 &&
173          clean_path[0] == '.' &&
174          clean_path[1] == OS_PATH_SEPARATOR) {
175     clean_path = clean_path.substr(2);
176   }
177   return clean_path;
178 }
179 
180 }  // namespace test
181 }  // namespace aidl
182 }  // namespace android
183