package mysql

import (
	"crypto/tls"
	"git.loafle.net/overflow/overflow_probe/discovery/core/scan/service/matcher/packet"
	"net"
	"testing"
)

func TestMySql(t *testing.T) {

	m := NewMySqlMatcher()

	/*
	   192.168.1.103:3306 - normal
	   192.168.1.105:8306 - ssl
	   192.168.1.203:3306 - mysql with error code
	*/
	conn, _ := net.Dial("tcp", "192.168.1.215:3306")

	defer conn.Close()

	bytes := make([]byte, 1024)
	n, _ := conn.Read(bytes)
	p := packet.NewPacket(bytes, n)

	if m.Match(0, p, nil) {
		t.Log(m.ServiceName())
		return
	}

	t.Error("MySQL not found")

}

func TestCassandraTLS(t *testing.T) {

	m := NewMySqlMatcher()

	conn, err := tls.Dial(
		"tcp",
		"192.168.1.105:8306",
		&tls.Config{
			InsecureSkipVerify: true,
			ServerName:         "192.168.1.105",
		},
	)
	if err != nil {
		t.Fatal(err)
	}

	defer conn.Close()

	for i := 0; i < m.PacketCount(); i++ {

		pack := m.Packet(i)
		conn.Write(pack.Buffer)
		bytes := make([]byte, 1024)
		n, _ := conn.Read(bytes)
		p := packet.NewPacket(bytes, n)

		if m.Match(i, p, nil) {
			t.Log(m.ServiceName())
			return
		}

		t.Error("MySQL not found")
	}

}
