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