Add makefile and more tests

This commit is contained in:
Ian Kent 2014-04-28 01:42:05 +01:00
parent 99a63efee5
commit 7f09380551
4 changed files with 173 additions and 9 deletions

23
Makefile Normal file
View file

@ -0,0 +1,23 @@
DEPS = $(go list -f '{{range .TestImports}}{{.}} {{end}}' ./...)
all: deps
go-bindata assets/...
go install
test: test-deps
go list ./... | xargs -n1 go test
release: release-deps
gox
deps:
go get github.com/jteeuwen/go-bindata/...
go get labix.org/v2/mgo
test-deps:
go get github.com/stretchr/testify
release-deps:
go get github.com/mitchellh/gox
.PNONY: all test deps

View file

@ -61,7 +61,9 @@ go get https://github.com/jteeuwen/go-bindata/...
go-bindata assets/...
```
Run tests using ```go test```. You'll need a copy of MailHog running for tests to pass.
If you build using ```make install```, necessary dependencies will be installed for you.
Run tests using ```make test```. You'll need a copy of MailHog running for tests to pass.
(Tests currently fail using in-memory storage, use MongoDB!)
If you make any changes, run ```go fmt``` before submitting a pull request.

View file

@ -7,6 +7,7 @@ import (
"net"
"strings"
"regexp"
"errors"
"github.com/ian-kent/MailHog/mailhog/config"
"github.com/ian-kent/MailHog/mailhog/storage"
"github.com/ian-kent/MailHog/mailhog/data"
@ -202,11 +203,14 @@ func (c *Session) Process(line string) {
}
case "MAIL":
c.log("Got MAIL command, switching to RCPT state")
r, _ := regexp.Compile("(?i:From):<([^>]+)>")
match := r.FindStringSubmatch(args)
c.message.From = match[1]
from, err := ParseMAIL(args)
if err != nil {
c.Write("550", err.Error())
return
}
c.message.From = from
c.state = RCPT
c.Write("250", "Sender " + match[1] + " ok")
c.Write("250", "Sender " + from + " ok")
default:
c.log("Got unknown command for MAIL state: '%s'", command)
c.Write("500", "Unrecognised command")
@ -215,11 +219,14 @@ func (c *Session) Process(line string) {
switch command {
case "RCPT":
c.log("Got RCPT command")
r, _ := regexp.Compile("(?i:To):<([^>]+)>")
match := r.FindStringSubmatch(args)
c.message.To = append(c.message.To, match[1])
to, err := ParseRCPT(args)
if err != nil {
c.Write("550", err.Error())
return
}
c.message.To = append(c.message.To, to)
c.state = RCPT
c.Write("250", "Recipient " + match[1] + " ok")
c.Write("250", "Recipient " + to + " ok")
case "DATA":
c.log("Got DATA command, switching to DATA state")
c.state = DATA
@ -230,3 +237,21 @@ func (c *Session) Process(line string) {
}
}
}
func ParseMAIL(mail string) (string, error) {
r := regexp.MustCompile("(?i:From):<([^>]+)>")
match := r.FindStringSubmatch(mail)
if(len(match) != 2) {
return "", errors.New("Invalid sender")
}
return match[1], nil;
}
func ParseRCPT(rcpt string) (string, error) {
r := regexp.MustCompile("(?i:To):<([^>]+)>")
match := r.FindStringSubmatch(rcpt)
if(len(match) != 2) {
return "", errors.New("Invalid recipient")
}
return match[1], nil;
}

View file

@ -0,0 +1,114 @@
package smtp
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestMAILParsing(t *testing.T) {
from, err := ParseMAIL("From:<foo@bar>")
assert.Equal(t, from, "foo@bar")
assert.Nil(t, err)
from, err = ParseMAIL("From:<foo@bar.com>")
assert.Equal(t, from, "foo@bar.com")
assert.Nil(t, err)
from, err = ParseMAIL("From:<foo>")
assert.Equal(t, from, "foo")
assert.Nil(t, err)
from, err = ParseMAIL("To:<foo@bar>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
from, err = ParseMAIL("To:<foo@bar.com>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
from, err = ParseMAIL("To:<foo>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
from, err = ParseMAIL("INVALID")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
from, err = ParseMAIL("From:INVALID")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
from, err = ParseMAIL("From:foo")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
from, err = ParseMAIL("From:foo@bar")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
from, err = ParseMAIL("From: <foo@bar>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid sender")
}
func TestRCPTParsing(t *testing.T) {
from, err := ParseRCPT("To:<foo@bar>")
assert.Equal(t, from, "foo@bar")
assert.Nil(t, err)
from, err = ParseRCPT("To:<foo@bar.com>")
assert.Equal(t, from, "foo@bar.com")
assert.Nil(t, err)
from, err = ParseRCPT("To:<foo>")
assert.Equal(t, from, "foo")
assert.Nil(t, err)
from, err = ParseRCPT("From:<foo@bar>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
from, err = ParseRCPT("From:<foo@bar.com>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
from, err = ParseRCPT("From:<foo>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
from, err = ParseRCPT("INVALID")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
from, err = ParseRCPT("To:INVALID")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
from, err = ParseRCPT("To:foo")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
from, err = ParseRCPT("To:foo@bar")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
from, err = ParseRCPT("To: <foo@bar>")
assert.Equal(t, from, "")
assert.NotNil(t, err)
assert.Equal(t, err.Error(), "Invalid recipient")
}