Browse code

NewNetwork and CreateEndpoint to validate resource name

Signed-off-by: Alessandro Boch <aboch@docker.com>

Alessandro Boch authored on 2015/05/08 11:59:06
Showing 4 changed files
... ...
@@ -128,6 +128,9 @@ func (c *controller) RegisterDriver(networkType string, driver driverapi.Driver)
128 128
 // NewNetwork creates a new network of the specified network type. The options
129 129
 // are network specific and modeled in a generic way.
130 130
 func (c *controller) NewNetwork(networkType, name string, options ...NetworkOption) (Network, error) {
131
+	if name == "" {
132
+		return nil, ErrInvalidNetworkName
133
+	}
131 134
 	// Check if a driver for the specified network type is available
132 135
 	c.Lock()
133 136
 	d, ok := c.drivers[networkType]
... ...
@@ -18,6 +18,12 @@ var (
18 18
 	// ErrNoContainer is returned when the endpoint has no container
19 19
 	// attached to it.
20 20
 	ErrNoContainer = errors.New("no container attached to the endpoint")
21
+	// ErrInvalidEndpointName is returned if an invalid endpoint name
22
+	// is passed when creating an endpoint
23
+	ErrInvalidEndpointName = errors.New("invalid endpoint name")
24
+	// ErrInvalidNetworkName is returned if an invalid network name
25
+	// is passed when creating a network
26
+	ErrInvalidNetworkName = errors.New("invalid network name")
21 27
 )
22 28
 
23 29
 // NetworkTypeError type is returned when the network type string is not
... ...
@@ -276,8 +276,16 @@ func TestDuplicateNetwork(t *testing.T) {
276 276
 
277 277
 func TestNetworkName(t *testing.T) {
278 278
 	defer netutils.SetupTestNetNS(t)()
279
-	networkName := "testnetwork"
280 279
 
280
+	_, err := createTestNetwork(bridgeNetType, "", options.Generic{}, options.Generic{})
281
+	if err == nil {
282
+		t.Fatal("Expected to fail. But instead succeeded")
283
+	}
284
+	if err != libnetwork.ErrInvalidNetworkName {
285
+		t.Fatal("Expected to fail with ErrInvalidNetworkName error")
286
+	}
287
+
288
+	networkName := "testnetwork"
281 289
 	n, err := createTestNetwork(bridgeNetType, networkName, options.Generic{}, options.Generic{})
282 290
 	if err != nil {
283 291
 		t.Fatal(err)
... ...
@@ -392,6 +400,14 @@ func TestUnknownEndpoint(t *testing.T) {
392 392
 		t.Fatal(err)
393 393
 	}
394 394
 
395
+	_, err = network.CreateEndpoint("")
396
+	if err == nil {
397
+		t.Fatal("Expected to fail. But instead succeeded")
398
+	}
399
+	if err != libnetwork.ErrInvalidEndpointName {
400
+		t.Fatal("Expected to fail with ErrInvalidEndpointName error")
401
+	}
402
+
395 403
 	ep, err := network.CreateEndpoint("testep")
396 404
 	if err != nil {
397 405
 		t.Fatal(err)
... ...
@@ -132,6 +132,9 @@ func (n *network) Delete() error {
132 132
 }
133 133
 
134 134
 func (n *network) CreateEndpoint(name string, options ...EndpointOption) (Endpoint, error) {
135
+	if name == "" {
136
+		return nil, ErrInvalidEndpointName
137
+	}
135 138
 	ep := &endpoint{name: name, generic: make(map[string]interface{})}
136 139
 	ep.id = types.UUID(stringid.GenerateRandomID())
137 140
 	ep.network = n