blob: 55baca2e3551f26c97f2cb8c9fdabad1ec837a35 [file] [log] [blame]
Tao Bao10334082016-12-12 17:10:20 -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
17#include <arpa/inet.h>
18#include <sys/socket.h>
19#include <sys/types.h>
20#include <sys/un.h>
21#include <unistd.h>
22
Tianjie Xua88cc542017-10-25 13:16:54 -070023#include <algorithm>
Tao Bao10334082016-12-12 17:10:20 -080024#include <string>
25
26#include <android-base/file.h>
27#include <android-base/logging.h>
28#include <android-base/properties.h>
Tao Baobdc8c1a2017-03-24 10:39:05 -070029#include <android-base/test_utils.h>
Tao Bao10334082016-12-12 17:10:20 -080030#include <android-base/unique_fd.h>
31#include <bootloader_message/bootloader_message.h>
32#include <gtest/gtest.h>
33
Tao Baobdc8c1a2017-03-24 10:39:05 -070034using namespace std::string_literals;
35
Tao Bao10334082016-12-12 17:10:20 -080036static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt";
37static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb";
38static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb";
39static const std::string INIT_SVC_UNCRYPT = "init.svc.uncrypt";
40static constexpr int SOCKET_CONNECTION_MAX_RETRY = 30;
41
Tianjie Xua88cc542017-10-25 13:16:54 -070042static void StopService() {
43 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb"));
44 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
45 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "uncrypt"));
46
47 bool success = false;
48 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
49 std::string setup_bcb = android::base::GetProperty(INIT_SVC_SETUP_BCB, "");
50 std::string clear_bcb = android::base::GetProperty(INIT_SVC_CLEAR_BCB, "");
51 std::string uncrypt = android::base::GetProperty(INIT_SVC_UNCRYPT, "");
52 GTEST_LOG_(INFO) << "setup-bcb: [" << setup_bcb << "] clear-bcb: [" << clear_bcb
53 << "] uncrypt: [" << uncrypt << "]";
54 if (setup_bcb != "running" && clear_bcb != "running" && uncrypt != "running") {
55 success = true;
56 break;
57 }
58 sleep(1);
59 }
60
61 ASSERT_TRUE(success) << "uncrypt service is not available.";
62}
63
Tao Bao10334082016-12-12 17:10:20 -080064class UncryptTest : public ::testing::Test {
65 protected:
Tianjie Xuca948562017-02-28 12:26:29 -080066 UncryptTest() : has_misc(true) {}
67
Tianjie Xua88cc542017-10-25 13:16:54 -070068 void SetUp() override {
Tao Baoad87d9d2017-04-04 19:59:40 -070069 std::string err;
70 has_misc = !get_bootloader_message_blk_device(&err).empty();
Tao Bao10334082016-12-12 17:10:20 -080071 }
Tianjie Xuca948562017-02-28 12:26:29 -080072
Tianjie Xua88cc542017-10-25 13:16:54 -070073 void TearDown() override {
74 // Clear the BCB.
75 if (has_misc) {
76 std::string err;
77 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
78 }
79 }
80
Tao Baobdc8c1a2017-03-24 10:39:05 -070081 void SetupOrClearBcb(bool isSetup, const std::string& message,
82 const std::string& message_in_bcb) const {
Tianjie Xua88cc542017-10-25 13:16:54 -070083 // Restart the setup-bcb service.
84 StopService();
Tao Baobdc8c1a2017-03-24 10:39:05 -070085 ASSERT_TRUE(android::base::SetProperty("ctl.start", isSetup ? "setup-bcb" : "clear-bcb"));
86
87 // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
88 sleep(1);
89
90 sockaddr_un un = {};
91 un.sun_family = AF_UNIX;
92 strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
93
94 int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
95 ASSERT_NE(-1, sockfd);
96
97 // Connect to the uncrypt socket.
98 bool success = false;
99 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
100 if (connect(sockfd, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un)) != 0) {
101 success = true;
102 break;
103 }
104 sleep(1);
105 }
106 ASSERT_TRUE(success);
107
108 if (isSetup) {
109 // Send out the BCB message.
110 int length = static_cast<int>(message.size());
111 int length_out = htonl(length);
112 ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int)))
113 << "Failed to write length: " << strerror(errno);
114 ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length))
115 << "Failed to write message: " << strerror(errno);
116 }
117
118 // Check the status code from uncrypt.
119 int status;
120 ASSERT_TRUE(android::base::ReadFully(sockfd, &status, sizeof(int)));
121 ASSERT_EQ(100U, ntohl(status));
122
123 // Ack having received the status code.
124 int code = 0;
125 ASSERT_TRUE(android::base::WriteFully(sockfd, &code, sizeof(int)));
126
127 ASSERT_EQ(0, close(sockfd));
128
129 ASSERT_TRUE(android::base::SetProperty("ctl.stop", isSetup ? "setup-bcb" : "clear-bcb"));
130
131 // Verify the message by reading from BCB directly.
132 bootloader_message boot;
133 std::string err;
134 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
135
136 if (isSetup) {
137 ASSERT_EQ("boot-recovery", std::string(boot.command));
138 ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
139
140 // The rest of the boot.recovery message should be zero'd out.
141 ASSERT_LE(message_in_bcb.size(), sizeof(boot.recovery));
142 size_t left = sizeof(boot.recovery) - message_in_bcb.size();
143 ASSERT_EQ(std::string(left, '\0'), std::string(&boot.recovery[message_in_bcb.size()], left));
144
145 // Clear the BCB.
146 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
147 } else {
148 // All the bytes should be cleared.
149 ASSERT_EQ(std::string(sizeof(boot), '\0'),
150 std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
151 }
152 }
153
Tianjie Xua88cc542017-10-25 13:16:54 -0700154 void VerifyBootloaderMessage(const std::string& expected) {
155 std::string err;
156 bootloader_message boot;
157 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
158
159 // Check that we have all the expected bytes.
160 ASSERT_EQ(expected, std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
161 }
162
Tianjie Xuca948562017-02-28 12:26:29 -0800163 bool has_misc;
Tao Bao10334082016-12-12 17:10:20 -0800164};
165
166TEST_F(UncryptTest, setup_bcb) {
Tianjie Xua88cc542017-10-25 13:16:54 -0700167 if (!has_misc) {
168 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
169 return;
170 }
171
172 std::string random_data;
173 random_data.reserve(sizeof(bootloader_message));
174 generate_n(back_inserter(random_data), sizeof(bootloader_message), []() { return rand() % 128; });
175
176 bootloader_message boot;
177 memcpy(&boot, random_data.c_str(), random_data.size());
178
179 std::string err;
180 ASSERT_TRUE(write_bootloader_message(boot, &err)) << "Failed to write BCB: " << err;
181 VerifyBootloaderMessage(random_data);
182
183 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
184 VerifyBootloaderMessage(std::string(sizeof(bootloader_message), '\0'));
185
Tao Bao10334082016-12-12 17:10:20 -0800186 std::string message = "--update_message=abc value";
187 std::string message_in_bcb = "recovery\n--update_message=abc value\n";
Tao Baobdc8c1a2017-03-24 10:39:05 -0700188 SetupOrClearBcb(true, message, message_in_bcb);
Tao Bao10334082016-12-12 17:10:20 -0800189
Tao Baobdc8c1a2017-03-24 10:39:05 -0700190 SetupOrClearBcb(false, "", "");
Tianjie Xuca948562017-02-28 12:26:29 -0800191
Tao Baobdc8c1a2017-03-24 10:39:05 -0700192 TemporaryFile wipe_package;
193 ASSERT_TRUE(android::base::WriteStringToFile(std::string(345, 'a'), wipe_package.path));
Tao Bao10334082016-12-12 17:10:20 -0800194
Tao Baobdc8c1a2017-03-24 10:39:05 -0700195 // It's expected to store a wipe package in /misc, with the package size passed to recovery.
Tianjie Xua88cc542017-10-25 13:16:54 -0700196 message = "--wipe_ab\n--wipe_package="s + wipe_package.path + "\n--reason=wipePackage"s;
197 message_in_bcb = "recovery\n--wipe_ab\n--wipe_package_size=345\n--reason=wipePackage\n";
Tao Baobdc8c1a2017-03-24 10:39:05 -0700198 SetupOrClearBcb(true, message, message_in_bcb);
Tao Bao10334082016-12-12 17:10:20 -0800199}