1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-12 19:59:38 +02:00

Properly handle passphrases with leading/trailing whitespace

This commit is contained in:
Paul Schaub 2025-08-30 12:34:00 +02:00
parent bd991d0a78
commit cccd3287c1
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
11 changed files with 102 additions and 40 deletions

View file

@ -34,19 +34,20 @@ public class PassphraseTest {
@Test
public void testTrimming() {
Passphrase leadingSpace = Passphrase.fromPassword(" space");
Passphrase leadingSpace = Passphrase.fromPassword(" space").withTrimmedWhitespace();
assertArrayEquals("space".toCharArray(), leadingSpace.getChars());
assertFalse(leadingSpace.isEmpty());
Passphrase trailingSpace = Passphrase.fromPassword("space ");
Passphrase trailingSpace = Passphrase.fromPassword("space ").withTrimmedWhitespace();
assertArrayEquals("space".toCharArray(), trailingSpace.getChars());
assertFalse(trailingSpace.isEmpty());
Passphrase leadingTrailingWhitespace = new Passphrase("\t Such whitespace, much wow\n ".toCharArray());
Passphrase leadingTrailingWhitespace = new Passphrase("\t Such whitespace, much wow\n ".toCharArray())
.withTrimmedWhitespace();
assertArrayEquals("Such whitespace, much wow".toCharArray(), leadingTrailingWhitespace.getChars());
assertFalse(leadingTrailingWhitespace.isEmpty());
Passphrase fromEmptyChars = new Passphrase(" ".toCharArray());
Passphrase fromEmptyChars = new Passphrase(" ".toCharArray()).withTrimmedWhitespace();
assertNull(fromEmptyChars.getChars());
assertTrue(fromEmptyChars.isEmpty());
}
@ -54,7 +55,7 @@ public class PassphraseTest {
@ParameterizedTest
@ValueSource(strings = {"", " ", " ", "\t", "\t\t"})
public void testEmptyPassphrases(String empty) {
Passphrase passphrase = Passphrase.fromPassword(empty);
Passphrase passphrase = Passphrase.fromPassword(empty).withTrimmedWhitespace();
assertTrue(passphrase.isEmpty());
assertEquals(Passphrase.emptyPassphrase(), passphrase);