Skip to content

Commit c6c8e2d

Browse files
committed
RM-3063: fix sonar warnings in unit tests, suppress where required
1 parent 24f2514 commit c6c8e2d

File tree

9 files changed

+37
-31
lines changed

9 files changed

+37
-31
lines changed

cdoc2-example-app/src/main/java/ee/cyber/cdoc2/converter/ConverterCmd.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import java.util.Arrays;
1818
import java.util.concurrent.Callable;
1919

20-
@Command( name = "cdoc-convert"
21-
)
20+
@Command( name = "cdoc-convert")
21+
@SuppressWarnings("squid:S106")
2222
public class ConverterCmd implements Callable<Void> {
2323

2424
private static final Logger log = LoggerFactory.getLogger(ConverterCmd.class);
@@ -48,8 +48,6 @@ public class ConverterCmd implements Callable<Void> {
4848
@Option(names = { "-h", "--help" }, usageHelp = false, description = "display a help message")
4949
private boolean helpRequested = false;
5050

51-
52-
5351
public static void main(String... args) {
5452

5553
if (args.length == 0) {
@@ -61,7 +59,6 @@ public static void main(String... args) {
6159
System.exit(exitCode);
6260
}
6361

64-
6562
@Override
6663
public Void call() throws Exception {
6764

cdoc2-lib/src/main/java/ee/cyber/cdoc2/crypto/KekTools.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public static byte[] deriveKekForSymmetricKey(
6060
secretKey,
6161
recipient.getSalt(),
6262
FMKEncryptionMethod.name(recipient.getFmkEncryptionMethod()));
63-
log.debug("kek={}", HexFormat.of().formatHex(kek.getEncoded()));
63+
if (log.isDebugEnabled()) {
64+
log.debug("kek={}", HexFormat.of().formatHex(kek.getEncoded()));
65+
}
6466
return kek.getEncoded();
6567
}
6668

cdoc2-lib/src/main/java/ee/cyber/cdoc2/crypto/keymaterial/encrypt/EncryptionKeyMaterialCollectionBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public EncryptionKeyMaterialCollectionBuilder fromEId(String[] identificationCod
9494
List<EncryptionKeyMaterial> keyMaterials = certData.stream()
9595
.filter(entry -> EllipticCurve.isSupported(entry.getPublicKey()))
9696
.map(SkLdapUtil::toEncryptionKeyMaterial)
97-
.collect(Collectors.toList());
97+
.toList();
9898

9999
recipients.addAll(keyMaterials);
100100
return this;

cdoc2-lib/src/main/java/ee/cyber/cdoc2/util/OperatingSystem.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ public enum OperatingSystem {
1818
* @return the operating system
1919
*/
2020
public static OperatingSystem getOS() {
21-
log.debug("os.family: {}, os.name: {}", System.getProperty("os.family"), System.getProperty(OS_NAME));
21+
if (log.isDebugEnabled()) {
22+
log.debug("os.family: {}, os.name: {}", System.getProperty("os.family"), System.getProperty(OS_NAME));
23+
}
2224
String os = System.getProperty(OS_NAME).toLowerCase();
2325

2426
if (os.contains("win")) {
@@ -31,9 +33,11 @@ public static OperatingSystem getOS() {
3133
return OperatingSystem.MAC;
3234
}
3335

34-
log.error("Unknown operating system: os.family: {}, os.name: {}",
35-
System.getProperty("os.family"), System.getProperty(OS_NAME)
36-
);
36+
if (log.isErrorEnabled()) {
37+
log.error("Unknown operating system: os.family: {}, os.name: {}",
38+
System.getProperty("os.family"), System.getProperty(OS_NAME)
39+
);
40+
}
3741
throw new IllegalStateException("Unknown OS");
3842
}
3943
}

cdoc2-lib/src/main/java/ee/cyber/cdoc2/util/SkLdapUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ private SkLdapUtil() {
5959
// distinguished name fragment for authentication certificates using e-resident digi-id
6060
private static final String AUTH_E_RESIDENT_DIGI_ID = AUTH_CERT_PART + E_RESIDENT_DIGI_ID;
6161

62+
@SuppressWarnings("java:S1149")
6263
private static DirContext initDirContext() throws NamingException {
6364
Hashtable<String, Object> env = new Hashtable<>(11);
6465
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

cdoc2-lib/src/test/java/ee/cyber/cdoc2/container/EnvelopeTestUtils.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,13 @@ static byte[] createTarWithExtraData() throws IOException {
171171

172172
log.debug("Compressed {} into {}", randomBytes.length, destTarZlib.size());
173173

174-
// Tar is able to process empty tar without exceptions
175-
//assertTrue(Tar.listFiles(new ByteArrayInputStream(destTarZlib.toByteArray())).isEmpty());
176174
return destTarZlib.toByteArray();
177175
}
178176

179177
static byte[] createTarWithIllegalFileType() throws IOException {
180178

181179
String validFileName = "validFile";
182180
byte[] data = new byte[10 * 1024];
183-
//new Random().nextBytes(data);
184181

185182
ByteArrayOutputStream dest = new ByteArrayOutputStream();
186183

cdoc2-lib/src/test/java/ee/cyber/cdoc2/container/TarDeflateTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,11 @@ void testArchiveData(@TempDir Path tempDir) throws IOException {
132132
void testTarGzBomb(@TempDir Path tempDir) throws IOException {
133133
byte[] zeros = new byte[1024]; //1KB
134134

135+
// can multiply with 1024 ones more for 1GM size
135136
long bigFileSize = 1024 //1KB
136137
* 1024; //1MB
137-
//*1024 //1GB
138-
//;
139138

140139
Path bombPath = tempDir.resolve("bomb.tgz");
141-
//bombPath.toFile().deleteOnExit();
142140

143141
try (TarArchiveOutputStream tarOs = new TarArchiveOutputStream(new DeflateCompressorOutputStream(
144142
new BufferedOutputStream(Files.newOutputStream(bombPath))))) {
@@ -161,14 +159,14 @@ void testTarGzBomb(@TempDir Path tempDir) throws IOException {
161159
tarOs.closeArchiveEntry();
162160
}
163161

164-
165162
Path outDir = tempDir.resolve("testTarGzBomb");
166163
Files.createDirectories(outDir);
167164

168165
log.debug("Extracting {} to {}", bombPath, outDir);
169-
Exception exception = assertThrows(IllegalStateException.class, () -> {
170166

171-
try (TarDeflate tar = new TarDeflate(Files.newInputStream(bombPath))) {
167+
InputStream inputStream = Files.newInputStream(bombPath);
168+
Exception exception = assertThrows(IllegalStateException.class, () -> {
169+
try (TarDeflate tar = new TarDeflate(inputStream)) {
172170
tar.extractToDir(outDir);
173171
}
174172
});
@@ -243,7 +241,7 @@ void shouldValidateFileNameWhenExtractingTar(@TempDir Path tempDir) throws IOExc
243241
File file = createTar(tempDir, TGZ_FILE_NAME + '.' + i++, fileName, PAYLOAD);
244242
var result = new TarDeflate(new FileInputStream(file))
245243
.extractFilesToDir(List.of(fileName), tempDir);
246-
assertTrue(result.size() == 1);
244+
assertEquals(1, result.size());
247245
}
248246
}
249247

cdoc2-lib/src/test/java/ee/cyber/cdoc2/crypto/ECKeysTest.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ void testEcPubKeyEncodeDecode() throws GeneralSecurityException {
7070
void testLoadEcPrivKey() throws GeneralSecurityException, IOException {
7171
@SuppressWarnings("checkstyle:OperatorWrap")
7272
String privKeyPem =
73-
"-----BEGIN EC PRIVATE KEY-----\n" +
74-
"MIGkAgEBBDBh1UAT832Nh2ZXvdc5JbNv3BcEZSYk90esUkSPFmg2XEuoA7avS/kd\n" +
75-
"4HtHGRbRRbagBwYFK4EEACKhZANiAASERl1rD+bm2aoiuGicY8obRkcs+jt8ks4j\n" +
76-
"C1jD/f/EQ8KdFYrJ+KwnM6R8rIXqDnUnLJFiF3OzDpu8TUjVOvdXgzQL+n67QiLd\n" +
77-
"yerTE6f5ujIXoXNkZB8O2kX/3vADuDA=\n" +
78-
"-----END EC PRIVATE KEY-----\n";
73+
"""
74+
-----BEGIN EC PRIVATE KEY-----
75+
MIGkAgEBBDBh1UAT832Nh2ZXvdc5JbNv3BcEZSYk90esUkSPFmg2XEuoA7avS/kd
76+
4HtHGRbRRbagBwYFK4EEACKhZANiAASERl1rD+bm2aoiuGicY8obRkcs+jt8ks4j
77+
C1jD/f/EQ8KdFYrJ+KwnM6R8rIXqDnUnLJFiF3OzDpu8TUjVOvdXgzQL+n67QiLd
78+
yerTE6f5ujIXoXNkZB8O2kX/3vADuDA=
79+
-----END EC PRIVATE KEY-----
80+
""";
7981

8082
// openssl ec -in key.pem -text -noout
8183
// read EC key
@@ -283,12 +285,16 @@ void testLoadKeyPairFromPemShort() throws GeneralSecurityException, IOException
283285
ECPrivateKey ecPrivKey = (ECPrivateKey) keyPair.getPrivate();
284286
ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
285287

286-
//log.debug("key: {}", ecPrivKey.getS().toString(16));
288+
if (log.isDebugEnabled()) {
289+
log.debug("key: {}", ecPrivKey.getS().toString(16));
290+
}
287291
assertTrue(KeyAlgorithm.isEcKeysAlgorithm(ecPrivKey.getAlgorithm()));
288292
assertEquals(expectedSecretHex, ecPrivKey.getS().toString(16));
289293

290294

291-
//log.debug("pub: {}", HexFormat.of().formatHex(ECKeys.encodeEcPubKeyForTls(ecPublicKey)));
295+
if (log.isDebugEnabled()) {
296+
log.debug("pub: {}", HexFormat.of().formatHex(ECKeys.encodeEcPubKeyForTls(ecPublicKey)));
297+
}
292298
assertTrue(KeyAlgorithm.isEcKeysAlgorithm(ecPublicKey.getAlgorithm()));
293299
assertEquals(expectedPubHex, HexFormat.of().formatHex(ECKeys.encodeEcPubKeyForTls(ecPublicKey)));
294300
}

cdoc2-schema/src/test/java/ee/cyber/cdoc2/fbs/header/FbsHeaderTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@
99

1010
import com.google.flatbuffers.FlatBufferBuilder;
1111

12-
import java.io.IOException;
1312
import java.nio.ByteBuffer;
1413
import java.util.Arrays;
1514

1615
import static ee.cyber.cdoc2.fbs.header.Capsule.recipients_KeyServerCapsule;
1716

17+
1818
class FbsHeaderTest {
1919

2020
public static final int KEYLEN_BYTES = 256 / 8;
2121

2222
@Test
23-
void testFbsHeaderSerialization() throws IOException {
23+
@SuppressWarnings("java:S125")
24+
void testFbsHeaderSerialization() {
2425

2526
byte payloadEnc = PayloadEncryptionMethod.CHACHA20POLY1305;
2627

0 commit comments

Comments
 (0)