OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / crypto / tests / javax / crypto / spec / OAEPParameterSpecTest.java
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 Alexander Y. Kleymenov
20 * @version $Revision$
21 */
22
23 package org.apache.harmony.crypto.tests.javax.crypto.spec;
24
25 import dalvik.annotation.TestTargetClass;
26 import dalvik.annotation.TestTargets;
27 import dalvik.annotation.TestLevel;
28 import dalvik.annotation.TestTargetNew;
29
30 import java.security.spec.MGF1ParameterSpec;
31 import java.security.spec.AlgorithmParameterSpec;
32
33 import javax.crypto.spec.OAEPParameterSpec;
34 import javax.crypto.spec.PSource;
35
36 import junit.framework.Test;
37 import junit.framework.TestCase;
38 import junit.framework.TestSuite;
39
40 @TestTargetClass(OAEPParameterSpec.class)
41 /**
42  */
43
44 public class OAEPParameterSpecTest extends TestCase {
45
46     /**
47      * OAEPParameterSpec(String mdName, String mgfName, AlgorithmParameterSpec
48      * mgfSpec, PSource pSrc) method testing. Tests that NullPointerException
49      * is thrown in the case of inappropriate constructor parameters and checks
50      * the value of DEFAULT field.
51      */
52     @TestTargetNew(
53         level = TestLevel.COMPLETE,
54         notes = "",
55         method = "OAEPParameterSpec",
56         args = {java.lang.String.class, java.lang.String.class, java.security.spec.AlgorithmParameterSpec.class, javax.crypto.spec.PSource.class}
57     )
58     public void testOAEPParameterSpec() {
59         // using init values for OAEPParameterSpec.DEFAULT
60         String mdName = "SHA-1";
61         String mgfName = "MGF1";
62         AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
63         PSource pSrc = PSource.PSpecified.DEFAULT;
64
65         try {
66             new OAEPParameterSpec(null, mgfName, mgfSpec, pSrc);
67             fail("NullPointerException should be thrown in the case of "
68                     + "null mdName.");
69         } catch (NullPointerException e) {
70         }
71
72         try {
73             new OAEPParameterSpec(mdName, null, mgfSpec, pSrc);
74             fail("NullPointerException should be thrown in the case of "
75                     + "null mgfName.");
76         } catch (NullPointerException e) {
77         }
78
79         try {
80             new OAEPParameterSpec(mdName, mgfName, mgfSpec, null);
81             fail("NullPointerException should be thrown in the case of "
82                     + "null pSrc.");
83         } catch (NullPointerException e) {
84         }
85
86         assertTrue("The message digest algorithm name of "
87                 + "OAEPParameterSpec.DEFAULT field should be " + mdName,
88                 OAEPParameterSpec.DEFAULT.getDigestAlgorithm().equals(mdName));
89
90         assertTrue("The mask generation function algorithm name of "
91                 + "OAEPParameterSpec.DEFAULT field should be " + mgfName,
92                 OAEPParameterSpec.DEFAULT.getMGFAlgorithm().equals(mgfName));
93
94         assertTrue("The mask generation function parameters of "
95                 + "OAEPParameterSpec.DEFAULT field should be the same object "
96                 + "as MGF1ParameterSpec.SHA1",
97                 OAEPParameterSpec.DEFAULT.getMGFParameters()
98                                                     == MGF1ParameterSpec.SHA1);
99         assertTrue("The source of the encoding input P of "
100                 + "OAEPParameterSpec.DEFAULT field should be the same object "
101                 + "PSource.PSpecified.DEFAULT",
102                 OAEPParameterSpec.DEFAULT.getPSource()
103                                                 == PSource.PSpecified.DEFAULT);
104     }
105
106     /**
107      * getDigestAlgorithm() method testing.
108      */
109     @TestTargetNew(
110         level = TestLevel.COMPLETE,
111         notes = "",
112         method = "getDigestAlgorithm",
113         args = {}
114     )
115     public void testGetDigestAlgorithm() {
116         String mdName = "SHA-1";
117         String mgfName = "MGF1";
118         AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
119         PSource pSrc = PSource.PSpecified.DEFAULT;
120
121         OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
122                                                                 mgfSpec, pSrc);
123         assertTrue("The returned value does not equal to the "
124                 + "value specified in the constructor.",
125                 ps.getDigestAlgorithm().equals(mdName));
126     }
127
128     /**
129      * getMGFAlgorithm() method testing.
130      */
131     @TestTargetNew(
132         level = TestLevel.COMPLETE,
133         notes = "",
134         method = "getMGFAlgorithm",
135         args = {}
136     )
137     public void testGetMGFAlgorithm() {
138         String mdName = "SHA-1";
139         String mgfName = "MGF1";
140         AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
141         PSource pSrc = PSource.PSpecified.DEFAULT;
142
143         OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
144                                                                 mgfSpec, pSrc);
145         assertTrue("The returned value does not equal to the "
146                 + "value specified in the constructor.",
147                 ps.getMGFAlgorithm().equals(mgfName));
148     }
149
150     /**
151      * getMGFParameters() method testing.
152      */
153     @TestTargetNew(
154         level = TestLevel.COMPLETE,
155         notes = "",
156         method = "getMGFParameters",
157         args = {}
158     )
159     public void testGetMGFParameters() {
160         String mdName = "SHA-1";
161         String mgfName = "MGF1";
162         AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
163         PSource pSrc = PSource.PSpecified.DEFAULT;
164
165         OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
166                                                                 mgfSpec, pSrc);
167         assertTrue("The returned value does not equal to the "
168                 + "value specified in the constructor.",
169                 ps.getMGFParameters() == mgfSpec);
170     }
171
172     /**
173      * getPSource() method testing.
174      */
175     @TestTargetNew(
176         level = TestLevel.COMPLETE,
177         notes = "",
178         method = "getPSource",
179         args = {}
180     )
181     public void testGetPSource() {
182         String mdName = "SHA-1";
183         String mgfName = "MGF1";
184         AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
185         PSource pSrc = PSource.PSpecified.DEFAULT;
186
187         OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName,
188                                                                 mgfSpec, pSrc);
189         assertTrue("The returned value does not equal to the "
190                 + "value specified in the constructor.",
191                 ps.getPSource() == pSrc);
192     }
193
194     public static Test suite() {
195         return new TestSuite(OAEPParameterSpecTest.class);
196     }
197 }
198