Files
dpi/protocol/detect_mqtt_test.go
maze 81a3829382 Refactoring
Refactored Protocol.Name -> Protocol.Type; added Encapsulation field
Refactored TLS parsing; added support for ALPN
2025-10-10 12:41:44 +02:00

87 lines
2.6 KiB
Go

package protocol
import "testing"
func TestDetectMQTT(t *testing.T) {
// A valid CONNECT packet
validSimplePacket := []byte{
0x10, 0x15, // Fixed Header: Type=CONNECT, Remaining Length=21
0x00, 0x04, 'M', 'Q', 'T', 'T', // Protocol Name: "MQTT"
0x04, // Protocol Version: 4 (v3.1.1)
0x02, // Connect Flags: Clean Session=true
0x00, 0x3C, // Keep Alive: 60 seconds
0x00, 0x07, 'm', 'a', 'z', 'e', '.', 'i', 'o', // Client ID: "maze.io"
}
// A valid packet with Will, Username, and Password
validFullPacket := []byte{
0x10, 0x3E, // Type=CONNECT, Remaining Length=62
0x00, 0x04, 'M', 'Q', 'T', 'T',
0x05, // Version
0b11001110, // Flags: User, Pass, Will(QoS=1, Retain=false), Clean
0x00, 0x1E, // Keep Alive: 30s
0x00, 0x0B, 't', 'e', 's', 't', '-', 'c', 'l', 'i', 'e', 'n', 't', // Client ID
0x00, 0x0D, 's', 't', 'a', 't', 'u', 's', '/', 'd', 'e', 'v', 'i', 'c', 'e', // Will Topic
0x00, 0x07, 'o', 'f', 'f', 'l', 'i', 'n', 'e', // Will Message
0x00, 0x04, 'u', 's', 'e', 'r', // Username
0x00, 0x09, 's', 'e', 'c', 'r', 'e', 't', '1', '2', '3', // Password
}
// Invalid packet (not a CONNECT packet)
//notConnectPacket := []byte{0x20, 0x02, 0x00, 0x00} // A CONNACK packet
// Partial packet (bad length)
partialPacket := []byte{
0x10, 0x0B, // Remaining length is 11, but we provide more data
0x00, 0x04, 'M', 'Q', 'T', 'T', // Protocol Name: "MQ"
}
// Trailing garbage packet (bad remaining length)
trailingGarbagePacket := []byte{
0x10, 0x0B, // Remaining length is 11, but we provide more data
0x00, 0x04, 'M', 'Q', 'T', 'T', // Protocol Name: "MQTT"
0x04, // Protocol Version: 4 (v3.1.1)
0b00000010, // Flags: Clean Session=true
0x00, 0x3C, // Keep Alive: 60 seconds
0x00, 0x07, 'm', 'a', 'z', 'e', '.', 'i', 'o', // Client ID: "maze.io"
0x00, 0x01, 0x02, 0x03, // garbage
}
tests := []*testCase{
{
Name: "MQTT simple packet",
Direction: Client,
Data: validSimplePacket,
DstPort: 1883,
WantType: TypeMQTT,
WantConfidence: .99,
},
{
Name: "MQTT full packet",
Direction: Client,
Data: validFullPacket,
DstPort: 1883,
WantType: TypeMQTT,
WantConfidence: .99,
},
{
Name: "MQTT partial packet",
Direction: Client,
Data: partialPacket,
DstPort: 1883,
WantType: TypeMQTT,
WantConfidence: .5,
},
{
Name: "MQTT trailing garbage packet",
Direction: Client,
Data: trailingGarbagePacket,
DstPort: 1883,
WantType: TypeMQTT,
WantConfidence: .75,
},
}
testRunner(t, tests)
}