From dad3143f6db33399508f10d19cdc1bb5a012199f Mon Sep 17 00:00:00 2001 From: Arne Babenhauserheide Date: Wed, 27 Sep 2023 08:32:01 +0200 Subject: [PATCH] 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 be13756b8b..60b7ee3ea7 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 6c328f8f6a..6593d0d703 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)); } }