Skip to content

Commit 9d87b06

Browse files
committed
nat: TestParsePort: use table-test
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 4b4d7de commit 9d87b06

File tree

1 file changed

+64
-30
lines changed

1 file changed

+64
-30
lines changed

nat/nat_test.go

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,73 @@ import (
66
)
77

88
func TestParsePort(t *testing.T) {
9-
var (
10-
p int
11-
err error
12-
)
13-
14-
p, err = ParsePort("1234")
15-
16-
if err != nil || p != 1234 {
17-
t.Fatal("Parsing '1234' did not succeed")
18-
}
19-
20-
// FIXME currently this is a valid port. I don't think it should be.
21-
// I'm leaving this test commented out until we make a decision.
22-
// - erikh
23-
24-
/*
25-
p, err = ParsePort("0123")
26-
27-
if err != nil {
28-
t.Fatal("Successfully parsed port '0123' to '123'")
29-
}
30-
*/
31-
32-
p, err = ParsePort("asdf")
33-
34-
if err == nil || p != 0 {
35-
t.Fatal("Parsing port 'asdf' succeeded")
9+
tests := []struct {
10+
doc string
11+
input string
12+
expPort int
13+
expErr string
14+
}{
15+
{
16+
doc: "invalid value",
17+
input: "asdf",
18+
expPort: 0,
19+
expErr: `strconv.ParseUint: parsing "asdf": invalid syntax`,
20+
},
21+
{
22+
doc: "empty value",
23+
input: "",
24+
expPort: 0,
25+
},
26+
{
27+
doc: "zero value",
28+
input: "0",
29+
expPort: 0,
30+
},
31+
{
32+
doc: "negative value",
33+
input: "-1",
34+
expPort: 0,
35+
expErr: `strconv.ParseUint: parsing "-1": invalid syntax`,
36+
},
37+
// FIXME currently this is a valid port. I don't think it should be.
38+
// I'm leaving this test commented out until we make a decision.
39+
// - erikh
40+
// {
41+
// doc: "octal value",
42+
// port: "0123",
43+
// expPort: 0,
44+
// expErr: `some error message`,
45+
// },
46+
{
47+
doc: "max value",
48+
input: "65535",
49+
expPort: 65535,
50+
},
51+
{
52+
doc: "value out of range",
53+
input: "65536",
54+
expPort: 0,
55+
expErr: `strconv.ParseUint: parsing "65536": value out of range`,
56+
},
3657
}
3758

38-
p, err = ParsePort("1asdf")
59+
for _, tc := range tests {
60+
t.Run(tc.doc, func(t *testing.T) {
61+
port, err := ParsePort(tc.input)
62+
if tc.expErr != "" {
63+
if err == nil || err.Error() != tc.expErr {
64+
t.Errorf("expected error '%s', got '%v'", tc.expErr, err.Error())
65+
}
66+
} else {
67+
if err != nil {
68+
t.Error(err)
69+
}
70+
}
71+
if port != tc.expPort {
72+
t.Errorf("expected port %d, got %d", tc.expPort, port)
73+
}
3974

40-
if err == nil || p != 0 {
41-
t.Fatal("Parsing port '1asdf' succeeded")
75+
})
4276
}
4377
}
4478

0 commit comments

Comments
 (0)