1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /** 19 * @author Vera Y. Petrashkova 20 * @version $Revision$ 21 */ 22 23 package org.apache.harmony.security.tests.java.security; 24 25 import java.security.KeyStore; 26 import java.security.cert.Certificate; 27 import java.util.HashSet; 28 import java.util.Set; 29 30 import org.apache.harmony.security.tests.support.cert.MyCertificate; 31 32 33 import junit.framework.TestCase; 34 35 /** 36 * Tests for <code>KeyStore.TrustedCertificateEntry</code> class constructor and methods 37 * 38 */ 39 40 public class KSTrustedCertificateEntryTest extends TestCase { 41 42 /** 43 * Test for <codfe>KeyStore.TrustedCertificateEntry(Certificate trustCert)</code> 44 * constructor 45 * Assertion: throws NullPointerException when trustCert is null 46 */ 47 public void testTrustedCertificateEntry() { 48 Certificate cert = null; 49 try { 50 new KeyStore.TrustedCertificateEntry(cert); 51 fail("NullPointerException must be thrown when trustCert is null"); 52 } catch (NullPointerException e) { 53 } 54 55 cert = new MyCertificate("TEST", new byte[10]); 56 try { 57 KeyStore.TrustedCertificateEntry ksTCE = new KeyStore.TrustedCertificateEntry(cert); 58 assertNotNull(ksTCE); 59 assertTrue(ksTCE instanceof KeyStore.TrustedCertificateEntry); 60 } catch (Exception e) { 61 fail("Unexpected exception was thrown when trustCert is not null"); 62 } 63 } 64 65 /** 66 * Test for <code>SecretKeyEntry(SecretKey secretKey, Set<Attribute> attributes)</code> 67 * constructor 68 * Assertion: throws NullPointerException when attributes is null 69 */ 70 public void testSecretKeyEntry_nullAttributes() { 71 Certificate cert = new MyCertificate("TEST", new byte[10]); 72 try { 73 new KeyStore.TrustedCertificateEntry(cert, null /* attributes */); 74 fail("NullPointerException must be thrown when attributes is null"); 75 } catch(NullPointerException expected) { 76 } 77 } 78 79 /** 80 * Test for <codfe>getTrustedCertificate()</code> method 81 * Assertion: returns trusted Certificate from goven entry 82 */ 83 public void testGetTrustedCertificate() { 84 Certificate cert = new MyCertificate("TEST", new byte[10]); 85 KeyStore.TrustedCertificateEntry ksTCE = 86 new KeyStore.TrustedCertificateEntry(cert); 87 assertEquals("Incorrect certificate", cert, ksTCE.getTrustedCertificate()); 88 } 89 90 /** 91 * Test for <code>getAttributes()</code> method 92 * Assertion: returns the attributes specified in the constructor, as an unmodifiable set 93 */ 94 public void testGetAttributes() { 95 Certificate cert = new MyCertificate("TEST", new byte[10]); 96 final String attributeName = "theAttributeName"; 97 KeyStore.Entry.Attribute myAttribute = new KeyStore.Entry.Attribute() { 98 @Override 99 public String getName() { 100 return attributeName; 101 } 102 103 @Override 104 public String getValue() { 105 return null; 106 } 107 }; 108 Set<KeyStore.Entry.Attribute> attributeSet = new HashSet<KeyStore.Entry.Attribute>(); 109 attributeSet.add(myAttribute); 110 111 KeyStore.TrustedCertificateEntry ksTCE = 112 new KeyStore.TrustedCertificateEntry(cert, attributeSet); 113 Set<KeyStore.Entry.Attribute> returnedAttributeSet = ksTCE.getAttributes(); 114 assertEquals(attributeSet, returnedAttributeSet); 115 // Adding an element to the original set is OK. 116 attributeSet.add(myAttribute); 117 // The returned set is unmodifiabled. 118 try { 119 returnedAttributeSet.add(myAttribute); 120 fail("The returned set of attributed should be unmodifiable"); 121 } catch (UnsupportedOperationException expected) { 122 } 123 } 124 125 /** 126 * Test for <codfe>toString()</code> method 127 * Assertion: returns non null string 128 */ 129 public void testToString() { 130 Certificate cert = new MyCertificate("TEST", new byte[10]); 131 KeyStore.TrustedCertificateEntry ksTCE = 132 new KeyStore.TrustedCertificateEntry(cert); 133 assertNotNull("toString() returns null string", ksTCE.toString()); 134 } 135 } 136