-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathprocessor_test.go
101 lines (92 loc) · 2.27 KB
/
processor_test.go
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package event_processor
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"strings"
"testing"
"time"
)
var (
testFile = "testdata/all.json"
)
type SSLDataEventTmp struct {
//Event_type uint8 `json:"Event_type"`
DataType int64 `json:"DataType"`
Timestamp uint64 `json:"Timestamp"`
Pid uint32 `json:"Pid"`
Tid uint32 `json:"Tid"`
DataLen int32 `json:"DataLen"`
Comm [16]byte `json:"Comm"`
Fd uint32 `json:"Fd"`
Version int32 `json:"Version"`
Data [4096]byte `json:"Data"`
}
func TestEventProcessor_Serve(t *testing.T) {
logger := log.Default()
//var buf bytes.Buffer
//logger.SetOutput(&buf)
var output = "./output.log"
f, e := os.Create(output)
if e != nil {
t.Fatal(e)
}
logger.SetOutput(f)
ep := NewEventProcessor(f, true)
go func() {
var err error
err = ep.Serve()
if err != nil {
log.Fatalf(err.Error())
}
}()
content, err := os.ReadFile(testFile)
if err != nil {
//Do something
log.Fatalf("open file error: %s, file:%s", err.Error(), testFile)
}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
if line == "" {
continue
}
var eventSSL SSLDataEventTmp
err := json.Unmarshal([]byte(line), &eventSSL)
if err != nil {
t.Fatalf("json unmarshal error: %s, body:%v", err.Error(), line)
}
payloadFile := fmt.Sprintf("testdata/%d.bin", eventSSL.Timestamp)
b, e := os.ReadFile(payloadFile)
if e != nil {
t.Fatalf("read payload file error: %s, file:%s", e.Error(), payloadFile)
}
copy(eventSSL.Data[:], b)
ep.Write(&BaseEvent{DataLen: eventSSL.DataLen, Data: eventSSL.Data, DataType: eventSSL.DataType, Timestamp: eventSSL.Timestamp, Pid: eventSSL.Pid, Tid: eventSSL.Tid, Comm: eventSSL.Comm, Fd: eventSSL.Fd, Version: eventSSL.Version})
}
tick := time.NewTicker(time.Second * 10)
<-tick.C
err = ep.Close()
logger.SetOutput(io.Discard)
bufString, e := os.ReadFile(output)
if e != nil {
t.Fatal(e)
}
lines = strings.Split(string(bufString), "\n")
ok := true
for _, line := range lines {
if strings.Contains(strings.ToLower(line), "dump") {
t.Log(line)
ok = false
}
}
if err != nil {
t.Fatalf("close error: %s", err.Error())
}
if !ok {
t.Fatalf("some errors occurred")
}
//t.Log(string(bufString))
t.Log("done")
}