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