package asset

import (
	"regexp"
	"testing"
	"time"
)

var (
	ReSSHLogin   = regexp.MustCompile(`^<\d+>[A-Z][a-z]{2} \d+ \d+:\d+:\d+ (\S+) sshd\[(\d+)\]: Accepted (\S+) for (\S+) from (\S+) port (\d+) ssh(?:|\d+)$`)
	ReSSHLoginPK = regexp.MustCompile(`^<\d+>[A-Z][a-z]{2} \d+ \d+:\d+:\d+ (\S+) sshd\[(\d+)\]: Accepted publickey for (\S+) from (\S+) port (?:\d+) ssh(?:|\d+):\s+(\S+)\s+(?:sha|SHA)256:(.*)$`)
	ReSSHLogout  = regexp.MustCompile(`^<\d+>[A-Z][a-z]{2} \d+ \d+:\d+:\d+ (\S+) sshd\[(\d+)\]: pam_unix\(sshd:session\): session closed for user (.*)$`)
)

func Test1(t *testing.T) {
	target := "<86>Sep 28 14:52:37 login01 sshd[4092]: Accepted keyboard-interactive/pam for fengchao from 10.206.8.202 port 55805 ssh2"
	if ReSSHLogin.MatchString(target) {
		start := time.Now()
		f := ReSSHLogin.FindStringSubmatch(target)
		s := time.Since(start)
		t.Logf("regexp use %d ms", s.Milliseconds())
		for _, v := range f {
			t.Log(v)
		}
	} else {
		t.Error("not match")
	}
}

func Test2(t *testing.T) {
	target := "<86>Dec 23 15:57:07 bw11 sshd[1575855]: Accepted publickey for root from 10.16.4.1 port 60058 ssh2: ED25519 SHA256:0o84iZ8MJUCRzTIipR8eLzX2g+Rx96MKMVq/RakG/GA"
	if ReSSHLoginPK.MatchString(target) {
		f := ReSSHLoginPK.FindStringSubmatch(target)
		for _, v := range f {
			t.Log(v)
		}
	} else {
		t.Error("not match")
	}
}

func Test3(t *testing.T) {
	target := "<86>Dec 22 19:09:50 liming-ecs sshd[3831482]: pam_unix(sshd:session): session closed for user root"
	if ReSSHLogout.MatchString(target) {
		f := ReSSHLogout.FindStringSubmatch(target)
		for _, v := range f {
			t.Log(v)
		}
	} else {
		t.Error("not match")
	}
}

func Test4(t *testing.T) {
	c := make(chan int, 128)
	go func(c chan<- int) {
		for i := range 100 {
			t.Logf("put: %d", i)
			c <- i
		}
	}(c)
	go func() {
		time.Sleep(time.Second * 5)
		close(c)
	}()
	
	for i := range c {
		t.Logf("%d", i)
	}
}
