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 Vladimir N. Molotkov 20* @version $Revision$ 21*/ 22 23package org.apache.harmony.security.tests.java.security; 24 25import dalvik.annotation.TestTargetClass; 26import dalvik.annotation.TestTargets; 27import dalvik.annotation.TestLevel; 28import dalvik.annotation.TestTargetNew; 29 30import java.security.KeyPair; 31import java.security.PrivateKey; 32import java.security.PublicKey; 33import java.security.spec.InvalidKeySpecException; 34 35import junit.framework.TestCase; 36@TestTargetClass( KeyPair.class) 37/** 38 * Tests for fields and methods of class <code>KeyPair</code> 39 * 40 */ 41public class KeyPairTest extends TestCase { 42 43 private static class TestKeyPair { 44 static PublicKey getPublic() { 45 return new PublicKey() { 46 public String getAlgorithm() { 47 return "never mind"; 48 } 49 public String getFormat() { 50 return "never mind"; 51 } 52 public byte[] getEncoded() { 53 return null; 54 } 55 }; 56 } 57 static PrivateKey getPrivate() { 58 return new PrivateKey() { 59 public String getAlgorithm() { 60 return "never mind"; 61 } 62 public String getFormat() { 63 return "never mind"; 64 } 65 public byte[] getEncoded() { 66 return null; 67 } 68 }; 69 } 70 } 71 72 73 /** 74 * Test #1 for <code>KeyPair(PublicKey, PrivateKey)</code> constructor<br> 75 * Assertion: creates new <code>KeyPair</code> instance using valid 76 * parameters (both <code>null</code>) 77 */ 78 @TestTargetNew( 79 level = TestLevel.PARTIAL_COMPLETE, 80 notes = "Verification when parameter is null", 81 method = "KeyPair", 82 args = {java.security.PublicKey.class, java.security.PrivateKey.class} 83 ) 84 public final void testKeyPair01() { 85 Object kp = new KeyPair(null, null); 86 assertTrue(kp instanceof KeyPair); 87 88 kp = new KeyPair(null, TestKeyPair.getPrivate()); 89 assertTrue(kp instanceof KeyPair); 90 kp = new KeyPair(TestKeyPair.getPublic(), null); 91 assertTrue(kp instanceof KeyPair); 92 } 93 94 /** 95 * Test #2 for <code>KeyPair(PublicKey, PrivateKey)</code> constructor<br> 96 * Assertion: creates new <code>KeyPair</code> instance using valid 97 * parameters (both valid keys) 98 * @throws InvalidKeySpecException 99 */ 100 @TestTargetNew( 101 level = TestLevel.PARTIAL_COMPLETE, 102 notes = "Verification when parameter is not null", 103 method = "KeyPair", 104 args = {java.security.PublicKey.class, java.security.PrivateKey.class} 105 ) 106 public final void testKeyPair02() throws InvalidKeySpecException { 107 Object kp = new KeyPair(TestKeyPair.getPublic(), TestKeyPair.getPrivate()); 108 assertTrue(kp instanceof KeyPair); 109 } 110 111 /** 112 * Test #1 for <code>getPrivate()</code> method<br> 113 * Assertion: returns private key (<code>null</code> in this case) 114 */ 115 @TestTargetNew( 116 level = TestLevel.PARTIAL_COMPLETE, 117 notes = "", 118 method = "getPrivate", 119 args = {} 120 ) 121 public final void testGetPrivate01() { 122 KeyPair kp = new KeyPair(null, null); 123 assertNull(kp.getPrivate()); 124 } 125 126 /** 127 * Test #2 for <code>getPrivate()</code> method<br> 128 * Assertion: returns private key (valid private key in this case) 129 * @throws InvalidKeySpecException 130 */ 131 @TestTargetNew( 132 level = TestLevel.PARTIAL_COMPLETE, 133 notes = "", 134 method = "getPrivate", 135 args = {} 136 ) 137 public final void testGetPrivate02() throws InvalidKeySpecException { 138 PrivateKey pk = TestKeyPair.getPrivate(); 139 KeyPair kp = new KeyPair(null, pk); 140 assertSame(pk, kp.getPrivate()); 141 } 142 143 /** 144 * Test #1 for <code>getPublic()</code> method<br> 145 * Assertion: returns public key (<code>null</code> in this case) 146 */ 147 @TestTargetNew( 148 level = TestLevel.PARTIAL_COMPLETE, 149 notes = "", 150 method = "getPublic", 151 args = {} 152 ) 153 public final void testGetPublic01() { 154 KeyPair kp = new KeyPair(null, null); 155 assertNull(kp.getPublic()); 156 } 157 158 /** 159 * Test #2 for <code>getPublic()</code> method<br> 160 * Assertion: returns public key (valid public key in this case) 161 * @throws InvalidKeySpecException 162 */ 163 @TestTargetNew( 164 level = TestLevel.PARTIAL_COMPLETE, 165 notes = "", 166 method = "getPublic", 167 args = {} 168 ) 169 public final void testGetPublic02() throws InvalidKeySpecException { 170 PublicKey pk = TestKeyPair.getPublic(); 171 KeyPair kp = new KeyPair(pk, null); 172 assertSame(pk, kp.getPublic()); 173 } 174 175} 176