Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agree to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <gtest/gtest.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 22 | #include <sys/stat.h> |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 23 | #include <sys/types.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 24 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 28 | #include <android-base/file.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 30 | #include <android-base/test_utils.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 31 | |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 32 | #include "common/test_constants.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 33 | #include "otautil/SysUtil.h" |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 34 | #include "verifier.h" |
| 35 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 36 | class VerifierTest : public testing::TestWithParam<std::vector<std::string>> { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 37 | protected: |
| 38 | void SetUp() override { |
| 39 | std::vector<std::string> args = GetParam(); |
| 40 | std::string package = from_testdata_base(args[0]); |
| 41 | if (sysMapFile(package.c_str(), &memmap) != 0) { |
| 42 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n"; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 45 | for (auto it = ++args.cbegin(); it != args.cend(); ++it) { |
| 46 | std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt"); |
| 47 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 48 | } |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | MemMapping memmap; |
| 52 | std::vector<Certificate> certs; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | class VerifierSuccessTest : public VerifierTest { |
| 56 | }; |
| 57 | |
| 58 | class VerifierFailureTest : public VerifierTest { |
| 59 | }; |
| 60 | |
| 61 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 62 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | TEST_P(VerifierFailureTest, VerifyFailure) { |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 66 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 70 | ::testing::Values( |
| 71 | std::vector<std::string>({"otasigned_v1.zip", "v1"}), |
| 72 | std::vector<std::string>({"otasigned_v2.zip", "v2"}), |
| 73 | std::vector<std::string>({"otasigned_v3.zip", "v3"}), |
| 74 | std::vector<std::string>({"otasigned_v4.zip", "v4"}), |
| 75 | std::vector<std::string>({"otasigned_v5.zip", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 76 | |
| 77 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 78 | ::testing::Values( |
| 79 | std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}), |
| 80 | std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}), |
| 81 | std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}), |
| 82 | std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}), |
| 83 | std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 84 | |
| 85 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 86 | ::testing::Values( |
| 87 | std::vector<std::string>({"otasigned_v1.zip", "v2"}), |
| 88 | std::vector<std::string>({"otasigned_v2.zip", "v1"}), |
| 89 | std::vector<std::string>({"otasigned_v3.zip", "v5"}), |
| 90 | std::vector<std::string>({"otasigned_v4.zip", "v5"}), |
| 91 | std::vector<std::string>({"otasigned_v5.zip", "v3"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 92 | |
| 93 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 94 | ::testing::Values( |
| 95 | std::vector<std::string>({"otasigned_v1.zip", "v3"}), |
| 96 | std::vector<std::string>({"otasigned_v2.zip", "v4"}), |
| 97 | std::vector<std::string>({"otasigned_v3.zip", "v1"}), |
| 98 | std::vector<std::string>({"otasigned_v4.zip", "v2"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 99 | |
| 100 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
Tao Bao | 7b22c92 | 2017-03-16 17:37:38 -0700 | [diff] [blame] | 101 | ::testing::Values( |
| 102 | std::vector<std::string>({"random.zip", "v1"}), |
| 103 | std::vector<std::string>({"fake-eocd.zip", "v1"}), |
| 104 | std::vector<std::string>({"alter-metadata.zip", "v1"}), |
| 105 | std::vector<std::string>({"alter-footer.zip", "v1"}), |
| 106 | std::vector<std::string>({"signature-boundary.zip", "v1"}))); |