예제 #1
0
def test_header_constraints_serialization():
    hc = HeaderConstraints()
    hcd = hc.dict()
    fields = (set(map(attrgetter('name'), attr.fields(HeaderConstraints))) -
              {'firewallClassifications'} | {'flowStates'})
    for field in fields:
        assert hcd[field] is None

    hc = HeaderConstraints(srcIps="1.1.1.1")
    assert hc.dict()["srcIps"] == "1.1.1.1"

    hc = HeaderConstraints(dstPorts=["10-20", "33-33"])
    assert hc.dict()["dstPorts"] == "10-20,33-33"

    hc = HeaderConstraints(dstPorts="10-20,33")
    assert hc.dict()["dstPorts"] == "10-20,33"

    for dp in [10, "10", [10], ["10"]]:
        hc = HeaderConstraints(dstPorts=dp)
        assert hc.dict()["dstPorts"] == "10"

    hc = HeaderConstraints(applications="dns,ssh")
    assert hc.dict()["applications"] == ['dns', 'ssh']

    hc = HeaderConstraints(applications=['dns', 'ssh'])
    assert hc.dict()["applications"] == ['dns', 'ssh']

    hc = HeaderConstraints(ipProtocols="  ,  dns,")
    assert hc.dict()["ipProtocols"] == ['dns']

    hc = HeaderConstraints(ipProtocols=['tcp', 'udp'])
    assert hc.dict()["ipProtocols"] == ['tcp', 'udp']

    with pytest.raises(ValueError):
        HeaderConstraints(applications="")
예제 #2
0
def test_header_constraints_serialization():
    hc = HeaderConstraints()
    hcd = hc.dict()
    fields = set(map(attrgetter("name"), attr.fields(HeaderConstraints)))
    for field in fields:
        assert hcd[field] is None

    hc = HeaderConstraints(srcIps="1.1.1.1")
    assert hc.dict()["srcIps"] == "1.1.1.1"

    hc = HeaderConstraints(dstPorts=["10-20", "33-33"])
    assert hc.dict()["dstPorts"] == "10-20,33-33"

    hc = HeaderConstraints(dstPorts="10-20,33")
    assert hc.dict()["dstPorts"] == "10-20,33"

    for dp in [10, "10", [10], ["10"]]:
        hc = HeaderConstraints(dstPorts=dp)
        assert hc.dict()["dstPorts"] == "10"

    hc = HeaderConstraints(applications="dns,ssh")
    assert hc.dict()["applications"] == ["dns", "ssh"]

    hc = HeaderConstraints(applications=["dns", "ssh"])
    assert hc.dict()["applications"] == ["dns", "ssh"]

    hc = HeaderConstraints(ipProtocols="  ,  dns,")
    assert hc.dict()["ipProtocols"] == ["dns"]

    hc = HeaderConstraints(ipProtocols=["tcp", "udp"])
    assert hc.dict()["ipProtocols"] == ["tcp", "udp"]

    match_syn = MatchTcpFlags(tcpFlags=TcpFlags(syn=True), useSyn=True)
    hc1 = HeaderConstraints(tcpFlags=match_syn)
    hc2 = HeaderConstraints(tcpFlags=[match_syn])
    assert hc1.dict() == hc2.dict()

    with pytest.raises(ValueError):
        HeaderConstraints(applications="")