48 lines
1.0 KiB
Go
48 lines
1.0 KiB
Go
package bt
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestParsePorts(t *testing.T) {
|
|
tests := []struct {
|
|
spec string
|
|
want []int
|
|
}{
|
|
// port spec examples.
|
|
{"6881,6885", []int{6881, 6885}},
|
|
{"6881-6999", rangeInts(6881, 6999)},
|
|
{"7000-7001,8000", []int{7000, 7001, 8000}},
|
|
{"6881-6889,6999", append(rangeInts(6881, 6889), 6999)},
|
|
// single bare port.
|
|
{"6881", []int{6881}},
|
|
// whitespace and empty fields are tolerated.
|
|
{" 6881 , 6885 ", []int{6881, 6885}},
|
|
{"6881,,6885", []int{6881, 6885}},
|
|
// empty / all-bad specs yield nil.
|
|
{"", nil},
|
|
{",", nil},
|
|
{"abc", nil},
|
|
// out-of-range ports are dropped.
|
|
{"0", nil},
|
|
{"70000", nil},
|
|
// a range that overruns 65535 keeps only the valid part.
|
|
{"65534-65540", []int{65534, 65535}},
|
|
}
|
|
for _, tt := range tests {
|
|
got := ParsePorts(tt.spec)
|
|
if !reflect.DeepEqual(got, tt.want) {
|
|
t.Errorf("ParsePorts(%q) = %v, want %v", tt.spec, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func rangeInts(a, b int) []int {
|
|
out := make([]int, 0, b-a+1)
|
|
for i := a; i <= b; i++ {
|
|
out = append(out, i)
|
|
}
|
|
return out
|
|
}
|