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/fake_prefs.h"
27 #include "update_engine/common/test_utils.h"
28 #include "update_engine/fake_system_state.h"
29
30 using chromeos_update_engine::test_utils::WriteFileString;
31 using std::string;
32
33 namespace chromeos_update_engine {
34
35 class ImagePropertiesTest : public ::testing::Test {
36 protected:
SetUp()37 void SetUp() override {
38 // Create a uniquely named test directory.
39 ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
40 osrelease_dir_ = tempdir_.GetPath().Append("etc/os-release.d");
41 EXPECT_TRUE(base::CreateDirectory(osrelease_dir_));
42 test::SetImagePropertiesRootPrefix(tempdir_.GetPath().value().c_str());
43 }
44
WriteOsRelease(const string & key,const string & value)45 void WriteOsRelease(const string& key, const string& value) {
46 ASSERT_TRUE(WriteFileString(osrelease_dir_.Append(key).value(), value));
47 }
48
WriteChannel(const string & channel)49 void WriteChannel(const string& channel) {
50 string misc(2080, '\0');
51 misc += channel;
52 misc.resize(4096);
53 ASSERT_TRUE(
54 WriteFileString(tempdir_.GetPath().Append("misc").value(), misc));
55 }
56
57 FakeSystemState fake_system_state_;
58
59 base::ScopedTempDir tempdir_;
60 base::FilePath osrelease_dir_;
61 };
62
TEST_F(ImagePropertiesTest,SimpleTest)63 TEST_F(ImagePropertiesTest, SimpleTest) {
64 WriteOsRelease("product_id", "abc");
65 WriteOsRelease("system_id", "def");
66 WriteOsRelease("product_version", "1.2.3.4");
67 WriteOsRelease("system_version", "5.6.7.8");
68 ImageProperties props = LoadImageProperties(&fake_system_state_);
69 EXPECT_EQ("abc", props.product_id);
70 EXPECT_EQ("def", props.system_id);
71 EXPECT_EQ("1.2.3.4", props.version);
72 EXPECT_EQ("5.6.7.8", props.system_version);
73 EXPECT_EQ("stable-channel", props.current_channel);
74 EXPECT_EQ(constants::kOmahaDefaultProductionURL, props.omaha_url);
75 }
76
TEST_F(ImagePropertiesTest,IDPrefixTest)77 TEST_F(ImagePropertiesTest, IDPrefixTest) {
78 WriteOsRelease("product_id", "abc:def");
79 WriteOsRelease("system_id", "foo:bar");
80 ImageProperties props = LoadImageProperties(&fake_system_state_);
81 EXPECT_EQ("abc:def", props.product_id);
82 EXPECT_EQ("abc:bar", props.system_id);
83 }
84
TEST_F(ImagePropertiesTest,IDInvalidPrefixTest)85 TEST_F(ImagePropertiesTest, IDInvalidPrefixTest) {
86 WriteOsRelease("product_id", "def");
87 WriteOsRelease("system_id", "foo:bar");
88 ImageProperties props = LoadImageProperties(&fake_system_state_);
89 EXPECT_EQ("def", props.product_id);
90 EXPECT_EQ("foo:bar", props.system_id);
91
92 WriteOsRelease("product_id", "abc:def");
93 WriteOsRelease("system_id", "bar");
94 props = LoadImageProperties(&fake_system_state_);
95 EXPECT_EQ("abc:def", props.product_id);
96 EXPECT_EQ("bar", props.system_id);
97 }
98
TEST_F(ImagePropertiesTest,LoadChannelTest)99 TEST_F(ImagePropertiesTest, LoadChannelTest) {
100 WriteChannel("unittest-channel");
101 ImageProperties props = LoadImageProperties(&fake_system_state_);
102 EXPECT_EQ("unittest-channel", props.current_channel);
103 }
104
TEST_F(ImagePropertiesTest,DefaultStableChannelTest)105 TEST_F(ImagePropertiesTest, DefaultStableChannelTest) {
106 WriteChannel("");
107 ImageProperties props = LoadImageProperties(&fake_system_state_);
108 EXPECT_EQ("stable-channel", props.current_channel);
109 }
110
TEST_F(ImagePropertiesTest,StoreLoadMutableChannelTest)111 TEST_F(ImagePropertiesTest, StoreLoadMutableChannelTest) {
112 FakePrefs prefs;
113 fake_system_state_.set_prefs(&prefs);
114 WriteChannel("previous-channel");
115 MutableImageProperties props;
116 props.target_channel = "new-channel";
117 EXPECT_TRUE(StoreMutableImageProperties(&fake_system_state_, props));
118 MutableImageProperties loaded_props =
119 LoadMutableImageProperties(&fake_system_state_);
120 EXPECT_EQ(props.target_channel, loaded_props.target_channel);
121 }
122
123 } // namespace chromeos_update_engine
124