diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-21 18:54:48 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-02-21 18:54:48 +0100 |
commit | dc322326e26d360cfc6ca6921f48265b1f7a4453 (patch) | |
tree | 5cbc2fa0af2e185b96a8a13ce0691768cc12f3f3 /plumbing/transport/client | |
parent | e26a605c93d8085dfff8e440838fd3c43b63cff6 (diff) | |
download | go-git-dc322326e26d360cfc6ca6921f48265b1f7a4453.tar.gz |
plumbing/transport: client avoid panics on nil protocol
Diffstat (limited to 'plumbing/transport/client')
-rw-r--r-- | plumbing/transport/client/client.go | 9 | ||||
-rw-r--r-- | plumbing/transport/client/client_test.go | 33 |
2 files changed, 26 insertions, 16 deletions
diff --git a/plumbing/transport/client/client.go b/plumbing/transport/client/client.go index f749875..ea1e034 100644 --- a/plumbing/transport/client/client.go +++ b/plumbing/transport/client/client.go @@ -23,6 +23,11 @@ var Protocols = map[string]transport.Transport{ // InstallProtocol adds or modifies an existing protocol. func InstallProtocol(scheme string, c transport.Transport) { + if c == nil { + delete(Protocols, scheme) + return + } + Protocols[scheme] = c } @@ -35,5 +40,9 @@ func NewClient(endpoint transport.Endpoint) (transport.Transport, error) { return nil, fmt.Errorf("unsupported scheme %q", endpoint.Scheme) } + if f == nil { + return nil, fmt.Errorf("malformed client for scheme %q, client is defined as nil", endpoint.Scheme) + } + return f, nil } diff --git a/plumbing/transport/client/client_test.go b/plumbing/transport/client/client_test.go index a3715d2..0377d34 100644 --- a/plumbing/transport/client/client_test.go +++ b/plumbing/transport/client/client_test.go @@ -16,22 +16,6 @@ type ClientSuite struct{} var _ = Suite(&ClientSuite{}) -func (s *ClientSuite) TestNewClientHTTP(c *C) { - e, err := transport.NewEndpoint("http://github.com/src-d/go-git") - c.Assert(err, IsNil) - - output, err := NewClient(e) - c.Assert(err, IsNil) - c.Assert(typeAsString(output), Equals, "*http.client") - - e, err = transport.NewEndpoint("https://github.com/src-d/go-git") - c.Assert(err, IsNil) - - output, err = NewClient(e) - c.Assert(err, IsNil) - c.Assert(typeAsString(output), Equals, "*http.client") -} - func (s *ClientSuite) TestNewClientSSH(c *C) { e, err := transport.NewEndpoint("ssh://github.com/src-d/go-git") c.Assert(err, IsNil) @@ -49,11 +33,28 @@ func (s *ClientSuite) TestNewClientUnknown(c *C) { c.Assert(err, NotNil) } +func (s *ClientSuite) TestNewClientNil(c *C) { + Protocols["newscheme"] = nil + e, err := transport.NewEndpoint("newscheme://github.com/src-d/go-git") + c.Assert(err, IsNil) + + _, err = NewClient(e) + c.Assert(err, NotNil) +} + func (s *ClientSuite) TestInstallProtocol(c *C) { InstallProtocol("newscheme", &dummyClient{}) c.Assert(Protocols["newscheme"], NotNil) } +func (s *ClientSuite) TestInstallProtocolNilValue(c *C) { + InstallProtocol("newscheme", &dummyClient{}) + InstallProtocol("newscheme", nil) + + _, ok := Protocols["newscheme"] + c.Assert(ok, Equals, false) +} + type dummyClient struct { *http.Client } |