Bo Zhu | 4d31291 | 2018-02-01 19:35:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 agreed 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 | package com.android.server.locksettings.recoverablekeystore.certificate; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import static org.testng.Assert.assertThrows; |
| 22 | import static org.testng.Assert.expectThrows; |
| 23 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 24 | import androidx.test.filters.SmallTest; |
| 25 | import androidx.test.runner.AndroidJUnit4; |
Bo Zhu | 4d31291 | 2018-02-01 19:35:19 -0800 | [diff] [blame] | 26 | |
| 27 | import org.junit.Before; |
| 28 | import org.junit.Test; |
| 29 | import org.junit.runner.RunWith; |
| 30 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 31 | import java.security.cert.CertPath; |
| 32 | import java.security.cert.X509Certificate; |
| 33 | import java.util.List; |
| 34 | |
Bo Zhu | 4d31291 | 2018-02-01 19:35:19 -0800 | [diff] [blame] | 35 | @SmallTest |
| 36 | @RunWith(AndroidJUnit4.class) |
| 37 | public final class CertXmlTest { |
| 38 | |
| 39 | private byte[] certXmlBytes; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() throws Exception { |
| 43 | certXmlBytes = TestData.readTestFile("xml/valid-cert-file.xml"); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void parse_succeeds() throws Exception { |
| 48 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 49 | assertThat(certXml.getSerial()).isEqualTo(1000L); |
Bo Zhu | 4d31291 | 2018-02-01 19:35:19 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Test |
| 53 | public void parse_succeedsIfNoIntermediateCerts() throws Exception { |
| 54 | CertXml certXml = |
| 55 | CertXml.parse(TestData.readTestFile("xml/valid-cert-file-no-intermediates.xml")); |
| 56 | assertThat(certXml.getAllIntermediateCerts()).isEmpty(); |
| 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void parse_checkIntermediateCerts() throws Exception { |
| 61 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 62 | List<X509Certificate> intermediateCerts = certXml.getAllIntermediateCerts(); |
| 63 | assertThat(intermediateCerts) |
| 64 | .containsExactly(TestData.INTERMEDIATE_CA_1, TestData.INTERMEDIATE_CA_2) |
| 65 | .inOrder(); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void parse_checkEndpointCerts() throws Exception { |
| 70 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 71 | List<X509Certificate> endpointCerts = certXml.getAllEndpointCerts(); |
| 72 | assertThat(endpointCerts).hasSize(3); |
| 73 | assertThat(endpointCerts).containsAllOf(TestData.LEAF_CERT_1, TestData.LEAF_CERT_2); |
| 74 | } |
| 75 | |
| 76 | @Test |
Bo Zhu | 8f24d7d | 2019-02-15 18:12:41 -0800 | [diff] [blame^] | 77 | public void parse_doesNotThrowIfNoRefreshInterval() throws Exception { |
| 78 | CertXml.parse( |
| 79 | TestData.readTestFile( |
| 80 | "xml/valid-cert-file-no-refresh-interval.xml")); |
| 81 | } |
| 82 | |
| 83 | @Test |
Bo Zhu | 4d31291 | 2018-02-01 19:35:19 -0800 | [diff] [blame] | 84 | public void parse_throwsIfNoEndpointCert() throws Exception { |
| 85 | CertParsingException expected = |
| 86 | expectThrows( |
| 87 | CertParsingException.class, |
| 88 | () -> |
| 89 | CertXml.parse( |
| 90 | TestData.readTestFile( |
| 91 | "xml/invalid-cert-file-no-endpoint-cert.xml"))); |
| 92 | assertThat(expected.getMessage()).contains("at least one"); |
| 93 | } |
| 94 | |
| 95 | @Test |
Bo Zhu | 4d31291 | 2018-02-01 19:35:19 -0800 | [diff] [blame] | 96 | public void parse_throwsIfNoSerial() throws Exception { |
| 97 | CertParsingException expected = |
| 98 | expectThrows( |
| 99 | CertParsingException.class, |
| 100 | () -> |
| 101 | CertXml.parse( |
| 102 | TestData.readTestFile( |
| 103 | "xml/invalid-cert-file-no-serial.xml"))); |
| 104 | assertThat(expected.getMessage()).contains("exactly one"); |
| 105 | } |
| 106 | |
| 107 | @Test |
Bo Zhu | 4d31291 | 2018-02-01 19:35:19 -0800 | [diff] [blame] | 108 | public void parse_throwsIfTwoSerials() throws Exception { |
| 109 | CertParsingException expected = |
| 110 | expectThrows( |
| 111 | CertParsingException.class, |
| 112 | () -> |
| 113 | CertXml.parse( |
| 114 | TestData.readTestFile( |
| 115 | "xml/invalid-cert-file-two-serials.xml"))); |
| 116 | assertThat(expected.getMessage()).contains("exactly one node"); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void parseAndValidateAllCerts_succeeds() throws Exception { |
| 121 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 122 | for (int index = 0; index < certXml.getAllEndpointCerts().size(); index++) { |
| 123 | assertThat( |
| 124 | certXml.getEndpointCert( |
| 125 | index, TestData.DATE_ALL_CERTS_VALID, TestData.ROOT_CA_TRUSTED)) |
| 126 | .isNotNull(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void parseAndValidate_returnsExpectedCertPath() throws Exception { |
| 132 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 133 | CertPath certPath = |
| 134 | certXml.getEndpointCert( |
| 135 | /*index=*/ 1, // TestData.LEAF_CERT_2 |
| 136 | TestData.DATE_ALL_CERTS_VALID, |
| 137 | TestData.ROOT_CA_TRUSTED); |
| 138 | assertThat(certPath.getCertificates()) |
| 139 | .containsExactly( |
| 140 | TestData.LEAF_CERT_2, TestData.INTERMEDIATE_CA_2, |
| 141 | TestData.INTERMEDIATE_CA_1) |
| 142 | .inOrder(); |
| 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void validateCert_throwsIfRootCertWithDifferentCommonName() throws Exception { |
| 147 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 148 | assertThrows( |
| 149 | CertValidationException.class, |
| 150 | () -> |
| 151 | certXml.getEndpointCert( |
| 152 | /*index=*/ 0, // TestData.LEAF_CERT_1 |
| 153 | TestData.DATE_ALL_CERTS_VALID, |
| 154 | TestData.ROOT_CA_DIFFERENT_COMMON_NAME)); |
| 155 | } |
| 156 | |
| 157 | @Test |
| 158 | public void validateCert_throwsIfRootCertWithDifferentPublicKey() throws Exception { |
| 159 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 160 | assertThrows( |
| 161 | CertValidationException.class, |
| 162 | () -> |
| 163 | certXml.getEndpointCert( |
| 164 | /*index=*/ 0, // TestData.LEAF_CERT_1 |
| 165 | TestData.DATE_ALL_CERTS_VALID, |
| 166 | TestData.ROOT_CA_DIFFERENT_KEY)); |
| 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void validateCert_throwsIfExpired() throws Exception { |
| 171 | CertXml certXml = CertXml.parse(certXmlBytes); |
| 172 | assertThrows( |
| 173 | CertValidationException.class, |
| 174 | () -> |
| 175 | certXml.getEndpointCert( |
| 176 | /*index=*/ 1, // TestData.LEAF_CERT_2 |
| 177 | TestData.DATE_LEAF_CERT_2_EXPIRED, |
| 178 | TestData.ROOT_CA_TRUSTED)); |
| 179 | } |
| 180 | } |