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 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.spec.AlgorithmParameterSpec;
     27 
     28 import javax.crypto.spec.IvParameterSpec;
     29 import javax.security.auth.DestroyFailedException;
     30 
     31 import junit.framework.TestCase;
     32 
     33 /**
     34  * Tests for <code>KeyStore.PasswordProtection</code> class constructor and methods
     35  *
     36  */
     37 public class KSPasswordProtectionTest extends TestCase {
     38 
     39     /**
     40      * Test for <code>KeyStore.PasswordProtection(char[] password)</code> constructor
     41      * and the following methods
     42      * <code>getPassword()<code>
     43      * <code>destroy()<code>
     44      * <code>isDestroyed()<code>
     45      * Assertions: constructor created new PasswordProtection object
     46      * getPassword() returns password or throws IllegalArgumentException
     47      * if PasswordProtection is destroyed
     48      */
     49     public void testGetPassword() throws DestroyFailedException {
     50         char [] pass = {'a', 'b', 'c'};
     51         KeyStore.PasswordProtection ksPWP = new KeyStore.PasswordProtection(pass);
     52         char [] rPass = ksPWP.getPassword();
     53         assertFalse("PasswordProtection Should not be destroyed", ksPWP.isDestroyed());
     54         assertEquals("Incorrect password length", pass.length, rPass.length);
     55         for (int i = 0; i < pass.length; i++) {
     56             assertEquals("Incorrect password (item: ".concat(Integer.toString(i))
     57                     .concat(")"), pass[i], rPass[i]);
     58         }
     59         ksPWP.destroy();
     60         assertTrue("PasswordProtection must be destroyed", ksPWP.isDestroyed());
     61         try {
     62             ksPWP.getPassword();
     63             fail("IllegalStateException must be thrown because PasswordProtection is destroyed");
     64         } catch (IllegalStateException e) {
     65         }
     66 
     67         try {
     68             ksPWP = new KeyStore.PasswordProtection(null);
     69         } catch (Exception e) {
     70             fail("Unexpected exception for NULL parameter");
     71         }
     72     }
     73 
     74     /**
     75      * Test for <code>KeyStore.PasswordProtection(char[] password, String protectionAlgorithm,
     76      * AlgorithmParameterSpec protectionParameters)</code> constructor
     77      * and the method <code>getProtectionAlgorithm()</code>
     78 
     79      * Assertions: constructor throws NullPointerException if protectionAlgorithm is null.
     80      * getProtectionAlgorithm() returns the protection algorithm passed in the constructor.
     81      */
     82     public void testGetProtectionAlgorithm() throws DestroyFailedException {
     83         char [] pass = {'a', 'b', 'c'};
     84         String protectionAlgorithm = "ThisBeautifulAlgorithm";
     85         AlgorithmParameterSpec protectionParameters = new IvParameterSpec(new byte[]{});
     86         KeyStore.PasswordProtection ksPWP;
     87         try {
     88             ksPWP = new KeyStore.PasswordProtection(
     89                     pass, null /* protectionAlgorithm */, protectionParameters);
     90             fail("Expected null pointer exception");
     91         } catch (NullPointerException expected) {
     92         }
     93         ksPWP = new KeyStore.PasswordProtection(
     94                 pass, protectionAlgorithm, null /* protectionParameters */);
     95         assertSame(protectionAlgorithm, ksPWP.getProtectionAlgorithm());
     96     }
     97 
     98     /**
     99      * Test for <code>KeyStore.PasswordProtection(char[] password, String protectionAlgorithm,
    100      * AlgorithmParameterSpec protectionParameters)</code> constructor
    101      * and the method <code>getProtectionParameters()</code>
    102 
    103      * Assertions: constructor creates new PasswordProtection object, even if protectionParameters
    104      * is null. getProtectionParameterrs() returns the protection algorithm passed in the
    105      * constructor.
    106      */
    107     public void testGetProtectionParameters() throws DestroyFailedException {
    108         char [] pass = {'a', 'b', 'c'};
    109         AlgorithmParameterSpec protectionParameters = new IvParameterSpec(new byte[]{});
    110         KeyStore.PasswordProtection ksPWP =
    111                 new KeyStore.PasswordProtection(
    112                         pass, "protectionAlgorithm", null /* protectionParameters */);
    113         assertNull(ksPWP.getProtectionParameters());
    114         ksPWP = new KeyStore.PasswordProtection(
    115                 pass, "protectionAlgorithm", protectionParameters);
    116         assertSame(protectionParameters, ksPWP.getProtectionParameters());
    117     }
    118 }
    119