1 //
2 // Copyright (C) 2014 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/update_manager/real_config_provider.h"
18
19 #include <memory>
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_hardware.h"
27 #include "update_engine/common/test_utils.h"
28 #include "update_engine/update_manager/umtest_utils.h"
29
30 using base::TimeDelta;
31 using chromeos_update_engine::test_utils::WriteFileString;
32 using std::string;
33 using std::unique_ptr;
34
35 namespace chromeos_update_manager {
36
37 class UmRealConfigProviderTest : public ::testing::Test {
38 protected:
SetUp()39 void SetUp() override {
40 ASSERT_TRUE(root_dir_.CreateUniqueTempDir());
41 provider_.reset(new RealConfigProvider(&fake_hardware_));
42 provider_->SetRootPrefix(root_dir_.path().value());
43 }
44
WriteStatefulConfig(const string & config)45 void WriteStatefulConfig(const string& config) {
46 base::FilePath kFile(root_dir_.path().value()
47 + chromeos_update_engine::kStatefulPartition
48 + "/etc/update_manager.conf");
49 ASSERT_TRUE(base::CreateDirectory(kFile.DirName()));
50 ASSERT_TRUE(WriteFileString(kFile.value(), config));
51 }
52
WriteRootfsConfig(const string & config)53 void WriteRootfsConfig(const string& config) {
54 base::FilePath kFile(root_dir_.path().value()
55 + "/etc/update_manager.conf");
56 ASSERT_TRUE(base::CreateDirectory(kFile.DirName()));
57 ASSERT_TRUE(WriteFileString(kFile.value(), config));
58 }
59
60 unique_ptr<RealConfigProvider> provider_;
61 chromeos_update_engine::FakeHardware fake_hardware_;
62 TimeDelta default_timeout_ = TimeDelta::FromSeconds(1);
63 base::ScopedTempDir root_dir_;
64 };
65
TEST_F(UmRealConfigProviderTest,InitTest)66 TEST_F(UmRealConfigProviderTest, InitTest) {
67 EXPECT_TRUE(provider_->Init());
68 EXPECT_NE(nullptr, provider_->var_is_oobe_enabled());
69 }
70
TEST_F(UmRealConfigProviderTest,NoFileFoundReturnsDefault)71 TEST_F(UmRealConfigProviderTest, NoFileFoundReturnsDefault) {
72 EXPECT_TRUE(provider_->Init());
73 UmTestUtils::ExpectVariableHasValue(true, provider_->var_is_oobe_enabled());
74 }
75
TEST_F(UmRealConfigProviderTest,DontReadStatefulInNormalMode)76 TEST_F(UmRealConfigProviderTest, DontReadStatefulInNormalMode) {
77 fake_hardware_.SetIsNormalBootMode(true);
78 WriteStatefulConfig("is_oobe_enabled=false");
79
80 EXPECT_TRUE(provider_->Init());
81 UmTestUtils::ExpectVariableHasValue(true, provider_->var_is_oobe_enabled());
82 }
83
TEST_F(UmRealConfigProviderTest,ReadStatefulInDevMode)84 TEST_F(UmRealConfigProviderTest, ReadStatefulInDevMode) {
85 fake_hardware_.SetIsNormalBootMode(false);
86 WriteRootfsConfig("is_oobe_enabled=true");
87 // Since the stateful is present, this should read that one.
88 WriteStatefulConfig("is_oobe_enabled=false");
89
90 EXPECT_TRUE(provider_->Init());
91 UmTestUtils::ExpectVariableHasValue(false, provider_->var_is_oobe_enabled());
92 }
93
TEST_F(UmRealConfigProviderTest,ReadRootfsIfStatefulNotFound)94 TEST_F(UmRealConfigProviderTest, ReadRootfsIfStatefulNotFound) {
95 fake_hardware_.SetIsNormalBootMode(false);
96 WriteRootfsConfig("is_oobe_enabled=false");
97
98 EXPECT_TRUE(provider_->Init());
99 UmTestUtils::ExpectVariableHasValue(false, provider_->var_is_oobe_enabled());
100 }
101
102 } // namespace chromeos_update_manager
103