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  */
21 
22 package org.apache.harmony.crypto.tests.javax.crypto;
23 
24 import java.security.InvalidKeyException;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.NoSuchProviderException;
27 import java.security.Provider;
28 import java.security.spec.InvalidKeySpecException;
29 import java.security.spec.KeySpec;
30 
31 import javax.crypto.SecretKey;
32 import javax.crypto.SecretKeyFactory;
33 import javax.crypto.SecretKeyFactorySpi;
34 import javax.crypto.spec.DESKeySpec;
35 import javax.crypto.spec.DESedeKeySpec;
36 import javax.crypto.spec.SecretKeySpec;
37 
38 import org.apache.harmony.crypto.tests.support.MySecretKeyFactorySpi;
39 import org.apache.harmony.security.tests.support.SpiEngUtils;
40 
41 import junit.framework.TestCase;
42 
43 
44 /**
45  * Tests for <code>SecretKeyFactory</code> class constructors and methods.
46  */
47 
48 public class SecretKeyFactoryTest extends TestCase {
49 
50     public static final String srvSecretKeyFactory = "SecretKeyFactory";
51 
52     private static String defaultAlgorithm1 = "DESede";
53     private static String defaultAlgorithm2 = "DES";
54 
55     public static String defaultAlgorithm = null;
56 
57     private static String defaultProviderName = null;
58 
59     private static Provider defaultProvider = null;
60 
61     private static final String[] invalidValues = SpiEngUtils.invalidValues;
62 
63     public static final String[] validValues = new String[2];
64     private static boolean DEFSupported = false;
65 
66     private static final String NotSupportMsg = "Default algorithm is not supported";
67 
68     static {
69         defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm1,
70                 srvSecretKeyFactory);
71         DEFSupported = (defaultProvider != null);
72         if (DEFSupported) {
73             defaultAlgorithm = defaultAlgorithm1;
74             validValues[0] = defaultAlgorithm.toUpperCase();
75             validValues[1] = defaultAlgorithm.toLowerCase();
76             defaultProviderName = defaultProvider.getName();
77         } else {
78             defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm2,
79                     srvSecretKeyFactory);
80             DEFSupported = (defaultProvider != null);
81             if (DEFSupported) {
82                 defaultAlgorithm = defaultAlgorithm2;
83                 validValues[0] = defaultAlgorithm.toUpperCase();
84                 validValues[2] = defaultAlgorithm.toLowerCase();
85                 defaultProviderName = defaultProvider.getName();
86             } else {
87                 defaultAlgorithm = null;
88                 defaultProviderName = null;
89                 defaultProvider = null;
90             }
91         }
92     }
93 
createSKFac()94     protected SecretKeyFactory[] createSKFac() {
95         if (!DEFSupported) {
96             fail(defaultAlgorithm + " algorithm is not supported");
97             return null;
98         }
99         SecretKeyFactory[] skF = new SecretKeyFactory[3];
100         try {
101             skF[0] = SecretKeyFactory.getInstance(defaultAlgorithm);
102             skF[1] = SecretKeyFactory.getInstance(defaultAlgorithm,
103                     defaultProvider);
104             skF[2] = SecretKeyFactory.getInstance(defaultAlgorithm,
105                     defaultProviderName);
106             return skF;
107         } catch (Exception e) {
108             e.printStackTrace();
109             return null;
110         }
111     }
112 
113     /**
114      * Test for <code>SecretKeyFactory</code> constructor
115      * Assertion: returns SecretKeyFactory object
116      */
testSecretKeyFactory01()117     public void testSecretKeyFactory01() throws NoSuchAlgorithmException,
118             InvalidKeySpecException, InvalidKeyException {
119         if (!DEFSupported) {
120             fail(NotSupportMsg);
121             return;
122         }
123         SecretKeyFactorySpi spi = new MySecretKeyFactorySpi();
124         SecretKeyFactory secKF = new mySecretKeyFactory(spi, defaultProvider,
125                 defaultAlgorithm);
126         assertEquals("Incorrect algorithm", secKF.getAlgorithm(),
127                 defaultAlgorithm);
128         assertEquals("Incorrect provider", secKF.getProvider(), defaultProvider);
129         assertNull("Incorrect result", secKF.generateSecret(null));
130         assertNull("Incorrect result", secKF.getKeySpec(null, null));
131         assertNull("Incorrect result", secKF.translateKey(null));
132         secKF = new mySecretKeyFactory(null, null, null);
133         assertNull("Algorithm must be null", secKF.getAlgorithm());
134         assertNull("Provider must be null", secKF.getProvider());
135         try {
136             secKF.translateKey(null);
137             fail("NullPointerException must be thrown");
138         } catch (NullPointerException e) {
139         }
140     }
141 
142     /**
143      * Test for <code>getInstance(String algorithm)</code> method
144      * Assertions:
145      * throws NullPointerException when algorithm is null;
146      * throws NoSuchAlgorithmException when algorithm has invalid value
147      */
testSecretKeyFactory02()148     public void testSecretKeyFactory02() throws NoSuchAlgorithmException {
149         try {
150             SecretKeyFactory.getInstance(null);
151             fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
152         } catch (NullPointerException e) {
153         } catch (NoSuchAlgorithmException e) {
154         }
155         for (int i = 0; i < invalidValues.length; i++) {
156             try {
157                 SecretKeyFactory.getInstance(invalidValues[i]);
158                 fail("NoSuchAlgorithmException was not thrown as expected");
159             } catch (NoSuchAlgorithmException e) {
160             }
161         }
162     }
163 
164     /**
165      * Test for <code>getInstance(String algorithm)</code> method
166      * Assertion: returns SecretKeyObject
167      */
testSecretKeyFactory03()168     public void testSecretKeyFactory03() throws NoSuchAlgorithmException {
169         if (!DEFSupported) {
170             fail(NotSupportMsg);
171             return;
172         }
173         for (int i = 0; i < validValues.length; i++) {
174             SecretKeyFactory secKF = SecretKeyFactory
175                     .getInstance(validValues[i]);
176             assertEquals("Incorrect algorithm", secKF.getAlgorithm(),
177                     validValues[i]);
178         }
179     }
180 
181     /**
182      * Test for <code>getInstance(String algorithm, String provider)</code>
183      * method
184      * Assertion:
185      * throws NullPointerException when algorithm is null;
186      * throws NoSuchAlgorithmException when algorithm is invalid
187      */
testSecretKeyFactory04()188     public void testSecretKeyFactory04() throws NoSuchAlgorithmException,
189             NoSuchProviderException {
190         if (!DEFSupported) {
191             fail(NotSupportMsg);
192             return;
193         }
194         try {
195             SecretKeyFactory.getInstance(null, defaultProviderName);
196             fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
197         } catch (NullPointerException e) {
198         } catch (NoSuchAlgorithmException e) {
199         }
200         for (int i = 0; i < invalidValues.length; i++) {
201             try {
202                 SecretKeyFactory.getInstance(invalidValues[i],
203                         defaultProviderName);
204                 fail("NoSuchAlgorithmException was not thrown as expected (algorithm: "
205                         .concat(invalidValues[i]).concat(")"));
206             } catch (NoSuchAlgorithmException e) {
207             }
208         }
209     }
210 
211     /**
212      * Test for <code>getInstance(String algorithm, String provider)</code>
213      * method
214      * Assertion:
215      * throws IllegalArgumentException when provider is null or empty;
216      * throws NoSuchProviderException when provider has invalid value
217      */
testSecretKeyFactory05()218     public void testSecretKeyFactory05() throws NoSuchAlgorithmException,
219             NoSuchProviderException {
220         if (!DEFSupported) {
221             fail(NotSupportMsg);
222             return;
223         }
224         String prov = null;
225         for (int i = 0; i < validValues.length; i++) {
226             try {
227                 SecretKeyFactory.getInstance(validValues[i], prov);
228                 fail("IllegalArgumentException was not thrown as expected (algorithm: "
229                         .concat(validValues[i]).concat(" provider: null"));
230             } catch (IllegalArgumentException e) {
231             }
232             try {
233                 SecretKeyFactory.getInstance(validValues[i], "");
234                 fail("IllegalArgumentException was not thrown as expected (algorithm: "
235                         .concat(validValues[i]).concat(" provider: empty"));
236             } catch (IllegalArgumentException e) {
237             }
238             for (int j = 1; j < invalidValues.length; j++) {
239                 try {
240                     SecretKeyFactory.getInstance(validValues[i],
241                             invalidValues[j]);
242                     fail("NoSuchProviderException was not thrown as expected (algorithm: "
243                             .concat(validValues[i]).concat(" provider: ")
244                             .concat(invalidValues[j]).concat(")"));
245                 } catch (NoSuchProviderException e) {
246                 }
247             }
248         }
249     }
250 
251     /**
252      * Test for <code>getInstance(String algorithm, String provider)</code>
253      * method
254      * Assertion: returns SecretKeyFactory object
255      */
testSecretKeyFactory06()256     public void testSecretKeyFactory06() throws NoSuchProviderException,
257             NoSuchAlgorithmException {
258         if (!DEFSupported) {
259             fail(NotSupportMsg);
260             return;
261         }
262         for (int i = 0; i < validValues.length; i++) {
263             SecretKeyFactory secKF = SecretKeyFactory.getInstance(
264                     validValues[i], defaultProviderName);
265             assertEquals("Incorrect algorithm", secKF.getAlgorithm(),
266                     validValues[i]);
267             assertEquals("Incorrect provider", secKF.getProvider().getName(),
268                     defaultProviderName);
269         }
270     }
271 
272     /**
273      * Test for <code>getInstance(String algorithm, Provider provider)</code>
274      * method
275      * Assertion: throws NullPointerException when algorithm is null;
276      * throws NoSuchAlgorithmException when algorithm is invalid
277      */
testSecretKeyFactory07()278     public void testSecretKeyFactory07() throws NoSuchAlgorithmException {
279         if (!DEFSupported) {
280             fail(NotSupportMsg);
281             return;
282         }
283         try {
284             SecretKeyFactory.getInstance(null, defaultProvider);
285             fail("NullPointerException or NoSuchAlgorithmException should be thrown if algorithm is null");
286         } catch (NullPointerException e) {
287         } catch (NoSuchAlgorithmException e) {
288         }
289         for (int i = 0; i < invalidValues.length; i++) {
290             try {
291                 SecretKeyFactory.getInstance(invalidValues[i], defaultProvider);
292                 fail("NoSuchAlgorithmException was not thrown as expected (algorithm: "
293                         .concat(invalidValues[i]).concat(")"));
294             } catch (NoSuchAlgorithmException e) {
295             }
296         }
297     }
298 
299     /**
300      * Test for <code>getInstance(String algorithm, Provider provider)</code>
301      * method
302      * Assertion: throws IllegalArgumentException when provider is null
303      */
testSecretKeyFactory08()304     public void testSecretKeyFactory08() throws NoSuchAlgorithmException {
305         if (!DEFSupported) {
306             fail(NotSupportMsg);
307             return;
308         }
309         Provider prov = null;
310         for (int i = 0; i < validValues.length; i++) {
311             try {
312                 SecretKeyFactory.getInstance(validValues[i], prov);
313                 fail("IllegalArgumentException was not thrown as expected (provider is null, algorithm: "
314                         .concat(validValues[i]).concat(")"));
315             } catch (IllegalArgumentException e) {
316             }
317         }
318     }
319 
320     /**
321      * Test for <code>getInstance(String algorithm, Provider provider)</code>
322      * method
323      * Assertion: returns SecretKeyFactory object
324      */
testSecretKeyFactory09()325     public void testSecretKeyFactory09() throws NoSuchAlgorithmException {
326         if (!DEFSupported) {
327             fail(NotSupportMsg);
328             return;
329         }
330         for (int i = 0; i < validValues.length; i++) {
331             SecretKeyFactory secKF = SecretKeyFactory.getInstance(
332                     validValues[i], defaultProvider);
333             assertEquals("Incorrect algorithm", secKF.getAlgorithm(),
334                     validValues[i]);
335             assertEquals("Incorrect provider", secKF.getProvider(),
336                     defaultProvider);
337         }
338     }
339 
340     /**
341      * Test for <code>generateSecret(KeySpec keySpec)</code> and
342      * <code>getKeySpec(SecretKey key, Class keySpec)
343      * methods
344      * Assertion:
345      * throw InvalidKeySpecException if parameter is inappropriate
346      */
testSecretKeyFactory10()347     public void testSecretKeyFactory10() throws InvalidKeyException,
348             InvalidKeySpecException {
349         if (!DEFSupported) {
350             fail(NotSupportMsg);
351             return;
352         }
353         byte[] bb = new byte[24];
354         KeySpec ks = (defaultAlgorithm.equals(defaultAlgorithm2) ? (KeySpec) new DESKeySpec(bb) :
355                 (KeySpec) new DESedeKeySpec(bb));
356         KeySpec rks = null;
357         SecretKeySpec secKeySpec = new SecretKeySpec(bb, defaultAlgorithm);
358         SecretKey secKey = null;
359         SecretKeyFactory[] skF = createSKFac();
360         assertNotNull("SecretKeyFactory object were not created", skF);
361         for (int i = 0; i < skF.length; i++) {
362             try {
363                 skF[i].generateSecret(null);
364                 fail("generateSecret(null): InvalidKeySpecException must be thrown");
365             } catch (InvalidKeySpecException e) {
366             }
367 
368             secKey = skF[i].generateSecret(ks);
369             try {
370                 skF[i].getKeySpec(null, null);
371                 fail("getKeySpec(null,null): InvalidKeySpecException must be thrown");
372             } catch (InvalidKeySpecException e) {
373             }
374             try {
375                 skF[i].getKeySpec(null, ks.getClass());
376                 fail("getKeySpec(null, Class): InvalidKeySpecException must be thrown");
377             } catch (InvalidKeySpecException e) {
378             }
379             try {
380                 skF[i].getKeySpec(secKey, null);
381                 fail("getKeySpec(secKey, null): NullPointerException or InvalidKeySpecException must be thrown");
382             } catch (InvalidKeySpecException e) {
383                 // Expected
384             } catch (NullPointerException e) {
385                 // Expected
386             }
387 
388             try {
389                 Class c;
390                 if (defaultAlgorithm.equals(defaultAlgorithm2)) {
391                     c = DESedeKeySpec.class;
392                 } else {
393                     c = DESKeySpec.class;
394                 }
395                 skF[i].getKeySpec(secKeySpec, c);
396                 fail("getKeySpec(secKey, Class): InvalidKeySpecException must be thrown");
397             } catch (InvalidKeySpecException e) {
398             }
399             rks = skF[i].getKeySpec(secKeySpec, ks.getClass());
400             if (defaultAlgorithm.equals(defaultAlgorithm1)) {
401                 assertTrue("Incorrect getKeySpec() result 1",
402                         rks instanceof DESedeKeySpec);
403             } else {
404                 assertTrue("Incorrect getKeySpec() result 1",
405                         rks instanceof DESKeySpec);
406             }
407 
408             rks = skF[i].getKeySpec(secKey, ks.getClass());
409             if (defaultAlgorithm.equals(defaultAlgorithm1)) {
410                 assertTrue("Incorrect getKeySpec() result 2",
411                         rks instanceof DESedeKeySpec);
412             } else {
413                 assertTrue("Incorrect getKeySpec() result 2",
414                         rks instanceof DESKeySpec);
415             }
416         }
417     }
418 }
419 
420 class mySecretKeyFactory extends SecretKeyFactory {
mySecretKeyFactory(SecretKeyFactorySpi spi, Provider prov, String alg)421     public mySecretKeyFactory(SecretKeyFactorySpi spi, Provider prov, String alg) {
422         super(spi, prov, alg);
423     }
424 }
425