Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 1 | /* |
| 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 Bao | bdc8c1a | 2017-03-24 10:39:05 -0700 | [diff] [blame] | 28 | #include <android-base/test_utils.h> |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 29 | #include <android-base/unique_fd.h> |
| 30 | #include <bootloader_message/bootloader_message.h> |
| 31 | #include <gtest/gtest.h> |
| 32 | |
Tianjie Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 33 | #include "common/component_test_util.h" |
| 34 | |
Tao Bao | bdc8c1a | 2017-03-24 10:39:05 -0700 | [diff] [blame] | 35 | using namespace std::string_literals; |
| 36 | |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 37 | static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt"; |
| 38 | static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb"; |
| 39 | static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb"; |
| 40 | static const std::string INIT_SVC_UNCRYPT = "init.svc.uncrypt"; |
| 41 | static constexpr int SOCKET_CONNECTION_MAX_RETRY = 30; |
| 42 | |
| 43 | class UncryptTest : public ::testing::Test { |
| 44 | protected: |
Tianjie Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 45 | UncryptTest() : has_misc(true) {} |
| 46 | |
| 47 | virtual void SetUp() override { |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 48 | 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 Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 67 | |
| 68 | has_misc = parse_misc(); |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 69 | } |
Tianjie Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 70 | |
Tao Bao | bdc8c1a | 2017-03-24 10:39:05 -0700 | [diff] [blame] | 71 | 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 Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 148 | bool has_misc; |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | TEST_F(UncryptTest, setup_bcb) { |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 152 | std::string message = "--update_message=abc value"; |
| 153 | std::string message_in_bcb = "recovery\n--update_message=abc value\n"; |
Tao Bao | bdc8c1a | 2017-03-24 10:39:05 -0700 | [diff] [blame] | 154 | SetupOrClearBcb(true, message, message_in_bcb); |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | TEST_F(UncryptTest, clear_bcb) { |
Tao Bao | bdc8c1a | 2017-03-24 10:39:05 -0700 | [diff] [blame] | 158 | SetupOrClearBcb(false, "", ""); |
| 159 | } |
Tianjie Xu | ca94856 | 2017-02-28 12:26:29 -0800 | [diff] [blame] | 160 | |
Tao Bao | bdc8c1a | 2017-03-24 10:39:05 -0700 | [diff] [blame] | 161 | TEST_F(UncryptTest, setup_bcb_wipe_ab) { |
| 162 | TemporaryFile wipe_package; |
| 163 | ASSERT_TRUE(android::base::WriteStringToFile(std::string(345, 'a'), wipe_package.path)); |
Tao Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 164 | |
Tao Bao | bdc8c1a | 2017-03-24 10:39:05 -0700 | [diff] [blame] | 165 | // 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 Bao | 1033408 | 2016-12-12 17:10:20 -0800 | [diff] [blame] | 171 | } |