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.AlgorithmParameterGenerator;
     26 import java.security.AlgorithmParameterGeneratorSpi;
     27 import java.security.AlgorithmParameters;
     28 import java.security.InvalidAlgorithmParameterException;
     29 import java.security.NoSuchAlgorithmException;
     30 import java.security.NoSuchProviderException;
     31 import java.security.Provider;
     32 import java.security.SecureRandom;
     33 import java.security.spec.AlgorithmParameterSpec;
     34 
     35 import org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi;
     36 import org.apache.harmony.security.tests.support.SpiEngUtils;
     37 
     38 import junit.framework.TestCase;
     39 
     40 /**
     41  * Tests for <code>AlgorithmParameterGenerator</code> class constructors and
     42  * methods.
     43  *
     44  */
     45 public class AlgorithmParameterGenerator1Test extends TestCase {
     46 
     47     private static String[] invalidValues = SpiEngUtils.invalidValues;
     48     private static String validAlgName = "DSA";
     49     private static String[] algs =  {
     50             "DSA", "dsa", "Dsa", "DsA", "dsA" };
     51 
     52     public static final String srvAlgorithmParameterGenerator = "AlgorithmParameterGenerator";
     53 
     54 
     55     private static String validProviderName = null;
     56 
     57     private static Provider validProvider = null;
     58 
     59     private static boolean DSASupported = false;
     60 
     61     static {
     62         validProvider = SpiEngUtils.isSupport(
     63                 validAlgName,
     64                 srvAlgorithmParameterGenerator);
     65         DSASupported = (validProvider != null);
     66         validProviderName = (DSASupported ? validProvider.getName() : null);
     67     }
     68 
     69     protected AlgorithmParameterGenerator[] createAPGen() {
     70         if (!DSASupported) {
     71             fail(validAlgName + " algorithm is not supported");
     72             return null;
     73         }
     74         AlgorithmParameterGenerator[] apg = new AlgorithmParameterGenerator[3];
     75         try {
     76             apg[0] = AlgorithmParameterGenerator.getInstance(validAlgName);
     77             apg[1] = AlgorithmParameterGenerator.getInstance(validAlgName,
     78                     validProvider);
     79             apg[2] = AlgorithmParameterGenerator.getInstance(validAlgName,
     80                     validProviderName);
     81             return apg;
     82         } catch (Exception e) {
     83             e.printStackTrace();
     84             return null;
     85         }
     86     }
     87 
     88     /**
     89      * Test for <code>getInstance(String algorithm)</code> method
     90      * Assertion:
     91      * throws NullPointerException must be thrown is null
     92      * throws NoSuchAlgorithmException must be thrown if algorithm is not available
     93      *
     94      */
     95     public void testAlgorithmParameterGenerator01()
     96             throws NoSuchAlgorithmException {
     97         try {
     98             AlgorithmParameterGenerator.getInstance(null);
     99             fail("NullPointerException or NoSuchAlgorithmException should be thrown");
    100         } catch (NullPointerException e) {
    101         } catch (NoSuchAlgorithmException e) {
    102         }
    103         for (int i = 0; i < invalidValues.length; i++) {
    104             try {
    105                 AlgorithmParameterGenerator.getInstance(invalidValues[i]);
    106                 fail("NoSuchAlgorithmException should be thrown");
    107             } catch (NoSuchAlgorithmException e) {
    108             }
    109         }
    110     }
    111 
    112     /**
    113      * Test for <code>getInstance(String algorithm)</code> method
    114      * Assertion: returns AlgorithmParameterGenerator instance
    115      * when algorithm is DSA
    116      */
    117     public void testAlgorithmParameterGenerator02()
    118             throws NoSuchAlgorithmException {
    119         if (!DSASupported) {
    120             fail(validAlgName + " algorithm is not supported");
    121             return;
    122         }
    123         AlgorithmParameterGenerator apg;
    124         for (int i = 0; i < algs.length; i++) {
    125             apg = AlgorithmParameterGenerator.getInstance(algs[i]);
    126             assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]);
    127         }
    128     }
    129 
    130     /**
    131      * Test for <code>getInstance(String algorithm, String provider)</code>
    132      * method
    133      * Assertion:
    134      * throws IllegalArgumentException if provider is null or empty
    135      */
    136     public void testAlgorithmParameterGenerator03()
    137             throws NoSuchAlgorithmException, NoSuchProviderException {
    138         if (!DSASupported) {
    139             fail(validAlgName + " algorithm is not supported");
    140             return;
    141         }
    142         String provider = null;
    143         for (int i = 0; i < algs.length; i++) {
    144             try {
    145                 AlgorithmParameterGenerator.getInstance(algs[i], provider);
    146                 fail("IllegalArgumentException must be thrown when provider is null");
    147             } catch (IllegalArgumentException e) {
    148             }
    149             try {
    150                 AlgorithmParameterGenerator.getInstance(algs[i], "");
    151                 fail("IllegalArgumentException must be thrown when provider is empty");
    152             } catch (IllegalArgumentException e) {
    153             }
    154         }
    155     }
    156 
    157     /**
    158      * Test for <code>getInstance(String algorithm, String provider)</code>
    159      * method
    160      * Assertion: throws NoSuchProviderException if provider is not
    161      * available
    162      */
    163     public void testAlgorithmParameterGenerator04()
    164             throws NoSuchAlgorithmException {
    165         if (!DSASupported) {
    166             fail(validAlgName + " algorithm is not supported");
    167             return;
    168         }
    169         for (int i = 0; i < algs.length; i++) {
    170             for (int j = 1; j < invalidValues.length; j++) {
    171                 try {
    172                     AlgorithmParameterGenerator.getInstance(algs[i],
    173                             invalidValues[j]);
    174                     fail("NoSuchProviderException must be thrown (provider: "
    175                             .concat(invalidValues[j]));
    176                 } catch (NoSuchProviderException e) {
    177                 }
    178             }
    179         }
    180     }
    181 
    182     /**
    183      * Test for <code>getInstance(String algorithm, String provider)</code>
    184      * method
    185      * Assertion:
    186      * throws NullPointerException must be thrown is null
    187      * throws NoSuchAlgorithmException must be thrown if algorithm is not available
    188      */
    189     public void testAlgorithmParameterGenerator05()
    190             throws NoSuchProviderException {
    191         if (!DSASupported) {
    192             fail(validAlgName + " algorithm is not supported");
    193             return;
    194         }
    195         try {
    196             AlgorithmParameterGenerator.getInstance(null, validProviderName);
    197             fail("NullPointerException or NoSuchAlgorithmException should be thrown");
    198         } catch (NullPointerException e) {
    199         } catch (NoSuchAlgorithmException e) {
    200         }
    201         for (int i = 0; i < invalidValues.length; i++) {
    202             try {
    203                 AlgorithmParameterGenerator.getInstance(invalidValues[i],
    204                         validProviderName);
    205                 fail("NoSuchAlgorithmException must be thrown when (algorithm: "
    206                         .concat(invalidValues[i].concat(")")));
    207             } catch (NoSuchAlgorithmException e) {
    208             }
    209         }
    210     }
    211 
    212     /**
    213      * Test for <code>getInstance(String algorithm, String provider)</code>
    214      * method
    215      * Assertion: return AlgorithmParameterGenerator
    216      */
    217     public void testAlgorithmParameterGenerator06()
    218             throws NoSuchAlgorithmException, NoSuchProviderException {
    219         if (!DSASupported) {
    220             fail(validAlgName + " algorithm is not supported");
    221             return;
    222         }
    223         AlgorithmParameterGenerator apg;
    224         for (int i = 0; i < algs.length; i++) {
    225             apg = AlgorithmParameterGenerator.getInstance(algs[i],
    226                     validProviderName);
    227             assertEquals("Incorrect algorithm", algs[i], apg.getAlgorithm());
    228             assertEquals("Incorrect provider", apg.getProvider().getName(),
    229                     validProviderName);
    230         }
    231     }
    232 
    233     /**
    234      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    235      * method
    236      * Assertion: throws IllegalArgumentException when provider is null
    237      */
    238     public void testAlgorithmParameterGenerator07()
    239             throws NoSuchAlgorithmException {
    240         if (!DSASupported) {
    241             fail(validAlgName + " algorithm is not supported");
    242             return;
    243         }
    244         Provider provider = null;
    245         for (int i = 0; i < algs.length; i++) {
    246             try {
    247                 AlgorithmParameterGenerator.getInstance(algs[i], provider);
    248                 fail("IllegalArgumentException must be thrown when provider is null");
    249             } catch (IllegalArgumentException e) {
    250             }
    251         }
    252     }
    253 
    254     /**
    255      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    256      * method
    257      * Assertion:
    258      * throws NullPointerException must be thrown is null
    259      * throws NoSuchAlgorithmException must be thrown if algorithm is not available
    260      */
    261     public void testAlgorithmParameterGenerator08() {
    262         if (!DSASupported) {
    263             fail(validAlgName + " algorithm is not supported");
    264             return;
    265         }
    266         try {
    267             AlgorithmParameterGenerator.getInstance(null, validProvider);
    268             fail("NullPointerException or NoSuchAlgorithmException should be thrown");
    269         } catch (NullPointerException e) {
    270         } catch (NoSuchAlgorithmException e) {
    271         }
    272         for (int i = 0; i < invalidValues.length; i++) {
    273             try {
    274                 AlgorithmParameterGenerator.getInstance(invalidValues[i],
    275                         validProvider);
    276                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
    277                         .concat(invalidValues[i]).concat(")"));
    278             } catch (NoSuchAlgorithmException e) {
    279             }
    280         }
    281     }
    282 
    283     /**
    284      * Test for <code>getInstance(String algorithm, Provider provider)</code>
    285      * method
    286      * Assertion: returns AlgorithmParameterGenerator object
    287      */
    288     public void testAlgorithmParameterGenerator09()
    289             throws NoSuchAlgorithmException {
    290         if (!DSASupported) {
    291             fail(validAlgName + " algorithm is not supported");
    292             return;
    293         }
    294         AlgorithmParameterGenerator apg;
    295         for (int i = 0; i < algs.length; i++) {
    296             apg = AlgorithmParameterGenerator.getInstance(algs[i],
    297                     validProvider);
    298             assertEquals("Incorrect algorithm", apg.getAlgorithm(), algs[i]);
    299             assertEquals("Incorrect provider", apg.getProvider(), validProvider);
    300         }
    301     }
    302 
    303     /**
    304      * Test for <code>generateParameters()</code> method
    305      * Assertion: returns AlgorithmParameters object
    306      */
    307     public void testAlgorithmParameterGenerator10()
    308             throws NoSuchAlgorithmException {
    309         if (!DSASupported) {
    310             fail(validAlgName + " algorithm is not supported");
    311             return;
    312         }
    313         AlgorithmParameterGenerator apg = AlgorithmParameterGenerator
    314                 .getInstance(validAlgName);
    315         apg.init(512);
    316         AlgorithmParameters ap = apg.generateParameters();
    317         assertEquals("Incorrect algorithm", ap.getAlgorithm().toUpperCase(),
    318                 apg.getAlgorithm().toUpperCase());
    319     }
    320 
    321     /**
    322      * Test for <code>init(AlgorithmParameterSpec param)</code> and
    323      * <code>init(AlgorithmParameterSpec param, SecureRandom random<code>
    324      * methods
    325      * Assertion: throws InvalidAlgorithmParameterException when param is null
    326      */
    327     public void testAlgorithmParameterGenerator12() {
    328         if (!DSASupported) {
    329             fail(validAlgName + " algorithm is not supported");
    330             return;
    331         }
    332         SecureRandom random = new SecureRandom();
    333         AlgorithmParameterSpec aps = null;
    334         AlgorithmParameterGenerator[] apgs = createAPGen();
    335         assertNotNull("AlgorithmParameterGenerator objects were not created",
    336                 apgs);
    337         for (int i = 0; i < apgs.length; i++) {
    338             try {
    339                 apgs[i].init(aps);
    340                 fail("InvalidAlgorithmParameterException expected for null argument.");
    341             } catch (InvalidAlgorithmParameterException e) {
    342                 //expected
    343             }
    344 
    345             try {
    346                 apgs[i].init(aps, random);
    347                 fail("InvalidAlgorithmParameterException expected for null argument.");
    348             } catch (InvalidAlgorithmParameterException e) {
    349                 //expected
    350             }
    351         }
    352     }
    353 
    354     /**
    355      * Test for <code>AlgorithmParameterGenerator</code> constructor
    356      * Assertion: returns AlgorithmParameterGenerator object
    357      */
    358     public void testConstructor() throws NoSuchAlgorithmException {
    359         if (!DSASupported) {
    360             fail(validAlgName + " algorithm is not supported");
    361             return;
    362         }
    363         AlgorithmParameterGeneratorSpi spi = new MyAlgorithmParameterGeneratorSpi();
    364         AlgorithmParameterGenerator apg =
    365                 new myAlgPG(spi, validProvider, validAlgName);
    366         assertEquals("Incorrect algorithm", apg.getAlgorithm(), validAlgName);
    367         assertEquals("Incorrect provider",apg.getProvider(),validProvider);
    368         try {
    369             apg.init(-10, null);
    370             fail("IllegalArgumentException must be thrown");
    371         } catch (IllegalArgumentException e) {
    372         }
    373 
    374         apg = new myAlgPG(null, null, null);
    375         assertNull("Incorrect algorithm", apg.getAlgorithm());
    376         assertNull("Incorrect provider", apg.getProvider());
    377         try {
    378             apg.init(-10, null);
    379             fail("NullPointerException must be thrown");
    380         } catch (NullPointerException e) {
    381         }
    382     }
    383 }
    384 /**
    385  * Additional class to verify AlgorithmParameterGenerator constructor
    386  */
    387 class myAlgPG extends AlgorithmParameterGenerator {
    388     public myAlgPG(AlgorithmParameterGeneratorSpi spi, Provider prov, String alg) {
    389         super(spi, prov, alg);
    390     }
    391 }
    392