service_matcher-go/smtp/smtp_test.go

76 lines
1.4 KiB
Go
Raw Normal View History

2018-08-13 07:48:32 +00:00
package smtp
import (
"crypto/tls"
"fmt"
"net"
"strings"
"testing"
2018-09-03 13:36:57 +00:00
osm "git.loafle.net/overflow/service_matcher-go"
2018-08-13 07:48:32 +00:00
"github.com/stretchr/testify/assert"
)
func TestSMTPTLS(t *testing.T) {
conn, err := tls.Dial("tcp",
"192.168.1.215:465",
&tls.Config{
InsecureSkipVerify: true,
ServerName: "192.168.1.215",
},
)
if err != nil {
2018-09-03 13:36:57 +00:00
t.Error(err)
2018-08-13 07:48:32 +00:00
return
}
2018-09-03 13:36:57 +00:00
defer conn.Close()
matchCtx := osm.NewMatchCtx("192.168.1.215", 465)
2018-08-13 07:48:32 +00:00
b := make([]byte, 1024)
2018-09-03 13:36:57 +00:00
check(matchCtx, t, b, conn, "", "220")
check(matchCtx, t, b, conn, "helo test\r\n", "250")
check(matchCtx, t, b, conn, "quit\r\n", "221")
2018-08-13 07:48:32 +00:00
}
func TestSMTP(t *testing.T) {
2018-09-03 13:36:57 +00:00
conn, err := net.Dial("tcp", "192.168.1.15:25")
if err != nil {
t.Error(err)
return
}
defer conn.Close()
2018-08-13 07:48:32 +00:00
2018-09-03 13:36:57 +00:00
matchCtx := osm.NewMatchCtx("192.168.1.15", 25)
2018-08-13 07:48:32 +00:00
2018-09-03 13:36:57 +00:00
b := make([]byte, 1024)
2018-08-13 07:48:32 +00:00
2018-09-03 13:36:57 +00:00
check(matchCtx, t, b, conn, "", "220")
check(matchCtx, t, b, conn, "helo test\r\n", "250")
check(matchCtx, t, b, conn, "quit\r\n", "221")
2018-08-13 07:48:32 +00:00
}
2018-09-03 13:36:57 +00:00
func check(matchCtx *osm.MatchCtx, t *testing.T, b []byte, conn net.Conn, cmd string, compare string) {
2018-08-13 07:48:32 +00:00
if cmd != "" {
wlen, _ := conn.Write([]byte(cmd))
assert.Equal(t, wlen, len(cmd))
}
rlen, _ := conn.Read(b)
fmt.Println(rlen)
fmt.Println(len(b))
data := string(b[:rlen])
fmt.Println(data)
assert.Equal(t, true, rlen > 4)
splits := strings.Split(data, " ")
assert.Equal(t, compare, splits[0])
}