summaryrefslogtreecommitdiff
path: root/internal/eventloop_socket_test.go
blob: 7712b3ca9ca769779969612b3cce31626f096334 (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package internal

import (
	"testing"

	"ior/internal/event"
	"ior/internal/globalfilter"
	"ior/internal/types"
)

func TestHandleSocketExitTracksReturnedFd(t *testing.T) {
	el := mustNewEventLoop(t, eventLoopConfig{})

	enter := &types.SocketEvent{
		EventType: types.ENTER_SOCKET_EVENT,
		TraceId:   types.SYS_ENTER_SOCKET,
		Time:      100,
		Pid:       42,
		Tid:       43,
		Family:    1,
		Type:      2,
		Protocol:  0,
	}
	exit := &types.RetEvent{
		EventType: types.EXIT_SOCKET_EVENT,
		TraceId:   types.SYS_EXIT_SOCKET,
		Time:      200,
		Ret:       55,
		Pid:       42,
		Tid:       43,
	}
	ep := &event.Pair{EnterEv: enter, ExitEv: exit}

	if ok := el.handleSocketExit(ep, enter); !ok {
		t.Fatal("handleSocketExit returned false")
	}
	verifyFileDescriptor(t, el, 55, "socket:1:2:0")
}

func TestHandleSocketExitAppliesPairFilter(t *testing.T) {
	el := mustNewEventLoop(t, eventLoopConfig{
		filter: globalfilter.Filter{
			Syscall: &globalfilter.StringFilter{Pattern: "openat"},
		},
	})

	enter := &types.SocketEvent{
		EventType: types.ENTER_SOCKET_EVENT,
		TraceId:   types.SYS_ENTER_SOCKET,
		Time:      100,
		Pid:       42,
		Tid:       43,
		Family:    1,
		Type:      2,
		Protocol:  0,
	}
	exit := &types.RetEvent{
		EventType: types.EXIT_SOCKET_EVENT,
		TraceId:   types.SYS_EXIT_SOCKET,
		Time:      200,
		Ret:       55,
		Pid:       42,
		Tid:       43,
	}
	ep := &event.Pair{EnterEv: enter, ExitEv: exit}

	if ok := el.handleSocketExit(ep, enter); ok {
		t.Fatal("handleSocketExit should reject pair due to filter mismatch")
	}
}

func TestHandleSocketpairExitTracksReturnedFdsFromExitEvent(t *testing.T) {
	el := mustNewEventLoop(t, eventLoopConfig{})

	enter := &types.SocketpairEvent{
		EventType: types.ENTER_SOCKETPAIR_EVENT,
		TraceId:   types.SYS_ENTER_SOCKETPAIR,
		Time:      100,
		Pid:       77,
		Tid:       78,
		Family:    1,
		Type:      1,
		Protocol:  0,
		Sv0:       -1,
		Sv1:       -1,
		Ret:       0,
	}
	exit := &types.SocketpairEvent{
		EventType: types.EXIT_SOCKETPAIR_EVENT,
		TraceId:   types.SYS_EXIT_SOCKETPAIR,
		Time:      200,
		Pid:       77,
		Tid:       78,
		Family:    1,
		Type:      1,
		Protocol:  0,
		Sv0:       61,
		Sv1:       62,
		Ret:       0,
	}
	ep := &event.Pair{EnterEv: enter, ExitEv: exit}

	if ok := el.handleSocketpairExit(ep, enter); !ok {
		t.Fatal("handleSocketpairExit returned false")
	}
	verifyFileDescriptor(t, el, 61, "socket:1:1:0")
	verifyFileDescriptor(t, el, 62, "socket:1:1:0")
}

func TestInitRawHandlersRegistersSocketEvents(t *testing.T) {
	el := mustNewEventLoop(t, eventLoopConfig{})
	if _, ok := el.rawHandlers[types.ENTER_SOCKET_EVENT]; !ok {
		t.Fatal("ENTER_SOCKET_EVENT handler is not registered")
	}
	if _, ok := el.rawHandlers[types.ENTER_SOCKETPAIR_EVENT]; !ok {
		t.Fatal("ENTER_SOCKETPAIR_EVENT handler is not registered")
	}
	if _, ok := el.rawHandlers[types.EXIT_SOCKETPAIR_EVENT]; !ok {
		t.Fatal("EXIT_SOCKETPAIR_EVENT handler is not registered")
	}
}