service_matcher-go/smtp/smtp_test.go
2018-09-03 22:36:57 +09:00

76 lines
1.4 KiB
Go

package smtp
import (
"crypto/tls"
"fmt"
"net"
"strings"
"testing"
osm "git.loafle.net/overflow/service_matcher-go"
"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 {
t.Error(err)
return
}
defer conn.Close()
matchCtx := osm.NewMatchCtx("192.168.1.215", 465)
b := make([]byte, 1024)
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")
}
func TestSMTP(t *testing.T) {
conn, err := net.Dial("tcp", "192.168.1.15:25")
if err != nil {
t.Error(err)
return
}
defer conn.Close()
matchCtx := osm.NewMatchCtx("192.168.1.15", 25)
b := make([]byte, 1024)
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")
}
func check(matchCtx *osm.MatchCtx, t *testing.T, b []byte, conn net.Conn, cmd string, compare string) {
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])
}