Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use assertArrayEquals and add proper error messages to asserts #864

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions test/freenet/client/filter/ContentFilterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
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);
assertTrue(Arrays.equals(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;
}
assertTrue(Arrays.equals(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
59 changes: 32 additions & 27 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 @@ -95,11 +98,13 @@ public void testRoundOneByte() throws GeneralSecurityException {
byte[] buf = origPlaintext.clone();
for(int j=0;j<buf.length;j++) {
crypt2.encrypt(buf, j, 1);
assertEquals(buf[j], origCiphertext[j]);
String message = "Encrypted bytes in position " + j + " do not match.";
assertEquals(message, buf[j], origCiphertext[j]);
}
for(int j=0;j<buf.length;j++) {
crypt2.decrypt(buf, j, 1);
assertEquals(buf[j], origPlaintext[j]);
String message = "Decrypted bytes in position " + j + " do not match.";
assertEquals(message, buf[j], origPlaintext[j]);
}
}
}
Expand Down Expand Up @@ -160,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 @@ -185,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)));
Bombe marked this conversation as resolved.
Show resolved Hide resolved
crypt.decrypt(buffer, footer, originalPlaintext.length);
assertArrayEquals("CryptByteBufferType: "+type.name(),
originalPlaintext, Arrays.copyOfRange(buffer, footer, footer+originalPlaintext.length));
Expand Down Expand Up @@ -214,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 @@ -259,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) {
ArneBab marked this conversation as resolved.
Show resolved Hide resolved
assertFalse(o1.equals(o2));
assertThat(o1, Matchers.not(equalTo(o2)));
}

@Test
Expand All @@ -289,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 @@ -327,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 @@ -389,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 @@ -423,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