Skip to content

Commit

Permalink
Test IPv4 and IPv6 formats using regexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Renelvon committed Jun 4, 2015
1 parent 909d84d commit c4ff02f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
28 changes: 26 additions & 2 deletions tests/test_packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,19 @@ def test_from_validated_bad_json_with_bad_sequence_number(self):
self._assert_json_fails_validation(bad_json)

def test_from_validated_bad_json_with_bad_dest_ip(self):
pass
bad_json = dict(self.json_dict)

bad_json['dest_ip'] = 42
self._assert_json_fails_validation(bad_json)

bad_json['dest_ip'] = '127.0'
self._assert_json_fails_validation(bad_json)

bad_json['dest_ip'] = 'FE80:0000:0000::z:B3FF:FE1E:8329'
self._assert_json_fails_validation(bad_json)

del bad_json['dest_ip']
self._assert_json_fails_validation(bad_json)

def test_from_validated_bad_json_with_bad_dest_port(self):
bad_json = dict(self.json_dict)
Expand All @@ -170,7 +182,19 @@ def test_from_validated_bad_json_with_bad_dest_port(self):
self._assert_json_fails_validation(bad_json)

def test_from_validated_bad_json_with_bad_source_ip(self):
pass
bad_json = dict(self.json_dict)

bad_json['source_ip'] = 42
self._assert_json_fails_validation(bad_json)

bad_json['source_ip'] = '127.0'
self._assert_json_fails_validation(bad_json)

bad_json['source_ip'] = 'FE80:0000:0000:z::B3FF:FE1E:8329'
self._assert_json_fails_validation(bad_json)

del bad_json['source_ip']
self._assert_json_fails_validation(bad_json)

def test_from_validated_bad_json_with_bad_source_port(self):
bad_json = dict(self.json_dict)
Expand Down
14 changes: 10 additions & 4 deletions txrudp/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

import jsonschema

# NOTE: jsonschema's `format` specifier was tested and found
# lacking. Hence the use of regexes from Regular Expressions Cookbook.
# For now, only standard (non-compressed) IPv6 addresses are
# supported. This might change in the future.
_IPV4_REGEX = '^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
_IPV6_REGEX = '^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$'

RUDP_PACKET_JSON_SCHEMA = {
'$schema': 'http://json-schema.org/schema#',
Expand All @@ -22,8 +28,8 @@
},
'dest_ip': {
'anyOf': [
{'type': 'string', 'format': 'ipv4'},
{'type': 'string', 'format': 'ipv6'},
{'type': 'string', 'pattern': _IPV4_REGEX},
{'type': 'string', 'pattern': _IPV6_REGEX}
]
},
'dest_port': {
Expand All @@ -33,8 +39,8 @@
},
'source_ip': {
'anyOf': [
{'type': 'string', 'format': 'ipv4'},
{'type': 'string', 'format': 'ipv6'},
{'type': 'string', 'pattern': _IPV4_REGEX},
{'type': 'string', 'pattern': _IPV6_REGEX}
]
},
'source_port': {
Expand Down

0 comments on commit c4ff02f

Please sign in to comment.