mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-25 07:42:06 +01:00
Let StringUtils.(insecure)randomString() return empty string
in case length is zero. Also do throw a NegativeArraySizeException if length is negative instead of returning null. This fixes the following sporadic test issue: org.jivesoftware.smackx.ox.PainlessOpenPgpProviderTest > encryptDecryptTest FAILED java.lang.AssertionError at org.jivesoftware.smack.util.XmlStringBuilder.escape(XmlStringBuilder.java:425) at org.jivesoftware.smackx.ox.element.EncryptedOpenPgpContentElement.addCommonXml(EncryptedOpenPgpContentElement.java:65) at org.jivesoftware.smackx.ox.element.CryptElement.toXML(CryptElement.java:51) at org.jivesoftware.smackx.ox.element.CryptElement.toXML(CryptElement.java:31) at org.jivesoftware.smack.packet.Element.toXML(Element.java:41) at org.jivesoftware.smackx.ox.element.OpenPgpContentElement.toInputStream(OpenPgpContentElement.java:186) at org.jivesoftware.smackx.ox.crypto.PainlessOpenPgpProvider.encrypt(PainlessOpenPgpProvider.java:136) at org.jivesoftware.smackx.ox.PainlessOpenPgpProviderTest.encryptDecryptTest(PainlessOpenPgpProviderTest.java:155) because EncryptedOpenPgpContentElement rpad field was sometimes 'null' in case the random function returned '0' as length.
This commit is contained in:
parent
14f288a763
commit
89c0fa4b99
2 changed files with 17 additions and 10 deletions
|
@ -283,8 +283,8 @@ public class StringUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String randomString(final int length, Random random) {
|
public static String randomString(final int length, Random random) {
|
||||||
if (length < 1) {
|
if (length == 0) {
|
||||||
return null;
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] randomBytes = new byte[length];
|
byte[] randomBytes = new byte[length];
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright 2003-2007 Jive Software.
|
* Copyright 2003-2007 Jive Software, 2019 Florian Schmaus.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -88,13 +88,7 @@ public class StringUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRandomString() {
|
public void testRandomString() {
|
||||||
// Boundary test
|
String result;
|
||||||
String result = StringUtils.randomString(-1);
|
|
||||||
assertNull(result);
|
|
||||||
|
|
||||||
// Zero length string test
|
|
||||||
result = StringUtils.randomString(0);
|
|
||||||
assertNull(result);
|
|
||||||
|
|
||||||
// Test various lengths - make sure the same length is returned
|
// Test various lengths - make sure the same length is returned
|
||||||
result = StringUtils.randomString(4);
|
result = StringUtils.randomString(4);
|
||||||
|
@ -104,4 +98,17 @@ public class StringUtilsTest {
|
||||||
result = StringUtils.randomString(128);
|
result = StringUtils.randomString(128);
|
||||||
assertTrue(result.length() == 128);
|
assertTrue(result.length() == 128);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected = NegativeArraySizeException.class)
|
||||||
|
public void testNegativeArraySizeException() {
|
||||||
|
// Boundary test
|
||||||
|
StringUtils.randomString(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testZeroLengthRandomString() {
|
||||||
|
// Zero length string test
|
||||||
|
String result = StringUtils.randomString(0);
|
||||||
|
assertEquals("", result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue