Home | History | Annotate | Download | only in spec
      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 tests.security.spec;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import java.security.spec.AlgorithmParameterSpec;
     28 import java.security.spec.MGF1ParameterSpec;
     29 import java.security.spec.PSSParameterSpec;
     30 
     31 /**
     32  * Tests for <code>PSSParameterSpec</code> class (1.5)
     33  *
     34  */
     35 public class PSSParameterSpecTest extends TestCase {
     36 
     37     /**
     38      * Test #1 for <code>PSSParameterSpec(int)</code> ctor<br>
     39      * Assertion: constructs using valid parameter
     40      * <code>PSSParameterSpec<code> object
     41      */
     42     public final void testPSSParameterSpec0101() {
     43         AlgorithmParameterSpec aps = new PSSParameterSpec(20);
     44         assertTrue(aps instanceof PSSParameterSpec);
     45     }
     46 
     47     /**
     48      * Test #2 for <code>PSSParameterSpec(int)</code> ctor<br>
     49      * Assertion:
     50      * throws <code>IllegalArgumentException</code>
     51      * if <code>saltLen</code> less than 0
     52      */
     53     public final void testPSSParameterSpec0102() {
     54         try {
     55             new PSSParameterSpec(-1);
     56             fail("Expected IAE not thrown");
     57         } catch (IllegalArgumentException e) {
     58         }
     59     }
     60 
     61     /**
     62      * Test #1 for
     63      * <code>
     64      * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
     65      * </code> ctor<br>
     66      * Assertion: constructs using valid parameters
     67      * <code>PSSParameterSpec<code> object
     68      */
     69     public final void testPSSParameterSpec0201() {
     70         AlgorithmParameterSpec aps = new PSSParameterSpec("SHA-1", "MGF1",
     71                 MGF1ParameterSpec.SHA1, 20, 1);
     72         assertTrue(aps instanceof PSSParameterSpec);
     73     }
     74 
     75     /**
     76      * Test #2 for
     77      * <code>
     78      * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
     79      * </code> ctor<br>
     80      * Assertion:
     81      * throws <code>NullPointerException</code>
     82      * if <code>mdName</code> is null
     83      */
     84     public final void testPSSParameterSpec0202() {
     85         try {
     86             new PSSParameterSpec(null, "MGF1", MGF1ParameterSpec.SHA1, 20, 1);
     87             fail("Expected NPE not thrown");
     88         } catch (NullPointerException e) {
     89         }
     90     }
     91 
     92     /**
     93      * Test #3 for
     94      * <code>
     95      * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
     96      * </code> ctor<br>
     97      * Assertion:
     98      * throws <code>NullPointerException</code>
     99      * if <code>mgfName</code> is null
    100      */
    101     public final void testPSSParameterSpec0203() {
    102         try {
    103             new PSSParameterSpec("SHA-1", null, MGF1ParameterSpec.SHA1, 20, 1);
    104             fail("Expected NPE not thrown");
    105         } catch (NullPointerException e) {
    106         }
    107     }
    108 
    109     /**
    110      * Test #4 for
    111      * <code>
    112      * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
    113      * </code> ctor<br>
    114      * Assertion:
    115      * throws <code>IllegalArgumentException<code>
    116      * if <code>saltLen<code> less than 0
    117      */
    118     public final void testPSSParameterSpec0204() {
    119         try {
    120             new PSSParameterSpec("SHA-1", "MGF1",
    121                     MGF1ParameterSpec.SHA1, -20, 1);
    122             fail("Expected IAE not thrown");
    123         } catch (IllegalArgumentException e) {
    124         }
    125     }
    126 
    127     /**
    128      * Test #5 for
    129      * <code>
    130      * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
    131      * </code> ctor<br>
    132      * Assertion:
    133      * throws <code>IllegalArgumentException</code>
    134      * if <code>trailerField</code> less than 0
    135      */
    136     public final void testPSSParameterSpec0205() {
    137         try {
    138             new PSSParameterSpec("SHA-1", "MGF1",
    139                     MGF1ParameterSpec.SHA1, 20, -1);
    140             fail("Expected IAE not thrown");
    141         } catch (IllegalArgumentException e) {
    142         }
    143     }
    144 
    145     /**
    146      * Test #6 for
    147      * <code>
    148      * PSSParameterSpec(String,String,AlgorithmParameterSpec,int,int)
    149      * </code> ctor<br>
    150      * Assertion: <code>AlgorithmParameterSpec</code> can be null
    151      *
    152      */
    153     public final void testPSSParameterSpec0206() {
    154         new PSSParameterSpec("SHA-1", "MGF1", null, 20, 1);
    155     }
    156 
    157     /**
    158      * Test for <code>getDigestAlgorithm()</code> method
    159      * Assertion: returns message digest algorithm name
    160      */
    161     public final void testGetDigestAlgorithm() {
    162         PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
    163                 MGF1ParameterSpec.SHA1, 20, 1);
    164         assertEquals("SHA-1", pssps.getDigestAlgorithm());
    165     }
    166 
    167     /**
    168      * Test for <code>getMGFAlgorithm()</code> method
    169      * Assertion: returns mask generation function algorithm name
    170      */
    171     public final void testGetMGFAlgorithm() {
    172         PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
    173                 MGF1ParameterSpec.SHA1, 20, 1);
    174         assertEquals("MGF1", pssps.getMGFAlgorithm());
    175     }
    176 
    177     /**
    178      * Test #1 for <code>getMGFParameters()</code> method
    179      * Assertion: returns mask generation function parameters
    180      */
    181     public final void testGetMGFParameters01() {
    182         PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
    183                 MGF1ParameterSpec.SHA1, 20, 1);
    184         assertTrue(MGF1ParameterSpec.SHA1.equals(pssps.getMGFParameters()));
    185     }
    186 
    187     /**
    188      * Test #2 for <code>getMGFParameters()</code> method
    189      * Assertion: returns <code>null</code>
    190      * if <code>null</code> had been passed as
    191      * AlgorithmParameterSpec parameter to the ctor
    192      */
    193     public final void testGetMGFParameters02() {
    194         PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
    195                 null, 20, 1);
    196         assertNull(pssps.getMGFParameters());
    197     }
    198 
    199 
    200     /**
    201      * Test for <code>getSaltLength()</code> method<br>
    202      * Assertion: returns salt length value
    203      */
    204     public final void testGetSaltLength() {
    205         PSSParameterSpec pssps = new PSSParameterSpec(20);
    206         assertEquals(20, pssps.getSaltLength());
    207     }
    208 
    209     /**
    210      * Test for <code>getTrailerField()</code> method<br>
    211      * Assertion: returns trailer field value
    212      */
    213     public final void testGetTrailerField() {
    214         PSSParameterSpec pssps = new PSSParameterSpec("SHA-1", "MGF1",
    215                 MGF1ParameterSpec.SHA1, 20, 1);
    216         assertEquals(1, pssps.getTrailerField());
    217     }
    218 
    219     /**
    220      * Test for <code>DEFAULT</code> field<br>
    221      * Assertion: default message digest algorithm name is "SHA-1"
    222      */
    223     public final void testDEFAULTmdName() {
    224         assertEquals("SHA-1", PSSParameterSpec.DEFAULT.getDigestAlgorithm());
    225     }
    226 
    227     /**
    228      * Test for <code>DEFAULT</code> field<br>
    229      * Assertion: default mask generation function algorithm name is "MGF1"
    230      */
    231     public final void testDEFAULTmgfName() {
    232         assertEquals("MGF1", PSSParameterSpec.DEFAULT.getMGFAlgorithm());
    233     }
    234 
    235     /**
    236      * Test for <code>DEFAULT</code> field<br>
    237      * Assertion: default algorithm parameters for mask
    238      * generation function are <code>MGF1ParameterSpec.SHA1</code>
    239      */
    240     public final void testDEFAULTmgfSpec() {
    241         assertTrue(MGF1ParameterSpec.SHA1.equals(PSSParameterSpec.DEFAULT.getMGFParameters()));
    242     }
    243 
    244     /**
    245      * Test for <code>DEFAULT</code> field<br>
    246      * Assertion: default salt length value is 20
    247      */
    248     public final void testDEFAULTsaltLen() {
    249         assertEquals(20, PSSParameterSpec.DEFAULT.getSaltLength());
    250     }
    251 
    252     /**
    253      * Test for <code>DEFAULT</code> field<br>
    254      * Assertion: default trailer field value is 1
    255      */
    256     public final void testDEFAULTtrailerField() {
    257         assertEquals(1, PSSParameterSpec.DEFAULT.getTrailerField());
    258     }
    259 }
    260