blob: 5e057e129356fec6501440739a3c9f8086f27656 [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>
Tao Baobdc8c1a2017-03-24 10:39:05 -070028#include <android-base/test_utils.h>
Tao Bao10334082016-12-12 17:10:20 -080029#include <android-base/unique_fd.h>
30#include <bootloader_message/bootloader_message.h>
31#include <gtest/gtest.h>
32
Tianjie Xuca948562017-02-28 12:26:29 -080033#include "common/component_test_util.h"
34
Tao Baobdc8c1a2017-03-24 10:39:05 -070035using namespace std::string_literals;
36
Tao Bao10334082016-12-12 17:10:20 -080037static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt";
38static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb";
39static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb";
40static const std::string INIT_SVC_UNCRYPT = "init.svc.uncrypt";
41static constexpr int SOCKET_CONNECTION_MAX_RETRY = 30;
42
43class UncryptTest : public ::testing::Test {
44 protected:
Tianjie Xuca948562017-02-28 12:26:29 -080045 UncryptTest() : has_misc(true) {}
46
47 virtual void SetUp() override {
Tao Bao10334082016-12-12 17:10:20 -080048 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb"));
49 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
50 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "uncrypt"));
51
52 bool success = false;
53 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
54 std::string setup_bcb = android::base::GetProperty(INIT_SVC_SETUP_BCB, "");
55 std::string clear_bcb = android::base::GetProperty(INIT_SVC_CLEAR_BCB, "");
56 std::string uncrypt = android::base::GetProperty(INIT_SVC_UNCRYPT, "");
57 LOG(INFO) << "setup-bcb: [" << setup_bcb << "] clear-bcb: [" << clear_bcb << "] uncrypt: ["
58 << uncrypt << "]";
59 if (setup_bcb != "running" && clear_bcb != "running" && uncrypt != "running") {
60 success = true;
61 break;
62 }
63 sleep(1);
64 }
65
66 ASSERT_TRUE(success) << "uncrypt service is not available.";
Tianjie Xuca948562017-02-28 12:26:29 -080067
68 has_misc = parse_misc();
Tao Bao10334082016-12-12 17:10:20 -080069 }
Tianjie Xuca948562017-02-28 12:26:29 -080070
Tao Baobdc8c1a2017-03-24 10:39:05 -070071 void SetupOrClearBcb(bool isSetup, const std::string& message,
72 const std::string& message_in_bcb) const {
73 if (!has_misc) {
74 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
75 return;
76 }
77
78 // Trigger the setup-bcb service.
79 ASSERT_TRUE(android::base::SetProperty("ctl.start", isSetup ? "setup-bcb" : "clear-bcb"));
80
81 // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
82 sleep(1);
83
84 sockaddr_un un = {};
85 un.sun_family = AF_UNIX;
86 strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
87
88 int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
89 ASSERT_NE(-1, sockfd);
90
91 // Connect to the uncrypt socket.
92 bool success = false;
93 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
94 if (connect(sockfd, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un)) != 0) {
95 success = true;
96 break;
97 }
98 sleep(1);
99 }
100 ASSERT_TRUE(success);
101
102 if (isSetup) {
103 // Send out the BCB message.
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
112 // Check the status code from uncrypt.
113 int status;
114 ASSERT_TRUE(android::base::ReadFully(sockfd, &status, sizeof(int)));
115 ASSERT_EQ(100U, ntohl(status));
116
117 // Ack having received the status code.
118 int code = 0;
119 ASSERT_TRUE(android::base::WriteFully(sockfd, &code, sizeof(int)));
120
121 ASSERT_EQ(0, close(sockfd));
122
123 ASSERT_TRUE(android::base::SetProperty("ctl.stop", isSetup ? "setup-bcb" : "clear-bcb"));
124
125 // Verify the message by reading from BCB directly.
126 bootloader_message boot;
127 std::string err;
128 ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
129
130 if (isSetup) {
131 ASSERT_EQ("boot-recovery", std::string(boot.command));
132 ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
133
134 // The rest of the boot.recovery message should be zero'd out.
135 ASSERT_LE(message_in_bcb.size(), sizeof(boot.recovery));
136 size_t left = sizeof(boot.recovery) - message_in_bcb.size();
137 ASSERT_EQ(std::string(left, '\0'), std::string(&boot.recovery[message_in_bcb.size()], left));
138
139 // Clear the BCB.
140 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
141 } else {
142 // All the bytes should be cleared.
143 ASSERT_EQ(std::string(sizeof(boot), '\0'),
144 std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
145 }
146 }
147
Tianjie Xuca948562017-02-28 12:26:29 -0800148 bool has_misc;
Tao Bao10334082016-12-12 17:10:20 -0800149};
150
151TEST_F(UncryptTest, setup_bcb) {
Tao Bao10334082016-12-12 17:10:20 -0800152 std::string message = "--update_message=abc value";
153 std::string message_in_bcb = "recovery\n--update_message=abc value\n";
Tao Baobdc8c1a2017-03-24 10:39:05 -0700154 SetupOrClearBcb(true, message, message_in_bcb);
Tao Bao10334082016-12-12 17:10:20 -0800155}
156
157TEST_F(UncryptTest, clear_bcb) {
Tao Baobdc8c1a2017-03-24 10:39:05 -0700158 SetupOrClearBcb(false, "", "");
159}
Tianjie Xuca948562017-02-28 12:26:29 -0800160
Tao Baobdc8c1a2017-03-24 10:39:05 -0700161TEST_F(UncryptTest, setup_bcb_wipe_ab) {
162 TemporaryFile wipe_package;
163 ASSERT_TRUE(android::base::WriteStringToFile(std::string(345, 'a'), wipe_package.path));
Tao Bao10334082016-12-12 17:10:20 -0800164
Tao Baobdc8c1a2017-03-24 10:39:05 -0700165 // It's expected to store a wipe package in /misc, with the package size passed to recovery.
166 std::string message =
167 "--wipe_ab\n--wipe_package="s + wipe_package.path + "\n--reason=wipePackage"s;
168 std::string message_in_bcb =
169 "recovery\n--wipe_ab\n--wipe_package_size=345\n--reason=wipePackage\n";
170 SetupOrClearBcb(true, message, message_in_bcb);
Tao Bao10334082016-12-12 17:10:20 -0800171}