Skip to content

Commit 02039e2

Browse files
committed
RM-3063: fix sonar warnings in unit test
1 parent 8676fd3 commit 02039e2

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

cdoc2-lib/src/main/java/ee/cyber/cdoc2/container/recipients/PBKDF2Recipient.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class PBKDF2Recipient extends Recipient {
2525

2626
private final byte[] encryptionSalt;
2727
private final byte[] passwordSalt;
28-
private final byte kdfAlgorithmIdentifier = KDFAlgorithmIdentifier.PBKDF2WithHmacSHA256;
29-
private final int kdfIterations = PBKDF2_ITERATIONS;
28+
private final byte kdfAlgorithmIdentifier;
29+
private final int kdfIterations;
3030

3131
public PBKDF2Recipient(
3232
byte[] encSalt,
@@ -37,6 +37,8 @@ public PBKDF2Recipient(
3737
super(encFmk, recipientLabel);
3838
this.encryptionSalt = encSalt.clone();
3939
this.passwordSalt = passwordSalt;
40+
this.kdfAlgorithmIdentifier = KDFAlgorithmIdentifier.PBKDF2WithHmacSHA256;
41+
this.kdfIterations = PBKDF2_ITERATIONS;
4042
}
4143

4244
@Override

cdoc2-lib/src/main/java/ee/cyber/cdoc2/container/recipients/Recipient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Arrays;
88
import java.util.Objects;
99

10+
1011
/**
1112
* Java POJO that represents flatbuffers {@link RecipientRecord header.RecipientRecord}
1213
* Capsule union field(s) will be implemented by subclasses.
@@ -15,11 +16,12 @@ public abstract class Recipient implements KekDerivable, SerializableFBS {
1516
// header.RecipientRecord specific fields
1617
protected final byte[] encryptedFmk;
1718
protected final String recipientKeyLabel;
18-
protected final byte fmkEncryptionMethod = FMKEncryptionMethod.XOR;
19+
protected final byte fmkEncryptionMethod;
1920

2021
protected Recipient(byte[] encFmk, String recipientLabel) {
2122
this.recipientKeyLabel = recipientLabel;
2223
this.encryptedFmk = encFmk.clone();
24+
this.fmkEncryptionMethod = FMKEncryptionMethod.XOR;
2325
}
2426

2527
public String getRecipientKeyLabel() {
@@ -59,4 +61,5 @@ public int hashCode() {
5961
result = 31 * result + Arrays.hashCode(encryptedFmk);
6062
return result;
6163
}
64+
6265
}

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

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15+
1516
/**
1617
* Curve values from {@link ee.cyber.cdoc2.fbs.recipients.EllipticCurve} defined as enum and mapped to
1718
* known elliptic curve names and oid's
1819
*/
1920
public enum EllipticCurve {
21+
2022
UNKNOWN(ee.cyber.cdoc2.fbs.recipients.EllipticCurve.UNKNOWN, null, null),
2123
SECP384R1(ee.cyber.cdoc2.fbs.recipients.EllipticCurve.secp384r1, ECKeys.SECP_384_R_1, ECKeys.SECP_384_OID);
2224

@@ -31,6 +33,7 @@ public enum EllipticCurve {
3133
this.name = name;
3234
this.oid = oid;
3335
}
36+
3437
public byte getValue() {
3538
return value;
3639
}
@@ -43,43 +46,34 @@ public String getOid() {
4346
}
4447

4548
public boolean isValidKey(ECPublicKey key) throws GeneralSecurityException {
46-
switch (this) {
47-
case SECP384R1:
48-
return ECKeys.isValidSecP384R1(key);
49-
default:
50-
throw new IllegalStateException("isValidKey not implemented for " + this);
49+
if (this == EllipticCurve.SECP384R1) {
50+
return ECKeys.isValidSecP384R1(key);
5151
}
52+
throw new IllegalStateException("isValidKey not implemented for " + this);
5253
}
5354

5455
public boolean isValidKeyPair(KeyPair keyPair) throws GeneralSecurityException {
55-
switch (this) {
56-
case SECP384R1:
57-
return ECKeys.isECSecp384r1(keyPair);
58-
default:
59-
throw new IllegalStateException("isValidKeyPair not implemented for " + this);
56+
if (this == EllipticCurve.SECP384R1) {
57+
return ECKeys.isECSecp384r1(keyPair);
6058
}
59+
throw new IllegalStateException("isValidKeyPair not implemented for " + this);
6160
}
6261

6362
/**
6463
* Key length in bytes. For secp384r1, its 384/8=48
6564
*/
6665
public int getKeyLength() {
67-
switch (this) {
68-
case SECP384R1:
69-
return ECKeys.SECP_384_R_1_LEN_BYTES;
70-
default:
71-
throw new IllegalStateException("getKeyLength not implemented for " + this);
66+
if (this == EllipticCurve.SECP384R1) {
67+
return ECKeys.SECP_384_R_1_LEN_BYTES;
7268
}
69+
throw new IllegalStateException("getKeyLength not implemented for " + this);
7370
}
7471

7572
public ECPublicKey decodeFromTls(ByteBuffer encoded) throws GeneralSecurityException {
76-
switch (this) {
77-
case SECP384R1:
78-
// calls also isValidSecP384R1
79-
return ECKeys.decodeSecP384R1EcPublicKeyFromTls(encoded);
80-
default:
81-
throw new IllegalStateException("decodeFromTls not implemented for " + this);
73+
if (this == EllipticCurve.SECP384R1) { // calls also isValidSecP384R1
74+
return ECKeys.decodeSecP384R1EcPublicKeyFromTls(encoded);
8275
}
76+
throw new IllegalStateException("decodeFromTls not implemented for " + this);
8377
}
8478

8579
public KeyPair generateEcKeyPair() throws GeneralSecurityException {
@@ -101,17 +95,15 @@ public static EllipticCurve forOid(String oid) throws NoSuchAlgorithmException {
10195
}
10296

10397
public static EllipticCurve forValue(byte value) throws NoSuchAlgorithmException {
104-
switch (value) {
105-
case ee.cyber.cdoc2.fbs.recipients.EllipticCurve.secp384r1:
106-
return SECP384R1;
107-
default:
108-
throw new NoSuchAlgorithmException("Unknown EC curve value " + value);
98+
if (value == ee.cyber.cdoc2.fbs.recipients.EllipticCurve.secp384r1) {
99+
return SECP384R1;
109100
}
101+
throw new NoSuchAlgorithmException("Unknown EC curve value " + value);
110102
}
111103

112104
/**
113105
* @param publicKey ECPublicKey
114-
* @return
106+
* @return EllipticCurve
115107
* @throws NoSuchAlgorithmException if publicKey EC curve is not supported
116108
* @throws InvalidParameterSpecException
117109
* @throws NoSuchProviderException
@@ -120,8 +112,7 @@ public static EllipticCurve forValue(byte value) throws NoSuchAlgorithmException
120112
public static EllipticCurve forPubKey(PublicKey publicKey) throws NoSuchAlgorithmException,
121113
InvalidParameterSpecException, NoSuchProviderException, InvalidKeyException {
122114

123-
if (publicKey instanceof ECPublicKey) {
124-
ECPublicKey ecPublicKey = (ECPublicKey) publicKey;
115+
if (publicKey instanceof ECPublicKey ecPublicKey) {
125116
return forOid(ECKeys.getCurveOid(ecPublicKey));
126117
} else {
127118
throw new InvalidKeyException("Unsupported key algorithm " + publicKey.getAlgorithm());
@@ -150,4 +141,5 @@ public static boolean isSupported(PublicKey publicKey) {
150141
public static String[] names() {
151142
return ee.cyber.cdoc2.fbs.recipients.EllipticCurve.names;
152143
}
144+
153145
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,17 +196,18 @@ void testMaxExtractEntries(@TempDir Path tempDir) {
196196

197197
@Test
198198
void shouldValidateFileNameWhenCreatingTar(@TempDir Path tempDir) throws IOException {
199-
File outputTarFile = tempDir.resolve(TGZ_FILE_NAME).toFile();
199+
tempDir.resolve(TGZ_FILE_NAME).toFile();
200200

201201
assertFalse(INVALID_FILE_NAMES.isEmpty());
202202

203203
// should fail
204204
for (String fileName: INVALID_FILE_NAMES) {
205205
File file = createAndWriteToFile(tempDir, fileName, PAYLOAD);
206206
OutputStream os = new ByteArrayOutputStream();
207+
List<File> files = List.of(file);
207208
assertThrows(
208209
InvalidPathException.class,
209-
() -> Tar.archiveFiles(os, List.of(file)),
210+
() -> Tar.archiveFiles(os, files),
210211
"File with name '" + file + "' should not be allowed in created tar"
211212
);
212213
}
@@ -282,7 +283,7 @@ void findZlibMinSize() throws IOException {
282283
}
283284

284285
@Test
285-
void shouldSupportLongFileName(@TempDir Path tempDir) throws IOException {
286+
void shouldSupportLongFileName() throws IOException {
286287
byte[] data = {0x00};
287288
ByteArrayOutputStream destTarZ = new ByteArrayOutputStream();
288289

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ void testLoadKeyPairFromPemShort() throws GeneralSecurityException, IOException
291291
assertTrue(KeyAlgorithm.isEcKeysAlgorithm(ecPrivKey.getAlgorithm()));
292292
assertEquals(expectedSecretHex, ecPrivKey.getS().toString(16));
293293

294-
295294
if (log.isDebugEnabled()) {
296295
log.debug("pub: {}", HexFormat.of().formatHex(ECKeys.encodeEcPubKeyForTls(ecPublicKey)));
297296
}

0 commit comments

Comments
 (0)