Debian Codegraph @

landlock/config_test.go

raw
1 package landlock
2
3 import (
4 "fmt"
5 "testing"
6
7 ll "github.com/landlock-lsm/go-landlock/landlock/syscall"
8 )
9
10 func TestConfigString(t *testing.T) {
11 for _, tc := range []struct {
12 cfg Config
13 want string
14 }{
15 {
16 cfg: Config{handledAccessFS: 0, handledAccessNet: 0},
17 want: "{Landlock V0; FS: ∅; Net: ∅; Scoped: ∅}",
18 },
19 {
20 cfg: Config{handledAccessFS: ll.AccessFSWriteFile},
21 want: "{Landlock V1; FS: {write_file}; Net: ∅; Scoped: ∅}",
22 },
23 {
24 cfg: Config{handledAccessNet: ll.AccessNetBindTCP},
25 want: "{Landlock V4; FS: ∅; Net: {bind_tcp}; Scoped: ∅}",
26 },
27 {
28 cfg: V1,
29 want: "{Landlock V1; FS: all; Net: ∅; Scoped: ∅}",
30 },
31 {
32 cfg: V1.BestEffort(),
33 want: "{Landlock V1; FS: all; Net: ∅; Scoped: ∅ (best effort)}",
34 },
35 {
36 cfg: V5,
37 want: "{Landlock V5; FS: all; Net: all; Scoped: ∅}",
38 },
39 {
40 cfg: V6,
41 want: "{Landlock V6; FS: all; Net: all; Scoped: all}",
42 },
43 {
44 // V7 and V8 are the same as V6...
45 cfg: V7,
46 want: "{Landlock V6; FS: all; Net: all; Scoped: all}",
47 },
48 {
49 cfg: V8,
50 want: "{Landlock V6; FS: all; Net: all; Scoped: all}",
51 },
52 {
53 cfg: V9,
54 want: "{Landlock V9; FS: all; Net: all; Scoped: all}",
55 },
56 {
57 // ...unless you enable one of the logging flags.
58 cfg: V7.EnableLoggingForSubprocesses(),
59 want: "{Landlock V7; FS: all; Net: all; Scoped: all (flags: log_new_exec_on)}",
60 },
61 {
62 // ...or some other ones.
63 cfg: V7.DisableLoggingForOriginatingProcess().DisableLoggingForSubdomains(),
64 want: "{Landlock V7; FS: all; Net: all; Scoped: all (flags: log_same_exec_off,log_subdomains_off)}",
65 },
66 {
67 cfg: Config{handledAccessFS: 1 << 63},
68 want: "{Landlock V???; FS: {1<<63}; Net: ∅; Scoped: ∅}",
69 },
70 } {
71 got := tc.cfg.String()
72 if got != tc.want {
73 t.Errorf("cfg.String() = %q, want %q", got, tc.want)
74 }
75 }
76 }
77
78 func TestNewConfig(t *testing.T) {
79 for _, tt := range []struct {
80 name string
81 args []any
82 want Config
83 }{
84 {
85 name: "fs_write_file",
86 args: []any{AccessFSSet(ll.AccessFSWriteFile)},
87 want: Config{handledAccessFS: ll.AccessFSWriteFile},
88 },
89 {
90 name: "fs_refer",
91 args: []any{AccessFSSet(ll.AccessFSRefer)},
92 want: Config{handledAccessFS: ll.AccessFSRefer},
93 },
94 {
95 name: "net_bind",
96 args: []any{AccessNetSet(ll.AccessNetBindTCP)},
97 want: Config{handledAccessNet: ll.AccessNetBindTCP},
98 },
99 {
100 name: "scoped_signal",
101 args: []any{ScopedSet(ll.ScopeSignal)},
102 want: Config{scoped: ll.ScopeSignal},
103 },
104 {
105 name: "christmas_tree",
106 args: []any{
107 AccessFSSet(ll.AccessFSReadDir | ll.AccessFSReadFile),
108 AccessNetSet(ll.AccessNetBindTCP | ll.AccessNetConnectTCP),
109 ScopedSet(ll.ScopeSignal | ll.ScopeAbstractUnixSocket),
110 },
111 want: Config{
112 handledAccessFS: ll.AccessFSReadDir | ll.AccessFSReadFile,
113 handledAccessNet: ll.AccessNetBindTCP | ll.AccessNetConnectTCP,
114 scoped: ll.ScopeSignal | ll.ScopeAbstractUnixSocket,
115 },
116 },
117 } {
118 t.Run(tt.name, func(t *testing.T) {
119 cfg, err := NewConfig(tt.args...)
120 if err != nil {
121 t.Errorf("NewConfig(): expected success, got %v", err)
122 }
123 if *cfg != tt.want {
124 t.Errorf("cfg = %v, want %v", cfg, tt.want)
125 }
126 })
127 }
128 }
129
130 func TestNewConfigEmpty(t *testing.T) {
131 // Constructing an empty config is a bit pointless, but should work.
132 c, err := NewConfig()
133 if err != nil {
134 t.Errorf("NewConfig(): expected success, got %v", err)
135 }
136 want := AccessFSSet(0)
137 if c.handledAccessFS != want {
138 t.Errorf("c.handledAccessFS = %v, want %v", c.handledAccessFS, want)
139 }
140 }
141
142 func TestNewConfigFailures(t *testing.T) {
143 for _, args := range [][]any{
144 {ll.AccessFSWriteFile},
145 {123},
146 {"a string"},
147 {"foo", 42},
148 // May not specify two AccessFSSets
149 {AccessFSSet(ll.AccessFSWriteFile), AccessFSSet(ll.AccessFSReadFile)},
150 // May not specify an unsupported AccessFSSet value
151 {AccessFSSet(1 << 17)},
152 {AccessFSSet(1 << 63)},
153 } {
154 _, err := NewConfig(args...)
155 if err == nil {
156 t.Errorf("NewConfig(%v) success, expected error", args)
157 }
158 }
159 }
160
161 func TestCompatibleWithABI(t *testing.T) {
162 for i, abi := range abiInfos {
163 cfg := abi.asConfig()
164 t.Run(fmt.Sprintf("V%v", i), func(t *testing.T) {
165 // abiInfos[6, 7, 8].asConfig() are all the same.
166 if i == 7 || i == 8 {
167 i = 6
168 }
169 for j := range i {
170 if cfg.compatibleWithABI(abiInfos[j]) {
171 t.Errorf("cfg.compatibleWithABI(abiInfos[%v]) = true, want false", j)
172 }
173 }
174 for j := i; j < len(abiInfos); j++ {
175 if !cfg.compatibleWithABI(abiInfos[j]) {
176 t.Errorf("cfg.compatibleWithABI(abiInfos[%v]) = false, want true", j)
177 }
178 }
179 })
180 }
181 }
182
183 func TestRestrictTo(t *testing.T) {
184 for i, abi := range abiInfos {
185 cfg := abi.asConfig()
186 t.Run(fmt.Sprintf("V%v", i), func(t *testing.T) {
187 for j := range abiInfos {
188 compatCfg := cfg.restrictTo(abiInfos[j])
189 if !compatCfg.compatibleWithABI(abiInfos[j]) {
190 t.Errorf("compatCfg.compatibleWithABI(abiInfos[%v]) = false, want true", j)
191 }
192 }
193 })
194 }
195 }