summaryrefslogtreecommitdiff
path: root/misc_writer/misc_writer_test.cpp
blob: 651f6409f54104f413f677f338a3e92c0edd6626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <memory>
#include <string>
#include <string_view>
#include <vector>

#include <android-base/file.h>
#include <bootloader_message/bootloader_message.h>
#include <gtest/gtest.h>

#include "misc_writer/misc_writer.h"

using namespace std::string_literals;

namespace android {
namespace hardware {
namespace google {
namespace pixel {

class MiscWriterTest : public ::testing::Test {
 protected:
  void TearDown() override {
    // Clear the vendor space.
    auto zeros = std::string(WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC, 0);
    std::string err;
    ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace(zeros.data(), zeros.size(), 0, &err))
        << err;
  }

  void CheckMiscPartitionVendorSpaceContent(size_t offset, const std::string& expected);

  std::unique_ptr<MiscWriter> misc_writer_;
};

void MiscWriterTest::CheckMiscPartitionVendorSpaceContent(size_t offset,
                                                          const std::string& expected) {
  ASSERT_TRUE(MiscWriter::OffsetAndSizeInVendorSpace(offset, expected.size()));
  std::string err;
  auto misc_blk_device = get_misc_blk_device(&err);
  ASSERT_FALSE(misc_blk_device.empty());
  android::base::unique_fd fd(open(misc_blk_device.c_str(), O_RDONLY));
  ASSERT_NE(-1, fd);

  std::string content(expected.size(), 0);
  ASSERT_TRUE(android::base::ReadFullyAtOffset(fd, content.data(), content.size(),
                                               VENDOR_SPACE_OFFSET_IN_MISC + offset));
  ASSERT_EQ(expected, content);
}

TEST_F(MiscWriterTest, SetClearDarkTheme) {
  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetDarkThemeFlag);
  ASSERT_TRUE(misc_writer_);
  ASSERT_TRUE(misc_writer_->PerformAction());
  std::string expected = "theme-dark";
  CheckMiscPartitionVendorSpaceContent(0, expected);

  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearDarkThemeFlag);
  ASSERT_TRUE(misc_writer_->PerformAction());
  std::string zeros(expected.size(), 0);
  CheckMiscPartitionVendorSpaceContent(0, zeros);
}

TEST_F(MiscWriterTest, SetClearDarkTheme_OffsetOverride) {
  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetDarkThemeFlag);
  size_t offset = 12360;
  ASSERT_TRUE(misc_writer_->PerformAction(offset));
  std::string expected = "theme-dark";
  CheckMiscPartitionVendorSpaceContent(offset, expected);

  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearDarkThemeFlag);
  ASSERT_TRUE(misc_writer_->PerformAction(offset));
  std::string zeros(expected.size(), 0);
  CheckMiscPartitionVendorSpaceContent(offset, zeros);
}

TEST_F(MiscWriterTest, SetClearSota) {
  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetSotaFlag);
  ASSERT_TRUE(misc_writer_);
  ASSERT_TRUE(misc_writer_->PerformAction());
  std::string expected = "enable-sota";
  CheckMiscPartitionVendorSpaceContent(32, expected);

  // Test we can write to the override offset.
  size_t override_offset = 12360;
  ASSERT_FALSE(misc_writer_->PerformAction(override_offset));
  CheckMiscPartitionVendorSpaceContent(override_offset, expected);

  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearSotaFlag);
  ASSERT_TRUE(misc_writer_->PerformAction());
  std::string zeros(expected.size(), 0);
  CheckMiscPartitionVendorSpaceContent(32, zeros);
}

TEST_F(MiscWriterTest, SetMaxRamSize) {
  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kSetMaxRamSize, "8192");
  size_t offset = MiscWriter::kMaxRamSizeOffsetInVendorSpace;
  ASSERT_TRUE(misc_writer_->PerformAction(offset));
  std::string expected = std::string(MiscWriter::kMaxRamSize) + "8192";
  CheckMiscPartitionVendorSpaceContent(offset, expected);

  misc_writer_ = std::make_unique<MiscWriter>(MiscWriterActions::kClearMaxRamSize);
  ASSERT_TRUE(misc_writer_->PerformAction(offset));
  std::string zeros(expected.size(), 0);
  CheckMiscPartitionVendorSpaceContent(offset, zeros);
}

TEST_F(MiscWriterTest, WriteMiscPartitionVendorSpace) {
  std::string kTestMessage = "kTestMessage";
  std::string err;
  ASSERT_TRUE(
      MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(), 0, &err));

  CheckMiscPartitionVendorSpaceContent(0, kTestMessage);

  // Write with an offset.
  ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace("\x00\x00", 2, 5, &err));
  CheckMiscPartitionVendorSpaceContent(0, "kTest\x00\x00ssage"s);

  // Write with the right size.
  auto start_offset =
      WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC - kTestMessage.size();
  ASSERT_TRUE(MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(),
                                                        start_offset, &err));

  // Out-of-bound write.
  ASSERT_FALSE(MiscWriter::WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(),
                                                         start_offset + 1, &err));

  // Message won't fit.
  std::string long_message(WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC + 1, 'a');
  ASSERT_FALSE(
      MiscWriter::WriteMiscPartitionVendorSpace(long_message.data(), long_message.size(), 0, &err));
}

}  // namespace pixel
}  // namespace google
}  // namespace hardware
}  // namespace android