mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
chore: refactor playSession etc to use t.Fatal (#494)
Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
@@ -49,9 +49,7 @@ func TestGreetStateValidCommands(t *testing.T) {
|
|||||||
script := []scriptStep{
|
script := []scriptStep{
|
||||||
tc,
|
tc,
|
||||||
{"QUIT", 221}}
|
{"QUIT", 221}}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,9 +75,7 @@ func TestGreetState(t *testing.T) {
|
|||||||
script := []scriptStep{
|
script := []scriptStep{
|
||||||
tc,
|
tc,
|
||||||
{"QUIT", 221}}
|
{"QUIT", 221}}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -94,18 +90,14 @@ func TestEmptyEnvelope(t *testing.T) {
|
|||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
{"MAIL FROM:<>", 501},
|
{"MAIL FROM:<>", 501},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test out some empty envelope with blanks
|
// Test out some empty envelope with blanks
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
{"MAIL FROM: <>", 501},
|
{"MAIL FROM: <>", 501},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test AUTH commands.
|
// Test AUTH commands.
|
||||||
@@ -125,9 +117,7 @@ func TestAuth(t *testing.T) {
|
|||||||
{"RSET", 250},
|
{"RSET", 250},
|
||||||
{"AUTH PLAIN aW5idWNrZXQ6cG Fzc3dvcmQK", 500},
|
{"AUTH PLAIN aW5idWNrZXQ6cG Fzc3dvcmQK", 500},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// LOGIN AUTH
|
// LOGIN AUTH
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
@@ -140,9 +130,7 @@ func TestAuth(t *testing.T) {
|
|||||||
{"", 334},
|
{"", 334},
|
||||||
{"", 235},
|
{"", 235},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test TLS commands.
|
// Test TLS commands.
|
||||||
@@ -157,9 +145,7 @@ func TestTLS(t *testing.T) {
|
|||||||
{"STARTTLS", 454}, // TLS unconfigured.
|
{"STARTTLS", 454}, // TLS unconfigured.
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test valid commands in READY state.
|
// Test valid commands in READY state.
|
||||||
@@ -191,9 +177,7 @@ func TestReadyStateValidCommands(t *testing.T) {
|
|||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
tc,
|
tc,
|
||||||
{"QUIT", 221}}
|
{"QUIT", 221}}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -217,9 +201,7 @@ func TestReadyStateRejectedDomains(t *testing.T) {
|
|||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
tc,
|
tc,
|
||||||
{"QUIT", 221}}
|
{"QUIT", 221}}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -249,9 +231,7 @@ func TestReadyStateInvalidCommands(t *testing.T) {
|
|||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
tc,
|
tc,
|
||||||
{"QUIT", 221}}
|
{"QUIT", 221}}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -276,9 +256,7 @@ func TestMailState(t *testing.T) {
|
|||||||
{"RCPT TO:<first last@host.com>", 501},
|
{"RCPT TO:<first last@host.com>", 501},
|
||||||
{"RCPT TO:<fred@fish@host.com", 501},
|
{"RCPT TO:<fred@fish@host.com", 501},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test out some good RCPT commands
|
// Test out some good RCPT commands
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
@@ -295,9 +273,7 @@ func TestMailState(t *testing.T) {
|
|||||||
{"RCPT TO:<u1@[127.0.0.1]>", 250},
|
{"RCPT TO:<u1@[127.0.0.1]>", 250},
|
||||||
{"RCPT TO:<u1@[IPv6:2001:db8:aaaa:1::100]>", 250},
|
{"RCPT TO:<u1@[IPv6:2001:db8:aaaa:1::100]>", 250},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test out recipient limit
|
// Test out recipient limit
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
@@ -310,9 +286,7 @@ func TestMailState(t *testing.T) {
|
|||||||
{"RCPT TO:<u5@gmail.com>", 250},
|
{"RCPT TO:<u5@gmail.com>", 250},
|
||||||
{"RCPT TO:<u6@gmail.com>", 552},
|
{"RCPT TO:<u6@gmail.com>", 552},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test DATA
|
// Test DATA
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
@@ -322,9 +296,7 @@ func TestMailState(t *testing.T) {
|
|||||||
{"DATA", 354},
|
{"DATA", 354},
|
||||||
{".", 250},
|
{".", 250},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test late EHLO, similar to RSET
|
// Test late EHLO, similar to RSET
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
@@ -335,9 +307,7 @@ func TestMailState(t *testing.T) {
|
|||||||
{"EHLO localhost", 250},
|
{"EHLO localhost", 250},
|
||||||
{"MAIL FROM:<john@gmail.com>", 250},
|
{"MAIL FROM:<john@gmail.com>", 250},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test RSET
|
// Test RSET
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
@@ -347,9 +317,7 @@ func TestMailState(t *testing.T) {
|
|||||||
{"RSET", 250},
|
{"RSET", 250},
|
||||||
{"MAIL FROM:<john@gmail.com>", 250},
|
{"MAIL FROM:<john@gmail.com>", 250},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test QUIT
|
// Test QUIT
|
||||||
script = []scriptStep{
|
script = []scriptStep{
|
||||||
@@ -358,9 +326,7 @@ func TestMailState(t *testing.T) {
|
|||||||
{"RCPT TO:<u1@gmail.com>", 250},
|
{"RCPT TO:<u1@gmail.com>", 250},
|
||||||
{"QUIT", 221},
|
{"QUIT", 221},
|
||||||
}
|
}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test commands in DATA state
|
// Test commands in DATA state
|
||||||
@@ -382,9 +348,7 @@ func TestDataState(t *testing.T) {
|
|||||||
{"RCPT TO:<u1@gmail.com>", 250},
|
{"RCPT TO:<u1@gmail.com>", 250},
|
||||||
{"DATA", 354},
|
{"DATA", 354},
|
||||||
}
|
}
|
||||||
if err := playScriptAgainst(t, c, script); err != nil {
|
playScriptAgainst(t, c, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send a message
|
// Send a message
|
||||||
body := `To: u1@gmail.com
|
body := `To: u1@gmail.com
|
||||||
@@ -414,9 +378,7 @@ Hi!
|
|||||||
{"RCPT TO:<u1@gmail.com>", 250},
|
{"RCPT TO:<u1@gmail.com>", 250},
|
||||||
{"DATA", 354},
|
{"DATA", 354},
|
||||||
}
|
}
|
||||||
if err := playScriptAgainst(t, c, script); err != nil {
|
playScriptAgainst(t, c, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Send a message
|
// Send a message
|
||||||
body = `X-Useless-Header: true
|
body = `X-Useless-Header: true
|
||||||
@@ -434,31 +396,30 @@ Hi!
|
|||||||
}
|
}
|
||||||
|
|
||||||
// playSession creates a new session, reads the greeting and then plays the script
|
// playSession creates a new session, reads the greeting and then plays the script
|
||||||
func playSession(t *testing.T, server *Server, script []scriptStep) error {
|
func playSession(t *testing.T, server *Server, script []scriptStep) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
pipe := setupSMTPSession(t, server)
|
pipe := setupSMTPSession(t, server)
|
||||||
c := textproto.NewConn(pipe)
|
c := textproto.NewConn(pipe)
|
||||||
|
|
||||||
if code, _, err := c.ReadCodeLine(220); err != nil {
|
if code, _, err := c.ReadCodeLine(220); err != nil {
|
||||||
return fmt.Errorf("expected a 220 greeting, got %v", code)
|
t.Errorf("expected a 220 greeting, got %v", code)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := playScriptAgainst(t, c, script)
|
playScriptAgainst(t, c, script)
|
||||||
|
|
||||||
// Not all tests leave the session in a clean state, so the following two
|
// Not all tests leave the session in a clean state, so the following two calls can fail
|
||||||
// calls can fail
|
|
||||||
_, _ = c.Cmd("QUIT")
|
_, _ = c.Cmd("QUIT")
|
||||||
_, _, _ = c.ReadCodeLine(221)
|
_, _, _ = c.ReadCodeLine(221)
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// playScriptAgainst an existing connection, does not handle server greeting
|
// playScriptAgainst an existing connection, does not handle server greeting
|
||||||
func playScriptAgainst(t *testing.T, c *textproto.Conn, script []scriptStep) error {
|
func playScriptAgainst(t *testing.T, c *textproto.Conn, script []scriptStep) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
for i, step := range script {
|
for i, step := range script {
|
||||||
id, err := c.Cmd(step.send)
|
id, err := c.Cmd(step.send)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Step %d, failed to send %q: %v", i, step.send, err)
|
t.Fatalf("Step %d, failed to send %q: %v", i, step.send, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.StartResponse(id)
|
c.StartResponse(id)
|
||||||
@@ -470,11 +431,10 @@ func playScriptAgainst(t *testing.T, c *textproto.Conn, script []scriptStep) err
|
|||||||
c.EndResponse(id)
|
c.EndResponse(id)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Return after c.EndResponse so we don't hang the connection
|
// Fail after c.EndResponse so we don't hang the connection
|
||||||
return err
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tests "MAIL FROM" emits BeforeMailAccepted event.
|
// Tests "MAIL FROM" emits BeforeMailAccepted event.
|
||||||
@@ -497,9 +457,7 @@ func TestBeforeMailAcceptedEventEmitted(t *testing.T) {
|
|||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
{"MAIL FROM:<john@gmail.com>", 250},
|
{"MAIL FROM:<john@gmail.com>", 250},
|
||||||
{"QUIT", 221}}
|
{"QUIT", 221}}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.NotNil(t, got, "BeforeMailListener did not receive Address")
|
assert.NotNil(t, got, "BeforeMailListener did not receive Address")
|
||||||
assert.Equal(t, "john", got.Local, "Address local part had wrong value")
|
assert.Equal(t, "john", got.Local, "Address local part had wrong value")
|
||||||
@@ -554,9 +512,7 @@ func TestBeforeMailAcceptedEventResponse(t *testing.T) {
|
|||||||
{"HELO localhost", 250},
|
{"HELO localhost", 250},
|
||||||
tc.script,
|
tc.script,
|
||||||
{"QUIT", 221}}
|
{"QUIT", 221}}
|
||||||
if err := playSession(t, server, script); err != nil {
|
playSession(t, server, script)
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
assert.NotNil(t, gotEvent, "BeforeMailListener did not receive Address")
|
assert.NotNil(t, gotEvent, "BeforeMailListener did not receive Address")
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user