Skip to content

Commit

Permalink
ParseCmd tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuck1s committed Mar 20, 2020
1 parent 5ad2c8e commit 04e888c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
5 changes: 4 additions & 1 deletion parse.go
Expand Up @@ -6,7 +6,8 @@ import (
"strings"
)

func parseCmd(line string) (cmd string, arg string, err error) {
// ParseCmd parses an SMTP command line
func ParseCmd(line string) (cmd string, arg string, err error) {
line = strings.TrimRight(line, "\r\n")

l := len(line)
Expand Down Expand Up @@ -35,6 +36,7 @@ func parseCmd(line string) (cmd string, arg string, err error) {
return strings.ToUpper(line[0:4]), strings.Trim(line[5:], " \n\r"), nil
}

/* Not used by proxy
// Takes the arguments proceeding a command and files them
// into a map[string]string after uppercasing each key. Sample arg
// string:
Expand All @@ -54,6 +56,7 @@ func parseArgs(args []string) (map[string]string, error) {
}
return argMap, nil
}
*/

func parseHelloArgument(arg string) (string, error) {
domain := arg
Expand Down
39 changes: 31 additions & 8 deletions proxy_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -691,6 +692,14 @@ func TestClientOtherFunctions(t *testing.T) {
}
}

// errorMatch allows for fuzzy matching when specific errors expected
func errorMatch(e1, eChk error) bool {
if e1 == nil && eChk == nil {
return true
}
return strings.Contains(e1.Error(), eChk.Error())
}

func TestServerOtherFunctions(t *testing.T) {
verboseOpt := true
insecureSkipVerify := true
Expand All @@ -699,13 +708,27 @@ func TestServerOtherFunctions(t *testing.T) {
if err != nil {
log.Fatal(err)
}

// start the proxy
go startProxy(t, s)
time.Sleep(1 * time.Second)
// Test additional server functions not used by the app
f := func(c *smtpproxy.Conn) {
fmt.Printf("%v\n", c)
_ = s // currently unused

type actionExpectedResponse struct {
line string
cmd string
arg string
err error
}
arList := []actionExpectedResponse{
{"", "", "", nil},
{"STARTTLS", "STARTTLS", "", nil},
{"M", "", "", errors.New(`Command too short`)},
{"QUIT", "QUIT", "", nil},
{"MAIL ", "", "", errors.New(`Mangled command`)},
{"MAILxx", "", "", errors.New(`Mangled command`)},
{"MAIL FROM", "MAIL", "FROM", nil},
}
for _, v := range arList {
cmd, arg, err := smtpproxy.ParseCmd(v.line)
if cmd != v.cmd || arg != v.arg || !errorMatch(err, v.err) {
t.Errorf("Unexpected value cmd, arg, err = (%v, %v, %v) - expected (%v, %v, %v)\n", cmd, arg, err, v.cmd, v.arg, v.err)
}
}
s.ForEachConn(f)
}
13 changes: 3 additions & 10 deletions server.go
Expand Up @@ -112,7 +112,7 @@ func (s *Server) handleConn(c *Conn) error {
for {
line, err := c.ReadLine()
if err == nil {
cmd, arg, err := parseCmd(line)
cmd, arg, err := ParseCmd(line)
if err != nil {
c.nbrErrors++
c.WriteResponse(501, EnhancedCode{5, 5, 2}, "Bad command")
Expand Down Expand Up @@ -159,7 +159,7 @@ func (s *Server) ListenAndServe() error {
return s.Serve(l)
}

// Close stops the server.
// Close function not needed
func (s *Server) Close() {
s.listener.Close()

Expand All @@ -171,11 +171,4 @@ func (s *Server) Close() {
}
}

// ForEachConn iterates through all opened connections.
func (s *Server) ForEachConn(f func(*Conn)) {
s.locker.Lock()
defer s.locker.Unlock()
for conn := range s.conns {
f(conn)
}
}
// ForEachConn not needed

0 comments on commit 04e888c

Please sign in to comment.