blob: 1b97c327b1bdf410fac1a60851160ae342448aa0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.config.internal;
import java.io.File;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.Key;
import javax.crypto.BadPaddingException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class AesECBCipherTest {
@Test
public void generateRandomSecretKey() {
AesECBCipher cipher = new AesECBCipher(null);
String key = cipher.generateRandomSecretKey();
assertThat(StringUtils.isNotBlank(key)).isTrue();
assertThat(Base64.isArrayByteBase64(key.getBytes())).isTrue();
}
@Test
public void encrypt() throws Exception {
AesECBCipher cipher = new AesECBCipher(pathToSecretKey());
String encryptedText = cipher.encrypt("this is a secret");
assertThat(StringUtils.isNotBlank(encryptedText)).isTrue();
assertThat(Base64.isArrayByteBase64(encryptedText.getBytes())).isTrue();
}
@Test
public void encrypt_bad_key() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/internal/AesCipherTest/bad_secret_key.txt");
AesECBCipher cipher = new AesECBCipher(new File(resource.toURI()).getCanonicalPath());
assertThatThrownBy(() -> cipher.encrypt("this is a secret"))
.isInstanceOf(RuntimeException.class)
.hasCauseInstanceOf(InvalidKeyException.class);
}
@Test
public void decrypt() throws Exception {
AesECBCipher cipher = new AesECBCipher(pathToSecretKey());
// the following value has been encrypted with the key /org/sonar/api/config/internal/AesCipherTest/aes_secret_key.txt
String clearText = cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY=");
assertThat(clearText).isEqualTo("this is a secret");
}
@Test
public void decrypt_bad_key() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/internal/AesCipherTest/bad_secret_key.txt");
AesECBCipher cipher = new AesECBCipher(new File(resource.toURI()).getCanonicalPath());
assertThatThrownBy(() -> cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="))
.isInstanceOf(RuntimeException.class)
.hasCauseInstanceOf(InvalidKeyException.class);
}
@Test
public void decrypt_other_key() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/internal/AesCipherTest/other_secret_key.txt");
AesECBCipher cipher = new AesECBCipher(new File(resource.toURI()).getCanonicalPath());
assertThatThrownBy(() -> cipher.decrypt("9mx5Zq4JVyjeChTcVjEide4kWCwusFl7P2dSVXtg9IY="))
.isInstanceOf(RuntimeException.class)
.hasCauseInstanceOf(BadPaddingException.class);
}
@Test
public void encryptThenDecrypt() throws Exception {
AesECBCipher cipher = new AesECBCipher(pathToSecretKey());
assertThat(cipher.decrypt(cipher.encrypt("foo"))).isEqualTo("foo");
}
@Test
public void testDefaultPathToSecretKey() {
AesECBCipher cipher = new AesECBCipher(null);
String path = cipher.getPathToSecretKey();
assertThat(StringUtils.isNotBlank(path)).isTrue();
assertThat(new File(path)).hasName("sonar-secret.txt");
}
@Test
public void loadSecretKeyFromFile() throws Exception {
AesECBCipher cipher = new AesECBCipher(null);
Key secretKey = cipher.loadSecretFileFromFile(pathToSecretKey());
assertThat(secretKey.getAlgorithm()).isEqualTo("AES");
assertThat(secretKey.getEncoded()).hasSizeGreaterThan(10);
}
@Test
public void loadSecretKeyFromFile_trim_content() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/internal/AesCipherTest/non_trimmed_secret_key.txt");
String path = new File(resource.toURI()).getCanonicalPath();
AesECBCipher cipher = new AesECBCipher(null);
Key secretKey = cipher.loadSecretFileFromFile(path);
assertThat(secretKey.getAlgorithm()).isEqualTo("AES");
assertThat(secretKey.getEncoded()).hasSizeGreaterThan(10);
}
@Test
public void loadSecretKeyFromFile_file_does_not_exist() throws Exception {
AesECBCipher cipher = new AesECBCipher(null);
assertThatThrownBy(() -> cipher.loadSecretFileFromFile("/file/does/not/exist"))
.isInstanceOf(IllegalStateException.class);
}
@Test
public void loadSecretKeyFromFile_no_property() throws Exception {
AesECBCipher cipher = new AesECBCipher(null);
assertThatThrownBy(() -> cipher.loadSecretFileFromFile(null))
.isInstanceOf(IllegalStateException.class);
}
@Test
public void hasSecretKey() throws Exception {
AesECBCipher cipher = new AesECBCipher(pathToSecretKey());
assertThat(cipher.hasSecretKey()).isTrue();
}
@Test
public void doesNotHaveSecretKey() {
AesECBCipher cipher = new AesECBCipher("/my/twitter/id/is/SimonBrandhof");
assertThat(cipher.hasSecretKey()).isFalse();
}
private String pathToSecretKey() throws Exception {
URL resource = getClass().getResource("/org/sonar/api/config/internal/AesCipherTest/aes_secret_key.txt");
return new File(resource.toURI()).getCanonicalPath();
}
}
|