Browse code

Check whether net.ParseIP returned nil or not

This is fix of #8102

Signed-off-by: Oh Jinkyun <tintypemolly@gmail.com>

Oh Jinkyun authored on 2014/09/19 21:31:57
Showing 2 changed files
... ...
@@ -390,6 +390,9 @@ func AllocatePort(job *engine.Job) engine.Status {
390 390
 
391 391
 	if hostIP != "" {
392 392
 		ip = net.ParseIP(hostIP)
393
+		if ip == nil {
394
+			return job.Errorf("Bad parameter: invalid host ip %s", hostIP)
395
+		}
393 396
 	}
394 397
 
395 398
 	// host ip, proto, and host port
... ...
@@ -39,6 +39,17 @@ func newPortAllocationJob(eng *engine.Engine, port int) (job *engine.Job) {
39 39
 	return
40 40
 }
41 41
 
42
+func newPortAllocationJobWithInvalidHostIP(eng *engine.Engine, port int) (job *engine.Job) {
43
+	strPort := strconv.Itoa(port)
44
+
45
+	job = eng.Job("allocate_port", "container_id")
46
+	job.Setenv("HostIP", "localhost")
47
+	job.Setenv("HostPort", strPort)
48
+	job.Setenv("Proto", "tcp")
49
+	job.Setenv("ContainerPort", strPort)
50
+	return
51
+}
52
+
42 53
 func TestAllocatePortDetection(t *testing.T) {
43 54
 	eng := engine.New()
44 55
 	eng.Logging = false
... ...
@@ -66,3 +77,28 @@ func TestAllocatePortDetection(t *testing.T) {
66 66
 		t.Fatal("Duplicate port allocation granted by AllocatePort")
67 67
 	}
68 68
 }
69
+
70
+func TestHostnameFormatChecking(t *testing.T) {
71
+	eng := engine.New()
72
+	eng.Logging = false
73
+
74
+	freePort := findFreePort(t)
75
+
76
+	// Init driver
77
+	job := eng.Job("initdriver")
78
+	if res := InitDriver(job); res != engine.StatusOK {
79
+		t.Fatal("Failed to initialize network driver")
80
+	}
81
+
82
+	// Allocate interface
83
+	job = eng.Job("allocate_interface", "container_id")
84
+	if res := Allocate(job); res != engine.StatusOK {
85
+		t.Fatal("Failed to allocate network interface")
86
+	}
87
+
88
+	// Allocate port with invalid HostIP, expect failure with Bad Request http status
89
+	job = newPortAllocationJobWithInvalidHostIP(eng, freePort)
90
+	if res := AllocatePort(job); res == engine.StatusOK {
91
+		t.Fatal("Failed to check invalid HostIP")
92
+	}
93
+}