Skip to content

Commit

Permalink
server: Test panic handler
Browse files Browse the repository at this point in the history
  • Loading branch information
foxcpp authored and emersion committed Jun 9, 2019
1 parent 23bb97d commit de3e249
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"io"
"io/ioutil"
"log"
"net"
"strings"
"testing"
Expand All @@ -22,7 +23,8 @@ type backend struct {
messages []*message
anonmsgs []*message

userErr error
panicOnMail bool
userErr error
}

func (be *backend) Login(_ *smtp.ConnectionState, username, password string) (smtp.Session, error) {
Expand Down Expand Up @@ -60,6 +62,9 @@ func (s *session) Logout() error {
}

func (s *session) Mail(from string) error {
if s.backend.panicOnMail {
panic("Everything is on fire!")
}
s.Reset()
s.msg.From = from
return nil
Expand Down Expand Up @@ -226,6 +231,24 @@ func TestServerEmptyFrom2(t *testing.T) {
return
}

func TestServerPanicRecover(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
defer c.Close()

s.Backend.(*backend).panicOnMail = true
// Don't log panic in tests to not confuse people who run 'go test'.
s.ErrorLog = log.New(ioutil.Discard, "", 0)

io.WriteString(c, "MAIL FROM:<[email protected]>\r\n")
scanner.Scan()
if !strings.HasPrefix(scanner.Text(), "421 ") {
t.Fatal("Invalid MAIL response:", scanner.Text())
}

return
}

func TestServerBadESMTPVar(t *testing.T) {
_, s, c, scanner := testServerAuthenticated(t)
defer s.Close()
Expand Down

0 comments on commit de3e249

Please sign in to comment.