| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package knftables |
| 18 |
|
| 19 |
import ( |
| 20 |
"net" |
| 21 |
"testing" |
| 22 |
) |
| 23 |
|
| 24 |
func TestConcat(t *testing.T) { |
| 25 |
_, cidr, _ := net.ParseCIDR("10.2.0.0/24") |
| 26 |
|
| 27 |
for _, tc := range []struct { |
| 28 |
name string |
| 29 |
values []interface{} |
| 30 |
out string |
| 31 |
}{ |
| 32 |
{ |
| 33 |
name: "empty", |
| 34 |
values: nil, |
| 35 |
out: "", |
| 36 |
}, |
| 37 |
{ |
| 38 |
name: "one string", |
| 39 |
values: []interface{}{ |
| 40 |
"foo", |
| 41 |
}, |
| 42 |
out: "foo", |
| 43 |
}, |
| 44 |
{ |
| 45 |
name: "one array", |
| 46 |
values: []interface{}{ |
| 47 |
[]string{"foo", "bar", "baz"}, |
| 48 |
}, |
| 49 |
out: "foo bar baz", |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "mixed", |
| 53 |
values: []interface{}{ |
| 54 |
"foo", []string{"bar", "baz"}, "quux", |
| 55 |
}, |
| 56 |
out: "foo bar baz quux", |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "empty array", |
| 60 |
values: []interface{}{ |
| 61 |
"foo", []string{}, "bar", "baz", |
| 62 |
}, |
| 63 |
|
| 64 |
out: "foo bar baz", |
| 65 |
}, |
| 66 |
{ |
| 67 |
name: "numbers", |
| 68 |
values: []interface{}{ |
| 69 |
1, uint16(65535), int64(-123456789), |
| 70 |
}, |
| 71 |
out: "1 65535 -123456789", |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "everything", |
| 75 |
values: []interface{}{ |
| 76 |
IPv4Family, "saddr", cidr, |
| 77 |
[]string{}, |
| 78 |
"th port", 8080, |
| 79 |
"@", "mySetName", |
| 80 |
[]string{"@", "myMapName"}, |
| 81 |
[]string{"ct", "state", "established"}, |
| 82 |
"drop", |
| 83 |
}, |
| 84 |
out: "ip saddr 10.2.0.0/24 th port 8080 @mySetName @myMapName ct state established drop", |
| 85 |
}, |
| 86 |
} { |
| 87 |
t.Run(tc.name, func(t *testing.T) { |
| 88 |
out := Concat(tc.values...) |
| 89 |
if out != tc.out { |
| 90 |
t.Errorf("expected %q got %q", tc.out, out) |
| 91 |
} |
| 92 |
}) |
| 93 |
} |
| 94 |
} |