Bugfix in variable int encoding for MQTT

This commit is contained in:
Ziver Koc 2024-10-03 23:12:05 +02:00
parent dd6ef1b3b7
commit bb3f92fe72
4 changed files with 140 additions and 5 deletions

View file

@ -8,6 +8,7 @@ import zutil.parser.binary.BinaryStructOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static org.junit.Assert.*;
@ -159,4 +160,59 @@ public class MqttPacketPublishTest {
MqttPacket.write(binOut, obj);
assertArrayEquals(Converter.toBytes(data), buffer.toByteArray());
}
@Test
public void encodeQos2() throws IOException {
char[] data = new char[]{
// Fixed Header
0b0011_0100, // Packet Type(4) + Reserved(4)
0xFF & 6, // Variable Header + Payload Length
// Variable Header
0b0000_0000, // length
0xFF & 2, // length
'a', // Topic Name
'b', // Topic Name
0b0000_0000, // Packet ID
0b0000_0101 // Packet ID
// Payload
};
MqttPacketPublish obj = new MqttPacketPublish();
obj.setFlagQoS(MqttPacketPublish.PUBLISH_QOS_2);
obj.topicName = "ab";
obj.packetId = 5;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
BinaryStructOutputStream binOut = new BinaryStructOutputStream(buffer);
MqttPacket.write(binOut, obj);
assertArrayEquals(Converter.toBytes(data), buffer.toByteArray());
}
@Test
public void encodePayloadStream() throws IOException {
char[] data = new char[]{
// Fixed Header
0b0011_0000, // Packet Type(4) + Reserved(4)
0xFF & 8, // Variable Header + Payload Length
// Variable Header
0b0000_0000, // length
0xFF & 2, // length
'a', // Topic Name
'b', // Topic Name
// Payload
't', 'e', 's', 't',
};
MqttPacketPublish obj = new MqttPacketPublish();
obj.topicName = "ab";
obj.payload = "test".getBytes(StandardCharsets.UTF_8);
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
BinaryStructOutputStream binOut = new BinaryStructOutputStream(buffer);
MqttPacket.write(binOut, obj);
assertArrayEquals(Converter.toBytes(data), buffer.toByteArray());
}
}

View file

@ -0,0 +1,79 @@
package zutil.net.mqtt.packet;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static org.junit.Assert.*;
public class MqttVariableIntSerializerTest {
@Test
public void read() throws IOException {
MqttVariableIntSerializer serializer = new MqttVariableIntSerializer();
int actual = serializer.read(new ByteArrayInputStream(new byte[]{0x0}), null);
assertEquals(0, actual);
actual = serializer.read(new ByteArrayInputStream(new byte[]{0x7f}), null);
assertEquals(127, actual);
actual = serializer.read(new ByteArrayInputStream(new byte[]{(byte)0x80, 0x01}), null);
assertEquals(128, actual);
actual = serializer.read(new ByteArrayInputStream(new byte[]{(byte)0xFF, 0x7f}), null);
assertEquals(16_383, actual);
actual = serializer.read(new ByteArrayInputStream(new byte[]{(byte)0x80, (byte)0x80, 0x01}), null);
assertEquals(16_384, actual);
actual = serializer.read(new ByteArrayInputStream(new byte[]{(byte)0xFF, (byte)0xFF, 0x7f}), null);
assertEquals(2_097_151, actual);
actual = serializer.read(new ByteArrayInputStream(new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x01}), null);
assertEquals(2_097_152, actual);
actual = serializer.read(new ByteArrayInputStream(new byte[]{(byte)0xFF, (byte)0xFF, (byte)0xFF, 0x7f}), null);
assertEquals(268_435_455, actual);
}
@Test
public void write() throws IOException {
MqttVariableIntSerializer serializer = new MqttVariableIntSerializer();
ByteArrayOutputStream out = new ByteArrayOutputStream();
out.reset();
serializer.write(out, 0, null);
assertArrayEquals(new byte[]{0x0}, out.toByteArray());
out.reset();
serializer.write(out, 127, null);
assertArrayEquals(new byte[]{0x7F}, out.toByteArray());
out.reset();
serializer.write(out, 128, null);
assertArrayEquals(new byte[]{(byte)0x80, 0x01}, out.toByteArray());
out.reset();
serializer.write(out, 16_383, null);
assertArrayEquals(new byte[]{(byte)0xFF, 0x7f}, out.toByteArray());
out.reset();
serializer.write(out, 16_384, null);
assertArrayEquals(new byte[]{(byte)0x80, (byte)0x80, 0x01}, out.toByteArray());
out.reset();
serializer.write(out, 2_097_151, null);
assertArrayEquals(new byte[]{(byte)0xFF, (byte)0xFF, 0x7f}, out.toByteArray());
out.reset();
serializer.write(out, 2_097_152, null);
assertArrayEquals(new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, 0x01}, out.toByteArray());
out.reset();
serializer.write(out, 268_435_455, null);
assertArrayEquals(new byte[]{(byte)0xFF, (byte)0xFF, (byte)0xFF, 0x7f}, out.toByteArray());
}
}