summaryrefslogtreecommitdiffstats
path: root/tests/component
diff options
context:
space:
mode:
Diffstat (limited to 'tests/component')
-rw-r--r--tests/component/uncrypt_test.cpp185
-rw-r--r--tests/component/verifier_test.cpp49
2 files changed, 128 insertions, 106 deletions
diff --git a/tests/component/uncrypt_test.cpp b/tests/component/uncrypt_test.cpp
index 4f2b8164f..5e057e129 100644
--- a/tests/component/uncrypt_test.cpp
+++ b/tests/component/uncrypt_test.cpp
@@ -25,12 +25,15 @@
#include <android-base/file.h>
#include <android-base/logging.h>
#include <android-base/properties.h>
+#include <android-base/test_utils.h>
#include <android-base/unique_fd.h>
#include <bootloader_message/bootloader_message.h>
#include <gtest/gtest.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 INIT_SVC_SETUP_BCB = "init.svc.setup-bcb";
static const std::string INIT_SVC_CLEAR_BCB = "init.svc.clear-bcb";
@@ -65,128 +68,104 @@ class UncryptTest : public ::testing::Test {
has_misc = parse_misc();
}
- bool has_misc;
-};
-
-TEST_F(UncryptTest, setup_bcb) {
- if (!has_misc) {
- GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
- return;
- }
-
- // Trigger the setup-bcb service.
- ASSERT_TRUE(android::base::SetProperty("ctl.start", "setup-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;
+ void SetupOrClearBcb(bool isSetup, const std::string& message,
+ const std::string& message_in_bcb) const {
+ if (!has_misc) {
+ GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
+ return;
}
- sleep(1);
- }
- ASSERT_TRUE(success);
-
- // 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_out = htonl(length);
- ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int)))
- << "Failed to write length: " << strerror(errno);
- ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length))
- << "Failed to write message: " << strerror(errno);
-
- // 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)));
+ // Trigger the setup-bcb service.
+ ASSERT_TRUE(android::base::SetProperty("ctl.start", isSetup ? "setup-bcb" : "clear-bcb"));
- ASSERT_EQ(0, close(sockfd));
+ // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
+ sleep(1);
- ASSERT_TRUE(android::base::SetProperty("ctl.stop", "setup-bcb"));
+ sockaddr_un un = {};
+ un.sun_family = AF_UNIX;
+ strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
- // Verify the message by reading from BCB directly.
- bootloader_message boot;
- std::string err;
- ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
+ int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
+ ASSERT_NE(-1, sockfd);
- ASSERT_EQ("boot-recovery", std::string(boot.command));
- ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
+ // Connect to the uncrypt socket.
+ bool success = false;
+ for (int retry = 0; retry < SOCKET_CONNECTION_MAX_RETRY; retry++) {
+ if (connect(sockfd, reinterpret_cast<sockaddr*>(&un), sizeof(sockaddr_un)) != 0) {
+ success = true;
+ break;
+ }
+ sleep(1);
+ }
+ ASSERT_TRUE(success);
+
+ if (isSetup) {
+ // Send out the BCB message.
+ int length = static_cast<int>(message.size());
+ int length_out = htonl(length);
+ ASSERT_TRUE(android::base::WriteFully(sockfd, &length_out, sizeof(int)))
+ << "Failed to write length: " << strerror(errno);
+ ASSERT_TRUE(android::base::WriteFully(sockfd, message.data(), length))
+ << "Failed to write message: " << strerror(errno);
+ }
- // The rest of the boot.recovery message should be zero'd out.
- ASSERT_LE(message_in_bcb.size(), sizeof(boot.recovery));
- size_t left = sizeof(boot.recovery) - message_in_bcb.size();
- ASSERT_EQ(std::string(left, '\0'), std::string(&boot.recovery[message_in_bcb.size()], left));
+ // Check the status code from uncrypt.
+ int status;
+ ASSERT_TRUE(android::base::ReadFully(sockfd, &status, sizeof(int)));
+ ASSERT_EQ(100U, ntohl(status));
- // Clear the BCB.
- ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
-}
+ // Ack having received the status code.
+ int code = 0;
+ ASSERT_TRUE(android::base::WriteFully(sockfd, &code, sizeof(int)));
-TEST_F(UncryptTest, clear_bcb) {
- if (!has_misc) {
- GTEST_LOG_(INFO) << "Test skipped due to no /misc partition found on the device.";
- return;
- }
+ ASSERT_EQ(0, close(sockfd));
- // Trigger the clear-bcb service.
- ASSERT_TRUE(android::base::SetProperty("ctl.start", "clear-bcb"));
+ ASSERT_TRUE(android::base::SetProperty("ctl.stop", isSetup ? "setup-bcb" : "clear-bcb"));
- // Test tends to be flaky if proceeding immediately ("Transport endpoint is not connected").
- sleep(1);
+ // Verify the message by reading from BCB directly.
+ bootloader_message boot;
+ std::string err;
+ ASSERT_TRUE(read_bootloader_message(&boot, &err)) << "Failed to read BCB: " << err;
- struct sockaddr_un un = {};
- un.sun_family = AF_UNIX;
- strlcpy(un.sun_path, UNCRYPT_SOCKET.c_str(), sizeof(un.sun_path));
+ if (isSetup) {
+ ASSERT_EQ("boot-recovery", std::string(boot.command));
+ ASSERT_EQ(message_in_bcb, std::string(boot.recovery));
- int sockfd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
- ASSERT_NE(-1, sockfd);
+ // The rest of the boot.recovery message should be zero'd out.
+ ASSERT_LE(message_in_bcb.size(), sizeof(boot.recovery));
+ size_t left = sizeof(boot.recovery) - message_in_bcb.size();
+ ASSERT_EQ(std::string(left, '\0'), std::string(&boot.recovery[message_in_bcb.size()], left));
- // 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;
+ // Clear the BCB.
+ ASSERT_TRUE(clear_bootloader_message(&err)) << "Failed to clear BCB: " << err;
+ } else {
+ // All the bytes should be cleared.
+ ASSERT_EQ(std::string(sizeof(boot), '\0'),
+ std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
}
- 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)));
+ bool has_misc;
+};
- ASSERT_EQ(0, close(sockfd));
+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);
+}
- ASSERT_TRUE(android::base::SetProperty("ctl.stop", "clear-bcb"));
+TEST_F(UncryptTest, clear_bcb) {
+ SetupOrClearBcb(false, "", "");
+}
- // 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;
+TEST_F(UncryptTest, setup_bcb_wipe_ab) {
+ TemporaryFile wipe_package;
+ ASSERT_TRUE(android::base::WriteStringToFile(std::string(345, 'a'), wipe_package.path));
- // All the bytes should be cleared.
- ASSERT_EQ(std::string(sizeof(boot), '\0'),
- std::string(reinterpret_cast<const char*>(&boot), sizeof(boot)));
+ // 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);
}
diff --git a/tests/component/verifier_test.cpp b/tests/component/verifier_test.cpp
index 07a8c960f..2cfb6d301 100644
--- a/tests/component/verifier_test.cpp
+++ b/tests/component/verifier_test.cpp
@@ -115,6 +115,51 @@ TEST(VerifierTest, load_keys_invalid_keys) {
ASSERT_FALSE(load_keys(key_file5.path, certs));
}
+TEST(VerifierTest, BadPackage_AlteredFooter) {
+ std::string testkey_v3;
+ ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
+ TemporaryFile key_file1;
+ ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
+ std::vector<Certificate> certs;
+ ASSERT_TRUE(load_keys(key_file1.path, certs));
+
+ std::string package;
+ ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
+ ASSERT_EQ(std::string("\xc0\x06\xff\xff\xd2\x06", 6), package.substr(package.size() - 6, 6));
+
+ // Alter the footer.
+ package[package.size() - 5] = '\x05';
+ ASSERT_EQ(VERIFY_FAILURE,
+ verify_file(reinterpret_cast<const unsigned char*>(package.data()), package.size(),
+ certs));
+}
+
+TEST(VerifierTest, BadPackage_AlteredContent) {
+ std::string testkey_v3;
+ ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
+ TemporaryFile key_file1;
+ ASSERT_TRUE(android::base::WriteStringToFile(testkey_v3, key_file1.path));
+ std::vector<Certificate> certs;
+ ASSERT_TRUE(load_keys(key_file1.path, certs));
+
+ std::string package;
+ ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("otasigned_v3.zip"), &package));
+ ASSERT_GT(package.size(), static_cast<size_t>(100));
+
+ // Alter the content.
+ std::string altered1(package);
+ altered1[50] += 1;
+ ASSERT_EQ(VERIFY_FAILURE,
+ verify_file(reinterpret_cast<const unsigned char*>(altered1.data()), altered1.size(),
+ certs));
+
+ std::string altered2(package);
+ altered2[10] += 1;
+ ASSERT_EQ(VERIFY_FAILURE,
+ verify_file(reinterpret_cast<const unsigned char*>(altered2.data()), altered2.size(),
+ certs));
+}
+
TEST_P(VerifierSuccessTest, VerifySucceed) {
ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
}
@@ -157,6 +202,4 @@ INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
::testing::Values(
std::vector<std::string>({"random.zip", "v1"}),
- std::vector<std::string>({"fake-eocd.zip", "v1"}),
- std::vector<std::string>({"alter-metadata.zip", "v1"}),
- std::vector<std::string>({"alter-footer.zip", "v1"})));
+ std::vector<std::string>({"fake-eocd.zip", "v1"})));