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