1//
2// Copyright (C) 2016 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/image_properties.h"
18
19#include <string>
20
21#include <base/files/file_util.h>
22#include <base/files/scoped_temp_dir.h>
23#include <gtest/gtest.h>
24
25#include "update_engine/common/constants.h"
26#include "update_engine/common/test_utils.h"
27#include "update_engine/fake_system_state.h"
28
29using chromeos_update_engine::test_utils::WriteFileString;
30using std::string;
31
32namespace chromeos_update_engine {
33
34class ImagePropertiesTest : public ::testing::Test {
35 protected:
36  void SetUp() override {
37    // Create a uniquely named test directory.
38    ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
39    EXPECT_TRUE(base::CreateDirectory(tempdir_.path().Append("etc")));
40    EXPECT_TRUE(base::CreateDirectory(
41        base::FilePath(tempdir_.path().value() + kStatefulPartition + "/etc")));
42    test::SetImagePropertiesRootPrefix(tempdir_.path().value().c_str());
43    SetLockDown(false);
44  }
45
46  void SetLockDown(bool locked_down) {
47    fake_system_state_.fake_hardware()->SetIsOfficialBuild(locked_down);
48    fake_system_state_.fake_hardware()->SetIsNormalBootMode(locked_down);
49  }
50
51  FakeSystemState fake_system_state_;
52
53  base::ScopedTempDir tempdir_;
54};
55
56TEST_F(ImagePropertiesTest, SimpleTest) {
57  ASSERT_TRUE(WriteFileString(tempdir_.path().Append("etc/lsb-release").value(),
58                              "CHROMEOS_RELEASE_BOARD=arm-generic\n"
59                              "CHROMEOS_RELEASE_FOO=bar\n"
60                              "CHROMEOS_RELEASE_VERSION=0.2.2.3\n"
61                              "CHROMEOS_RELEASE_TRACK=dev-channel\n"
62                              "CHROMEOS_AUSERVER=http://www.google.com"));
63  ImageProperties props = LoadImageProperties(&fake_system_state_);
64  EXPECT_EQ("arm-generic", props.board);
65  EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", props.product_id);
66  EXPECT_EQ("0.2.2.3", props.version);
67  EXPECT_EQ("dev-channel", props.current_channel);
68  EXPECT_EQ("http://www.google.com", props.omaha_url);
69}
70
71TEST_F(ImagePropertiesTest, AppIDTest) {
72  ASSERT_TRUE(WriteFileString(
73      tempdir_.path().Append("etc/lsb-release").value(),
74      "CHROMEOS_RELEASE_APPID={58c35cef-9d30-476e-9098-ce20377d535d}"));
75  ImageProperties props = LoadImageProperties(&fake_system_state_);
76  EXPECT_EQ("{58c35cef-9d30-476e-9098-ce20377d535d}", props.product_id);
77}
78
79TEST_F(ImagePropertiesTest, ConfusingReleaseTest) {
80  ASSERT_TRUE(
81      WriteFileString(tempdir_.path().Append("etc/lsb-release").value(),
82                      "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
83                      "CHROMEOS_RELEASE_VERSION=0.2.2.3"));
84  ImageProperties props = LoadImageProperties(&fake_system_state_);
85  EXPECT_EQ("0.2.2.3", props.version);
86}
87
88TEST_F(ImagePropertiesTest, MissingVersionTest) {
89  ImageProperties props = LoadImageProperties(&fake_system_state_);
90  EXPECT_EQ("", props.version);
91}
92
93TEST_F(ImagePropertiesTest, OverrideTest) {
94  ASSERT_TRUE(WriteFileString(tempdir_.path().Append("etc/lsb-release").value(),
95                              "CHROMEOS_RELEASE_BOARD=arm-generic\n"
96                              "CHROMEOS_RELEASE_FOO=bar\n"
97                              "CHROMEOS_RELEASE_TRACK=dev-channel\n"
98                              "CHROMEOS_AUSERVER=http://www.google.com"));
99  ASSERT_TRUE(WriteFileString(
100      tempdir_.path().value() + kStatefulPartition + "/etc/lsb-release",
101      "CHROMEOS_RELEASE_BOARD=x86-generic\n"
102      "CHROMEOS_RELEASE_TRACK=beta-channel\n"
103      "CHROMEOS_AUSERVER=https://www.google.com"));
104  ImageProperties props = LoadImageProperties(&fake_system_state_);
105  EXPECT_EQ("x86-generic", props.board);
106  EXPECT_EQ("dev-channel", props.current_channel);
107  EXPECT_EQ("https://www.google.com", props.omaha_url);
108  MutableImageProperties mutable_props =
109      LoadMutableImageProperties(&fake_system_state_);
110  EXPECT_EQ("beta-channel", mutable_props.target_channel);
111}
112
113TEST_F(ImagePropertiesTest, OverrideLockDownTest) {
114  ASSERT_TRUE(WriteFileString(tempdir_.path().Append("etc/lsb-release").value(),
115                              "CHROMEOS_RELEASE_BOARD=arm-generic\n"
116                              "CHROMEOS_RELEASE_FOO=bar\n"
117                              "CHROMEOS_RELEASE_TRACK=dev-channel\n"
118                              "CHROMEOS_AUSERVER=https://www.google.com"));
119  ASSERT_TRUE(WriteFileString(
120      tempdir_.path().value() + kStatefulPartition + "/etc/lsb-release",
121      "CHROMEOS_RELEASE_BOARD=x86-generic\n"
122      "CHROMEOS_RELEASE_TRACK=stable-channel\n"
123      "CHROMEOS_AUSERVER=http://www.google.com"));
124  SetLockDown(true);
125  ImageProperties props = LoadImageProperties(&fake_system_state_);
126  EXPECT_EQ("arm-generic", props.board);
127  EXPECT_EQ("dev-channel", props.current_channel);
128  EXPECT_EQ("https://www.google.com", props.omaha_url);
129  MutableImageProperties mutable_props =
130      LoadMutableImageProperties(&fake_system_state_);
131  EXPECT_EQ("stable-channel", mutable_props.target_channel);
132}
133
134TEST_F(ImagePropertiesTest, BoardAppIdUsedForNonCanaryChannelTest) {
135  ASSERT_TRUE(WriteFileString(tempdir_.path().Append("etc/lsb-release").value(),
136                              "CHROMEOS_RELEASE_APPID=r\n"
137                              "CHROMEOS_BOARD_APPID=b\n"
138                              "CHROMEOS_CANARY_APPID=c\n"
139                              "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
140  ImageProperties props = LoadImageProperties(&fake_system_state_);
141  EXPECT_EQ("stable-channel", props.current_channel);
142  EXPECT_EQ("b", props.product_id);
143}
144
145TEST_F(ImagePropertiesTest, CanaryAppIdUsedForCanaryChannelTest) {
146  ASSERT_TRUE(WriteFileString(tempdir_.path().Append("etc/lsb-release").value(),
147                              "CHROMEOS_RELEASE_APPID=r\n"
148                              "CHROMEOS_BOARD_APPID=b\n"
149                              "CHROMEOS_CANARY_APPID=c\n"
150                              "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
151  ImageProperties props = LoadImageProperties(&fake_system_state_);
152  EXPECT_EQ("canary-channel", props.current_channel);
153  EXPECT_EQ("c", props.canary_product_id);
154}
155
156TEST_F(ImagePropertiesTest, ReleaseAppIdUsedAsDefaultTest) {
157  ASSERT_TRUE(WriteFileString(tempdir_.path().Append("etc/lsb-release").value(),
158                              "CHROMEOS_RELEASE_APPID=r\n"
159                              "CHROMEOS_CANARY_APPID=c\n"
160                              "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
161  ImageProperties props = LoadImageProperties(&fake_system_state_);
162  EXPECT_EQ("stable-channel", props.current_channel);
163  EXPECT_EQ("r", props.product_id);
164}
165
166}  // namespace chromeos_update_engine
167