summaryrefslogtreecommitdiff
path: root/internal/tracepoints/selector_test.go
blob: dd9084b91a4765de59ac88a955fd0b919305e478 (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
package tracepoints

import "testing"

func TestParseSelectorEmpty(t *testing.T) {
	// An empty attach and exclude string means accept everything.
	sel, err := ParseSelector("", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if !sel.ShouldAttach("sys_enter_openat") {
		t.Fatal("expected ShouldAttach=true for empty selector")
	}
}

func TestParseSelectorAttachFilter(t *testing.T) {
	// Only openat tracepoints should be accepted when an explicit attach list
	// is provided.
	sel, err := ParseSelector("^sys_enter_openat$,^sys_exit_openat$", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if !sel.ShouldAttach("sys_enter_openat") {
		t.Error("expected ShouldAttach=true for sys_enter_openat")
	}
	if !sel.ShouldAttach("sys_exit_openat") {
		t.Error("expected ShouldAttach=true for sys_exit_openat")
	}
	if sel.ShouldAttach("sys_enter_write") {
		t.Error("expected ShouldAttach=false for sys_enter_write (not in attach list)")
	}
}

func TestParseSelectorExcludeFilter(t *testing.T) {
	// Excluded tracepoints are rejected even when no explicit attach list exists.
	sel, err := ParseSelector("", "name_to_handle_at,open_by_handle_at")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if sel.ShouldAttach("sys_enter_name_to_handle_at") {
		t.Error("expected ShouldAttach=false for excluded tracepoint")
	}
	if !sel.ShouldAttach("sys_enter_openat") {
		t.Error("expected ShouldAttach=true for non-excluded tracepoint")
	}
}

func TestParseSelectorExcludeTakesPrecedence(t *testing.T) {
	// An exclude pattern beats an attach pattern when both match.
	sel, err := ParseSelector("openat", "openat")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	if sel.ShouldAttach("sys_enter_openat") {
		t.Error("expected ShouldAttach=false: exclude must beat attach")
	}
}

func TestParseSelectorInvalidRegexReturnsError(t *testing.T) {
	_, err := ParseSelector("[", "")
	if err == nil {
		t.Fatal("expected error for invalid attach regex")
	}
}

func TestParseSelectorInvalidExcludeRegexReturnsError(t *testing.T) {
	_, err := ParseSelector("", "[")
	if err == nil {
		t.Fatal("expected error for invalid exclude regex")
	}
}

func TestSelectorCloneIsIndependent(t *testing.T) {
	// Modifications to the clone's Attach slice must not affect the original.
	sel, err := ParseSelector("openat", "")
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	clone := sel.Clone()
	clone.Attach = nil
	if !sel.ShouldAttach("sys_enter_openat") {
		t.Error("original Selector was mutated through clone")
	}
}

func TestSelectorCloneCopiesSyscallAllowlist(t *testing.T) {
	sel := Selector{
		Syscalls: map[string]struct{}{
			"openat": {},
		},
		RestrictSyscalls: true,
	}
	clone := sel.Clone()
	delete(clone.Syscalls, "openat")
	if !sel.ShouldAttach("sys_enter_openat") {
		t.Fatal("original syscall allowlist mutated through clone")
	}
}