Skip to content

Commit 62c7de1

Browse files
committed
nat: rewrite ParsePortRange tests to use subtests
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 16d13be commit 62c7de1

1 file changed

Lines changed: 114 additions & 46 deletions

File tree

nat/parse_test.go

Lines changed: 114 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,122 @@
11
package nat
22

3-
import (
4-
"strings"
5-
"testing"
6-
)
3+
import "testing"
74

85
func TestParsePortRange(t *testing.T) {
9-
if start, end, err := ParsePortRange("8000-8080"); err != nil || start != 8000 || end != 8080 {
10-
t.Fatalf("Error: %s or Expecting {start,end} values {8000,8080} but found {%d,%d}.", err, start, end)
11-
}
12-
}
13-
14-
func TestParsePortRangeEmpty(t *testing.T) {
15-
if _, _, err := ParsePortRange(""); err == nil || err.Error() != "empty string specified for ports" {
16-
t.Fatalf("Expected error 'empty string specified for ports', got %v", err)
17-
}
18-
}
19-
20-
func TestParsePortRangeWithNoRange(t *testing.T) {
21-
start, end, err := ParsePortRange("8080")
22-
if err != nil {
23-
t.Fatal(err)
24-
}
25-
if start != 8080 || end != 8080 {
26-
t.Fatalf("Expected start and end to be the same and equal to 8080, but were %v and %v", start, end)
27-
}
28-
}
29-
30-
func TestParsePortRangeIncorrectRange(t *testing.T) {
31-
if _, _, err := ParsePortRange("9000-8080"); err == nil || !strings.Contains(err.Error(), "invalid range specified for port") {
32-
t.Fatalf("Expecting error 'invalid range specified for port' but received %s.", err)
33-
}
34-
}
35-
36-
func TestParsePortRangeIncorrectEndRange(t *testing.T) {
37-
if _, _, err := ParsePortRange("8000-a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
38-
t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
39-
}
40-
41-
if _, _, err := ParsePortRange("8000-30a"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
42-
t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
43-
}
44-
}
45-
46-
func TestParsePortRangeIncorrectStartRange(t *testing.T) {
47-
if _, _, err := ParsePortRange("a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
48-
t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
6+
tests := []struct {
7+
doc string
8+
input string
9+
expBegin uint64
10+
expEnd uint64
11+
expErr string
12+
}{
13+
{
14+
doc: "empty value",
15+
expErr: `empty string specified for ports`,
16+
},
17+
{
18+
doc: "single port",
19+
input: "1234",
20+
expBegin: 1234,
21+
expEnd: 1234,
22+
},
23+
{
24+
doc: "single port range",
25+
input: "1234-1234",
26+
expBegin: 1234,
27+
expEnd: 1234,
28+
},
29+
{
30+
doc: "two port range",
31+
input: "1234-1235",
32+
expBegin: 1234,
33+
expEnd: 1235,
34+
},
35+
{
36+
doc: "large range",
37+
input: "8000-9000",
38+
expBegin: 8000,
39+
expEnd: 9000,
40+
},
41+
{
42+
doc: "zero port",
43+
input: "0",
44+
},
45+
{
46+
doc: "zero range",
47+
input: "0-0",
48+
},
49+
// invalid cases
50+
{
51+
doc: "non-numeric port",
52+
input: "asdf",
53+
expErr: `strconv.ParseUint: parsing "asdf": invalid syntax`,
54+
},
55+
{
56+
doc: "reversed range",
57+
input: "9000-8000",
58+
expErr: `invalid range specified for port: 9000-8000`,
59+
},
60+
{
61+
doc: "range missing end",
62+
input: "9000-",
63+
expErr: `strconv.ParseUint: parsing "": invalid syntax`,
64+
},
65+
{
66+
doc: "range missing start",
67+
input: "-8000",
68+
expErr: `strconv.ParseUint: parsing "": invalid syntax`,
69+
},
70+
{
71+
doc: "invalid range end",
72+
input: "9000-a",
73+
expErr: `strconv.ParseUint: parsing "a": invalid syntax`,
74+
},
75+
{
76+
doc: "invalid range end port",
77+
input: "9000-8000a",
78+
expErr: `strconv.ParseUint: parsing "8000a": invalid syntax`,
79+
},
80+
{
81+
doc: "range range start",
82+
input: "a-8000",
83+
expErr: `strconv.ParseUint: parsing "a": invalid syntax`,
84+
},
85+
{
86+
doc: "range range start port",
87+
input: "9000a-8000",
88+
expErr: `strconv.ParseUint: parsing "9000a": invalid syntax`,
89+
},
90+
{
91+
doc: "range with trailing hyphen",
92+
input: "-8000-",
93+
expErr: `strconv.ParseUint: parsing "": invalid syntax`,
94+
},
95+
{
96+
doc: "range without ports",
97+
input: "-",
98+
expErr: `strconv.ParseUint: parsing "": invalid syntax`,
99+
},
49100
}
50101

51-
if _, _, err := ParsePortRange("30a-8000"); err == nil || !strings.Contains(err.Error(), "invalid syntax") {
52-
t.Fatalf("Expecting error 'invalid syntax' but received %s.", err)
102+
for _, tc := range tests {
103+
t.Run(tc.doc, func(t *testing.T) {
104+
begin, end, err := ParsePortRange(tc.input)
105+
if tc.expErr == "" {
106+
if err != nil {
107+
t.Error(err)
108+
}
109+
} else {
110+
if err == nil || err.Error() != tc.expErr {
111+
t.Errorf("expected error '%s', got '%v'", tc.expErr, err)
112+
}
113+
}
114+
if begin != tc.expBegin {
115+
t.Errorf("expected begin %d, got %d", tc.expBegin, begin)
116+
}
117+
if end != tc.expEnd {
118+
t.Errorf("expected end %d, got %d", tc.expEnd, end)
119+
}
120+
})
53121
}
54122
}

0 commit comments

Comments
 (0)