• 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::StringAppendF;
27 using android::base::StringPrintf;
28 using std::string;
29 using std::unique_ptr;
30 using std::vector;
31 
32 namespace android {
33 namespace aidl {
34 namespace test {
35 
36 // Claims to always write successfully, but can't close the file.
37 class BrokenCodeWriter : public CodeWriter {
Write(const char *,...)38   bool Write(const char* /* format */, ...) override {  return true; }
Close()39   bool Close() override { return false; }
40   ~BrokenCodeWriter() override = default;
41 };  // class BrokenCodeWriter
42 
GetFileContents(const string & relative_filename,const string & content_suffix) const43 unique_ptr<string> FakeIoDelegate::GetFileContents(
44     const string& relative_filename,
45     const string& content_suffix) const {
46   string filename = CleanPath(relative_filename);
47   unique_ptr<string> contents;
48   auto it = file_contents_.find(filename);
49   if (it == file_contents_.end()) {
50     return contents;
51   }
52   contents.reset(new string);
53   *contents = it->second;
54   contents->append(content_suffix);
55 
56   return contents;
57 }
58 
GetLineReader(const string & file_path) const59 unique_ptr<LineReader> FakeIoDelegate::GetLineReader(
60     const string& file_path) const {
61   unique_ptr<LineReader> ret;
62   const auto& it = file_contents_.find(CleanPath(file_path));
63   if (it != file_contents_.cend()) {
64     ret = LineReader::ReadFromMemory(it->second);
65   }
66   return ret;
67 }
68 
FileIsReadable(const string & path) const69 bool FakeIoDelegate::FileIsReadable(const string& path) const {
70   return file_contents_.find(CleanPath(path)) != file_contents_.end();
71 }
72 
GetCodeWriter(const std::string & file_path) const73 std::unique_ptr<CodeWriter> FakeIoDelegate::GetCodeWriter(
74     const std::string& file_path) const {
75   if (broken_files_.count(file_path) > 0) {
76     return unique_ptr<CodeWriter>(new BrokenCodeWriter);
77   }
78   removed_files_.erase(file_path);
79   written_file_contents_[file_path] = "";
80   return CodeWriter::ForString(&written_file_contents_[file_path]);
81 }
82 
RemovePath(const std::string & file_path) const83 void FakeIoDelegate::RemovePath(const std::string& file_path) const {
84   removed_files_.insert(file_path);
85 }
86 
SetFileContents(const string & filename,const string & contents)87 void FakeIoDelegate::SetFileContents(const string& filename,
88                                      const string& contents) {
89   file_contents_[filename] = contents;
90 }
91 
ListFiles(const string & dir) const92 vector<string> FakeIoDelegate::ListFiles(const string& dir) const {
93   const string dir_name = dir.back() == OS_PATH_SEPARATOR ? dir : dir + OS_PATH_SEPARATOR;
94   vector<string> files;
95   for (auto it = file_contents_.begin(); it != file_contents_.end(); it++) {
96     if (android::base::StartsWith(it->first, dir_name) && !it->second.empty()) {
97       files.emplace_back(it->first);
98     }
99   }
100   return files;
101 }
102 
AddStubParcelable(const string & canonical_name,const string & cpp_header)103 void FakeIoDelegate::AddStubParcelable(const string& canonical_name,
104                                        const string& cpp_header) {
105   string package, class_name, rel_path;
106   SplitPackageClass(canonical_name, &rel_path, &package, &class_name);
107   string contents;
108   if (cpp_header.empty()) {
109     contents = StringPrintf("package %s;\nparcelable %s;",
110                             package.c_str(), class_name.c_str());
111   } else {
112     contents = StringPrintf("package %s;\nparcelable %s cpp_header \"%s\";",
113                             package.c_str(), class_name.c_str(),
114                             cpp_header.c_str());
115   }
116   SetFileContents(rel_path, contents);
117 }
118 
AddStubInterface(const string & canonical_name)119 void FakeIoDelegate::AddStubInterface(const string& canonical_name) {
120   string package, class_name, rel_path;
121   SplitPackageClass(canonical_name, &rel_path, &package, &class_name);
122   string contents = StringPrintf("package %s;\ninterface %s { }",
123                                  package.c_str(), class_name.c_str());
124   SetFileContents(rel_path, contents);
125 }
126 
AddCompoundParcelable(const string & canonical_name,const vector<string> & subclasses)127 void FakeIoDelegate::AddCompoundParcelable(const string& canonical_name,
128                                            const vector<string>& subclasses) {
129   string package, class_name, rel_path;
130   SplitPackageClass(canonical_name, &rel_path, &package, &class_name);
131   string contents = StringPrintf("package %s;\n", package.c_str());
132   for (const string& subclass : subclasses) {
133     StringAppendF(&contents, "parcelable %s.%s;\n",
134                   class_name.c_str(), subclass.c_str());
135   }
136   SetFileContents(rel_path, contents);
137 }
138 
AddBrokenFilePath(const std::string & path)139 void FakeIoDelegate::AddBrokenFilePath(const std::string& path) {
140   broken_files_.insert(path);
141 }
142 
GetWrittenContents(const string & path,string * content)143 bool FakeIoDelegate::GetWrittenContents(const string& path, string* content) {
144   const auto it = written_file_contents_.find(path);
145   if (it == written_file_contents_.end()) {
146     return false;
147   }
148   if (content) {
149     *content = it->second;
150   }
151   return true;
152 }
153 
PathWasRemoved(const std::string & path)154 bool FakeIoDelegate::PathWasRemoved(const std::string& path) {
155   if (removed_files_.count(path) > 0) {
156     return true;
157   }
158   return false;
159 }
160 
CleanPath(const string & path) const161 string FakeIoDelegate::CleanPath(const string& path) const {
162   string clean_path = path;
163   while (clean_path.length() >= 2 &&
164          clean_path[0] == '.' &&
165          clean_path[1] == OS_PATH_SEPARATOR) {
166     clean_path = clean_path.substr(2);
167   }
168   return clean_path;
169 }
170 
171 }  // namespace test
172 }  // namespace android
173 }  // namespace aidl
174