blob: 0248daf9d3c840575e5e907c18473648da34caad [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.
19 bool DoTest(const UpdateCheckResponse& in,
20 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
54bool 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
80TEST_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;
92 EXPECT_TRUE(DoTest(in, "/dev/sda1", &install_plan));
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);
96 EXPECT_EQ(utils::kStatefulPartition + "/the_update_a.b.c.d_FULL_.tgz",
97 install_plan.download_path);
98 EXPECT_EQ("/dev/sda2", install_plan.install_path);
99 }
100 {
101 UpdateCheckResponse in;
102 in.update_exists = true;
103 in.display_version = "a.b.c.d";
104 in.codebase = "http://foo/the_update_a.b.c.d_DELTA_.tgz";
105 in.more_info_url = "http://more/info";
106 in.hash = "HASHj+";
107 in.size = 12;
108 in.needs_admin = true;
109 in.prompt = true;
110 InstallPlan install_plan;
111 EXPECT_TRUE(DoTest(in, "/dev/sda4", &install_plan));
112 EXPECT_FALSE(install_plan.is_full_update);
113 EXPECT_EQ(in.codebase, install_plan.download_url);
114 EXPECT_EQ(in.hash, install_plan.download_hash);
115 EXPECT_EQ(utils::kStatefulPartition + "/the_update_a.b.c.d_DELTA_.tgz",
116 install_plan.download_path);
117 EXPECT_EQ("/dev/sda3", install_plan.install_path);
118 }
119 {
120 UpdateCheckResponse in;
121 in.update_exists = true;
122 in.display_version = "a.b.c.d";
123 in.codebase = kLongName;
124 in.more_info_url = "http://more/info";
125 in.hash = "HASHj+";
126 in.size = 12;
127 in.needs_admin = true;
128 in.prompt = true;
129 InstallPlan install_plan;
130 EXPECT_TRUE(DoTest(in, "/dev/sda4", &install_plan));
131 EXPECT_FALSE(install_plan.is_full_update);
132 EXPECT_EQ(in.codebase, install_plan.download_url);
133 EXPECT_EQ(in.hash, install_plan.download_hash);
134 EXPECT_EQ(utils::kStatefulPartition + "/" + kLongName.substr(0, 255),
135 install_plan.download_path);
136 EXPECT_EQ("/dev/sda3", install_plan.install_path);
137 }
138}
139
140TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
141 UpdateCheckResponse in;
142 in.update_exists = false;
143 InstallPlan install_plan;
144 EXPECT_FALSE(DoTest(in, "/dev/sda1", &install_plan));
145 EXPECT_FALSE(install_plan.is_full_update);
146 EXPECT_EQ("", install_plan.download_url);
147 EXPECT_EQ("", install_plan.download_hash);
148 EXPECT_EQ("", install_plan.download_path);
149 EXPECT_EQ("", install_plan.install_path);
150}
151
152} // namespace chromeos_update_engine