GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Auto-port 5.0: MQTT: Allow MQTT 5 CONNECT with password only (#16835) · netty/netty@01c012d

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-mqtt/src
    • main/java/io/netty/handler/codec/mqtt
      • MqttEncoder.java
    • test/java/io/netty/handler/codec/mqtt
      • MqttCodecTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttEncoder.java‎

Copy file name to clipboardExpand all lines: codec-mqtt/src/main/java/io/netty/handler/codec/mqtt/MqttEncoder.java
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,9 @@ private static ByteBuf encodeConnectMessage(
150150
(byte) variableHeader.version());
151151
setMqttVersion(ctx, mqttVersion);
152152

153-
// as MQTT 3.1 & 3.1.1 spec, If the User Name Flag is set to 0, the Password Flag MUST be set to 0
154-
if (!variableHeader.hasUserName() && variableHeader.hasPassword()) {
153+
// MQTT 3.1 and 3.1.1 require the Password Flag to be 0 when the User Name Flag is 0.
154+
if ((mqttVersion == MqttVersion.MQTT_3_1 || mqttVersion == MqttVersion.MQTT_3_1_1) &&
155+
!variableHeader.hasUserName() && variableHeader.hasPassword()) {
155156
throw new EncoderException("Without a username, the password MUST be not set");
156157
}
157158

Collapse file

‎codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java‎

Copy file name to clipboardExpand all lines: codec-mqtt/src/test/java/io/netty/handler/codec/mqtt/MqttCodecTest.java
+38-1Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,19 @@ private void checkForSingleDecoderException(final List<Object> out) {
260260
}
261261

262262
@Test
263-
public void testConnectMessageNoPassword() throws Exception {
263+
public void testConnectMessagePasswordOnlyForMqtt31() throws Exception {
264+
final MqttConnectMessage message = createConnectMessage(
265+
MqttVersion.MQTT_3_1,
266+
null,
267+
PASSWORD,
268+
MqttProperties.NO_PROPERTIES,
269+
MqttProperties.NO_PROPERTIES);
270+
271+
assertThrows(EncoderException.class, () -> MqttEncoder.doEncode(ctx, message));
272+
}
273+
274+
@Test
275+
public void testConnectMessagePasswordOnlyForMqtt311() throws Exception {
264276
final MqttConnectMessage message = createConnectMessage(
265277
MqttVersion.MQTT_3_1_1,
266278
null,
@@ -271,6 +283,31 @@ public void testConnectMessageNoPassword() throws Exception {
271283
assertThrows(EncoderException.class, () -> MqttEncoder.doEncode(ctx, message));
272284
}
273285

286+
@Test
287+
public void testConnectMessagePasswordOnlyForMqtt5() throws Exception {
288+
final MqttConnectMessage message = createConnectMessage(
289+
MqttVersion.MQTT_5,
290+
null,
291+
PASSWORD,
292+
MqttProperties.NO_PROPERTIES,
293+
MqttProperties.NO_PROPERTIES);
294+
295+
assertFalse(message.variableHeader().hasUserName());
296+
assertTrue(message.variableHeader().hasPassword());
297+
298+
ByteBuf byteBuf = MqttEncoder.doEncode(ctx, message);
299+
300+
mqttDecoder.channelRead(ctx, byteBuf);
301+
302+
assertEquals(1, out.size());
303+
304+
final MqttConnectMessage decodedMessage = (MqttConnectMessage) out.get(0);
305+
306+
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
307+
validateConnectVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
308+
validateConnectPayload(message.payload(), decodedMessage.payload());
309+
}
310+
274311
@Test
275312
public void testConnAckMessage() throws Exception {
276313
final MqttConnAckMessage message = createConnAckMessage();

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.