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/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 
30 using chromeos_update_engine::test_utils::WriteFileString;
31 using std::string;
32 
33 namespace chromeos_update_engine {
34 
35 class HardwareChromeOSTest : public ::testing::Test {
36  protected:
37   void SetUp() override { ASSERT_TRUE(root_dir_.CreateUniqueTempDir()); }
38 
39   void WriteStatefulConfig(const string& config) {
40     base::FilePath kFile(root_dir_.GetPath().value() + kStatefulPartition +
41                          "/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) {
47     base::FilePath kFile(root_dir_.GetPath().value() +
48                          "/etc/update_manager.conf");
49     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) {
55     hardware_.LoadConfig(root_dir_.GetPath().value(), normal_mode);
56   }
57 
58   HardwareChromeOS hardware_;
59   base::ScopedTempDir root_dir_;
60 };
61 
62 TEST_F(HardwareChromeOSTest, NoFileFoundReturnsDefault) {
63   CallLoadConfig(true /* normal_mode */);
64   EXPECT_TRUE(hardware_.IsOOBEEnabled());
65 }
66 
67 TEST_F(HardwareChromeOSTest, DontReadStatefulInNormalMode) {
68   WriteStatefulConfig("is_oobe_enabled=false");
69 
70   CallLoadConfig(true /* normal_mode */);
71   EXPECT_TRUE(hardware_.IsOOBEEnabled());
72 }
73 
74 TEST_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 
83 TEST_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
91