From 4c49003bb8aa08bde8d8d760d7435d6c48609709 Mon Sep 17 00:00:00 2001 From: Hunt Xu Date: Fri, 15 Dec 2017 16:28:34 +0800 Subject: [PATCH] Fix port comparison in port range validation Ports are compared as integers. Change-Id: Icfe0c101f5095a779580ea3794d2e7c939b12af5 Closes-Bug: #1738371 --- neutron_lib/api/validators/__init__.py | 2 +- neutron_lib/tests/unit/api/validators/test_validators.py | 8 ++++++-- .../port-range-compared-as-int-4d07fe030206f818.yaml | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/port-range-compared-as-int-4d07fe030206f818.yaml diff --git a/neutron_lib/api/validators/__init__.py b/neutron_lib/api/validators/__init__.py index 01c4a9f8d..72e03ecf9 100644 --- a/neutron_lib/api/validators/__init__.py +++ b/neutron_lib/api/validators/__init__.py @@ -950,7 +950,7 @@ def validate_port_range_or_none(data, valid_values=None): msg = _("Invalid port: %s.") % p LOG.debug(msg) return msg - if len(ports) > 1 and ports[0] > ports[1]: + if len(ports) > 1 and int(ports[0]) > int(ports[1]): msg = _("First port in a port range must be lower than the second " "port.") LOG.debug(msg) diff --git a/neutron_lib/tests/unit/api/validators/test_validators.py b/neutron_lib/tests/unit/api/validators/test_validators.py index 1b7fd7fa2..08e53751d 100644 --- a/neutron_lib/tests/unit/api/validators/test_validators.py +++ b/neutron_lib/tests/unit/api/validators/test_validators.py @@ -1125,7 +1125,9 @@ class TestPortRangeValidation(base.BaseTestCase): self.assertIsNone(result) def test_valid_range(self): - result = validators.validate_port_range_or_none("80:8888") + # NOTE(huntxu): This case would fail when ports are compared as + # strings, since '9' > '1111'. + result = validators.validate_port_range_or_none("9:1111") self.assertIsNone(result) def test_port_too_high(self): @@ -1145,7 +1147,9 @@ class TestPortRangeValidation(base.BaseTestCase): self.assertEqual(u"Invalid port: -1.", result) def test_range_wrong_way(self): - result = validators.validate_port_range_or_none("8888:80") + # NOTE(huntxu): This case would fail when ports are compared as + # strings, since '1111' < '9'. + result = validators.validate_port_range_or_none("1111:9") self.assertEqual(u"First port in a port range must be lower than the " "second port.", result) diff --git a/releasenotes/notes/port-range-compared-as-int-4d07fe030206f818.yaml b/releasenotes/notes/port-range-compared-as-int-4d07fe030206f818.yaml new file mode 100644 index 000000000..f475290bb --- /dev/null +++ b/releasenotes/notes/port-range-compared-as-int-4d07fe030206f818.yaml @@ -0,0 +1,5 @@ +--- +fixes: + - Bug `1738371 `_ is fixed + by comparing min port and max port in port range specification as integers + instead of strings, during port range validation.