blob: b740af96b4ee9ac9854c5da5dcf09bc9f22d3e1b [file] [log] [blame]
Jed Estep43291862016-02-03 17:02:09 -08001/*
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 Estep43291862016-02-03 17:02:09 -080022#include <sys/stat.h>
Mattias Nissler452df6d2016-04-04 16:17:01 +020023#include <sys/types.h>
Jed Estep43291862016-02-03 17:02:09 -080024
25#include <memory>
26#include <string>
27#include <vector>
28
Mattias Nissler452df6d2016-04-04 16:17:01 +020029#include <openssl/sha.h>
30
Jed Estep43291862016-02-03 17:02:09 -080031#include <android-base/stringprintf.h>
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070032#include <ziparchive/zip_archive.h>
Jed Estep43291862016-02-03 17:02:09 -080033
34#include "common.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080035#include "common/test_constants.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070036#include "otautil/SysUtil.h"
Jed Estep43291862016-02-03 17:02:09 -080037#include "ui.h"
38#include "verifier.h"
39
Jed Estep43291862016-02-03 17:02:09 -080040RecoveryUI* ui = NULL;
41
42class MockUI : public RecoveryUI {
Tao Bao736d59c2017-01-03 10:15:33 -080043 bool Init(const std::string&) override {
44 return true;
45 }
46 void SetStage(int, int) override {}
47 void SetBackground(Icon /*icon*/) override {}
48 void SetSystemUpdateText(bool /*security_update*/) override {}
Jed Estep43291862016-02-03 17:02:09 -080049
Tao Bao736d59c2017-01-03 10:15:33 -080050 void SetProgressType(ProgressType /*determinate*/) override {}
51 void ShowProgress(float /*portion*/, float /*seconds*/) override {}
52 void SetProgress(float /*fraction*/) override {}
Jed Estep43291862016-02-03 17:02:09 -080053
Tao Bao736d59c2017-01-03 10:15:33 -080054 void ShowText(bool /*visible*/) override {}
55 bool IsTextVisible() override {
56 return false;
57 }
58 bool WasTextEverVisible() override {
59 return false;
60 }
61 void Print(const char* fmt, ...) override {
62 va_list ap;
63 va_start(ap, fmt);
64 vfprintf(stderr, fmt, ap);
65 va_end(ap);
66 }
67 void PrintOnScreenOnly(const char* fmt, ...) override {
68 va_list ap;
69 va_start(ap, fmt);
70 vfprintf(stderr, fmt, ap);
71 va_end(ap);
72 }
73 void ShowFile(const char*) override {}
Jed Estep43291862016-02-03 17:02:09 -080074
Tao Bao736d59c2017-01-03 10:15:33 -080075 void StartMenu(const char* const* /*headers*/, const char* const* /*items*/,
76 int /*initial_selection*/) override {}
77 int SelectMenu(int /*sel*/) override {
78 return 0;
79 }
80 void EndMenu() override {}
Jed Estep43291862016-02-03 17:02:09 -080081};
82
83void
84ui_print(const char* format, ...) {
85 va_list ap;
86 va_start(ap, format);
87 vfprintf(stdout, format, ap);
88 va_end(ap);
89}
90
91class 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();
Tao Bao4102b282016-11-02 16:17:17 -070098 std::string package = from_testdata_base(args[0]);
Mattias Nissler452df6d2016-04-04 16:17:01 +020099 if (sysMapFile(package.c_str(), &memmap) != 0) {
Tao Bao5af4b192016-08-01 11:31:43 -0700100 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Mattias Nissler452df6d2016-04-04 16:17:01 +0200101 }
102
Jed Estep43291862016-02-03 17:02:09 -0800103 for (auto it = ++(args.cbegin()); it != args.cend(); ++it) {
Tao Bao4102b282016-11-02 16:17:17 -0700104 std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
Mattias Nissler452df6d2016-04-04 16:17:01 +0200105 ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
Jed Estep43291862016-02-03 17:02:09 -0800106 }
107 }
108
109 static void SetUpTestCase() {
110 ui = new MockUI();
111 }
112};
113
114class VerifierSuccessTest : public VerifierTest {
115};
116
117class VerifierFailureTest : public VerifierTest {
118};
119
120TEST_P(VerifierSuccessTest, VerifySucceed) {
121 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS);
122}
123
124TEST_P(VerifierFailureTest, VerifyFailure) {
125 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE);
126}
127
128INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
129 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700130 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
131 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
132 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
133 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
134 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800135
136INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
137 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700138 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
139 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
140 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
141 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
142 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800143
144INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
145 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700146 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
147 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
148 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
149 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
150 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800151
152INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
153 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700154 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
155 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
156 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
157 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800158
159INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
160 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700161 std::vector<std::string>({"random.zip", "v1"}),
162 std::vector<std::string>({"fake-eocd.zip", "v1"}),
163 std::vector<std::string>({"alter-metadata.zip", "v1"}),
164 std::vector<std::string>({"alter-footer.zip", "v1"})));