Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # vim: set fileencoding=utf=8 :
- '''Test regular expressions for matching an Internet host name
- These regular expressions were found at
- http://stackoverflow.com/questions/1418423/the-hostname-regex
- '''
- from __future__ import unicode_literals
- import re
- regexes = [
- re.compile( # http://stackoverflow.com/q/1418423/187736
- r'^[0-9a-z]([0-9a-z\-]{0,61}[0-9a-z])?'
- r'(\.[0-9a-z](0-9a-z\-]{0,61}[0-9a-z])?)*$'
- ),
- re.compile( # http://stackoverflow.com/a/1420225/187736
- r'^(?=.{1,255}$)[0-9A-Za-z]'
- r'(?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?'
- r'(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$'
- ),
- re.compile( # http://stackoverflow.com/a/1418724/187736
- r'(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*'
- r'(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z])[.]?)'
- ),
- re.compile( # http://stackoverflow.com/a/1418724/187736
- r'(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]{0,61})?[a-zA-Z0-9])[.])*'
- r'(?:[a-zA-Z][-a-zA-Z0-9]{0,61}[a-zA-Z0-9]|[a-zA-Z])[.]?)'
- ),
- # This one doesn't even compile
- #re.compile( # http://stackoverflow.com/a/18494710/187736
- # r'^([a-zA-Z0-9](?:(?:[a-zA-Z0-9-]*|(?<!-)\.(?![-.]))*[a-zA-Z0-9]+)?)$'
- #),
- re.compile( # http://stackoverflow.com/a/10874552/187736
- r'^(?=.{1,255})([0-9A-Za-z]|_{1}|\*{1}$)'
- r'(?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?'
- r'(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*\.?$'
- ),
- ]
- regex = regexes[1]
- def test_single_label():
- '''ensure the regex matches a single-label name'''
- assert regex.match('hostname') is not None
- def test_local():
- '''ensure the regex matches a two-part name'''
- assert regex.match('hostname.local') is not None
- def test_fqdn():
- '''ensure the regex matches a three-part name'''
- assert regex.match('hostname.domain.tld') is not None
- def test_fqdn_leaf():
- '''ensure the regex matches a four-part name'''
- assert regex.match('leaf.hostname.domain.tld') is not None
- def check_invalid(name):
- '''ensure the regex does not match a name containing {0}'''
- assert regex.match(name) is None
- def test_invalid():
- '''ensure the regex does not match a name with invalid characters'''
- syms = '`~!@#$%^&*()+=_'
- for s in syms:
- check_invalid.description = check_invalid.__doc__.format(s)
- yield check_invalid, s
- def test_whitespace():
- '''ensure the regex does not match a name containing whitespace'''
- assert regex.match('host name') is None
- def test_hyphen():
- '''ensure the regex matches a name containing a hyphen'''
- assert regex.match('host-name') is not None
- def test_leading_hyphen():
- '''ensure the regex does not match a name starting with a hyphen'''
- assert regex.match('-hostname') is None
- def test_trailing_hyphen():
- '''ensure the regex does not match a name ending with a hyphen'''
- assert regex.match('hostname-') is None
- def test_leading_period():
- '''ensure the regex does not match a name starting with a dot'''
- assert regex.match('.hostname') is None
- def test_trailing_period():
- '''ensure the regex matches a name ending with a dot'''
- assert regex.match('hostname.') is not None
- def test_only_dot():
- '''ensure the regex does not match only a single dot'''
- assert regex.match('.') is None
- def test_max_len_single_label():
- '''ensure the regex does not match a 64-character single name'''
- assert regex.match(64 * 'a') is None
- def test_max_len_single_label2():
- '''ensure the regex matches a 63-character single name'''
- assert regex.match(63 * 'a') is not None
- def test_max_len_tld():
- '''ensure the regex matches a 63-character top level name'''
- assert regex.match(63 * 'a' + '.') is not None
- def test_max_len_fqdn():
- '''ensure the regex matches a 63-character hostname in a FQDN'''
- assert regex.match(63 * 'a' + '.' + 'b') is not None
- def test_max_len_fqdn():
- '''ensure the regex matches a 255-character FQDN'''
- h = '.'.join((
- 63 * 'a',
- 63 * 'b',
- 63 * 'c',
- 63 * 'd',
- ))
- assert regex.match(h) is not None
- def test_max_len_fqdn2():
- '''ensure the regex does not match a 255-character 2-part FQDN'''
- h = '.'.join((
- 63 * 'a',
- 191 * 'b',
- ))
- assert regex.match(h) is None
- def test_double_dot():
- '''ensure the regex does not match multiple successive dots'''
- assert regex.match('hostname..domain') is None
- def test_idn():
- '''ensure the regex does not match a UTF-8 encoded IDN'''
- assert regex.match('bücher') is None
- def test_idn_punycode():
- '''ensure the regex matches a Punycode encoded IDN'''
- assert regex.match('bücher'.encode('idna').decode('ascii')) is not None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement