Skip to content

Commit 4a121f6

Browse files
SONARJAVA-4895: Add tests for non-compiling cases
1 parent 7c00ea5 commit 4a121f6

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package checks.security;
2+
3+
import java.security.InvalidAlgorithmParameterException;
4+
import java.security.InvalidKeyException;
5+
import java.security.SecureRandom;
6+
import javax.crypto.Cipher;
7+
import javax.crypto.spec.IvParameterSpec;
8+
import javax.crypto.spec.SecretKeySpec;
9+
10+
import static javax.crypto.Cipher.ENCRYPT_MODE;
11+
12+
public class CipherBlockChainingCheckShouldDetectCustomIVFactories {
13+
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
14+
15+
// We use fields to omit the details of cipher and secret key creation because they are not relevant for this test.
16+
static Cipher cipher;
17+
static SecretKeySpec secretKey;
18+
19+
static class Control {
20+
void should_raise_issue_for_insecure_iv() throws InvalidAlgorithmParameterException, InvalidKeyException {
21+
final byte[] insecureIv = new byte[42];
22+
cipher.init(ENCRYPT_MODE, secretKey, new IvParameterSpec(insecureIv)); // Noncompliant
23+
}
24+
}
25+
26+
void should_not_raise_if_iv_generator_does_not_return_byte_array_01() throws InvalidAlgorithmParameterException, InvalidKeyException {
27+
final byte[] iv = returnsNothing();
28+
// This code is non-compiling and the called iv generator is not recognized as secure because it has the wrong return type.
29+
// We accept possible FPs in such cases:
30+
cipher.init(ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); // Noncompliant
31+
}
32+
33+
void should_not_raise_if_iv_generator_does_not_return_byte_array_02() throws InvalidAlgorithmParameterException, InvalidKeyException {
34+
final byte[] iv = returnsIntArray(42);
35+
// This code is non-compiling and the called iv generator is not recognized as secure because it has the wrong return type.
36+
// We accept possible FPs in such cases:
37+
cipher.init(ENCRYPT_MODE, secretKey, new IvParameterSpec(iv)); // Noncompliant
38+
}
39+
40+
private void returnsNothing() {
41+
}
42+
43+
private int[] returnsIntArray(final int length) {
44+
return new int[length];
45+
}
46+
}

java-checks/src/test/java/org/sonar/java/checks/security/CipherBlockChainingCheckTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,38 @@
2323
import static org.sonar.java.checks.verifier.TestUtils.nonCompilingTestSourcesPath;
2424

2525
class CipherBlockChainingCheckTest {
26-
27-
private static final String SOURCE_PATH = "checks/security/CipherBlockChainingCheck.java";
26+
private static final String BASE_PATH = "checks/security";
27+
private static final String DEFAULT_SOURCE_PATH = BASE_PATH + "/CipherBlockChainingCheck.java";
28+
private static final String CUSTOM_IV_FACTORY_DETECTION_SOURCE_PATH = BASE_PATH + "/CipherBlockChainingCheckShouldDetectCustomIVFactories.java";
2829

2930
@Test
3031
void test() {
3132
CheckVerifier.newVerifier()
32-
.onFile(mainCodeSourcesPath(SOURCE_PATH))
33+
.onFile(mainCodeSourcesPath(DEFAULT_SOURCE_PATH))
34+
.withCheck(new CipherBlockChainingCheck())
35+
.verifyIssues();
36+
}
37+
38+
@Test
39+
void test_non_compiling() {
40+
CheckVerifier.newVerifier()
41+
.onFile(nonCompilingTestSourcesPath(DEFAULT_SOURCE_PATH))
3342
.withCheck(new CipherBlockChainingCheck())
3443
.verifyIssues();
3544
}
3645

3746
@Test
3847
void should_detect_custom_iv_factories() {
3948
CheckVerifier.newVerifier()
40-
.onFile(mainCodeSourcesPath("checks/security/CipherBlockChainingCheckShouldDetectCustomIVFactories.java"))
49+
.onFile(mainCodeSourcesPath(CUSTOM_IV_FACTORY_DETECTION_SOURCE_PATH))
4150
.withCheck(new CipherBlockChainingCheck())
4251
.verifyIssues();
4352
}
4453

4554
@Test
46-
void test_non_compiling() {
55+
void should_detect_custom_iv_factories_non_compiling() {
4756
CheckVerifier.newVerifier()
48-
.onFile(nonCompilingTestSourcesPath(SOURCE_PATH))
57+
.onFile(nonCompilingTestSourcesPath(CUSTOM_IV_FACTORY_DETECTION_SOURCE_PATH))
4958
.withCheck(new CipherBlockChainingCheck())
5059
.verifyIssues();
5160
}

0 commit comments

Comments
 (0)