|
| 1 | +// Copyright (c) 2007-2026 Broadcom. All Rights Reserved. The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. |
| 2 | +// |
| 3 | +// This software, the RabbitMQ Java client library, is triple-licensed under the |
| 4 | +// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2 |
| 5 | +// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see |
| 6 | +// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL, |
| 7 | +// please see LICENSE-APACHE2. |
| 8 | +// |
| 9 | +// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, |
| 10 | +// either express or implied. See the LICENSE file for specific language governing |
| 11 | +// rights and limitations of this software. |
| 12 | +// |
| 13 | +// If you have any questions regarding licensing, please contact us at |
| 14 | +// info@rabbitmq.com. |
| 15 | + |
| 16 | +package com.rabbitmq.client.test; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatCode; |
| 20 | + |
| 21 | +import com.rabbitmq.client.AMQP; |
| 22 | +import com.rabbitmq.client.impl.ValueReader; |
| 23 | +import com.rabbitmq.client.impl.ValueWriter; |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.DataInputStream; |
| 27 | +import java.io.DataOutputStream; |
| 28 | +import java.io.IOException; |
| 29 | +import java.nio.charset.StandardCharsets; |
| 30 | +import java.util.Arrays; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +public class ShortstrRoundTripTest { |
| 34 | + |
| 35 | + private static String read(byte[] payload) throws IOException { |
| 36 | + ByteArrayOutputStream frame = new ByteArrayOutputStream(); |
| 37 | + frame.write(payload.length); |
| 38 | + frame.write(payload); |
| 39 | + return new ValueReader(new DataInputStream(new ByteArrayInputStream(frame.toByteArray()))) |
| 40 | + .readShortstr(); |
| 41 | + } |
| 42 | + |
| 43 | + private static byte[] write(String s) throws IOException { |
| 44 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 45 | + new ValueWriter(new DataOutputStream(out)).writeShortstr(s); |
| 46 | + return out.toByteArray(); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void valueReadFromWireCanBeWrittenBack() throws IOException { |
| 51 | + byte[] payload = new byte[255]; |
| 52 | + Arrays.fill(payload, (byte) 0xFF); |
| 53 | + String decoded = read(payload); |
| 54 | + assertThat(decoded.getBytes(StandardCharsets.UTF_8).length).isLessThanOrEqualTo(255); |
| 55 | + assertThatCode(() -> write(decoded)).doesNotThrowAnyException(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void wellFormedValuesArePreserved() throws IOException { |
| 60 | + for (String value : new String[] {"", "hello", "santé", "你好", "😀"}) { |
| 61 | + assertThat(read(value.getBytes(StandardCharsets.UTF_8))).isEqualTo(value); |
| 62 | + } |
| 63 | + StringBuilder sb = new StringBuilder(); |
| 64 | + for (int i = 0; i < 85; i++) { |
| 65 | + sb.append("你"); |
| 66 | + } |
| 67 | + String maxLength = sb.toString(); |
| 68 | + assertThat(maxLength.getBytes(StandardCharsets.UTF_8).length).isEqualTo(255); |
| 69 | + assertThat(read(maxLength.getBytes(StandardCharsets.UTF_8))).isEqualTo(maxLength); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void partiallyMalformedValueStaysWithinLimit() throws IOException { |
| 74 | + byte[] payload = new byte[255]; |
| 75 | + Arrays.fill(payload, (byte) 'a'); |
| 76 | + for (int i = 100; i < 255; i++) { |
| 77 | + payload[i] = (byte) 0xFF; |
| 78 | + } |
| 79 | + String decoded = read(payload); |
| 80 | + assertThat(decoded.getBytes(StandardCharsets.UTF_8).length).isLessThanOrEqualTo(255); |
| 81 | + assertThatCode(() -> write(decoded)).doesNotThrowAnyException(); |
| 82 | + assertThat(decoded).startsWith("aaaa"); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void messagePropertiesReadFromWireCanBeWrittenBack() throws IOException { |
| 87 | + byte[] malformed = new byte[255]; |
| 88 | + Arrays.fill(malformed, (byte) 0xFF); |
| 89 | + |
| 90 | + ByteArrayOutputStream header = new ByteArrayOutputStream(); |
| 91 | + DataOutputStream out = new DataOutputStream(header); |
| 92 | + out.writeShort(0); |
| 93 | + out.writeLong(6); |
| 94 | + out.writeShort((1 << 10) | (1 << 9) | (1 << 7) | (1 << 5) | (1 << 4)); |
| 95 | + out.writeByte(malformed.length); |
| 96 | + out.write(malformed); |
| 97 | + out.writeByte(malformed.length); |
| 98 | + out.write(malformed); |
| 99 | + out.writeByte(malformed.length); |
| 100 | + out.write(malformed); |
| 101 | + out.writeByte(malformed.length); |
| 102 | + out.write(malformed); |
| 103 | + out.writeByte(malformed.length); |
| 104 | + out.write(malformed); |
| 105 | + out.flush(); |
| 106 | + |
| 107 | + AMQP.BasicProperties properties = |
| 108 | + new AMQP.BasicProperties( |
| 109 | + new DataInputStream(new ByteArrayInputStream(header.toByteArray()))); |
| 110 | + |
| 111 | + assertThat(properties.getCorrelationId()).isNotNull(); |
| 112 | + assertThat(properties.getReplyTo()).isNotNull(); |
| 113 | + assertThat(properties.getMessageId()).isNotNull(); |
| 114 | + assertThat(properties.getType()).isNotNull(); |
| 115 | + assertThat(properties.getUserId()).isNotNull(); |
| 116 | + |
| 117 | + AMQP.BasicProperties echoed = |
| 118 | + new AMQP.BasicProperties.Builder() |
| 119 | + .correlationId(properties.getCorrelationId()) |
| 120 | + .replyTo(properties.getReplyTo()) |
| 121 | + .messageId(properties.getMessageId()) |
| 122 | + .type(properties.getType()) |
| 123 | + .userId(properties.getUserId()) |
| 124 | + .build(); |
| 125 | + |
| 126 | + assertThatCode( |
| 127 | + () -> { |
| 128 | + ByteArrayOutputStream sink = new ByteArrayOutputStream(); |
| 129 | + echoed.writePropertiesTo( |
| 130 | + new com.rabbitmq.client.impl.ContentHeaderPropertyWriter( |
| 131 | + new DataOutputStream(sink))); |
| 132 | + }) |
| 133 | + .doesNotThrowAnyException(); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void oversizedValuesAreStillRejected() { |
| 138 | + StringBuilder sb = new StringBuilder(); |
| 139 | + for (int i = 0; i < 256; i++) { |
| 140 | + sb.append('a'); |
| 141 | + } |
| 142 | + assertThatCode(() -> write(sb.toString())).isInstanceOf(IllegalArgumentException.class); |
| 143 | + } |
| 144 | +} |
0 commit comments