Home | History | Annotate | Download | only in security
      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 
     23 package org.apache.harmony.security.tests.java.security;
     24 
     25 import java.security.KeyPair;
     26 import java.security.PrivateKey;
     27 import java.security.PublicKey;
     28 import java.security.spec.InvalidKeySpecException;
     29 
     30 import junit.framework.TestCase;
     31 
     32 /**
     33  * Tests for fields and methods of class <code>KeyPair</code>
     34  *
     35  */
     36 public class KeyPairTest extends TestCase {
     37 
     38     private static class TestKeyPair {
     39         static PublicKey getPublic() {
     40             return new PublicKey() {
     41                 public String getAlgorithm() {
     42                     return "never mind";
     43                 }
     44                 public String getFormat() {
     45                     return "never mind";
     46                 }
     47                 public byte[] getEncoded() {
     48                     return null;
     49                 }
     50             };
     51         }
     52         static PrivateKey getPrivate() {
     53             return new PrivateKey() {
     54                 public String getAlgorithm() {
     55                     return "never mind";
     56                 }
     57                 public String getFormat() {
     58                     return "never mind";
     59                 }
     60                 public byte[] getEncoded() {
     61                     return null;
     62                 }
     63             };
     64         }
     65     }
     66 
     67 
     68     /**
     69      * Test #1 for <code>KeyPair(PublicKey, PrivateKey)</code> constructor<br>
     70      * Assertion: creates new <code>KeyPair</code> instance using valid
     71      * parameters (both <code>null</code>)
     72      */
     73     public final void testKeyPair01() {
     74         Object kp = new KeyPair(null, null);
     75         assertTrue(kp instanceof KeyPair);
     76 
     77         kp = new KeyPair(null, TestKeyPair.getPrivate());
     78         assertTrue(kp instanceof KeyPair);
     79         kp = new KeyPair(TestKeyPair.getPublic(), null);
     80         assertTrue(kp instanceof KeyPair);
     81     }
     82 
     83     /**
     84      * Test #2 for <code>KeyPair(PublicKey, PrivateKey)</code> constructor<br>
     85      * Assertion: creates new <code>KeyPair</code> instance using valid
     86      * parameters (both valid keys)
     87      * @throws InvalidKeySpecException
     88      */
     89     public final void testKeyPair02() throws InvalidKeySpecException {
     90         Object kp = new KeyPair(TestKeyPair.getPublic(), TestKeyPair.getPrivate());
     91         assertTrue(kp instanceof KeyPair);
     92     }
     93 
     94     /**
     95      * Test #1 for <code>getPrivate()</code> method<br>
     96      * Assertion: returns private key (<code>null</code> in this case)
     97      */
     98     public final void testGetPrivate01() {
     99         KeyPair kp = new KeyPair(null, null);
    100         assertNull(kp.getPrivate());
    101     }
    102 
    103     /**
    104      * Test #2 for <code>getPrivate()</code> method<br>
    105      * Assertion: returns private key (valid private key in this case)
    106      * @throws InvalidKeySpecException
    107      */
    108     public final void testGetPrivate02() throws InvalidKeySpecException {
    109         PrivateKey pk = TestKeyPair.getPrivate();
    110         KeyPair kp = new KeyPair(null, pk);
    111         assertSame(pk, kp.getPrivate());
    112     }
    113 
    114     /**
    115      * Test #1 for <code>getPublic()</code> method<br>
    116      * Assertion: returns public key (<code>null</code> in this case)
    117      */
    118     public final void testGetPublic01() {
    119         KeyPair kp = new KeyPair(null, null);
    120         assertNull(kp.getPublic());
    121     }
    122 
    123     /**
    124      * Test #2 for <code>getPublic()</code> method<br>
    125      * Assertion: returns public key (valid public key in this case)
    126      * @throws InvalidKeySpecException
    127      */
    128     public final void testGetPublic02() throws InvalidKeySpecException {
    129         PublicKey pk = TestKeyPair.getPublic();
    130         KeyPair kp = new KeyPair(pk, null);
    131         assertSame(pk, kp.getPublic());
    132     }
    133 
    134 }
    135