• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/cros/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/cros/fake_system_state.h"
28 
29 using chromeos_update_engine::test_utils::WriteFileString;
30 using std::string;
31 
32 namespace chromeos_update_engine {
33 
34 class ImagePropertiesTest : public ::testing::Test {
35  protected:
SetUp()36   void SetUp() override {
37     // Create a uniquely named test directory.
38     ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
39     EXPECT_TRUE(base::CreateDirectory(tempdir_.GetPath().Append("etc")));
40     EXPECT_TRUE(base::CreateDirectory(base::FilePath(
41         tempdir_.GetPath().value() + kStatefulPartition + "/etc")));
42     test::SetImagePropertiesRootPrefix(tempdir_.GetPath().value().c_str());
43     FakeSystemState::CreateInstance();
44     SetLockDown(false);
45   }
46 
SetLockDown(bool locked_down)47   void SetLockDown(bool locked_down) {
48     FakeSystemState::Get()->fake_hardware()->SetIsOfficialBuild(locked_down);
49     FakeSystemState::Get()->fake_hardware()->SetIsNormalBootMode(locked_down);
50   }
51 
52   base::ScopedTempDir tempdir_;
53 };
54 
TEST_F(ImagePropertiesTest,SimpleTest)55 TEST_F(ImagePropertiesTest, SimpleTest) {
56   ASSERT_TRUE(
57       WriteFileString(tempdir_.GetPath().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();
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 
TEST_F(ImagePropertiesTest,AppIDTest)71 TEST_F(ImagePropertiesTest, AppIDTest) {
72   ASSERT_TRUE(WriteFileString(
73       tempdir_.GetPath().Append("etc/lsb-release").value(),
74       "CHROMEOS_RELEASE_APPID={58c35cef-9d30-476e-9098-ce20377d535d}"));
75   ImageProperties props = LoadImageProperties();
76   EXPECT_EQ("{58c35cef-9d30-476e-9098-ce20377d535d}", props.product_id);
77 }
78 
TEST_F(ImagePropertiesTest,ConfusingReleaseTest)79 TEST_F(ImagePropertiesTest, ConfusingReleaseTest) {
80   ASSERT_TRUE(
81       WriteFileString(tempdir_.GetPath().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();
85   EXPECT_EQ("0.2.2.3", props.version);
86 }
87 
TEST_F(ImagePropertiesTest,MissingVersionTest)88 TEST_F(ImagePropertiesTest, MissingVersionTest) {
89   ImageProperties props = LoadImageProperties();
90   EXPECT_EQ("", props.version);
91 }
92 
TEST_F(ImagePropertiesTest,OverrideTest)93 TEST_F(ImagePropertiesTest, OverrideTest) {
94   ASSERT_TRUE(
95       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
96                       "CHROMEOS_RELEASE_BOARD=arm-generic\n"
97                       "CHROMEOS_RELEASE_FOO=bar\n"
98                       "CHROMEOS_RELEASE_TRACK=dev-channel\n"
99                       "CHROMEOS_AUSERVER=http://www.google.com"));
100   ASSERT_TRUE(WriteFileString(
101       tempdir_.GetPath().value() + kStatefulPartition + "/etc/lsb-release",
102       "CHROMEOS_RELEASE_BOARD=x86-generic\n"
103       "CHROMEOS_RELEASE_TRACK=beta-channel\n"
104       "CHROMEOS_AUSERVER=https://www.google.com"));
105   ImageProperties props = LoadImageProperties();
106   EXPECT_EQ("x86-generic", props.board);
107   EXPECT_EQ("dev-channel", props.current_channel);
108   EXPECT_EQ("https://www.google.com", props.omaha_url);
109   MutableImageProperties mutable_props = LoadMutableImageProperties();
110   EXPECT_EQ("beta-channel", mutable_props.target_channel);
111 }
112 
TEST_F(ImagePropertiesTest,OverrideLockDownTest)113 TEST_F(ImagePropertiesTest, OverrideLockDownTest) {
114   ASSERT_TRUE(
115       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
116                       "CHROMEOS_RELEASE_BOARD=arm-generic\n"
117                       "CHROMEOS_RELEASE_FOO=bar\n"
118                       "CHROMEOS_RELEASE_TRACK=dev-channel\n"
119                       "CHROMEOS_AUSERVER=https://www.google.com"));
120   ASSERT_TRUE(WriteFileString(
121       tempdir_.GetPath().value() + kStatefulPartition + "/etc/lsb-release",
122       "CHROMEOS_RELEASE_BOARD=x86-generic\n"
123       "CHROMEOS_RELEASE_TRACK=stable-channel\n"
124       "CHROMEOS_AUSERVER=http://www.google.com"));
125   SetLockDown(true);
126   ImageProperties props = LoadImageProperties();
127   EXPECT_EQ("arm-generic", props.board);
128   EXPECT_EQ("dev-channel", props.current_channel);
129   EXPECT_EQ("https://www.google.com", props.omaha_url);
130   MutableImageProperties mutable_props = LoadMutableImageProperties();
131   EXPECT_EQ("stable-channel", mutable_props.target_channel);
132 }
133 
TEST_F(ImagePropertiesTest,BoardAppIdUsedForNonCanaryChannelTest)134 TEST_F(ImagePropertiesTest, BoardAppIdUsedForNonCanaryChannelTest) {
135   ASSERT_TRUE(
136       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
137                       "CHROMEOS_RELEASE_APPID=r\n"
138                       "CHROMEOS_BOARD_APPID=b\n"
139                       "CHROMEOS_CANARY_APPID=c\n"
140                       "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
141   ImageProperties props = LoadImageProperties();
142   EXPECT_EQ("stable-channel", props.current_channel);
143   EXPECT_EQ("b", props.product_id);
144 }
145 
TEST_F(ImagePropertiesTest,CanaryAppIdUsedForCanaryChannelTest)146 TEST_F(ImagePropertiesTest, CanaryAppIdUsedForCanaryChannelTest) {
147   ASSERT_TRUE(
148       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
149                       "CHROMEOS_RELEASE_APPID=r\n"
150                       "CHROMEOS_BOARD_APPID=b\n"
151                       "CHROMEOS_CANARY_APPID=c\n"
152                       "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
153   ImageProperties props = LoadImageProperties();
154   EXPECT_EQ("canary-channel", props.current_channel);
155   EXPECT_EQ("c", props.canary_product_id);
156 }
157 
TEST_F(ImagePropertiesTest,ReleaseAppIdUsedAsDefaultTest)158 TEST_F(ImagePropertiesTest, ReleaseAppIdUsedAsDefaultTest) {
159   ASSERT_TRUE(
160       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
161                       "CHROMEOS_RELEASE_APPID=r\n"
162                       "CHROMEOS_CANARY_APPID=c\n"
163                       "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
164   ImageProperties props = LoadImageProperties();
165   EXPECT_EQ("stable-channel", props.current_channel);
166   EXPECT_EQ("r", props.product_id);
167 }
168 
169 }  // namespace chromeos_update_engine
170