aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/net/test_check.py
blob: 7def75fd9286d36bea483450ab877580404d1f5c (plain)
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
74
75
76
77
78
# coding=utf-8

from mitmproxy.net import check


def test_is_valid_host():
    assert not check.is_valid_host(b"")
    assert not check.is_valid_host(b"xn--ke.ws")
    assert check.is_valid_host(b"one.two")
    assert not check.is_valid_host(b"one" * 255)
    assert check.is_valid_host(b"one.two.")
    # Allow underscore
    assert check.is_valid_host(b"one_two")
    assert check.is_valid_host(b"::1")

    # IPv6 Validations
    assert check.is_valid_host(b'2001:0db8:85a3:0000:0000:8a2e:0370:7334')
    assert check.is_valid_host(b'2001:db8:85a3:0:0:8a2e:370:7334')
    assert check.is_valid_host(b'2001:db8:85a3::8a2e:370:7334')
    assert not check.is_valid_host(b'2001:db8::85a3::7334')
    assert check.is_valid_host(b'2001-db8-85a3-8d3-1319-8a2e-370-7348.ipv6-literal.net')

    # TLD must be between 2 and 63 chars
    assert not check.is_valid_host(b'example.t')
    assert check.is_valid_host(b'example.tl')
    assert check.is_valid_host(b'example.tld')
    assert check.is_valid_host(b'example.' + b"x" * 63)
    assert not check.is_valid_host(b'example.' + b"x" * 64)

    # misc characters test
    assert not check.is_valid_host(b'ex@mple')
    assert not check.is_valid_host(b'ex@mple.com')
    assert not check.is_valid_host(b'example..com')
    assert not check.is_valid_host(b'.example.com')
    assert not check.is_valid_host(b'@.example.com')
    assert not check.is_valid_host(b'!.example.com')

    # Every label must be between 1 and 63 chars
    assert not check.is_valid_host(b'.tld')
    assert check.is_valid_host(b'x' * 1 + b'.tld')
    assert check.is_valid_host(b'x' * 30 + b'.tld')
    assert not check.is_valid_host(b'x' * 64 + b'.tld')
    assert check.is_valid_host(b'x' * 1 + b'.example.tld')
    assert check.is_valid_host(b'x' * 30 + b'.example.tld')
    assert not check.is_valid_host(b'x' * 64 + b'.example.tld')

    # Misc Underscore Test Cases
    assert check.is_valid_host(b'_example')
    assert check.is_valid_host(b'_example_')
    assert check.is_valid_host(b'example_')
    assert check.is_valid_host(b'_a.example.tld')
    assert check.is_valid_host(b'a_.example.tld')
    assert check.is_valid_host(b'_a_.example.tld')
    assert not check.is_valid_host(b'a._example')
    assert not check.is_valid_host(b'a._example_')
    assert not check.is_valid_host(b'a.example_')

    # Misc Dash/Hyphen/Minus Test Cases
    assert check.is_valid_host(b'-example')
    assert check.is_valid_host(b'-example_')
    assert check.is_valid_host(b'example-')
    assert check.is_valid_host(b'-a.example.tld')
    assert check.is_valid_host(b'a-.example.tld')
    assert check.is_valid_host(b'-a-.example.tld')
    assert not check.is_valid_host(b'a.-example')
    assert not check.is_valid_host(b'a.-example-')
    assert not check.is_valid_host(b'a.example-')

    # Misc Combo Test Cases
    assert check.is_valid_host(b'api-.example.com')
    assert check.is_valid_host(b'__a.example-site.com')
    assert check.is_valid_host(b'_-a.example-site.com')
    assert check.is_valid_host(b'_a_.example-site.com')
    assert check.is_valid_host(b'-a-.example-site.com')
    assert check.is_valid_host(b'api-.a.example.com')
    assert check.is_valid_host(b'api-._a.example.com')
    assert check.is_valid_host(b'api-.a_.example.com')
    assert check.is_valid_host(b'api-.ab.example.com')