blob: 07a8c960f7b12c196e9d0ecaf8c42548876e1d36 [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
Jed Estep43291862016-02-03 17:02:09 -080025#include <string>
26#include <vector>
27
Tao Bao5e535012017-03-16 17:37:38 -070028#include <android-base/file.h>
Jed Estep43291862016-02-03 17:02:09 -080029#include <android-base/stringprintf.h>
Tao Bao5e535012017-03-16 17:37:38 -070030#include <android-base/test_utils.h>
Jed Estep43291862016-02-03 17:02:09 -080031
Jed Estepb8a693b2016-03-09 17:51:34 -080032#include "common/test_constants.h"
Tianjie Xu8cf5c8f2016-09-08 20:10:11 -070033#include "otautil/SysUtil.h"
Jed Estep43291862016-02-03 17:02:09 -080034#include "verifier.h"
35
Jed Estep43291862016-02-03 17:02:09 -080036class VerifierTest : public testing::TestWithParam<std::vector<std::string>> {
Tao Bao5e535012017-03-16 17:37:38 -070037 protected:
38 void SetUp() override {
39 std::vector<std::string> args = GetParam();
40 std::string package = from_testdata_base(args[0]);
41 if (sysMapFile(package.c_str(), &memmap) != 0) {
42 FAIL() << "Failed to mmap " << package << ": " << strerror(errno) << "\n";
Jed Estep43291862016-02-03 17:02:09 -080043 }
44
Tao Bao5e535012017-03-16 17:37:38 -070045 for (auto it = ++args.cbegin(); it != args.cend(); ++it) {
46 std::string public_key_file = from_testdata_base("testkey_" + *it + ".txt");
47 ASSERT_TRUE(load_keys(public_key_file.c_str(), certs));
Jed Estep43291862016-02-03 17:02:09 -080048 }
Tao Bao5e535012017-03-16 17:37:38 -070049 }
50
51 MemMapping memmap;
52 std::vector<Certificate> certs;
Jed Estep43291862016-02-03 17:02:09 -080053};
54
55class VerifierSuccessTest : public VerifierTest {
56};
57
58class VerifierFailureTest : public VerifierTest {
59};
60
Tao Bao3116ce42017-03-16 17:37:38 -070061TEST(VerifierTest, load_keys_multiple_keys) {
62 std::string testkey_v4;
63 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
64
65 std::string testkey_v3;
66 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v3.txt"), &testkey_v3));
67
68 std::string keys = testkey_v4 + "," + testkey_v3 + "," + testkey_v4;
69 TemporaryFile key_file1;
70 ASSERT_TRUE(android::base::WriteStringToFile(keys, key_file1.path));
71 std::vector<Certificate> certs;
72 ASSERT_TRUE(load_keys(key_file1.path, certs));
73 ASSERT_EQ(3U, certs.size());
74}
75
76TEST(VerifierTest, load_keys_invalid_keys) {
77 std::vector<Certificate> certs;
78 ASSERT_FALSE(load_keys("/doesntexist", certs));
79
80 // Empty file.
81 TemporaryFile key_file1;
82 ASSERT_FALSE(load_keys(key_file1.path, certs));
83
84 // Invalid contents.
85 ASSERT_TRUE(android::base::WriteStringToFile("invalid", key_file1.path));
86 ASSERT_FALSE(load_keys(key_file1.path, certs));
87
88 std::string testkey_v4;
89 ASSERT_TRUE(android::base::ReadFileToString(from_testdata_base("testkey_v4.txt"), &testkey_v4));
90
91 // Invalid key version: "v4 ..." => "v6 ...".
92 std::string invalid_key2(testkey_v4);
93 invalid_key2[1] = '6';
94 TemporaryFile key_file2;
95 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key2, key_file2.path));
96 ASSERT_FALSE(load_keys(key_file2.path, certs));
97
98 // Invalid key content: inserted extra bytes ",2209831334".
99 std::string invalid_key3(testkey_v4);
100 invalid_key3.insert(invalid_key2.size() - 2, ",2209831334");
101 TemporaryFile key_file3;
102 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key3, key_file3.path));
103 ASSERT_FALSE(load_keys(key_file3.path, certs));
104
105 // Invalid key: the last key must not end with an extra ','.
106 std::string invalid_key4 = testkey_v4 + ",";
107 TemporaryFile key_file4;
108 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key4, key_file4.path));
109 ASSERT_FALSE(load_keys(key_file4.path, certs));
110
111 // Invalid key separator.
112 std::string invalid_key5 = testkey_v4 + ";" + testkey_v4;
113 TemporaryFile key_file5;
114 ASSERT_TRUE(android::base::WriteStringToFile(invalid_key5, key_file5.path));
115 ASSERT_FALSE(load_keys(key_file5.path, certs));
116}
117
Jed Estep43291862016-02-03 17:02:09 -0800118TEST_P(VerifierSuccessTest, VerifySucceed) {
Tao Bao5e535012017-03-16 17:37:38 -0700119 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_SUCCESS);
Jed Estep43291862016-02-03 17:02:09 -0800120}
121
122TEST_P(VerifierFailureTest, VerifyFailure) {
Tao Bao5e535012017-03-16 17:37:38 -0700123 ASSERT_EQ(verify_file(memmap.addr, memmap.length, certs, nullptr), VERIFY_FAILURE);
Jed Estep43291862016-02-03 17:02:09 -0800124}
125
126INSTANTIATE_TEST_CASE_P(SingleKeySuccess, VerifierSuccessTest,
Tao Bao5e535012017-03-16 17:37:38 -0700127 ::testing::Values(
128 std::vector<std::string>({"otasigned_v1.zip", "v1"}),
129 std::vector<std::string>({"otasigned_v2.zip", "v2"}),
130 std::vector<std::string>({"otasigned_v3.zip", "v3"}),
131 std::vector<std::string>({"otasigned_v4.zip", "v4"}),
132 std::vector<std::string>({"otasigned_v5.zip", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800133
134INSTANTIATE_TEST_CASE_P(MultiKeySuccess, VerifierSuccessTest,
Tao Bao5e535012017-03-16 17:37:38 -0700135 ::testing::Values(
136 std::vector<std::string>({"otasigned_v1.zip", "v1", "v2"}),
137 std::vector<std::string>({"otasigned_v2.zip", "v5", "v2"}),
138 std::vector<std::string>({"otasigned_v3.zip", "v5", "v1", "v3"}),
139 std::vector<std::string>({"otasigned_v4.zip", "v5", "v1", "v4"}),
140 std::vector<std::string>({"otasigned_v5.zip", "v4", "v1", "v5"})));
Jed Estep43291862016-02-03 17:02:09 -0800141
142INSTANTIATE_TEST_CASE_P(WrongKey, VerifierFailureTest,
Tao Bao5e535012017-03-16 17:37:38 -0700143 ::testing::Values(
144 std::vector<std::string>({"otasigned_v1.zip", "v2"}),
145 std::vector<std::string>({"otasigned_v2.zip", "v1"}),
146 std::vector<std::string>({"otasigned_v3.zip", "v5"}),
147 std::vector<std::string>({"otasigned_v4.zip", "v5"}),
148 std::vector<std::string>({"otasigned_v5.zip", "v3"})));
Jed Estep43291862016-02-03 17:02:09 -0800149
150INSTANTIATE_TEST_CASE_P(WrongHash, VerifierFailureTest,
Tao Bao5e535012017-03-16 17:37:38 -0700151 ::testing::Values(
152 std::vector<std::string>({"otasigned_v1.zip", "v3"}),
153 std::vector<std::string>({"otasigned_v2.zip", "v4"}),
154 std::vector<std::string>({"otasigned_v3.zip", "v1"}),
155 std::vector<std::string>({"otasigned_v4.zip", "v2"})));
Jed Estep43291862016-02-03 17:02:09 -0800156
157INSTANTIATE_TEST_CASE_P(BadPackage, VerifierFailureTest,
Tao Bao5e535012017-03-16 17:37:38 -0700158 ::testing::Values(
159 std::vector<std::string>({"random.zip", "v1"}),
160 std::vector<std::string>({"fake-eocd.zip", "v1"}),
161 std::vector<std::string>({"alter-metadata.zip", "v1"}),
162 std::vector<std::string>({"alter-footer.zip", "v1"})));