diff --git a/libnetwork/controller.go b/libnetwork/controller.go index 51c5a16a01..f4b5dd781d 100644 --- a/libnetwork/controller.go +++ b/libnetwork/controller.go @@ -128,6 +128,9 @@ func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver) // NewNetwork creates a new network of the specified network type. The options // are network specific and modeled in a generic way. func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) { + if name == "" { + return nil, ErrInvalidNetworkName + } // Check if a driver for the specified network type is available c.Lock() d, ok := c.drivers[networkType] diff --git a/libnetwork/error.go b/libnetwork/error.go index 601f0afefe..491c1bff39 100644 --- a/libnetwork/error.go +++ b/libnetwork/error.go @@ -18,6 +18,12 @@ var ( // ErrNoContainer is returned when the endpoint has no container // attached to it. ErrNoContainer = errors.New("no container attached to the endpoint") + // ErrInvalidEndpointName is returned if an invalid endpoint name + // is passed when creating an endpoint + ErrInvalidEndpointName = errors.New("invalid endpoint name") + // ErrInvalidNetworkName is returned if an invalid network name + // is passed when creating a network + ErrInvalidNetworkName = errors.New("invalid network name") ) // NetworkTypeError type is returned when the network type string is not diff --git a/libnetwork/libnetwork_test.go b/libnetwork/libnetwork_test.go index 2df1444c1d..f5edbd8bb6 100644 --- a/libnetwork/libnetwork_test.go +++ b/libnetwork/libnetwork_test.go @@ -276,8 +276,16 @@ func TestDuplicateNetwork(t *testing.T) { func TestNetworkName(t *testing.T) { defer netutils.SetupTestNetNS(t)() - networkName := "testnetwork" + _, err := createTestNetwork(bridgeNetType, "", options.Generic{}, options.Generic{}) + if err == nil { + t.Fatal("Expected to fail. But instead succeeded") + } + if err != libnetwork.ErrInvalidNetworkName { + t.Fatal("Expected to fail with ErrInvalidNetworkName error") + } + + networkName := "testnetwork" n, err := createTestNetwork(bridgeNetType, networkName, options.Generic{}, options.Generic{}) if err != nil { t.Fatal(err) @@ -392,6 +400,14 @@ func TestUnknownEndpoint(t *testing.T) { t.Fatal(err) } + _, err = network.CreateEndpoint("") + if err == nil { + t.Fatal("Expected to fail. But instead succeeded") + } + if err != libnetwork.ErrInvalidEndpointName { + t.Fatal("Expected to fail with ErrInvalidEndpointName error") + } + ep, err := network.CreateEndpoint("testep") if err != nil { t.Fatal(err) diff --git a/libnetwork/network.go b/libnetwork/network.go index f1830e90d5..df89b8a3ae 100644 --- a/libnetwork/network.go +++ b/libnetwork/network.go @@ -132,6 +132,9 @@ func (n *network) Delete() error { } func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) { + if name == "" { + return nil, ErrInvalidEndpointName + } ep := &endpoint{name: name, generic: make(map[string]interface{})} ep.id = types.UUID(stringid.GenerateRandomID()) ep.network = n