summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Android.mk1
-rw-r--r--tests/component/verifier_test.cpp206
-rw-r--r--tests/unit/asn1_decoder_test.cpp28
3 files changed, 116 insertions, 119 deletions
diff --git a/tests/Android.mk b/tests/Android.mk
index 65f736d13..ff6e14c9b 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -120,7 +120,6 @@ LOCAL_STATIC_LIBRARIES := \
libupdater \
libbootloader_message \
libverifier \
- libminui \
libotautil \
libmounts \
libdivsufsort \
diff --git a/tests/component/verifier_test.cpp b/tests/component/verifier_test.cpp
index b740af96b..07a8c960f 100644
--- a/tests/component/verifier_test.cpp
+++ b/tests/component/verifier_test.cpp
@@ -22,93 +22,34 @@
#include <sys/stat.h>
#include <sys/types.h>
-#include <memory>
#include <string>
#include <vector>
-#include <openssl/sha.h>
-
+#include <android-base/file.h>
#include <android-base/stringprintf.h>
-#include <ziparchive/zip_archive.h>
+#include <android-base/test_utils.h>
-#include "common.h"
#include "common/test_constants.h"
#include "otautil/SysUtil.h"
-#include "ui.h"
#include "verifier.h"
-RecoveryUI* ui = NULL;
-
-class MockUI : public RecoveryUI {
- bool Init(const std::string&) override {
- return true;
- }
- void SetStage(int, int) override {}
- void SetBackground(Icon /*icon*/) override {}
- void SetSystemUpdateText(bool /*security_update*/) override {}
-
- void SetProgressType(ProgressType /*determinate*/) override {}
- void ShowProgress(float /*portion*/, float /*seconds*/) override {}
- void SetProgress(float /*fraction*/) override {}
-
- void ShowText(bool /*visible*/) override {}
- bool IsTextVisible() override {
- return false;
- }
- bool WasTextEverVisible() override {
- return false;
- }
- void Print(const char* fmt, ...) override {
- va_list ap;
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- }
- void PrintOnScreenOnly(const char* fmt, ...) override {
- va_list ap;
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- }
- void ShowFile(const char*) override {}
-
- void StartMenu(const char* const* /*headers*/, const char* const* /*items*/,
- int /*initial_selection*/) override {}
- int SelectMenu(int /*sel*/) override {
- return 0;
- }
- void EndMenu() override {}
-};
-
-void
-ui_print(const char* format, ...) {
- va_list ap;
- va_start(ap, format);
- vfprintf(stdout, format, ap);
- va_end(ap);
-}
-
class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
- public:
- MemMapping memmap;
- std::vector<Certificate> certs;
-
- virtual void SetUp() {
- std::vector<std::string> args = GetParam();
- std::string package = from_testdata_base(args[0]);
- if (sysMapFile(package.c_str(), &memmap) != 0) {
- FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
- }
-
- for (auto it = ++(args.cbegin()); it != args.cend(); ++it) {
- std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
- ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
- }
+ protected:
+ void SetUp() override {
+ std::vector<std::string> args = GetParam();
+ std::string package = from_testdata_base(args[0]);
+ if (sysMapFile(package.c_str(), &memmap) != 0) {
+ FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
}
- static void SetUpTestCase() {
- ui = new MockUI();
+ for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
+ std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
+ ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
}
+ }
+
+ MemMapping memmap;
+ std::vector<Certificate> certs;
};
class VerifierSuccessTest : public VerifierTest {
@@ -117,48 +58,105 @@ class VerifierSuccessTest : public VerifierTest {
class VerifierFailureTest : public VerifierTest {
};
+TEST(VerifierTest, load_keys_multiple_keys) {
+ std::string testkey_v4;
+ ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
+
+ std::string testkey_v3;
+ ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
+
+ std::string keys = testkey_v4 + "," + testkey_v3 + "," + testkey_v4;
+ TemporaryFile key_file1;
+ ASSERT_TRUE(android::base::WriteStringToFile(keys, key_file1.path));
+ std::vector<Certificate> certs;
+ ASSERT_TRUE(load_keys(key_file1.path, certs));
+ ASSERT_EQ(3U, certs.size());
+}
+
+TEST(VerifierTest, load_keys_invalid_keys) {
+ std::vector<Certificate> certs;
+ ASSERT_FALSE(load_keys("/doesntexist", certs));
+
+ // Empty file.
+ TemporaryFile key_file1;
+ ASSERT_FALSE(load_keys(key_file1.path, certs));
+
+ // Invalid contents.
+ ASSERT_TRUE(android::base::WriteStringToFile("invalid", key_file1.path));
+ ASSERT_FALSE(load_keys(key_file1.path, certs));
+
+ std::string testkey_v4;
+ ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
+
+ // Invalid key version: "v4 ..." => "v6 ...".
+ std::string invalid_key2(testkey_v4);
+ invalid_key2[1] = '6';
+ TemporaryFile key_file2;
+ ASSERT_TRUE(android::base::WriteStringToFile(invalid_key2, key_file2.path));
+ ASSERT_FALSE(load_keys(key_file2.path, certs));
+
+ // Invalid key content: inserted extra bytes ",2209831334".
+ std::string invalid_key3(testkey_v4);
+ invalid_key3.insert(invalid_key2.size() - 2, ",2209831334");
+ TemporaryFile key_file3;
+ ASSERT_TRUE(android::base::WriteStringToFile(invalid_key3, key_file3.path));
+ ASSERT_FALSE(load_keys(key_file3.path, certs));
+
+ // Invalid key: the last key must not end with an extra ','.
+ std::string invalid_key4 = testkey_v4 + ",";
+ TemporaryFile key_file4;
+ ASSERT_TRUE(android::base::WriteStringToFile(invalid_key4, key_file4.path));
+ ASSERT_FALSE(load_keys(key_file4.path, certs));
+
+ // Invalid key separator.
+ std::string invalid_key5 = testkey_v4 + ";" + testkey_v4;
+ TemporaryFile key_file5;
+ ASSERT_TRUE(android::base::WriteStringToFile(invalid_key5, key_file5.path));
+ ASSERT_FALSE(load_keys(key_file5.path, certs));
+}
+
TEST_P(VerifierSuccessTest, VerifySucceed) {
- ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS);
+ ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
}
TEST_P(VerifierFailureTest, VerifyFailure) {
- ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE);
+ ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
}
INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
- ::testing::Values(
- std::vector<std::string>({"otasigned_v1.zip", "v1"}),
- std::vector<std::string>({"otasigned_v2.zip", "v2"}),
- std::vector<std::string>({"otasigned_v3.zip", "v3"}),
- std::vector<std::string>({"otasigned_v4.zip", "v4"}),
- std::vector<std::string>({"otasigned_v5.zip", "v5"})));
+ ::testing::Values(
+ std::vector<std::string>({"otasigned_v1.zip", "v1"}),
+ std::vector<std::string>({"otasigned_v2.zip", "v2"}),
+ std::vector<std::string>({"otasigned_v3.zip", "v3"}),
+ std::vector<std::string>({"otasigned_v4.zip", "v4"}),
+ std::vector<std::string>({"otasigned_v5.zip", "v5"})));
INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
- ::testing::Values(
- std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
- std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
- std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
- std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
- std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
+ ::testing::Values(
+ std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
+ std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
+ std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
+ std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
+ std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
- ::testing::Values(
- std::vector<std::string>({"otasigned_v1.zip", "v2"}),
- std::vector<std::string>({"otasigned_v2.zip", "v1"}),
- std::vector<std::string>({"otasigned_v3.zip", "v5"}),
- std::vector<std::string>({"otasigned_v4.zip", "v5"}),
- std::vector<std::string>({"otasigned_v5.zip", "v3"})));
+ ::testing::Values(
+ std::vector<std::string>({"otasigned_v1.zip", "v2"}),
+ std::vector<std::string>({"otasigned_v2.zip", "v1"}),
+ std::vector<std::string>({"otasigned_v3.zip", "v5"}),
+ std::vector<std::string>({"otasigned_v4.zip", "v5"}),
+ std::vector<std::string>({"otasigned_v5.zip", "v3"})));
INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
- ::testing::Values(
- std::vector<std::string>({"otasigned_v1.zip", "v3"}),
- std::vector<std::string>({"otasigned_v2.zip", "v4"}),
- std::vector<std::string>({"otasigned_v3.zip", "v1"}),
- std::vector<std::string>({"otasigned_v4.zip", "v2"})));
+ ::testing::Values(
+ std::vector<std::string>({"otasigned_v1.zip", "v3"}),
+ std::vector<std::string>({"otasigned_v2.zip", "v4"}),
+ std::vector<std::string>({"otasigned_v3.zip", "v1"}),
+ std::vector<std::string>({"otasigned_v4.zip", "v2"})));
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"})));
+ ::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"})));
diff --git a/tests/unit/asn1_decoder_test.cpp b/tests/unit/asn1_decoder_test.cpp
index af96d87d2..997639d8a 100644
--- a/tests/unit/asn1_decoder_test.cpp
+++ b/tests/unit/asn1_decoder_test.cpp
@@ -39,7 +39,7 @@ TEST_F(Asn1DecoderTest, Empty_Failure) {
EXPECT_EQ(NULL, asn1_set_get(ctx));
EXPECT_FALSE(asn1_sequence_next(ctx));
- uint8_t* junk;
+ const uint8_t* junk;
size_t length;
EXPECT_FALSE(asn1_oid_get(ctx, &junk, &length));
EXPECT_FALSE(asn1_octet_string_get(ctx, &junk, &length));
@@ -68,7 +68,7 @@ TEST_F(Asn1DecoderTest, ConstructedGet_TooSmallForChild_Failure) {
asn1_context_t* ptr = asn1_constructed_get(ctx);
ASSERT_NE((asn1_context_t*)NULL, ptr);
EXPECT_EQ(5, asn1_constructed_type(ptr));
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
asn1_context_free(ptr);
@@ -81,7 +81,7 @@ TEST_F(Asn1DecoderTest, ConstructedGet_Success) {
asn1_context_t* ptr = asn1_constructed_get(ctx);
ASSERT_NE((asn1_context_t*)NULL, ptr);
EXPECT_EQ(5, asn1_constructed_type(ptr));
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
EXPECT_EQ(1U, length);
@@ -103,7 +103,7 @@ TEST_F(Asn1DecoderTest, ConstructedSkipAll_Success) {
0x06, 0x01, 0xA5, };
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
ASSERT_TRUE(asn1_constructed_skip_all(ctx));
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
ASSERT_TRUE(asn1_oid_get(ctx, &oid, &length));
EXPECT_EQ(1U, length);
@@ -123,7 +123,7 @@ TEST_F(Asn1DecoderTest, SequenceGet_TooSmallForChild_Failure) {
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
asn1_context_t* ptr = asn1_sequence_get(ctx);
ASSERT_NE((asn1_context_t*)NULL, ptr);
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
asn1_context_free(ptr);
@@ -135,7 +135,7 @@ TEST_F(Asn1DecoderTest, SequenceGet_Success) {
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
asn1_context_t* ptr = asn1_sequence_get(ctx);
ASSERT_NE((asn1_context_t*)NULL, ptr);
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
EXPECT_EQ(1U, length);
@@ -156,7 +156,7 @@ TEST_F(Asn1DecoderTest, SetGet_TooSmallForChild_Failure) {
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
asn1_context_t* ptr = asn1_set_get(ctx);
ASSERT_NE((asn1_context_t*)NULL, ptr);
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
asn1_context_free(ptr);
@@ -168,7 +168,7 @@ TEST_F(Asn1DecoderTest, SetGet_Success) {
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
asn1_context_t* ptr = asn1_set_get(ctx);
ASSERT_NE((asn1_context_t*)NULL, ptr);
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
EXPECT_EQ(1U, length);
@@ -180,7 +180,7 @@ TEST_F(Asn1DecoderTest, SetGet_Success) {
TEST_F(Asn1DecoderTest, OidGet_LengthZero_Failure) {
uint8_t data[] = { 0x06, 0x00, 0x01, };
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
EXPECT_FALSE(asn1_oid_get(ctx, &oid, &length));
asn1_context_free(ctx);
@@ -189,7 +189,7 @@ TEST_F(Asn1DecoderTest, OidGet_LengthZero_Failure) {
TEST_F(Asn1DecoderTest, OidGet_TooSmall_Failure) {
uint8_t data[] = { 0x06, 0x01, };
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
EXPECT_FALSE(asn1_oid_get(ctx, &oid, &length));
asn1_context_free(ctx);
@@ -198,7 +198,7 @@ TEST_F(Asn1DecoderTest, OidGet_TooSmall_Failure) {
TEST_F(Asn1DecoderTest, OidGet_Success) {
uint8_t data[] = { 0x06, 0x01, 0x99, };
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
- uint8_t* oid;
+ const uint8_t* oid;
size_t length;
ASSERT_TRUE(asn1_oid_get(ctx, &oid, &length));
EXPECT_EQ(1U, length);
@@ -209,7 +209,7 @@ TEST_F(Asn1DecoderTest, OidGet_Success) {
TEST_F(Asn1DecoderTest, OctetStringGet_LengthZero_Failure) {
uint8_t data[] = { 0x04, 0x00, 0x55, };
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
- uint8_t* string;
+ const uint8_t* string;
size_t length;
ASSERT_FALSE(asn1_octet_string_get(ctx, &string, &length));
asn1_context_free(ctx);
@@ -218,7 +218,7 @@ TEST_F(Asn1DecoderTest, OctetStringGet_LengthZero_Failure) {
TEST_F(Asn1DecoderTest, OctetStringGet_TooSmall_Failure) {
uint8_t data[] = { 0x04, 0x01, };
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
- uint8_t* string;
+ const uint8_t* string;
size_t length;
ASSERT_FALSE(asn1_octet_string_get(ctx, &string, &length));
asn1_context_free(ctx);
@@ -227,7 +227,7 @@ TEST_F(Asn1DecoderTest, OctetStringGet_TooSmall_Failure) {
TEST_F(Asn1DecoderTest, OctetStringGet_Success) {
uint8_t data[] = { 0x04, 0x01, 0xAA, };
asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
- uint8_t* string;
+ const uint8_t* string;
size_t length;
ASSERT_TRUE(asn1_octet_string_get(ctx, &string, &length));
EXPECT_EQ(1U, length);