Advertisement
AdmiralNemo

test_hostname_regex

May 3rd, 2014
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.90 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # vim: set fileencoding=utf=8 :
  3. '''Test regular expressions for matching an Internet host name
  4.  
  5. These regular expressions were found at
  6. http://stackoverflow.com/questions/1418423/the-hostname-regex
  7. '''
  8. from __future__ import unicode_literals
  9. import re
  10.  
  11.  
  12. regexes = [
  13.     re.compile( # http://stackoverflow.com/q/1418423/187736
  14.         r'^[0-9a-z]([0-9a-z\-]{0,61}[0-9a-z])?'
  15.         r'(\.[0-9a-z](0-9a-z\-]{0,61}[0-9a-z])?)*$'
  16.     ),
  17.     re.compile( # http://stackoverflow.com/a/1420225/187736
  18.         r'^(?=.{1,255}$)[0-9A-Za-z]'
  19.         r'(?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?'
  20.         r'(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$'
  21.     ),
  22.     re.compile( # http://stackoverflow.com/a/1418724/187736
  23.         r'(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*'
  24.         r'(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z])[.]?)'
  25.     ),
  26.     re.compile( # http://stackoverflow.com/a/1418724/187736
  27.         r'(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]{0,61})?[a-zA-Z0-9])[.])*'
  28.         r'(?:[a-zA-Z][-a-zA-Z0-9]{0,61}[a-zA-Z0-9]|[a-zA-Z])[.]?)'
  29.     ),
  30.     # This one doesn't even compile
  31.     #re.compile( # http://stackoverflow.com/a/18494710/187736
  32.     #    r'^([a-zA-Z0-9](?:(?:[a-zA-Z0-9-]*|(?<!-)\.(?![-.]))*[a-zA-Z0-9]+)?)$'
  33.     #),
  34.     re.compile( # http://stackoverflow.com/a/10874552/187736
  35.         r'^(?=.{1,255})([0-9A-Za-z]|_{1}|\*{1}$)'
  36.         r'(?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?'
  37.         r'(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*\.?$'
  38.     ),
  39. ]
  40. regex = regexes[1]
  41.  
  42.  
  43. def test_single_label():
  44.     '''ensure the regex matches a single-label name'''
  45.  
  46.     assert regex.match('hostname') is not None
  47.  
  48.  
  49. def test_local():
  50.     '''ensure the regex matches a two-part name'''
  51.  
  52.     assert regex.match('hostname.local') is not None
  53.  
  54.  
  55. def test_fqdn():
  56.     '''ensure the regex matches a three-part name'''
  57.  
  58.     assert regex.match('hostname.domain.tld') is not None
  59.  
  60.  
  61. def test_fqdn_leaf():
  62.     '''ensure the regex matches a four-part name'''
  63.  
  64.     assert regex.match('leaf.hostname.domain.tld') is not None
  65.  
  66.  
  67. def check_invalid(name):
  68.     '''ensure the regex does not match a name containing {0}'''
  69.     assert regex.match(name) is None
  70.  
  71.  
  72. def test_invalid():
  73.     '''ensure the regex does not match a name with invalid characters'''
  74.  
  75.     syms = '`~!@#$%^&*()+=_'
  76.     for s in syms:
  77.         check_invalid.description = check_invalid.__doc__.format(s)
  78.         yield check_invalid, s
  79.  
  80.  
  81. def test_whitespace():
  82.     '''ensure the regex does not match a name containing whitespace'''
  83.  
  84.     assert regex.match('host name') is None
  85.  
  86.  
  87. def test_hyphen():
  88.     '''ensure the regex matches a name containing a hyphen'''
  89.  
  90.     assert regex.match('host-name') is not None
  91.  
  92. def test_leading_hyphen():
  93.     '''ensure the regex does not match a name starting with a hyphen'''
  94.  
  95.     assert regex.match('-hostname') is None
  96.  
  97.  
  98. def test_trailing_hyphen():
  99.     '''ensure the regex does not match a name ending with a hyphen'''
  100.  
  101.     assert regex.match('hostname-') is None
  102.  
  103.  
  104. def test_leading_period():
  105.     '''ensure the regex does not match a name starting with a dot'''
  106.  
  107.     assert regex.match('.hostname') is None
  108.  
  109.  
  110. def test_trailing_period():
  111.     '''ensure the regex matches a name ending with a dot'''
  112.  
  113.     assert regex.match('hostname.') is not None
  114.  
  115.  
  116. def test_only_dot():
  117.     '''ensure the regex does not match only a single dot'''
  118.  
  119.     assert regex.match('.') is None
  120.  
  121.  
  122. def test_max_len_single_label():
  123.     '''ensure the regex does not match a 64-character single name'''
  124.  
  125.     assert regex.match(64 * 'a') is None
  126.  
  127.  
  128. def test_max_len_single_label2():
  129.     '''ensure the regex matches a 63-character single name'''
  130.  
  131.     assert regex.match(63 * 'a') is not None
  132.  
  133.  
  134. def test_max_len_tld():
  135.     '''ensure the regex matches a 63-character top level name'''
  136.  
  137.     assert regex.match(63 * 'a' + '.') is not None
  138.  
  139.  
  140. def test_max_len_fqdn():
  141.     '''ensure the regex matches a 63-character hostname in a FQDN'''
  142.  
  143.     assert regex.match(63 * 'a' + '.' + 'b') is not None
  144.  
  145.  
  146. def test_max_len_fqdn():
  147.     '''ensure the regex matches a 255-character FQDN'''
  148.  
  149.     h = '.'.join((
  150.         63 * 'a',
  151.         63 * 'b',
  152.         63 * 'c',
  153.         63 * 'd',
  154.     ))
  155.     assert regex.match(h) is not None
  156.  
  157.  
  158. def test_max_len_fqdn2():
  159.     '''ensure the regex does not match a 255-character 2-part FQDN'''
  160.  
  161.     h = '.'.join((
  162.         63 * 'a',
  163.         191 * 'b',
  164.     ))
  165.     assert regex.match(h) is None
  166.  
  167.  
  168. def test_double_dot():
  169.     '''ensure the regex does not match multiple successive dots'''
  170.  
  171.     assert regex.match('hostname..domain') is None
  172.  
  173.  
  174. def test_idn():
  175.     '''ensure the regex does not match a UTF-8 encoded IDN'''
  176.  
  177.     assert regex.match('bücher') is None
  178.  
  179.  
  180. def test_idn_punycode():
  181.     '''ensure the regex matches a Punycode encoded IDN'''
  182.  
  183.     assert regex.match('bücher'.encode('idna').decode('ascii')) is not None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement