|
18 | 18 |
|
19 | 19 | import io.netty.buffer.ByteBuf; |
20 | 20 | import io.netty.buffer.ByteBufAllocator; |
| 21 | +import io.netty.buffer.Unpooled; |
21 | 22 | import io.netty.buffer.UnpooledByteBufAllocator; |
22 | 23 | import io.netty.channel.Channel; |
23 | 24 | import io.netty.channel.ChannelHandlerContext; |
|
63 | 64 | import static org.junit.jupiter.api.Assertions.assertEquals; |
64 | 65 | import static org.junit.jupiter.api.Assertions.assertFalse; |
65 | 66 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 67 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
66 | 68 | import static org.junit.jupiter.api.Assertions.assertNull; |
67 | 69 | import static org.junit.jupiter.api.Assertions.assertThrows; |
68 | 70 | import static org.junit.jupiter.api.Assertions.assertTrue; |
@@ -1334,4 +1336,150 @@ private void validateReasonCodeAndPropertiesVariableHeader(MqttReasonCodeAndProp |
1334 | 1336 | final MqttProperties actualProps = actual.properties(); |
1335 | 1337 | validateProperties(expectedProps, actualProps); |
1336 | 1338 | } |
| 1339 | + |
| 1340 | + /** |
| 1341 | + * Builds a minimal MQTT 3.1.1 CONNECT packet whose ClientId field contains the supplied |
| 1342 | + * raw bytes (length prefix is computed automatically). Protocol = "MQTT", level = 4, |
| 1343 | + * clean-session flag set, keepalive 60. |
| 1344 | + */ |
| 1345 | + private static ByteBuf buildConnectWithClientIdBytes(byte[] clientIdBytes) { |
| 1346 | + ByteBuf buf = Unpooled.buffer(); |
| 1347 | + // variable header (10 bytes) + ClientId field (2 + N bytes); test packets stay small, |
| 1348 | + // so a single-byte Remaining Length is sufficient. |
| 1349 | + int remainingLength = 10 + 2 + clientIdBytes.length; |
| 1350 | + buf.writeByte(0x10); // CONNECT |
| 1351 | + buf.writeByte(remainingLength); |
| 1352 | + // Variable header |
| 1353 | + buf.writeShort(4); |
| 1354 | + buf.writeBytes(new byte[] {'M', 'Q', 'T', 'T'}); |
| 1355 | + buf.writeByte(0x04); // protocol level (MQTT 3.1.1) |
| 1356 | + buf.writeByte(0x02); // clean session |
| 1357 | + buf.writeShort(60); // keep alive |
| 1358 | + // Payload: ClientId |
| 1359 | + buf.writeShort(clientIdBytes.length); |
| 1360 | + buf.writeBytes(clientIdBytes); |
| 1361 | + return buf; |
| 1362 | + } |
| 1363 | + |
| 1364 | + private static MqttMessage decodeUtf8TestPacket(MqttDecoder decoder, ByteBuf in) { |
| 1365 | + EmbeddedChannel channel = new EmbeddedChannel(decoder); |
| 1366 | + try { |
| 1367 | + channel.writeInbound(in); |
| 1368 | + return channel.readInbound(); |
| 1369 | + } finally { |
| 1370 | + channel.finishAndReleaseAll(); |
| 1371 | + } |
| 1372 | + } |
| 1373 | + |
| 1374 | + private static void assertMalformedUtf8(MqttMessage msg) { |
| 1375 | + assertNotNull(msg); |
| 1376 | + assertTrue(msg.decoderResult().isFailure(), "expected decoder failure but got: " + msg); |
| 1377 | + assertInstanceOf(DecoderException.class, msg.decoderResult().cause()); |
| 1378 | + } |
| 1379 | + |
| 1380 | + @Test |
| 1381 | + public void invalidTwoByteUtf8SequenceIsRejectedByDefault() { |
| 1382 | + // 0xC3 must be followed by a 10xxxxxx continuation byte; 0x28 is not. |
| 1383 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[] {(byte) 0xC3, 0x28}); |
| 1384 | + assertMalformedUtf8(decodeUtf8TestPacket(new MqttDecoder(), packet)); |
| 1385 | + } |
| 1386 | + |
| 1387 | + @Test |
| 1388 | + public void truncatedMultibyteUtf8SequenceIsRejected() { |
| 1389 | + // Lone 0xC3 with no continuation byte at all (string ends mid-sequence). |
| 1390 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[] {(byte) 0xC3}); |
| 1391 | + assertMalformedUtf8(decodeUtf8TestPacket(new MqttDecoder(), packet)); |
| 1392 | + } |
| 1393 | + |
| 1394 | + @Test |
| 1395 | + public void overlongNullUtf8EncodingIsRejected() { |
| 1396 | + // 0xC0 0x80 is the (invalid) Modified-UTF-8 overlong encoding of U+0000. |
| 1397 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[] {(byte) 0xC0, (byte) 0x80}); |
| 1398 | + assertMalformedUtf8(decodeUtf8TestPacket(new MqttDecoder(), packet)); |
| 1399 | + } |
| 1400 | + |
| 1401 | + @Test |
| 1402 | + public void isolatedHighSurrogateUtf8IsRejected() { |
| 1403 | + // 0xED 0xA0 0x80 = U+D800, an unpaired UTF-16 high surrogate; not valid UTF-8. |
| 1404 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[] {(byte) 0xED, (byte) 0xA0, (byte) 0x80}); |
| 1405 | + assertMalformedUtf8(decodeUtf8TestPacket(new MqttDecoder(), packet)); |
| 1406 | + } |
| 1407 | + |
| 1408 | + @Test |
| 1409 | + public void fiveByteOverlongUtf8SequenceIsRejected() { |
| 1410 | + // 0xF8 starts a 5-byte sequence which is not allowed by RFC 3629. |
| 1411 | + ByteBuf packet = buildConnectWithClientIdBytes( |
| 1412 | + new byte[] {(byte) 0xF8, (byte) 0x88, (byte) 0x80, (byte) 0x80, (byte) 0x80}); |
| 1413 | + assertMalformedUtf8(decodeUtf8TestPacket(new MqttDecoder(), packet)); |
| 1414 | + } |
| 1415 | + |
| 1416 | + @Test |
| 1417 | + public void embeddedNullCharacterInUtf8StringIsRejected() { |
| 1418 | + // U+0000 must cause Malformed Packet. |
| 1419 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[] {'a', 0x00, 'b'}); |
| 1420 | + assertMalformedUtf8(decodeUtf8TestPacket(new MqttDecoder(), packet)); |
| 1421 | + } |
| 1422 | + |
| 1423 | + @Test |
| 1424 | + public void wellFormedMultibyteUtf8IsAccepted() { |
| 1425 | + byte[] cid = {'h', 'e', 'l', 'l', 'o'}; |
| 1426 | + ByteBuf packet = buildConnectWithClientIdBytes(cid); |
| 1427 | + MqttMessage msg = decodeUtf8TestPacket(new MqttDecoder(), packet); |
| 1428 | + assertNotNull(msg); |
| 1429 | + try { |
| 1430 | + assertTrue(msg.decoderResult().isSuccess(), |
| 1431 | + "expected success but got: " + msg.decoderResult().cause()); |
| 1432 | + assertInstanceOf(MqttConnectMessage.class, msg); |
| 1433 | + assertEquals("hello", ((MqttConnectMessage) msg).payload().clientIdentifier()); |
| 1434 | + } finally { |
| 1435 | + ReferenceCountUtil.release(msg); |
| 1436 | + } |
| 1437 | + } |
| 1438 | + |
| 1439 | + @Test |
| 1440 | + public void emptyClientIdIsAcceptedUnderStrictUtf8() { |
| 1441 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[0]); |
| 1442 | + MqttMessage msg = decodeUtf8TestPacket(new MqttDecoder(), packet); |
| 1443 | + assertNotNull(msg); |
| 1444 | + try { |
| 1445 | + assertTrue(msg.decoderResult().isSuccess()); |
| 1446 | + } finally { |
| 1447 | + ReferenceCountUtil.release(msg); |
| 1448 | + } |
| 1449 | + } |
| 1450 | + |
| 1451 | + @Test |
| 1452 | + public void legacyModeAcceptsMalformedUtf8AsReplacementChar() { |
| 1453 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[] {(byte) 0xC3, 0x28}); |
| 1454 | + MqttDecoder lenient = new MqttDecoder(8 * 1024 * 1024, 23, false); |
| 1455 | + MqttMessage msg = decodeUtf8TestPacket(lenient, packet); |
| 1456 | + assertNotNull(msg); |
| 1457 | + try { |
| 1458 | + assertTrue(msg.decoderResult().isSuccess(), |
| 1459 | + "lenient mode should accept malformed UTF-8"); |
| 1460 | + assertInstanceOf(MqttConnectMessage.class, msg); |
| 1461 | + String cid = ((MqttConnectMessage) msg).payload().clientIdentifier(); |
| 1462 | + assertNotNull(cid); |
| 1463 | + // java.lang.String inserts U+FFFD for malformed input. Exact length is JDK |
| 1464 | + // implementation defined; just make sure decoding did not throw. |
| 1465 | + assertTrue(cid.indexOf('\uFFFD') >= 0 || cid.length() > 0, |
| 1466 | + "expected replacement char or non-empty result"); |
| 1467 | + } finally { |
| 1468 | + ReferenceCountUtil.release(msg); |
| 1469 | + } |
| 1470 | + } |
| 1471 | + |
| 1472 | + @Test |
| 1473 | + public void legacyModeAcceptsEmbeddedNullCharacter() { |
| 1474 | + ByteBuf packet = buildConnectWithClientIdBytes(new byte[] {'a', 0x00, 'b'}); |
| 1475 | + MqttDecoder lenient = new MqttDecoder(8 * 1024 * 1024, 23, false); |
| 1476 | + MqttMessage msg = decodeUtf8TestPacket(lenient, packet); |
| 1477 | + assertNotNull(msg); |
| 1478 | + try { |
| 1479 | + assertTrue(msg.decoderResult().isSuccess()); |
| 1480 | + assertEquals("a\u0000b", ((MqttConnectMessage) msg).payload().clientIdentifier()); |
| 1481 | + } finally { |
| 1482 | + ReferenceCountUtil.release(msg); |
| 1483 | + } |
| 1484 | + } |
1337 | 1485 | } |