• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
18 #include <string>
19 #include <string_view>
20 #include <vector>
21 
22 #include <android-base/file.h>
23 #include <android-base/properties.h>
24 #include <bootloader_message/bootloader_message.h>
25 #include <gtest/gtest.h>
26 
27 #include "misc_writer/misc_writer.h"
28 
29 using namespace std::string_literals;
30 
31 namespace android {
32 namespace hardware {
33 namespace google {
34 namespace pixel {
35 
36 class MiscWriterTest : public ::testing::Test {
37  protected:
TearDown()38   void TearDown() override {
39     // Clear the vendor space.
40     auto zeros = std::string(WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC, 0);
41     std::string err;
42     ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace(zeros.data(), zeros.size(), 0, &err))
43         << err;
44   }
45 
46   void CheckMiscPartitionVendorSpaceContent(size_t offset, const std::string& expected);
47 
48   std::unique_ptr<MiscWriter> misc_writer_;
49 };
50 
CheckMiscPartitionVendorSpaceContent(size_t offset,const std::string & expected)51 void MiscWriterTest::CheckMiscPartitionVendorSpaceContent(size_t offset,
52                                                           const std::string& expected) {
53   ASSERT_TRUE(MiscWriter::OffsetAndSizeInVendorSpace(offset, expected.size()));
54   std::string err;
55   auto misc_blk_device = get_misc_blk_device(&err);
56   ASSERT_FALSE(misc_blk_device.empty());
57   android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
58   ASSERT_NE(-1, fd);
59 
60   std::string content(expected.size(), 0);
61   ASSERT_TRUE(android::base::ReadFullyAtOffset(fd, content.data(), content.size(),
62                                                VENDOR_SPACE_OFFSET_IN_MISC + offset));
63   ASSERT_EQ(expected, content);
64 }
65 
TEST_F(MiscWriterTest,SetClearDarkTheme)66 TEST_F(MiscWriterTest, SetClearDarkTheme) {
67   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetDarkThemeFlag);
68   ASSERT_TRUE(misc_writer_);
69   ASSERT_TRUE(misc_writer_->PerformAction());
70   std::string expected = "theme-dark";
71   CheckMiscPartitionVendorSpaceContent(0, expected);
72 
73   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearDarkThemeFlag);
74   ASSERT_TRUE(misc_writer_->PerformAction());
75   std::string zeros(expected.size(), 0);
76   CheckMiscPartitionVendorSpaceContent(0, zeros);
77 }
78 
TEST_F(MiscWriterTest,SetClearDarkTheme_OffsetOverride)79 TEST_F(MiscWriterTest, SetClearDarkTheme_OffsetOverride) {
80   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetDarkThemeFlag);
81   size_t offset = 12360;
82   ASSERT_TRUE(misc_writer_->PerformAction(offset));
83   std::string expected = "theme-dark";
84   CheckMiscPartitionVendorSpaceContent(offset, expected);
85 
86   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearDarkThemeFlag);
87   ASSERT_TRUE(misc_writer_->PerformAction(offset));
88   std::string zeros(expected.size(), 0);
89   CheckMiscPartitionVendorSpaceContent(offset, zeros);
90 }
91 
TEST_F(MiscWriterTest,SetClearSota)92 TEST_F(MiscWriterTest, SetClearSota) {
93   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetSotaFlag);
94   ASSERT_TRUE(misc_writer_);
95   ASSERT_TRUE(misc_writer_->PerformAction());
96   std::string expected = "enable-sota";
97   CheckMiscPartitionVendorSpaceContent(32, expected);
98 
99   expected = ::android::base::GetProperty("persist.vendor.nfc.factoryota.state", "");
100   if (expected.size() != 0 && expected.size() <= 40)
101     CheckMiscPartitionVendorSpaceContent(224, expected);
102 
103   // Test we can write to the override offset.
104   size_t override_offset = 12360;
105   ASSERT_FALSE(misc_writer_->PerformAction(override_offset));
106   CheckMiscPartitionVendorSpaceContent(override_offset, expected);
107 
108   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearSotaFlag);
109   ASSERT_TRUE(misc_writer_->PerformAction());
110   std::string zeros(expected.size(), 0);
111   CheckMiscPartitionVendorSpaceContent(32, zeros);
112 }
113 
TEST_F(MiscWriterTest,SetMaxRamSize)114 TEST_F(MiscWriterTest, SetMaxRamSize) {
115   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetMaxRamSize, "8192");
116   size_t offset = MiscWriter::kMaxRamSizeOffsetInVendorSpace;
117   ASSERT_TRUE(misc_writer_->PerformAction(offset));
118   std::string expected = std::string(MiscWriter::kMaxRamSize) + "8192";
119   CheckMiscPartitionVendorSpaceContent(offset, expected);
120 
121   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearMaxRamSize);
122   ASSERT_TRUE(misc_writer_->PerformAction(offset));
123   std::string zeros(expected.size(), 0);
124   CheckMiscPartitionVendorSpaceContent(offset, zeros);
125 }
126 
TEST_F(MiscWriterTest,SetClearDisplayMode)127 TEST_F(MiscWriterTest, SetClearDisplayMode) {
128   std::string displayMode = "1440x3120@60:120";
129   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetDisplayMode, displayMode);
130   ASSERT_TRUE(misc_writer_);
131   ASSERT_TRUE(misc_writer_->PerformAction());
132   std::string expected = "mode=1440x3120@60:120";
133   CheckMiscPartitionVendorSpaceContent(MiscWriter::kDisplayModeOffsetInVendorSpace, expected);
134 
135   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearDisplayMode);
136   ASSERT_TRUE(misc_writer_->PerformAction());
137   std::string zeros(32, 0);
138   CheckMiscPartitionVendorSpaceContent(MiscWriter::kDisplayModeOffsetInVendorSpace, zeros);
139 }
140 
TEST_F(MiscWriterTest,WriteMiscPartitionVendorSpace)141 TEST_F(MiscWriterTest, WriteMiscPartitionVendorSpace) {
142   std::string kTestMessage = "kTestMessage";
143   std::string err;
144   ASSERT_TRUE(
145       MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(), 0, &err));
146 
147   CheckMiscPartitionVendorSpaceContent(0, kTestMessage);
148 
149   // Write with an offset.
150   ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace("\x00\x00", 2, 5, &err));
151   CheckMiscPartitionVendorSpaceContent(0, "kTest\x00\x00ssage"s);
152 
153   // Write with the right size.
154   auto start_offset =
155       WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC - kTestMessage.size();
156   ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(),
157                                                         start_offset, &err));
158 
159   // Out-of-bound write.
160   ASSERT_FALSE(MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(),
161                                                          start_offset + 1, &err));
162 
163   // Message won't fit.
164   std::string long_message(WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC + 1, 'a');
165   ASSERT_FALSE(
166       MiscWriter::WriteMiscPartitionVendorSpace(long_message.data(), long_message.size(), 0, &err));
167 }
168 
169 }  // namespace pixel
170 }  // namespace google
171 }  // namespace hardware
172 }  // namespace android
173