-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_records_test.go
77 lines (70 loc) · 1.55 KB
/
log_records_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
package main
import (
"os"
"testing"
)
func TestProcessFile(t *testing.T) {
test := []struct {
filename string
testData []byte
tagMap map[string]int
portProtocolMap map[string]int
expectedError error
}{
{
filename: "valid test input",
testData: []byte(`80,tcp,web
53,udp,dns
22,tcp,ssh
80,tcp
`),
tagMap: map[string]int{
"web": 1,
"dns": 1,
"ssh": 1,
"untagged": 1,
},
portProtocolMap: map[string]int{
"80,tcp": 2,
"53,udp": 1,
"22,tcp": 1,
},
},
{
filename: "valid test input-empty file",
testData: []byte(``),
tagMap: map[string]int{},
portProtocolMap: map[string]int{},
},
}
for _, tt := range test {
// Create a temporary file to simulate the input file
tmpfile, err := os.CreateTemp("", "testfile")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
if _, err := tmpfile.Write([]byte(tt.testData)); err != nil {
t.Fatal(err)
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
// Call ProcessFile function with the temp file
tagMap, portProtocolMap, err := ProcessFile(tmpfile.Name())
if err != nil {
t.Fatalf("ProcessFile returned an error: %v", err)
}
// Validate tagMap
for k, v := range tt.tagMap {
if tagMap[k] != v {
t.Errorf("tagMap[%s] = %d; want %d", k, tagMap[k], v)
}
}
for k, v := range tt.portProtocolMap {
if portProtocolMap[k] != v {
t.Errorf("portProtocolMap[%s] = %d; want %d", k, portProtocolMap[k], v)
}
}
}
}