| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 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 |  | 
 | 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: | 
 | 36 |   void SetUp() override { | 
 | 37 |     // Create a uniquely named test directory. | 
 | 38 |     ASSERT_TRUE(tempdir_.CreateUniqueTempDir()); | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 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()); | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 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 |  | 
 | 56 | TEST_F(ImagePropertiesTest, SimpleTest) { | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 57 |   ASSERT_TRUE( | 
 | 58 |       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(), | 
 | 59 |                       "CHROMEOS_RELEASE_BOARD=arm-generic\n" | 
 | 60 |                       "CHROMEOS_RELEASE_FOO=bar\n" | 
 | 61 |                       "CHROMEOS_RELEASE_VERSION=0.2.2.3\n" | 
 | 62 |                       "CHROMEOS_RELEASE_TRACK=dev-channel\n" | 
 | 63 |                       "CHROMEOS_AUSERVER=http://www.google.com")); | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 64 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 65 |   EXPECT_EQ("arm-generic", props.board); | 
 | 66 |   EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", props.product_id); | 
 | 67 |   EXPECT_EQ("0.2.2.3", props.version); | 
 | 68 |   EXPECT_EQ("dev-channel", props.current_channel); | 
 | 69 |   EXPECT_EQ("http://www.google.com", props.omaha_url); | 
 | 70 | } | 
 | 71 |  | 
 | 72 | TEST_F(ImagePropertiesTest, AppIDTest) { | 
 | 73 |   ASSERT_TRUE(WriteFileString( | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 74 |       tempdir_.GetPath().Append("etc/lsb-release").value(), | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 75 |       "CHROMEOS_RELEASE_APPID={58c35cef-9d30-476e-9098-ce20377d535d}")); | 
 | 76 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 77 |   EXPECT_EQ("{58c35cef-9d30-476e-9098-ce20377d535d}", props.product_id); | 
 | 78 | } | 
 | 79 |  | 
 | 80 | TEST_F(ImagePropertiesTest, ConfusingReleaseTest) { | 
 | 81 |   ASSERT_TRUE( | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 82 |       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(), | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 83 |                       "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n" | 
 | 84 |                       "CHROMEOS_RELEASE_VERSION=0.2.2.3")); | 
 | 85 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 86 |   EXPECT_EQ("0.2.2.3", props.version); | 
 | 87 | } | 
 | 88 |  | 
 | 89 | TEST_F(ImagePropertiesTest, MissingVersionTest) { | 
 | 90 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 91 |   EXPECT_EQ("", props.version); | 
 | 92 | } | 
 | 93 |  | 
 | 94 | TEST_F(ImagePropertiesTest, OverrideTest) { | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 95 |   ASSERT_TRUE( | 
 | 96 |       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(), | 
 | 97 |                       "CHROMEOS_RELEASE_BOARD=arm-generic\n" | 
 | 98 |                       "CHROMEOS_RELEASE_FOO=bar\n" | 
 | 99 |                       "CHROMEOS_RELEASE_TRACK=dev-channel\n" | 
 | 100 |                       "CHROMEOS_AUSERVER=http://www.google.com")); | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 101 |   ASSERT_TRUE(WriteFileString( | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 102 |       tempdir_.GetPath().value() + kStatefulPartition + "/etc/lsb-release", | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 103 |       "CHROMEOS_RELEASE_BOARD=x86-generic\n" | 
 | 104 |       "CHROMEOS_RELEASE_TRACK=beta-channel\n" | 
 | 105 |       "CHROMEOS_AUSERVER=https://www.google.com")); | 
 | 106 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 107 |   EXPECT_EQ("x86-generic", props.board); | 
 | 108 |   EXPECT_EQ("dev-channel", props.current_channel); | 
 | 109 |   EXPECT_EQ("https://www.google.com", props.omaha_url); | 
 | 110 |   MutableImageProperties mutable_props = | 
 | 111 |       LoadMutableImageProperties(&fake_system_state_); | 
 | 112 |   EXPECT_EQ("beta-channel", mutable_props.target_channel); | 
 | 113 | } | 
 | 114 |  | 
 | 115 | TEST_F(ImagePropertiesTest, OverrideLockDownTest) { | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 116 |   ASSERT_TRUE( | 
 | 117 |       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(), | 
 | 118 |                       "CHROMEOS_RELEASE_BOARD=arm-generic\n" | 
 | 119 |                       "CHROMEOS_RELEASE_FOO=bar\n" | 
 | 120 |                       "CHROMEOS_RELEASE_TRACK=dev-channel\n" | 
 | 121 |                       "CHROMEOS_AUSERVER=https://www.google.com")); | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 122 |   ASSERT_TRUE(WriteFileString( | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 123 |       tempdir_.GetPath().value() + kStatefulPartition + "/etc/lsb-release", | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 124 |       "CHROMEOS_RELEASE_BOARD=x86-generic\n" | 
 | 125 |       "CHROMEOS_RELEASE_TRACK=stable-channel\n" | 
 | 126 |       "CHROMEOS_AUSERVER=http://www.google.com")); | 
 | 127 |   SetLockDown(true); | 
 | 128 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 129 |   EXPECT_EQ("arm-generic", props.board); | 
 | 130 |   EXPECT_EQ("dev-channel", props.current_channel); | 
 | 131 |   EXPECT_EQ("https://www.google.com", props.omaha_url); | 
 | 132 |   MutableImageProperties mutable_props = | 
 | 133 |       LoadMutableImageProperties(&fake_system_state_); | 
 | 134 |   EXPECT_EQ("stable-channel", mutable_props.target_channel); | 
 | 135 | } | 
 | 136 |  | 
 | 137 | TEST_F(ImagePropertiesTest, BoardAppIdUsedForNonCanaryChannelTest) { | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 138 |   ASSERT_TRUE( | 
 | 139 |       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(), | 
 | 140 |                       "CHROMEOS_RELEASE_APPID=r\n" | 
 | 141 |                       "CHROMEOS_BOARD_APPID=b\n" | 
 | 142 |                       "CHROMEOS_CANARY_APPID=c\n" | 
 | 143 |                       "CHROMEOS_RELEASE_TRACK=stable-channel\n")); | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 144 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 145 |   EXPECT_EQ("stable-channel", props.current_channel); | 
 | 146 |   EXPECT_EQ("b", props.product_id); | 
 | 147 | } | 
 | 148 |  | 
 | 149 | TEST_F(ImagePropertiesTest, CanaryAppIdUsedForCanaryChannelTest) { | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 150 |   ASSERT_TRUE( | 
 | 151 |       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(), | 
 | 152 |                       "CHROMEOS_RELEASE_APPID=r\n" | 
 | 153 |                       "CHROMEOS_BOARD_APPID=b\n" | 
 | 154 |                       "CHROMEOS_CANARY_APPID=c\n" | 
 | 155 |                       "CHROMEOS_RELEASE_TRACK=canary-channel\n")); | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 156 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 157 |   EXPECT_EQ("canary-channel", props.current_channel); | 
 | 158 |   EXPECT_EQ("c", props.canary_product_id); | 
 | 159 | } | 
 | 160 |  | 
 | 161 | TEST_F(ImagePropertiesTest, ReleaseAppIdUsedAsDefaultTest) { | 
| Hidehiko Abe | 2b9d241 | 2017-12-13 18:56:18 +0900 | [diff] [blame] | 162 |   ASSERT_TRUE( | 
 | 163 |       WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(), | 
 | 164 |                       "CHROMEOS_RELEASE_APPID=r\n" | 
 | 165 |                       "CHROMEOS_CANARY_APPID=c\n" | 
 | 166 |                       "CHROMEOS_RELEASE_TRACK=stable-channel\n")); | 
| Sen Jiang | 297e583 | 2016-03-17 14:45:51 -0700 | [diff] [blame] | 167 |   ImageProperties props = LoadImageProperties(&fake_system_state_); | 
 | 168 |   EXPECT_EQ("stable-channel", props.current_channel); | 
 | 169 |   EXPECT_EQ("r", props.product_id); | 
 | 170 | } | 
 | 171 |  | 
 | 172 | }  // namespace chromeos_update_engine |