Skip to content

Commit

Permalink
Switch to Hamcrest matches to get better error reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneBab committed Sep 27, 2023
1 parent 0838a87 commit dad3143
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 36 deletions.
22 changes: 11 additions & 11 deletions test/freenet/crypt/CTRBlockCipherTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand All @@ -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);
Expand All @@ -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));

}
}
Expand Down
53 changes: 28 additions & 25 deletions test/freenet/crypt/CryptByteBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand All @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}

Expand All @@ -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));
}
}

Expand Down

0 comments on commit dad3143

Please sign in to comment.