1 /* 2 * Copyright (C) 2008 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 android.net.http.cts; 18 19 import java.math.BigInteger; 20 import java.security.InvalidKeyException; 21 import java.security.NoSuchAlgorithmException; 22 import java.security.NoSuchProviderException; 23 import java.security.Principal; 24 import java.security.PublicKey; 25 import java.security.SignatureException; 26 import java.security.cert.CertificateEncodingException; 27 import java.security.cert.CertificateException; 28 import java.security.cert.CertificateExpiredException; 29 import java.security.cert.CertificateNotYetValidException; 30 import java.security.cert.X509Certificate; 31 import java.text.DateFormat; 32 import java.util.Date; 33 import java.util.Set; 34 35 import junit.framework.TestCase; 36 import android.net.http.SslCertificate; 37 import android.net.http.SslCertificate.DName; 38 import android.os.Bundle; 39 40 public class SslCertificateTest extends TestCase { 41 testConstructor()42 public void testConstructor() { 43 // new the SslCertificate instance 44 String date = DateFormat.getInstance().format(new Date()); 45 new SslCertificate("c=129", "e=weji", date, date); 46 47 // new the SslCertificate instance 48 new SslCertificate(new MockX509Certificate()); 49 50 } 51 52 class MockX509Certificate extends X509Certificate { 53 54 @Override checkValidity()55 public void checkValidity() throws CertificateExpiredException, 56 CertificateNotYetValidException { 57 } 58 59 @Override checkValidity(Date date)60 public void checkValidity(Date date) throws CertificateExpiredException, 61 CertificateNotYetValidException { 62 } 63 64 @Override getBasicConstraints()65 public int getBasicConstraints() { 66 return 0; 67 } 68 69 @Override getIssuerDN()70 public Principal getIssuerDN() { 71 return new MockPrincipal(); 72 } 73 74 @Override getIssuerUniqueID()75 public boolean[] getIssuerUniqueID() { 76 return null; 77 } 78 79 @Override getKeyUsage()80 public boolean[] getKeyUsage() { 81 return null; 82 } 83 84 @Override getNotAfter()85 public Date getNotAfter() { 86 return new Date(System.currentTimeMillis()); 87 } 88 89 @Override getNotBefore()90 public Date getNotBefore() { 91 return new Date(System.currentTimeMillis() - 1000); 92 } 93 94 @Override getSerialNumber()95 public BigInteger getSerialNumber() { 96 return null; 97 } 98 99 @Override getSigAlgName()100 public String getSigAlgName() { 101 return null; 102 } 103 104 @Override getSigAlgOID()105 public String getSigAlgOID() { 106 return null; 107 } 108 109 @Override getSigAlgParams()110 public byte[] getSigAlgParams() { 111 return null; 112 } 113 114 @Override getSignature()115 public byte[] getSignature() { 116 return null; 117 } 118 119 @Override getSubjectDN()120 public Principal getSubjectDN() { 121 return new MockPrincipal(); 122 } 123 124 class MockPrincipal implements Principal { getName()125 public String getName() { 126 return null; 127 } 128 } 129 @Override getSubjectUniqueID()130 public boolean[] getSubjectUniqueID() { 131 return null; 132 } 133 134 @Override getTBSCertificate()135 public byte[] getTBSCertificate() throws CertificateEncodingException { 136 return null; 137 } 138 139 @Override getVersion()140 public int getVersion() { 141 return 0; 142 } 143 144 @Override getEncoded()145 public byte[] getEncoded() throws CertificateEncodingException { 146 return null; 147 } 148 149 @Override getPublicKey()150 public PublicKey getPublicKey() { 151 return null; 152 } 153 154 @Override toString()155 public String toString() { 156 return null; 157 } 158 159 @Override verify(PublicKey key)160 public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, 161 InvalidKeyException, NoSuchProviderException, SignatureException { 162 } 163 164 @Override verify(PublicKey key, String sigProvider)165 public void verify(PublicKey key, String sigProvider) throws CertificateException, 166 NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, 167 SignatureException { 168 } 169 getCriticalExtensionOIDs()170 public Set<String> getCriticalExtensionOIDs() { 171 return null; 172 } 173 getExtensionValue(String oid)174 public byte[] getExtensionValue(String oid) { 175 return null; 176 } 177 getNonCriticalExtensionOIDs()178 public Set<String> getNonCriticalExtensionOIDs() { 179 return null; 180 } 181 hasUnsupportedCriticalExtension()182 public boolean hasUnsupportedCriticalExtension() { 183 return false; 184 } 185 } 186 testState()187 public void testState() { 188 // set the expected value 189 190 Date date1 = new Date(System.currentTimeMillis() - 1000); 191 Date date2 = new Date(System.currentTimeMillis()); 192 SslCertificate ssl = new SslCertificate("c=129", "e=weji", date1, date2); 193 Bundle saved = SslCertificate.saveState(ssl); 194 assertTrue(saved.size() == 4); 195 196 assertNotNull(saved.getString("issued-to")); 197 assertNotNull(saved.getString("issued-by")); 198 assertNotNull(saved.getString("valid-not-before")); 199 assertNotNull(saved.getString("valid-not-after")); 200 assertNull(SslCertificate.saveState(null)); 201 202 SslCertificate restored = SslCertificate.restoreState(saved); 203 assertEquals(ssl.getValidNotAfter(), restored.getValidNotAfter()); 204 assertEquals(ssl.getValidNotBefore(), restored.getValidNotBefore()); 205 } 206 testSslCertificate()207 public void testSslCertificate() { 208 209 final String TO = "c=ccc,o=testOName,ou=testUName,cn=testCName"; 210 final String BY = "e=aeei,c=adb,o=testOName,ou=testUName,cn=testCName"; 211 // new the SslCertificate instance 212 Date date1 = new Date(System.currentTimeMillis() - 1000); 213 Date date2 = new Date(System.currentTimeMillis()); 214 SslCertificate ssl = new SslCertificate(TO, BY, date1, date2); 215 DName issuedTo = ssl.getIssuedTo(); 216 DName issuedBy = ssl.getIssuedBy(); 217 218 assertEquals("testCName", issuedTo.getCName()); 219 assertEquals(TO, issuedTo.getDName()); 220 assertEquals("testOName", issuedTo.getOName()); 221 assertEquals("testUName", issuedTo.getUName()); 222 223 assertEquals("testCName", issuedBy.getCName()); 224 assertEquals(BY, issuedBy.getDName()); 225 assertEquals("testOName", issuedBy.getOName()); 226 assertEquals("testUName", issuedBy.getUName()); 227 228 assertEquals(date1, ssl.getValidNotBeforeDate()); 229 assertEquals(date2, ssl.getValidNotAfterDate()); 230 final String EXPECTED = "Issued to: c=ccc,o=testOName,ou=testUName,cn=testCName;\n" 231 + "Issued by: e=aeei,c=adb,o=testOName,ou=testUName,cn=testCName;\n"; 232 assertEquals(EXPECTED, ssl.toString()); 233 } 234 235 } 236