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 <bootloader_message/bootloader_message.h>
24 #include <gtest/gtest.h>
25 
26 #include "misc_writer/misc_writer.h"
27 
28 using namespace std::string_literals;
29 
30 namespace android {
31 namespace hardware {
32 namespace google {
33 namespace pixel {
34 
35 class MiscWriterTest : public ::testing::Test {
36  protected:
TearDown()37   void TearDown() override {
38     // Clear the vendor space.
39     auto zeros = std::string(WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC, 0);
40     std::string err;
41     ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace(zeros.data(), zeros.size(), 0, &err))
42         << err;
43   }
44 
45   void CheckMiscPartitionVendorSpaceContent(size_t offset, const std::string& expected);
46 
47   std::unique_ptr<MiscWriter> misc_writer_;
48 };
49 
CheckMiscPartitionVendorSpaceContent(size_t offset,const std::string & expected)50 void MiscWriterTest::CheckMiscPartitionVendorSpaceContent(size_t offset,
51                                                           const std::string& expected) {
52   ASSERT_TRUE(MiscWriter::OffsetAndSizeInVendorSpace(offset, expected.size()));
53   std::string err;
54   auto misc_blk_device = get_misc_blk_device(&err);
55   ASSERT_FALSE(misc_blk_device.empty());
56   android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
57   ASSERT_NE(-1, fd);
58 
59   std::string content(expected.size(), 0);
60   ASSERT_TRUE(android::base::ReadFullyAtOffset(fd, content.data(), content.size(),
61                                                VENDOR_SPACE_OFFSET_IN_MISC + offset));
62   ASSERT_EQ(expected, content);
63 }
64 
TEST_F(MiscWriterTest,SetClearDarkTheme)65 TEST_F(MiscWriterTest, SetClearDarkTheme) {
66   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetDarkThemeFlag);
67   ASSERT_TRUE(misc_writer_);
68   ASSERT_TRUE(misc_writer_->PerformAction());
69   std::string expected = "theme-dark";
70   CheckMiscPartitionVendorSpaceContent(0, expected);
71 
72   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearDarkThemeFlag);
73   ASSERT_TRUE(misc_writer_->PerformAction());
74   std::string zeros(expected.size(), 0);
75   CheckMiscPartitionVendorSpaceContent(0, zeros);
76 }
77 
TEST_F(MiscWriterTest,SetClearDarkTheme_OffsetOverride)78 TEST_F(MiscWriterTest, SetClearDarkTheme_OffsetOverride) {
79   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetDarkThemeFlag);
80   size_t offset = 12360;
81   ASSERT_TRUE(misc_writer_->PerformAction(offset));
82   std::string expected = "theme-dark";
83   CheckMiscPartitionVendorSpaceContent(offset, expected);
84 
85   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearDarkThemeFlag);
86   ASSERT_TRUE(misc_writer_->PerformAction(offset));
87   std::string zeros(expected.size(), 0);
88   CheckMiscPartitionVendorSpaceContent(offset, zeros);
89 }
90 
TEST_F(MiscWriterTest,SetClearSota)91 TEST_F(MiscWriterTest, SetClearSota) {
92   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetSotaFlag);
93   ASSERT_TRUE(misc_writer_);
94   ASSERT_TRUE(misc_writer_->PerformAction());
95   std::string expected = "enable-sota";
96   CheckMiscPartitionVendorSpaceContent(32, expected);
97 
98   // Test we can write to the override offset.
99   size_t override_offset = 12360;
100   ASSERT_FALSE(misc_writer_->PerformAction(override_offset));
101   CheckMiscPartitionVendorSpaceContent(override_offset, expected);
102 
103   misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearSotaFlag);
104   ASSERT_TRUE(misc_writer_->PerformAction());
105   std::string zeros(expected.size(), 0);
106   CheckMiscPartitionVendorSpaceContent(32, zeros);
107 }
108 
TEST_F(MiscWriterTest,WriteMiscPartitionVendorSpace)109 TEST_F(MiscWriterTest, WriteMiscPartitionVendorSpace) {
110   std::string kTestMessage = "kTestMessage";
111   std::string err;
112   ASSERT_TRUE(
113       MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(), 0, &err));
114 
115   CheckMiscPartitionVendorSpaceContent(0, kTestMessage);
116 
117   // Write with an offset.
118   ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace("\x00\x00", 2, 5, &err));
119   CheckMiscPartitionVendorSpaceContent(0, "kTest\x00\x00ssage"s);
120 
121   // Write with the right size.
122   auto start_offset =
123       WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC - kTestMessage.size();
124   ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(),
125                                                         start_offset, &err));
126 
127   // Out-of-bound write.
128   ASSERT_FALSE(MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(),
129                                                          start_offset + 1, &err));
130 
131   // Message won't fit.
132   std::string long_message(WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC + 1, 'a');
133   ASSERT_FALSE(
134       MiscWriter::WriteMiscPartitionVendorSpace(long_message.data(), long_message.size(), 0, &err));
135 }
136 
137 }  // namespace pixel
138 }  // namespace google
139 }  // namespace hardware
140 }  // namespace android
141