|
19 | 19 |
|
20 | 20 | package com.cloud.utils.crypt; |
21 | 21 |
|
22 | | -import static org.mockito.Mockito.mock; |
23 | | -import static org.mockito.Mockito.verifyNoInteractions; |
24 | | -import static org.mockito.Mockito.when; |
25 | | - |
26 | | -import org.jasypt.encryption.StringEncryptor; |
| 22 | +import org.junit.After; |
27 | 23 | import org.junit.Assert; |
| 24 | +import org.junit.Before; |
28 | 25 | import org.junit.Test; |
29 | 26 |
|
30 | 27 | public class EncryptablePropertyPlaceholderConfigurerTest { |
31 | 28 |
|
| 29 | + private static final String ENCRYPTED_VALUE = "ENC(iYVsCZXiGiC6SzZLMNBvBL93hoUpntxkuRjyaqC8L+JYKXw=)"; |
| 30 | + |
| 31 | + private final EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(); |
| 32 | + |
| 33 | + @After |
| 34 | + public void tearDown() { |
| 35 | + EncryptionSecretKeyChecker.resetEncryptor(); |
| 36 | + } |
| 37 | + |
32 | 38 | @Test |
33 | | - public void convertPropertyValueDecryptsWrappedValue() { |
34 | | - StringEncryptor encryptor = mock(StringEncryptor.class); |
35 | | - when(encryptor.decrypt("bmb2VaFdQb")).thenReturn("secret"); |
36 | | - EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor); |
| 39 | + public void convertPropertyValueDecryptsWrappedValueWhenEncryptionEnabled() { |
| 40 | + EncryptionSecretKeyChecker.initEncryptor("managementkey"); |
37 | 41 |
|
38 | | - String result = configurer.convertPropertyValue("ENC(bmb2VaFdQb)"); |
| 42 | + String result = configurer.convertPropertyValue(ENCRYPTED_VALUE); |
39 | 43 |
|
40 | | - Assert.assertEquals("secret", result); |
| 44 | + Assert.assertEquals("encthis", result); |
41 | 45 | } |
42 | 46 |
|
43 | 47 | @Test |
44 | 48 | public void convertPropertyValueTrimsSurroundingWhitespaceBeforeDecrypting() { |
45 | | - StringEncryptor encryptor = mock(StringEncryptor.class); |
46 | | - when(encryptor.decrypt("bmb2VaFdQb")).thenReturn("secret"); |
47 | | - EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor); |
| 49 | + EncryptionSecretKeyChecker.initEncryptor("managementkey"); |
48 | 50 |
|
49 | | - String result = configurer.convertPropertyValue(" ENC(bmb2VaFdQb) "); |
| 51 | + String result = configurer.convertPropertyValue(" " + ENCRYPTED_VALUE + " "); |
| 52 | + |
| 53 | + Assert.assertEquals("encthis", result); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void convertPropertyValuePassesThroughWrappedValueWhenEncryptionNotConfigured() { |
| 58 | + String result = configurer.convertPropertyValue(ENCRYPTED_VALUE); |
50 | 59 |
|
51 | | - Assert.assertEquals("secret", result); |
| 60 | + Assert.assertEquals(ENCRYPTED_VALUE, result); |
52 | 61 | } |
53 | 62 |
|
54 | 63 | @Test |
55 | 64 | public void convertPropertyValuePassesThroughPlainValues() { |
56 | | - StringEncryptor encryptor = mock(StringEncryptor.class); |
57 | | - EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor); |
| 65 | + EncryptionSecretKeyChecker.initEncryptor("managementkey"); |
58 | 66 |
|
59 | 67 | String result = configurer.convertPropertyValue("guest"); |
60 | 68 |
|
61 | 69 | Assert.assertEquals("guest", result); |
62 | | - verifyNoInteractions(encryptor); |
63 | 70 | } |
64 | 71 |
|
65 | 72 | @Test |
66 | 73 | public void convertPropertyValueHandlesNull() { |
67 | | - StringEncryptor encryptor = mock(StringEncryptor.class); |
68 | | - EncryptablePropertyPlaceholderConfigurer configurer = new EncryptablePropertyPlaceholderConfigurer(encryptor); |
| 74 | + EncryptionSecretKeyChecker.initEncryptor("managementkey"); |
69 | 75 |
|
70 | 76 | Assert.assertNull(configurer.convertPropertyValue(null)); |
71 | | - verifyNoInteractions(encryptor); |
72 | 77 | } |
73 | 78 | } |
0 commit comments