image_properties_android_unittest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Copyright (C) 2017 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/image_properties.h"
  17. #include <string>
  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_prefs.h"
  23. #include "update_engine/common/test_utils.h"
  24. #include "update_engine/fake_system_state.h"
  25. using chromeos_update_engine::test_utils::WriteFileString;
  26. using std::string;
  27. namespace chromeos_update_engine {
  28. class ImagePropertiesTest : public ::testing::Test {
  29. protected:
  30. void SetUp() override {
  31. // Create a uniquely named test directory.
  32. ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
  33. osrelease_dir_ = tempdir_.GetPath().Append("etc/os-release.d");
  34. EXPECT_TRUE(base::CreateDirectory(osrelease_dir_));
  35. test::SetImagePropertiesRootPrefix(tempdir_.GetPath().value().c_str());
  36. }
  37. void WriteOsRelease(const string& key, const string& value) {
  38. ASSERT_TRUE(WriteFileString(osrelease_dir_.Append(key).value(), value));
  39. }
  40. void WriteChannel(const string& channel) {
  41. string misc(2080, '\0');
  42. misc += channel;
  43. misc.resize(4096);
  44. ASSERT_TRUE(
  45. WriteFileString(tempdir_.GetPath().Append("misc").value(), misc));
  46. }
  47. FakeSystemState fake_system_state_;
  48. base::ScopedTempDir tempdir_;
  49. base::FilePath osrelease_dir_;
  50. };
  51. TEST_F(ImagePropertiesTest, SimpleTest) {
  52. WriteOsRelease("product_id", "abc");
  53. WriteOsRelease("system_id", "def");
  54. WriteOsRelease("product_version", "1.2.3.4");
  55. WriteOsRelease("system_version", "5.6.7.8");
  56. ImageProperties props = LoadImageProperties(&fake_system_state_);
  57. EXPECT_EQ("abc", props.product_id);
  58. EXPECT_EQ("def", props.system_id);
  59. EXPECT_EQ("1.2.3.4", props.version);
  60. EXPECT_EQ("5.6.7.8", props.system_version);
  61. EXPECT_EQ("stable-channel", props.current_channel);
  62. EXPECT_EQ(constants::kOmahaDefaultProductionURL, props.omaha_url);
  63. }
  64. TEST_F(ImagePropertiesTest, IDPrefixTest) {
  65. WriteOsRelease("product_id", "abc:def");
  66. WriteOsRelease("system_id", "foo:bar");
  67. ImageProperties props = LoadImageProperties(&fake_system_state_);
  68. EXPECT_EQ("abc:def", props.product_id);
  69. EXPECT_EQ("abc:bar", props.system_id);
  70. }
  71. TEST_F(ImagePropertiesTest, IDInvalidPrefixTest) {
  72. WriteOsRelease("product_id", "def");
  73. WriteOsRelease("system_id", "foo:bar");
  74. ImageProperties props = LoadImageProperties(&fake_system_state_);
  75. EXPECT_EQ("def", props.product_id);
  76. EXPECT_EQ("foo:bar", props.system_id);
  77. WriteOsRelease("product_id", "abc:def");
  78. WriteOsRelease("system_id", "bar");
  79. props = LoadImageProperties(&fake_system_state_);
  80. EXPECT_EQ("abc:def", props.product_id);
  81. EXPECT_EQ("bar", props.system_id);
  82. }
  83. TEST_F(ImagePropertiesTest, LoadChannelTest) {
  84. WriteChannel("unittest-channel");
  85. ImageProperties props = LoadImageProperties(&fake_system_state_);
  86. EXPECT_EQ("unittest-channel", props.current_channel);
  87. }
  88. TEST_F(ImagePropertiesTest, DefaultStableChannelTest) {
  89. WriteChannel("");
  90. ImageProperties props = LoadImageProperties(&fake_system_state_);
  91. EXPECT_EQ("stable-channel", props.current_channel);
  92. }
  93. TEST_F(ImagePropertiesTest, StoreLoadMutableChannelTest) {
  94. FakePrefs prefs;
  95. fake_system_state_.set_prefs(&prefs);
  96. WriteChannel("previous-channel");
  97. MutableImageProperties props;
  98. props.target_channel = "new-channel";
  99. EXPECT_TRUE(StoreMutableImageProperties(&fake_system_state_, props));
  100. MutableImageProperties loaded_props =
  101. LoadMutableImageProperties(&fake_system_state_);
  102. EXPECT_EQ(props.target_channel, loaded_props.target_channel);
  103. }
  104. } // namespace chromeos_update_engine