blob: a76838a0db9a7f7d322fadf4cda85ea1482c25f8 [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);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080096 EXPECT_EQ(string(utils::kStatefulPartition) +
97 "/the_update_a.b.c.d_FULL_.tgz",
adlr@google.com3defe6a2009-12-04 20:57:17 +000098 install_plan.download_path);
99 EXPECT_EQ("/dev/sda2", install_plan.install_path);
100 }
101 {
102 UpdateCheckResponse in;
103 in.update_exists = true;
104 in.display_version = "a.b.c.d";
105 in.codebase = "http://foo/the_update_a.b.c.d_DELTA_.tgz";
106 in.more_info_url = "http://more/info";
107 in.hash = "HASHj+";
108 in.size = 12;
109 in.needs_admin = true;
110 in.prompt = true;
111 InstallPlan install_plan;
112 EXPECT_TRUE(DoTest(in, "/dev/sda4", &install_plan));
113 EXPECT_FALSE(install_plan.is_full_update);
114 EXPECT_EQ(in.codebase, install_plan.download_url);
115 EXPECT_EQ(in.hash, install_plan.download_hash);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800116 EXPECT_EQ(string(utils::kStatefulPartition) +
117 "/the_update_a.b.c.d_DELTA_.tgz",
adlr@google.com3defe6a2009-12-04 20:57:17 +0000118 install_plan.download_path);
119 EXPECT_EQ("/dev/sda3", install_plan.install_path);
120 }
121 {
122 UpdateCheckResponse in;
123 in.update_exists = true;
124 in.display_version = "a.b.c.d";
125 in.codebase = kLongName;
126 in.more_info_url = "http://more/info";
127 in.hash = "HASHj+";
128 in.size = 12;
129 in.needs_admin = true;
130 in.prompt = true;
131 InstallPlan install_plan;
132 EXPECT_TRUE(DoTest(in, "/dev/sda4", &install_plan));
133 EXPECT_FALSE(install_plan.is_full_update);
134 EXPECT_EQ(in.codebase, install_plan.download_url);
135 EXPECT_EQ(in.hash, install_plan.download_hash);
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -0800136 EXPECT_EQ(string(utils::kStatefulPartition) + "/" +
137 kLongName.substr(0, 255),
adlr@google.com3defe6a2009-12-04 20:57:17 +0000138 install_plan.download_path);
139 EXPECT_EQ("/dev/sda3", install_plan.install_path);
140 }
141}
142
143TEST_F(OmahaResponseHandlerActionTest, NoUpdatesTest) {
144 UpdateCheckResponse in;
145 in.update_exists = false;
146 InstallPlan install_plan;
147 EXPECT_FALSE(DoTest(in, "/dev/sda1", &install_plan));
148 EXPECT_FALSE(install_plan.is_full_update);
149 EXPECT_EQ("", install_plan.download_url);
150 EXPECT_EQ("", install_plan.download_hash);
151 EXPECT_EQ("", install_plan.download_path);
152 EXPECT_EQ("", install_plan.install_path);
153}
154
155} // namespace chromeos_update_engine