Home | History | Annotate | Download | only in update_engine
      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_.path().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_.path().value() + "/etc/update_manager.conf");
     48     ASSERT_TRUE(base::CreateDirectory(kFile.DirName()));
     49     ASSERT_TRUE(WriteFileString(kFile.value(), config));
     50   }
     51 
     52   // Helper method to call HardwareChromeOS::LoadConfig with the test directory.
     53   void CallLoadConfig(bool normal_mode) {
     54     hardware_.LoadConfig(root_dir_.path().value(), normal_mode);
     55   }
     56 
     57   HardwareChromeOS hardware_;
     58   base::ScopedTempDir root_dir_;
     59 };
     60 
     61 TEST_F(HardwareChromeOSTest, NoFileFoundReturnsDefault) {
     62   CallLoadConfig(true /* normal_mode */);
     63   EXPECT_TRUE(hardware_.IsOOBEEnabled());
     64 }
     65 
     66 TEST_F(HardwareChromeOSTest, DontReadStatefulInNormalMode) {
     67   WriteStatefulConfig("is_oobe_enabled=false");
     68 
     69   CallLoadConfig(true /* normal_mode */);
     70   EXPECT_TRUE(hardware_.IsOOBEEnabled());
     71 }
     72 
     73 TEST_F(HardwareChromeOSTest, ReadStatefulInDevMode) {
     74   WriteRootfsConfig("is_oobe_enabled=true");
     75   // Since the stateful is present, we should read that one.
     76   WriteStatefulConfig("is_oobe_enabled=false");
     77 
     78   CallLoadConfig(false /* normal_mode */);
     79   EXPECT_FALSE(hardware_.IsOOBEEnabled());
     80 }
     81 
     82 TEST_F(HardwareChromeOSTest, ReadRootfsIfStatefulNotFound) {
     83   WriteRootfsConfig("is_oobe_enabled=false");
     84 
     85   CallLoadConfig(false /* normal_mode */);
     86   EXPECT_FALSE(hardware_.IsOOBEEnabled());
     87 }
     88 
     89 }  // namespace chromeos_update_engine
     90