Always use 'this.' when accessing fields

Apply an Eclipse cleanup rules to ensure that fields are always accessed
using `this.`. This aligns with the style used by Spring Framework and
helps users quickly see the difference between a local and member
variable.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-07-26 11:51:05 -07:00
committed by Rob Winch
parent 6894ff5d12
commit 8866fa6fb0
793 changed files with 8689 additions and 8459 deletions

View File

@@ -31,38 +31,38 @@ public class Argon2EncodingUtilsTests {
private TestDataEntry testDataEntry1 = new TestDataEntry(
"$argon2i$v=19$m=1024,t=3,p=2$Y1JkRmJDdzIzZ3oyTWx4aw$cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs",
new Argon2EncodingUtils.Argon2Hash(decoder.decode("cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs"),
new Argon2EncodingUtils.Argon2Hash(this.decoder.decode("cGE5Cbd/cx7micVhXVBdH5qTr66JI1iUyuNNVAnErXs"),
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_i)).withVersion(19).withMemoryAsKB(1024)
.withIterations(3).withParallelism(2).withSalt("cRdFbCw23gz2Mlxk".getBytes()).build()));
private TestDataEntry testDataEntry2 = new TestDataEntry(
"$argon2id$v=19$m=333,t=5,p=2$JDR8N3k1QWx0$+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA",
new Argon2EncodingUtils.Argon2Hash(
decoder.decode(
this.decoder.decode(
"+PrEoHOHsWkU9lnsxqnOFrWTVEuOh7ZRIUIbe2yUG8FgTYNCWJfHQI09JAAFKzr2JAvoejEpTMghUt0WsntQYA"),
(new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id)).withVersion(19).withMemoryAsKB(333)
.withIterations(5).withParallelism(2).withSalt("$4|7y5Alt".getBytes()).build()));
@Test
public void decodeWhenValidEncodedHashWithIThenDecodeCorrectly() {
assertArgon2HashEquals(testDataEntry1.decoded, Argon2EncodingUtils.decode(testDataEntry1.encoded));
assertArgon2HashEquals(this.testDataEntry1.decoded, Argon2EncodingUtils.decode(this.testDataEntry1.encoded));
}
@Test
public void decodeWhenValidEncodedHashWithIDThenDecodeCorrectly() {
assertArgon2HashEquals(testDataEntry2.decoded, Argon2EncodingUtils.decode(testDataEntry2.encoded));
assertArgon2HashEquals(this.testDataEntry2.decoded, Argon2EncodingUtils.decode(this.testDataEntry2.encoded));
}
@Test
public void encodeWhenValidArgumentsWithIThenEncodeToCorrectHash() {
assertThat(Argon2EncodingUtils.encode(testDataEntry1.decoded.getHash(), testDataEntry1.decoded.getParameters()))
.isEqualTo(testDataEntry1.encoded);
assertThat(Argon2EncodingUtils.encode(this.testDataEntry1.decoded.getHash(),
this.testDataEntry1.decoded.getParameters())).isEqualTo(this.testDataEntry1.encoded);
}
@Test
public void encodeWhenValidArgumentsWithID2ThenEncodeToCorrectHash() {
assertThat(Argon2EncodingUtils.encode(testDataEntry2.decoded.getHash(), testDataEntry2.decoded.getParameters()))
.isEqualTo(testDataEntry2.encoded);
assertThat(Argon2EncodingUtils.encode(this.testDataEntry2.decoded.getHash(),
this.testDataEntry2.decoded.getParameters())).isEqualTo(this.testDataEntry2.encoded);
}
@Test(expected = IllegalArgumentException.class)

View File

@@ -41,47 +41,47 @@ public class Argon2PasswordEncoderTests {
@Test
public void encodeDoesNotEqualPassword() {
String result = encoder.encode("password");
String result = this.encoder.encode("password");
assertThat(result).isNotEqualTo("password");
}
@Test
public void encodeWhenEqualPasswordThenMatches() {
String result = encoder.encode("password");
assertThat(encoder.matches("password", result)).isTrue();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("password", result)).isTrue();
}
@Test
public void encodeWhenEqualWithUnicodeThenMatches() {
String result = encoder.encode("passw\u9292rd");
assertThat(encoder.matches("pass\u9292\u9292rd", result)).isFalse();
assertThat(encoder.matches("passw\u9292rd", result)).isTrue();
String result = this.encoder.encode("passw\u9292rd");
assertThat(this.encoder.matches("pass\u9292\u9292rd", result)).isFalse();
assertThat(this.encoder.matches("passw\u9292rd", result)).isTrue();
}
@Test
public void encodeWhenNotEqualThenNotMatches() {
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("bogus", result)).isFalse();
}
@Test
public void encodeWhenEqualPasswordWithCustomParamsThenMatches() {
encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
String result = encoder.encode("password");
assertThat(encoder.matches("password", result)).isTrue();
this.encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("password", result)).isTrue();
}
@Test
public void encodeWhenRanTwiceThenResultsNotEqual() {
String password = "secret";
assertThat(encoder.encode(password)).isNotEqualTo(encoder.encode(password));
assertThat(this.encoder.encode(password)).isNotEqualTo(this.encoder.encode(password));
}
@Test
public void encodeWhenRanTwiceWithCustomParamsThenNotEquals() {
encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
this.encoder = new Argon2PasswordEncoder(20, 64, 4, 256, 4);
String password = "secret";
assertThat(encoder.encode(password)).isNotEqualTo(encoder.encode(password));
assertThat(this.encoder.encode(password)).isNotEqualTo(this.encoder.encode(password));
}
@Test
@@ -96,24 +96,24 @@ public class Argon2PasswordEncoderTests {
@Test
public void matchesWhenEncodedPassIsNullThenFalse() {
assertThat(encoder.matches("password", null)).isFalse();
assertThat(this.encoder.matches("password", null)).isFalse();
}
@Test
public void matchesWhenEncodedPassIsEmptyThenFalse() {
assertThat(encoder.matches("password", "")).isFalse();
assertThat(this.encoder.matches("password", "")).isFalse();
}
@Test
public void matchesWhenEncodedPassIsBogusThenFalse() {
assertThat(encoder.matches("password", "012345678901234567890123456789")).isFalse();
assertThat(this.encoder.matches("password", "012345678901234567890123456789")).isFalse();
}
@Test
public void encodeWhenUsingPredictableSaltThenEqualTestHash() throws Exception {
injectPredictableSaltGen();
String hash = encoder.encode("sometestpassword");
String hash = this.encoder.encode("sometestpassword");
assertThat(hash).isEqualTo(
"$argon2id$v=19$m=4096,t=3,p=1$QUFBQUFBQUFBQUFBQUFBQQ$hmmTNyJlwbb6HAvFoHFWF+u03fdb0F2qA+39oPlcAqo");
@@ -121,9 +121,9 @@ public class Argon2PasswordEncoderTests {
@Test
public void encodeWhenUsingPredictableSaltWithCustomParamsThenEqualTestHash() throws Exception {
encoder = new Argon2PasswordEncoder(16, 32, 4, 512, 5);
this.encoder = new Argon2PasswordEncoder(16, 32, 4, 512, 5);
injectPredictableSaltGen();
String hash = encoder.encode("sometestpassword");
String hash = this.encoder.encode("sometestpassword");
assertThat(hash).isEqualTo(
"$argon2id$v=19$m=512,t=5,p=4$QUFBQUFBQUFBQUFBQUFBQQ$PNv4C3K50bz3rmON+LtFpdisD7ePieLNq+l5iUHgc1k");
@@ -131,16 +131,16 @@ public class Argon2PasswordEncoderTests {
@Test
public void upgradeEncodingWhenSameEncodingThenFalse() {
String hash = encoder.encode("password");
String hash = this.encoder.encode("password");
assertThat(encoder.upgradeEncoding(hash)).isFalse();
assertThat(this.encoder.upgradeEncoding(hash)).isFalse();
}
@Test
public void upgradeEncodingWhenSameStandardParamsThenFalse() {
Argon2PasswordEncoder newEncoder = new Argon2PasswordEncoder();
String hash = encoder.encode("password");
String hash = this.encoder.encode("password");
assertThat(newEncoder.upgradeEncoding(hash)).isFalse();
}
@@ -187,30 +187,30 @@ public class Argon2PasswordEncoderTests {
@Test
public void upgradeEncodingWhenEncodedPassIsNullThenFalse() {
assertThat(encoder.upgradeEncoding(null)).isFalse();
assertThat(this.encoder.upgradeEncoding(null)).isFalse();
}
@Test
public void upgradeEncodingWhenEncodedPassIsEmptyThenFalse() {
assertThat(encoder.upgradeEncoding("")).isFalse();
assertThat(this.encoder.upgradeEncoding("")).isFalse();
}
@Test(expected = IllegalArgumentException.class)
public void upgradeEncodingWhenEncodedPassIsBogusThenThrowException() {
encoder.upgradeEncoding("thisIsNoValidHash");
this.encoder.upgradeEncoding("thisIsNoValidHash");
}
private void injectPredictableSaltGen() throws Exception {
byte[] bytes = new byte[16];
Arrays.fill(bytes, (byte) 0x41);
Mockito.when(keyGeneratorMock.generateKey()).thenReturn(bytes);
Mockito.when(this.keyGeneratorMock.generateKey()).thenReturn(bytes);
// we can't use the @InjectMock-annotation because the salt-generator is set in
// the constructor
// and Mockito will only inject mocks if they are null
Field saltGen = encoder.getClass().getDeclaredField("saltGenerator");
Field saltGen = this.encoder.getClass().getDeclaredField("saltGenerator");
saltGen.setAccessible(true);
saltGen.set(encoder, keyGeneratorMock);
saltGen.set(this.encoder, this.keyGeneratorMock);
saltGen.setAccessible(false);
}

View File

@@ -54,29 +54,29 @@ public class HexTests {
@Test
public void decodeNotEven() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Hex-encoded string must have an even number of characters");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Hex-encoded string must have an even number of characters");
Hex.decode("414243444");
}
@Test
public void decodeExistNonHexCharAtFirst() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Detected a Non-hex character at 1 or 2 position");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Detected a Non-hex character at 1 or 2 position");
Hex.decode("G0");
}
@Test
public void decodeExistNonHexCharAtSecond() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Detected a Non-hex character at 3 or 4 position");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Detected a Non-hex character at 3 or 4 position");
Hex.decode("410G");
}
@Test
public void decodeExistNonHexCharAtBoth() {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Detected a Non-hex character at 5 or 6 position");
this.expectedException.expect(IllegalArgumentException.class);
this.expectedException.expectMessage("Detected a Non-hex character at 5 or 6 position");
Hex.decode("4142GG");
}

View File

@@ -41,64 +41,65 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
@Before
public void setup() {
// generate random password, salt, and test data
password = UUID.randomUUID().toString();
this.password = UUID.randomUUID().toString();
/** insecure salt byte, recommend 64 or larger than 64 */
byte[] saltBytes = new byte[16];
secureRandom.nextBytes(saltBytes);
salt = new String(Hex.encode(saltBytes));
this.secureRandom.nextBytes(saltBytes);
this.salt = new String(Hex.encode(saltBytes));
}
@Test
public void bouncyCastleAesCbcWithPredictableIvEquvalent() throws Exception {
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16));
testEquivalence(bcEncryptor, jceEncryptor);
}
@Test
public void bouncyCastleAesCbcWithSecureIvCompatible() throws Exception {
CryptoAssumptions.assumeCBCJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(16));
testCompatibility(bcEncryptor, jceEncryptor);
}
@Test
public void bouncyCastleAesGcmWithPredictableIvEquvalent() throws Exception {
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, new PredictableRandomBytesKeyGenerator(16),
CipherAlgorithm.GCM);
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt,
new PredictableRandomBytesKeyGenerator(16), CipherAlgorithm.GCM);
testEquivalence(bcEncryptor, jceEncryptor);
}
@Test
public void bouncyCastleAesGcmWithSecureIvCompatible() throws Exception {
CryptoAssumptions.assumeGCMJCE();
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt,
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt,
KeyGenerators.secureRandom(16));
BytesEncryptor jceEncryptor = new AesBytesEncryptor(password, salt, KeyGenerators.secureRandom(16),
BytesEncryptor jceEncryptor = new AesBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(16),
CipherAlgorithm.GCM);
testCompatibility(bcEncryptor, jceEncryptor);
}
private void testEquivalence(BytesEncryptor left, BytesEncryptor right) {
for (int size = 1; size < 2048; size++) {
testData = new byte[size];
secureRandom.nextBytes(testData);
this.testData = new byte[size];
this.secureRandom.nextBytes(this.testData);
// tests that right and left generate the same encrypted bytes
// and can decrypt back to the original input
byte[] leftEncrypted = left.encrypt(testData);
byte[] rightEncrypted = right.encrypt(testData);
byte[] leftEncrypted = left.encrypt(this.testData);
byte[] rightEncrypted = right.encrypt(this.testData);
Assert.assertArrayEquals(leftEncrypted, rightEncrypted);
byte[] leftDecrypted = left.decrypt(leftEncrypted);
byte[] rightDecrypted = right.decrypt(rightEncrypted);
Assert.assertArrayEquals(testData, leftDecrypted);
Assert.assertArrayEquals(testData, rightDecrypted);
Assert.assertArrayEquals(this.testData, leftDecrypted);
Assert.assertArrayEquals(this.testData, rightDecrypted);
}
}
@@ -107,14 +108,14 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
// tests that right can decrypt what left encrypted and vice versa
// and that the decypted data is the same as the original
for (int size = 1; size < 2048; size++) {
testData = new byte[size];
secureRandom.nextBytes(testData);
byte[] leftEncrypted = left.encrypt(testData);
byte[] rightEncrypted = right.encrypt(testData);
this.testData = new byte[size];
this.secureRandom.nextBytes(this.testData);
byte[] leftEncrypted = left.encrypt(this.testData);
byte[] rightEncrypted = right.encrypt(this.testData);
byte[] leftDecrypted = left.decrypt(rightEncrypted);
byte[] rightDecrypted = right.decrypt(leftEncrypted);
Assert.assertArrayEquals(testData, leftDecrypted);
Assert.assertArrayEquals(testData, rightDecrypted);
Assert.assertArrayEquals(this.testData, leftDecrypted);
Assert.assertArrayEquals(this.testData, rightDecrypted);
}
}
@@ -133,12 +134,12 @@ public class BouncyCastleAesBytesEncryptorEquivalencyTests {
}
public int getKeyLength() {
return keyLength;
return this.keyLength;
}
public byte[] generateKey() {
byte[] bytes = new byte[keyLength];
random.nextBytes(bytes);
byte[] bytes = new byte[this.keyLength];
this.random.nextBytes(bytes);
return bytes;
}

View File

@@ -38,44 +38,44 @@ public class BouncyCastleAesBytesEncryptorTests {
public void setup() {
// generate random password, salt, and test data
SecureRandom secureRandom = new SecureRandom();
password = UUID.randomUUID().toString();
this.password = UUID.randomUUID().toString();
byte[] saltBytes = new byte[16];
secureRandom.nextBytes(saltBytes);
salt = new String(Hex.encode(saltBytes));
testData = new byte[1024 * 1024];
secureRandom.nextBytes(testData);
this.salt = new String(Hex.encode(saltBytes));
this.testData = new byte[1024 * 1024];
secureRandom.nextBytes(this.testData);
}
@Test
public void bcCbcWithSecureIvGeneratesDifferentMessages() {
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(password, salt);
BytesEncryptor bcEncryptor = new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt);
generatesDifferentCipherTexts(bcEncryptor);
}
@Test
public void bcGcmWithSecureIvGeneratesDifferentMessages() {
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(password, salt);
BytesEncryptor bcEncryptor = new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt);
generatesDifferentCipherTexts(bcEncryptor);
}
private void generatesDifferentCipherTexts(BytesEncryptor bcEncryptor) {
byte[] encrypted1 = bcEncryptor.encrypt(testData);
byte[] encrypted2 = bcEncryptor.encrypt(testData);
byte[] encrypted1 = bcEncryptor.encrypt(this.testData);
byte[] encrypted2 = bcEncryptor.encrypt(this.testData);
Assert.assertFalse(Arrays.areEqual(encrypted1, encrypted2));
byte[] decrypted1 = bcEncryptor.decrypt(encrypted1);
byte[] decrypted2 = bcEncryptor.decrypt(encrypted2);
Assert.assertArrayEquals(testData, decrypted1);
Assert.assertArrayEquals(testData, decrypted2);
Assert.assertArrayEquals(this.testData, decrypted1);
Assert.assertArrayEquals(this.testData, decrypted2);
}
@Test(expected = IllegalArgumentException.class)
public void bcCbcWithWrongLengthIv() {
new BouncyCastleAesCbcBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
new BouncyCastleAesCbcBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(8));
}
@Test(expected = IllegalArgumentException.class)
public void bcGcmWithWrongLengthIv() {
new BouncyCastleAesGcmBytesEncryptor(password, salt, KeyGenerators.secureRandom(8));
new BouncyCastleAesGcmBytesEncryptor(this.password, this.salt, KeyGenerators.secureRandom(8));
}
}

View File

@@ -220,7 +220,7 @@ public class DelegatingPasswordEncoderTests {
public void upgradeEncodingWhenSameIdAndEncoderFalseThenEncoderDecidesFalse() {
assertThat(this.passwordEncoder.upgradeEncoding(this.bcryptEncodedPassword)).isFalse();
verify(bcrypt).upgradeEncoding(this.encodedPassword);
verify(this.bcrypt).upgradeEncoding(this.encodedPassword);
}
@Test
@@ -229,14 +229,14 @@ public class DelegatingPasswordEncoderTests {
assertThat(this.passwordEncoder.upgradeEncoding(this.bcryptEncodedPassword)).isTrue();
verify(bcrypt).upgradeEncoding(this.encodedPassword);
verify(this.bcrypt).upgradeEncoding(this.encodedPassword);
}
@Test
public void upgradeEncodingWhenDifferentIdThenTrue() {
assertThat(this.passwordEncoder.upgradeEncoding(this.noopEncodedPassword)).isTrue();
verifyZeroInteractions(bcrypt);
verifyZeroInteractions(this.bcrypt);
}
}

View File

@@ -26,21 +26,21 @@ public class StandardPasswordEncoderTests {
@Test
public void matches() {
String result = encoder.encode("password");
String result = this.encoder.encode("password");
assertThat(result).isNotEqualTo("password");
assertThat(encoder.matches("password", result)).isTrue();
assertThat(this.encoder.matches("password", result)).isTrue();
}
@Test
public void matchesLengthChecked() {
String result = encoder.encode("password");
assertThat(encoder.matches("password", result.substring(0, result.length() - 2))).isFalse();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("password", result.substring(0, result.length() - 2))).isFalse();
}
@Test
public void notMatches() {
String result = encoder.encode("password");
assertThat(encoder.matches("bogus", result)).isFalse();
String result = this.encoder.encode("password");
assertThat(this.encoder.matches("bogus", result)).isFalse();
}
}