File: test_validation.py

package info (click to toggle)
python-path-and-address 2.0.1-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 124 kB
  • sloc: python: 214; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 1,874 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from itertools import product

from path_and_address import valid_address, valid_hostname, valid_port


def _join(host_and_port):
    return '%s:%s' % host_and_port


def _join_all(hostnames, ports):
    return list(map(_join, product(hostnames, ports)))


hostnames = [
    '0.0.0.0',
    '127.0.0.1',
    'localhost',
    'example.com',
    'example.org',
]
invalid_hostnames = [
    'http://example.com',
    'http://example.com:8080',
    'example.com/',
    'example.com:8080/',
    'example.com:0',
    '0.0.0.0:0',
]


ports = [0, 1, 80, 5000, 8080, 65535]
invalid_ports = [
    None, -80, -1, 0, 65536, 75000,
    float('nan'), '', 'nan', 'hello', 'a string',
]


addresses = hostnames + ports + _join_all(hostnames, ports)
invalid_addresses = (
    invalid_hostnames +
    _join_all(hostnames, invalid_ports) +
    _join_all(invalid_hostnames, ports) +
    _join_all(invalid_hostnames, invalid_ports))


def test_valid_address():
    for address in addresses:
        assert (valid_address(address),
                'Invalid address, expected to be valid: ' + repr(address))

    for address in invalid_addresses:
        assert (not valid_address(address),
                'Valid address, expected to be invalid: ' + repr(address))


def test_valid_hostname():
    for hostname in hostnames:
        assert (valid_hostname(hostname),
                'Invalid hostname, expected to be valid: ' + repr(hostname))

    for hostname in invalid_hostnames:
        assert (not valid_hostname(hostname),
                'Valid hostname, expected to be invalid: ' + repr(hostname))


def test_valid_port():
    for port in ports:
        assert (valid_port(port),
                'Invalid port, expected to be valid: ' + repr(port))

    for port in invalid_ports:
        assert (not valid_port(port),
                'Valid port, expected to be invalid: ' + repr(port))