From 89c0fa4b9933f225cf9deaf0713df5dc0c6a0d31 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 25 Mar 2019 12:11:59 +0100 Subject: [PATCH] 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. --- .../jivesoftware/smack/util/StringUtils.java | 4 ++-- .../smack/util/StringUtilsTest.java | 23 ++++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java index 82e75f8b7..a3d7ad096 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/StringUtils.java @@ -283,8 +283,8 @@ public class StringUtils { } public static String randomString(final int length, Random random) { - if (length < 1) { - return null; + if (length == 0) { + return ""; } byte[] randomBytes = new byte[length]; diff --git a/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java b/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java index 0cff5a730..7679a118e 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/util/StringUtilsTest.java @@ -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"); * you may not use this file except in compliance with the License. @@ -88,13 +88,7 @@ public class StringUtilsTest { @Test public void testRandomString() { - // Boundary test - String result = StringUtils.randomString(-1); - assertNull(result); - - // Zero length string test - result = StringUtils.randomString(0); - assertNull(result); + String result; // Test various lengths - make sure the same length is returned result = StringUtils.randomString(4); @@ -104,4 +98,17 @@ public class StringUtilsTest { result = StringUtils.randomString(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); + } }