summaryrefslogtreecommitdiff
path: root/java/common/src/test
diff options
context:
space:
mode:
authorRobert Gemmell <robbie@apache.org>2011-06-07 15:04:42 +0000
committerRobert Gemmell <robbie@apache.org>2011-06-07 15:04:42 +0000
commitb294a80f3e7613ca2391c670566145c0add76a6c (patch)
tree0c5b1883d1aad10b240550ba608e3ccf1f2edfe5 /java/common/src/test
parent53e2a20bf93aaf123b2ff920b73a6af98a1e90af (diff)
downloadqpid-python-b294a80f3e7613ca2391c670566145c0add76a6c.tar.gz
QPID-2158: add length validation into AMQShortString, remove dead code from AMQDestinations, truncate exception messages with length over 255 before sending them over the wire in AMQChannelException and AMQConnectionException.
Applied patch by Oleksandr Rudyy <orudyy@gmail.com> git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@1133037 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/common/src/test')
-rw-r--r--java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java13
-rw-r--r--java/common/src/test/java/org/apache/qpid/framing/AMQShortStringTest.java214
2 files changed, 227 insertions, 0 deletions
diff --git a/java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java b/java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java
index ef6cd41492..f65427e583 100644
--- a/java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java
+++ b/java/common/src/test/java/org/apache/qpid/AMQExceptionTest.java
@@ -23,6 +23,7 @@ package org.apache.qpid;
import junit.framework.TestCase;
import org.apache.qpid.protocol.AMQConstant;
import org.apache.qpid.framing.AMQFrameDecodingException;
+import org.apache.qpid.framing.AMQShortString;
/**
* This test is to ensure that when an AMQException is rethrown that the specified exception is correctly wrapped up.
@@ -91,6 +92,18 @@ public class AMQExceptionTest extends TestCase
return amqe;
}
+ public void testGetMessageAsString()
+ {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < 25; i++)
+ {
+ sb.append("message [" + i + "]");
+ }
+ AMQException e = new AMQException(AMQConstant.INTERNAL_ERROR, sb.toString(), null);
+ AMQShortString message = e.getMessageAsShortString();
+ assertEquals(sb.substring(0, AMQShortString.MAX_LENGTH - 3) + "...", message.toString());
+ }
+
/**
* Private class that extends AMQException but does not have a default exception.
*/
diff --git a/java/common/src/test/java/org/apache/qpid/framing/AMQShortStringTest.java b/java/common/src/test/java/org/apache/qpid/framing/AMQShortStringTest.java
index 92e7ce0a80..9a805d87b3 100644
--- a/java/common/src/test/java/org/apache/qpid/framing/AMQShortStringTest.java
+++ b/java/common/src/test/java/org/apache/qpid/framing/AMQShortStringTest.java
@@ -20,6 +20,10 @@
package org.apache.qpid.framing;
+import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.List;
+
import junit.framework.TestCase;
public class AMQShortStringTest extends TestCase
{
@@ -105,5 +109,215 @@ public class AMQShortStringTest extends TestCase
assertFalse(new AMQShortString("A").equals(new AMQShortString("a")));
}
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(byte[])}.
+ */
+ public void testCreateAMQShortStringByteArray()
+ {
+ byte[] bytes = null;
+ try
+ {
+ bytes = "test".getBytes("UTF-8");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ fail("UTF-8 encoding is not supported anymore by JVM:" + e.getMessage());
+ }
+ AMQShortString string = new AMQShortString(bytes);
+ assertEquals("constructed amq short string length differs from expected", 4, string.length());
+ assertTrue("constructed amq short string differs from expected", string.equals("test"));
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(java.lang.String)}
+ * <p>
+ * Tests short string construction from string with length less than 255.
+ */
+ public void testCreateAMQShortStringString()
+ {
+ AMQShortString string = new AMQShortString("test");
+ assertEquals("constructed amq short string length differs from expected", 4, string.length());
+ assertTrue("constructed amq short string differs from expected", string.equals("test"));
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(char[])}.
+ * <p>
+ * Tests short string construction from char array with length less than 255.
+ */
+ public void testCreateAMQShortStringCharArray()
+ {
+ char[] chars = "test".toCharArray();
+ AMQShortString string = new AMQShortString(chars);
+ assertEquals("constructed amq short string length differs from expected", 4, string.length());
+ assertTrue("constructed amq short string differs from expected", string.equals("test"));
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(java.lang.CharSequence)}
+ * <p>
+ * Tests short string construction from char sequence with length less than 255.
+ */
+ public void testCreateAMQShortStringCharSequence()
+ {
+ AMQShortString string = new AMQShortString((CharSequence) "test");
+ assertEquals("constructed amq short string length differs from expected", 4, string.length());
+ assertTrue("constructed amq short string differs from expected", string.equals("test"));
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(byte[])}.
+ * <p>
+ * Tests an attempt to create an AMQP short string from byte array with length over 255.
+ */
+ public void testCreateAMQShortStringByteArrayOver255()
+ {
+ String test = buildString('a', 256);
+ byte[] bytes = null;
+ try
+ {
+ bytes = test.getBytes("UTF-8");
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ fail("UTF-8 encoding is not supported anymore by JVM:" + e.getMessage());
+ }
+ try
+ {
+ new AMQShortString(bytes);
+ fail("It should not be possible to create AMQShortString with length over 255");
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals("Exception message differs from expected",
+ "Cannot create AMQShortString with number of octets over 255!", e.getMessage());
+ }
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(java.lang.String)}
+ * <p>
+ * Tests an attempt to create an AMQP short string from string with length over 255
+ */
+ public void testCreateAMQShortStringStringOver255()
+ {
+ String test = buildString('a', 256);
+ try
+ {
+ new AMQShortString(test);
+ fail("It should not be possible to create AMQShortString with length over 255");
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals("Exception message differs from expected",
+ "Cannot create AMQShortString with number of octets over 255!", e.getMessage());
+ }
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(char[])}.
+ * <p>
+ * Tests an attempt to create an AMQP short string from char array with length over 255.
+ */
+ public void testCreateAMQShortStringCharArrayOver255()
+ {
+ String test = buildString('a', 256);
+ char[] chars = test.toCharArray();
+ try
+ {
+ new AMQShortString(chars);
+ fail("It should not be possible to create AMQShortString with length over 255");
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals("Exception message differs from expected",
+ "Cannot create AMQShortString with number of octets over 255!", e.getMessage());
+ }
+ }
+
+ /**
+ * Test method for
+ * {@link org.apache.qpid.framing.AMQShortString#AMQShortString(java.lang.CharSequence)}
+ * <p>
+ * Tests an attempt to create an AMQP short string from char sequence with length over 255.
+ */
+ public void testCreateAMQShortStringCharSequenceOver255()
+ {
+ String test = buildString('a', 256);
+ try
+ {
+ new AMQShortString((CharSequence) test);
+ fail("It should not be possible to create AMQShortString with length over 255");
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals("Exception message differs from expected",
+ "Cannot create AMQShortString with number of octets over 255!", e.getMessage());
+ }
+ }
+
+ /**
+ * Tests joining of short strings into a short string with length over 255.
+ */
+ public void testJoinOverflow()
+ {
+ List<AMQShortString> data = new ArrayList<AMQShortString>();
+ for (int i = 0; i < 25; i++)
+ {
+ data.add(new AMQShortString("test data!"));
+ }
+ try
+ {
+ AMQShortString.join(data, new AMQShortString(" "));
+ fail("It should not be possible to create AMQShortString with length over 255");
+ }
+ catch (IllegalArgumentException e)
+ {
+ assertEquals("Exception message differs from expected",
+ "Cannot create AMQShortString with number of octets over 255!", e.getMessage());
+ }
+ }
+
+ /**
+ * Tests joining of short strings into a short string with length less than 255.
+ */
+ public void testJoin()
+ {
+ StringBuilder expected = new StringBuilder();
+ List<AMQShortString> data = new ArrayList<AMQShortString>();
+ data.add(new AMQShortString("test data 1"));
+ expected.append("test data 1");
+ data.add(new AMQShortString("test data 2"));
+ expected.append(" test data 2");
+ AMQShortString result = AMQShortString.join(data, new AMQShortString(" "));
+ assertEquals("join result differs from expected", expected.toString(), result.asString());
+ }
+
+ /**
+ * A helper method to generate a string with given length containing given
+ * character
+ *
+ * @param ch
+ * char to build string with
+ * @param length
+ * target string length
+ * @return string
+ */
+ private String buildString(char ch, int length)
+ {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < length; i++)
+ {
+ sb.append(ch);
+ }
+ return sb.toString();
+ }
}