diff --git a/horizon/test/tests/utils.py b/horizon/test/tests/utils.py index 7077bfcddc..d717109124 100644 --- a/horizon/test/tests/utils.py +++ b/horizon/test/tests/utils.py @@ -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') diff --git a/horizon/utils/validators.py b/horizon/utils/validators.py index d2103bf0c8..e8dd3f1a5c 100644 --- a/horizon/utils/validators.py +++ b/horizon/utils/validators.py @@ -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]*$') diff --git a/openstack_dashboard/dashboards/project/access_and_security/security_groups/forms.py b/openstack_dashboard/dashboards/project/access_and_security/security_groups/forms.py index f38227c8f9..4437dd0399 100644 --- a/openstack_dashboard/dashboards/project/access_and_security/security_groups/forms.py +++ b/openstack_dashboard/dashboards/project/access_and_security/security_groups/forms.py @@ -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')), diff --git a/openstack_dashboard/dashboards/project/access_and_security/security_groups/tests.py b/openstack_dashboard/dashboards/project/access_and_security/security_groups/tests.py index 606171301f..d2386c26bf 100644 --- a/openstack_dashboard/dashboards/project/access_and_security/security_groups/tests.py +++ b/openstack_dashboard/dashboards/project/access_and_security/security_groups/tests.py @@ -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,