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> |
| 32 | |
| 33 | #include "common.h" |
Jed Estep | b8a693b | 2016-03-09 17:51:34 -0800 | [diff] [blame] | 34 | #include "common/test_constants.h" |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 35 | #include "minzip/SysUtil.h" |
| 36 | #include "ui.h" |
| 37 | #include "verifier.h" |
| 38 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 39 | static const char* DATA_PATH = getenv("ANDROID_DATA"); |
Jed Estep | d5a14c6 | 2016-02-05 11:24:27 -0800 | [diff] [blame] | 40 | static const char* TESTDATA_PATH = "/recovery/testdata/"; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 41 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 42 | RecoveryUI* ui = NULL; |
| 43 | |
| 44 | class MockUI : public RecoveryUI { |
| 45 | void Init() { } |
| 46 | void SetStage(int, int) { } |
| 47 | void SetLocale(const char*) { } |
Tao Bao | e1a16af | 2016-02-04 11:30:42 -0800 | [diff] [blame] | 48 | void SetBackground(Icon /*icon*/) { } |
Tianjie Xu | 70b5353 | 2016-05-02 11:17:57 -0700 | [diff] [blame] | 49 | void SetSystemUpdateText(bool /*security_update*/) { } |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 50 | |
Tao Bao | e1a16af | 2016-02-04 11:30:42 -0800 | [diff] [blame] | 51 | void SetProgressType(ProgressType /*determinate*/) { } |
| 52 | void ShowProgress(float /*portion*/, float /*seconds*/) { } |
| 53 | void SetProgress(float /*fraction*/) { } |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 54 | |
Tao Bao | e1a16af | 2016-02-04 11:30:42 -0800 | [diff] [blame] | 55 | void ShowText(bool /*visible*/) { } |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 56 | bool IsTextVisible() { return false; } |
| 57 | bool WasTextEverVisible() { return false; } |
| 58 | void Print(const char* fmt, ...) { |
| 59 | va_list ap; |
| 60 | va_start(ap, fmt); |
| 61 | vfprintf(stderr, fmt, ap); |
| 62 | va_end(ap); |
| 63 | } |
| 64 | void PrintOnScreenOnly(const char* fmt, ...) { |
| 65 | va_list ap; |
| 66 | va_start(ap, fmt); |
| 67 | vfprintf(stderr, fmt, ap); |
| 68 | va_end(ap); |
| 69 | } |
| 70 | void ShowFile(const char*) { } |
| 71 | |
Tao Bao | e1a16af | 2016-02-04 11:30:42 -0800 | [diff] [blame] | 72 | void StartMenu(const char* const* /*headers*/, |
| 73 | const char* const* /*items*/, |
| 74 | int /*initial_selection*/) { } |
| 75 | int SelectMenu(int /*sel*/) { return 0; } |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 76 | void EndMenu() { } |
| 77 | }; |
| 78 | |
| 79 | void |
| 80 | ui_print(const char* format, ...) { |
| 81 | va_list ap; |
| 82 | va_start(ap, format); |
| 83 | vfprintf(stdout, format, ap); |
| 84 | va_end(ap); |
| 85 | } |
| 86 | |
| 87 | class VerifierTest : public testing::TestWithParam<std::vector<std::string>> { |
| 88 | public: |
| 89 | MemMapping memmap; |
| 90 | std::vector<Certificate> certs; |
| 91 | |
| 92 | virtual void SetUp() { |
| 93 | std::vector<std::string> args = GetParam(); |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 94 | std::string package = |
| 95 | android::base::StringPrintf("%s%s%s%s", DATA_PATH, NATIVE_TEST_PATH, |
| 96 | TESTDATA_PATH, args[0].c_str()); |
| 97 | if (sysMapFile(package.c_str(), &memmap) != 0) { |
| 98 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) |
| 99 | << "\n"; |
| 100 | } |
| 101 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 102 | for (auto it = ++(args.cbegin()); it != args.cend(); ++it) { |
| 103 | if (it->substr(it->length() - 3, it->length()) == "256") { |
| 104 | if (certs.empty()) { |
| 105 | FAIL() << "May only specify -sha256 after key type\n"; |
| 106 | } |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 107 | certs.back().hash_len = SHA256_DIGEST_LENGTH; |
| 108 | } else { |
| 109 | std::string public_key_file = android::base::StringPrintf( |
| 110 | "%s%s%stest_key_%s.txt", DATA_PATH, NATIVE_TEST_PATH, |
| 111 | TESTDATA_PATH, it->c_str()); |
| 112 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
| 113 | certs.back().hash_len = SHA_DIGEST_LENGTH; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | if (certs.empty()) { |
Mattias Nissler | 452df6d | 2016-04-04 16:17:01 +0200 | [diff] [blame] | 117 | std::string public_key_file = android::base::StringPrintf( |
| 118 | "%s%s%stest_key_e3.txt", DATA_PATH, NATIVE_TEST_PATH, |
| 119 | TESTDATA_PATH); |
| 120 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
| 121 | certs.back().hash_len = SHA_DIGEST_LENGTH; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
| 125 | static void SetUpTestCase() { |
| 126 | ui = new MockUI(); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | class VerifierSuccessTest : public VerifierTest { |
| 131 | }; |
| 132 | |
| 133 | class VerifierFailureTest : public VerifierTest { |
| 134 | }; |
| 135 | |
| 136 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
| 137 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS); |
| 138 | } |
| 139 | |
| 140 | TEST_P(VerifierFailureTest, VerifyFailure) { |
| 141 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE); |
| 142 | } |
| 143 | |
| 144 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
| 145 | ::testing::Values( |
| 146 | std::vector<std::string>({"otasigned.zip", "e3"}), |
| 147 | std::vector<std::string>({"otasigned_f4.zip", "f4"}), |
| 148 | std::vector<std::string>({"otasigned_sha256.zip", "e3", "sha256"}), |
| 149 | std::vector<std::string>({"otasigned_f4_sha256.zip", "f4", "sha256"}), |
| 150 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip", "ec", "sha256"}))); |
| 151 | |
| 152 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
| 153 | ::testing::Values( |
| 154 | std::vector<std::string>({"otasigned.zip", "f4", "e3"}), |
| 155 | std::vector<std::string>({"otasigned_f4.zip", "ec", "f4"}), |
| 156 | std::vector<std::string>({"otasigned_sha256.zip", "ec", "e3", "e3", "sha256"}), |
| 157 | std::vector<std::string>({"otasigned_f4_sha256.zip", "ec", "sha256", "e3", "f4", "sha256"}), |
| 158 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip", "f4", "sha256", "e3", "ec", "sha256"}))); |
| 159 | |
| 160 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
| 161 | ::testing::Values( |
| 162 | std::vector<std::string>({"otasigned.zip", "f4"}), |
| 163 | std::vector<std::string>({"otasigned_f4.zip", "e3"}), |
| 164 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip", "e3", "sha256"}))); |
| 165 | |
| 166 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
| 167 | ::testing::Values( |
| 168 | std::vector<std::string>({"otasigned.zip", "e3", "sha256"}), |
| 169 | std::vector<std::string>({"otasigned_f4.zip", "f4", "sha256"}), |
| 170 | std::vector<std::string>({"otasigned_sha256.zip"}), |
| 171 | std::vector<std::string>({"otasigned_f4_sha256.zip", "f4"}), |
| 172 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip"}))); |
| 173 | |
| 174 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
| 175 | ::testing::Values( |
| 176 | std::vector<std::string>({"random.zip"}), |
| 177 | std::vector<std::string>({"fake-eocd.zip"}), |
| 178 | std::vector<std::string>({"alter-metadata.zip"}), |
| 179 | std::vector<std::string>({"alter-footer.zip"}))); |