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 <base/files/scoped_temp_dir.h> 25 #include <gtest/gtest.h> 26 27 #include "update_engine/common/constants.h" 28 #include "update_engine/common/fake_prefs.h" 29 #include "update_engine/common/platform_constants.h" 30 #include "update_engine/common/test_utils.h" 31 #include "update_engine/common/utils.h" 32 #include "update_engine/fake_system_state.h" 33 34 using chromeos_update_engine::test_utils::WriteFileString; 35 using std::string; 36 37 namespace chromeos_update_engine { 38 39 class OmahaRequestParamsTest : public ::testing::Test { 40 public: 41 OmahaRequestParamsTest() : params_(&fake_system_state_) {} 42 43 protected: 44 void SetUp() override { 45 // Create a uniquely named test directory. 46 ASSERT_TRUE(tempdir_.CreateUniqueTempDir()); 47 params_.set_root(tempdir_.GetPath().value()); 48 SetLockDown(false); 49 fake_system_state_.set_prefs(&fake_prefs_); 50 } 51 52 void SetLockDown(bool locked_down) { 53 fake_system_state_.fake_hardware()->SetIsOfficialBuild(locked_down); 54 fake_system_state_.fake_hardware()->SetIsNormalBootMode(locked_down); 55 } 56 57 FakeSystemState fake_system_state_; 58 OmahaRequestParams params_{&fake_system_state_}; 59 FakePrefs fake_prefs_; 60 61 base::ScopedTempDir tempdir_; 62 }; 63 64 namespace { 65 string GetMachineType() { 66 string machine_type; 67 if (!utils::ReadPipe("uname -m", &machine_type)) 68 return ""; 69 // Strip anything from the first newline char. 70 size_t newline_pos = machine_type.find('\n'); 71 if (newline_pos != string::npos) 72 machine_type.erase(newline_pos); 73 return machine_type; 74 } 75 } // namespace 76 77 TEST_F(OmahaRequestParamsTest, MissingChannelTest) { 78 EXPECT_TRUE(params_.Init("", "", false)); 79 // By default, if no channel is set, we should track the stable-channel. 80 EXPECT_EQ("stable-channel", params_.target_channel()); 81 } 82 83 TEST_F(OmahaRequestParamsTest, ForceVersionTest) { 84 EXPECT_TRUE(params_.Init("ForcedVersion", "", false)); 85 EXPECT_EQ(string("ForcedVersion_") + GetMachineType(), params_.os_sp()); 86 EXPECT_EQ("ForcedVersion", params_.app_version()); 87 } 88 89 TEST_F(OmahaRequestParamsTest, ForcedURLTest) { 90 EXPECT_TRUE(params_.Init("", "http://forced.google.com", false)); 91 EXPECT_EQ("http://forced.google.com", params_.update_url()); 92 } 93 94 TEST_F(OmahaRequestParamsTest, MissingURLTest) { 95 EXPECT_TRUE(params_.Init("", "", false)); 96 EXPECT_EQ(constants::kOmahaDefaultProductionURL, params_.update_url()); 97 } 98 99 TEST_F(OmahaRequestParamsTest, DeltaOKTest) { 100 EXPECT_TRUE(params_.Init("", "", false)); 101 EXPECT_TRUE(params_.delta_okay()); 102 } 103 104 TEST_F(OmahaRequestParamsTest, NoDeltasTest) { 105 ASSERT_TRUE( 106 WriteFileString(tempdir_.GetPath().Append(".nodelta").value(), "")); 107 EXPECT_TRUE(params_.Init("", "", false)); 108 EXPECT_FALSE(params_.delta_okay()); 109 } 110 111 TEST_F(OmahaRequestParamsTest, SetTargetChannelTest) { 112 { 113 OmahaRequestParams params(&fake_system_state_); 114 params.set_root(tempdir_.GetPath().value()); 115 EXPECT_TRUE(params.Init("", "", false)); 116 EXPECT_TRUE(params.SetTargetChannel("canary-channel", false, nullptr)); 117 EXPECT_FALSE(params.mutable_image_props_.is_powerwash_allowed); 118 } 119 params_.set_root(tempdir_.GetPath().value()); 120 EXPECT_TRUE(params_.Init("", "", false)); 121 EXPECT_EQ("canary-channel", params_.target_channel()); 122 EXPECT_FALSE(params_.mutable_image_props_.is_powerwash_allowed); 123 } 124 125 TEST_F(OmahaRequestParamsTest, SetIsPowerwashAllowedTest) { 126 { 127 OmahaRequestParams params(&fake_system_state_); 128 params.set_root(tempdir_.GetPath().value()); 129 EXPECT_TRUE(params.Init("", "", false)); 130 EXPECT_TRUE(params.SetTargetChannel("canary-channel", true, nullptr)); 131 EXPECT_TRUE(params.mutable_image_props_.is_powerwash_allowed); 132 } 133 params_.set_root(tempdir_.GetPath().value()); 134 EXPECT_TRUE(params_.Init("", "", false)); 135 EXPECT_EQ("canary-channel", params_.target_channel()); 136 EXPECT_TRUE(params_.mutable_image_props_.is_powerwash_allowed); 137 } 138 139 TEST_F(OmahaRequestParamsTest, SetTargetChannelInvalidTest) { 140 { 141 OmahaRequestParams params(&fake_system_state_); 142 params.set_root(tempdir_.GetPath().value()); 143 SetLockDown(true); 144 EXPECT_TRUE(params.Init("", "", false)); 145 params.image_props_.allow_arbitrary_channels = false; 146 string error_message; 147 EXPECT_FALSE( 148 params.SetTargetChannel("dogfood-channel", true, &error_message)); 149 // The error message should include a message about the valid channels. 150 EXPECT_NE(string::npos, error_message.find("stable-channel")); 151 EXPECT_FALSE(params.mutable_image_props_.is_powerwash_allowed); 152 } 153 params_.set_root(tempdir_.GetPath().value()); 154 EXPECT_TRUE(params_.Init("", "", false)); 155 EXPECT_EQ("stable-channel", params_.target_channel()); 156 EXPECT_FALSE(params_.mutable_image_props_.is_powerwash_allowed); 157 } 158 159 TEST_F(OmahaRequestParamsTest, IsValidChannelTest) { 160 EXPECT_TRUE(params_.IsValidChannel("canary-channel")); 161 EXPECT_TRUE(params_.IsValidChannel("stable-channel")); 162 EXPECT_TRUE(params_.IsValidChannel("beta-channel")); 163 EXPECT_TRUE(params_.IsValidChannel("dev-channel")); 164 EXPECT_FALSE(params_.IsValidChannel("testimage-channel")); 165 EXPECT_FALSE(params_.IsValidChannel("dogfood-channel")); 166 EXPECT_FALSE(params_.IsValidChannel("some-channel")); 167 EXPECT_FALSE(params_.IsValidChannel("")); 168 params_.image_props_.allow_arbitrary_channels = true; 169 EXPECT_TRUE(params_.IsValidChannel("some-channel")); 170 EXPECT_FALSE(params_.IsValidChannel("wrong-suffix")); 171 EXPECT_FALSE(params_.IsValidChannel("")); 172 } 173 174 TEST_F(OmahaRequestParamsTest, SetTargetChannelWorks) { 175 params_.set_target_channel("dev-channel"); 176 EXPECT_EQ("dev-channel", params_.target_channel()); 177 178 // When an invalid value is set, it should be ignored. 179 EXPECT_FALSE(params_.SetTargetChannel("invalid-channel", false, nullptr)); 180 EXPECT_EQ("dev-channel", params_.target_channel()); 181 182 // When set to a valid value, it should take effect. 183 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr)); 184 EXPECT_EQ("beta-channel", params_.target_channel()); 185 186 // When set to the same value, it should be idempotent. 187 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr)); 188 EXPECT_EQ("beta-channel", params_.target_channel()); 189 190 // When set to a valid value while a change is already pending, it should 191 // succeed. 192 EXPECT_TRUE(params_.SetTargetChannel("stable-channel", true, nullptr)); 193 EXPECT_EQ("stable-channel", params_.target_channel()); 194 195 // Set a different channel in mutable_image_props_. 196 params_.set_target_channel("stable-channel"); 197 198 // When set to a valid value while a change is already pending, it should 199 // succeed. 200 params_.Init("", "", false); 201 EXPECT_TRUE(params_.SetTargetChannel("beta-channel", true, nullptr)); 202 // The target channel should reflect the change, but the download channel 203 // should continue to retain the old value ... 204 EXPECT_EQ("beta-channel", params_.target_channel()); 205 EXPECT_EQ("stable-channel", params_.download_channel()); 206 207 // ... until we update the download channel explicitly. 208 params_.UpdateDownloadChannel(); 209 EXPECT_EQ("beta-channel", params_.download_channel()); 210 EXPECT_EQ("beta-channel", params_.target_channel()); 211 } 212 213 TEST_F(OmahaRequestParamsTest, ChannelIndexTest) { 214 int canary = params_.GetChannelIndex("canary-channel"); 215 int dev = params_.GetChannelIndex("dev-channel"); 216 int beta = params_.GetChannelIndex("beta-channel"); 217 int stable = params_.GetChannelIndex("stable-channel"); 218 EXPECT_LE(canary, dev); 219 EXPECT_LE(dev, beta); 220 EXPECT_LE(beta, stable); 221 222 // testimage-channel or other names are not recognized, so index will be -1. 223 int testimage = params_.GetChannelIndex("testimage-channel"); 224 int bogus = params_.GetChannelIndex("bogus-channel"); 225 EXPECT_EQ(-1, testimage); 226 EXPECT_EQ(-1, bogus); 227 } 228 229 TEST_F(OmahaRequestParamsTest, ToMoreStableChannelFlagTest) { 230 params_.image_props_.current_channel = "canary-channel"; 231 params_.download_channel_ = "stable-channel"; 232 EXPECT_TRUE(params_.ToMoreStableChannel()); 233 params_.image_props_.current_channel = "stable-channel"; 234 EXPECT_FALSE(params_.ToMoreStableChannel()); 235 params_.download_channel_ = "beta-channel"; 236 EXPECT_FALSE(params_.ToMoreStableChannel()); 237 } 238 239 TEST_F(OmahaRequestParamsTest, ShouldPowerwashTest) { 240 params_.mutable_image_props_.is_powerwash_allowed = false; 241 EXPECT_FALSE(params_.ShouldPowerwash()); 242 params_.mutable_image_props_.is_powerwash_allowed = true; 243 params_.image_props_.allow_arbitrary_channels = true; 244 params_.image_props_.current_channel = "foo-channel"; 245 params_.download_channel_ = "bar-channel"; 246 EXPECT_TRUE(params_.ShouldPowerwash()); 247 params_.image_props_.allow_arbitrary_channels = false; 248 params_.image_props_.current_channel = "canary-channel"; 249 params_.download_channel_ = "stable-channel"; 250 EXPECT_TRUE(params_.ShouldPowerwash()); 251 } 252 253 TEST_F(OmahaRequestParamsTest, CollectECFWVersionsTest) { 254 params_.hwid_ = string("STUMPY ALEX 12345"); 255 EXPECT_FALSE(params_.CollectECFWVersions()); 256 257 params_.hwid_ = string("SNOW 12345"); 258 EXPECT_TRUE(params_.CollectECFWVersions()); 259 } 260 261 } // namespace chromeos_update_engine 262