From 93bc1b72e3cd566e6447ad7a1f832474aad5dfcc Mon Sep 17 00:00:00 2001 From: Mohammed Al Sahaf Date: Tue, 12 Nov 2019 01:33:38 +0300 Subject: core: Use port ranges to avoid OOM with bad inputs (#2859) * fix OOM issue caught by fuzzing * use ParsedAddress as the struct name for the result of ParseNetworkAddress * simplify code using the ParsedAddress type * minor cleanups --- listeners_test.go | 105 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 39 deletions(-) (limited to 'listeners_test.go') diff --git a/listeners_test.go b/listeners_test.go index bdddf32..076b365 100644 --- a/listeners_test.go +++ b/listeners_test.go @@ -152,74 +152,101 @@ func TestJoinNetworkAddress(t *testing.T) { func TestParseNetworkAddress(t *testing.T) { for i, tc := range []struct { - input string - expectNetwork string - expectAddrs []string - expectErr bool + input string + expectAddr ParsedAddress + expectErr bool }{ { - input: "", - expectNetwork: "tcp", - expectErr: true, + input: "", + expectErr: true, }, { - input: ":", - expectNetwork: "tcp", - expectErr: true, + input: ":", + expectErr: true, }, { - input: ":1234", - expectNetwork: "tcp", - expectAddrs: []string{":1234"}, + input: ":1234", + expectAddr: ParsedAddress{ + Network: "tcp", + Host: "", + StartPort: 1234, + EndPort: 1234, + }, }, { - input: "tcp/:1234", - expectNetwork: "tcp", - expectAddrs: []string{":1234"}, + input: "tcp/:1234", + expectAddr: ParsedAddress{ + Network: "tcp", + Host: "", + StartPort: 1234, + EndPort: 1234, + }, }, { - input: "tcp6/:1234", - expectNetwork: "tcp6", - expectAddrs: []string{":1234"}, + input: "tcp6/:1234", + expectAddr: ParsedAddress{ + Network: "tcp6", + Host: "", + StartPort: 1234, + EndPort: 1234, + }, }, { - input: "tcp4/localhost:1234", - expectNetwork: "tcp4", - expectAddrs: []string{"localhost:1234"}, + input: "tcp4/localhost:1234", + expectAddr: ParsedAddress{ + Network: "tcp4", + Host: "localhost", + StartPort: 1234, + EndPort: 1234, + }, }, { - input: "unix//foo/bar", - expectNetwork: "unix", - expectAddrs: []string{"/foo/bar"}, + input: "unix//foo/bar", + expectAddr: ParsedAddress{ + Network: "unix", + Host: "/foo/bar", + }, }, { - input: "localhost:1234-1234", - expectNetwork: "tcp", - expectAddrs: []string{"localhost:1234"}, + input: "localhost:1234-1234", + expectAddr: ParsedAddress{ + Network: "tcp", + Host: "localhost", + StartPort: 1234, + EndPort: 1234, + }, }, { - input: "localhost:2-1", - expectNetwork: "tcp", - expectErr: true, + input: "localhost:2-1", + expectErr: true, }, { - input: "localhost:0", - expectNetwork: "tcp", - expectAddrs: []string{"localhost:0"}, + input: "localhost:0", + expectAddr: ParsedAddress{ + Network: "tcp", + Host: "localhost", + StartPort: 0, + EndPort: 0, + }, + }, + { + input: "localhost:1-999999999999", + expectErr: true, }, } { - actualNetwork, actualAddrs, err := ParseNetworkAddress(tc.input) + actualAddr, err := ParseNetworkAddress(tc.input) if tc.expectErr && err == nil { t.Errorf("Test %d: Expected error but got: %v", i, err) } if !tc.expectErr && err != nil { t.Errorf("Test %d: Expected no error but got: %v", i, err) } - if actualNetwork != tc.expectNetwork { - t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork) + + if actualAddr.Network != tc.expectAddr.Network { + t.Errorf("Test %d: Expected network '%v' but got '%v'", i, tc.expectAddr, actualAddr) } - if !reflect.DeepEqual(tc.expectAddrs, actualAddrs) { - t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddrs, actualAddrs) + if !reflect.DeepEqual(tc.expectAddr, actualAddr) { + t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddr, actualAddr) } } } -- cgit v1.2.3