2009-12-10 02:01:45 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 The Android Open Source Project
|
|
|
|
*
|
2016-02-04 02:02:09 +01:00
|
|
|
* Licensed under the Apache License, version 2.0 (the "License");
|
2009-12-10 02:01:45 +01:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
2016-02-04 02:02:09 +01:00
|
|
|
* Unless required by applicable law or agree to in writing, software
|
2009-12-10 02:01:45 +01:00
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2014-12-29 21:46:43 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2016-02-04 02:02:09 +01:00
|
|
|
#include <gtest/gtest.h>
|
2009-12-10 02:01:45 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-01-13 23:16:58 +01:00
|
|
|
#include <sys/stat.h>
|
2016-04-04 16:17:01 +02:00
|
|
|
#include <sys/types.h>
|
2009-12-10 02:01:45 +01:00
|
|
|
|
2016-02-02 23:02:27 +01:00
|
|
|
#include <memory>
|
2016-02-04 02:02:09 +01:00
|
|
|
#include <string>
|
2016-02-02 23:02:27 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2016-04-04 16:17:01 +02:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2016-09-09 05:10:11 +02:00
|
|
|
#include <ziparchive/zip_archive.h>
|
2016-02-04 02:02:09 +01:00
|
|
|
|
2013-05-14 20:03:02 +02:00
|
|
|
#include "common.h"
|
2016-03-10 02:51:34 +01:00
|
|
|
#include "common/test_constants.h"
|
2016-09-09 05:10:11 +02:00
|
|
|
#include "otautil/SysUtil.h"
|
2016-02-04 02:02:09 +01:00
|
|
|
#include "ui.h"
|
|
|
|
#include "verifier.h"
|
|
|
|
|
2011-10-31 17:34:15 +01:00
|
|
|
RecoveryUI* ui = NULL;
|
2009-12-10 02:01:45 +01:00
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
class MockUI : public RecoveryUI {
|
2017-01-03 19:15:33 +01:00
|
|
|
bool Init(const std::string&) override {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void SetStage(int, int) override {}
|
|
|
|
void SetBackground(Icon /*icon*/) override {}
|
|
|
|
void SetSystemUpdateText(bool /*security_update*/) override {}
|
2009-12-10 02:01:45 +01:00
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
void SetProgressType(ProgressType /*determinate*/) override {}
|
|
|
|
void ShowProgress(float /*portion*/, float /*seconds*/) override {}
|
|
|
|
void SetProgress(float /*fraction*/) override {}
|
2011-10-31 17:34:15 +01:00
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
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 {}
|
2011-10-31 17:34:15 +01:00
|
|
|
|
2017-01-03 19:15:33 +01:00
|
|
|
void StartMenu(const char* const* /*headers*/, const char* const* /*items*/,
|
|
|
|
int /*initial_selection*/) override {}
|
|
|
|
int SelectMenu(int /*sel*/) override {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
void EndMenu() override {}
|
2011-10-31 17:34:15 +01:00
|
|
|
};
|
2009-12-10 02:01:45 +01:00
|
|
|
|
2013-05-14 20:03:02 +02:00
|
|
|
void
|
|
|
|
ui_print(const char* format, ...) {
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, format);
|
|
|
|
vfprintf(stdout, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
|
|
|
|
public:
|
|
|
|
MemMapping memmap;
|
2016-02-02 23:02:27 +01:00
|
|
|
std::vector<Certificate> certs;
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
virtual void SetUp() {
|
|
|
|
std::vector<std::string> args = GetParam();
|
2016-11-03 00:17:17 +01:00
|
|
|
std::string package = from_testdata_base(args[0]);
|
2016-04-04 16:17:01 +02:00
|
|
|
if (sysMapFile(package.c_str(), &memmap) != 0) {
|
2016-08-01 20:31:43 +02:00
|
|
|
FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
|
2016-04-04 16:17:01 +02:00
|
|
|
}
|
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
for (auto it = ++(args.cbegin()); it != args.cend(); ++it) {
|
2016-11-03 00:17:17 +01:00
|
|
|
std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
|
2016-04-04 16:17:01 +02:00
|
|
|
ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
|
2013-10-09 19:14:35 +02:00
|
|
|
}
|
2013-04-10 20:32:17 +02:00
|
|
|
}
|
2013-10-09 19:14:35 +02:00
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
static void SetUpTestCase() {
|
|
|
|
ui = new MockUI();
|
2013-10-09 19:14:35 +02:00
|
|
|
}
|
2016-02-04 02:02:09 +01:00
|
|
|
};
|
2013-10-09 19:14:35 +02:00
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
class VerifierSuccessTest : public VerifierTest {
|
|
|
|
};
|
2012-07-25 22:10:58 +02:00
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
class VerifierFailureTest : public VerifierTest {
|
|
|
|
};
|
2011-10-31 17:34:15 +01:00
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
TEST_P(VerifierSuccessTest, VerifySucceed) {
|
|
|
|
ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS);
|
|
|
|
}
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
TEST_P(VerifierFailureTest, VerifyFailure) {
|
|
|
|
ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE);
|
2009-12-10 02:01:45 +01:00
|
|
|
}
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
|
|
|
|
::testing::Values(
|
2016-08-01 20:31:43 +02:00
|
|
|
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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
|
|
|
|
::testing::Values(
|
2016-08-01 20:31:43 +02:00
|
|
|
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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
|
|
|
|
::testing::Values(
|
2016-08-01 20:31:43 +02:00
|
|
|
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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
|
|
|
|
::testing::Values(
|
2016-08-01 20:31:43 +02:00
|
|
|
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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
|
|
|
|
::testing::Values(
|
2016-08-01 20:31:43 +02:00
|
|
|
std::vector<std::string>({"random.zip", "v1"}),
|
|
|
|
std::vector<std::string>({"fake-eocd.zip", "v1"}),
|
|
|
|
std::vector<std::string>({"alter-metadata.zip", "v1"}),
|
2016-12-16 23:27:55 +01:00
|
|
|
std::vector<std::string>({"alter-footer.zip", "v1"}),
|
|
|
|
std::vector<std::string>({"signature-boundary.zip", "v1"})));
|