1 // 2 // Copyright (C) 2017 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()); 39 osrelease_dir_ = tempdir_.path().Append("etc/os-release.d"); 40 EXPECT_TRUE(base::CreateDirectory(osrelease_dir_)); 41 test::SetImagePropertiesRootPrefix(tempdir_.path().value().c_str()); 42 } 43 44 void WriteOsRelease(const string& key, const string& value) { 45 ASSERT_TRUE(WriteFileString(osrelease_dir_.Append(key).value(), value)); 46 } 47 48 FakeSystemState fake_system_state_; 49 50 base::ScopedTempDir tempdir_; 51 base::FilePath osrelease_dir_; 52 }; 53 54 TEST_F(ImagePropertiesTest, SimpleTest) { 55 WriteOsRelease("product_id", "abc"); 56 WriteOsRelease("system_id", "def"); 57 WriteOsRelease("product_version", "1.2.3.4"); 58 WriteOsRelease("system_version", "5.6.7.8"); 59 ImageProperties props = LoadImageProperties(&fake_system_state_); 60 EXPECT_EQ("abc", props.product_id); 61 EXPECT_EQ("def", props.system_id); 62 EXPECT_EQ("1.2.3.4", props.version); 63 EXPECT_EQ("5.6.7.8", props.system_version); 64 EXPECT_EQ("stable-channel", props.current_channel); 65 EXPECT_EQ(constants::kOmahaDefaultProductionURL, props.omaha_url); 66 } 67 68 TEST_F(ImagePropertiesTest, IDPrefixTest) { 69 WriteOsRelease("product_id", "abc:def"); 70 WriteOsRelease("system_id", "foo:bar"); 71 ImageProperties props = LoadImageProperties(&fake_system_state_); 72 EXPECT_EQ("abc:def", props.product_id); 73 EXPECT_EQ("abc:bar", props.system_id); 74 } 75 76 TEST_F(ImagePropertiesTest, IDInvalidPrefixTest) { 77 WriteOsRelease("product_id", "def"); 78 WriteOsRelease("system_id", "foo:bar"); 79 ImageProperties props = LoadImageProperties(&fake_system_state_); 80 EXPECT_EQ("def", props.product_id); 81 EXPECT_EQ("foo:bar", props.system_id); 82 83 WriteOsRelease("product_id", "abc:def"); 84 WriteOsRelease("system_id", "bar"); 85 props = LoadImageProperties(&fake_system_state_); 86 EXPECT_EQ("abc:def", props.product_id); 87 EXPECT_EQ("bar", props.system_id); 88 } 89 90 } // namespace chromeos_update_engine 91