Add and emit download action error codes.
BUG=560
TEST=unit tests
Review URL: http://codereview.chromium.org/3046007
diff --git a/action_processor.h b/action_processor.h
index f04345d..7b1a312 100644
--- a/action_processor.h
+++ b/action_processor.h
@@ -25,6 +25,10 @@
enum ActionExitCode {
kActionCodeSuccess = 0,
kActionCodeError = 1,
+ kActionCodeInstallDeviceOpenError = 2,
+ kActionCodeKernelDeviceOpenError = 3,
+ kActionCodeDownloadTransferError = 4,
+ kActionCodeDownloadHashMismatchError = 5,
};
class AbstractAction;
diff --git a/download_action.cc b/download_action.cc
index 9be81f0..5ecb99e 100644
--- a/download_action.cc
+++ b/download_action.cc
@@ -61,7 +61,7 @@
if (rc < 0) {
LOG(ERROR) << "Unable to open output file " << install_plan_.install_path;
// report error to processor
- processor_->ActionComplete(this, kActionCodeError);
+ processor_->ActionComplete(this, kActionCodeInstallDeviceOpenError);
return;
}
if (!install_plan_.is_full_update) {
@@ -70,7 +70,7 @@
LOG(ERROR) << "Unable to open kernel file "
<< install_plan_.kernel_install_path.c_str();
writer_->Close();
- processor_->ActionComplete(this, kActionCodeError);
+ processor_->ActionComplete(this, kActionCodeKernelDeviceOpenError);
return;
}
}
@@ -116,25 +116,25 @@
CHECK_EQ(writer_->Close(), 0) << errno;
writer_ = NULL;
}
- if (successful) {
+ ActionExitCode code =
+ successful ? kActionCodeSuccess : kActionCodeDownloadTransferError;
+ if (code == kActionCodeSuccess) {
// Make sure hash is correct
omaha_hash_calculator_.Finalize();
if (omaha_hash_calculator_.hash() != install_plan_.download_hash) {
LOG(ERROR) << "Download of " << install_plan_.download_url
<< " failed. Expect hash " << install_plan_.download_hash
<< " but got hash " << omaha_hash_calculator_.hash();
- successful = false;
+ code = kActionCodeDownloadHashMismatchError;
}
}
FlushLinuxCaches();
- // Write the path to the output pipe if we're successful
- if (successful && HasOutputPipe())
+ // Write the path to the output pipe if we're successful.
+ if (code == kActionCodeSuccess && HasOutputPipe())
SetOutputObject(GetInputObject());
- processor_->ActionComplete(
- this,
- successful ? kActionCodeSuccess : kActionCodeError);
+ processor_->ActionComplete(this, code);
}
}; // namespace {}
diff --git a/download_action_unittest.cc b/download_action_unittest.cc
index 104107f..4e11626 100644
--- a/download_action_unittest.cc
+++ b/download_action_unittest.cc
@@ -23,8 +23,10 @@
namespace {
class DownloadActionTestProcessorDelegate : public ActionProcessorDelegate {
public:
- DownloadActionTestProcessorDelegate()
- : loop_(NULL), processing_done_called_(false) {}
+ explicit DownloadActionTestProcessorDelegate(ActionExitCode expected_code)
+ : loop_(NULL),
+ processing_done_called_(false),
+ expected_code_(expected_code) {}
virtual ~DownloadActionTestProcessorDelegate() {
EXPECT_TRUE(processing_done_called_);
}
@@ -44,14 +46,19 @@
virtual void ActionCompleted(ActionProcessor* processor,
AbstractAction* action,
ActionExitCode code) {
- // make sure actions always succeed
- EXPECT_EQ(kActionCodeSuccess, code);
+ const string type = action->Type();
+ if (type == DownloadAction::StaticType()) {
+ EXPECT_EQ(expected_code_, code);
+ } else {
+ EXPECT_EQ(kActionCodeSuccess, code);
+ }
}
GMainLoop *loop_;
string path_;
vector<char> expected_data_;
bool processing_done_called_;
+ ActionExitCode expected_code_;
};
struct EntryPointArgs {
@@ -66,7 +73,7 @@
return FALSE;
}
-void TestWithData(const vector<char>& data) {
+void TestWithData(const vector<char>& data, bool hash_test) {
GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
// TODO(adlr): see if we need a different file for build bots
@@ -74,10 +81,13 @@
DirectFileWriter writer;
// takes ownership of passed in HttpFetcher
+ string hash = hash_test ?
+ OmahaHashCalculator::OmahaHashOfString("random string") :
+ OmahaHashCalculator::OmahaHashOfData(data);
InstallPlan install_plan(true,
"",
0,
- OmahaHashCalculator::OmahaHashOfData(data),
+ hash,
output_temp_file.GetPath(),
"");
ObjectFeederAction<InstallPlan> feeder_action;
@@ -87,7 +97,8 @@
download_action.SetTestFileWriter(&writer);
BondActions(&feeder_action, &download_action);
- DownloadActionTestProcessorDelegate delegate;
+ DownloadActionTestProcessorDelegate delegate(
+ hash_test ? kActionCodeDownloadHashMismatchError : kActionCodeSuccess);
delegate.loop_ = loop;
delegate.expected_data_ = data;
delegate.path_ = output_temp_file.GetPath();
@@ -106,7 +117,7 @@
vector<char> small;
const char* foo = "foo";
small.insert(small.end(), foo, foo + strlen(foo));
- TestWithData(small);
+ TestWithData(small, false);
}
TEST(DownloadActionTest, LargeTest) {
@@ -119,7 +130,14 @@
else
c++;
}
- TestWithData(big);
+ TestWithData(big, false);
+}
+
+TEST(DownloadActionTest, BadHashTest) {
+ vector<char> small;
+ const char* foo = "foo";
+ small.insert(small.end(), foo, foo + strlen(foo));
+ TestWithData(small, true);
}
namespace {