blob: 162dec4b1641d8fb4b7b3011600fda9d74ccfea6 [file] [log] [blame]
Alex Deymo46a9aae2016-05-04 20:20:11 -07001//
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/hardware_chromeos.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
30using chromeos_update_engine::test_utils::WriteFileString;
31using std::string;
32
33namespace chromeos_update_engine {
34
35class HardwareChromeOSTest : public ::testing::Test {
36 protected:
37 void SetUp() override { ASSERT_TRUE(root_dir_.CreateUniqueTempDir()); }
38
39 void WriteStatefulConfig(const string& config) {
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090040 base::FilePath kFile(root_dir_.GetPath().value() + kStatefulPartition +
Alex Deymo46a9aae2016-05-04 20:20:11 -070041 "/etc/update_manager.conf");
42 ASSERT_TRUE(base::CreateDirectory(kFile.DirName()));
43 ASSERT_TRUE(WriteFileString(kFile.value(), config));
44 }
45
46 void WriteRootfsConfig(const string& config) {
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090047 base::FilePath kFile(root_dir_.GetPath().value() +
48 "/etc/update_manager.conf");
Alex Deymo46a9aae2016-05-04 20:20:11 -070049 ASSERT_TRUE(base::CreateDirectory(kFile.DirName()));
50 ASSERT_TRUE(WriteFileString(kFile.value(), config));
51 }
52
53 // Helper method to call HardwareChromeOS::LoadConfig with the test directory.
54 void CallLoadConfig(bool normal_mode) {
Hidehiko Abe2b9d2412017-12-13 18:56:18 +090055 hardware_.LoadConfig(root_dir_.GetPath().value(), normal_mode);
Alex Deymo46a9aae2016-05-04 20:20:11 -070056 }
57
58 HardwareChromeOS hardware_;
59 base::ScopedTempDir root_dir_;
60};
61
62TEST_F(HardwareChromeOSTest, NoFileFoundReturnsDefault) {
63 CallLoadConfig(true /* normal_mode */);
64 EXPECT_TRUE(hardware_.IsOOBEEnabled());
65}
66
67TEST_F(HardwareChromeOSTest, DontReadStatefulInNormalMode) {
68 WriteStatefulConfig("is_oobe_enabled=false");
69
70 CallLoadConfig(true /* normal_mode */);
71 EXPECT_TRUE(hardware_.IsOOBEEnabled());
72}
73
74TEST_F(HardwareChromeOSTest, ReadStatefulInDevMode) {
75 WriteRootfsConfig("is_oobe_enabled=true");
76 // Since the stateful is present, we should read that one.
77 WriteStatefulConfig("is_oobe_enabled=false");
78
79 CallLoadConfig(false /* normal_mode */);
80 EXPECT_FALSE(hardware_.IsOOBEEnabled());
81}
82
83TEST_F(HardwareChromeOSTest, ReadRootfsIfStatefulNotFound) {
84 WriteRootfsConfig("is_oobe_enabled=false");
85
86 CallLoadConfig(false /* normal_mode */);
87 EXPECT_FALSE(hardware_.IsOOBEEnabled());
88}
89
90} // namespace chromeos_update_engine