Merge "Neutron: fix ICMP code and type validators"

This commit is contained in:
Jenkins 2016-01-05 01:36:33 +00:00 committed by Gerrit Code Review
commit c1fb06b13d
4 changed files with 22 additions and 4 deletions

View File

@ -218,6 +218,18 @@ class ValidatorsTests(test.TestCase):
validators.validate_ip_protocol,
proto)
def test_icmp_code_type(self):
VALID_VALUE = (0, 255)
INVALID_VALUE = (-1, 256)
for value in VALID_VALUE:
self.assertIsNone(validators.validate_icmp(value))
for value in INVALID_VALUE:
self.assertRaises(ValidationError,
validators.validate_icmp,
value)
def test_port_range_validator(self):
VALID_RANGE = ('1:65535',
'1:1')

View File

@ -62,6 +62,12 @@ def validate_metadata(value):
if not len(keyval) == 2 or not keyval[0]:
raise ValidationError(error_msg)
def validate_icmp(code_or_type):
if code_or_type not in range(0, 256):
raise ValidationError(_("Not a valid ICMP code or type"))
# Same as POSIX [:print:]. Accordingly, diacritics are disallowed.
PRINT_REGEX = re.compile(r'^[\x20-\x7E]*$')

View File

@ -186,7 +186,7 @@ class AddRule(forms.SelfHandlingForm):
'data-switch-on': 'rule_menu',
'data-rule_menu-icmp': _('Type')}),
validators=[
utils_validators.validate_port_range])
utils_validators.validate_icmp])
icmp_code = forms.IntegerField(label=_("Code"),
required=False,
@ -197,7 +197,7 @@ class AddRule(forms.SelfHandlingForm):
'data-switch-on': 'rule_menu',
'data-rule_menu-icmp': _('Code')}),
validators=[
utils_validators.validate_port_range])
utils_validators.validate_icmp])
remote = forms.ChoiceField(label=_('Remote'),
choices=[('cidr', _('CIDR')),

View File

@ -500,7 +500,7 @@ class SecurityGroupsViewTests(test.TestCase):
'remote': 'cidr'}
res = self.client.post(self.edit_url, formData)
self.assertNoMessages()
self.assertContains(res, "The ICMP type not in range (-1, 255)")
self.assertContains(res, "Not a valid ICMP code or type")
formData = {'method': 'AddRule',
'id': sec_group.id,
@ -512,7 +512,7 @@ class SecurityGroupsViewTests(test.TestCase):
'remote': 'cidr'}
res = self.client.post(self.edit_url, formData)
self.assertNoMessages()
self.assertContains(res, "The ICMP code not in range (-1, 255)")
self.assertContains(res, "Not a valid ICMP code or type")
formData = {'method': 'AddRule',
'id': sec_group.id,