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