blob: 95d875e69ff536ce142974f45965b24ebcd8a709 [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>
Tao Bao7ae01692019-05-16 14:42:42 -070018#include <string_view>
Tianjie Xuca948562017-02-28 12:26:29 -080019#include <vector>
20
Mark Salyzyn8b54bc52018-11-14 15:18:30 -080021#include <android-base/file.h>
Tao Bao8b7301b2016-12-14 15:52:34 -080022#include <android-base/strings.h>
23#include <bootloader_message/bootloader_message.h>
24#include <gtest/gtest.h>
25
Tao Bao7ae01692019-05-16 14:42:42 -070026using namespace std::string_literals;
27
28extern void SetMiscBlockDeviceForTest(std::string_view misc_device);
29
Tianjie Xua88cc542017-10-25 13:16:54 -070030TEST(BootloaderMessageTest, read_and_write_bootloader_message) {
31 TemporaryFile temp_misc;
Tianjie Xuca948562017-02-28 12:26:29 -080032
Tao Bao8b7301b2016-12-14 15:52:34 -080033 // Write the BCB.
34 bootloader_message boot = {};
35 strlcpy(boot.command, "command", sizeof(boot.command));
36 strlcpy(boot.recovery, "message1\nmessage2\n", sizeof(boot.recovery));
37 strlcpy(boot.status, "status1", sizeof(boot.status));
38
39 std::string err;
Tianjie Xua88cc542017-10-25 13:16:54 -070040 ASSERT_TRUE(write_bootloader_message_to(boot, temp_misc.path, &err))
41 << "Failed to write BCB: " << err;
Tao Bao8b7301b2016-12-14 15:52:34 -080042
43 // Read and verify.
44 bootloader_message boot_verify;
Tianjie Xua88cc542017-10-25 13:16:54 -070045 ASSERT_TRUE(read_bootloader_message_from(&boot_verify, temp_misc.path, &err))
46 << "Failed to read BCB: " << err;
Tao Bao8b7301b2016-12-14 15:52:34 -080047
48 ASSERT_EQ(std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)),
49 std::string(reinterpret_cast<const char*>(&boot_verify), sizeof(boot_verify)));
50}
51
Tianjie Xua88cc542017-10-25 13:16:54 -070052TEST(BootloaderMessageTest, update_bootloader_message_in_struct) {
Tao Bao8b7301b2016-12-14 15:52:34 -080053 // Write the options to BCB.
54 std::vector<std::string> options = { "option1", "option2" };
Tao Bao8b7301b2016-12-14 15:52:34 -080055
Tianjie Xua88cc542017-10-25 13:16:54 -070056 bootloader_message boot = {};
57 // Inject some bytes into boot.
Tao Bao8b7301b2016-12-14 15:52:34 -080058 strlcpy(boot.recovery, "random message", sizeof(boot.recovery));
Tianjie Xua88cc542017-10-25 13:16:54 -070059 strlcpy(boot.status, "status bytes", sizeof(boot.status));
60 strlcpy(boot.stage, "stage bytes", sizeof(boot.stage));
Tao Bao8b7301b2016-12-14 15:52:34 -080061 strlcpy(boot.reserved, "reserved bytes", sizeof(boot.reserved));
62
Tianjie Xua88cc542017-10-25 13:16:54 -070063 ASSERT_TRUE(update_bootloader_message_in_struct(&boot, options));
Tao Bao8b7301b2016-12-14 15:52:34 -080064
65 // Verify that command and recovery fields should be set.
66 ASSERT_EQ("boot-recovery", std::string(boot.command));
67 std::string expected = "recovery\n" + android::base::Join(options, "\n") + "\n";
68 ASSERT_EQ(expected, std::string(boot.recovery));
69
Tianjie Xua88cc542017-10-25 13:16:54 -070070 // The rest should be intact.
71 ASSERT_EQ("status bytes", std::string(boot.status));
72 ASSERT_EQ("stage bytes", std::string(boot.stage));
73 ASSERT_EQ("reserved bytes", std::string(boot.reserved));
Tao Bao8b7301b2016-12-14 15:52:34 -080074}
75
Tianjie Xua88cc542017-10-25 13:16:54 -070076TEST(BootloaderMessageTest, update_bootloader_message_recovery_options_empty) {
Tao Bao8b7301b2016-12-14 15:52:34 -080077 // Write empty vector.
78 std::vector<std::string> options;
Tao Bao8b7301b2016-12-14 15:52:34 -080079
80 // Read and verify.
Tianjie Xua88cc542017-10-25 13:16:54 -070081 bootloader_message boot = {};
82 ASSERT_TRUE(update_bootloader_message_in_struct(&boot, options));
Tao Bao8b7301b2016-12-14 15:52:34 -080083
84 // command and recovery fields should be set.
85 ASSERT_EQ("boot-recovery", std::string(boot.command));
86 ASSERT_EQ("recovery\n", std::string(boot.recovery));
87
Tianjie Xua88cc542017-10-25 13:16:54 -070088 // The rest should be empty.
Tao Bao8b7301b2016-12-14 15:52:34 -080089 ASSERT_EQ(std::string(sizeof(boot.status), '\0'), std::string(boot.status, sizeof(boot.status)));
90 ASSERT_EQ(std::string(sizeof(boot.stage), '\0'), std::string(boot.stage, sizeof(boot.stage)));
91 ASSERT_EQ(std::string(sizeof(boot.reserved), '\0'),
92 std::string(boot.reserved, sizeof(boot.reserved)));
93}
94
Tianjie Xua88cc542017-10-25 13:16:54 -070095TEST(BootloaderMessageTest, update_bootloader_message_recovery_options_long) {
Tao Bao8b7301b2016-12-14 15:52:34 -080096 // Write super long message.
97 std::vector<std::string> options;
98 for (int i = 0; i < 100; i++) {
99 options.push_back("option: " + std::to_string(i));
100 }
101
Tao Bao8b7301b2016-12-14 15:52:34 -0800102 // Read and verify.
Tianjie Xua88cc542017-10-25 13:16:54 -0700103 bootloader_message boot = {};
104 ASSERT_TRUE(update_bootloader_message_in_struct(&boot, options));
Tao Bao8b7301b2016-12-14 15:52:34 -0800105
106 // Make sure it's long enough.
107 std::string expected = "recovery\n" + android::base::Join(options, "\n") + "\n";
108 ASSERT_GE(expected.size(), sizeof(boot.recovery));
109
110 // command and recovery fields should be set.
111 ASSERT_EQ("boot-recovery", std::string(boot.command));
112 ASSERT_EQ(expected.substr(0, sizeof(boot.recovery) - 1), std::string(boot.recovery));
113 ASSERT_EQ('\0', boot.recovery[sizeof(boot.recovery) - 1]);
114
Tianjie Xua88cc542017-10-25 13:16:54 -0700115 // The rest should be empty.
Tao Bao8b7301b2016-12-14 15:52:34 -0800116 ASSERT_EQ(std::string(sizeof(boot.status), '\0'), std::string(boot.status, sizeof(boot.status)));
117 ASSERT_EQ(std::string(sizeof(boot.stage), '\0'), std::string(boot.stage, sizeof(boot.stage)));
118 ASSERT_EQ(std::string(sizeof(boot.reserved), '\0'),
119 std::string(boot.reserved, sizeof(boot.reserved)));
120}
Tao Bao2292db82016-12-13 21:53:31 -0800121
Tao Bao7ae01692019-05-16 14:42:42 -0700122TEST(BootloaderMessageTest, WriteMiscPartitionVendorSpace) {
123 TemporaryFile temp_misc;
124 ASSERT_TRUE(android::base::WriteStringToFile(std::string(4096, '\x00'), temp_misc.path));
125 SetMiscBlockDeviceForTest(temp_misc.path);
126
127 constexpr std::string_view kTestMessage = "kTestMessage";
128 std::string err;
129 ASSERT_TRUE(WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(), 0, &err));
130
131 std::string message;
132 message.resize(kTestMessage.size());
133 ASSERT_TRUE(ReadMiscPartitionVendorSpace(message.data(), message.size(), 0, &err));
134 ASSERT_EQ(kTestMessage, message);
135
136 // Write with an offset.
137 ASSERT_TRUE(WriteMiscPartitionVendorSpace("\x00\x00", 2, 5, &err));
138 ASSERT_TRUE(ReadMiscPartitionVendorSpace(message.data(), message.size(), 0, &err));
139 ASSERT_EQ("kTest\x00\x00ssage"s, message);
140
141 // Write with the right size.
142 auto start_offset =
143 WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC - kTestMessage.size();
144 ASSERT_TRUE(
145 WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(), start_offset, &err));
146
147 // Out-of-bound write.
148 ASSERT_FALSE(WriteMiscPartitionVendorSpace(kTestMessage.data(), kTestMessage.size(),
149 start_offset + 1, &err));
150
151 // Message won't fit.
152 std::string long_message(WIPE_PACKAGE_OFFSET_IN_MISC - VENDOR_SPACE_OFFSET_IN_MISC + 1, 'a');
153 ASSERT_FALSE(WriteMiscPartitionVendorSpace(long_message.data(), long_message.size(), 0, &err));
154}