blob: 2a78173d869ed400b78d949a2abdb78cf877ee80 [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 Estep43291862016-02-03 17:02:09 -080034#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
44static const char* DATA_PATH = getenv("ANDROID_DATA");
Jed Estepd5a14c62016-02-05 11:24:27 -080045static const char* TESTDATA_PATH = "/recovery/testdata/";
Jed Estep43291862016-02-03 17:02:09 -080046
Jed Estep43291862016-02-03 17:02:09 -080047RecoveryUI* ui = NULL;
48
49class MockUI : public RecoveryUI {
50 void Init() { }
51 void SetStage(int, int) { }
52 void SetLocale(const char*) { }
Tao Baoe1a16af2016-02-04 11:30:42 -080053 void SetBackground(Icon /*icon*/) { }
Jed Estep43291862016-02-03 17:02:09 -080054
Tao Baoe1a16af2016-02-04 11:30:42 -080055 void SetProgressType(ProgressType /*determinate*/) { }
56 void ShowProgress(float /*portion*/, float /*seconds*/) { }
57 void SetProgress(float /*fraction*/) { }
Jed Estep43291862016-02-03 17:02:09 -080058
Tao Baoe1a16af2016-02-04 11:30:42 -080059 void ShowText(bool /*visible*/) { }
Jed Estep43291862016-02-03 17:02:09 -080060 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 Baoe1a16af2016-02-04 11:30:42 -080076 void StartMenu(const char* const* /*headers*/,
77 const char* const* /*items*/,
78 int /*initial_selection*/) { }
79 int SelectMenu(int /*sel*/) { return 0; }
Jed Estep43291862016-02-03 17:02:09 -080080 void EndMenu() { }
81};
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();
Mattias Nissler452df6d2016-04-04 16:17:01 +020098 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) {
Tao Bao5af4b192016-08-01 11:31:43 -0700102 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Mattias Nissler452df6d2016-04-04 16:17:01 +0200103 }
104
Jed Estep43291862016-02-03 17:02:09 -0800105 for (auto it = ++(args.cbegin()); it != args.cend(); ++it) {
Mattias Nissler452df6d2016-04-04 16:17:01 +0200106 std::string public_key_file = android::base::StringPrintf(
Tao Bao5af4b192016-08-01 11:31:43 -0700107 "%s%s%stestkey_%s.txt", DATA_PATH, NATIVE_TEST_PATH,
108 TESTDATA_PATH, it->c_str());
Mattias Nissler452df6d2016-04-04 16:17:01 +0200109 ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
Jed Estep43291862016-02-03 17:02:09 -0800110 }
111 }
112
113 static void SetUpTestCase() {
114 ui = new MockUI();
115 }
116};
117
118class VerifierSuccessTest : public VerifierTest {
119};
120
121class VerifierFailureTest : public VerifierTest {
122};
123
124TEST_P(VerifierSuccessTest, VerifySucceed) {
125 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_SUCCESS);
126}
127
128TEST_P(VerifierFailureTest, VerifyFailure) {
129 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs), VERIFY_FAILURE);
130}
131
132INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
133 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700134 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
135 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
136 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
137 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
138 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800139
140INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
141 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700142 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
143 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
144 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
145 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
146 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800147
148INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
149 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700150 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
151 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
152 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
153 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
154 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800155
156INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
157 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700158 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
159 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
160 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
161 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800162
163INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
164 ::testing::Values(
Tao Bao5af4b192016-08-01 11:31:43 -0700165 std::vector<std::string>({"random.zip", "v1"}),
166 std::vector<std::string>({"fake-eocd.zip", "v1"}),
167 std::vector<std::string>({"alter-metadata.zip", "v1"}),
168 std::vector<std::string>({"alter-footer.zip", "v1"})));