Skip to content

Commit

Permalink
More tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tuck1s committed Mar 20, 2020
1 parent 5e8bc9a commit a1f7615
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
13 changes: 10 additions & 3 deletions cmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ func main() {
}

// Logging of downstream (client to proxy server) commands and responses
dbgFile, err := smtpproxy.DownstreamDebug(*downstreamDebug)
if err != nil {
log.Fatal(err)
var dbgFile *os.File
if *downstreamDebug != "" {
dbgFile, err = os.OpenFile(*downstreamDebug, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
} else {
defer dbgFile.Close()
log.Println("Proxy logging SMTP commands, responses and downstream DATA to", dbgFile.Name())
}
}

s, _, err := smtpproxy.CreateProxy(*inHostPort, *outHostPort, *verboseOpt, cert, privkey, *insecureSkipVerify, dbgFile)
if err != nil {
log.Fatal(err)
Expand Down
14 changes: 0 additions & 14 deletions proxy_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,6 @@ func CreateProxy(inHostPort, outHostPort string, verboseOpt bool, cert, privkey
return s, be, err
}

// DownstreamDebug creates a debug file, if non-empty filename passed in
func DownstreamDebug(downstreamDebug string) (*os.File, error) {
var dbgFile *os.File
var err error
if downstreamDebug != "" {
dbgFile, err = os.OpenFile(downstreamDebug, os.O_CREATE|os.O_WRONLY, 0644)
if err == nil {
defer dbgFile.Close()
log.Println("Proxy logging SMTP commands, responses and downstream DATA to", dbgFile.Name())
}
}
return dbgFile, err
}

//-----------------------------------------------------------------------------
// Backend handlers

Expand Down
51 changes: 26 additions & 25 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,20 +250,28 @@ func tlsClientConfig(cert []byte, privkey []byte) (*tls.Config, error) {

const inHostPort = "localhost:5580" // need to specifically have keyword localhost in here for c.Auth to accept nonsecure connections
const outHostPort = ":5581"
const downstreamDebug = "debug_proxy_test2.log"
const downstreamDebug = "debug_proxy_test.log"
const inHostPort2 = "localhost:5582" // need to specifically have keyword localhost in here for c.Auth to accept nonsecure connections

func TestProxy(t *testing.T) {
rand.Seed(time.Now().UTC().UnixNano())
verboseOpt := true
insecureSkipVerify := true
var err error

// Logging of downstream (client to proxy server) commands and responses
dbgFile, err := smtpproxy.DownstreamDebug(downstreamDebug)
if err != nil {
log.Fatal(err)
var dbgFile *os.File
if downstreamDebug != "" {
dbgFile, err = os.OpenFile(downstreamDebug, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
} else {
defer dbgFile.Close()
log.Println("Proxy logging SMTP commands, responses and downstream DATA to", dbgFile.Name())
}
}
_ = dbgFile

s, be, err := smtpproxy.CreateProxy(inHostPort, outHostPort, verboseOpt, localhostCert, localhostKey, insecureSkipVerify, nil)
s, be, err := smtpproxy.CreateProxy(inHostPort, outHostPort, verboseOpt, localhostCert, localhostKey, insecureSkipVerify, dbgFile)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -486,11 +494,10 @@ func TestProxyFaultyInputs(t *testing.T) {
s = makeFakeSession(t, be, dummyServer)
r := strings.NewReader("it is only the hairs on a gooseberry") // this should cause a mailcopy error, as it's not valid RFC822
code, msg, err = s.Data(r, myWriteCloser{Writer: ioutil.Discard})

/*
if err == nil {
t.Errorf("This test should have returned a non-nil error code")
}
/* Simple proxy can send any text, does not check MIME parts etc
if err == nil {
t.Errorf("This test should have returned a non-nil error code")
}
*/

// Valid input mail, but cannot write to the destination stream
Expand All @@ -509,10 +516,10 @@ func TestProxyFaultyInputs(t *testing.T) {
be2 := smtpproxy.NewBackend(outHostPort, verboseOpt, insecureSkipVerify)
s = makeFakeSession(t, be2, dummyServer)
code, msg, err = s.Data(r, myWriteCloser{Writer: ioutil.Discard})
/*
if err == nil {
t.Errorf("This test should have returned a non-nil error code")
}
/* Simple proxy can send any text, does not check MIME parts etc
if err == nil {
t.Errorf("This test should have returned a non-nil error code")
}
*/

_, _, _, _ = caps, code, msg, w // workaround these variables being "unused" yet useful for debugging the test
Expand Down Expand Up @@ -651,7 +658,7 @@ func RandomRecipient() string {
}

//-----------------------------------------------------------------------------
/*

func TestClientOtherFunctions(t *testing.T) {
// client uses same certs as mock server and proxy, which seems fine for testing purposes
cfg, err := tlsClientConfig(localhostCert, localhostKey)
Expand Down Expand Up @@ -684,17 +691,12 @@ func TestClientOtherFunctions(t *testing.T) {
}
}

/*
func TestServerOtherFunctions(t *testing.T) {
verboseOpt := true
insecureSkipVerify := true
// Logging of downstream (client to proxy server) commands and responses
dbgFile, err := smtpproxy.DownstreamDebug(downstreamDebug)
if err != nil {
log.Fatal(err)
}
s, _, err := smtpproxy.CreateProxy("localhost:5586", outHostPort, verboseOpt, localhostCert, localhostKey, insecureSkipVerify, dbgFile)
// this time, don't log
var dbgFile *os.File
s, _, err := smtpproxy.CreateProxy(inHostPort2, outHostPort, verboseOpt, localhostCert, localhostKey, insecureSkipVerify, dbgFile)
if err != nil {
log.Fatal(err)
}
Expand All @@ -708,4 +710,3 @@ func TestServerOtherFunctions(t *testing.T) {
}
s.ForEachConn(f)
}
*/

0 comments on commit a1f7615

Please sign in to comment.