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 static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  
23  import java.io.IOException;
24  import java.security.GeneralSecurityException;
25  import java.util.Properties;
26  
27  import org.apache.commons.crypto.utils.AES;
28  import org.junit.jupiter.api.Test;
29  
30  
31  public class CryptoCipherFactoryTest {
32      @Test
33      public void testDefaultCipher() throws GeneralSecurityException {
34          final CryptoCipher defaultCipher = CryptoCipherFactory
35                  .getCryptoCipher(AES.CTR_NO_PADDING);
36          final String name = defaultCipher.getClass().getName();
37          if (OpenSsl.getLoadingFailureReason() == null) {
38              assertEquals(OpenSslCipher.class.getName(), name);
39          } else {
40              assertEquals(JceCipher.class.getName(), name);
41          }
42      }
43  
44      @Test
45      public void testEmptyCipher() throws GeneralSecurityException, IOException {
46          final Properties properties = new Properties();
47          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ""); // TODO should this really mean use the default?
48          try (CryptoCipher defaultCipher = CryptoCipherFactory.getCryptoCipher(
49                  AES.CBC_NO_PADDING, properties)) {
50              final String name = defaultCipher.getClass().getName();
51              if (OpenSsl.getLoadingFailureReason() == null) {
52                  assertEquals(OpenSslCipher.class.getName(), name);
53              } else {
54                  assertEquals(JceCipher.class.getName(), name);
55              }
56          }
57      }
58  
59      @Test
60      public void testInvalidCipher() {
61          final Properties properties = new Properties();
62          properties.setProperty(CryptoCipherFactory.CLASSES_KEY,
63                  "InvalidCipherName");
64          assertThrows(GeneralSecurityException.class,
65                  () -> CryptoCipherFactory.getCryptoCipher(AES.CBC_NO_PADDING, properties));
66  
67      }
68  
69      @Test
70      public void testInvalidTransformation() {
71        final Properties properties = new Properties();
72          assertThrows(GeneralSecurityException.class,
73                  () -> CryptoCipherFactory.getCryptoCipher("AES/Invalid/NoPadding", properties));
74  
75      }
76  
77      @Test
78      public void testNoCipher() {
79          final Properties properties = new Properties();
80          // An empty string currently means use the default
81          // However the splitter drops empty fields
82          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ",");
83          assertThrows(IllegalArgumentException.class,
84                  () -> CryptoCipherFactory.getCryptoCipher(AES.CBC_NO_PADDING, properties));
85  
86      }
87  
88  }