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-04 02:02:09 +01:00
|
|
|
#include <string>
|
2016-02-02 23:02:27 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2017-03-17 01:37:38 +01:00
|
|
|
#include <android-base/file.h>
|
2016-02-04 02:02:09 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
2017-03-17 01:37:38 +01:00
|
|
|
#include <android-base/test_utils.h>
|
2016-02-04 02:02:09 +01:00
|
|
|
|
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 "verifier.h"
|
|
|
|
|
|
|
|
class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
|
2017-03-17 01:37:38 +01:00
|
|
|
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";
|
2013-04-10 20:32:17 +02:00
|
|
|
}
|
2013-10-09 19:14:35 +02:00
|
|
|
|
2017-03-17 01:37:38 +01:00
|
|
|
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));
|
2013-10-09 19:14:35 +02:00
|
|
|
}
|
2017-03-17 01:37:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
MemMapping memmap;
|
|
|
|
std::vector<Certificate> certs;
|
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) {
|
2017-03-17 01:37:38 +01:00
|
|
|
ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
|
2016-02-04 02:02:09 +01:00
|
|
|
}
|
2014-01-13 23:16:58 +01:00
|
|
|
|
2016-02-04 02:02:09 +01:00
|
|
|
TEST_P(VerifierFailureTest, VerifyFailure) {
|
2017-03-17 01:37:38 +01:00
|
|
|
ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
|
2009-12-10 02:01:45 +01:00
|
|
|
}
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
|
2017-03-17 01:37:38 +01:00
|
|
|
::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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
|
2017-03-17 01:37:38 +01:00
|
|
|
::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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
|
2017-03-17 01:37:38 +01:00
|
|
|
::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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
|
2017-03-17 01:37:38 +01:00
|
|
|
::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"})));
|
2016-02-04 02:02:09 +01:00
|
|
|
|
|
|
|
INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
|
2017-03-17 01:37:38 +01:00
|
|
|
::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"})));
|