Merge "tests: Add a test for --wipe_ab into UncryptTest."

am: 7b0cda5b37

Change-Id: Iadc316dd0503240b3d8bbdf30334c9da92ac238f
This commit is contained in:
Tao Bao 2017-03-28 22:22:12 +00:00 committed by android-build-merger
commit c6df7d2937

View file

@ -25,12 +25,15 @@
#include <android-base/file.h> #include <android-base/file.h>
#include <android-base/logging.h> #include <android-base/logging.h>
#include <android-base/properties.h> #include <android-base/properties.h>
#include <android-base/test_utils.h>
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
#include <bootloader_message/bootloader_message.h> #include <bootloader_message/bootloader_message.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include "common/component_test_util.h" #include "common/component_test_util.h"
using namespace std::string_literals;
static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt"; static const std::string UNCRYPT_SOCKET = "/dev/socket/uncrypt";
static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb"; static const std::string INIT_SVC_SETUP_BCB = "init.svc.setup-bcb";
static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb"; static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb";
@ -65,22 +68,20 @@ class UncryptTest : public ::testing::Test {
has_misc = parse_misc(); has_misc = parse_misc();
} }
bool has_misc; void SetupOrClearBcb(bool isSetup, const std::string& message,
}; const std::string& message_in_bcb) const {
TEST_F(UncryptTest, setup_bcb) {
if (!has_misc) { if (!has_misc) {
GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device."; GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
return; return;
} }
// Trigger the setup-bcb service. // Trigger the setup-bcb service.
ASSERT_TRUE(android::base::SetProperty("ctl.start", "setup-bcb")); ASSERT_TRUE(android::base::SetProperty("ctl.start", isSetup ? "setup-bcb" : "clear-bcb"));
// Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected"). // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
sleep(1); sleep(1);
struct sockaddr_un un = {}; sockaddr_un un = {};
un.sun_family = AF_UNIX; un.sun_family = AF_UNIX;
strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path)); strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
@ -90,7 +91,7 @@ TEST_F(UncryptTest, setup_bcb) {
// Connect to the uncrypt socket. // Connect to the uncrypt socket.
bool success = false; bool success = false;
for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) { for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
if (connect(sockfd, reinterpret_cast<struct sockaddr*>(&un), sizeof(struct sockaddr_un)) != 0) { if (connect(sockfd, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un)) != 0) {
success = true; success = true;
break; break;
} }
@ -98,15 +99,15 @@ TEST_F(UncryptTest, setup_bcb) {
} }
ASSERT_TRUE(success); ASSERT_TRUE(success);
if (isSetup) {
// Send out the BCB message. // Send out the BCB message.
std::string message = "--update_message=abc value";
std::string message_in_bcb = "recovery\n--update_message=abc value\n";
int length = static_cast<int>(message.size()); int length = static_cast<int>(message.size());
int length_out = htonl(length); int length_out = htonl(length);
ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int))) ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int)))
<< "Failed to write length: " << strerror(errno); << "Failed to write length: " << strerror(errno);
ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length)) ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length))
<< "Failed to write message: " << strerror(errno); << "Failed to write message: " << strerror(errno);
}
// Check the status code from uncrypt. // Check the status code from uncrypt.
int status; int status;
@ -119,13 +120,14 @@ TEST_F(UncryptTest, setup_bcb) {
ASSERT_EQ(0, close(sockfd)); ASSERT_EQ(0, close(sockfd));
ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb")); ASSERT_TRUE(android::base::SetProperty("ctl.stop", isSetup ? "setup-bcb" : "clear-bcb"));
// Verify the message by reading from BCB directly. // Verify the message by reading from BCB directly.
bootloader_message boot; bootloader_message boot;
std::string err; std::string err;
ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err; ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
if (isSetup) {
ASSERT_EQ("boot-recovery", std::string(boot.command)); ASSERT_EQ("boot-recovery", std::string(boot.command));
ASSERT_EQ(message_in_bcb, std::string(boot.recovery)); ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
@ -136,57 +138,34 @@ TEST_F(UncryptTest, setup_bcb) {
// Clear the BCB. // Clear the BCB.
ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err; ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
} } else {
TEST_F(UncryptTest, clear_bcb) {
if (!has_misc) {
GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
return;
}
// Trigger the clear-bcb service.
ASSERT_TRUE(android::base::SetProperty("ctl.start", "clear-bcb"));
// Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
sleep(1);
struct sockaddr_un un = {};
un.sun_family = AF_UNIX;
strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
ASSERT_NE(-1, sockfd);
// Connect to the uncrypt socket.
bool success = false;
for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
if (connect(sockfd, reinterpret_cast<struct sockaddr*>(&un), sizeof(struct sockaddr_un)) != 0) {
success = true;
break;
}
sleep(1);
}
ASSERT_TRUE(success);
// Check the status code from uncrypt.
int status;
ASSERT_TRUE(android::base::ReadFully(sockfd, &status, sizeof(int)));
ASSERT_EQ(100U, ntohl(status));
// Ack having received the status code.
int code = 0;
ASSERT_TRUE(android::base::WriteFully(sockfd, &code, sizeof(int)));
ASSERT_EQ(0, close(sockfd));
ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
// Verify the content by reading from BCB directly.
bootloader_message boot;
std::string err;
ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
// All the bytes should be cleared. // All the bytes should be cleared.
ASSERT_EQ(std::string(sizeof(boot), '\0'), ASSERT_EQ(std::string(sizeof(boot), '\0'),
std::string(reinterpret_cast<const char*>(&boot), sizeof(boot))); std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
} }
}
bool has_misc;
};
TEST_F(UncryptTest, setup_bcb) {
std::string message = "--update_message=abc value";
std::string message_in_bcb = "recovery\n--update_message=abc value\n";
SetupOrClearBcb(true, message, message_in_bcb);
}
TEST_F(UncryptTest, clear_bcb) {
SetupOrClearBcb(false, "", "");
}
TEST_F(UncryptTest, setup_bcb_wipe_ab) {
TemporaryFile wipe_package;
ASSERT_TRUE(android::base::WriteStringToFile(std::string(345, 'a'), wipe_package.path));
// It's expected to store a wipe package in /misc, with the package size passed to recovery.
std::string message =
"--wipe_ab\n--wipe_package="s + wipe_package.path + "\n--reason=wipePackage"s;
std::string message_in_bcb =
"recovery\n--wipe_ab\n--wipe_package_size=345\n--reason=wipePackage\n";
SetupOrClearBcb(true, message, message_in_bcb);
}