Exemple #1
0
def test_mo_msb():
    """Tests RuleMgnt's Mos Significant Byte function"""
    import sys  # Temporary disabling python3 testing, something is going wrong in MO_MSB fuinction
    if sys.version_info[0] < 3:
        tv_0 = '01011111'
        fv_0 = '01001000'
        assert RuleMngt.MO_MSB(tv_0, fv_0, 0, arg=8 * 3)
        assert not RuleMngt.MO_MSB(tv_0, fv_0, 0, arg=8 * 4)
Exemple #2
0
def test_mo_equal():
    """Tests RuleMgnt's Equal function"""
    tv_0 = "value"
    fv_0 = "value"
    assert RuleMngt.MO_equal(tv_0, fv_0, 0)
    tv_1 = "value"
    fv_1 = "value_2"
    assert not RuleMngt.MO_equal(tv_1, fv_1, 0)
    tv_2 = "value"
    fv_2 = 5
    assert not RuleMngt.MO_equal(tv_2, fv_2, 0)
Exemple #3
0
def test_mo_matchmapping_list():
    """Tests RuleMgnt's matchmapping function"""
    tv_0 = [0, 1, 2, 3]
    fv_0 = 2
    assert RuleMngt.MO_matchmapping(tv_0, fv_0, 0)

    tv_1 = ["0", "1", "2", "3"]
    fv_1 = 2
    assert not RuleMngt.MO_matchmapping(tv_1, fv_1, 0)

    tv_2 = [0, 1, 2, 3]
    fv_2 = 4
    assert not RuleMngt.MO_matchmapping(tv_2, fv_2, 0)
Exemple #4
0
def test___init__():
    """Tests RuleMgnt's constructor"""
    mgr = RuleMngt.RuleManager()
    assert not mgr.context
    assert "ignore" in mgr.MatchingOperators
    assert "equal" in mgr.MatchingOperators
    assert "match-mapping" in mgr.MatchingOperators
    assert "MSB" in mgr.MatchingOperators
Exemple #5
0
def test_findrulefromheader():
    """Tests RuleMgnt's findRuleFromHeader"""
    mgr = RuleMngt.RuleManager()
    rule_id = 14
    rule = {
        "ruleid":
        rule_id,
        "content": [
            # fID, Pos, DI, TV, MO, CDA
            ["field1", 1, "bi", 6, "equal", "not-sent"],
        ]
    }
    mgr.addRule(rule)
    assert mgr.FindRuleFromHeader({("field1", 1): "test"}, "up") is not None
Exemple #6
0
def test_apply():
    """ Tests Decompressor's apply function"""
    rule_coap1 = {
        "ruleid":
        1,
        "content":
        [["IPv6.version", 1, "bi", 6, "equal", "not-sent"],
         ["IPv6.trafficClass", 1, "bi", 0x00, "equal", "not-sent"],
         ["IPv6.flowLabel", 1, "bi", 0x000000, "equal", "not-sent"],
         ["IPv6.payloadLength", 1, "bi", None, "ignore", "compute-length"],
         ["IPv6.nextHeader", 1, "bi", 17, "equal", "not-sent"],
         ["IPv6.hopLimit", 1, "bi", 30, "ignore", "not-sent"],
         ["IPv6.prefixES", 1, "bi", 0xFE80000000000000, "equal", "not-sent"],
         ["IPv6.iidES", 1, "bi", 0x0000000000000001, "equal", "not-sent"],
         [
             "IPv6.prefixLA", 1, "bi",
             [
                 0x2001066073010001, 0x2001123456789012, 0x2001123456789013,
                 0xFE80000000000000
             ], "match-mapping", "mapping-sent"
         ], ["IPv6.iidLA", 1, "bi", 0x0000000000000002, "equal", "not-sent"],
         ["UDP.PortES", 1, "bi", 5682, "equal", "not-sent"],
         ["UDP.PortLA", 1, "bi", 5683, "equal", "not-sent"],
         ["UDP.length", 1, "bi", None, "ignore", "compute-length"],
         ["UDP.checksum", 1, "bi", None, "ignore", "compute-checksum"],
         ["CoAP.version", 1, "bi", 1, "equal", "not-sent"],
         ["CoAP.type", 1, "up", 0, "equal", "not-sent"],
         ["CoAP.type", 1, "dw", 2, "equal", "not-sent"],
         ["CoAP.tokenLength", 1, "bi", 1, "equal", "not-sent"],
         ["CoAP.code", 1, "up", 2, "equal", "not-sent"],
         ["CoAP.code", 1, "dw", [69, 132], "match-mapping", "mapping-sent"],
         ["CoAP.messageID", 1, "bi", 1, "MSB(12)", "LSB"],
         ["CoAP.token", 1, "bi", 0x80, "MSB(4)", "LSB"],
         ["CoAP.Uri-Path", 1, "up", "foo", "equal", "not-sent"],
         ["CoAP.Uri-Path", 2, "up", "bar", "equal", "not-sent"],
         ["CoAP.Uri-Path", 3, "up", None, "ignore", "value-sent"],
         ["CoAP.Uri-Query", 1, "up", "k=", "MSB(16)", "LSB"],
         ["CoAP.Option-End", 1, "up", 0xFF, "equal", "not-sent"]]
    }
    compressed = bytearray(b'\xde\x40')  # 11 bits
    rule_manager = RuleMngt.RuleManager()
    rule_manager.addRule(rule_coap1)
    rule = rule_manager.FindRuleFromID(1)
    dec = Decompressor.Decompressor(rule_manager)

    # reenable pylint when uncommenting verification
    header, length = dec.apply(compressed, rule, "dw")  # pylint: disable=unused-variable
Exemple #7
0
def test_findrulefromid():
    """Tests RuleMgnt's findRuleFromID"""
    mgr = RuleMngt.RuleManager()
    rule_id = 14
    rule = {
        "ruleid":
        rule_id,
        "content": [
            # fID, Pos, DI, TV, MO, CDA
            ["field1", 1, "bi", 6, "equal", "not-sent"],
            ["field2", 1, "up", 6, "equal", "not-sent"],
            ["field3", 1, "do", 6, "equal", "not-sent"],
            ["field4", 1, "do", 6, "equal", "not-sent"],
        ]
    }
    mgr.addRule(rule)
    assert mgr.FindRuleFromID(rule_id) is not None
Exemple #8
0
def test_addrule():
    """Tests RuleMgnt's addRule"""
    mgr = RuleMngt.RuleManager()
    rule = {
        "ruleid":
        0,
        "content": [
            # fID, Pos, DI, TV, MO, CDA
            ["field1", 1, "bi", 6, "equal", "not-sent"],
            ["field2", 1, "up", 6, "equal", "not-sent"],
            ["field3", 1, "do", 6, "equal", "not-sent"],
            ["field4", 1, "do", 6, "equal", "not-sent"],
        ]
    }
    mgr.addRule(rule)

    assert len(mgr.context) == 1
    assert rule['upRules'] == 2
    assert rule['downRules'] == 3
Exemple #9
0
def test_addrule_doubleid():
    """Tests RuleMgnt's addrule function with an
    already existing ID"""
    mgr = RuleMngt.RuleManager()
    rule_id = 14
    rule = {
        "ruleid":
        rule_id,
        "content": [
            # fID, Pos, DI, TV, MO, CDA
            ["field1", 1, "bi", 6, "equal", "not-sent"],
            ["field2", 1, "up", 6, "equal", "not-sent"],
            ["field3", 1, "do", 6, "equal", "not-sent"],
            ["field4", 1, "do", 6, "equal", "not-sent"],
        ]
    }
    mgr.addRule(rule)
    assert len(mgr.context) == 1
    with pytest.raises(ValueError) as exception_info:
        mgr.addRule(rule)
    assert 'Rule ID already exists' in str(exception_info.value)
    assert str(rule_id) in str(exception_info.value)
Exemple #10
0
def test_mo_ignore():
    """Tests RuleMgnt's ignore function"""
    assert RuleMngt.MO_ignore("", "", 0)