blob: 33aadb3fb0bc15877e319f1bfe59a4d614d97574 [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 {
Sen Jiangd5304492016-12-09 16:20:49 -080043 bool Init() { return true; }
Jed Estep43291862016-02-03 17:02:09 -080044 void SetStage(int, int) { }
45 void SetLocale(const char*) { }
Tao Baoe1a16af2016-02-04 11:30:42 -080046 void SetBackground(Icon /*icon*/) { }
Tianjie Xucacb47b2016-05-02 10:57:21 -070047 void SetSystemUpdateText(bool /*security_update*/) { }
Jed Estep43291862016-02-03 17:02:09 -080048
Tao Baoe1a16af2016-02-04 11:30:42 -080049 void SetProgressType(ProgressType /*determinate*/) { }
50 void ShowProgress(float /*portion*/, float /*seconds*/) { }
51 void SetProgress(float /*fraction*/) { }
Jed Estep43291862016-02-03 17:02:09 -080052
Tao Baoe1a16af2016-02-04 11:30:42 -080053 void ShowText(bool /*visible*/) { }
Jed Estep43291862016-02-03 17:02:09 -080054 bool IsTextVisible() { return false; }
55 bool WasTextEverVisible() { return false; }
56 void Print(const char* fmt, ...) {
57 va_list ap;
58 va_start(ap, fmt);
59 vfprintf(stderr, fmt, ap);
60 va_end(ap);
61 }
62 void PrintOnScreenOnly(const char* fmt, ...) {
63 va_list ap;
64 va_start(ap, fmt);
65 vfprintf(stderr, fmt, ap);
66 va_end(ap);
67 }
68 void ShowFile(const char*) { }
69
Tao Baoe1a16af2016-02-04 11:30:42 -080070 void StartMenu(const char* const* /*headers*/,
71 const char* const* /*items*/,
72 int /*initial_selection*/) { }
73 int SelectMenu(int /*sel*/) { return 0; }
Jed Estep43291862016-02-03 17:02:09 -080074 void EndMenu() { }
75};
76
77void
78ui_print(const char* format, ...) {
79 va_list ap;
80 va_start(ap, format);
81 vfprintf(stdout, format, ap);
82 va_end(ap);
83}
84
85class 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();
Tao Bao4102b282016-11-02 16:17:17 -070092 std::string package = from_testdata_base(args[0]);
Mattias Nissler452df6d2016-04-04 16:17:01 +020093 if (sysMapFile(package.c_str(), &memmap) != 0) {
Tao Bao5af4b192016-08-01 11:31:43 -070094 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Mattias Nissler452df6d2016-04-04 16:17:01 +020095 }
96
Jed Estep43291862016-02-03 17:02:09 -080097 for (auto it = ++(args.cbegin()); it != args.cend(); ++it) {
Tao Bao4102b282016-11-02 16:17:17 -070098 std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
Mattias Nissler452df6d2016-04-04 16:17:01 +020099 ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
Jed Estep43291862016-02-03 17:02:09 -0800100 }
101 }
102
103 static void SetUpTestCase() {
104 ui = new MockUI();
105 }
106};
107
108class VerifierSuccessTest : public VerifierTest {
109};
110
111class VerifierFailureTest : public VerifierTest {
112};
113
114TEST_P(VerifierSuccessTest, VerifySucceed) {
115 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS);
116}
117
118TEST_P(VerifierFailureTest, VerifyFailure) {
119 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE);
120}
121
122INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
123 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700124 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
125 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
126 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
127 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
128 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800129
130INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
131 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700132 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
133 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
134 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
135 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
136 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800137
138INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
139 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700140 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
141 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
142 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
143 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
144 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800145
146INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
147 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700148 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
149 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
150 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
151 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800152
153INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
154 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700155 std::vector<std::string>({"random.zip", "v1"}),
156 std::vector<std::string>({"fake-eocd.zip", "v1"}),
157 std::vector<std::string>({"alter-metadata.zip", "v1"}),
158 std::vector<std::string>({"alter-footer.zip", "v1"})));