summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorChristopher Wiley <wiley@google.com>2015-11-13 12:18:16 -0800
committerChristopher Wiley <wiley@google.com>2015-11-13 12:20:12 -0800
commit9d6e0b29add607669e440085f1fc60cd434dc987 (patch)
tree45657eb6db32a5bf492ba8f51ef80a45d8f7e9db /tests
parent864bc0936d95410b06a3709e1559c884258bc52c (diff)
downloadandroid_system_tools_aidl-9d6e0b29add607669e440085f1fc60cd434dc987.tar.gz
android_system_tools_aidl-9d6e0b29add607669e440085f1fc60cd434dc987.tar.bz2
android_system_tools_aidl-9d6e0b29add607669e440085f1fc60cd434dc987.zip
Remove generated code on IO errors
Bug: 25026025 Test: unit tests Change-Id: Ice5d823102c742d546386f1ad71ef63a48c90820
Diffstat (limited to 'tests')
-rw-r--r--tests/fake_io_delegate.cpp29
-rw-r--r--tests/fake_io_delegate.h10
2 files changed, 38 insertions, 1 deletions
diff --git a/tests/fake_io_delegate.cpp b/tests/fake_io_delegate.cpp
index 39bb90f..df8f578 100644
--- a/tests/fake_io_delegate.cpp
+++ b/tests/fake_io_delegate.cpp
@@ -34,6 +34,13 @@ namespace android {
namespace aidl {
namespace test {
+// Claims to always write successfully, but can't close the file.
+class BrokenCodeWriter : public CodeWriter {
+ bool Write(const char* /* format */, ...) override { return true; }
+ bool Close() override { return false; }
+ virtual ~BrokenCodeWriter() = default;
+}; // class BrokenCodeWriter
+
unique_ptr<string> FakeIoDelegate::GetFileContents(
const string& relative_filename,
const string& content_suffix) const {
@@ -73,10 +80,17 @@ bool FakeIoDelegate::CreatedNestedDirs(
std::unique_ptr<CodeWriter> FakeIoDelegate::GetCodeWriter(
const std::string& file_path) const {
+ if (broken_files_.count(file_path) > 0) {
+ return unique_ptr<CodeWriter>(new BrokenCodeWriter);
+ }
+ removed_files_.erase(file_path);
written_file_contents_[file_path] = "";
return GetStringWriter(&written_file_contents_[file_path]);
}
+void FakeIoDelegate::RemovePath(const std::string& file_path) const {
+ removed_files_.insert(file_path);
+}
void FakeIoDelegate::SetFileContents(const string& filename,
const string& contents) {
@@ -104,15 +118,28 @@ void FakeIoDelegate::AddCompoundParcelable(const string& canonical_name,
SetFileContents(rel_path.value(), contents);
}
+void FakeIoDelegate::AddBrokenFilePath(const std::string& path) {
+ broken_files_.insert(path);
+}
+
bool FakeIoDelegate::GetWrittenContents(const string& path, string* content) {
const auto it = written_file_contents_.find(path);
if (it == written_file_contents_.end()) {
return false;
}
- *content = it->second;
+ if (content) {
+ *content = it->second;
+ }
return true;
}
+bool FakeIoDelegate::PathWasRemoved(const std::string& path) {
+ if (removed_files_.count(path) > 0) {
+ return true;
+ }
+ return false;
+}
+
void FakeIoDelegate::AddStub(const string& canonical_name,
const char* format_str) {
string package, class_name;
diff --git a/tests/fake_io_delegate.h b/tests/fake_io_delegate.h
index 5ec6115..f90009a 100644
--- a/tests/fake_io_delegate.h
+++ b/tests/fake_io_delegate.h
@@ -21,6 +21,7 @@
#include <map>
#include <memory>
+#include <set>
#include <string>
#include <vector>
@@ -47,6 +48,7 @@ class FakeIoDelegate : public IoDelegate {
const std::vector<std::string>& nested_subdirs) const override;
std::unique_ptr<CodeWriter> GetCodeWriter(
const std::string& file_path) const override;
+ void RemovePath(const std::string& file_path) const override;
// Methods added to facilitate testing.
void SetFileContents(const std::string& filename,
@@ -55,10 +57,13 @@ class FakeIoDelegate : public IoDelegate {
void AddStubInterface(const std::string& canonical_name);
void AddCompoundParcelable(const std::string& canonical_name,
const std::vector<std::string>& subclasses);
+ void AddBrokenFilePath(const std::string& path);
// Returns true iff we've previously written to |path|.
// When we return true, we'll set *contents to the written string.
bool GetWrittenContents(const std::string& path, std::string* content);
+ bool PathWasRemoved(const std::string& path);
+
private:
void AddStub(const std::string& canonical_name, const char* format_str);
// Remove leading "./" from |path|.
@@ -70,6 +75,11 @@ class FakeIoDelegate : public IoDelegate {
// intentionally by storing the written strings.
mutable std::map<std::string, std::string> written_file_contents_;
+ // We normally just write to strings in |written_file_contents_| but for
+ // files in this list, we simulate I/O errors.
+ std::set<std::string> broken_files_;
+ mutable std::set<std::string> removed_files_;
+
DISALLOW_COPY_AND_ASSIGN(FakeIoDelegate);
}; // class FakeIoDelegate