blob: 7d4dc2ddd2ec196a4e9e2fa3275f648395eecd5a [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo8427b4a2014-11-05 14:00:32 -080017#include "update_engine/omaha_request_params.h"
18
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <stdio.h>
Darin Petkovfbb40092010-07-29 17:05:50 -070020
adlr@google.com3defe6a2009-12-04 20:57:17 +000021#include <string>
Darin Petkovfbb40092010-07-29 17:05:50 -070022
Alex Deymo8427b4a2014-11-05 14:00:32 -080023#include <base/files/file_util.h>
Sen Jiang297e5832016-03-17 14:45:51 -070024#include <base/files/scoped_temp_dir.h>
Alex Deymo8427b4a2014-11-05 14:00:32 -080025#include <gtest/gtest.h>
26
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/constants.h"
Sen Jiang297e5832016-03-17 14:45:51 -070028#include "update_engine/common/fake_prefs.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080029#include "update_engine/common/platform_constants.h"
30#include "update_engine/common/test_utils.h"
31#include "update_engine/common/utils.h"
Gilad Arnold5bb4c902014-04-10 12:32:13 -070032#include "update_engine/fake_system_state.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000033
Alex Deymo10875d92014-11-10 21:52:57 -080034using chromeos_update_engine::test_utils::WriteFileString;
adlr@google.com3defe6a2009-12-04 20:57:17 +000035using std::string;
36
37namespace chromeos_update_engine {
38
Jay Srinivasanae4697c2013-03-18 17:08:08 -070039class OmahaRequestParamsTest : public ::testing::Test {
40 public:
Gilad Arnold5bb4c902014-04-10 12:32:13 -070041 OmahaRequestParamsTest() : params_(&fake_system_state_) {}
Jay Srinivasanae4697c2013-03-18 17:08:08 -070042
Darin Petkov5a7f5652010-07-22 21:40:09 -070043 protected:
Alex Deymo610277e2014-11-11 21:18:11 -080044 void SetUp() override {
Gilad Arnoldc9abbec2013-07-01 06:24:42 -070045 // Create a uniquely named test directory.
Sen Jiang297e5832016-03-17 14:45:51 -070046 ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
Jay Srinivasanae4697c2013-03-18 17:08:08 -070047 // Create a fresh copy of the params for each test, so there's no
48 // unintended reuse of state across tests.
Sen Jiang297e5832016-03-17 14:45:51 -070049 params_ = OmahaRequestParams(&fake_system_state_);
50 params_.set_root(tempdir_.path().value());
Alex Deymo85616652015-10-15 18:48:31 -070051 SetLockDown(false);
Sen Jiang297e5832016-03-17 14:45:51 -070052 fake_system_state_.set_prefs(&fake_prefs_);
Alex Deymo85616652015-10-15 18:48:31 -070053 }
54
55 void SetLockDown(bool locked_down) {
56 fake_system_state_.fake_hardware()->SetIsOfficialBuild(locked_down);
57 fake_system_state_.fake_hardware()->SetIsNormalBootMode(locked_down);
Darin Petkov5a7f5652010-07-22 21:40:09 -070058 }
59
Jay Srinivasanae4697c2013-03-18 17:08:08 -070060 OmahaRequestParams params_;
Gilad Arnold5bb4c902014-04-10 12:32:13 -070061 FakeSystemState fake_system_state_;
Sen Jiang297e5832016-03-17 14:45:51 -070062 FakePrefs fake_prefs_;
Darin Petkov49d91322010-10-25 16:34:58 -070063
Sen Jiang297e5832016-03-17 14:45:51 -070064 base::ScopedTempDir tempdir_;
adlr@google.com3defe6a2009-12-04 20:57:17 +000065};
66
adlr@google.com3defe6a2009-12-04 20:57:17 +000067namespace {
adlr@google.com3defe6a2009-12-04 20:57:17 +000068string GetMachineType() {
Gilad Arnold19a45f02012-07-19 12:36:10 -070069 string machine_type;
70 if (!utils::ReadPipe("uname -m", &machine_type))
adlr@google.com3defe6a2009-12-04 20:57:17 +000071 return "";
Gilad Arnold19a45f02012-07-19 12:36:10 -070072 // Strip anything from the first newline char.
73 size_t newline_pos = machine_type.find('\n');
74 if (newline_pos != string::npos)
75 machine_type.erase(newline_pos);
76 return machine_type;
adlr@google.com3defe6a2009-12-04 20:57:17 +000077}
Alex Vakulenkod2779df2014-06-16 13:19:00 -070078} // namespace
adlr@google.com3defe6a2009-12-04 20:57:17 +000079
Jay Srinivasanae4697c2013-03-18 17:08:08 -070080TEST_F(OmahaRequestParamsTest, MissingChannelTest) {
Sen Jiang297e5832016-03-17 14:45:51 -070081 EXPECT_TRUE(params_.Init("", "", false));
Alex Deymo85616652015-10-15 18:48:31 -070082 // By default, if no channel is set, we should track the stable-channel.
Sen Jiang297e5832016-03-17 14:45:51 -070083 EXPECT_EQ("stable-channel", params_.target_channel());
Darin Petkov5a7f5652010-07-22 21:40:09 -070084}
85
Jay Srinivasanae4697c2013-03-18 17:08:08 -070086TEST_F(OmahaRequestParamsTest, ForceVersionTest) {
Sen Jiang297e5832016-03-17 14:45:51 -070087 EXPECT_TRUE(params_.Init("ForcedVersion", "", false));
88 EXPECT_EQ(string("ForcedVersion_") + GetMachineType(), params_.os_sp());
89 EXPECT_EQ("ForcedVersion", params_.app_version());
Darin Petkov5a7f5652010-07-22 21:40:09 -070090}
91
Jay Srinivasanae4697c2013-03-18 17:08:08 -070092TEST_F(OmahaRequestParamsTest, ForcedURLTest) {
Sen Jiang297e5832016-03-17 14:45:51 -070093 EXPECT_TRUE(params_.Init("", "http://forced.google.com", false));
94 EXPECT_EQ("http://forced.google.com", params_.update_url());
Darin Petkov5a7f5652010-07-22 21:40:09 -070095}
96
Jay Srinivasanae4697c2013-03-18 17:08:08 -070097TEST_F(OmahaRequestParamsTest, MissingURLTest) {
Sen Jiang297e5832016-03-17 14:45:51 -070098 EXPECT_TRUE(params_.Init("", "", false));
99 EXPECT_EQ(constants::kOmahaDefaultProductionURL, params_.update_url());
100}
101
102TEST_F(OmahaRequestParamsTest, DeltaOKTest) {
103 EXPECT_TRUE(params_.Init("", "", false));
104 EXPECT_TRUE(params_.delta_okay());
Andrew de los Reyes970bb282009-12-09 16:34:04 -0800105}
106
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700107TEST_F(OmahaRequestParamsTest, NoDeltasTest) {
Sen Jiang297e5832016-03-17 14:45:51 -0700108 ASSERT_TRUE(WriteFileString(tempdir_.path().Append(".nodelta").value(), ""));
109 EXPECT_TRUE(params_.Init("", "", false));
110 EXPECT_FALSE(params_.delta_okay());
Darin Petkova3df55b2010-11-15 13:33:55 -0800111}
112
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700113TEST_F(OmahaRequestParamsTest, SetTargetChannelTest) {
Darin Petkov49d91322010-10-25 16:34:58 -0700114 {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700115 OmahaRequestParams params(&fake_system_state_);
Sen Jiang297e5832016-03-17 14:45:51 -0700116 params.set_root(tempdir_.path().value());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700117 EXPECT_TRUE(params.Init("", "", false));
Sen Jiang297e5832016-03-17 14:45:51 -0700118 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700119 EXPECT_FALSE(params.is_powerwash_allowed());
Darin Petkov49d91322010-10-25 16:34:58 -0700120 }
Sen Jiang297e5832016-03-17 14:45:51 -0700121 params_.set_root(tempdir_.path().value());
122 EXPECT_TRUE(params_.Init("", "", false));
123 EXPECT_EQ("canary-channel", params_.target_channel());
124 EXPECT_FALSE(params_.is_powerwash_allowed());
Darin Petkov49d91322010-10-25 16:34:58 -0700125}
126
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700127TEST_F(OmahaRequestParamsTest, SetIsPowerwashAllowedTest) {
Darin Petkov49d91322010-10-25 16:34:58 -0700128 {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700129 OmahaRequestParams params(&fake_system_state_);
Sen Jiang297e5832016-03-17 14:45:51 -0700130 params.set_root(tempdir_.path().value());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700131 EXPECT_TRUE(params.Init("", "", false));
Sen Jiang297e5832016-03-17 14:45:51 -0700132 EXPECT_TRUE(params.SetTargetChannel("canary-channel", true, nullptr));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700133 EXPECT_TRUE(params.is_powerwash_allowed());
Darin Petkov49d91322010-10-25 16:34:58 -0700134 }
Sen Jiang297e5832016-03-17 14:45:51 -0700135 params_.set_root(tempdir_.path().value());
136 EXPECT_TRUE(params_.Init("", "", false));
137 EXPECT_EQ("canary-channel", params_.target_channel());
138 EXPECT_TRUE(params_.is_powerwash_allowed());
Darin Petkov49d91322010-10-25 16:34:58 -0700139}
140
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700141TEST_F(OmahaRequestParamsTest, SetTargetChannelInvalidTest) {
Darin Petkov49d91322010-10-25 16:34:58 -0700142 {
Gilad Arnold5bb4c902014-04-10 12:32:13 -0700143 OmahaRequestParams params(&fake_system_state_);
Sen Jiang297e5832016-03-17 14:45:51 -0700144 params.set_root(tempdir_.path().value());
Alex Deymo85616652015-10-15 18:48:31 -0700145 SetLockDown(true);
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700146 EXPECT_TRUE(params.Init("", "", false));
Alex Deymod942f9d2015-11-06 16:11:50 -0800147 string error_message;
148 EXPECT_FALSE(
149 params.SetTargetChannel("dogfood-channel", true, &error_message));
150 // The error message should include a message about the valid channels.
151 EXPECT_NE(string::npos, error_message.find("stable-channel"));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700152 EXPECT_FALSE(params.is_powerwash_allowed());
Darin Petkov49d91322010-10-25 16:34:58 -0700153 }
Sen Jiang297e5832016-03-17 14:45:51 -0700154 params_.set_root(tempdir_.path().value());
155 EXPECT_TRUE(params_.Init("", "", false));
156 EXPECT_EQ("stable-channel", params_.target_channel());
157 EXPECT_FALSE(params_.is_powerwash_allowed());
Darin Petkov49d91322010-10-25 16:34:58 -0700158}
159
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700160TEST_F(OmahaRequestParamsTest, IsValidChannelTest) {
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700161 EXPECT_TRUE(params_.IsValidChannel("canary-channel"));
162 EXPECT_TRUE(params_.IsValidChannel("stable-channel"));
163 EXPECT_TRUE(params_.IsValidChannel("beta-channel"));
164 EXPECT_TRUE(params_.IsValidChannel("dev-channel"));
165 EXPECT_FALSE(params_.IsValidChannel("testimage-channel"));
166 EXPECT_FALSE(params_.IsValidChannel("dogfood-channel"));
167 EXPECT_FALSE(params_.IsValidChannel("some-channel"));
168 EXPECT_FALSE(params_.IsValidChannel(""));
Darin Petkov49d91322010-10-25 16:34:58 -0700169}
170
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700171TEST_F(OmahaRequestParamsTest, SetTargetChannelWorks) {
Sen Jiang297e5832016-03-17 14:45:51 -0700172 params_.set_target_channel("dev-channel");
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700173 EXPECT_EQ("dev-channel", params_.target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200174
Sen Jiang297e5832016-03-17 14:45:51 -0700175 // When an invalid value is set, it should be ignored.
Alex Deymod942f9d2015-11-06 16:11:50 -0800176 EXPECT_FALSE(params_.SetTargetChannel("invalid-channel", false, nullptr));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700177 EXPECT_EQ("dev-channel", params_.target_channel());
178
179 // When set to a valid value, it should take effect.
Alex Deymod942f9d2015-11-06 16:11:50 -0800180 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700181 EXPECT_EQ("beta-channel", params_.target_channel());
182
183 // When set to the same value, it should be idempotent.
Alex Deymod942f9d2015-11-06 16:11:50 -0800184 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr));
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700185 EXPECT_EQ("beta-channel", params_.target_channel());
186
187 // When set to a valid value while a change is already pending, it should
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700188 // succeed.
Alex Deymod942f9d2015-11-06 16:11:50 -0800189 EXPECT_TRUE(params_.SetTargetChannel("stable-channel", true, nullptr));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700190 EXPECT_EQ("stable-channel", params_.target_channel());
191
Sen Jiang297e5832016-03-17 14:45:51 -0700192 // Set a different channel in mutable_image_props_.
193 params_.set_target_channel("stable-channel");
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700194
195 // When set to a valid value while a change is already pending, it should
196 // succeed.
197 params_.Init("", "", false);
Alex Deymod942f9d2015-11-06 16:11:50 -0800198 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr));
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700199 // The target channel should reflect the change, but the download channel
200 // should continue to retain the old value ...
201 EXPECT_EQ("beta-channel", params_.target_channel());
202 EXPECT_EQ("stable-channel", params_.download_channel());
203
204 // ... until we update the download channel explicitly.
205 params_.UpdateDownloadChannel();
206 EXPECT_EQ("beta-channel", params_.download_channel());
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700207 EXPECT_EQ("beta-channel", params_.target_channel());
Patrick Dubroy7fbbe8a2011-08-01 17:28:22 +0200208}
209
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700210TEST_F(OmahaRequestParamsTest, ChannelIndexTest) {
211 int canary = params_.GetChannelIndex("canary-channel");
212 int dev = params_.GetChannelIndex("dev-channel");
213 int beta = params_.GetChannelIndex("beta-channel");
214 int stable = params_.GetChannelIndex("stable-channel");
215 EXPECT_LE(canary, dev);
216 EXPECT_LE(dev, beta);
217 EXPECT_LE(beta, stable);
218
219 // testimage-channel or other names are not recognized, so index will be -1.
220 int testimage = params_.GetChannelIndex("testimage-channel");
221 int bogus = params_.GetChannelIndex("bogus-channel");
222 EXPECT_EQ(-1, testimage);
223 EXPECT_EQ(-1, bogus);
224}
225
226TEST_F(OmahaRequestParamsTest, ToMoreStableChannelFlagTest) {
Sen Jiang297e5832016-03-17 14:45:51 -0700227 params_.image_props_.current_channel = "canary-channel";
228 params_.download_channel_ = "stable-channel";
229 EXPECT_TRUE(params_.to_more_stable_channel());
Jay Srinivasandb0acdf2013-04-02 14:47:45 -0700230}
231
Chris Sosac1972482013-04-30 22:31:10 -0700232TEST_F(OmahaRequestParamsTest, CollectECFWVersionsTest) {
Sen Jiang297e5832016-03-17 14:45:51 -0700233 params_.hwid_ = string("STUMPY ALEX 12345");
234 EXPECT_FALSE(params_.CollectECFWVersions());
Chris Sosac1972482013-04-30 22:31:10 -0700235
Sen Jiang297e5832016-03-17 14:45:51 -0700236 params_.hwid_ = string("SNOW 12345");
237 EXPECT_TRUE(params_.CollectECFWVersions());
Chris Sosac1972482013-04-30 22:31:10 -0700238
Sen Jiang297e5832016-03-17 14:45:51 -0700239 params_.hwid_ = string("SAMS ALEX 12345");
240 EXPECT_TRUE(params_.CollectECFWVersions());
Chris Sosac1972482013-04-30 22:31:10 -0700241}
242
adlr@google.com3defe6a2009-12-04 20:57:17 +0000243} // namespace chromeos_update_engine