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> |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [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 | |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [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 | e130576 | 2016-02-22 10:59:37 -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*) { } |
| 48 | void SetBackground(Icon icon) { } |
| 49 | |
| 50 | void SetProgressType(ProgressType determinate) { } |
| 51 | void ShowProgress(float portion, float seconds) { } |
| 52 | void SetProgress(float fraction) { } |
| 53 | |
| 54 | void ShowText(bool visible) { } |
| 55 | bool IsTextVisible() { return false; } |
| 56 | bool WasTextEverVisible() { return false; } |
| 57 | void Print(const char* fmt, ...) { |
| 58 | va_list ap; |
| 59 | va_start(ap, fmt); |
| 60 | vfprintf(stderr, fmt, ap); |
| 61 | va_end(ap); |
| 62 | } |
| 63 | void PrintOnScreenOnly(const char* fmt, ...) { |
| 64 | va_list ap; |
| 65 | va_start(ap, fmt); |
| 66 | vfprintf(stderr, fmt, ap); |
| 67 | va_end(ap); |
| 68 | } |
| 69 | void ShowFile(const char*) { } |
| 70 | |
| 71 | void StartMenu(const char* const * headers, const char* const * items, |
| 72 | int initial_selection) { } |
| 73 | int SelectMenu(int sel) { return 0; } |
| 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(); |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 92 | std::string package = |
| 93 | android::base::StringPrintf("%s%s%s%s", DATA_PATH, NATIVE_TEST_PATH, |
| 94 | TESTDATA_PATH, args[0].c_str()); |
| 95 | if (sysMapFile(package.c_str(), &memmap) != 0) { |
| 96 | FAIL() << "Failed to mmap " << package << ": " << strerror(errno) |
| 97 | << "\n"; |
| 98 | } |
| 99 | |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 100 | for (auto it = ++(args.cbegin()); it != args.cend(); ++it) { |
| 101 | if (it->substr(it->length() - 3, it->length()) == "256") { |
| 102 | if (certs.empty()) { |
| 103 | FAIL() << "May only specify -sha256 after key type\n"; |
| 104 | } |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 105 | certs.back().hash_len = SHA256_DIGEST_LENGTH; |
| 106 | } else { |
| 107 | std::string public_key_file = android::base::StringPrintf( |
| 108 | "%s%s%stest_key_%s.txt", DATA_PATH, NATIVE_TEST_PATH, |
| 109 | TESTDATA_PATH, it->c_str()); |
| 110 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
| 111 | certs.back().hash_len = SHA_DIGEST_LENGTH; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | if (certs.empty()) { |
Elliott Hughes | 8febafa | 2016-04-13 16:39:56 -0700 | [diff] [blame] | 115 | std::string public_key_file = android::base::StringPrintf( |
| 116 | "%s%s%stest_key_e3.txt", DATA_PATH, NATIVE_TEST_PATH, |
| 117 | TESTDATA_PATH); |
| 118 | ASSERT_TRUE(load_keys(public_key_file.c_str(), certs)); |
| 119 | certs.back().hash_len = SHA_DIGEST_LENGTH; |
Jed Estep | 4329186 | 2016-02-03 17:02:09 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | static void SetUpTestCase() { |
| 124 | ui = new MockUI(); |
| 125 | } |
| 126 | }; |
| 127 | |
| 128 | class VerifierSuccessTest : public VerifierTest { |
| 129 | }; |
| 130 | |
| 131 | class VerifierFailureTest : public VerifierTest { |
| 132 | }; |
| 133 | |
| 134 | TEST_P(VerifierSuccessTest, VerifySucceed) { |
| 135 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS); |
| 136 | } |
| 137 | |
| 138 | TEST_P(VerifierFailureTest, VerifyFailure) { |
| 139 | ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE); |
| 140 | } |
| 141 | |
| 142 | INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest, |
| 143 | ::testing::Values( |
| 144 | std::vector<std::string>({"otasigned.zip", "e3"}), |
| 145 | std::vector<std::string>({"otasigned_f4.zip", "f4"}), |
| 146 | std::vector<std::string>({"otasigned_sha256.zip", "e3", "sha256"}), |
| 147 | std::vector<std::string>({"otasigned_f4_sha256.zip", "f4", "sha256"}), |
| 148 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip", "ec", "sha256"}))); |
| 149 | |
| 150 | INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest, |
| 151 | ::testing::Values( |
| 152 | std::vector<std::string>({"otasigned.zip", "f4", "e3"}), |
| 153 | std::vector<std::string>({"otasigned_f4.zip", "ec", "f4"}), |
| 154 | std::vector<std::string>({"otasigned_sha256.zip", "ec", "e3", "e3", "sha256"}), |
| 155 | std::vector<std::string>({"otasigned_f4_sha256.zip", "ec", "sha256", "e3", "f4", "sha256"}), |
| 156 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip", "f4", "sha256", "e3", "ec", "sha256"}))); |
| 157 | |
| 158 | INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest, |
| 159 | ::testing::Values( |
| 160 | std::vector<std::string>({"otasigned.zip", "f4"}), |
| 161 | std::vector<std::string>({"otasigned_f4.zip", "e3"}), |
| 162 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip", "e3", "sha256"}))); |
| 163 | |
| 164 | INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest, |
| 165 | ::testing::Values( |
| 166 | std::vector<std::string>({"otasigned.zip", "e3", "sha256"}), |
| 167 | std::vector<std::string>({"otasigned_f4.zip", "f4", "sha256"}), |
| 168 | std::vector<std::string>({"otasigned_sha256.zip"}), |
| 169 | std::vector<std::string>({"otasigned_f4_sha256.zip", "f4"}), |
| 170 | std::vector<std::string>({"otasigned_ecdsa_sha256.zip"}))); |
| 171 | |
| 172 | INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest, |
| 173 | ::testing::Values( |
| 174 | std::vector<std::string>({"random.zip"}), |
| 175 | std::vector<std::string>({"fake-eocd.zip"}), |
| 176 | std::vector<std::string>({"alter-metadata.zip"}), |
| 177 | std::vector<std::string>({"alter-footer.zip"}))); |