From f106a72e3f5b13a888f714d600a35a09d2434f08 Mon Sep 17 00:00:00 2001 From: Arne Babenhauserheide Date: Sun, 10 Sep 2023 19:23:07 +0200 Subject: [PATCH 1/5] Use assertArrayEquals and add proper error messages to asserts --- test/freenet/crypt/CTRBlockCipherTest.java | 4 ++-- test/freenet/crypt/CryptByteBufferTest.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/freenet/crypt/CTRBlockCipherTest.java b/test/freenet/crypt/CTRBlockCipherTest.java index 51c4e174781..be13756b8b7 100644 --- a/test/freenet/crypt/CTRBlockCipherTest.java +++ b/test/freenet/crypt/CTRBlockCipherTest.java @@ -226,7 +226,7 @@ private void checkNISTRandomLength(int bits, byte[] key, byte[] iv, } c.doFinal(plaintext, 0, plaintext.length - inputPtr, output, outputPtr); - assertTrue(Arrays.equals(output, ciphertext)); + assertArrayEquals(output, ciphertext); } Rijndael cipher = new Rijndael(bits, 128); @@ -242,7 +242,7 @@ private void checkNISTRandomLength(int bits, byte[] key, byte[] iv, ctr.processBytes(plaintext, ptr, count, output, ptr); ptr += count; } - assertTrue(Arrays.equals(output, ciphertext)); + assertArrayEquals(output, ciphertext); } } diff --git a/test/freenet/crypt/CryptByteBufferTest.java b/test/freenet/crypt/CryptByteBufferTest.java index b4040b5ef40..6c328f8f6a8 100755 --- a/test/freenet/crypt/CryptByteBufferTest.java +++ b/test/freenet/crypt/CryptByteBufferTest.java @@ -95,11 +95,13 @@ public void testRoundOneByte() throws GeneralSecurityException { byte[] buf = origPlaintext.clone(); for(int j=0;j Date: Wed, 27 Sep 2023 08:30:50 +0200 Subject: [PATCH 2/5] Fix rebase error --- test/freenet/client/filter/ContentFilterTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/freenet/client/filter/ContentFilterTest.java b/test/freenet/client/filter/ContentFilterTest.java index a680db592e3..0a779dfbade 100644 --- a/test/freenet/client/filter/ContentFilterTest.java +++ b/test/freenet/client/filter/ContentFilterTest.java @@ -184,8 +184,8 @@ public void testHTMLFilter() throws Exception { assertEquals(CSS_SPEC_EXAMPLE1, htmlFilter(CSS_SPEC_EXAMPLE1)); - assertEquals(SPAN_WITH_STYLE, HTMLFilter(SPAN_WITH_STYLE)); - assertEquals(HTML5_TAGS, HTMLFilter(HTML5_TAGS)); + assertEquals(SPAN_WITH_STYLE, htmlFilter(SPAN_WITH_STYLE)); + assertEquals(HTML5_TAGS, htmlFilter(HTML5_TAGS)); assertEquals(BASE_HREF, htmlFilter(BASE_HREF)); assertEquals(DELETED_BASE_HREF, htmlFilter(BAD_BASE_HREF)); From dad3143f6db33399508f10d19cdc1bb5a012199f Mon Sep 17 00:00:00 2001 From: Arne Babenhauserheide Date: Wed, 27 Sep 2023 08:32:01 +0200 Subject: [PATCH 3/5] Switch to Hamcrest matches to get better error reporting --- test/freenet/crypt/CTRBlockCipherTest.java | 22 ++++----- test/freenet/crypt/CryptByteBufferTest.java | 53 +++++++++++---------- 2 files changed, 39 insertions(+), 36 deletions(-) diff --git a/test/freenet/crypt/CTRBlockCipherTest.java b/test/freenet/crypt/CTRBlockCipherTest.java index be13756b8b7..60b7ee3ea72 100644 --- a/test/freenet/crypt/CTRBlockCipherTest.java +++ b/test/freenet/crypt/CTRBlockCipherTest.java @@ -1,11 +1,11 @@ package freenet.crypt; -import static org.junit.Assert.*; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; -import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; @@ -183,7 +183,7 @@ private void checkNIST(int bits, byte[] key, byte[] iv, byte[] plaintext, Cipher c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider); c.init(Cipher.ENCRYPT_MODE, k, new IvParameterSpec(iv)); byte[] output = c.doFinal(plaintext); - assertTrue(Arrays.equals(output, ciphertext)); + assertThat(output, equalTo(ciphertext)); } Rijndael cipher = new Rijndael(bits, 128); @@ -192,7 +192,7 @@ private void checkNIST(int bits, byte[] key, byte[] iv, byte[] plaintext, ctr.init(iv); byte[] output = new byte[plaintext.length]; ctr.processBytes(plaintext, 0, plaintext.length, output, 0); - assertTrue(Arrays.equals(output, ciphertext)); + assertThat(output, equalTo(ciphertext)); } private void checkNISTRandomLength(int bits, byte[] key, byte[] iv, @@ -226,7 +226,7 @@ private void checkNISTRandomLength(int bits, byte[] key, byte[] iv, } c.doFinal(plaintext, 0, plaintext.length - inputPtr, output, outputPtr); - assertArrayEquals(output, ciphertext); + assertThat(output, equalTo(ciphertext)); } Rijndael cipher = new Rijndael(bits, 128); @@ -242,7 +242,7 @@ private void checkNISTRandomLength(int bits, byte[] key, byte[] iv, ctr.processBytes(plaintext, ptr, count, output, ptr); ptr += count; } - assertArrayEquals(output, ciphertext); + assertThat(output, equalTo(ciphertext)); } } @@ -263,7 +263,7 @@ public void testRandomJCA() throws NoSuchAlgorithmException, NoSuchPaddingExcept c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider); c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv)); byte[] decrypted = c.doFinal(output); - assertTrue(Arrays.equals(decrypted, plaintext)); + assertThat(decrypted, equalTo(plaintext)); } } @@ -288,17 +288,17 @@ public void testRandom() throws UnsupportedCipherException, NoSuchAlgorithmExcep ctr.init(iv); byte[] finalPlaintext = new byte[plaintext.length]; ctr.processBytes(ciphertext, 0, ciphertext.length, finalPlaintext, 0); - assertTrue(Arrays.equals(finalPlaintext, plaintext)); + assertThat(finalPlaintext, equalTo(plaintext)); if(TEST_JCA) { SecretKeySpec k = new SecretKeySpec(key, "AES"); Cipher c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider); c.init(Cipher.ENCRYPT_MODE, k, new IvParameterSpec(iv)); byte[] output = c.doFinal(plaintext); - assertTrue(Arrays.equals(output, ciphertext)); + assertThat(output, equalTo(ciphertext)); c = Cipher.getInstance("AES/CTR/NOPADDING", Rijndael.AesCtrProvider); c.init(Cipher.DECRYPT_MODE, k, new IvParameterSpec(iv)); byte[] decrypted = c.doFinal(output); - assertTrue(Arrays.equals(decrypted, plaintext)); + assertThat(decrypted, equalTo(plaintext)); } // Now encrypt again, in random pieces. cipher.initialize(key); @@ -313,7 +313,7 @@ public void testRandom() throws UnsupportedCipherException, NoSuchAlgorithmExcep ctr.processBytes(plaintext, ptr, count, output, ptr); ptr += count; } - assertTrue(Arrays.equals(output, ciphertext)); + assertThat(output, equalTo(ciphertext)); } } diff --git a/test/freenet/crypt/CryptByteBufferTest.java b/test/freenet/crypt/CryptByteBufferTest.java index 6c328f8f6a8..6593d0d7031 100755 --- a/test/freenet/crypt/CryptByteBufferTest.java +++ b/test/freenet/crypt/CryptByteBufferTest.java @@ -3,6 +3,8 @@ * http://www.gnu.org/ for further details of the GPL. */ package freenet.crypt; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -22,6 +24,7 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; +import org.hamcrest.Matchers; import org.junit.Test; public class CryptByteBufferTest { @@ -162,10 +165,10 @@ public void testSuccessfulRoundTripInPlace() throws GeneralSecurityException { byte[] buffer = Hex.decode(plainText[i]); byte[] plaintextCopy = buffer.clone(); crypt.encrypt(buffer, 0, buffer.length); - assertTrue(!Arrays.equals(buffer, plaintextCopy)); + assertNotEquals(buffer, plaintextCopy); crypt.decrypt(buffer, 0, buffer.length); - assertArrayEquals("CryptByteBufferType: "+type.name(), - plaintextCopy, buffer); + assertThat("CryptByteBufferType: "+type.name(), + plaintextCopy, equalTo(buffer)); } } @@ -187,7 +190,7 @@ public void testSuccessfulRoundTripInPlaceOffset() throws GeneralSecurityExcepti byte[] copyBuffer = buffer.clone(); System.arraycopy(originalPlaintext, 0, buffer, header, originalPlaintext.length); crypt.encrypt(buffer, footer, originalPlaintext.length); - assertTrue(!Arrays.equals(buffer, copyBuffer)); + assertThat(buffer, Matchers.not(equalTo(copyBuffer))); crypt.decrypt(buffer, footer, originalPlaintext.length); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, footer, footer+originalPlaintext.length)); @@ -216,10 +219,10 @@ public void testSuccessfulRoundTripOutOfPlaceOffset() throws GeneralSecurityExce byte[] outBuffer = new byte[outHeader + originalPlaintext.length + outFooter]; crypt.encrypt(buffer, inFooter, originalPlaintext.length, outBuffer, outHeader); - assertTrue(Arrays.equals(buffer, copyBuffer)); + assertThat(buffer, equalTo(copyBuffer)); copyBuffer = outBuffer.clone(); crypt.decrypt(outBuffer, outHeader, originalPlaintext.length, buffer, inFooter); - assertTrue(Arrays.equals(copyBuffer, outBuffer)); + assertThat(copyBuffer, equalTo(outBuffer)); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, inFooter, inFooter+originalPlaintext.length)); @@ -261,14 +264,14 @@ public void testSuccessfulRoundTripByteArrayReset() throws GeneralSecurityExcept ByteBuffer decipheredtext1 = crypt.decryptCopy(ciphertext1); ByteBuffer decipheredtext2 = crypt.decryptCopy(ciphertext2); ByteBuffer decipheredtext3 = crypt.decryptCopy(ciphertext3); - assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext1)); - assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext2)); - assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext3)); + assertThat("CryptByteBufferType: "+type.name(), plain, equalTo(decipheredtext1)); + assertThat("CryptByteBufferType: "+type.name(), plain, equalTo(decipheredtext2)); + assertThat("CryptByteBufferType: "+type.name(), plain, equalTo(decipheredtext3)); } } private void assertNotEquals(Object o1, Object o2) { - assertFalse(o1.equals(o2)); + assertThat(o1, Matchers.not(equalTo(o2))); } @Test @@ -291,18 +294,18 @@ public void testEncryptWrapByteBuffer() throws GeneralSecurityException { } ByteBuffer plaintext = ByteBuffer.wrap(buf, header, origPlaintext.length); ByteBuffer ciphertext = crypt.encryptCopy(plaintext); - assertTrue(Arrays.equals(buf, cloneBuf)); // Plaintext not modified. + assertThat(buf, equalTo(cloneBuf)); // Plaintext not modified. assertEquals(ciphertext.remaining(), origPlaintext.length); byte[] altCiphertext = crypt2.encryptCopy(origPlaintext); byte[] ciphertextBytes = new byte[origPlaintext.length]; ciphertext.get(ciphertextBytes); ciphertext.position(0); - assertTrue(Arrays.equals(altCiphertext, ciphertextBytes)); + assertThat(altCiphertext, equalTo(ciphertextBytes)); ByteBuffer deciphered = crypt.decryptCopy(ciphertext); - assertTrue(deciphered.equals(plaintext)); + assertThat(deciphered, equalTo(plaintext)); byte[] data = new byte[origPlaintext.length]; deciphered.get(data); - assertTrue(Arrays.equals(data, origPlaintext)); + assertThat(data, equalTo(origPlaintext)); } } @@ -329,14 +332,14 @@ public void testEncryptByteBufferToByteBuffer() throws GeneralSecurityException crypt.encrypt(plaintext, ciphertext); assertEquals(plaintext.position(), header+origPlaintext.length); assertEquals(ciphertext.position(), header+origPlaintext.length); - assertTrue(Arrays.equals(buf, cloneBuf)); // Plaintext not modified. + assertThat(buf, equalTo(cloneBuf)); // Plaintext not modified. plaintext.position(header); ciphertext.position(header); - assertTrue(!Arrays.equals(ciphertextBuf, copyCiphertextBuf)); + assertNotEquals(ciphertextBuf, copyCiphertextBuf); Arrays.fill(buf, (byte)0); - assertFalse(Arrays.equals(buf, cloneBuf)); + assertNotEquals(buf, cloneBuf); crypt.decrypt(ciphertext, plaintext); - assertTrue(Arrays.equals(buf, cloneBuf)); + assertThat(buf, equalTo(cloneBuf)); } } @@ -391,18 +394,18 @@ public void testDecryptWrapByteBuffer() throws GeneralSecurityException { } ByteBuffer plaintext = ByteBuffer.wrap(buf); ByteBuffer ciphertext = crypt.encryptCopy(plaintext); - assertTrue(Arrays.equals(buf, origPlaintext)); // Plaintext not modified. + assertThat(buf, equalTo(origPlaintext)); // Plaintext not modified. assertEquals(ciphertext.remaining(), origPlaintext.length); byte[] decryptBuf = new byte[header+origPlaintext.length+footer]; ciphertext.get(decryptBuf, header, origPlaintext.length); byte[] copyOfDecryptBuf = decryptBuf.clone(); ByteBuffer toDecipher = ByteBuffer.wrap(decryptBuf, header, origPlaintext.length); ByteBuffer deciphered = crypt.decryptCopy(toDecipher); - assertTrue(Arrays.equals(decryptBuf, copyOfDecryptBuf)); - assertTrue(deciphered.equals(plaintext)); + assertThat(decryptBuf, equalTo(copyOfDecryptBuf)); + assertThat(deciphered, equalTo(plaintext)); byte[] data = new byte[origPlaintext.length]; deciphered.get(data); - assertTrue(Arrays.equals(data, origPlaintext)); + assertThat(data, equalTo(origPlaintext)); } } @@ -425,14 +428,14 @@ public void testEncryptDirectByteBuffer() throws GeneralSecurityException { plaintext.clear(); byte[] copyPlaintext = new byte[origPlaintext.length]; plaintext.get(copyPlaintext); - assertTrue(Arrays.equals(origPlaintext, copyPlaintext)); // Plaintext not modified. + assertThat(origPlaintext, equalTo(copyPlaintext)); // Plaintext not modified. plaintext.clear(); assertEquals(ciphertext.remaining(), origPlaintext.length); ByteBuffer deciphered = crypt.decryptCopy(ciphertext); - assertTrue(deciphered.equals(plaintext)); + assertThat(deciphered, equalTo(plaintext)); byte[] data = new byte[origPlaintext.length]; deciphered.get(data); - assertTrue(Arrays.equals(data, origPlaintext)); + assertThat(data, equalTo(origPlaintext)); } } From 6c3b55b46682e6111e87e751732164e50aa17fbe Mon Sep 17 00:00:00 2001 From: Arne Babenhauserheide Date: Tue, 30 Jan 2024 22:27:45 +0100 Subject: [PATCH 4/5] inline assertNotEquals --- test/freenet/crypt/CryptByteBufferTest.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/test/freenet/crypt/CryptByteBufferTest.java b/test/freenet/crypt/CryptByteBufferTest.java index 6593d0d7031..e75a1d54ced 100755 --- a/test/freenet/crypt/CryptByteBufferTest.java +++ b/test/freenet/crypt/CryptByteBufferTest.java @@ -165,7 +165,7 @@ public void testSuccessfulRoundTripInPlace() throws GeneralSecurityException { byte[] buffer = Hex.decode(plainText[i]); byte[] plaintextCopy = buffer.clone(); crypt.encrypt(buffer, 0, buffer.length); - assertNotEquals(buffer, plaintextCopy); + assertThat((Object) buffer, Matchers.not(equalTo(plaintextCopy))); crypt.decrypt(buffer, 0, buffer.length); assertThat("CryptByteBufferType: "+type.name(), plaintextCopy, equalTo(buffer)); @@ -256,9 +256,9 @@ public void testSuccessfulRoundTripByteArrayReset() throws GeneralSecurityExcept // Once we have initialised the cipher, it is treated as a stream. // Repeated encryption of the same data will return different ciphertext, // as it is treated as a later point in the stream. - assertNotEquals(ciphertext1, ciphertext2); - assertNotEquals(ciphertext1, ciphertext3); - assertNotEquals(ciphertext2, ciphertext3); + assertThat((Object) ciphertext1, Matchers.not(equalTo(ciphertext2))); + assertThat((Object) ciphertext1, Matchers.not(equalTo(ciphertext3))); + assertThat((Object) ciphertext2, Matchers.not(equalTo(ciphertext3))); } ByteBuffer decipheredtext1 = crypt.decryptCopy(ciphertext1); @@ -269,10 +269,6 @@ public void testSuccessfulRoundTripByteArrayReset() throws GeneralSecurityExcept assertThat("CryptByteBufferType: "+type.name(), plain, equalTo(decipheredtext3)); } } - - private void assertNotEquals(Object o1, Object o2) { - assertThat(o1, Matchers.not(equalTo(o2))); - } @Test public void testEncryptWrapByteBuffer() throws GeneralSecurityException { @@ -335,9 +331,9 @@ public void testEncryptByteBufferToByteBuffer() throws GeneralSecurityException assertThat(buf, equalTo(cloneBuf)); // Plaintext not modified. plaintext.position(header); ciphertext.position(header); - assertNotEquals(ciphertextBuf, copyCiphertextBuf); + assertThat((Object) ciphertextBuf, Matchers.not(equalTo(copyCiphertextBuf))); Arrays.fill(buf, (byte)0); - assertNotEquals(buf, cloneBuf); + assertThat((Object) buf, Matchers.not(equalTo(cloneBuf))); crypt.decrypt(ciphertext, plaintext); assertThat(buf, equalTo(cloneBuf)); } From 7800af13bbd23fed84a64e7f4c7ad09a160e4368 Mon Sep 17 00:00:00 2001 From: Arne Babenhauserheide Date: Tue, 30 Jan 2024 22:28:47 +0100 Subject: [PATCH 5/5] import Matchers.not statically --- test/freenet/crypt/CryptByteBufferTest.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/freenet/crypt/CryptByteBufferTest.java b/test/freenet/crypt/CryptByteBufferTest.java index e75a1d54ced..d42cdaf7f94 100755 --- a/test/freenet/crypt/CryptByteBufferTest.java +++ b/test/freenet/crypt/CryptByteBufferTest.java @@ -5,11 +5,11 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.not; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.nio.ByteBuffer; @@ -24,7 +24,6 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; -import org.hamcrest.Matchers; import org.junit.Test; public class CryptByteBufferTest { @@ -165,7 +164,7 @@ public void testSuccessfulRoundTripInPlace() throws GeneralSecurityException { byte[] buffer = Hex.decode(plainText[i]); byte[] plaintextCopy = buffer.clone(); crypt.encrypt(buffer, 0, buffer.length); - assertThat((Object) buffer, Matchers.not(equalTo(plaintextCopy))); + assertThat((Object) buffer, not(equalTo(plaintextCopy))); crypt.decrypt(buffer, 0, buffer.length); assertThat("CryptByteBufferType: "+type.name(), plaintextCopy, equalTo(buffer)); @@ -190,7 +189,7 @@ public void testSuccessfulRoundTripInPlaceOffset() throws GeneralSecurityExcepti byte[] copyBuffer = buffer.clone(); System.arraycopy(originalPlaintext, 0, buffer, header, originalPlaintext.length); crypt.encrypt(buffer, footer, originalPlaintext.length); - assertThat(buffer, Matchers.not(equalTo(copyBuffer))); + assertThat(buffer, not(equalTo(copyBuffer))); crypt.decrypt(buffer, footer, originalPlaintext.length); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, footer, footer+originalPlaintext.length)); @@ -256,9 +255,9 @@ public void testSuccessfulRoundTripByteArrayReset() throws GeneralSecurityExcept // Once we have initialised the cipher, it is treated as a stream. // Repeated encryption of the same data will return different ciphertext, // as it is treated as a later point in the stream. - assertThat((Object) ciphertext1, Matchers.not(equalTo(ciphertext2))); - assertThat((Object) ciphertext1, Matchers.not(equalTo(ciphertext3))); - assertThat((Object) ciphertext2, Matchers.not(equalTo(ciphertext3))); + assertThat((Object) ciphertext1, not(equalTo(ciphertext2))); + assertThat((Object) ciphertext1, not(equalTo(ciphertext3))); + assertThat((Object) ciphertext2, not(equalTo(ciphertext3))); } ByteBuffer decipheredtext1 = crypt.decryptCopy(ciphertext1); @@ -331,9 +330,9 @@ public void testEncryptByteBufferToByteBuffer() throws GeneralSecurityException assertThat(buf, equalTo(cloneBuf)); // Plaintext not modified. plaintext.position(header); ciphertext.position(header); - assertThat((Object) ciphertextBuf, Matchers.not(equalTo(copyCiphertextBuf))); + assertThat((Object) ciphertextBuf, not(equalTo(copyCiphertextBuf))); Arrays.fill(buf, (byte)0); - assertThat((Object) buf, Matchers.not(equalTo(cloneBuf))); + assertThat((Object) buf, not(equalTo(cloneBuf))); crypt.decrypt(ciphertext, plaintext); assertThat(buf, equalTo(cloneBuf)); }