diff --git a/pom.xml b/pom.xml index 60fab19..766bba9 100644 --- a/pom.xml +++ b/pom.xml @@ -45,9 +45,9 @@ - junit - junit - 4.13.1 + org.junit.jupiter + junit-jupiter + 5.10.2 test diff --git a/src/test/java/com/southernstorm/noise/tests/CipherStateTests.java b/src/test/java/com/southernstorm/noise/tests/CipherStateTests.java index f2aaafc..c1a7545 100644 --- a/src/test/java/com/southernstorm/noise/tests/CipherStateTests.java +++ b/src/test/java/com/southernstorm/noise/tests/CipherStateTests.java @@ -22,18 +22,17 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; - import java.security.NoSuchAlgorithmException; import java.util.Arrays; import javax.crypto.BadPaddingException; import javax.crypto.ShortBufferException; -import org.junit.Test; - import com.southernstorm.noise.protocol.CipherState; import com.southernstorm.noise.protocol.Noise; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; /** * Perform tests on the cipher algorithms used by Noise. @@ -43,51 +42,35 @@ public class CipherStateTests { private void testCipher(String name, int keyLen, int macLen, String key, long nonce, String ad, String plaintext, String ciphertext, - String mac, boolean forceFallbacks) - { + String mac, boolean forceFallbacks) throws NoSuchAlgorithmException, ShortBufferException, BadPaddingException { byte[] keyBytes = TestUtils.stringToData(key); byte[] adBytes = TestUtils.stringToData(ad); byte[] plaintextBytes = TestUtils.stringToData(plaintext); byte[] ciphertextBytes; - byte[] buffer; if (ciphertext.length() > 0) ciphertextBytes = TestUtils.stringToData(ciphertext + mac.substring(2)); else ciphertextBytes = TestUtils.stringToData(mac); + final byte[] plaintextBuffer = new byte[plaintextBytes.length]; + final byte[] ciphertextBuffer = new byte[ciphertextBytes.length]; + // Create the cipher object and check its properties. - CipherState cipher = null; Noise.setForceFallbacks(forceFallbacks); - try { - cipher = Noise.createCipher(name); - } catch (NoSuchAlgorithmException e) { - fail(name + " cipher is not supported"); - } + final CipherState cipher = Noise.createCipher(name); assertEquals(name, cipher.getCipherName()); assertEquals(keyLen, cipher.getKeyLength()); assertEquals(0, cipher.getMACLength()); // Key has not been set yet. // Try to encrypt. Because the key is not set yet, this will // return the plaintext as-is. - try { - buffer = new byte [plaintextBytes.length]; - Arrays.fill(buffer, (byte)0xAA); - assertEquals(plaintextBytes.length, cipher.encryptWithAd(adBytes, plaintextBytes, 0, buffer, 0, plaintextBytes.length)); - assertArrayEquals(plaintextBytes, buffer); - } catch (ShortBufferException e) { - fail("Buffer should have been big enough"); - } - + Arrays.fill(plaintextBuffer, (byte)0xAA); + assertEquals(plaintextBytes.length, cipher.encryptWithAd(adBytes, plaintextBytes, 0, plaintextBuffer, 0, plaintextBytes.length)); + assertArrayEquals(plaintextBytes, plaintextBuffer); + // Try to decrypt. Will return the ciphertext and MAC as-is. - buffer = new byte [ciphertextBytes.length]; - Arrays.fill(buffer, (byte)0xAA); - try { - assertEquals(ciphertextBytes.length, cipher.decryptWithAd(adBytes, ciphertextBytes, 0, buffer, 0, ciphertextBytes.length)); - } catch (BadPaddingException e) { - fail(); - } catch (ShortBufferException e) { - fail(); - } + Arrays.fill(ciphertextBuffer, (byte)0xAA); + assertEquals(ciphertextBytes.length, cipher.decryptWithAd(adBytes, ciphertextBytes, 0, ciphertextBuffer, 0, ciphertextBytes.length)); // Set the key and fast-forward the nonce. cipher.initializeKey(keyBytes, 0); @@ -95,39 +78,24 @@ public class CipherStateTests { assertEquals(macLen, cipher.getMACLength()); // Encrypt the data. - try { - buffer = new byte [ciphertextBytes.length]; - Arrays.fill(buffer, (byte)0xAA); - assertEquals(ciphertextBytes.length, cipher.encryptWithAd(adBytes, plaintextBytes, 0, buffer, 0, plaintextBytes.length)); - assertArrayEquals(ciphertextBytes, buffer); - } catch (ShortBufferException e) { - fail("Buffer should have been big enough"); - } + Arrays.fill(ciphertextBuffer, (byte)0xAA); + assertEquals(ciphertextBytes.length, cipher.encryptWithAd(adBytes, plaintextBytes, 0, ciphertextBuffer, 0, plaintextBytes.length)); + assertArrayEquals(ciphertextBytes, ciphertextBuffer); // Try to decrypt. The MAC check should fail because the internal // nonce was incremented and no longer matches the parameter. - try { - cipher.decryptWithAd(adBytes, ciphertextBytes, 0, buffer, 0, ciphertextBytes.length); - fail(); - } catch (BadPaddingException e) { - // Success! - } catch (ShortBufferException e) { - fail(); - } - + assertThrows(BadPaddingException.class, () -> + cipher.decryptWithAd(adBytes, ciphertextBytes, 0, ciphertextBuffer, 0, ciphertextBytes.length)); + // Fast-forward the nonce to just before the rollover. We will be able // to encrypt one more block, and then the next request will be rejected. cipher.setNonce(-2L); try { - buffer = new byte [ciphertextBytes.length]; - Arrays.fill(buffer, (byte)0xAA); - cipher.encryptWithAd(adBytes, plaintextBytes, 0, buffer, 0, plaintextBytes.length); - try { - cipher.encryptWithAd(adBytes, plaintextBytes, 0, buffer, 0, plaintextBytes.length); - fail(); - } catch (IllegalStateException e) { - // Success! - } + Arrays.fill(ciphertextBuffer, (byte)0xAA); + cipher.encryptWithAd(adBytes, plaintextBytes, 0, ciphertextBuffer, 0, plaintextBytes.length); + + assertThrows(IllegalStateException.class, () -> + cipher.encryptWithAd(adBytes, plaintextBytes, 0, ciphertextBuffer, 0, plaintextBytes.length)); } catch (ShortBufferException e) { fail("Buffer should have been big enough"); } @@ -138,57 +106,34 @@ public class CipherStateTests { assertEquals(macLen, cipher.getMACLength()); // Decrypt the test ciphertext and MAC. - try { - buffer = new byte [plaintextBytes.length]; - Arrays.fill(buffer, (byte)0xAA); - assertEquals(plaintextBytes.length, cipher.decryptWithAd(adBytes, ciphertextBytes, 0, buffer, 0, ciphertextBytes.length)); - assertArrayEquals(plaintextBytes, buffer); - } catch (BadPaddingException e) { - fail(); - } catch (ShortBufferException e) { - fail(); - } + Arrays.fill(plaintextBuffer, (byte)0xAA); + assertEquals(plaintextBytes.length, cipher.decryptWithAd(adBytes, ciphertextBytes, 0, plaintextBuffer, 0, ciphertextBytes.length)); + assertArrayEquals(plaintextBytes, plaintextBuffer); - try { - // Fast-forward the nonce to just before the rollover. We will be able - // to decrypt one more block, and then the next request will be rejected. - cipher.setNonce(-2L); + // Fast-forward the nonce to just before the rollover. We will be able + // to decrypt one more block, and then the next request will be rejected. + cipher.setNonce(-2L); - buffer = new byte [plaintextBytes.length]; - Arrays.fill(buffer, (byte)0xAA); - try { - cipher.decryptWithAd(adBytes, ciphertextBytes, 0, buffer, 0, ciphertextBytes.length); - fail(); - } catch (BadPaddingException e) { - // Success! - } + Arrays.fill(plaintextBuffer, (byte)0xAA); + assertThrows(BadPaddingException.class, () -> + cipher.decryptWithAd(adBytes, ciphertextBytes, 0, plaintextBuffer, 0, ciphertextBytes.length)); - cipher.setNonce(-1L); + cipher.setNonce(-1L); - try { - cipher.decryptWithAd(adBytes, ciphertextBytes, 0, buffer, 0, ciphertextBytes.length); - fail(); - } catch (IllegalStateException e) { - // Success! - } catch (BadPaddingException e) { - fail(); - } - } catch (ShortBufferException e) { - fail("Buffer should have been big enough"); - } + assertThrows(IllegalStateException.class, () -> + cipher.decryptWithAd(adBytes, ciphertextBytes, 0, plaintextBuffer, 0, ciphertextBytes.length)); } private void testCipher(String name, int keyLen, int macLen, String key, long nonce, String ad, String plaintext, String ciphertext, - String mac) - { + String mac) throws ShortBufferException, NoSuchAlgorithmException, BadPaddingException { testCipher(name, keyLen, macLen, key, nonce, ad, plaintext, ciphertext, mac, true); testCipher(name, keyLen, macLen, key, nonce, ad, plaintext, ciphertext, mac, false); } @Test - public void AESGCM() { + public void AESGCM() throws ShortBufferException, NoSuchAlgorithmException, BadPaddingException { /* Test vectors for AES in GCM mode from Appendix B of: http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf We can only use a few of the vectors because most of the IV's in the @@ -216,7 +161,7 @@ public class CipherStateTests { } @Test - public void ChaChaPoly() { + public void ChaChaPoly() throws ShortBufferException, NoSuchAlgorithmException, BadPaddingException { // ChaChaPoly test vectors from Appendix A.5 of RFC 7539. testCipher ("ChaChaPoly", 32, 16, diff --git a/src/test/java/com/southernstorm/noise/tests/Curve25519Tests.java b/src/test/java/com/southernstorm/noise/tests/Curve25519Tests.java index d54a887..e5abac4 100644 --- a/src/test/java/com/southernstorm/noise/tests/Curve25519Tests.java +++ b/src/test/java/com/southernstorm/noise/tests/Curve25519Tests.java @@ -22,13 +22,12 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; - import java.util.Arrays; -import org.junit.Test; - import com.southernstorm.noise.crypto.Curve25519; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class Curve25519Tests { diff --git a/src/test/java/com/southernstorm/noise/tests/Curve448Tests.java b/src/test/java/com/southernstorm/noise/tests/Curve448Tests.java index b1ec1a0..a707c16 100644 --- a/src/test/java/com/southernstorm/noise/tests/Curve448Tests.java +++ b/src/test/java/com/southernstorm/noise/tests/Curve448Tests.java @@ -22,13 +22,12 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; - import java.util.Arrays; -import org.junit.Test; - import com.southernstorm.noise.crypto.Curve448; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class Curve448Tests { diff --git a/src/test/java/com/southernstorm/noise/tests/GHASHTests.java b/src/test/java/com/southernstorm/noise/tests/GHASHTests.java index 36ad0a3..882edd9 100644 --- a/src/test/java/com/southernstorm/noise/tests/GHASHTests.java +++ b/src/test/java/com/southernstorm/noise/tests/GHASHTests.java @@ -22,13 +22,12 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; - import java.util.Arrays; -import org.junit.Test; - import com.southernstorm.noise.crypto.GHASH; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; public class GHASHTests { diff --git a/src/test/java/com/southernstorm/noise/tests/HashTests.java b/src/test/java/com/southernstorm/noise/tests/HashTests.java index 824d14b..536e2c7 100644 --- a/src/test/java/com/southernstorm/noise/tests/HashTests.java +++ b/src/test/java/com/southernstorm/noise/tests/HashTests.java @@ -22,23 +22,21 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; - import java.security.DigestException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import org.junit.Test; - import com.southernstorm.noise.protocol.Noise; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; /** * Perform tests on the hash algorithms used by Noise. */ public class HashTests { - private void testHash(MessageDigest digest, String input, String hash) - { + private void testHash(MessageDigest digest, String input, String hash) throws DigestException { byte[] inputBytes = TestUtils.stringToData(input); byte[] hashBytes = TestUtils.stringToData(hash); byte[] result = new byte [digest.getDigestLength()]; @@ -46,50 +44,36 @@ public class HashTests { // Hash the entire input in one request. digest.reset(); digest.update(inputBytes); - try { - digest.digest(result, 0, result.length); - } catch (DigestException e) { - fail("digest failed"); - } + digest.digest(result, 0, result.length); assertArrayEquals(hashBytes, result); // Hash the input in pieces to test split requests. digest.reset(); digest.update(inputBytes, 0, inputBytes.length / 2); digest.update(inputBytes, inputBytes.length / 2, inputBytes.length - (inputBytes.length / 2)); - try { - digest.digest(result, 0, result.length); - } catch (DigestException e) { - fail("digest failed"); - } + digest.digest(result, 0, result.length); assertArrayEquals(hashBytes, result); } - private void testHash(String name, String input, String hash) - { + private void testHash(String name, String input, String hash) throws NoSuchAlgorithmException, DigestException { MessageDigest digest = null; Noise.setForceFallbacks(true); try { digest = Noise.createHash(name); - } catch (NoSuchAlgorithmException e) { - fail("No crypto provider for " + name); } finally { Noise.setForceFallbacks(false); } testHash(digest, input, hash); Noise.setForceFallbacks(false); - try { - digest = Noise.createHash(name); - } catch (NoSuchAlgorithmException e) { - fail("No crypto provider for " + name); - } + digest = Noise.createHash(name); + testHash(digest, input, hash); } @Test - public void blake2b() { + public void blake2b() throws DigestException, NoSuchAlgorithmException { testHash("BLAKE2b", "", "0x786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"); testHash("BLAKE2b", "abc", "0xba80a53f981c4d0d6a2797b69f12f6e94c212f14685ac4b74b12bb6fdbffa2d17d87c5392aab792dc252d5de4533cc9518d38aa8dbf1925ab92386edd4009923"); testHash("BLAKE2b", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "0x7285ff3e8bd768d69be62b3bf18765a325917fa9744ac2f582a20850bc2b1141ed1b3e4528595acc90772bdf2d37dc8a47130b44f33a02e8730e5ad8e166e888"); @@ -97,7 +81,7 @@ public class HashTests { } @Test - public void blake2s() { + public void blake2s() throws DigestException, NoSuchAlgorithmException { testHash("BLAKE2s", "", "0x69217a3079908094e11121d042354a7c1f55b6482ca1a51e1b250dfd1ed0eef9"); testHash("BLAKE2s", "abc", "0x508c5e8c327c14e2e1a72ba34eeb452f37458b209ed63a294d999b4c86675982"); testHash("BLAKE2s", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "0x6f4df5116a6f332edab1d9e10ee87df6557beab6259d7663f3bcd5722c13f189"); @@ -105,14 +89,14 @@ public class HashTests { } @Test - public void sha256() { + public void sha256() throws DigestException, NoSuchAlgorithmException { testHash("SHA256", "", "0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"); testHash("SHA256", "abc", "0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"); testHash("SHA256", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "0x248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1"); } @Test - public void sha512() { + public void sha512() throws DigestException, NoSuchAlgorithmException { testHash("SHA512", "", "0xcf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e"); testHash("SHA512", "abc", "0xddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"); testHash("SHA512", "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "0x8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909"); diff --git a/src/test/java/com/southernstorm/noise/tests/Poly1305Tests.java b/src/test/java/com/southernstorm/noise/tests/Poly1305Tests.java index 0c82782..a318bba 100644 --- a/src/test/java/com/southernstorm/noise/tests/Poly1305Tests.java +++ b/src/test/java/com/southernstorm/noise/tests/Poly1305Tests.java @@ -22,13 +22,12 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; - import java.util.Arrays; -import org.junit.Test; - import com.southernstorm.noise.crypto.Poly1305; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; /** * Perform tests on the Poly1305 implementation in isolation from ChaChaPoly. diff --git a/src/test/java/com/southernstorm/noise/tests/RijndaelAESTests.java b/src/test/java/com/southernstorm/noise/tests/RijndaelAESTests.java index 10d1365..c8881b6 100644 --- a/src/test/java/com/southernstorm/noise/tests/RijndaelAESTests.java +++ b/src/test/java/com/southernstorm/noise/tests/RijndaelAESTests.java @@ -22,13 +22,12 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import java.util.Arrays; -import org.junit.Test; - import com.southernstorm.noise.crypto.RijndaelAES; +import org.junit.jupiter.api.Test; /** * AES test cases to verify the fallback RijndaelAES implementation. diff --git a/src/test/java/com/southernstorm/noise/tests/UnitVectorTests.java b/src/test/java/com/southernstorm/noise/tests/UnitVectorTests.java index c5642cd..3fc3566 100644 --- a/src/test/java/com/southernstorm/noise/tests/UnitVectorTests.java +++ b/src/test/java/com/southernstorm/noise/tests/UnitVectorTests.java @@ -1,8 +1,10 @@ package com.southernstorm.noise.tests; +import org.junit.jupiter.api.Test; + import java.io.InputStream; -import org.junit.Assert; -import org.junit.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; public class UnitVectorTests { @@ -11,7 +13,7 @@ public class UnitVectorTests { try (final InputStream stream = getClass().getResourceAsStream("test-vectors.json")) { VectorTests vectorTests = new VectorTests(); vectorTests.processInputStream(stream); - Assert.assertEquals(vectorTests.getFailed(), 0); + assertEquals(vectorTests.getFailed(), 0); } } } diff --git a/src/test/java/com/southernstorm/noise/tests/VectorTests.java b/src/test/java/com/southernstorm/noise/tests/VectorTests.java index 3815eb0..7bec9d5 100644 --- a/src/test/java/com/southernstorm/noise/tests/VectorTests.java +++ b/src/test/java/com/southernstorm/noise/tests/VectorTests.java @@ -22,8 +22,6 @@ package com.southernstorm.noise.tests; -import static org.junit.Assert.*; - import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -42,6 +40,8 @@ import com.southernstorm.noise.protocol.CipherState; import com.southernstorm.noise.protocol.CipherStatePair; import com.southernstorm.noise.protocol.HandshakeState; +import static org.junit.jupiter.api.Assertions.*; + /** * Executes Noise vector tests in JSON format. */ @@ -61,7 +61,7 @@ public class VectorTests { /** * Information about a handshake or transport message. */ - private class TestMessage + private static class TestMessage { public byte[] payload; public byte[] ciphertext; @@ -70,7 +70,7 @@ public class VectorTests { /** * Information about a Noise test vector that was parsed from a JSON stream. */ - private class TestVector + private static class TestVector { public String name; public String pattern; @@ -116,7 +116,7 @@ public class VectorTests { private void assertSubArrayEquals(String msg, byte[] expected, byte[] actual) { for (int index = 0; index < expected.length; ++index) - assertEquals(msg + "[" + Integer.toString(index) + "]", expected[index], actual[index]); + assertEquals(expected[index], actual[index], msg + "[" + index + "]"); } /** @@ -169,12 +169,13 @@ public class VectorTests { // Work through the messages one by one until both sides "split". int role = HandshakeState.INITIATOR; int index = 0; - HandshakeState send, recv; boolean isOneWay = (vec.pattern.length() == 1); boolean fallback = vec.fallback_expected; byte[] message = new byte [8192]; byte[] plaintext = new byte [8192]; for (; index < vec.messages.length; ++index) { + final HandshakeState send, recv; + if (initiator.getAction() == HandshakeState.SPLIT && responder.getAction() == HandshakeState.SPLIT) { break; @@ -196,15 +197,11 @@ public class VectorTests { TestMessage msg = vec.messages[index]; int len = send.writeMessage(message, 0, msg.payload, 0, msg.payload.length); assertEquals(msg.ciphertext.length, len); - assertSubArrayEquals(Integer.toString(index) + ": ciphertext", msg.ciphertext, message); + assertSubArrayEquals(index + ": ciphertext", msg.ciphertext, message); if (fallback) { // Perform a read on the responder, which will fail. - try { - recv.readMessage(message, 0, len, plaintext, 0); - fail("read should have triggered fallback"); - } catch (BadPaddingException e) { - // Success! - } + assertThrows(BadPaddingException.class, () -> recv.readMessage(message, 0, len, plaintext, 0), + "read should have triggered fallback"); // Look up the pattern to fall back to. String pattern = vec.fallback_pattern; @@ -224,7 +221,7 @@ public class VectorTests { } else { int plen = recv.readMessage(message, 0, len, plaintext, 0); assertEquals(msg.payload.length, plen); - assertSubArrayEquals(Integer.toString(index) + ": payload", msg.payload, plaintext); + assertSubArrayEquals(index + ": payload", msg.payload, plaintext); } } if (vec.fallback_expected) { @@ -276,10 +273,10 @@ public class VectorTests { } int len = csend.encryptWithAd(null, msg.payload, 0, message, 0, msg.payload.length); assertEquals(msg.ciphertext.length, len); - assertSubArrayEquals(Integer.toString(index) + ": ciphertext", msg.ciphertext, message); + assertSubArrayEquals(index + ": ciphertext", msg.ciphertext, message); int plen = crecv.decryptWithAd(null, message, 0, plaintext, 0, len); assertEquals(msg.payload.length, plen); - assertSubArrayEquals(Integer.toString(index) + ": payload", msg.payload, plaintext); + assertSubArrayEquals(index + ": payload", msg.payload, plaintext); } // Clean up. @@ -462,7 +459,7 @@ public class VectorTests { } reader.endObject(); } catch (IOException e) { - System.err.println("Exception while parsing JSON: " + e.toString()); + System.err.println("Exception while parsing JSON: " + e); e.printStackTrace(); } finally { reader.close();