Skip to content

Commit

Permalink
tests: Construct two bad packages at runtime for VerifierTest.
Browse files Browse the repository at this point in the history
For the BadPackage tests from VerifierTest: one alters the footer, and
the other alters the metadata. Move the two tests to be based on
otasigned_v3.zip (they're based on otasigned_v1.zip previously). Also
construct the testdata files dynamically (to save the space and for
better readability).

Test: recovery_component_test
Change-Id: I7604d563f8b4fa0c55fec8730c063384158e3abc
(cherry picked from commit 217d9f9)
  • Loading branch information
Tao Bao committed Mar 27, 2017
1 parent 055703e commit 7e61c6a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
49 changes: 46 additions & 3 deletions tests/component/verifier_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,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(VerifierTest, BadPackage_SignatureStartOutOfBounds) {
std::string testkey_v3;
ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
Expand Down Expand Up @@ -174,6 +219,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"})));
Binary file removed tests/testdata/alter-footer.zip
Binary file not shown.
Binary file removed tests/testdata/alter-metadata.zip
Binary file not shown.

0 comments on commit 7e61c6a

Please sign in to comment.