1    
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
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, ""); 
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          
81          
82          properties.setProperty(CryptoCipherFactory.CLASSES_KEY, ",");
83          assertThrows(IllegalArgumentException.class,
84                  () -> CryptoCipherFactory.getCryptoCipher(AES.CBC_NO_PADDING, properties));
85  
86      }
87  
88  }