hardware_chromeos_unittest.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "update_engine/hardware_chromeos.h"
  17. #include <memory>
  18. #include <base/files/file_util.h>
  19. #include <base/files/scoped_temp_dir.h>
  20. #include <gtest/gtest.h>
  21. #include "update_engine/common/constants.h"
  22. #include "update_engine/common/fake_hardware.h"
  23. #include "update_engine/common/test_utils.h"
  24. #include "update_engine/update_manager/umtest_utils.h"
  25. using chromeos_update_engine::test_utils::WriteFileString;
  26. using std::string;
  27. namespace chromeos_update_engine {
  28. class HardwareChromeOSTest : public ::testing::Test {
  29. protected:
  30. void SetUp() override { ASSERT_TRUE(root_dir_.CreateUniqueTempDir()); }
  31. void WriteStatefulConfig(const string& config) {
  32. base::FilePath kFile(root_dir_.GetPath().value() + kStatefulPartition +
  33. "/etc/update_manager.conf");
  34. ASSERT_TRUE(base::CreateDirectory(kFile.DirName()));
  35. ASSERT_TRUE(WriteFileString(kFile.value(), config));
  36. }
  37. void WriteRootfsConfig(const string& config) {
  38. base::FilePath kFile(root_dir_.GetPath().value() +
  39. "/etc/update_manager.conf");
  40. ASSERT_TRUE(base::CreateDirectory(kFile.DirName()));
  41. ASSERT_TRUE(WriteFileString(kFile.value(), config));
  42. }
  43. // Helper method to call HardwareChromeOS::LoadConfig with the test directory.
  44. void CallLoadConfig(bool normal_mode) {
  45. hardware_.LoadConfig(root_dir_.GetPath().value(), normal_mode);
  46. }
  47. HardwareChromeOS hardware_;
  48. base::ScopedTempDir root_dir_;
  49. };
  50. TEST_F(HardwareChromeOSTest, NoFileFoundReturnsDefault) {
  51. CallLoadConfig(true /* normal_mode */);
  52. EXPECT_TRUE(hardware_.IsOOBEEnabled());
  53. }
  54. TEST_F(HardwareChromeOSTest, DontReadStatefulInNormalMode) {
  55. WriteStatefulConfig("is_oobe_enabled=false");
  56. CallLoadConfig(true /* normal_mode */);
  57. EXPECT_TRUE(hardware_.IsOOBEEnabled());
  58. }
  59. TEST_F(HardwareChromeOSTest, ReadStatefulInDevMode) {
  60. WriteRootfsConfig("is_oobe_enabled=true");
  61. // Since the stateful is present, we should read that one.
  62. WriteStatefulConfig("is_oobe_enabled=false");
  63. CallLoadConfig(false /* normal_mode */);
  64. EXPECT_FALSE(hardware_.IsOOBEEnabled());
  65. }
  66. TEST_F(HardwareChromeOSTest, ReadRootfsIfStatefulNotFound) {
  67. WriteRootfsConfig("is_oobe_enabled=false");
  68. CallLoadConfig(false /* normal_mode */);
  69. EXPECT_FALSE(hardware_.IsOOBEEnabled());
  70. }
  71. } // namespace chromeos_update_engine