blob: 4f2b8164ff400fd50f226349388823d787ccf10d [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
23#include <string>
24
25#include <android-base/file.h>
26#include <android-base/logging.h>
27#include <android-base/properties.h>
28#include <android-base/unique_fd.h>
29#include <bootloader_message/bootloader_message.h>
30#include <gtest/gtest.h>
31
Tianjie Xuca948562017-02-28 12:26:29 -080032#include "common/component_test_util.h"
33
Tao Bao10334082016-12-12 17:10:20 -080034static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt";
35static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb";
36static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb";
37static const std::string INIT_SVC_UNCRYPT = "init.svc.uncrypt";
38static constexpr int SOCKET_CONNECTION_MAX_RETRY = 30;
39
40class UncryptTest : public ::testing::Test {
41 protected:
Tianjie Xuca948562017-02-28 12:26:29 -080042 UncryptTest() : has_misc(true) {}
43
44 virtual void SetUp() override {
Tao Bao10334082016-12-12 17:10:20 -080045 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb"));
46 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
47 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "uncrypt"));
48
49 bool success = false;
50 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
51 std::string setup_bcb = android::base::GetProperty(INIT_SVC_SETUP_BCB, "");
52 std::string clear_bcb = android::base::GetProperty(INIT_SVC_CLEAR_BCB, "");
53 std::string uncrypt = android::base::GetProperty(INIT_SVC_UNCRYPT, "");
54 LOG(INFO) << "setup-bcb: [" << setup_bcb << "] clear-bcb: [" << clear_bcb << "] uncrypt: ["
55 << uncrypt << "]";
56 if (setup_bcb != "running" && clear_bcb != "running" && uncrypt != "running") {
57 success = true;
58 break;
59 }
60 sleep(1);
61 }
62
63 ASSERT_TRUE(success) << "uncrypt service is not available.";
Tianjie Xuca948562017-02-28 12:26:29 -080064
65 has_misc = parse_misc();
Tao Bao10334082016-12-12 17:10:20 -080066 }
Tianjie Xuca948562017-02-28 12:26:29 -080067
68 bool has_misc;
Tao Bao10334082016-12-12 17:10:20 -080069};
70
71TEST_F(UncryptTest, setup_bcb) {
Tianjie Xuca948562017-02-28 12:26:29 -080072 if (!has_misc) {
73 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
74 return;
75 }
76
Tao Bao10334082016-12-12 17:10:20 -080077 // Trigger the setup-bcb service.
78 ASSERT_TRUE(android::base::SetProperty("ctl.start", "setup-bcb"));
79
80 // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
81 sleep(1);
82
83 struct sockaddr_un un = {};
84 un.sun_family = AF_UNIX;
85 strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
86
87 int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
88 ASSERT_NE(-1, sockfd);
89
90 // Connect to the uncrypt socket.
91 bool success = false;
92 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
93 if (connect(sockfd, reinterpret_cast<struct sockaddr*>(&un), sizeof(struct sockaddr_un)) != 0) {
94 success = true;
95 break;
96 }
97 sleep(1);
98 }
99 ASSERT_TRUE(success);
100
101 // Send out the BCB message.
102 std::string message = "--update_message=abc value";
103 std::string message_in_bcb = "recovery\n--update_message=abc value\n";
104 int length = static_cast<int>(message.size());
105 int length_out = htonl(length);
106 ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int)))
107 << "Failed to write length: " << strerror(errno);
108 ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length))
109 << "Failed to write message: " << strerror(errno);
110
111 // Check the status code from uncrypt.
112 int status;
113 ASSERT_TRUE(android::base::ReadFully(sockfd, &status, sizeof(int)));
114 ASSERT_EQ(100U, ntohl(status));
115
116 // Ack having received the status code.
117 int code = 0;
118 ASSERT_TRUE(android::base::WriteFully(sockfd, &code, sizeof(int)));
119
120 ASSERT_EQ(0, close(sockfd));
121
122 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb"));
123
124 // Verify the message by reading from BCB directly.
125 bootloader_message boot;
126 std::string err;
127 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
128
129 ASSERT_EQ("boot-recovery", std::string(boot.command));
130 ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
131
132 // The rest of the boot.recovery message should be zero'd out.
133 ASSERT_LE(message_in_bcb.size(), sizeof(boot.recovery));
134 size_t left = sizeof(boot.recovery) - message_in_bcb.size();
135 ASSERT_EQ(std::string(left, '\0'), std::string(&boot.recovery[message_in_bcb.size()], left));
136
137 // Clear the BCB.
138 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
139}
140
141TEST_F(UncryptTest, clear_bcb) {
Tianjie Xuca948562017-02-28 12:26:29 -0800142 if (!has_misc) {
143 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
144 return;
145 }
146
Tao Bao10334082016-12-12 17:10:20 -0800147 // Trigger the clear-bcb service.
148 ASSERT_TRUE(android::base::SetProperty("ctl.start", "clear-bcb"));
149
150 // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
151 sleep(1);
152
153 struct sockaddr_un un = {};
154 un.sun_family = AF_UNIX;
155 strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
156
157 int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
158 ASSERT_NE(-1, sockfd);
159
160 // Connect to the uncrypt socket.
161 bool success = false;
162 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
163 if (connect(sockfd, reinterpret_cast<struct sockaddr*>(&un), sizeof(struct sockaddr_un)) != 0) {
164 success = true;
165 break;
166 }
167 sleep(1);
168 }
169 ASSERT_TRUE(success);
170
171 // Check the status code from uncrypt.
172 int status;
173 ASSERT_TRUE(android::base::ReadFully(sockfd, &status, sizeof(int)));
174 ASSERT_EQ(100U, ntohl(status));
175
176 // Ack having received the status code.
177 int code = 0;
178 ASSERT_TRUE(android::base::WriteFully(sockfd, &code, sizeof(int)));
179
180 ASSERT_EQ(0, close(sockfd));
181
182 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
183
184 // Verify the content by reading from BCB directly.
185 bootloader_message boot;
186 std::string err;
187 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
188
189 // All the bytes should be cleared.
190 ASSERT_EQ(std::string(sizeof(boot), '\0'),
191 std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
192}