View Javadoc
1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.commons.crypto.cipher;
19  
20  import java.security.GeneralSecurityException;
21  import java.util.Properties;
22  
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  public class CryptoCipherFactoryTest {
27      @Test
28      public void testDefaultCipher() throws GeneralSecurityException {
29          CryptoCipher defaultCipher = CryptoCipherFactory
30                  .getCryptoCipher("AES/CBC/NoPadding");
31          final String name = defaultCipher.getClass().getName();
32          if (OpenSsl.getLoadingFailureReason() == null) {
33              Assert.assertEquals(OpenSslCipher.class.getName(), name);
34          } else {
35              Assert.assertEquals(JceCipher.class.getName(), name);
36          }
37      }
38  
39      @Test
40      public void testEmptyCipher() throws GeneralSecurityException {
41          Properties properties = new Properties();
42          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ""); // TODO should this really mean use the default?
43          CryptoCipher defaultCipher = CryptoCipherFactory.getCryptoCipher(
44                  "AES/CBC/NoPadding", properties);
45          final String name = defaultCipher.getClass().getName();
46          if (OpenSsl.getLoadingFailureReason() == null) {
47              Assert.assertEquals(OpenSslCipher.class.getName(), name);
48          } else {
49              Assert.assertEquals(JceCipher.class.getName(), name);
50          }
51      }
52  
53      @Test(expected = GeneralSecurityException.class)
54      public void testInvalidCipher() throws GeneralSecurityException {
55          Properties properties = new Properties();
56          properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
57                  "InvalidCipherName");
58          CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties);
59      }
60  
61      @Test(expected = GeneralSecurityException.class)
62      public void testInvalidTransformation() throws GeneralSecurityException {
63        Properties properties = new Properties();
64        CryptoCipherFactory.getCryptoCipher("AES/Invalid/NoPadding", properties);
65      }
66  
67      @Test(expected = IllegalArgumentException.class)
68      public void testNoCipher() throws Exception {
69          Properties properties = new Properties();
70          // An empty string currently means use the default
71          // However the splitter drops empty fields
72          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ",");
73          CryptoCipherFactory.getCryptoCipher("AES/CBC/NoPadding", properties);
74      }
75  
76  }