mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-06-16 08:34:54 +02:00
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.
115 lines
3.6 KiB
Java
115 lines
3.6 KiB
Java
/**
|
|
*
|
|
* 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.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package org.jivesoftware.smack.util;
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertNull;
|
|
import static org.junit.Assert.assertTrue;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import org.junit.Test;
|
|
|
|
/**
|
|
* A test case for the StringUtils class.
|
|
*/
|
|
public class StringUtilsTest {
|
|
@Test
|
|
public void testEscapeForXml() {
|
|
String input = null;
|
|
|
|
assertNull(StringUtils.escapeForXml(null));
|
|
|
|
input = "<b>";
|
|
assertCharSequenceEquals("<b>", StringUtils.escapeForXml(input));
|
|
|
|
input = "\"";
|
|
assertCharSequenceEquals(""", StringUtils.escapeForXml(input));
|
|
|
|
input = "&";
|
|
assertCharSequenceEquals("&", StringUtils.escapeForXml(input));
|
|
|
|
input = "<b>\n\t\r</b>";
|
|
assertCharSequenceEquals("<b>\n\t\r</b>", StringUtils.escapeForXml(input));
|
|
|
|
input = " & ";
|
|
assertCharSequenceEquals(" & ", StringUtils.escapeForXml(input));
|
|
|
|
input = " \" ";
|
|
assertCharSequenceEquals(" " ", StringUtils.escapeForXml(input));
|
|
|
|
input = "> of me <";
|
|
assertCharSequenceEquals("> of me <", StringUtils.escapeForXml(input));
|
|
|
|
input = "> of me & you<";
|
|
assertCharSequenceEquals("> of me & you<", StringUtils.escapeForXml(input));
|
|
|
|
input = "& <";
|
|
assertCharSequenceEquals("& <", StringUtils.escapeForXml(input));
|
|
|
|
input = "&";
|
|
assertCharSequenceEquals("&", StringUtils.escapeForXml(input));
|
|
|
|
input = "It's a good day today";
|
|
assertCharSequenceEquals("It's a good day today", StringUtils.escapeForXml(input));
|
|
}
|
|
|
|
public static void assertCharSequenceEquals(CharSequence expected, CharSequence actual) {
|
|
assertEquals(expected.toString(), actual.toString());
|
|
}
|
|
|
|
@Test
|
|
public void testEncodeHex() throws UnsupportedEncodingException {
|
|
String input = "";
|
|
String output = "";
|
|
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StringUtils.UTF8))),
|
|
output);
|
|
|
|
input = "foo bar 123";
|
|
output = "666f6f2062617220313233";
|
|
assertEquals(new String(StringUtils.encodeHex(input.getBytes(StringUtils.UTF8))),
|
|
output);
|
|
}
|
|
|
|
@Test
|
|
public void testRandomString() {
|
|
String result;
|
|
|
|
// Test various lengths - make sure the same length is returned
|
|
result = StringUtils.randomString(4);
|
|
assertTrue(result.length() == 4);
|
|
result = StringUtils.randomString(16);
|
|
assertTrue(result.length() == 16);
|
|
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);
|
|
}
|
|
}
|