<reponame>CESNET/exafs<gh_stars>1-10
import pytest
import flowapp.validators


def test_port_string_len_raises(field):
    port = flowapp.validators.PortString()
    field.data = "1;2;3;4;5;6;7;8"
    with pytest.raises(flowapp.validators.ValidationError):
        port(None, field)
        

@pytest.mark.parametrize("address, mask, expected", [
    ("172.16.58.3", "24", False),
    ("172.16.31.10", "24", True),
    ("0.0.0.0", "0", True),
    ("fdf8:f53e:61e4::18", "64", False),
    ("2001:718:1C01:1111::", "64", True),
])
def test_is_valid_address_with_mask(address, mask, expected):
    assert flowapp.validators.address_with_mask(address, mask) == expected


@pytest.mark.parametrize("address", [
    u"172.16.58.3",
    u"172.16.31.10"
])
def test_ip4address_passes(field, address):
    adr = flowapp.validators.IPv4Address()
    field.data = address
    adr(None, field)


@pytest.mark.parametrize("address", [
    u"fdf8:f53e:61e4::18",
    u"2001:718:1C01:1111::",
])
def test_ip6address_passes(field, address):
    adr = flowapp.validators.IPv6Address()
    field.data = address
    adr(None, field)


@pytest.mark.parametrize("address", [
    u"fdf8:f53e:61e4::18",
    u"2001:718:1C01:1111::",
])
def test_ip6address_passes(field, address):
    adr = flowapp.validators.IPv4Address()
    field.data = address
    with pytest.raises(flowapp.validators.ValidationError):
        adr(None, field)


@pytest.mark.parametrize("expired", [
    u"2018/10/25 14:46",
    u"2018/12/20 9:46",
    u"2019/05/22 12:33"
])
def test_expired_date_raises(field, expired):
    adr = flowapp.validators.DateNotExpired()
    field.data = expired
    with pytest.raises(flowapp.validators.ValidationError):
        adr(None, field)


@pytest.mark.parametrize("address", [
    u"147.230.1000.25",
    u"2001:718::::",
])
def test_ipaddress_raises(field, address):
    adr = flowapp.validators.IPv6Address()
    field.data = address
    with pytest.raises(flowapp.validators.ValidationError):
        adr(None, field)


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True)
])
def test_editable_rule(rule, address, mask, ranges, expected):
    rule.source = address
    rule.source_mask = mask
    assert flowapp.validators.editable_range(rule, ranges) == expected


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], False),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"2001:718:1c01::/48"], True),
    (u"192.168.3.11", u"16", [u"0.0.0.0/0", u"::/0"], True)
])
def test_address_in_range(address, mask, ranges, expected):
    assert flowapp.validators.address_in_range(address, ranges) == expected


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], False),
    (u"192.168.3.11", u"16", [u"192.168.3.11/18", u"192.168.127.12/21"], False),
    (u"192.168.3.11", u"16", [u"0.0.0.0/0", u"::/0"], True),
    (u"192.168.3.11", u"16", [u'172.16.58.3/16', u'2001:718:1c01::/48', u'0.0.0.0/0', u'::/0'], True)
])
def test_network_in_range(address, mask, ranges, expected):
    assert flowapp.validators.network_in_range(address, mask, ranges) == expected


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"192.168.3.11", u"16", [u"172.16.58.3/16", u"172.16.58.3/24"], True),
])
def test_range_in_network(address, mask, ranges, expected):
    assert flowapp.validators.range_in_network(address, mask, ranges) == expected

import pytest
import flowapp.validators


def test_port_string_len_raises(field):
    port = flowapp.validators.PortString()
    field.data = "1;2;3;4;5;6;7;8"
    with pytest.raises(flowapp.validators.ValidationError):
        port(None, field)
        

@pytest.mark.parametrize("address, mask, expected", [
    ("172.16.58.3", "24", False),
    ("172.16.31.10", "24", True),
    ("0.0.0.0", "0", True),
    ("fdf8:f53e:61e4::18", "64", False),
    ("2001:718:1C01:1111::", "64", True),
])
def test_is_valid_address_with_mask(address, mask, expected):
    assert flowapp.validators.address_with_mask(address, mask) == expected


@pytest.mark.parametrize("address", [
    u"172.16.58.3",
    u"172.16.31.10"
])
def test_ip4address_passes(field, address):
    adr = flowapp.validators.IPv4Address()
    field.data = address
    adr(None, field)


@pytest.mark.parametrize("address", [
    u"fdf8:f53e:61e4::18",
    u"2001:718:1C01:1111::",
])
def test_ip6address_passes(field, address):
    adr = flowapp.validators.IPv6Address()
    field.data = address
    adr(None, field)


@pytest.mark.parametrize("address", [
    u"fdf8:f53e:61e4::18",
    u"2001:718:1C01:1111::",
])
def test_ip6address_passes(field, address):
    adr = flowapp.validators.IPv4Address()
    field.data = address
    with pytest.raises(flowapp.validators.ValidationError):
        adr(None, field)


@pytest.mark.parametrize("expired", [
    u"2018/10/25 14:46",
    u"2018/12/20 9:46",
    u"2019/05/22 12:33"
])
def test_expired_date_raises(field, expired):
    adr = flowapp.validators.DateNotExpired()
    field.data = expired
    with pytest.raises(flowapp.validators.ValidationError):
        adr(None, field)


@pytest.mark.parametrize("address", [
    u"147.230.1000.25",
    u"2001:718::::",
])
def test_ipaddress_raises(field, address):
    adr = flowapp.validators.IPv6Address()
    field.data = address
    with pytest.raises(flowapp.validators.ValidationError):
        adr(None, field)


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True)
])
def test_editable_rule(rule, address, mask, ranges, expected):
    rule.source = address
    rule.source_mask = mask
    assert flowapp.validators.editable_range(rule, ranges) == expected


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], False),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"2001:718:1c01::/48"], True),
    (u"192.168.3.11", u"16", [u"0.0.0.0/0", u"::/0"], True)
])
def test_address_in_range(address, mask, ranges, expected):
    assert flowapp.validators.address_in_range(address, ranges) == expected


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], True),
    (u"172.16.31.10", u"24", [u"172.16.58.3/16", u"172.16.58.3/16"], False),
    (u"192.168.3.11", u"16", [u"192.168.3.11/18", u"192.168.127.12/21"], False),
    (u"192.168.3.11", u"16", [u"0.0.0.0/0", u"::/0"], True),
    (u"192.168.3.11", u"16", [u'172.16.58.3/16', u'2001:718:1c01::/48', u'0.0.0.0/0', u'::/0'], True)
])
def test_network_in_range(address, mask, ranges, expected):
    assert flowapp.validators.network_in_range(address, mask, ranges) == expected


@pytest.mark.parametrize("address, mask, ranges, expected", [
    (u"192.168.3.11", u"16", [u"172.16.58.3/16", u"172.16.58.3/24"], True),
])
def test_range_in_network(address, mask, ranges, expected):
    assert flowapp.validators.range_in_network(address, mask, ranges) == expected