-
Notifications
You must be signed in to change notification settings - Fork 18
/
eventhandler_test.go
92 lines (83 loc) · 3.52 KB
/
eventhandler_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
package main
import (
"fmt"
"net/http"
"testing"
"github.com/jarcoal/httpmock"
)
func TestEventHandler_HandleEvent(t *testing.T) {
type fields struct {
CorrelationId string
Repo string
ApiClient *ApiClient
ProcessConnectionMap map[string]bool
ProcessFileMap map[string]bool
ProcessMap map[string]*Process
}
type args struct {
event *Event
}
apiclient := &ApiClient{Client: &http.Client{}, APIURL: agentApiBaseUrl}
httpmock.ActivateNonDefault(apiclient.Client)
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/github/owner/repo/actions/jobs/123/networkconnection", agentApiBaseUrl),
httpmock.NewStringResponder(200, ""))
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/github/owner/repo/actions/jobs/123/fileevent", agentApiBaseUrl),
httpmock.NewStringResponder(200, ""))
tests := []struct {
name string
fields fields
args args
}{
{name: "netMonitorEvent", fields: fields{CorrelationId: "123", Repo: "owner/repo", ApiClient: apiclient, ProcessConnectionMap: make(map[string]bool)},
args: args{event: &Event{IPAddress: "2.2.2.2", Port: "443", EventType: netMonitorTag, Exe: "/path/to/exe"}}},
{name: "netMonitorPrivateIP", fields: fields{CorrelationId: "123", Repo: "owner/repo", ApiClient: apiclient, ProcessConnectionMap: make(map[string]bool)},
args: args{event: &Event{IPAddress: "127.0.0.53", Port: "53", EventType: netMonitorTag, Exe: "/path/to/exe"}}},
{name: "fileMonitorEvent", fields: fields{CorrelationId: "123", Repo: "owner/repo", ApiClient: apiclient, ProcessFileMap: make(map[string]bool)},
args: args{event: &Event{EventType: fileMonitorTag, Exe: "/path/to/exe", FileName: ".git/objects"}}},
{name: "fileMonitorEventSourceCode", fields: fields{CorrelationId: "123", Repo: "owner/repo", ApiClient: apiclient, ProcessFileMap: make(map[string]bool)},
args: args{event: &Event{EventType: fileMonitorTag, Exe: "/path/to/exe", FileName: "/path/to/code/code.go"}}},
}
for _, tt := range tests {
cache := InitCache(EgressPolicyAudit)
proxy := &DNSProxy{
Cache: &cache,
ApiClient: apiclient,
EgressPolicy: EgressPolicyAudit,
ReverseIPLookup: make(map[string]string),
}
t.Run(tt.name, func(t *testing.T) {
eventHandler := &EventHandler{
CorrelationId: tt.fields.CorrelationId,
Repo: tt.fields.Repo,
ApiClient: tt.fields.ApiClient,
ProcessConnectionMap: tt.fields.ProcessConnectionMap,
ProcessFileMap: tt.fields.ProcessFileMap,
ProcessMap: tt.fields.ProcessMap,
SourceCodeMap: make(map[string][]*Event),
DNSProxy: proxy,
}
eventHandler.HandleEvent(tt.args.event)
})
}
}
func TestGetContainerIdByPid(t *testing.T) {
type args struct {
cgroupPath string
}
tests := []struct {
name string
args args
want string
}{
{name: "Success", args: args{cgroupPath: "./testfiles/cgroup.txt"}, want: "0002745556f5dd47cd58d08a2b463e87ad792b2e64886a7f9ef20a8087a95a64"},
{name: "Success with containerd", args: args{cgroupPath: "./testfiles/cgroup-containerd.txt"}, want: "9f21229140eb8ff3ab5b2b76c2536ddf4cc10811bc408ae9430324a0d85cf112"},
{name: "Success with docker", args: args{cgroupPath: "./testfiles/cgroup-docker.txt"}, want: "53dea357e1650308b9839301f307414740c75a149ac748713a1d827e1f7065a9"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetContainerIdByPid(tt.args.cgroupPath); got != tt.want {
t.Errorf("GetContainerIdByPid() = %v, want %v", got, tt.want)
}
})
}
}