smtpd/protocol_test.go
Christian Joergensen 03861efdd8 Improve the command parser to try to parse malformed commands.
Timon Relitzki reported that some systems, for example the Synology
DSM Rackstations, will send an extra space character with their SMTP
commands.

MAIL FROM:<christian@technobabble.dk>

Becomes:

MAIL FROM: <christian@technobabble.dk>

This confused the command parser. In the spirit of the robustness
principle if have choosen to improve the command parse to also handle
these extra spaces.

The command parser has also been extended with some extra unit tests,
to demonstrate the problem.
2017-05-23 21:42:13 +02:00

83 lines
1.8 KiB
Go

package smtpd
import "testing"
func TestParseLine(t *testing.T) {
cmd := parseLine("HELO hostname")
if cmd.action != "HELO" {
t.Fatalf("unexpected action: %s", cmd.action)
}
if len(cmd.fields) != 2 {
t.Fatalf("unexpected fields length: %d", len(cmd.fields))
}
if len(cmd.params) != 1 {
t.Fatalf("unexpected params length: %d", len(cmd.params))
}
if cmd.params[0] != "hostname" {
t.Fatalf("unexpected value for param 0: %v", cmd.params[0])
}
cmd = parseLine("DATA")
if cmd.action != "DATA" {
t.Fatalf("unexpected action: %s", cmd.action)
}
if len(cmd.fields) != 1 {
t.Fatalf("unexpected fields length: %d", len(cmd.fields))
}
if cmd.params != nil {
t.Fatalf("unexpected params: %v", cmd.params)
}
cmd = parseLine("MAIL FROM:<christian@technobabble.dk>")
if cmd.action != "MAIL" {
t.Fatalf("unexpected action: %s", cmd.action)
}
if len(cmd.fields) != 2 {
t.Fatalf("unexpected fields length: %d", len(cmd.fields))
}
if len(cmd.params) != 2 {
t.Fatalf("unexpected params length: %d", len(cmd.params))
}
if cmd.params[0] != "FROM" {
t.Fatalf("unexpected value for param 0: %v", cmd.params[0])
}
if cmd.params[1] != "<christian@technobabble.dk>" {
t.Fatalf("unexpected value for param 1: %v", cmd.params[1])
}
}
func TestParseLineMailformedMAILFROM(t *testing.T) {
cmd := parseLine("MAIL FROM: <christian@technobabble.dk>")
if cmd.action != "MAIL" {
t.Fatalf("unexpected action: %s", cmd.action)
}
if len(cmd.fields) != 2 {
t.Fatalf("unexpected fields length: %d", len(cmd.fields))
}
if len(cmd.params) != 2 {
t.Fatalf("unexpected params length: %d", len(cmd.params))
}
if cmd.params[0] != "FROM" {
t.Fatalf("unexpected value for param 0: %v", cmd.params[0])
}
if cmd.params[1] != "<christian@technobabble.dk>" {
t.Fatalf("unexpected value for param 1: %v", cmd.params[1])
}
}