package smtp


import (
	"crypto/tls"
	"fmt"
	"github.com/stretchr/testify/assert"
	"net"
	"strings"
	"testing"
)

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.Log(err)
		return
	}

	b := make([]byte, 1024)

	check(t, b, conn, "", "220")
	check(t, b, conn, "helo test\r\n", "250")
	check(t, b, conn, "quit\r\n", "221")

	conn.Close()
}

func TestSMTP(t *testing.T) {

	conn, _ := net.Dial("tcp", "192.168.1.15:25")

	b := make([]byte, 1024)

	check(t, b, conn, "", "220")
	check(t, b, conn, "helo test\r\n", "250")
	check(t, b, conn, "quit\r\n", "221")

	conn.Close()

}
func check(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])

}
