adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <string> |
| 6 | #include <gtest/gtest.h> |
| 7 | #include "update_engine/omaha_response_handler_action.h" |
| 8 | #include "update_engine/test_utils.h" |
| 9 | #include "update_engine/utils.h" |
| 10 | |
| 11 | using std::string; |
| 12 | |
| 13 | namespace chromeos_update_engine { |
| 14 | |
| 15 | class OmahaResponseHandlerActionTest : public ::testing::Test { |
| 16 | public: |
| 17 | // Return true iff the OmahaResponseHandlerAction succeeded. |
| 18 | // If out is non-NULL, it's set w/ the response from the action. |
| 19 | bool DoTest(const UpdateCheckResponse& in, |
| 20 | const string& boot_dev, |
| 21 | InstallPlan* out); |
| 22 | }; |
| 23 | |
| 24 | class OmahaResponseHandlerActionProcessorDelegate |
| 25 | : public ActionProcessorDelegate { |
| 26 | public: |
| 27 | OmahaResponseHandlerActionProcessorDelegate() |
| 28 | : success_(false), |
| 29 | success_set_(false) {} |
| 30 | void ActionCompleted(ActionProcessor* processor, |
| 31 | AbstractAction* action, |
| 32 | bool success) { |
| 33 | if (action->Type() == OmahaResponseHandlerAction::StaticType()) { |
| 34 | success_ = success; |
| 35 | success_set_ = true; |
| 36 | } |
| 37 | } |
| 38 | bool success_; |
| 39 | bool success_set_; |
| 40 | }; |
| 41 | |
| 42 | namespace { |
| 43 | const string kLongName = |
| 44 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 45 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 46 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 47 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 48 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 49 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 50 | "very_long_name_and_no_slashes-very_long_name_and_no_slashes" |
| 51 | "-the_update_a.b.c.d_DELTA_.tgz"; |
| 52 | } // namespace {} |
| 53 | |
| 54 | bool OmahaResponseHandlerActionTest::DoTest(const UpdateCheckResponse& in, |
| 55 | const string& boot_dev, |
| 56 | InstallPlan* out) { |
| 57 | ActionProcessor processor; |
| 58 | OmahaResponseHandlerActionProcessorDelegate delegate; |
| 59 | processor.set_delegate(&delegate); |
| 60 | |
| 61 | ObjectFeederAction<UpdateCheckResponse> feeder_action; |
| 62 | feeder_action.set_obj(in); |
| 63 | OmahaResponseHandlerAction response_handler_action; |
| 64 | response_handler_action.set_boot_device(boot_dev); |
| 65 | BondActions(&feeder_action, &response_handler_action); |
| 66 | ObjectCollectorAction<InstallPlan> collector_action; |
| 67 | BondActions(&response_handler_action, &collector_action); |
| 68 | processor.EnqueueAction(&feeder_action); |
| 69 | processor.EnqueueAction(&response_handler_action); |
| 70 | processor.EnqueueAction(&collector_action); |
| 71 | processor.StartProcessing(); |
| 72 | EXPECT_TRUE(!processor.IsRunning()) |
| 73 | << "Update test to handle non-asynch actions"; |
| 74 | if (out) |
| 75 | *out = collector_action.object(); |
| 76 | EXPECT_TRUE(delegate.success_set_); |
| 77 | return delegate.success_; |
| 78 | } |
| 79 | |
| 80 | TEST_F(OmahaResponseHandlerActionTest, SimpleTest) { |
| 81 | { |
| 82 | UpdateCheckResponse in; |
| 83 | in.update_exists = true; |
| 84 | in.display_version = "a.b.c.d"; |
| 85 | in.codebase = "http://foo/the_update_a.b.c.d_FULL_.tgz"; |
| 86 | in.more_info_url = "http://more/info"; |
| 87 | in.hash = "HASH+"; |
| 88 | in.size = 12; |
| 89 | in.needs_admin = true; |
| 90 | in.prompt = false; |
| 91 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame^] | 92 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 93 | EXPECT_TRUE(install_plan.is_full_update); |
| 94 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 95 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame^] | 96 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 97 | } |
| 98 | { |
| 99 | UpdateCheckResponse in; |
| 100 | in.update_exists = true; |
| 101 | in.display_version = "a.b.c.d"; |
| 102 | in.codebase = "http://foo/the_update_a.b.c.d_DELTA_.tgz"; |
| 103 | in.more_info_url = "http://more/info"; |
| 104 | in.hash = "HASHj+"; |
| 105 | in.size = 12; |
| 106 | in.needs_admin = true; |
| 107 | in.prompt = true; |
| 108 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame^] | 109 | EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 110 | EXPECT_FALSE(install_plan.is_full_update); |
| 111 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 112 | EXPECT_EQ(in.hash, install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 113 | EXPECT_EQ("/dev/sda3", install_plan.install_path); |
| 114 | } |
| 115 | { |
| 116 | UpdateCheckResponse in; |
| 117 | in.update_exists = true; |
| 118 | in.display_version = "a.b.c.d"; |
| 119 | in.codebase = kLongName; |
| 120 | in.more_info_url = "http://more/info"; |
| 121 | in.hash = "HASHj+"; |
| 122 | in.size = 12; |
| 123 | in.needs_admin = true; |
| 124 | in.prompt = true; |
| 125 | InstallPlan install_plan; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame^] | 126 | EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan)); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 127 | EXPECT_FALSE(install_plan.is_full_update); |
| 128 | EXPECT_EQ(in.codebase, install_plan.download_url); |
| 129 | EXPECT_EQ(in.hash, install_plan.download_hash); |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame^] | 130 | EXPECT_EQ("/dev/sda5", install_plan.install_path); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) { |
| 135 | UpdateCheckResponse in; |
| 136 | in.update_exists = false; |
| 137 | InstallPlan install_plan; |
| 138 | EXPECT_FALSE(DoTest(in, "/dev/sda1", &install_plan)); |
| 139 | EXPECT_FALSE(install_plan.is_full_update); |
| 140 | EXPECT_EQ("", install_plan.download_url); |
| 141 | EXPECT_EQ("", install_plan.download_hash); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 142 | EXPECT_EQ("", install_plan.install_path); |
| 143 | } |
| 144 | |
| 145 | } // namespace chromeos_update_engine |