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 | |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 29 | #include <openssl/sha.h> |
| 30 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 31 | #include <android-base/stringprintf.h> |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 32 | #include <ziparchive/zip_archive.h> |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 33 | |
| 34 | #include "common.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 35 | #include "common/test_constants.h" |
Tianjie Xu | 8cf5c8f | 2016-09-08 20:10:11 -0700 | [diff] [blame] | 36 | #include "otautil/SysUtil.h" |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 37 | #include "ui.h" |
| 38 | #include "verifier.h" |
| 39 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 40 | RecoveryUI* ui = NULL; |
| 41 | |
| 42 | class MockUI : public RecoveryUI { |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 43 | bool Init(const std::string&) override { |
| 44 | return true; |
| 45 | } |
| 46 | void SetStage(int, int) override {} |
| 47 | void SetBackground(Icon /*icon*/) override {} |
| 48 | void SetSystemUpdateText(bool /*security_update*/) override {} |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 49 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 50 | void SetProgressType(ProgressType /*determinate*/) override {} |
| 51 | void ShowProgress(float /*portion*/, float /*seconds*/) override {} |
| 52 | void SetProgress(float /*fraction*/) override {} |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 53 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 54 | void ShowText(bool /*visible*/) override {} |
| 55 | bool IsTextVisible() override { |
| 56 | return false; |
| 57 | } |
| 58 | bool WasTextEverVisible() override { |
| 59 | return false; |
| 60 | } |
| 61 | void Print(const char* fmt, ...) override { |
| 62 | va_list ap; |
| 63 | va_start(ap, fmt); |
| 64 | vfprintf(stderr, fmt, ap); |
| 65 | va_end(ap); |
| 66 | } |
| 67 | void PrintOnScreenOnly(const char* fmt, ...) override { |
| 68 | va_list ap; |
| 69 | va_start(ap, fmt); |
| 70 | vfprintf(stderr, fmt, ap); |
| 71 | va_end(ap); |
| 72 | } |
| 73 | void ShowFile(const char*) override {} |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 74 | |
Tao Bao | 736d59c | 2017-01-03 10:15:33 -0800 | [diff] [blame] | 75 | void StartMenu(const char* const* /*headers*/, const char* const* /*items*/, |
| 76 | int /*initial_selection*/) override {} |
| 77 | int SelectMenu(int /*sel*/) override { |
| 78 | return 0; |
| 79 | } |
| 80 | void EndMenu() override {} |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | void |
| 84 | ui_print(const char* format, ...) { |
| 85 | va_list ap; |
| 86 | va_start(ap, format); |
| 87 | vfprintf(stdout, format, ap); |
| 88 | va_end(ap); |
| 89 | } |
| 90 | |
| 91 | class VerifierTest : public testing::TestWithParam<std::vector<std::string>> { |
| 92 | public: |
| 93 | MemMapping memmap; |
| 94 | std::vector<Certificate> certs; |
| 95 | |
| 96 | virtual void SetUp() { |
| 97 | std::vector<std::string> args = GetParam(); |
Tao Bao | 4102b28 | 2016-11-02 16:17:17 -0700 | [diff] [blame] | 98 | std::string package = from_testdata_base(args[0]); |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 99 | if (sysMapFile(package.c_str(), &memmap) != 0) { |
Tao Bao | 5af4b19 | 2016-08-01 11:31:43 -0700 | [diff] [blame] | 100 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n"; |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 103 | for (auto it = ++(args.cbegin()); it != args.cend(); ++it) { |
Tao Bao | 4102b28 | 2016-11-02 16:17:17 -0700 | [diff] [blame] | 104 | std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt"); |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 105 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | static void SetUpTestCase() { |
| 110 | ui = new MockUI(); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | class VerifierSuccessTest : public VerifierTest { |
| 115 | }; |
| 116 | |
| 117 | class VerifierFailureTest : public VerifierTest { |
| 118 | }; |
| 119 | |
| 120 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
| 121 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS); |
| 122 | } |
| 123 | |
| 124 | TEST_P(VerifierFailureTest, VerifyFailure) { |
| 125 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE); |
| 126 | } |
| 127 | |
| 128 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
| 129 | ::testing::Values( |
Tao Bao | 5af4b19 | 2016-08-01 11:31:43 -0700 | [diff] [blame] | 130 | std::vector<std::string>({"otasigned_v1.zip", "v1"}), |
| 131 | std::vector<std::string>({"otasigned_v2.zip", "v2"}), |
| 132 | std::vector<std::string>({"otasigned_v3.zip", "v3"}), |
| 133 | std::vector<std::string>({"otasigned_v4.zip", "v4"}), |
| 134 | std::vector<std::string>({"otasigned_v5.zip", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 135 | |
| 136 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
| 137 | ::testing::Values( |
Tao Bao | 5af4b19 | 2016-08-01 11:31:43 -0700 | [diff] [blame] | 138 | std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}), |
| 139 | std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}), |
| 140 | std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}), |
| 141 | std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}), |
| 142 | std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 143 | |
| 144 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
| 145 | ::testing::Values( |
Tao Bao | 5af4b19 | 2016-08-01 11:31:43 -0700 | [diff] [blame] | 146 | std::vector<std::string>({"otasigned_v1.zip", "v2"}), |
| 147 | std::vector<std::string>({"otasigned_v2.zip", "v1"}), |
| 148 | std::vector<std::string>({"otasigned_v3.zip", "v5"}), |
| 149 | std::vector<std::string>({"otasigned_v4.zip", "v5"}), |
| 150 | std::vector<std::string>({"otasigned_v5.zip", "v3"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 151 | |
| 152 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
| 153 | ::testing::Values( |
Tao Bao | 5af4b19 | 2016-08-01 11:31:43 -0700 | [diff] [blame] | 154 | std::vector<std::string>({"otasigned_v1.zip", "v3"}), |
| 155 | std::vector<std::string>({"otasigned_v2.zip", "v4"}), |
| 156 | std::vector<std::string>({"otasigned_v3.zip", "v1"}), |
| 157 | std::vector<std::string>({"otasigned_v4.zip", "v2"}))); |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 158 | |
| 159 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
| 160 | ::testing::Values( |
Tao Bao | 5af4b19 | 2016-08-01 11:31:43 -0700 | [diff] [blame] | 161 | std::vector<std::string>({"random.zip", "v1"}), |
| 162 | std::vector<std::string>({"fake-eocd.zip", "v1"}), |
| 163 | std::vector<std::string>({"alter-metadata.zip", "v1"}), |
| 164 | std::vector<std::string>({"alter-footer.zip", "v1"}))); |