Change ports range in validators and test function

Current implementation for validating the port number and protocol
number accepts invalid values. For port number it accepts -1 which is
invalid, and the 0 which is used as programming technique for specifying
system-allocated (dynamic) ports. For protocol number it accepts only
one invalid value which is -1.

Proposed solution changes the range of:
- valid port numbers from <-1, 65535> to <1, 65535>
- valid protocol numbers from <-1, 255> to <0, 255>

Co-Authored-By: Wlodzimierz Borkowski <wlodek.borkowski@gmail.com>
Co-Authored-By: Kamil Rykowski <kamil.rykowski@intel.com>

Closes-Bug: 1329571
Closes-Bug: 1431367
Change-Id: I2dc83b611deaa0cc91d586dad1c01d8de91d1957
This commit is contained in:
Wlodzimierz Borkowski
2014-08-24 15:48:18 +02:00
committed by Kamil Rykowski
parent 0fb9a982e7
commit dd9fb91a31
2 changed files with 12 additions and 13 deletions

View File

@@ -195,8 +195,8 @@ class ValidatorsTests(test.TestCase):
self.assertRaises(ValidationError, ip.validate, cidr)
def test_port_validator(self):
VALID_PORTS = (-1, 65535)
INVALID_PORTS = (-2, 65536)
VALID_PORTS = (1, 65535)
INVALID_PORTS = (-1, 0, 65536)
for port in VALID_PORTS:
self.assertIsNone(validators.validate_port_range(port))
@@ -207,8 +207,8 @@ class ValidatorsTests(test.TestCase):
port)
def test_ip_proto_validator(self):
VALID_PROTO = (-1, 255)
INVALID_PROTO = (-2, 256)
VALID_PROTO = (0, 255)
INVALID_PROTO = (-1, 256)
for proto in VALID_PROTO:
self.assertIsNone(validators.validate_ip_protocol(proto))
@@ -220,9 +220,10 @@ class ValidatorsTests(test.TestCase):
def test_port_range_validator(self):
VALID_RANGE = ('1:65535',
'-1:-1')
'1:1')
INVALID_RANGE = ('22:22:22:22',
'-1:65536')
'1:-1',
'0:65535')
test_call = validators.validate_port_or_colon_separated_port_range
for prange in VALID_RANGE:

View File

@@ -14,6 +14,8 @@
import re
from oslo_utils import netutils
from django.core.exceptions import ValidationError # noqa
from django.core import validators # noqa
from django.utils.translation import ugettext_lazy as _
@@ -22,12 +24,12 @@ from horizon import conf
def validate_port_range(port):
if port not in range(-1, 65536):
if not netutils.is_valid_port(port):
raise ValidationError(_("Not a valid port number"))
def validate_ip_protocol(ip_proto):
if ip_proto not in range(-1, 256):
if ip_proto not in range(0, 256):
raise ValidationError(_("Not a valid IP protocol number"))
@@ -45,11 +47,7 @@ def validate_port_or_colon_separated_port_range(port_range):
raise ValidationError(_("One colon allowed in port range"))
ports = port_range.split(':')
for port in ports:
try:
if int(port) not in range(-1, 65536):
raise ValidationError(_("Not a valid port number"))
except ValueError:
raise ValidationError(_("Port number must be integer"))
validate_port_range(port)
# Same as POSIX [:print:]. Accordingly, diacritics are disallowed.
PRINT_REGEX = re.compile(r'^[\x20-\x7E]*$')