blob: b38bc7134c2d335bb118edf435afc600d6984bd1 [file] [log] [blame]
Tao Bao8b7301b2016-12-14 15:52:34 -08001/*
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
Tianjie Xuca948562017-02-28 12:26:29 -080017#include <string>
18#include <vector>
19
Tao Bao8b7301b2016-12-14 15:52:34 -080020#include <android-base/strings.h>
21#include <bootloader_message/bootloader_message.h>
22#include <gtest/gtest.h>
23
Tao Bao8b7301b2016-12-14 15:52:34 -080024class BootloaderMessageTest : public ::testing::Test {
25 protected:
Tianjie Xuca948562017-02-28 12:26:29 -080026 BootloaderMessageTest() : has_misc(true) {}
27
28 virtual void SetUp() override {
Tao Baoad87d9d2017-04-04 19:59:40 -070029 std::string err;
30 has_misc = !get_bootloader_message_blk_device(&err).empty();
Tianjie Xuca948562017-02-28 12:26:29 -080031 }
32
Tao Bao8b7301b2016-12-14 15:52:34 -080033 virtual void TearDown() override {
34 // Clear the BCB.
Tianjie Xucace7432017-03-03 11:28:49 -080035 if (has_misc) {
36 std::string err;
37 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
38 }
Tao Bao8b7301b2016-12-14 15:52:34 -080039 }
Tianjie Xuca948562017-02-28 12:26:29 -080040
41 bool has_misc;
Tao Bao8b7301b2016-12-14 15:52:34 -080042};
43
44TEST_F(BootloaderMessageTest, clear_bootloader_message) {
Tianjie Xuca948562017-02-28 12:26:29 -080045 if (!has_misc) {
46 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
47 return;
48 }
49
Tao Bao8b7301b2016-12-14 15:52:34 -080050 // Clear the BCB.
51 std::string err;
52 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
53
54 // Verify the content.
55 bootloader_message boot;
56 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
57
58 // All the bytes should be cleared.
59 ASSERT_EQ(std::string(sizeof(boot), '\0'),
60 std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
61}
62
63TEST_F(BootloaderMessageTest, read_and_write_bootloader_message) {
Tianjie Xuca948562017-02-28 12:26:29 -080064 if (!has_misc) {
65 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
66 return;
67 }
68
Tao Bao8b7301b2016-12-14 15:52:34 -080069 // Write the BCB.
70 bootloader_message boot = {};
71 strlcpy(boot.command, "command", sizeof(boot.command));
72 strlcpy(boot.recovery, "message1\nmessage2\n", sizeof(boot.recovery));
73 strlcpy(boot.status, "status1", sizeof(boot.status));
74
75 std::string err;
76 ASSERT_TRUE(write_bootloader_message(boot, &err)) << "Failed to write BCB: " << err;
77
78 // Read and verify.
79 bootloader_message boot_verify;
80 ASSERT_TRUE(read_bootloader_message(&boot_verify, &err)) << "Failed to read BCB: " << err;
81
82 ASSERT_EQ(std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)),
83 std::string(reinterpret_cast<const char*>(&boot_verify), sizeof(boot_verify)));
84}
85
86TEST_F(BootloaderMessageTest, write_bootloader_message_options) {
Tianjie Xuca948562017-02-28 12:26:29 -080087 if (!has_misc) {
88 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
89 return;
90 }
91
Tao Bao8b7301b2016-12-14 15:52:34 -080092 // Write the options to BCB.
93 std::vector<std::string> options = { "option1", "option2" };
94 std::string err;
95 ASSERT_TRUE(write_bootloader_message(options, &err)) << "Failed to write BCB: " << err;
96
97 // Inject some bytes into boot, which should be overwritten while reading.
98 bootloader_message boot;
99 strlcpy(boot.recovery, "random message", sizeof(boot.recovery));
100 strlcpy(boot.reserved, "reserved bytes", sizeof(boot.reserved));
101
102 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
103
104 // Verify that command and recovery fields should be set.
105 ASSERT_EQ("boot-recovery", std::string(boot.command));
106 std::string expected = "recovery\n" + android::base::Join(options, "\n") + "\n";
107 ASSERT_EQ(expected, std::string(boot.recovery));
108
109 // The rest should be cleared.
110 ASSERT_EQ(std::string(sizeof(boot.status), '\0'), std::string(boot.status, sizeof(boot.status)));
111 ASSERT_EQ(std::string(sizeof(boot.stage), '\0'), std::string(boot.stage, sizeof(boot.stage)));
112 ASSERT_EQ(std::string(sizeof(boot.reserved), '\0'),
113 std::string(boot.reserved, sizeof(boot.reserved)));
114}
115
116TEST_F(BootloaderMessageTest, write_bootloader_message_options_empty) {
Tianjie Xuca948562017-02-28 12:26:29 -0800117 if (!has_misc) {
118 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
119 return;
120 }
121
Tao Bao8b7301b2016-12-14 15:52:34 -0800122 // Write empty vector.
123 std::vector<std::string> options;
124 std::string err;
125 ASSERT_TRUE(write_bootloader_message(options, &err)) << "Failed to write BCB: " << err;
126
127 // Read and verify.
128 bootloader_message boot;
129 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
130
131 // command and recovery fields should be set.
132 ASSERT_EQ("boot-recovery", std::string(boot.command));
133 ASSERT_EQ("recovery\n", std::string(boot.recovery));
134
135 // The rest should be cleared.
136 ASSERT_EQ(std::string(sizeof(boot.status), '\0'), std::string(boot.status, sizeof(boot.status)));
137 ASSERT_EQ(std::string(sizeof(boot.stage), '\0'), std::string(boot.stage, sizeof(boot.stage)));
138 ASSERT_EQ(std::string(sizeof(boot.reserved), '\0'),
139 std::string(boot.reserved, sizeof(boot.reserved)));
140}
141
142TEST_F(BootloaderMessageTest, write_bootloader_message_options_long) {
Tianjie Xuca948562017-02-28 12:26:29 -0800143 if (!has_misc) {
144 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
145 return;
146 }
147
Tao Bao8b7301b2016-12-14 15:52:34 -0800148 // Write super long message.
149 std::vector<std::string> options;
150 for (int i = 0; i < 100; i++) {
151 options.push_back("option: " + std::to_string(i));
152 }
153
154 std::string err;
155 ASSERT_TRUE(write_bootloader_message(options, &err)) << "Failed to write BCB: " << err;
156
157 // Read and verify.
158 bootloader_message boot;
159 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
160
161 // Make sure it's long enough.
162 std::string expected = "recovery\n" + android::base::Join(options, "\n") + "\n";
163 ASSERT_GE(expected.size(), sizeof(boot.recovery));
164
165 // command and recovery fields should be set.
166 ASSERT_EQ("boot-recovery", std::string(boot.command));
167 ASSERT_EQ(expected.substr(0, sizeof(boot.recovery) - 1), std::string(boot.recovery));
168 ASSERT_EQ('\0', boot.recovery[sizeof(boot.recovery) - 1]);
169
170 // The rest should be cleared.
171 ASSERT_EQ(std::string(sizeof(boot.status), '\0'), std::string(boot.status, sizeof(boot.status)));
172 ASSERT_EQ(std::string(sizeof(boot.stage), '\0'), std::string(boot.stage, sizeof(boot.stage)));
173 ASSERT_EQ(std::string(sizeof(boot.reserved), '\0'),
174 std::string(boot.reserved, sizeof(boot.reserved)));
175}
Tao Bao2292db82016-12-13 21:53:31 -0800176
177TEST_F(BootloaderMessageTest, update_bootloader_message) {
Tianjie Xuca948562017-02-28 12:26:29 -0800178 if (!has_misc) {
179 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
180 return;
181 }
182
Tao Bao2292db82016-12-13 21:53:31 -0800183 // Inject some bytes into boot, which should be not overwritten later.
184 bootloader_message boot;
185 strlcpy(boot.recovery, "random message", sizeof(boot.recovery));
186 strlcpy(boot.reserved, "reserved bytes", sizeof(boot.reserved));
187 std::string err;
188 ASSERT_TRUE(write_bootloader_message(boot, &err)) << "Failed to write BCB: " << err;
189
190 // Update the BCB message.
191 std::vector<std::string> options = { "option1", "option2" };
192 ASSERT_TRUE(update_bootloader_message(options, &err)) << "Failed to update BCB: " << err;
193
194 bootloader_message boot_verify;
195 ASSERT_TRUE(read_bootloader_message(&boot_verify, &err)) << "Failed to read BCB: " << err;
196
197 // Verify that command and recovery fields should be set.
198 ASSERT_EQ("boot-recovery", std::string(boot_verify.command));
199 std::string expected = "recovery\n" + android::base::Join(options, "\n") + "\n";
200 ASSERT_EQ(expected, std::string(boot_verify.recovery));
201
202 // The rest should be intact.
203 ASSERT_EQ(std::string(boot.status), std::string(boot_verify.status));
204 ASSERT_EQ(std::string(boot.stage), std::string(boot_verify.stage));
205 ASSERT_EQ(std::string(boot.reserved), std::string(boot_verify.reserved));
206}