1/* 2 * Copyright 2014 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 17package tests.security.cert; 18 19import org.apache.harmony.testframework.serialization.SerializationTest; 20import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 21import java.io.IOException; 22import java.io.OutputStream; 23import java.io.Serializable; 24import java.security.cert.CRLReason; 25import java.security.cert.CertificateRevokedException; 26import java.security.cert.Extension; 27import java.util.Date; 28import java.util.HashMap; 29import java.util.Map; 30import javax.security.auth.x500.X500Principal; 31 32import junit.framework.TestCase; 33 34/** 35 * 36 */ 37public class CertificateRevocationExceptionTest extends TestCase implements SerializableAssert { 38 private CertificateRevokedException getTestException() { 39 HashMap<String, Extension> extensions = new HashMap<String, Extension>(); 40 // REASON_CODE 41 extensions.put("2.5.29.21", getReasonExtension()); 42 extensions.put("2.5.29.24", getInvalidityExtension()); 43 return new CertificateRevokedException( 44 new Date(1199226851000L), 45 CRLReason.CESSATION_OF_OPERATION, 46 new X500Principal("CN=test1"), 47 extensions); 48 } 49 50 private Extension getReasonExtension() { 51 return new Extension() { 52 @Override 53 public String getId() { 54 return "2.5.29.21"; 55 } 56 57 @Override 58 public boolean isCritical() { 59 return false; 60 } 61 62 @Override 63 public byte[] getValue() { 64 return new byte[] {4, 3, 10, 1, 5}; 65 } 66 67 @Override 68 public void encode(OutputStream out) throws IOException { 69 throw new UnsupportedOperationException(); 70 } 71 }; 72 } 73 74 private Extension getInvalidityExtension() { 75 return new Extension() { 76 @Override 77 public String getId() { 78 return "2.5.29.24"; 79 } 80 81 @Override 82 public boolean isCritical() { 83 return false; 84 } 85 86 @Override 87 public byte[] getValue() { 88 return new byte[] { 89 0x18, 0x0F, 0x32, 0x30, 0x31, 0x34, 0x30, 0x31, 0x31, 0x37, 0x30, 0x38, 90 0x33, 0x30, 0x30, 0x39, 0x5a 91 }; 92 } 93 94 @Override 95 public void encode(OutputStream out) throws IOException { 96 throw new UnsupportedOperationException(); 97 } 98 }; 99 } 100 101 public void testGetExtensions() throws Exception { 102 CertificateRevokedException original = getTestException(); 103 Map<String, Extension> extensions = original.getExtensions(); 104 assertNotSame(extensions, original.getExtensions()); 105 106 try { 107 extensions.put("2.2.2.2", getReasonExtension()); 108 fail(); 109 } catch (UnsupportedOperationException expected) { 110 } 111 } 112 113 public void testGetRevocationDate() throws Exception { 114 CertificateRevokedException exception = getTestException(); 115 116 Date firstDate = exception.getRevocationDate(); 117 assertNotSame(firstDate, exception.getRevocationDate()); 118 119 firstDate.setYear(firstDate.getYear() + 1); 120 assertTrue(firstDate.compareTo(exception.getRevocationDate()) > 0); 121 } 122 123 public void testGetInvalidityDate() throws Exception { 124 CertificateRevokedException exception = getTestException(); 125 126 Date firstDate = exception.getInvalidityDate(); 127 assertNotSame(firstDate, exception.getInvalidityDate()); 128 129 firstDate.setYear(firstDate.getYear() + 1); 130 assertTrue(firstDate.compareTo(exception.getInvalidityDate()) > 0); 131 } 132 133 public void testGetAuthorityName() throws Exception { 134 CertificateRevokedException exception = getTestException(); 135 assertEquals(new X500Principal("CN=test1"), exception.getAuthorityName()); 136 } 137 138 /** 139 * serialization/deserialization compatibility. 140 */ 141 public void testSerializationCertificateRevokedExceptionSelf() throws Exception { 142 SerializationTest.verifySelf(getTestException(), this); 143 } 144 145 /** 146 * serialization/deserialization compatibility with RI. 147 */ 148 public void testSerializationCertificateRevokedExceptionCompatability() throws Exception { 149 // create test file (once) 150 // SerializationTest.createGoldenFile("/tmp", this, getTestException()); 151 SerializationTest.verifyGolden(this, getTestException()); 152 } 153 154 @Override 155 public void assertDeserialized(Serializable initial, Serializable deserialized) { 156 assertTrue(initial instanceof CertificateRevokedException); 157 assertTrue(deserialized instanceof CertificateRevokedException); 158 159 CertificateRevokedException expected = (CertificateRevokedException) initial; 160 CertificateRevokedException actual = (CertificateRevokedException) deserialized; 161 162 assertEquals(expected.getInvalidityDate(), actual.getInvalidityDate()); 163 assertNotSame(expected.getInvalidityDate(), actual.getInvalidityDate()); 164 assertEquals(expected.getRevocationDate(), actual.getRevocationDate()); 165 assertNotSame(expected.getRevocationDate(), actual.getRevocationDate()); 166 assertEquals(expected.getRevocationReason(), expected.getRevocationReason()); 167 assertEquals(expected.getAuthorityName(), actual.getAuthorityName()); 168 assertNotSame(expected.getAuthorityName(), actual.getAuthorityName()); 169 170 assertEquals(expected.getExtensions().size(), actual.getExtensions().size()); 171 assertEquals(expected.getExtensions().keySet(), actual.getExtensions().keySet()); 172 } 173} 174