You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cs...@apache.org on 2026/10/15 19:43:24 UTC
(activemq) branch activemq-6.3.x updated: Fix nested values in MapMessage (#2672) (#2673)
This is an automated email from the ASF dual-hosted git repository.
cshannon pushed a commit to branch activemq-6.3.x
in repository https://gitbox.apache.org/repos/asf/activemq.git
The following commit(s) were added to refs/heads/activemq-6.3.x by this push:
new bd6a13f882 Fix nested values in MapMessage (#2672) (#2673)
bd6a13f882 is described below
commit bd6a13f882755e52d0c586066e60cd64be6b95f1
Author: Christopher L. Shannon <cs...@apache.org>
AuthorDate: Thu Oct 15 15:43:18 2026 -0400
Fix nested values in MapMessage (#2672) (#2673)
* Fix nested values in MapMessage
This fixes the string encoding for MapMessages so that nested strings
are properly returned as String values again instead of UTF8Buffers
This is a fix for a regression introduced in #2080
This closes #2669
(cherry picked from commit 92d524968fbeb1fdd92fcf275f9ac51a39915fdb)
---
.../apache/activemq/util/MarshallingSupport.java | 27 +-
.../activemq/command/ActiveMQMapMessageTest.java | 181 +++++++++++++
.../activemq/util/MarshallingSupportTest.java | 286 ++++++++++++++++++++-
3 files changed, 487 insertions(+), 7 deletions(-)
diff --git a/activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java b/activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java
index 43eed3aa71..3a1af63083 100644
--- a/activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java
+++ b/activemq-client/src/main/java/org/apache/activemq/util/MarshallingSupport.java
@@ -241,9 +241,15 @@ public final class MarshallingSupport {
in.readFully((byte[])value);
break;
case STRING_TYPE:
- value = readUTF(in, maxBufferSize, in.readUnsignedShort());
+ // force flag means we want to return it as a String and not a Buffer
+ if (force) {
+ value = readShortUTF8(in, maxBufferSize);
+ } else {
+ value = readUTF(in, maxBufferSize, in.readUnsignedShort());
+ }
break;
case BIG_STRING_TYPE: {
+ // force flag means we want to return it as a String and not a Buffer
if (force) {
value = readUTF8(in, maxBufferSize);
} else {
@@ -393,15 +399,26 @@ public final class MarshallingSupport {
return offset;
}
+ // Used to read BIG_STRING_TYPE
public static String readUTF8(DataInputStream dataIn) throws IOException {
return readUTF8(dataIn, Integer.MAX_VALUE);
}
- static String readUTF8(DataInputStream dataIn, int maxBufferSize) throws IOException {
- int utflen = validateBufferSize(dataIn, maxBufferSize, dataIn.readInt());
+ // USed to read BIG_STRING_TYPE
+ public static String readUTF8(DataInputStream dataIn, int maxBufferSize) throws IOException {
+ return readUTF8(dataIn, maxBufferSize, dataIn.readInt());
+ }
+
+ // Used to read STRING_TYPE
+ public static String readShortUTF8(DataInputStream dataIn, int maxBufferSize) throws IOException {
+ return readUTF8(dataIn, maxBufferSize, dataIn.readUnsignedShort());
+ }
+
+ private static String readUTF8(DataInputStream dataIn, int maxBufferSize, int size) throws IOException {
+ int utflen = validateBufferSize(dataIn, maxBufferSize, size);
if (utflen > -1) {
- byte bytearr[] = new byte[utflen];
- char chararr[] = new char[utflen];
+ byte[] bytearr = new byte[utflen];
+ char[] chararr = new char[utflen];
dataIn.readFully(bytearr, 0, utflen);
return convertUTF8WithBuf(bytearr, chararr, 0, utflen);
} else {
diff --git a/activemq-client/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java b/activemq-client/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java
new file mode 100644
index 0000000000..29196c4c61
--- /dev/null
+++ b/activemq-client/src/test/java/org/apache/activemq/command/ActiveMQMapMessageTest.java
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.activemq.command;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+
+import jakarta.jms.MessageFormatException;
+import jakarta.jms.MessageNotWriteableException;
+import org.junit.Test;
+
+public class ActiveMQMapMessageTest {
+
+ @Test
+ public void nestedMapsAndListsPreserveValuesAndStringTypes() throws Exception {
+ Map<String, Object> nested = Map.of(
+ "text", "\u03a9\u4e2d",
+ "map", Map.of("list", List.of("inner", Map.of("leaf", "value"))),
+ "emptyMap", Map.of(),
+ "emptyList", List.of());
+ ActiveMQMapMessage message = new ActiveMQMapMessage();
+ message.setObject("nested", nested);
+ message.storeContentAndClear();
+ message.setReadOnlyBody(true);
+
+ assertEquals(nested, message.getObject("nested"));
+ assertEquals(nested, message.getContentMap().get("nested"));
+ }
+
+ @Test
+ public void nestedListPreservesNullsPrimitiveTypesAndByteArrays() throws Exception {
+ ActiveMQMapMessage message = new ActiveMQMapMessage();
+ message.setObject("nested", Arrays.asList(null, true, (byte) 2, (short) 3, 'x',
+ 4, 5L, 6.5f, 7.5d, new byte[] {8, 9},
+ Map.of("list", Arrays.asList("text", null))));
+ message.storeContentAndClear();
+ message.setReadOnlyBody(true);
+
+ List<?> received = (List<?>) message.getObject("nested");
+ assertEquals(Arrays.asList(null, true, (byte) 2, (short) 3, 'x', 4, 5L, 6.5f, 7.5d),
+ received.subList(0, 9));
+ assertArrayEquals(new byte[] {8, 9}, (byte[]) received.get(9));
+ assertEquals(Map.of("list", Arrays.asList("text", null)), received.get(10));
+ assertEquals(11, received.size());
+ }
+
+ @Test
+ public void nestedListPreservesPrimitiveBoundariesAndZeroValues() throws Exception {
+ List<Object> expected = Arrays.asList(
+ null,
+ false, true,
+ Byte.MIN_VALUE, (byte) 0, Byte.MAX_VALUE,
+ Short.MIN_VALUE, (short) 0, Short.MAX_VALUE,
+ Character.MIN_VALUE, Character.MAX_VALUE,
+ Integer.MIN_VALUE, 0, Integer.MAX_VALUE,
+ Long.MIN_VALUE, 0L, Long.MAX_VALUE,
+ Float.MIN_VALUE, 0.0f, Float.MAX_VALUE,
+ Double.MIN_VALUE, 0.0d, Double.MAX_VALUE);
+ ActiveMQMapMessage message = new ActiveMQMapMessage();
+ message.setObject("nested", List.of(expected));
+ message.storeContentAndClear();
+ message.setReadOnlyBody(true);
+
+ List<?> nested = (List<?>) message.getObject("nested");
+ assertEquals(1, nested.size());
+ assertEquals(expected, nested.get(0));
+ }
+
+ @Test
+ public void compressedNestedValuesPreserveLargeUnicodeStrings() throws Exception {
+ String largeString = "\u03a9\u4e2d\ud83d\ude03".repeat(2048);
+ Map<String, Object> nested = Map.of("list", List.of(largeString, Map.of("text", largeString)));
+ ActiveMQMapMessage message = new ActiveMQMapMessage();
+ message.setObject("nested", nested);
+ message.compress();
+ message.clearUnMarshalledState();
+ message.setReadOnlyBody(true);
+
+ assertTrue(message.isCompressed());
+ assertEquals(nested, message.getObject("nested"));
+ }
+
+ @Test
+ public void primitiveValuesRoundTripAndExposeNames() throws Exception {
+ ActiveMQMapMessage message = new ActiveMQMapMessage();
+ message.setBoolean("boolean", true);
+ message.setByte("byte", (byte) 2);
+ message.setShort("short", (short) 3);
+ message.setChar("char", 'x');
+ message.setInt("int", 4);
+ message.setLong("long", 5L);
+ message.setFloat("float", 6.5f);
+ message.setDouble("double", 7.5d);
+ message.setString("string", "value");
+ message.setBytes("bytes", new byte[] {8, 9});
+ message.setObject("nullable", null);
+
+ message.storeContentAndClear();
+ message.setReadOnlyBody(true);
+
+ assertTrue(message.getBoolean("boolean"));
+ assertEquals(2, message.getByte("byte"));
+ assertEquals(3, message.getShort("short"));
+ assertEquals('x', message.getChar("char"));
+ assertEquals(4, message.getInt("int"));
+ assertEquals(5L, message.getLong("long"));
+ assertEquals(6.5f, message.getFloat("float"), 0.0f);
+ assertEquals(7.5d, message.getDouble("double"), 0.0d);
+ assertEquals("value", message.getString("string"));
+ assertArrayEquals(new byte[] {8, 9}, message.getBytes("bytes"));
+ assertTrue(message.itemExists("nullable"));
+ assertFalse(message.itemExists("missing"));
+
+ Enumeration<String> names = message.getMapNames();
+ List<String> found = new ArrayList<>();
+ while (names.hasMoreElements()) {
+ found.add(names.nextElement());
+ }
+ assertEquals(11, found.size());
+ assertTrue(found.contains("nullable"));
+ }
+
+ @Test
+ public void conversionsAndInvalidReads() throws Exception {
+ ActiveMQMapMessage message = new ActiveMQMapMessage();
+ message.setString("number", "12");
+ message.setByte("small", (byte) 7);
+ message.setFloat("decimal", 2.5f);
+ message.setBytes("bytes", new byte[] {1});
+
+ assertEquals(12, message.getInt("number"));
+ assertEquals(7, message.getLong("small"));
+ assertEquals(2.5d, message.getDouble("decimal"), 0.0d);
+ assertThrows(MessageFormatException.class, () -> message.getInt("bytes"));
+ assertThrows(MessageFormatException.class, () -> message.getString("bytes"));
+ assertThrows(MessageFormatException.class, () -> message.getBytes("number"));
+ assertThrows(NullPointerException.class, () -> message.getChar("missing"));
+ }
+
+ @Test
+ public void bodyReadOnlyClearAndInvalidSetters() throws Exception {
+ ActiveMQMapMessage message = new ActiveMQMapMessage();
+ assertThrows(IllegalArgumentException.class, () -> message.setInt(null, 1));
+ assertThrows(IllegalArgumentException.class, () -> message.setInt("", 1));
+ assertThrows(MessageFormatException.class, () -> message.setObject("object", new Object()));
+
+ message.setInt("number", 1);
+ message.storeContentAndClear();
+ message.setReadOnlyBody(true);
+ assertThrows(MessageNotWriteableException.class, () -> message.setInt("number", 2));
+
+ message.clearBody();
+ message.setInt("number", 3);
+ assertEquals(3, message.getInt("number"));
+ assertFalse(message.itemExists("old"));
+ }
+
+}
diff --git a/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java b/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java
index d9e778c8e5..a83b35229b 100644
--- a/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java
+++ b/activemq-client/src/test/java/org/apache/activemq/util/MarshallingSupportTest.java
@@ -16,22 +16,31 @@
*/
package org.apache.activemq.util;
-import static org.apache.activemq.util.MarshallingSupport.unmarshalPrimitiveList;
import static org.apache.activemq.util.MarshallingSupport.unmarshalPrimitiveMap;
+import static org.apache.activemq.util.MarshallingSupport.unmarshalPrimitiveList;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
+import java.io.EOFException;
import java.io.IOException;
+import java.io.UTFDataFormatException;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+import java.util.zip.DeflaterOutputStream;
+import org.fusesource.hawtbuf.UTF8Buffer;
import org.junit.Test;
public class MarshallingSupportTest {
@@ -122,6 +131,279 @@ public class MarshallingSupportTest {
"Max unmarshaling depth: 1 exceeded, depth: 2");
}
+ @Test
+ public void testUnknownPrimitiveAndRemainingBufferLimit() throws Exception {
+ DataInputStream unknownType = new DataInputStream(new ByteArrayInputStream(new byte[] {99}));
+ assertException(() -> MarshallingSupport.unmarshalPrimitive(unknownType), "Unknown primitive type: 99");
+
+ DataInputStream withinRemaining = new DataInputStream(new ByteArrayInputStream(new byte[] {1, 2}));
+ MarshallingSupport.validateBufferSizeRemaining(withinRemaining, 2);
+ assertException(() -> MarshallingSupport.validateBufferSizeRemaining(withinRemaining, 3),
+ "remaining available data");
+ }
+
+ @Test
+ public void testInflaterInputStream() throws Exception {
+ ByteArrayOutputStream compressed = new ByteArrayOutputStream();
+ try (DeflaterOutputStream deflater = new DeflaterOutputStream(compressed)) {
+ deflater.write(new byte[] {1, 2, 3});
+ }
+
+ DataInputStream inflated = new DataInputStream(MarshallingSupport.createInflaterInputStream(3,
+ new ByteArrayInputStream(compressed.toByteArray())));
+ assertArrayEquals(new byte[] {1, 2, 3}, inflated.readNBytes(3));
+ }
+
+ @Test
+ public void testPrimitiveValuesRoundTrip() throws Exception {
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(bytesOut);
+ String largeString = "x".repeat(Short.MAX_VALUE / 4);
+
+ MarshallingSupport.marshalPrimitive(dataOut, null);
+ MarshallingSupport.marshalPrimitive(dataOut, true);
+ MarshallingSupport.marshalPrimitive(dataOut, (byte) -12);
+ MarshallingSupport.marshalPrimitive(dataOut, '\u03a9');
+ MarshallingSupport.marshalPrimitive(dataOut, (short) 1234);
+ MarshallingSupport.marshalPrimitive(dataOut, -123456);
+ MarshallingSupport.marshalPrimitive(dataOut, 1234567890123L);
+ MarshallingSupport.marshalPrimitive(dataOut, 1.25f);
+ MarshallingSupport.marshalPrimitive(dataOut, -2.5d);
+ MarshallingSupport.marshalPrimitive(dataOut, new byte[] {1, 2, 3});
+ MarshallingSupport.marshalPrimitive(dataOut, "short string");
+ MarshallingSupport.marshalPrimitive(dataOut, new UTF8Buffer("buffer string"));
+ MarshallingSupport.marshalPrimitive(dataOut, largeString);
+
+ DataInputStream dataIn = new DataInputStream(new ByteArrayInputStream(bytesOut.toByteArray()));
+ assertNull(MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Boolean.TRUE, MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Byte.valueOf((byte) -12), MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Character.valueOf('\u03a9'), MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Short.valueOf((short) 1234), MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Integer.valueOf(-123456), MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Long.valueOf(1234567890123L), MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Float.valueOf(1.25f), MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals(Double.valueOf(-2.5d), MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertArrayEquals(new byte[] {1, 2, 3}, (byte[]) MarshallingSupport.unmarshalPrimitive(dataIn));
+ assertEquals("short string", MarshallingSupport.unmarshalPrimitive(dataIn).toString());
+ assertEquals("buffer string", MarshallingSupport.unmarshalPrimitive(dataIn).toString());
+ assertEquals(largeString, MarshallingSupport.unmarshalPrimitive(dataIn).toString());
+ assertEquals(0, dataIn.available());
+ }
+
+ @Test
+ public void testNullPrimitiveMapAndUnsupportedPrimitive() throws Exception {
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(bytesOut);
+ MarshallingSupport.marshalPrimitiveMap(null, dataOut);
+ DataInputStream dataIn = new DataInputStream(new ByteArrayInputStream(bytesOut.toByteArray()));
+ assertNull(unmarshalPrimitiveMap(dataIn));
+
+ try {
+ MarshallingSupport.marshalPrimitive(dataOut, new Object());
+ fail("Expected unsupported primitive to be rejected");
+ } catch (IOException expected) {
+ assertTrue(expected.getMessage().contains("Object is not a primitive"));
+ }
+ }
+
+ @Test
+ public void testCountUTFBytesAtEncodingBoundaries() {
+ String[] values = {"", "\u0000", "\u0001", "\u007f", "\u0080", "\u07ff",
+ "\u0800", "\uffff", "\ud83d\ude03", "ascii\u0000\u007f\u0080\u07ff\u0800\ud83d\ude03"};
+ long[] lengths = {0, 2, 1, 1, 2, 2, 3, 3, 6, 21};
+ for (int i = 0; i < values.length; i++) {
+ assertEquals("Encoded length for case " + i, lengths[i], MarshallingSupport.countUTFBytes(values[i]));
+ }
+ }
+
+ @Test
+ public void testWriteUTFBytesToBufferMatchesModifiedUTFAtOffset() throws Exception {
+ String value = "A\u0000\u007f\u0080\u07ff\u0800\uffff\ud83d\ude03";
+ byte[] expected = modifiedUTFBytes(value);
+ byte[] buffer = new byte[expected.length + 4];
+ Arrays.fill(buffer, (byte) 0x55);
+
+ assertEquals(expected.length + 2,
+ MarshallingSupport.writeUTFBytesToBuffer(value, expected.length, buffer, 2));
+ assertArrayEquals(expected, Arrays.copyOfRange(buffer, 2, 2 + expected.length));
+ assertArrayEquals(new byte[] {0x55, 0x55}, Arrays.copyOfRange(buffer, 0, 2));
+ assertArrayEquals(new byte[] {0x55, 0x55}, Arrays.copyOfRange(buffer, buffer.length - 2, buffer.length));
+ assertEquals(2, MarshallingSupport.writeUTFBytesToBuffer("", 0, buffer, 2));
+ }
+
+ @Test
+ public void testConvertUTF8WithBufMatchesJavaDecoderAtOffset() throws Exception {
+ String value = "A\u0000\u007f\u0080\u07ff\u0800\uffff\ud83d\ude03";
+ byte[] encoded = modifiedUTFBytes(value);
+ byte[] buffer = new byte[encoded.length + 2];
+ buffer[0] = (byte) 0xff;
+ buffer[buffer.length - 1] = (byte) 0xff;
+ System.arraycopy(encoded, 0, buffer, 1, encoded.length);
+
+ assertEquals(value, MarshallingSupport.convertUTF8WithBuf(buffer, new char[value.length()], 1, encoded.length));
+ assertEquals("", MarshallingSupport.convertUTF8WithBuf(buffer, new char[0], 1, 0));
+ }
+
+ @Test
+ public void testConvertUTF8WithBufRejectsMalformedSequences() {
+ byte[][] malformed = {
+ {(byte) 0x80}, {(byte) 0xff}, {(byte) 0xc2}, {(byte) 0xc2, 0x41},
+ {(byte) 0xe1}, {(byte) 0xe1, (byte) 0x80},
+ {(byte) 0xe1, 0x41, (byte) 0x80}, {(byte) 0xe1, (byte) 0x80, 0x41},
+ {(byte) 0xf0, (byte) 0x9f, (byte) 0x98, (byte) 0x83}
+ };
+ for (byte[] bytes : malformed) {
+ assertThrows(UTFDataFormatException.class,
+ () -> MarshallingSupport.convertUTF8WithBuf(bytes, new char[bytes.length], 0, bytes.length));
+ }
+ }
+
+ @Test
+ public void testWriteUTF8UsesIntLengthAndModifiedUTFEncoding() throws Exception {
+ String value = "A\u0000\u0080\u0800\ud83d\ude03";
+ byte[] expected = modifiedUTFBytes(value);
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(bytesOut);
+ MarshallingSupport.writeUTF8(dataOut, value);
+ MarshallingSupport.writeUTF8(dataOut, "");
+ MarshallingSupport.writeUTF8(dataOut, null);
+
+ DataInputStream dataIn = input(bytesOut.toByteArray());
+ assertEquals(expected.length, dataIn.readInt());
+ byte[] actual = new byte[expected.length];
+ dataIn.readFully(actual);
+ assertArrayEquals(expected, actual);
+ assertEquals(0, dataIn.readInt());
+ assertEquals(-1, dataIn.readInt());
+ assertEquals(0, dataIn.available());
+ }
+
+ @Test
+ public void testReadUTF8ReadsNullEmptyAndLongStringsWithoutConsumingNextValue() throws Exception {
+ String longString = "\u0800".repeat(22000);
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(bytesOut);
+ dataOut.writeInt(-1);
+ dataOut.writeInt(0);
+ MarshallingSupport.writeUTF8(dataOut, longString);
+ dataOut.writeByte(42);
+
+ DataInputStream dataIn = input(bytesOut.toByteArray());
+ assertNull(MarshallingSupport.readUTF8(dataIn));
+ assertEquals("", MarshallingSupport.readUTF8(dataIn));
+ assertEquals(longString, MarshallingSupport.readUTF8(dataIn));
+ assertEquals(42, dataIn.readUnsignedByte());
+ }
+
+ @Test
+ public void testReadUTF8EnforcesEncodedByteLimitAndRejectsTruncation() throws Exception {
+ byte[] encoded = modifiedUTFBytes("\u0800");
+ assertEquals("\u0800", MarshallingSupport.readUTF8(input(lengthPrefixed(encoded, false)), 3));
+ assertThrows(MarshallingSupport.ActiveMQUnmarshalEOFException.class,
+ () -> MarshallingSupport.readUTF8(input(lengthPrefixed(encoded, false)), 2));
+ assertThrows(MarshallingSupport.ActiveMQUnmarshalEOFException.class,
+ () -> MarshallingSupport.readUTF8(input(new byte[] {0, 0, 0, 3, 65})));
+ assertThrows(EOFException.class, () -> MarshallingSupport.readUTF8(input(new byte[] {0, 0, 0})));
+ assertThrows(UTFDataFormatException.class,
+ () -> MarshallingSupport.readUTF8(input(lengthPrefixed(new byte[] {(byte) 0xc2, 65}, false))));
+ }
+
+ @Test
+ public void testReadShortUTF8UsesUnsignedLengthAndEnforcesLimits() throws Exception {
+ String value = "x".repeat(65535);
+ byte[] encoded = modifiedUTFBytes(value);
+ assertEquals(value, MarshallingSupport.readShortUTF8(input(lengthPrefixed(encoded, true)), 65535));
+ assertEquals("", MarshallingSupport.readShortUTF8(input(new byte[] {0, 0}), 0));
+ assertThrows(MarshallingSupport.ActiveMQUnmarshalEOFException.class,
+ () -> MarshallingSupport.readShortUTF8(input(lengthPrefixed(encoded, true)), 65534));
+ assertThrows(MarshallingSupport.ActiveMQUnmarshalEOFException.class,
+ () -> MarshallingSupport.readShortUTF8(input(new byte[] {0, 2, 65}), 2));
+ assertThrows(EOFException.class, () -> MarshallingSupport.readShortUTF8(input(new byte[] {0}), 10));
+ assertThrows(UTFDataFormatException.class,
+ () -> MarshallingSupport.readShortUTF8(input(lengthPrefixed(new byte[] {(byte) 0xc2}, true)), 1));
+ }
+
+ @Test
+ public void testReadUTFRetainsEncodedBytesAndRespectsLength() throws Exception {
+ byte[] encoded = modifiedUTFBytes("A\u0000\u0080\u0800");
+ byte[] withTrailer = Arrays.copyOf(encoded, encoded.length + 1);
+ withTrailer[encoded.length] = 42;
+ DataInputStream dataIn = input(withTrailer);
+ UTF8Buffer result = MarshallingSupport.readUTF(dataIn, encoded.length, encoded.length);
+ assertArrayEquals(encoded, Arrays.copyOfRange(result.data, result.offset, result.offset + result.length));
+ assertEquals(42, dataIn.readUnsignedByte());
+ assertEquals("", MarshallingSupport.readUTF(input(new byte[0]), 0, 0).toString());
+ assertThrows(MarshallingSupport.ActiveMQUnmarshalEOFException.class,
+ () -> MarshallingSupport.readUTF(input(encoded), encoded.length - 1, encoded.length));
+ assertThrows(MarshallingSupport.ActiveMQUnmarshalEOFException.class,
+ () -> MarshallingSupport.readUTF(input(encoded), encoded.length + 1, encoded.length + 1));
+ }
+
+ @Test
+ public void testMarshalStringAtShortAndBigStringBoundary() throws Exception {
+ int boundary = Short.MAX_VALUE / 4;
+ String[] values = {"", "\u0800".repeat(boundary - 1), "\u0800".repeat(boundary)};
+ for (String value : values) {
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ MarshallingSupport.marshalString(new DataOutputStream(bytesOut), value);
+ DataInputStream dataIn = input(bytesOut.toByteArray());
+ if (value.length() < boundary) {
+ assertEquals(MarshallingSupport.STRING_TYPE, dataIn.readByte());
+ assertEquals(value, dataIn.readUTF());
+ } else {
+ assertEquals(MarshallingSupport.BIG_STRING_TYPE, dataIn.readByte());
+ assertEquals(value, MarshallingSupport.readUTF8(dataIn));
+ }
+ assertEquals(0, dataIn.available());
+ }
+ }
+
+ private static DataInputStream input(byte[] bytes) {
+ return new DataInputStream(new ByteArrayInputStream(bytes));
+ }
+
+ private static byte[] modifiedUTFBytes(String value) throws IOException {
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ new DataOutputStream(bytesOut).writeUTF(value);
+ byte[] encoded = bytesOut.toByteArray();
+ return Arrays.copyOfRange(encoded, 2, encoded.length);
+ }
+
+ private static byte[] lengthPrefixed(byte[] bytes, boolean shortLength) throws IOException {
+ ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
+ DataOutputStream dataOut = new DataOutputStream(bytesOut);
+ if (shortLength) {
+ dataOut.writeShort(bytes.length);
+ } else {
+ dataOut.writeInt(bytes.length);
+ }
+ dataOut.write(bytes);
+ return bytesOut.toByteArray();
+ }
+
+ @Test
+ public void testPropertiesAndTruncateBoundaries() throws Exception {
+ assertEquals("", MarshallingSupport.propertiesToString(null));
+ assertEquals(new Properties(), MarshallingSupport.stringToProperties(null));
+ assertEquals(new Properties(), MarshallingSupport.stringToProperties(""));
+ assertEquals("x".repeat(63), MarshallingSupport.truncate64("x".repeat(63)));
+ String longText = "x".repeat(64);
+ assertEquals("x".repeat(45) + "..." + "x".repeat(12), MarshallingSupport.truncate64(longText));
+ }
+
+ @Test
+ public void testSizeLimitsAndFrameLimitedStream() throws Exception {
+ assertException(() -> MarshallingSupport.validateMaxInflatedDataSize(3, 4), "requested 4");
+ MarshallingSupport.validateMaxInflatedDataSize(3, 3);
+
+ DataInputStream limited = new DataInputStream(MarshallingSupport.createFrameLimitedInputStream(2,
+ new ByteArrayInputStream(new byte[] {1, 2, 3})));
+ assertEquals(1, limited.read());
+ assertEquals(1, limited.read(new byte[2]));
+ assertException(() -> limited.read(), "max available 0 bytes");
+ }
+
private void assertException(ThrowableRunnable<IOException> runnable, String error) throws Exception {
try {
// Trigger unmarshaling the properties. This should throw an error because
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@activemq.apache.org
For additional commands, e-mail: commits-help@activemq.apache.org
For further information, visit: https://activemq.apache.org/contact