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