-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug for parsing IPv6 addresses (#17)
IPv6 addresses contain colons ':' and since sshtunnel uses strings.Split(addr, ":"), to spit host and port, it ends up resulting in faulty splits. Using net.SplitHostPort fixes this since it handles IPv6 addresses correctly. If the user did not supply a port, then endpoint.Host is left as is. This includes a partially breaking change where NewEdnpoint and NewSSHTunnel will now return an error. If you are not using IPv6 it is safe to ignore this error.
- Loading branch information
Showing
3 changed files
with
103 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package sshtunnel_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/elliotchance/sshtunnel" | ||
) | ||
|
||
func TestCreateEndpoint(t *testing.T) { | ||
// these are test cases for which we expect no error to occur when | ||
// constructing endpoints i.e. they should be correct | ||
testCases := []struct { | ||
input string | ||
expectedEndpoint *sshtunnel.Endpoint | ||
}{ | ||
{ | ||
"localhost:9000", | ||
&sshtunnel.Endpoint{ | ||
Host: "localhost", | ||
Port: 9000, | ||
User: "", | ||
}, | ||
}, | ||
{ | ||
"[email protected]", | ||
&sshtunnel.Endpoint{ | ||
Host: "jumpbox.us-east-1.mydomain.com", | ||
Port: 0, | ||
User: "ec2-user", | ||
}, | ||
}, | ||
{ | ||
"dqrsdfdssdfx.us-east-1.redshift.amazonaws.com:5439", | ||
&sshtunnel.Endpoint{ | ||
Host: "dqrsdfdssdfx.us-east-1.redshift.amazonaws.com", | ||
Port: 5439, | ||
User: "", | ||
}, | ||
}, | ||
{ | ||
"[email protected]:22", // IPv4 address | ||
&sshtunnel.Endpoint{ | ||
Host: "1.2.3.4", | ||
Port: 22, | ||
User: "admin", | ||
}, | ||
}, | ||
{ | ||
"admin@[2001:db8:1::ab9:C0A8:102]:22", // IPv6 address | ||
&sshtunnel.Endpoint{ | ||
Host: "2001:db8:1::ab9:C0A8:102", | ||
Port: 22, | ||
User: "admin", | ||
}, | ||
}, | ||
} | ||
for i, tc := range testCases { | ||
got, err := sshtunnel.NewEndpoint(tc.input) | ||
if err != nil { | ||
t.Errorf("unexpected error for correct input '%s': %v", | ||
tc.input, err) | ||
} | ||
if !reflect.DeepEqual(got, tc.expectedEndpoint) { | ||
t.Errorf("For test case %d, expected: %+v, got: %+v", | ||
i, *tc.expectedEndpoint, *got) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters