From 7b22c92ac1265f439f8e2623c4a1d6b45bbb05f2 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 16 Mar 2017 17:37:38 -0700 Subject: Drop the dependency on 'ui' in verify_file(). verify_file() has a dependency on the global variable of 'ui' for posting the verification progress, which requires the users of libverifier to provide a UI instance. This CL adds an optional argument to verify_file() so that it can post the progress through the provided callback function. As a result, we can drop the MockUI class in verifier_test.cpp. Test: recovery_component_test passes. Test: verify_file() posts progress update when installing an OTA. Change-Id: I8b87d0f0d99777ea755d33d6dbbe2b6d44243bf1 (cherry picked from commit 5e535014dd7961fbf812abeaa27f3339775031f1) --- tests/component/verifier_test.cpp | 151 ++++++++++++-------------------------- 1 file changed, 46 insertions(+), 105 deletions(-) (limited to 'tests') diff --git a/tests/component/verifier_test.cpp b/tests/component/verifier_test.cpp index 4294d90eb..03829f393 100644 --- a/tests/component/verifier_test.cpp +++ b/tests/component/verifier_test.cpp @@ -22,93 +22,34 @@ #include #include -#include #include #include -#include - +#include #include -#include +#include -#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> { - public: - MemMapping memmap; - std::vector certs; - - virtual void SetUp() { - std::vector 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 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 certs; }; class VerifierSuccessTest : public VerifierTest { @@ -118,48 +59,48 @@ class VerifierFailureTest : public VerifierTest { }; 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({"otasigned_v1.zip", "v1"}), - std::vector({"otasigned_v2.zip", "v2"}), - std::vector({"otasigned_v3.zip", "v3"}), - std::vector({"otasigned_v4.zip", "v4"}), - std::vector({"otasigned_v5.zip", "v5"}))); + ::testing::Values( + std::vector({"otasigned_v1.zip", "v1"}), + std::vector({"otasigned_v2.zip", "v2"}), + std::vector({"otasigned_v3.zip", "v3"}), + std::vector({"otasigned_v4.zip", "v4"}), + std::vector({"otasigned_v5.zip", "v5"}))); INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, - ::testing::Values( - std::vector({"otasigned_v1.zip", "v1", "v2"}), - std::vector({"otasigned_v2.zip", "v5", "v2"}), - std::vector({"otasigned_v3.zip", "v5", "v1", "v3"}), - std::vector({"otasigned_v4.zip", "v5", "v1", "v4"}), - std::vector({"otasigned_v5.zip", "v4", "v1", "v5"}))); + ::testing::Values( + std::vector({"otasigned_v1.zip", "v1", "v2"}), + std::vector({"otasigned_v2.zip", "v5", "v2"}), + std::vector({"otasigned_v3.zip", "v5", "v1", "v3"}), + std::vector({"otasigned_v4.zip", "v5", "v1", "v4"}), + std::vector({"otasigned_v5.zip", "v4", "v1", "v5"}))); INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, - ::testing::Values( - std::vector({"otasigned_v1.zip", "v2"}), - std::vector({"otasigned_v2.zip", "v1"}), - std::vector({"otasigned_v3.zip", "v5"}), - std::vector({"otasigned_v4.zip", "v5"}), - std::vector({"otasigned_v5.zip", "v3"}))); + ::testing::Values( + std::vector({"otasigned_v1.zip", "v2"}), + std::vector({"otasigned_v2.zip", "v1"}), + std::vector({"otasigned_v3.zip", "v5"}), + std::vector({"otasigned_v4.zip", "v5"}), + std::vector({"otasigned_v5.zip", "v3"}))); INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, - ::testing::Values( - std::vector({"otasigned_v1.zip", "v3"}), - std::vector({"otasigned_v2.zip", "v4"}), - std::vector({"otasigned_v3.zip", "v1"}), - std::vector({"otasigned_v4.zip", "v2"}))); + ::testing::Values( + std::vector({"otasigned_v1.zip", "v3"}), + std::vector({"otasigned_v2.zip", "v4"}), + std::vector({"otasigned_v3.zip", "v1"}), + std::vector({"otasigned_v4.zip", "v2"}))); INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, - ::testing::Values( - std::vector({"random.zip", "v1"}), - std::vector({"fake-eocd.zip", "v1"}), - std::vector({"alter-metadata.zip", "v1"}), - std::vector({"alter-footer.zip", "v1"}), - std::vector({"signature-boundary.zip", "v1"}))); + ::testing::Values( + std::vector({"random.zip", "v1"}), + std::vector({"fake-eocd.zip", "v1"}), + std::vector({"alter-metadata.zip", "v1"}), + std::vector({"alter-footer.zip", "v1"}), + std::vector({"signature-boundary.zip", "v1"}))); -- cgit v1.2.3