package activedirectory

import (
	"fmt"
	"git.loafle.net/overflow/overflow_probe/discovery/discovery/types"
	"net"
	"testing"

	"crypto/tls"
)

func TestADNor(t *testing.T) {
	client, err := net.Dial("tcp", "192.168.1.1:389")

	if err != nil {
		t.Log(err)
	}

	defer client.Close()

	dDRun(client, t)
}

func TestADTLS(t *testing.T) {
	conn, err := tls.Dial(
		"tcp",
		"192.168.1.1:636",
		&tls.Config{
			InsecureSkipVerify: true,
			ServerName:         "192.168.1.1",
		},
	)

	if err != nil {
		t.Log(err)
		return
	}

	defer conn.Close()

	dDRun(conn, t)
}

func dDRun(client net.Conn, t *testing.T) {

	lm := NewActiveDirectoryMatcher()

	port := types.NewPort("389", types.NewHost("192.168.1.1"), types.TYPE_TCP)

	var ipport string
	ipport = port.Host.Ip + ":" + string(port.Port)

	fmt.Println(ipport)

	fmt.Println(lm.PacketCount())

	for ii := 0; ii < lm.PacketCount(); ii++ {

		pack := lm.Packet(ii)

		fmt.Println(pack)

		client.Write(pack.Buffer)

		bytes := make([]byte, 1024)

		read, _ := client.Read(bytes)

		fmt.Println(bytes)

		b := lm.Match(ii, packet.NewPacket(bytes, read), scanInfo)

		if b {
			fmt.Println("Good")
		}

	}

	t.Log(scanInfo)
}
