blob: 6a3eebf295d3efca37642de3cd6864f6dba86409 [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>
32
33#include "common.h"
Jed Estepb8a693b2016-03-09 17:51:34 -080034#include "common/test_constants.h"
Jed Estep43291862016-02-03 17:02:09 -080035#include "minzip/SysUtil.h"
36#include "ui.h"
37#include "verifier.h"
38
Jed Estep43291862016-02-03 17:02:09 -080039static const char* DATA_PATH = getenv("ANDROID_DATA");
Jed Estepd5a14c62016-02-05 11:24:27 -080040static const char* TESTDATA_PATH = "/recovery/testdata/";
Jed Estep43291862016-02-03 17:02:09 -080041
Jed Estep43291862016-02-03 17:02:09 -080042RecoveryUI* ui = NULL;
43
44class MockUI : public RecoveryUI {
45 void Init() { }
46 void SetStage(int, int) { }
47 void SetLocale(const char*) { }
Tao Baoe1a16af2016-02-04 11:30:42 -080048 void SetBackground(Icon /*icon*/) { }
Tianjie Xucacb47b2016-05-02 10:57:21 -070049 void SetSystemUpdateText(bool /*security_update*/) { }
Jed Estep43291862016-02-03 17:02:09 -080050
Tao Baoe1a16af2016-02-04 11:30:42 -080051 void SetProgressType(ProgressType /*determinate*/) { }
52 void ShowProgress(float /*portion*/, float /*seconds*/) { }
53 void SetProgress(float /*fraction*/) { }
Jed Estep43291862016-02-03 17:02:09 -080054
Tao Baoe1a16af2016-02-04 11:30:42 -080055 void ShowText(bool /*visible*/) { }
Jed Estep43291862016-02-03 17:02:09 -080056 bool IsTextVisible() { return false; }
57 bool WasTextEverVisible() { return false; }
58 void Print(const char* fmt, ...) {
59 va_list ap;
60 va_start(ap, fmt);
61 vfprintf(stderr, fmt, ap);
62 va_end(ap);
63 }
64 void PrintOnScreenOnly(const char* fmt, ...) {
65 va_list ap;
66 va_start(ap, fmt);
67 vfprintf(stderr, fmt, ap);
68 va_end(ap);
69 }
70 void ShowFile(const char*) { }
71
Tao Baoe1a16af2016-02-04 11:30:42 -080072 void StartMenu(const char* const* /*headers*/,
73 const char* const* /*items*/,
74 int /*initial_selection*/) { }
75 int SelectMenu(int /*sel*/) { return 0; }
Jed Estep43291862016-02-03 17:02:09 -080076 void EndMenu() { }
77};
78
79void
80ui_print(const char* format, ...) {
81 va_list ap;
82 va_start(ap, format);
83 vfprintf(stdout, format, ap);
84 va_end(ap);
85}
86
87class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
88 public:
89 MemMapping memmap;
90 std::vector<Certificate> certs;
91
92 virtual void SetUp() {
93 std::vector<std::string> args = GetParam();
Mattias Nissler452df6d2016-04-04 16:17:01 +020094 std::string package =
95 android::base::StringPrintf("%s%s%s%s", DATA_PATH, NATIVE_TEST_PATH,
96 TESTDATA_PATH, args[0].c_str());
97 if (sysMapFile(package.c_str(), &memmap) != 0) {
Tao Bao5af4b192016-08-01 11:31:43 -070098 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Mattias Nissler452df6d2016-04-04 16:17:01 +020099 }
100
Jed Estep43291862016-02-03 17:02:09 -0800101 for (auto it = ++(args.cbegin()); it != args.cend(); ++it) {
Mattias Nissler452df6d2016-04-04 16:17:01 +0200102 std::string public_key_file = android::base::StringPrintf(
Tao Bao5af4b192016-08-01 11:31:43 -0700103 "%s%s%stestkey_%s.txt", DATA_PATH, NATIVE_TEST_PATH,
104 TESTDATA_PATH, it->c_str());
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"})));