assert_test.go 1.85 KB
Newer Older
liming6's avatar
liming6 committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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) {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
	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)
	}
liming6's avatar
liming6 committed
70
}