image_properties_chromeos_unittest.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/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/test_utils.h"
  23. #include "update_engine/fake_system_state.h"
  24. using chromeos_update_engine::test_utils::WriteFileString;
  25. using std::string;
  26. namespace chromeos_update_engine {
  27. class ImagePropertiesTest : public ::testing::Test {
  28. protected:
  29. void SetUp() override {
  30. // Create a uniquely named test directory.
  31. ASSERT_TRUE(tempdir_.CreateUniqueTempDir());
  32. EXPECT_TRUE(base::CreateDirectory(tempdir_.GetPath().Append("etc")));
  33. EXPECT_TRUE(base::CreateDirectory(base::FilePath(
  34. tempdir_.GetPath().value() + kStatefulPartition + "/etc")));
  35. test::SetImagePropertiesRootPrefix(tempdir_.GetPath().value().c_str());
  36. SetLockDown(false);
  37. }
  38. void SetLockDown(bool locked_down) {
  39. fake_system_state_.fake_hardware()->SetIsOfficialBuild(locked_down);
  40. fake_system_state_.fake_hardware()->SetIsNormalBootMode(locked_down);
  41. }
  42. FakeSystemState fake_system_state_;
  43. base::ScopedTempDir tempdir_;
  44. };
  45. TEST_F(ImagePropertiesTest, SimpleTest) {
  46. ASSERT_TRUE(
  47. WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
  48. "CHROMEOS_RELEASE_BOARD=arm-generic\n"
  49. "CHROMEOS_RELEASE_FOO=bar\n"
  50. "CHROMEOS_RELEASE_VERSION=0.2.2.3\n"
  51. "CHROMEOS_RELEASE_TRACK=dev-channel\n"
  52. "CHROMEOS_AUSERVER=http://www.google.com"));
  53. ImageProperties props = LoadImageProperties(&fake_system_state_);
  54. EXPECT_EQ("arm-generic", props.board);
  55. EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", props.product_id);
  56. EXPECT_EQ("0.2.2.3", props.version);
  57. EXPECT_EQ("dev-channel", props.current_channel);
  58. EXPECT_EQ("http://www.google.com", props.omaha_url);
  59. }
  60. TEST_F(ImagePropertiesTest, AppIDTest) {
  61. ASSERT_TRUE(WriteFileString(
  62. tempdir_.GetPath().Append("etc/lsb-release").value(),
  63. "CHROMEOS_RELEASE_APPID={58c35cef-9d30-476e-9098-ce20377d535d}"));
  64. ImageProperties props = LoadImageProperties(&fake_system_state_);
  65. EXPECT_EQ("{58c35cef-9d30-476e-9098-ce20377d535d}", props.product_id);
  66. }
  67. TEST_F(ImagePropertiesTest, ConfusingReleaseTest) {
  68. ASSERT_TRUE(
  69. WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
  70. "CHROMEOS_RELEASE_FOO=CHROMEOS_RELEASE_VERSION=1.2.3.4\n"
  71. "CHROMEOS_RELEASE_VERSION=0.2.2.3"));
  72. ImageProperties props = LoadImageProperties(&fake_system_state_);
  73. EXPECT_EQ("0.2.2.3", props.version);
  74. }
  75. TEST_F(ImagePropertiesTest, MissingVersionTest) {
  76. ImageProperties props = LoadImageProperties(&fake_system_state_);
  77. EXPECT_EQ("", props.version);
  78. }
  79. TEST_F(ImagePropertiesTest, OverrideTest) {
  80. ASSERT_TRUE(
  81. WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
  82. "CHROMEOS_RELEASE_BOARD=arm-generic\n"
  83. "CHROMEOS_RELEASE_FOO=bar\n"
  84. "CHROMEOS_RELEASE_TRACK=dev-channel\n"
  85. "CHROMEOS_AUSERVER=http://www.google.com"));
  86. ASSERT_TRUE(WriteFileString(
  87. tempdir_.GetPath().value() + kStatefulPartition + "/etc/lsb-release",
  88. "CHROMEOS_RELEASE_BOARD=x86-generic\n"
  89. "CHROMEOS_RELEASE_TRACK=beta-channel\n"
  90. "CHROMEOS_AUSERVER=https://www.google.com"));
  91. ImageProperties props = LoadImageProperties(&fake_system_state_);
  92. EXPECT_EQ("x86-generic", props.board);
  93. EXPECT_EQ("dev-channel", props.current_channel);
  94. EXPECT_EQ("https://www.google.com", props.omaha_url);
  95. MutableImageProperties mutable_props =
  96. LoadMutableImageProperties(&fake_system_state_);
  97. EXPECT_EQ("beta-channel", mutable_props.target_channel);
  98. }
  99. TEST_F(ImagePropertiesTest, OverrideLockDownTest) {
  100. ASSERT_TRUE(
  101. WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
  102. "CHROMEOS_RELEASE_BOARD=arm-generic\n"
  103. "CHROMEOS_RELEASE_FOO=bar\n"
  104. "CHROMEOS_RELEASE_TRACK=dev-channel\n"
  105. "CHROMEOS_AUSERVER=https://www.google.com"));
  106. ASSERT_TRUE(WriteFileString(
  107. tempdir_.GetPath().value() + kStatefulPartition + "/etc/lsb-release",
  108. "CHROMEOS_RELEASE_BOARD=x86-generic\n"
  109. "CHROMEOS_RELEASE_TRACK=stable-channel\n"
  110. "CHROMEOS_AUSERVER=http://www.google.com"));
  111. SetLockDown(true);
  112. ImageProperties props = LoadImageProperties(&fake_system_state_);
  113. EXPECT_EQ("arm-generic", props.board);
  114. EXPECT_EQ("dev-channel", props.current_channel);
  115. EXPECT_EQ("https://www.google.com", props.omaha_url);
  116. MutableImageProperties mutable_props =
  117. LoadMutableImageProperties(&fake_system_state_);
  118. EXPECT_EQ("stable-channel", mutable_props.target_channel);
  119. }
  120. TEST_F(ImagePropertiesTest, BoardAppIdUsedForNonCanaryChannelTest) {
  121. ASSERT_TRUE(
  122. WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
  123. "CHROMEOS_RELEASE_APPID=r\n"
  124. "CHROMEOS_BOARD_APPID=b\n"
  125. "CHROMEOS_CANARY_APPID=c\n"
  126. "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
  127. ImageProperties props = LoadImageProperties(&fake_system_state_);
  128. EXPECT_EQ("stable-channel", props.current_channel);
  129. EXPECT_EQ("b", props.product_id);
  130. }
  131. TEST_F(ImagePropertiesTest, CanaryAppIdUsedForCanaryChannelTest) {
  132. ASSERT_TRUE(
  133. WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
  134. "CHROMEOS_RELEASE_APPID=r\n"
  135. "CHROMEOS_BOARD_APPID=b\n"
  136. "CHROMEOS_CANARY_APPID=c\n"
  137. "CHROMEOS_RELEASE_TRACK=canary-channel\n"));
  138. ImageProperties props = LoadImageProperties(&fake_system_state_);
  139. EXPECT_EQ("canary-channel", props.current_channel);
  140. EXPECT_EQ("c", props.canary_product_id);
  141. }
  142. TEST_F(ImagePropertiesTest, ReleaseAppIdUsedAsDefaultTest) {
  143. ASSERT_TRUE(
  144. WriteFileString(tempdir_.GetPath().Append("etc/lsb-release").value(),
  145. "CHROMEOS_RELEASE_APPID=r\n"
  146. "CHROMEOS_CANARY_APPID=c\n"
  147. "CHROMEOS_RELEASE_TRACK=stable-channel\n"));
  148. ImageProperties props = LoadImageProperties(&fake_system_state_);
  149. EXPECT_EQ("stable-channel", props.current_channel);
  150. EXPECT_EQ("r", props.product_id);
  151. }
  152. } // namespace chromeos_update_engine