blob: 3925236a55ba4d8f1ef7057c8e3d2a379416cde2 [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
Tao Baobdc8c1a2017-03-24 10:39:05 -070033using namespace std::string_literals;
34
Tao Bao10334082016-12-12 17:10:20 -080035static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt";
36static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb";
37static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb";
38static const std::string INIT_SVC_UNCRYPT = "init.svc.uncrypt";
39static constexpr int SOCKET_CONNECTION_MAX_RETRY = 30;
40
41class UncryptTest : public ::testing::Test {
42 protected:
Tianjie Xuca948562017-02-28 12:26:29 -080043 UncryptTest() : has_misc(true) {}
44
45 virtual void SetUp() override {
Tao Bao10334082016-12-12 17:10:20 -080046 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb"));
47 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
48 ASSERT_TRUE(android::base::SetProperty("ctl.stop", "uncrypt"));
49
50 bool success = false;
51 for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
52 std::string setup_bcb = android::base::GetProperty(INIT_SVC_SETUP_BCB, "");
53 std::string clear_bcb = android::base::GetProperty(INIT_SVC_CLEAR_BCB, "");
54 std::string uncrypt = android::base::GetProperty(INIT_SVC_UNCRYPT, "");
55 LOG(INFO) << "setup-bcb: [" << setup_bcb << "] clear-bcb: [" << clear_bcb << "] uncrypt: ["
56 << uncrypt << "]";
57 if (setup_bcb != "running" && clear_bcb != "running" && uncrypt != "running") {
58 success = true;
59 break;
60 }
61 sleep(1);
62 }
63
64 ASSERT_TRUE(success) << "uncrypt service is not available.";
Tianjie Xuca948562017-02-28 12:26:29 -080065
Tao Baoad87d9d2017-04-04 19:59:40 -070066 std::string err;
67 has_misc = !get_bootloader_message_blk_device(&err).empty();
Tao Bao10334082016-12-12 17:10:20 -080068 }
Tianjie Xuca948562017-02-28 12:26:29 -080069
Tao Baobdc8c1a2017-03-24 10:39:05 -070070 void SetupOrClearBcb(bool isSetup, const std::string& message,
71 const std::string& message_in_bcb) const {
72 if (!has_misc) {
73 GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
74 return;
75 }
76
77 // Trigger the setup-bcb service.
78 ASSERT_TRUE(android::base::SetProperty("ctl.start", isSetup ? "setup-bcb" : "clear-bcb"));
79
80 // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
81 sleep(1);
82
83 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<sockaddr*>(&un), sizeof(sockaddr_un)) != 0) {
94 success = true;
95 break;
96 }
97 sleep(1);
98 }
99 ASSERT_TRUE(success);
100
101 if (isSetup) {
102 // Send out the BCB message.
103 int length = static_cast<int>(message.size());
104 int length_out = htonl(length);
105 ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int)))
106 << "Failed to write length: " << strerror(errno);
107 ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length))
108 << "Failed to write message: " << strerror(errno);
109 }
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", isSetup ? "setup-bcb" : "clear-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 if (isSetup) {
130 ASSERT_EQ("boot-recovery", std::string(boot.command));
131 ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
132
133 // The rest of the boot.recovery message should be zero'd out.
134 ASSERT_LE(message_in_bcb.size(), sizeof(boot.recovery));
135 size_t left = sizeof(boot.recovery) - message_in_bcb.size();
136 ASSERT_EQ(std::string(left, '\0'), std::string(&boot.recovery[message_in_bcb.size()], left));
137
138 // Clear the BCB.
139 ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
140 } else {
141 // All the bytes should be cleared.
142 ASSERT_EQ(std::string(sizeof(boot), '\0'),
143 std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
144 }
145 }
146
Tianjie Xuca948562017-02-28 12:26:29 -0800147 bool has_misc;
Tao Bao10334082016-12-12 17:10:20 -0800148};
149
150TEST_F(UncryptTest, setup_bcb) {
Tao Bao10334082016-12-12 17:10:20 -0800151 std::string message = "--update_message=abc value";
152 std::string message_in_bcb = "recovery\n--update_message=abc value\n";
Tao Baobdc8c1a2017-03-24 10:39:05 -0700153 SetupOrClearBcb(true, message, message_in_bcb);
Tao Bao10334082016-12-12 17:10:20 -0800154}
155
156TEST_F(UncryptTest, clear_bcb) {
Tao Baobdc8c1a2017-03-24 10:39:05 -0700157 SetupOrClearBcb(false, "", "");
158}
Tianjie Xuca948562017-02-28 12:26:29 -0800159
Tao Baobdc8c1a2017-03-24 10:39:05 -0700160TEST_F(UncryptTest, setup_bcb_wipe_ab) {
161 TemporaryFile wipe_package;
162 ASSERT_TRUE(android::base::WriteStringToFile(std::string(345, 'a'), wipe_package.path));
Tao Bao10334082016-12-12 17:10:20 -0800163
Tao Baobdc8c1a2017-03-24 10:39:05 -0700164 // It's expected to store a wipe package in /misc, with the package size passed to recovery.
165 std::string message =
166 "--wipe_ab\n--wipe_package="s + wipe_package.path + "\n--reason=wipePackage"s;
167 std::string message_in_bcb =
168 "recovery\n--wipe_ab\n--wipe_package_size=345\n--reason=wipePackage\n";
169 SetupOrClearBcb(true, message, message_in_bcb);
Tao Bao10334082016-12-12 17:10:20 -0800170}