From dc322326e26d360cfc6ca6921f48265b1f7a4453 Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Tue, 21 Feb 2017 18:54:48 +0100 Subject: plumbing/transport: client avoid panics on nil protocol --- plumbing/transport/client/client.go | 9 +++++++++ plumbing/transport/client/client_test.go | 33 ++++++++++++++++---------------- 2 files changed, 26 insertions(+), 16 deletions(-) (limited to 'plumbing') 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 } -- cgit