1 // 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 // 16 17 #include "update_engine/omaha_request_params.h" 18 19 #include <stdio.h> 20 21 #include <string> 22 23 #include <base/files/file_util.h> 24 #include <gtest/gtest.h> 25 26 #include "update_engine/common/constants.h" 27 #include "update_engine/common/platform_constants.h" 28 #include "update_engine/common/test_utils.h" 29 #include "update_engine/common/utils.h" 30 #include "update_engine/fake_system_state.h" 31 #include "update_engine/payload_consumer/install_plan.h" 32 33 using chromeos_update_engine::test_utils::WriteFileString; 34 using std::string; 35 36 namespace chromeos_update_engine { 37 38 class OmahaRequestParamsTest : public ::testing::Test { 39 public: 40 OmahaRequestParamsTest() : params_(&fake_system_state_) {} 41 42 protected: 43 // Return true iff the OmahaRequestParams::Init succeeded. If 44 // out is non-null, it's set w/ the generated data. 45 bool DoTest(OmahaRequestParams* out, const string& app_version, 46 const string& omaha_url); 47 48 void SetUp() override { 49 // Create a uniquely named test directory. 50 ASSERT_TRUE(utils::MakeTempDirectory(kTestDirTemplate, &test_dir_)); 51 EXPECT_TRUE(base::CreateDirectory(base::FilePath(test_dir_ + "/etc"))); 52 EXPECT_TRUE(base::CreateDirectory( 53 base::FilePath(test_dir_ + kStatefulPartition + "/etc"))); 54 // Create a fresh copy of the params for each test, so there's no 55 // unintended reuse of state across tests. 56 OmahaRequestParams new_params(&fake_system_state_); 57 params_ = new_params; 58 params_.set_root(test_dir_); 59 SetLockDown(false); 60 } 61 62 void TearDown() override { 63 EXPECT_TRUE(base::DeleteFile(base::FilePath(test_dir_), true)); 64 } 65 66 void SetLockDown(bool locked_down) { 67 fake_system_state_.fake_hardware()->SetIsOfficialBuild(locked_down); 68 fake_system_state_.fake_hardware()->SetIsNormalBootMode(locked_down); 69 } 70 71 OmahaRequestParams params_; 72 FakeSystemState fake_system_state_; 73 74 static const char* kTestDirTemplate; 75 string test_dir_; 76 }; 77 78 const char* OmahaRequestParamsTest::kTestDirTemplate = 79 "omaha_request_params-test-XXXXXX"; 80 81 bool OmahaRequestParamsTest::DoTest(OmahaRequestParams* out, 82 const string& app_version, 83 const string& omaha_url) { 84 bool success = params_.Init(app_version, omaha_url, false); 85 if (out) 86 *out = params_; 87 return success; 88 } 89 90 namespace { 91 string GetMachineType() { 92 string machine_type; 93 if (!utils::ReadPipe("uname -m", &machine_type)) 94 return ""; 95 // Strip anything from the first newline char. 96 size_t newline_pos = machine_type.find('\n'); 97 if (newline_pos != string::npos) 98 machine_type.erase(newline_pos); 99 return machine_type; 100 } 101 } // namespace 102 103 TEST_F(OmahaRequestParamsTest, SimpleTest) { 104 ASSERT_TRUE(WriteFileString( 105 test_dir_ + "/etc/lsb-release", 106 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 107 "CHROMEOS_RELEASE_FOO=bar\n" 108 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 109 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 110 "CHROMEOS_AUSERVER=http://www.google.com")); 111 OmahaRequestParams out(&fake_system_state_); 112 EXPECT_TRUE(DoTest(&out, "", "")); 113 EXPECT_EQ("Chrome OS", out.os_platform()); 114 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 115 EXPECT_EQ("arm-generic", out.os_board()); 116 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 117 EXPECT_EQ("0.2.2.3", out.app_version()); 118 EXPECT_EQ("en-US", out.app_lang()); 119 EXPECT_EQ(fake_system_state_.hardware()->GetHardwareClass(), out.hwid()); 120 EXPECT_TRUE(out.delta_okay()); 121 EXPECT_EQ("dev-channel", out.target_channel()); 122 EXPECT_EQ("http://www.google.com", out.update_url()); 123 } 124 125 TEST_F(OmahaRequestParamsTest, AppIDTest) { 126 ASSERT_TRUE(WriteFileString( 127 test_dir_ + "/etc/lsb-release", 128 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 129 "CHROMEOS_RELEASE_FOO=bar\n" 130 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 131 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 132 "CHROMEOS_RELEASE_APPID={58c35cef-9d30-476e-9098-ce20377d535d}\n" 133 "CHROMEOS_AUSERVER=http://www.google.com")); 134 OmahaRequestParams out(&fake_system_state_); 135 EXPECT_TRUE(DoTest(&out, "", "")); 136 EXPECT_EQ("Chrome OS", out.os_platform()); 137 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 138 EXPECT_EQ("arm-generic", out.os_board()); 139 EXPECT_EQ("{58c35cef-9d30-476e-9098-ce20377d535d}", out.GetAppId()); 140 EXPECT_EQ("0.2.2.3", out.app_version()); 141 EXPECT_EQ("en-US", out.app_lang()); 142 EXPECT_EQ(fake_system_state_.hardware()->GetHardwareClass(), out.hwid()); 143 EXPECT_TRUE(out.delta_okay()); 144 EXPECT_EQ("dev-channel", out.target_channel()); 145 EXPECT_EQ("http://www.google.com", out.update_url()); 146 } 147 148 TEST_F(OmahaRequestParamsTest, MissingChannelTest) { 149 ASSERT_TRUE(WriteFileString( 150 test_dir_ + "/etc/lsb-release", 151 "CHROMEOS_RELEASE_FOO=bar\n" 152 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 153 "CHROMEOS_RELEASE_TRXCK=dev-channel")); 154 OmahaRequestParams out(&fake_system_state_); 155 EXPECT_TRUE(DoTest(&out, "", "")); 156 EXPECT_EQ("Chrome OS", out.os_platform()); 157 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 158 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 159 EXPECT_EQ("0.2.2.3", out.app_version()); 160 EXPECT_EQ("en-US", out.app_lang()); 161 // By default, if no channel is set, we should track the stable-channel. 162 EXPECT_EQ("stable-channel", out.target_channel()); 163 } 164 165 TEST_F(OmahaRequestParamsTest, ConfusingReleaseTest) { 166 ASSERT_TRUE(WriteFileString( 167 test_dir_ + "/etc/lsb-release", 168 "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" 169 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 170 "CHROMEOS_RELEASE_TRXCK=dev-channel")); 171 OmahaRequestParams out(&fake_system_state_); 172 EXPECT_TRUE(DoTest(&out, "", "")); 173 EXPECT_EQ("Chrome OS", out.os_platform()); 174 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 175 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 176 EXPECT_EQ("0.2.2.3", out.app_version()); 177 EXPECT_EQ("en-US", out.app_lang()); 178 EXPECT_EQ("stable-channel", out.target_channel()); 179 } 180 181 TEST_F(OmahaRequestParamsTest, MissingVersionTest) { 182 ASSERT_TRUE(WriteFileString( 183 test_dir_ + "/etc/lsb-release", 184 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 185 "CHROMEOS_RELEASE_FOO=bar\n" 186 "CHROMEOS_RELEASE_TRACK=dev-channel")); 187 OmahaRequestParams out(&fake_system_state_); 188 EXPECT_TRUE(DoTest(&out, "", "")); 189 EXPECT_EQ("Chrome OS", out.os_platform()); 190 EXPECT_EQ(string("_") + GetMachineType(), out.os_sp()); 191 EXPECT_EQ("arm-generic", out.os_board()); 192 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 193 EXPECT_EQ("", out.app_version()); 194 EXPECT_EQ("en-US", out.app_lang()); 195 EXPECT_TRUE(out.delta_okay()); 196 EXPECT_EQ("dev-channel", out.target_channel()); 197 } 198 199 TEST_F(OmahaRequestParamsTest, ForceVersionTest) { 200 ASSERT_TRUE(WriteFileString( 201 test_dir_ + "/etc/lsb-release", 202 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 203 "CHROMEOS_RELEASE_FOO=bar\n" 204 "CHROMEOS_RELEASE_TRACK=dev-channel")); 205 OmahaRequestParams out(&fake_system_state_); 206 EXPECT_TRUE(DoTest(&out, "ForcedVersion", "")); 207 EXPECT_EQ("Chrome OS", out.os_platform()); 208 EXPECT_EQ(string("ForcedVersion_") + GetMachineType(), out.os_sp()); 209 EXPECT_EQ("arm-generic", out.os_board()); 210 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 211 EXPECT_EQ("ForcedVersion", out.app_version()); 212 EXPECT_EQ("en-US", out.app_lang()); 213 EXPECT_TRUE(out.delta_okay()); 214 EXPECT_EQ("dev-channel", out.target_channel()); 215 } 216 217 TEST_F(OmahaRequestParamsTest, ForcedURLTest) { 218 ASSERT_TRUE(WriteFileString( 219 test_dir_ + "/etc/lsb-release", 220 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 221 "CHROMEOS_RELEASE_FOO=bar\n" 222 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 223 "CHROMEOS_RELEASE_TRACK=dev-channel")); 224 OmahaRequestParams out(&fake_system_state_); 225 EXPECT_TRUE(DoTest(&out, "", "http://forced.google.com")); 226 EXPECT_EQ("Chrome OS", out.os_platform()); 227 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 228 EXPECT_EQ("arm-generic", out.os_board()); 229 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 230 EXPECT_EQ("0.2.2.3", out.app_version()); 231 EXPECT_EQ("en-US", out.app_lang()); 232 EXPECT_TRUE(out.delta_okay()); 233 EXPECT_EQ("dev-channel", out.target_channel()); 234 EXPECT_EQ("http://forced.google.com", out.update_url()); 235 } 236 237 TEST_F(OmahaRequestParamsTest, MissingURLTest) { 238 ASSERT_TRUE(WriteFileString( 239 test_dir_ + "/etc/lsb-release", 240 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 241 "CHROMEOS_RELEASE_FOO=bar\n" 242 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 243 "CHROMEOS_RELEASE_TRACK=dev-channel")); 244 OmahaRequestParams out(&fake_system_state_); 245 EXPECT_TRUE(DoTest(&out, "", "")); 246 EXPECT_EQ("Chrome OS", out.os_platform()); 247 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 248 EXPECT_EQ("arm-generic", out.os_board()); 249 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 250 EXPECT_EQ("0.2.2.3", out.app_version()); 251 EXPECT_EQ("en-US", out.app_lang()); 252 EXPECT_TRUE(out.delta_okay()); 253 EXPECT_EQ("dev-channel", out.target_channel()); 254 EXPECT_EQ(constants::kOmahaDefaultProductionURL, out.update_url()); 255 } 256 257 TEST_F(OmahaRequestParamsTest, NoDeltasTest) { 258 ASSERT_TRUE(WriteFileString( 259 test_dir_ + "/etc/lsb-release", 260 "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" 261 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 262 "CHROMEOS_RELEASE_TRXCK=dev-channel")); 263 ASSERT_TRUE(WriteFileString(test_dir_ + "/.nodelta", "")); 264 OmahaRequestParams out(&fake_system_state_); 265 EXPECT_TRUE(DoTest(&out, "", "")); 266 EXPECT_FALSE(out.delta_okay()); 267 } 268 269 TEST_F(OmahaRequestParamsTest, OverrideTest) { 270 ASSERT_TRUE(WriteFileString( 271 test_dir_ + "/etc/lsb-release", 272 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 273 "CHROMEOS_RELEASE_FOO=bar\n" 274 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 275 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 276 "CHROMEOS_AUSERVER=http://www.google.com")); 277 ASSERT_TRUE(WriteFileString( 278 test_dir_ + kStatefulPartition + "/etc/lsb-release", 279 "CHROMEOS_RELEASE_BOARD=x86-generic\n" 280 "CHROMEOS_RELEASE_TRACK=beta-channel\n" 281 "CHROMEOS_AUSERVER=https://www.google.com")); 282 OmahaRequestParams out(&fake_system_state_); 283 EXPECT_TRUE(DoTest(&out, "", "")); 284 EXPECT_EQ("Chrome OS", out.os_platform()); 285 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 286 EXPECT_EQ("x86-generic", out.os_board()); 287 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 288 EXPECT_EQ("0.2.2.3", out.app_version()); 289 EXPECT_EQ("en-US", out.app_lang()); 290 EXPECT_EQ(fake_system_state_.hardware()->GetHardwareClass(), out.hwid()); 291 EXPECT_FALSE(out.delta_okay()); 292 EXPECT_EQ("beta-channel", out.target_channel()); 293 EXPECT_EQ("https://www.google.com", out.update_url()); 294 } 295 296 TEST_F(OmahaRequestParamsTest, OverrideLockDownTest) { 297 ASSERT_TRUE(WriteFileString( 298 test_dir_ + "/etc/lsb-release", 299 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 300 "CHROMEOS_RELEASE_FOO=bar\n" 301 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 302 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 303 "CHROMEOS_AUSERVER=https://www.google.com")); 304 ASSERT_TRUE(WriteFileString( 305 test_dir_ + kStatefulPartition + "/etc/lsb-release", 306 "CHROMEOS_RELEASE_BOARD=x86-generic\n" 307 "CHROMEOS_RELEASE_TRACK=stable-channel\n" 308 "CHROMEOS_AUSERVER=http://www.google.com")); 309 SetLockDown(true); 310 OmahaRequestParams out(&fake_system_state_); 311 EXPECT_TRUE(DoTest(&out, "", "")); 312 EXPECT_EQ("arm-generic", out.os_board()); 313 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 314 EXPECT_EQ("0.2.2.3", out.app_version()); 315 EXPECT_EQ(fake_system_state_.hardware()->GetHardwareClass(), out.hwid()); 316 EXPECT_FALSE(out.delta_okay()); 317 EXPECT_EQ("stable-channel", out.target_channel()); 318 EXPECT_EQ("https://www.google.com", out.update_url()); 319 } 320 321 TEST_F(OmahaRequestParamsTest, OverrideSameChannelTest) { 322 ASSERT_TRUE(WriteFileString( 323 test_dir_ + "/etc/lsb-release", 324 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 325 "CHROMEOS_RELEASE_FOO=bar\n" 326 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 327 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 328 "CHROMEOS_AUSERVER=http://www.google.com")); 329 ASSERT_TRUE(WriteFileString( 330 test_dir_ + kStatefulPartition + "/etc/lsb-release", 331 "CHROMEOS_RELEASE_BOARD=x86-generic\n" 332 "CHROMEOS_RELEASE_TRACK=dev-channel")); 333 OmahaRequestParams out(&fake_system_state_); 334 EXPECT_TRUE(DoTest(&out, "", "")); 335 EXPECT_EQ("x86-generic", out.os_board()); 336 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 337 EXPECT_EQ("0.2.2.3", out.app_version()); 338 EXPECT_EQ(fake_system_state_.hardware()->GetHardwareClass(), out.hwid()); 339 EXPECT_TRUE(out.delta_okay()); 340 EXPECT_EQ("dev-channel", out.target_channel()); 341 EXPECT_EQ("http://www.google.com", out.update_url()); 342 } 343 344 TEST_F(OmahaRequestParamsTest, SetTargetChannelTest) { 345 ASSERT_TRUE(WriteFileString( 346 test_dir_ + "/etc/lsb-release", 347 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 348 "CHROMEOS_RELEASE_FOO=bar\n" 349 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 350 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 351 "CHROMEOS_AUSERVER=http://www.google.com")); 352 { 353 OmahaRequestParams params(&fake_system_state_); 354 params.set_root(test_dir_); 355 EXPECT_TRUE(params.Init("", "", false)); 356 params.SetTargetChannel("canary-channel", false, nullptr); 357 EXPECT_FALSE(params.is_powerwash_allowed()); 358 } 359 OmahaRequestParams out(&fake_system_state_); 360 out.set_root(test_dir_); 361 EXPECT_TRUE(DoTest(&out, "", "")); 362 EXPECT_EQ("canary-channel", out.target_channel()); 363 EXPECT_FALSE(out.is_powerwash_allowed()); 364 } 365 366 TEST_F(OmahaRequestParamsTest, SetIsPowerwashAllowedTest) { 367 ASSERT_TRUE(WriteFileString( 368 test_dir_ + "/etc/lsb-release", 369 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 370 "CHROMEOS_RELEASE_FOO=bar\n" 371 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 372 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 373 "CHROMEOS_AUSERVER=http://www.google.com")); 374 { 375 OmahaRequestParams params(&fake_system_state_); 376 params.set_root(test_dir_); 377 EXPECT_TRUE(params.Init("", "", false)); 378 params.SetTargetChannel("canary-channel", true, nullptr); 379 EXPECT_TRUE(params.is_powerwash_allowed()); 380 } 381 OmahaRequestParams out(&fake_system_state_); 382 out.set_root(test_dir_); 383 EXPECT_TRUE(DoTest(&out, "", "")); 384 EXPECT_EQ("canary-channel", out.target_channel()); 385 EXPECT_TRUE(out.is_powerwash_allowed()); 386 } 387 388 TEST_F(OmahaRequestParamsTest, SetTargetChannelInvalidTest) { 389 ASSERT_TRUE(WriteFileString( 390 test_dir_ + "/etc/lsb-release", 391 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 392 "CHROMEOS_RELEASE_FOO=bar\n" 393 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 394 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 395 "CHROMEOS_AUSERVER=http://www.google.com")); 396 { 397 OmahaRequestParams params(&fake_system_state_); 398 params.set_root(test_dir_); 399 SetLockDown(true); 400 EXPECT_TRUE(params.Init("", "", false)); 401 string error_message; 402 EXPECT_FALSE( 403 params.SetTargetChannel("dogfood-channel", true, &error_message)); 404 // The error message should include a message about the valid channels. 405 EXPECT_NE(string::npos, error_message.find("stable-channel")); 406 EXPECT_FALSE(params.is_powerwash_allowed()); 407 } 408 OmahaRequestParams out(&fake_system_state_); 409 out.set_root(test_dir_); 410 EXPECT_TRUE(DoTest(&out, "", "")); 411 EXPECT_EQ("arm-generic", out.os_board()); 412 EXPECT_EQ("dev-channel", out.target_channel()); 413 EXPECT_FALSE(out.is_powerwash_allowed()); 414 } 415 416 TEST_F(OmahaRequestParamsTest, IsValidChannelTest) { 417 EXPECT_TRUE(params_.IsValidChannel("canary-channel")); 418 EXPECT_TRUE(params_.IsValidChannel("stable-channel")); 419 EXPECT_TRUE(params_.IsValidChannel("beta-channel")); 420 EXPECT_TRUE(params_.IsValidChannel("dev-channel")); 421 EXPECT_FALSE(params_.IsValidChannel("testimage-channel")); 422 EXPECT_FALSE(params_.IsValidChannel("dogfood-channel")); 423 EXPECT_FALSE(params_.IsValidChannel("some-channel")); 424 EXPECT_FALSE(params_.IsValidChannel("")); 425 } 426 427 TEST_F(OmahaRequestParamsTest, ValidChannelTest) { 428 ASSERT_TRUE(WriteFileString( 429 test_dir_ + "/etc/lsb-release", 430 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 431 "CHROMEOS_RELEASE_FOO=bar\n" 432 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 433 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 434 "CHROMEOS_AUSERVER=http://www.google.com")); 435 SetLockDown(true); 436 OmahaRequestParams out(&fake_system_state_); 437 EXPECT_TRUE(DoTest(&out, "", "")); 438 EXPECT_EQ("Chrome OS", out.os_platform()); 439 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp()); 440 EXPECT_EQ("arm-generic", out.os_board()); 441 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.GetAppId()); 442 EXPECT_EQ("0.2.2.3", out.app_version()); 443 EXPECT_EQ("en-US", out.app_lang()); 444 EXPECT_EQ(fake_system_state_.hardware()->GetHardwareClass(), out.hwid()); 445 EXPECT_TRUE(out.delta_okay()); 446 EXPECT_EQ("dev-channel", out.target_channel()); 447 EXPECT_EQ("http://www.google.com", out.update_url()); 448 } 449 450 TEST_F(OmahaRequestParamsTest, SetTargetChannelWorks) { 451 ASSERT_TRUE(WriteFileString( 452 test_dir_ + "/etc/lsb-release", 453 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 454 "CHROMEOS_RELEASE_FOO=bar\n" 455 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 456 "CHROMEOS_RELEASE_TRACK=dev-channel\n" 457 "CHROMEOS_AUSERVER=http://www.google.com")); 458 459 // Check LSB value is used by default when SetTargetChannel is not called. 460 params_.Init("", "", false); 461 EXPECT_EQ("dev-channel", params_.target_channel()); 462 463 // When an invalid value is set, it should be ignored and the 464 // value from lsb-release should be used instead. 465 params_.Init("", "", false); 466 EXPECT_FALSE(params_.SetTargetChannel("invalid-channel", false, nullptr)); 467 EXPECT_EQ("dev-channel", params_.target_channel()); 468 469 // When set to a valid value, it should take effect. 470 params_.Init("", "", false); 471 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr)); 472 EXPECT_EQ("beta-channel", params_.target_channel()); 473 474 // When set to the same value, it should be idempotent. 475 params_.Init("", "", false); 476 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr)); 477 EXPECT_EQ("beta-channel", params_.target_channel()); 478 479 // When set to a valid value while a change is already pending, it should 480 // succeed. 481 params_.Init("", "", false); 482 EXPECT_TRUE(params_.SetTargetChannel("stable-channel", true, nullptr)); 483 EXPECT_EQ("stable-channel", params_.target_channel()); 484 485 // Set a different channel in stateful LSB release. 486 ASSERT_TRUE(WriteFileString( 487 test_dir_ + kStatefulPartition + "/etc/lsb-release", 488 "CHROMEOS_RELEASE_TRACK=stable-channel\n" 489 "CHROMEOS_IS_POWERWASH_ALLOWED=true\n")); 490 491 // When set to a valid value while a change is already pending, it should 492 // succeed. 493 params_.Init("", "", false); 494 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr)); 495 // The target channel should reflect the change, but the download channel 496 // should continue to retain the old value ... 497 EXPECT_EQ("beta-channel", params_.target_channel()); 498 EXPECT_EQ("stable-channel", params_.download_channel()); 499 500 // ... until we update the download channel explicitly. 501 params_.UpdateDownloadChannel(); 502 EXPECT_EQ("beta-channel", params_.download_channel()); 503 EXPECT_EQ("beta-channel", params_.target_channel()); 504 } 505 506 TEST_F(OmahaRequestParamsTest, ChannelIndexTest) { 507 int canary = params_.GetChannelIndex("canary-channel"); 508 int dev = params_.GetChannelIndex("dev-channel"); 509 int beta = params_.GetChannelIndex("beta-channel"); 510 int stable = params_.GetChannelIndex("stable-channel"); 511 EXPECT_LE(canary, dev); 512 EXPECT_LE(dev, beta); 513 EXPECT_LE(beta, stable); 514 515 // testimage-channel or other names are not recognized, so index will be -1. 516 int testimage = params_.GetChannelIndex("testimage-channel"); 517 int bogus = params_.GetChannelIndex("bogus-channel"); 518 EXPECT_EQ(-1, testimage); 519 EXPECT_EQ(-1, bogus); 520 } 521 522 TEST_F(OmahaRequestParamsTest, ToMoreStableChannelFlagTest) { 523 ASSERT_TRUE(WriteFileString( 524 test_dir_ + "/etc/lsb-release", 525 "CHROMEOS_RELEASE_BOARD=arm-generic\n" 526 "CHROMEOS_RELEASE_FOO=bar\n" 527 "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" 528 "CHROMEOS_RELEASE_TRACK=canary-channel\n" 529 "CHROMEOS_AUSERVER=http://www.google.com")); 530 ASSERT_TRUE(WriteFileString( 531 test_dir_ + kStatefulPartition + "/etc/lsb-release", 532 "CHROMEOS_RELEASE_BOARD=x86-generic\n" 533 "CHROMEOS_RELEASE_TRACK=stable-channel\n" 534 "CHROMEOS_AUSERVER=https://www.google.com")); 535 OmahaRequestParams out(&fake_system_state_); 536 EXPECT_TRUE(DoTest(&out, "", "")); 537 EXPECT_EQ("https://www.google.com", out.update_url()); 538 EXPECT_FALSE(out.delta_okay()); 539 EXPECT_EQ("stable-channel", out.target_channel()); 540 EXPECT_TRUE(out.to_more_stable_channel()); 541 } 542 543 TEST_F(OmahaRequestParamsTest, BoardAppIdUsedForNonCanaryChannelTest) { 544 ASSERT_TRUE(WriteFileString( 545 test_dir_ + "/etc/lsb-release", 546 "CHROMEOS_RELEASE_APPID=r\n" 547 "CHROMEOS_BOARD_APPID=b\n" 548 "CHROMEOS_CANARY_APPID=c\n" 549 "CHROMEOS_RELEASE_TRACK=stable-channel\n")); 550 OmahaRequestParams out(&fake_system_state_); 551 EXPECT_TRUE(DoTest(&out, "", "")); 552 EXPECT_EQ("stable-channel", out.download_channel()); 553 EXPECT_EQ("b", out.GetAppId()); 554 } 555 556 TEST_F(OmahaRequestParamsTest, CanaryAppIdUsedForCanaryChannelTest) { 557 ASSERT_TRUE(WriteFileString( 558 test_dir_ + "/etc/lsb-release", 559 "CHROMEOS_RELEASE_APPID=r\n" 560 "CHROMEOS_BOARD_APPID=b\n" 561 "CHROMEOS_CANARY_APPID=c\n" 562 "CHROMEOS_RELEASE_TRACK=canary-channel\n")); 563 OmahaRequestParams out(&fake_system_state_); 564 EXPECT_TRUE(DoTest(&out, "", "")); 565 EXPECT_EQ("canary-channel", out.download_channel()); 566 EXPECT_EQ("c", out.GetAppId()); 567 } 568 569 TEST_F(OmahaRequestParamsTest, ReleaseAppIdUsedAsDefaultTest) { 570 ASSERT_TRUE(WriteFileString( 571 test_dir_ + "/etc/lsb-release", 572 "CHROMEOS_RELEASE_APPID=r\n" 573 "CHROMEOS_CANARY_APPID=c\n" 574 "CHROMEOS_RELEASE_TRACK=stable-channel\n")); 575 OmahaRequestParams out(&fake_system_state_); 576 EXPECT_TRUE(DoTest(&out, "", "")); 577 EXPECT_EQ("stable-channel", out.download_channel()); 578 EXPECT_EQ("r", out.GetAppId()); 579 } 580 581 TEST_F(OmahaRequestParamsTest, CollectECFWVersionsTest) { 582 ASSERT_TRUE(WriteFileString( 583 test_dir_ + "/etc/lsb-release", 584 "CHROMEOS_RELEASE_APPID=r\n" 585 "CHROMEOS_CANARY_APPID=c\n" 586 "CHROMEOS_RELEASE_TRACK=stable-channel\n")); 587 OmahaRequestParams out(&fake_system_state_); 588 out.hwid_ = string("STUMPY ALEX 12345"); 589 EXPECT_FALSE(out.CollectECFWVersions()); 590 591 out.hwid_ = string("SNOW 12345"); 592 EXPECT_TRUE(out.CollectECFWVersions()); 593 594 out.hwid_ = string("SAMS ALEX 12345"); 595 EXPECT_TRUE(out.CollectECFWVersions()); 596 } 597 598 } // namespace chromeos_update_engine 599