blob: 489d5e76bb6d7228bd16aabfd9b6804a38af308d [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// 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
11using std::string;
12
13namespace chromeos_update_engine {
14
15class 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.
Darin Petkov6a5b3222010-07-13 14:55:28 -070019 bool DoTest(const OmahaResponse& in,
adlr@google.com3defe6a2009-12-04 20:57:17 +000020 const string& boot_dev,
21 InstallPlan* out);
22};
23
24class 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
42namespace {
43const 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
Darin Petkov6a5b3222010-07-13 14:55:28 -070054bool OmahaResponseHandlerActionTest::DoTest(const OmahaResponse& in,
adlr@google.com3defe6a2009-12-04 20:57:17 +000055 const string& boot_dev,
56 InstallPlan* out) {
57 ActionProcessor processor;
58 OmahaResponseHandlerActionProcessorDelegate delegate;
59 processor.set_delegate(&delegate);
60
Darin Petkov6a5b3222010-07-13 14:55:28 -070061 ObjectFeederAction<OmahaResponse> feeder_action;
adlr@google.com3defe6a2009-12-04 20:57:17 +000062 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
80TEST_F(OmahaResponseHandlerActionTest, SimpleTest) {
81 {
Darin Petkov6a5b3222010-07-13 14:55:28 -070082 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +000083 in.update_exists = true;
84 in.display_version = "a.b.c.d";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -070085 in.codebase = "http://foo/the_update_a.b.c.d.tgz";
adlr@google.com3defe6a2009-12-04 20:57:17 +000086 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 Reyesf98bff82010-05-06 13:33:25 -070092 EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan));
adlr@google.com3defe6a2009-12-04 20:57:17 +000093 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 Reyesf98bff82010-05-06 13:33:25 -070096 EXPECT_EQ("/dev/sda5", install_plan.install_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +000097 }
98 {
Darin Petkov6a5b3222010-07-13 14:55:28 -070099 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000100 in.update_exists = true;
101 in.display_version = "a.b.c.d";
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700102 in.codebase = "http://foo/the_update_a.b.c.d.tgz";
adlr@google.com3defe6a2009-12-04 20:57:17 +0000103 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 Reyesf98bff82010-05-06 13:33:25 -0700109 EXPECT_TRUE(DoTest(in, "/dev/sda5", &install_plan));
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700110 EXPECT_TRUE(install_plan.is_full_update);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000111 EXPECT_EQ(in.codebase, install_plan.download_url);
112 EXPECT_EQ(in.hash, install_plan.download_hash);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000113 EXPECT_EQ("/dev/sda3", install_plan.install_path);
114 }
115 {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700116 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000117 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 Reyesf98bff82010-05-06 13:33:25 -0700126 EXPECT_TRUE(DoTest(in, "/dev/sda3", &install_plan));
Andrew de los Reyes63b96d72010-05-10 13:08:54 -0700127 EXPECT_TRUE(install_plan.is_full_update);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000128 EXPECT_EQ(in.codebase, install_plan.download_url);
129 EXPECT_EQ(in.hash, install_plan.download_hash);
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700130 EXPECT_EQ("/dev/sda5", install_plan.install_path);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000131 }
132}
133
134TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700135 OmahaResponse in;
adlr@google.com3defe6a2009-12-04 20:57:17 +0000136 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.com3defe6a2009-12-04 20:57:17 +0000142 EXPECT_EQ("", install_plan.install_path);
143}
144
145} // namespace chromeos_update_engine