-
Notifications
You must be signed in to change notification settings - Fork 30
/
parse_test.go
426 lines (410 loc) · 19.3 KB
/
parse_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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"testing"
"github.com/google/go-cmp/cmp"
)
var testPolicyNoTrace = []string{
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*mangle",
":PREROUTING ACCEPT [6644470:1039872350]",
":INPUT ACCEPT [6644470:1039872350]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [6093378:960619800]",
":POSTROUTING ACCEPT [6093378:960619800]",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*raw",
":PREROUTING ACCEPT [2502678:375978678]",
":OUTPUT ACCEPT [2325323:325134634]",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*filter",
":INPUT ACCEPT [38659:3282330]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [35452:1942057]",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*nat",
":PREROUTING ACCEPT [1292886:69592056]",
":POSTROUTING ACCEPT [71297:4698134]",
":OUTPUT ACCEPT [71295:4698054]",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
}
var testPolicyNoTraceRules = []string{
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*mangle",
":PREROUTING ACCEPT [6644470:1039872350]",
":INPUT ACCEPT [6644470:1039872350]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [6093378:960619800]",
":POSTROUTING ACCEPT [6093378:960619800]",
"-A INPUT -m mark --mark 0x1/0x1 -j ACCEPT",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*raw",
":PREROUTING ACCEPT [2502678:375978678]",
":OUTPUT ACCEPT [2325323:325134634]",
"-A OUTPUT -j ACCEPT",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*filter",
":INPUT ACCEPT [38659:3282330]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [35452:1942057]",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*nat",
":PREROUTING ACCEPT [1292886:69592056]",
":POSTROUTING ACCEPT [71297:4698134]",
":OUTPUT ACCEPT [71295:4698054]",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
}
var testPolicyTraceID3 = []string{
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*mangle",
":PREROUTING ACCEPT [6644470:1039872350]",
":INPUT ACCEPT [6644470:1039872350]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [6093378:960619800]",
":POSTROUTING ACCEPT [6093378:960619800]",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:0\" --nflog-group 22",
"-I INPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:1\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:2\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:3\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:4\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*raw",
":PREROUTING ACCEPT [2502678:375978678]",
":OUTPUT ACCEPT [2325323:325134634]",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:5\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:6\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*filter",
":INPUT ACCEPT [38659:3282330]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [35452:1942057]",
"-I INPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:7\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:8\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:9\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*nat",
":PREROUTING ACCEPT [1292886:69592056]",
":POSTROUTING ACCEPT [71297:4698134]",
":OUTPUT ACCEPT [71295:4698054]",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:10\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:11\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:12\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
}
var testPolicyTraceID3and4 = []string{
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*mangle",
":PREROUTING ACCEPT [6644470:1039872350]",
":INPUT ACCEPT [6644470:1039872350]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [6093378:960619800]",
":POSTROUTING ACCEPT [6093378:960619800]",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:0\" --nflog-group 22",
"-I INPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:1\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:2\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:3\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:4\" --nflog-group 22",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:0\" --nflog-group 22",
"-I INPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:1\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:2\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:3\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:4\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*raw",
":PREROUTING ACCEPT [2502678:375978678]",
":OUTPUT ACCEPT [2325323:325134634]",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:5\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:6\" --nflog-group 22",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:5\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:6\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*filter",
":INPUT ACCEPT [38659:3282330]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [35452:1942057]",
"-I INPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:7\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:8\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:9\" --nflog-group 22",
"-I INPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:7\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:8\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:9\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*nat",
":PREROUTING ACCEPT [1292886:69592056]",
":POSTROUTING ACCEPT [71297:4698134]",
":OUTPUT ACCEPT [71295:4698054]",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:10\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:11\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:3:12\" --nflog-group 22",
"-I PREROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:10\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:11\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -j NFLOG --nflog-prefix \"iptr:4:12\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
}
var testPolicyTraceID5withMark = []string{
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*mangle",
":PREROUTING ACCEPT [6644470:1039872350]",
":INPUT ACCEPT [6644470:1039872350]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [6093378:960619800]",
":POSTROUTING ACCEPT [6093378:960619800]",
"-I PREROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:0\" --nflog-group 22",
"-I INPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:1\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:2\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:3\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:4\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*raw",
":PREROUTING ACCEPT [2502678:375978678]",
":OUTPUT ACCEPT [2325323:325134634]",
"-I PREROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:5\" --nflog-group 22",
"-I PREROUTING -s 1.2.3.4 -m comment --comment \"iptr:5:mark\" -m limit --limit 60/minute --limit-burst 1 -j MARK --set-xmark 0x124/0x124",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:6\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*filter",
":INPUT ACCEPT [38659:3282330]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [35452:1942057]",
"-I INPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:7\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:8\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:9\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*nat",
":PREROUTING ACCEPT [1292886:69592056]",
":POSTROUTING ACCEPT [71297:4698134]",
":OUTPUT ACCEPT [71295:4698054]",
"-I PREROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:10\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:11\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:5:12\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
}
var testPolicyTraceID6withMarkAndRules = []string{
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*mangle",
":PREROUTING ACCEPT [6644470:1039872350]",
":INPUT ACCEPT [6644470:1039872350]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [6093378:960619800]",
":POSTROUTING ACCEPT [6093378:960619800]",
"-A INPUT -m mark --mark 0x125/0x125 -j NFLOG --nflog-prefix \"iptr:6:0\" --nflog-group 22",
"-A INPUT -m mark --mark 0x1/0x1 -j ACCEPT",
"-I PREROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:1\" --nflog-group 22",
"-I INPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:2\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:3\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:4\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:5\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*raw",
":PREROUTING ACCEPT [2502678:375978678]",
":OUTPUT ACCEPT [2325323:325134634]",
"-A OUTPUT -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:6\" --nflog-group 22",
"-A OUTPUT -j ACCEPT",
"-I PREROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:7\" --nflog-group 22",
"-I PREROUTING -s 1.2.3.4 -m comment --comment \"iptr:6:mark\" -m limit --limit 60/minute --limit-burst 1 -j MARK --set-xmark 0x124/0x124",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:8\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*filter",
":INPUT ACCEPT [38659:3282330]",
":FORWARD ACCEPT [0:0]",
":OUTPUT ACCEPT [35452:1942057]",
"-I INPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:9\" --nflog-group 22",
"-I FORWARD -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:10\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:11\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
"# Generated by iptables-save v1.6.0 on Sat Nov 24 11:59:50 2018",
"*nat",
":PREROUTING ACCEPT [1292886:69592056]",
":POSTROUTING ACCEPT [71297:4698134]",
":OUTPUT ACCEPT [71295:4698054]",
"-I PREROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:12\" --nflog-group 22",
"-I POSTROUTING -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:13\" --nflog-group 22",
"-I OUTPUT -s 1.2.3.4 -m mark --mark 0x124/0x124 -j NFLOG --nflog-prefix \"iptr:6:14\" --nflog-group 22",
"COMMIT",
"# Completed on Sat Nov 24 11:59:50 2018",
}
func TestClearIptablesPolicy(t *testing.T) {
policies := []struct {
policy []string
cleanupID int
expected []string
}{
{nil, 0, nil},
{[]string{"foobar"}, 0, []string{"foobar"}},
{testPolicyNoTrace, 0, testPolicyNoTrace},
{testPolicyNoTraceRules, 0, testPolicyNoTraceRules},
{testPolicyNoTrace, 3, testPolicyNoTrace},
{testPolicyTraceID3, 3, testPolicyNoTrace},
{testPolicyTraceID3, 0, testPolicyNoTrace},
{testPolicyTraceID3, 4, testPolicyTraceID3},
{testPolicyTraceID3and4, 4, testPolicyTraceID3},
{testPolicyTraceID3and4, 5, testPolicyTraceID3and4},
{testPolicyTraceID3and4, 0, testPolicyNoTrace},
{testPolicyTraceID5withMark, 5, testPolicyNoTrace},
{testPolicyTraceID5withMark, 0, testPolicyNoTrace},
{testPolicyTraceID5withMark, 6, testPolicyTraceID5withMark},
{testPolicyTraceID6withMarkAndRules, 6, testPolicyNoTraceRules},
{testPolicyTraceID6withMarkAndRules, 0, testPolicyNoTraceRules},
{testPolicyTraceID6withMarkAndRules, 7, testPolicyTraceID6withMarkAndRules},
}
for _, policy := range policies {
got := clearIptablesPolicy(policy.policy, policy.cleanupID)
if !cmp.Equal(got, policy.expected) {
t.Errorf("clearIptablesPolicy was incorrect, got: '%s', expected: '%s'", got, policy.expected)
}
}
}
var testRuleMap = map[int]iptablesRule{
0: {Table: "mangle", Chain: "PREROUTING", ChainEntry: true},
1: {Table: "mangle", Chain: "INPUT", ChainEntry: true},
2: {Table: "mangle", Chain: "FORWARD", ChainEntry: true},
3: {Table: "mangle", Chain: "OUTPUT", ChainEntry: true},
4: {Table: "mangle", Chain: "POSTROUTING", ChainEntry: true},
5: {Table: "raw", Chain: "PREROUTING", ChainEntry: true},
6: {Table: "raw", Chain: "OUTPUT", ChainEntry: true},
7: {Table: "filter", Chain: "INPUT", ChainEntry: true},
8: {Table: "filter", Chain: "FORWARD", ChainEntry: true},
9: {Table: "filter", Chain: "OUTPUT", ChainEntry: true},
10: {Table: "nat", Chain: "PREROUTING", ChainEntry: true},
11: {Table: "nat", Chain: "POSTROUTING", ChainEntry: true},
12: {Table: "nat", Chain: "OUTPUT", ChainEntry: true},
}
var testRuleMapWithRules = map[int]iptablesRule{
0: {Table: "mangle", Chain: "INPUT", Rule: "-m mark --mark 0x1/0x1 -j ACCEPT", ChainEntry: false},
1: {Table: "mangle", Chain: "PREROUTING", ChainEntry: true},
2: {Table: "mangle", Chain: "INPUT", ChainEntry: true},
3: {Table: "mangle", Chain: "FORWARD", ChainEntry: true},
4: {Table: "mangle", Chain: "OUTPUT", ChainEntry: true},
5: {Table: "mangle", Chain: "POSTROUTING", ChainEntry: true},
6: {Table: "raw", Chain: "OUTPUT", Rule: "-j ACCEPT", ChainEntry: false},
7: {Table: "raw", Chain: "PREROUTING", ChainEntry: true},
8: {Table: "raw", Chain: "OUTPUT", ChainEntry: true},
9: {Table: "filter", Chain: "INPUT", ChainEntry: true},
10: {Table: "filter", Chain: "FORWARD", ChainEntry: true},
11: {Table: "filter", Chain: "OUTPUT", ChainEntry: true},
12: {Table: "nat", Chain: "PREROUTING", ChainEntry: true},
13: {Table: "nat", Chain: "POSTROUTING", ChainEntry: true},
14: {Table: "nat", Chain: "OUTPUT", ChainEntry: true},
}
func TestExtendIptablesPolicy(t *testing.T) {
tables := []struct {
policy []string
traceID int
traceFilter string
fwMark int
packetLimit int
traceRules bool
nflogGroup int
expectedPolicy []string
expectedRuleMap map[int]iptablesRule
expectedMaxLength int
}{
{nil, 0, "", 0, 0, false, 22, nil, map[int]iptablesRule{}, 0},
{testPolicyNoTrace, 3, "-s 1.2.3.4", 0, 0, false, 22, testPolicyTraceID3, testRuleMap, 11},
{testPolicyTraceID3, 4, "-s 1.2.3.4", 0, 0, false, 22, testPolicyTraceID3and4, testRuleMap, 11},
{testPolicyNoTrace, 5, "-s 1.2.3.4", 0x124, 60, false, 22, testPolicyTraceID5withMark, testRuleMap, 11},
{testPolicyNoTraceRules, 6, "-s 1.2.3.4", 0x124, 60, true, 22, testPolicyTraceID6withMarkAndRules, testRuleMapWithRules, 11},
}
for _, table := range tables {
gotPolicy, gotRuleMap, gotMaxLength := extendIptablesPolicy(table.policy, table.traceID, table.traceFilter, table.fwMark, table.packetLimit, table.traceRules, table.nflogGroup)
if !cmp.Equal(gotPolicy, table.expectedPolicy) {
t.Errorf("clearIptablesPolicy returned policy was incorrect, got: '%s', expected: '%s'", gotPolicy, table.expectedPolicy)
}
if !cmp.Equal(gotRuleMap, table.expectedRuleMap) {
t.Errorf("clearIptablesPolicy returned rule map was incorrect, got: '%v', expected: '%v'", gotRuleMap, table.expectedRuleMap)
}
if gotMaxLength != table.expectedMaxLength {
t.Errorf("clearIptablesPolicy returned max length was incorrect, got: '%d', expected: '%d'", gotMaxLength, table.expectedMaxLength)
}
}
}
func TestResolveFilter(t *testing.T) {
cases := []struct {
originalRule string
fwMark int
resolvedFilter []string
ok bool
}{
{"-j ACCEPT", 5, []string{"-m mark --mark 0x5/0x5"}, true},
{"-g OUTPUT", 5, []string{"-m mark --mark 0x5/0x5"}, true},
{"-s 1.1.1.1 -j ACCEPT", 5, []string{"-s 1.1.1.1", "-m mark --mark 0x5/0x5"}, true},
{"-m mark --mark 1 -j ACCEPT", 5, []string{}, false},
{"-m mark --mark 1/1 -j ACCEPT", 5, []string{}, false},
{"-m mark --mark 0x2/0x2 -j ACCEPT", 5, []string{"-m mark --mark 0x7/0x7"}, true},
{"-m mark --mark 0x4/0x5 -j ACCEPT", 5, []string{}, false},
{"-m mark --mark 0x1/0x3 -j ACCEPT", 5, []string{}, false},
{"-m mark --mark 0xf0/0xf0 -j ACCEPT", 5, []string{"-m mark --mark 0xf5/0xf5"}, true},
{"-m mark ! --mark 0xf0/0xf0 -j ACCEPT", 5, []string{"-m mark --mark 0x5/0x5"}, true},
{"-m mark ! --mark 0xf1/0xf1 -j ACCEPT", 5, []string{}, false},
}
for id, c := range cases {
filter, ok := resolveRuleFilterAndMergeMark(c.originalRule, c.fwMark)
if !cmp.Equal(c.ok, ok) || !cmp.Equal(c.resolvedFilter, filter) {
t.Errorf("resolve #%d error, got: '%v, %v', expected: '%v, %v'",
id, filter, ok, c.resolvedFilter, c.ok)
}
}
}
func TestResolveMark(t *testing.T) {
cases := []struct {
originalFilterMark string
fwMark int
resolvedFilterMark string
ok bool
}{
{"--mark 1", 5, "", false},
{"--mark 1/1", 5, "", false},
{"--mark 0x1/0x1", 5, "", false},
{"--mark 0x2/0x2", 5, "--mark 0x7/0x7", true},
{"--mark 0x1/0x3", 5, "", false},
{"--mark 0xf0/0xf0", 5, "--mark 0xf5/0xf5", true},
{"! --mark 0xf0/0xf0", 5, "--mark 0x5/0x5", true},
{"! --mark 0xf1/0xf1", 5, "", false},
}
for id, c := range cases {
filter, res := resolveMarkFilterAndMerge(c.originalFilterMark, c.fwMark)
if !cmp.Equal(c.ok, res) || !cmp.Equal(c.resolvedFilterMark, filter) {
t.Errorf("resolve #%d error, got: '%v, %v', expected: '%v, %v'",
id, filter, res, c.resolvedFilterMark, c.ok)
}
}
}