This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-codec.git
commit 09d901b6c768afd16c922a016e48732dad8d51b0 Author: Gary D. Gregory <[email protected]> AuthorDate: Fri Dec 27 10:25:31 2024 -0500 Sort members --- .../org/apache/commons/codec/digest/CryptTest.java | 86 +++++++++++----------- 1 file changed, 43 insertions(+), 43 deletions(-) diff --git a/src/test/java/org/apache/commons/codec/digest/CryptTest.java b/src/test/java/org/apache/commons/codec/digest/CryptTest.java index e8a28904..b1b452d0 100644 --- a/src/test/java/org/apache/commons/codec/digest/CryptTest.java +++ b/src/test/java/org/apache/commons/codec/digest/CryptTest.java @@ -25,11 +25,53 @@ import org.junit.jupiter.api.Test; public class CryptTest { + // Allow CLI testing + // CLASSPATH=target/classes:target/test-classes/ java org.apache.commons.codec.digest.CryptTest + public static void main(final String[] args) { + final String hash; + switch (args.length) { + case 1: + hash = Crypt.crypt(args[0]); + System.out.println(hash.length() + ": " + hash); + break; + case 2: + hash = Crypt.crypt(args[0], args[1]); + System.out.println(hash.length() + "; " + hash); + break; + default: + System.out.println("Enter key [salt (remember to quote this!)]"); + break; + } + } + + // Helper method + private void startsWith(final String string, final String prefix) { + assertTrue(string.startsWith(prefix), string + " should start with " + prefix); + } + + @Test + public void testBadSalt() { + // No salt + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$1$")); + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$5$")); + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$6$")); + // wrong char + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$1$%")); + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$5$!")); + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$6$_")); + } + + @Test + public void testBadType() { + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$2$xxxx")); + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$3$xxxx")); + assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$4$")); + } + @Test public void testCrypt() { assertNotNull(new Crypt()); // just for Test Coverage } - @Test public void testCryptWithBytes() { final byte[] keyBytes = { 'b', 'y', 't', 'e' }; @@ -48,14 +90,12 @@ public class CryptTest { public void testCryptWithEmptySalt() { assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "")); } - @Test public void testDefaultCryptVariant() { // If salt is null or completely omitted, a random "$6$" is used. assertTrue(Crypt.crypt("secret").startsWith("$6$")); assertTrue(Crypt.crypt("secret", null).startsWith("$6$")); } - @Test public void testSamples() { // From Javadoc assertEquals("$1$xxxx$aMkevjfEIpa35Bh3G4bAc.", Crypt.crypt("secret", "$1$xxxx")); @@ -67,10 +107,6 @@ public class CryptTest { assertEquals("xxWAum7tHdIUw", Crypt.crypt("secret", "xxWAum7tHdIUw")); } - // Helper method - private void startsWith(final String string, final String prefix) { - assertTrue(string.startsWith(prefix), string + " should start with " + prefix); - } @Test public void testType() { startsWith(Crypt.crypt("secret", "xxxx"), "xx"); @@ -78,40 +114,4 @@ public class CryptTest { startsWith(Crypt.crypt("secret", "$5$xxxx"), "$5$xxxx$"); startsWith(Crypt.crypt("secret", "$6$xxxx"), "$6$xxxx$"); } - @Test - public void testBadType() { - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$2$xxxx")); - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$3$xxxx")); - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$4$")); - } - @Test - public void testBadSalt() { - // No salt - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$1$")); - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$5$")); - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$6$")); - // wrong char - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$1$%")); - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$5$!")); - assertThrowsExactly(IllegalArgumentException.class, () -> Crypt.crypt("secret", "$6$_")); - } - - // Allow CLI testing - // CLASSPATH=target/classes:target/test-classes/ java org.apache.commons.codec.digest.CryptTest - public static void main(final String[] args) { - final String hash; - switch (args.length) { - case 1: - hash = Crypt.crypt(args[0]); - System.out.println(hash.length() + ": " + hash); - break; - case 2: - hash = Crypt.crypt(args[0], args[1]); - System.out.println(hash.length() + "; " + hash); - break; - default: - System.out.println("Enter key [salt (remember to quote this!)]"); - break; - } - } }
