Skip to content

Commit 1f6d2e8

Browse files
committed
[broker-25] add tests for connect out packet
1 parent 0d0e0a0 commit 1f6d2e8

File tree

5 files changed

+127
-5
lines changed

5 files changed

+127
-5
lines changed

src/main/java/com/ss/mqtt/broker/network/packet/out/Connect311OutPacket.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.ss.mqtt.broker.network.packet.out;
22

33
import com.ss.mqtt.broker.model.MqttVersion;
4+
import com.ss.mqtt.broker.model.QoS;
45
import com.ss.mqtt.broker.network.packet.PacketType;
56
import com.ss.rlib.common.util.ArrayUtils;
67
import com.ss.rlib.common.util.StringUtils;
@@ -24,7 +25,8 @@ public class Connect311OutPacket extends MqttWritablePacket {
2425
private final @NotNull byte[] password;
2526
private final @NotNull byte[] willPayload;
2627

27-
private final int willQos;
28+
private final @NotNull QoS willQos;
29+
2830
private final int keepAlive;
2931

3032
private final boolean willRetain;
@@ -58,7 +60,9 @@ protected void writePayload(@NotNull ByteBuffer buffer) {
5860
writeString(buffer, clientId);
5961

6062
// for MQTT 5
61-
appendWillProperties(buffer);
63+
if (StringUtils.isNotEmpty(willTopic)) {
64+
appendWillProperties(buffer);
65+
}
6266

6367
if (StringUtils.isNotEmpty(willTopic)) {
6468
writeString(buffer, willTopic);
@@ -88,7 +92,7 @@ private int buildConnectFlags() {
8892

8993
if (StringUtils.isNotEmpty(willTopic)) {
9094
connectFlags |= 0b0000_0100;
91-
connectFlags |= (willQos << 3);
95+
connectFlags |= (willQos.ordinal() << 3);
9296
if (willRetain) {
9397
connectFlags |= 0b0010_0000;
9498
}

src/main/java/com/ss/mqtt/broker/network/packet/out/Connect5OutPacket.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.ss.mqtt.broker.model.MqttPropertyConstants;
44
import com.ss.mqtt.broker.model.MqttVersion;
55
import com.ss.mqtt.broker.model.PacketProperty;
6+
import com.ss.mqtt.broker.model.QoS;
67
import com.ss.mqtt.broker.util.MqttDataUtils;
78
import org.jetbrains.annotations.NotNull;
89

@@ -193,7 +194,7 @@ public Connect5OutPacket(
193194
@NotNull String clientId,
194195
@NotNull byte[] password,
195196
@NotNull byte[] willPayload,
196-
int willQos,
197+
@NotNull QoS willQos,
197198
int keepAlive,
198199
boolean willRetain,
199200
boolean cleanStart,
@@ -221,6 +222,11 @@ public Connect5OutPacket(
221222
return MqttVersion.MQTT_5;
222223
}
223224

225+
@Override
226+
protected boolean isPropertiesSupported() {
227+
return true;
228+
}
229+
224230
@Override
225231
protected void appendWillProperties(@NotNull ByteBuffer buffer) {
226232

@@ -278,7 +284,7 @@ protected void writeProperties(@NotNull ByteBuffer buffer) {
278284
buffer,
279285
PacketProperty.MAXIMUM_PACKET_SIZE,
280286
maximumPacketSize,
281-
MqttPropertyConstants.MAXIMUM_PACKET_SIZE_DEFAULT
287+
MqttPropertyConstants.MAXIMUM_PACKET_SIZE_UNDEFINED
282288
);
283289
}
284290

src/test/groovy/com/ss/mqtt/broker/test/network/NetworkUnitSpecification.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class NetworkUnitSpecification extends UnitSpecification {
2929

3030
public static final maxQos = QoS.AT_MOST_ONCE_DELIVERY
3131
public static final sessionPresent = true
32+
public static final cleanStart = false
33+
public static final willRetain = false
3234
public static final clientId = "testClientId"
3335
public static final packetId = 1234 as short
3436
public static final userName = "testUser"
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.ss.mqtt.broker.test.network.out
2+
3+
import com.ss.mqtt.broker.model.QoS
4+
import com.ss.mqtt.broker.network.packet.in.ConnectInPacket
5+
import com.ss.mqtt.broker.network.packet.out.Connect311OutPacket
6+
import com.ss.rlib.common.util.ArrayUtils
7+
import com.ss.rlib.common.util.BufferUtils
8+
import com.ss.rlib.common.util.array.Array
9+
10+
class Connect311OutPacketTest extends BaseOutPacketTest {
11+
12+
def "should write packet correctly"() {
13+
14+
given:
15+
16+
def packet = new Connect311OutPacket(
17+
userName,
18+
"",
19+
clientId,
20+
userPassword,
21+
ArrayUtils.EMPTY_BYTE_ARRAY,
22+
QoS.AT_MOST_ONCE_DELIVERY,
23+
keepAlive,
24+
willRetain,
25+
cleanStart,
26+
)
27+
28+
when:
29+
30+
def dataBuffer = BufferUtils.prepareBuffer(512) {
31+
packet.write(it)
32+
}
33+
34+
def reader = new ConnectInPacket(0b0001_0000 as byte)
35+
def result = reader.read(mqtt311Connection, dataBuffer, dataBuffer.limit())
36+
37+
then:
38+
result
39+
reader.username == userName
40+
reader.clientId == clientId
41+
reader.password == userPassword
42+
reader.keepAlive == keepAlive
43+
reader.userProperties == Array.empty()
44+
reader.cleanStart == cleanStart
45+
reader.willRetain == willRetain
46+
}
47+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.ss.mqtt.broker.test.network.out
2+
3+
import com.ss.mqtt.broker.model.QoS
4+
import com.ss.mqtt.broker.network.packet.in.ConnectInPacket
5+
import com.ss.mqtt.broker.network.packet.out.Connect5OutPacket
6+
import com.ss.rlib.common.util.ArrayUtils
7+
import com.ss.rlib.common.util.BufferUtils
8+
import com.ss.rlib.common.util.array.Array
9+
10+
class Connect5OutPacketTest extends BaseOutPacketTest {
11+
12+
def "should write packet correctly"() {
13+
14+
given:
15+
16+
def packet = new Connect5OutPacket(
17+
userName,
18+
"",
19+
clientId,
20+
userPassword,
21+
ArrayUtils.EMPTY_BYTE_ARRAY,
22+
QoS.AT_MOST_ONCE_DELIVERY,
23+
keepAlive,
24+
willRetain,
25+
cleanStart,
26+
authMethod,
27+
authData,
28+
sessionExpiryInterval,
29+
receiveMaximum,
30+
maximumPacketSize,
31+
topicAliasMaximum,
32+
requestResponseInformation,
33+
requestProblemInformation
34+
)
35+
36+
when:
37+
38+
def dataBuffer = BufferUtils.prepareBuffer(512) {
39+
packet.write(it)
40+
}
41+
42+
def reader = new ConnectInPacket(0b0001_0000 as byte)
43+
def result = reader.read(mqtt5Connection, dataBuffer, dataBuffer.limit())
44+
45+
then:
46+
result
47+
reader.username == userName
48+
reader.clientId == clientId
49+
reader.password == userPassword
50+
reader.keepAlive == keepAlive
51+
reader.userProperties == Array.empty()
52+
reader.cleanStart == cleanStart
53+
reader.willRetain == willRetain
54+
reader.authenticationMethod == authMethod
55+
reader.authenticationData == authData
56+
reader.sessionExpiryInterval == sessionExpiryInterval
57+
reader.receiveMax == receiveMaximum
58+
reader.maximumPacketSize == maximumPacketSize
59+
reader.topicAliasMaximum == topicAliasMaximum
60+
reader.requestResponseInformation == requestResponseInformation
61+
reader.requestProblemInformation == requestProblemInformation
62+
}
63+
}

0 commit comments

Comments
 (0)