Home | History | Annotate | Download | only in interfaces
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package tests.security.interfaces;
     18 
     19 import dalvik.annotation.TestLevel;
     20 import dalvik.annotation.TestTargetNew;
     21 import dalvik.annotation.TestTargetClass;
     22 
     23 import junit.framework.TestCase;
     24 
     25 import java.math.BigInteger;
     26 import java.security.interfaces.DSAKeyPairGenerator;
     27 import java.security.interfaces.DSAParams;
     28 import java.security.SecureRandom;
     29 import java.security.spec.DSAParameterSpec;
     30 import java.security.InvalidParameterException;
     31 
     32 import org.apache.harmony.security.tests.support.interfaces.DSAKeyPairGeneratorImpl;
     33 
     34 @TestTargetClass(DSAKeyPairGenerator.class)
     35 public class DSAKeyPairGeneratorTest extends TestCase {
     36 
     37     private final BigInteger p = new BigInteger("4");
     38     private final BigInteger q = BigInteger.TEN;
     39     private final BigInteger g = BigInteger.ZERO;
     40 
     41 
     42     class MyDSA extends DSAKeyPairGeneratorImpl {
     43         public MyDSA(DSAParams dsaParams) {
     44             super(dsaParams);
     45         }
     46     }
     47 
     48     /**
     49      * @tests java.security.interfaces.DSAKeyPairGenerator
     50      * #initialize(DSAParams params, SecureRandom random)
     51      */
     52     @TestTargetNew(
     53         level = TestLevel.COMPLETE,
     54         notes = "",
     55         method = "initialize",
     56         args = {java.security.interfaces.DSAParams.class, java.security.SecureRandom.class}
     57     )
     58     public void test_DSAKeyPairGenerator01() {
     59         DSAParams dsaParams = new DSAParameterSpec(p, q, g);
     60         SecureRandom random = null;
     61         MyDSA dsa = new MyDSA(dsaParams);
     62         try {
     63             random = SecureRandom.getInstance("SHA1PRNG");
     64         } catch (Exception e) {
     65             fail("Unexpected exception for SecureRandom: " + e);
     66         }
     67 
     68         try {
     69             dsa.initialize(dsaParams, random);
     70         } catch (Exception e) {
     71             fail("Unexpected exception: " + e);
     72         }
     73 
     74         try {
     75             dsa.initialize(dsaParams, null);
     76             fail("InvalidParameterException was not thrown");
     77         } catch (InvalidParameterException ipe) {
     78             //expected
     79         } catch (Exception e) {
     80             fail(e + " was thrown instead of InvalidParameterException");
     81         }
     82         try {
     83             dsa.initialize(null, random);
     84             fail("InvalidParameterException was not thrown");
     85         } catch (InvalidParameterException ipe) {
     86             //expected
     87         } catch (Exception e) {
     88             fail(e + " was thrown instead of InvalidParameterException");
     89         }
     90     }
     91 
     92     /**
     93      * @tests java.security.interfaces.DSAKeyPairGenerator
     94      * #initialize(int modlen, boolean genParams, SecureRandom randomm)
     95      */
     96     @TestTargetNew(
     97         level = TestLevel.COMPLETE,
     98         notes = "",
     99         method = "initialize",
    100         args = {int.class, boolean.class, java.security.SecureRandom.class}
    101     )
    102     public void test_DSAKeyPairGenerator02() {
    103         int[] invalidLen = {-1, 0, 511, 513, 650, 1023, 1025};
    104         DSAParams dsaParams = new DSAParameterSpec(p, q, g);
    105         SecureRandom random = null;
    106         MyDSA dsa = new MyDSA(null);
    107         try {
    108             random = SecureRandom.getInstance("SHA1PRNG");
    109         } catch (Exception e) {
    110             fail("Unexpected exception for SecureRandom: " + e);
    111         }
    112 
    113         //exception case
    114         try {
    115             dsa.initialize(520, false, random);
    116             fail("InvalidParameterException was not thrown");
    117         } catch (InvalidParameterException ipe) {
    118             String str = ipe.getMessage();
    119             if (!str.equals("there are not precomputed parameters")) {
    120                 fail("Incorrect exception's message: " + str);
    121             }
    122         } catch (Exception e) {
    123             fail(e + " was thrown instead of InvalidParameterException");
    124         }
    125 
    126         //exception case
    127         for (int i = 0; i < invalidLen.length; i++) {
    128             try {
    129                 dsa.initialize(invalidLen[i], true, random);
    130                 fail("InvalidParameterException was not thrown");
    131             } catch (InvalidParameterException ipe) {
    132                 String str = ipe.getMessage();
    133                 if (!str.equals("Incorrect modlen")) {
    134                     fail("Incorrect exception's message: " + str);
    135                 }
    136             } catch (Exception e) {
    137                 fail(e + " was thrown instead of InvalidParameterException");
    138             }
    139         }
    140 
    141         //positive case
    142         dsa = new MyDSA(dsaParams);
    143         try {
    144             dsa.initialize(520, true, random);
    145         } catch (Exception e) {
    146             fail(e + " was thrown for subcase 1");
    147         }
    148 
    149         //positive case
    150         try {
    151             dsa.initialize(520, false, random);
    152         } catch (Exception e) {
    153             fail(e + " was thrown for subcase 1");
    154         }
    155     }
    156 }
    157