コード例 #1
0
def test_ruleman_01():
    # XXX actually, it is not test code right now.
    RM = RuleManager()
    RM.add_context(context1, rule1, rule2, rule3)
    RM.add_context(context2, rule1)
    print(RM._db)
    #RM.add_rules(context1, [conflictingRule0])
    #RM.add_rules(context1, [bogusRule0])
    print(RM.find_context_bydevL2addr("AABBCCDD"))
    print(RM.find_context_bydstiid("2001:0db8:85a3::beef"))
    RM.find_rule_bypacket(context1,
                          BitBuffer(int("10000111", 2).to_bytes(1, "big")))
コード例 #2
0
def test_fragment_bitmap_02():
    print("## find_missing_tiles:")
    tile_list = [
        {
            "w-num": 0,
            "t-num": 6,
            "nb_tiles": 3
        },
        {
            "w-num": 0,
            "t-num": 3,
            "nb_tiles": 4
        },
        {
            "w-num": 1,
            "t-num": 6,
            "nb_tiles": 3
        },
        {
            "w-num": 1,
            "t-num": 3,
            "nb_tiles": 3
        },
        {
            "w-num": 3,
            "t-num": 5,
            "nb_tiles": 2
        },
        {
            "w-num": 3,
            "t-num": 7,
            "nb_tiles": 1
        },
    ]
    bitmaps = find_missing_tiles(tile_list, 3, 7)
    expected = [(1, BitBuffer([1, 1, 1, 1, 1, 1, 0])),
                (2, BitBuffer([0, 0, 0, 0, 0, 0, 0])),
                (3, BitBuffer([0, 1, 1, 0, 0, 0, 1]))]
    check_bitmaps(bitmaps, expected)
コード例 #3
0
def test_bitarray2():
    print("""
    ## set_bit()
        """)
    a = BitBuffer()
    a.set_bit(0)
    a.set_bit(0)
    a.set_bit(1)
    a.set_bit(1)
    a.set_bit(1)
    a.set_bit(0)
    a.set_bit(1)
    print(a.get_content())  # 0011 1010 : \x3a
    print(a)
    assert str(a) == r"b'\x3a'/7"

    a.set_bit(1, 0)
    a.set_bit(0, 3)
    print(a.get_content())  # 1010 1010 : \xaa
    print(a)
    assert str(a) == r"b'\xaa'/7"

    print("""
    ## add_bits()
        """)
    a = BitBuffer()
    a.add_bits(7, 4)  # rule : 0111
    a.add_bits(2, 3)  # dtag : 010
    print("added bits:", a.count_added_bits())
    print("padding bits:", a.count_padding_bits())
    print("remaining bits:", a.count_remaining_bits())
    assert a.count_added_bits() == 7
    assert a.count_padding_bits() == 1
    assert a.count_remaining_bits() == 7
    a.add_bits(3, 3)  # win  : 011
    a.add_bits(6, 3)  # fcn  : 110
    print(a.get_content())
    print(a)
    # 0111 0100 1111 0000 : \x74f0

    assert str(a) == r"b'\x74\xf0'/13"

    print("""
    ## copy()
        """)
    c = BitBuffer(bytearray([1, 2, 3, 4, 5]))
    c.add_bits(5, 6)
    d = c.copy()
    print(c)
    print(d)
    assert str(c) == str(d)
    assert id(c) != id(d)
    assert id(c._content) != id(d._content)
    k = 100
    al = [BitBuffer(bytearray([_ for _ in range(16)])) for _ in range(k)]
    b = BitBuffer()
    for i in al:
        b += i
    c = BitBuffer(bytearray([_ % 16 for _ in range(16 * k)]))
    print(c)
    assert str(b) == str(c)

    print("""
    ## get_bits()
        """)
    b = a.copy()
    print(a)
    print(b)
    print(b.get_bits(4), ": rule = 7, rpos =", b._rpos)
    print(b.get_bits(3), ": dtag = 2, rpos =", b._rpos)
    print("added bits:", b.count_added_bits())
    print("padding bits:", b.count_padding_bits())
    print("remaining bits:", b.count_remaining_bits())
    assert b.count_added_bits() == 13
    assert b.count_padding_bits() == 3
    assert b.count_remaining_bits() == 6
    print(b.get_bits(3), ": win  = 3, rpos =", b._rpos)
    print(b.get_bits(3), ": fcn  = 6, rpos =", b._rpos)
    print("remaining bits:", b.count_remaining_bits())
    assert b.count_remaining_bits() == 0

    print("""
    ## get_bits(position)
        """)
    b = a.copy()
    print(b.get_bits(4, 0), ": rule = 7, rpos =", b._rpos)
    print(b.get_bits(3, 4), ": dtag = 2, rpos =", b._rpos)
    print("added bits:", b.count_added_bits())
    print("padding bits:", b.count_padding_bits())
    print("remaining bits:", b.count_remaining_bits())
    assert b.count_added_bits() == 13
    assert b.count_padding_bits() == 3
    assert b.count_remaining_bits() == 13
    print(b.get_bits(3, 7), ": win  = 3, rpos =", b._rpos)
    print(b.get_bits(3, 10), ": fcn  = 6, rpos =", b._rpos)
    print("remaining bits:", b.count_remaining_bits())
    assert b.count_remaining_bits() == 13

    print("""
    ## get_bits_as_buffer()
        """)
    b = a.copy()
    b.get_bits(1)
    print(b)
    # a = b'\x74\xf0'/12[1:13]
    #     0111 0100 1111 0000
    # b = b'\xe9\xe0'/12
    #     1110 1001 1110 0000
    c = b.get_bits_as_buffer(3)
    print(c)
    assert str(c) == r"b'\xe0'/3"
    c = b.get_bits_as_buffer()
    print(c)
    assert str(c) == r"b'\x4f\x00'/9"

    print("""
    ## __add__
        """)
    # the header size is 27 bits.
    # frag: 0000 0001 0000 0002 0000 0003 111
    c = BitBuffer(bytearray([1, 2, 3]))
    c.add_bits(7, 5)
    print("a =", c)
    print("c =", c)
    b = a + c
    print("a + c =", b)
    print("c =", c)
    assert str(b) == r"b'\x74\xf0\x08\x10\x19\xc0'/42"

    print("""
    ### to_bit_list()
        """)
    a = BitBuffer([0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0])
    print(a)
    bl = a.to_bit_list()
    print(bl)
    assert bl == [0, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0]

    print("""
    ### allones()
        """)
    a = BitBuffer([1, 1, 1, 1])
    print(a, a.allones())
    assert True == a.allones()
    a = BitBuffer([1, 1, 1, 1, 0])
    print(a, a.allones())
    assert False == a.allones()
    a = BitBuffer([0, 1, 1, 1, 1])
    print(a, "[1:]", a.allones(1))
    assert True == a.allones(1)

    print("""
    ## add_bytes()
        """)
    a = BitBuffer(bytearray([1, 2]))
    print(a)
    a.add_bytes(bytearray([3, 4]))
    print(a)
    assert str(a) == r"b'\x01\x02\x03\x04'/32"
    a = BitBuffer([0, 1, 0, 1])
    print(a)
    a.add_bytes(bytearray([3, 4]))
    print(a)
    assert str(a) == r"b'\x50\x30\x40'/20"
コード例 #4
0
 def gen_bits(N):
     #
     if N < 1:
         return None
     if N == 1:
         b = BitBuffer()
         b.set_bit(0)
         yield b
         b = BitBuffer()
         b._wpos = 0
         b.set_bit(1)
         yield b
     else:
         p = (1 << N) - 2
         for i in range(p + 1):
             b = BitBuffer()
             b.add_bits(pow(2, i) - 1, i)
             b.set_bit(0)
             b.add_bits(pow(2, p - i) - 1, p - i)
             yield b
         b = BitBuffer()
         b.add_bits(pow(2, p + 1) - 1, p + 1)
         yield b
コード例 #5
0
def test_fragment_bitmap_04():
    print("""## Figure 28 in draft-17""")
    N = 5  # all-1 = 31
    WINDOW_SIZE = 28
    tile_list = [
        {
            "w-num": 0,
            "t-num": 27,
            "nb_tiles": 4
        },
        {
            "w-num": 0,
            "t-num": 23,
            "nb_tiles": 4
        },
        {
            "w-num": 0,
            "t-num": 19,
            "nb_tiles": 4
        },
        #{"w-num": 0, "t-num": 15, "nb_tiles": 4 },
        {
            "w-num": 0,
            "t-num": 11,
            "nb_tiles": 4
        },
        {
            "w-num": 0,
            "t-num": 7,
            "nb_tiles": 4
        },
        {
            "w-num": 0,
            "t-num": 3,
            "nb_tiles": 4
        },
        {
            "w-num": 1,
            "t-num": 27,
            "nb_tiles": 4
        },
        {
            "w-num": 1,
            "t-num": 23,
            "nb_tiles": 4
        },
        {
            "w-num": 1,
            "t-num": 19,
            "nb_tiles": 4
        },
        {
            "w-num": 1,
            "t-num": 15,
            "nb_tiles": 4
        },
        {
            "w-num": 1,
            "t-num": 11,
            "nb_tiles": 4
        },
        {
            "w-num": 1,
            "t-num": 7,
            "nb_tiles": 4
        },
        #{"w-num": 1, "t-num":  3, "nb_tiles": 4 },
        {
            "w-num": 2,
            "t-num": 27,
            "nb_tiles": 4
        },
        {
            "w-num": 2,
            "t-num": 23,
            "nb_tiles": 4
        },
        {
            "w-num": 2,
            "t-num": 19,
            "nb_tiles": 1
        },
        {
            "w-num": 2,
            "t-num": 18,
            "nb_tiles": 1
        },
        {
            "w-num": 2,
            "t-num": 17,
            "nb_tiles": 1
        },
        {
            "w-num": 2,
            "t-num": 16,
            "nb_tiles": 1
        },
        {
            "w-num": 2,
            "t-num": 15,
            "nb_tiles": 1
        },
        {
            "w-num": 2,
            "t-num": 14,
            "nb_tiles": 1
        },
        #{"w-num": 2, "t-num": 13, "nb_tiles": 1 },
        {
            "w-num": 2,
            "t-num": 12,
            "nb_tiles": 1
        },
        {
            "w-num": 2,
            "t-num": 31,
            "nb_tiles": 1
        },
    ]

    bitmaps = find_missing_tiles(tile_list, N, WINDOW_SIZE)
    expected = [(0,
                 BitBuffer([
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1,
                     1, 1, 1, 1, 1, 1, 1, 1, 1
                 ])),
                (1,
                 BitBuffer([
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     1, 1, 1, 1, 1, 0, 0, 0, 0
                 ])),
                (2,
                 BitBuffer([
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 1
                 ]))]
    check_bitmaps(bitmaps, expected)

    tile_list.append({"w-num": 0, "t-num": 15, "nb_tiles": 4})
    bitmaps = find_missing_tiles(tile_list, N, WINDOW_SIZE)
    expected = [(1,
                 BitBuffer([
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     1, 1, 1, 1, 1, 0, 0, 0, 0
                 ])),
                (2,
                 BitBuffer([
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 1
                 ]))]
    check_bitmaps(bitmaps, expected)

    tile_list.append({"w-num": 1, "t-num": 3, "nb_tiles": 4})
    bitmaps = find_missing_tiles(tile_list, N, WINDOW_SIZE)
    expected = [(2,
                 BitBuffer([
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 1
                 ]))]
    check_bitmaps(bitmaps, expected)

    tile_list.append({"w-num": 2, "t-num": 13, "nb_tiles": 1})
    bitmaps = find_missing_tiles(tile_list, N, WINDOW_SIZE)
    expected = [(2,
                 BitBuffer([
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
                     0, 0, 0, 0, 0, 0, 0, 0, 1
                 ]))]
    check_bitmaps(bitmaps, expected)
コード例 #6
0
def test_fragment_bitmap_03():
    print("""## Figure 27 in draft-17""")
    N = 3
    WINDOW_SIZE = 7
    nb_tiles = 11
    tile_list = [
        {
            "w-num": 0,
            "t-num": 6,
            "nb_tiles": 1
        },
        {
            "w-num": 0,
            "t-num": 5,
            "nb_tiles": 1
        },
        #{"w-num": 0, "t-num": 4, "nb_tiles": 1 },
        {
            "w-num": 0,
            "t-num": 3,
            "nb_tiles": 1
        },
        #{"w-num": 0, "t-num": 2, "nb_tiles": 1 },
        {
            "w-num": 0,
            "t-num": 1,
            "nb_tiles": 1
        },
        {
            "w-num": 0,
            "t-num": 0,
            "nb_tiles": 1
        },
        {
            "w-num": 1,
            "t-num": 6,
            "nb_tiles": 1
        },
        {
            "w-num": 1,
            "t-num": 5,
            "nb_tiles": 1
        },
        #{"w-num": 1, "t-num": 4, "nb_tiles": 1 },
        {
            "w-num": 1,
            "t-num": 7,
            "nb_tiles": 1
        },
    ]

    bitmaps = find_missing_tiles(tile_list, N, WINDOW_SIZE)
    expected = [(0, BitBuffer([1, 1, 0, 1, 0, 1, 1])),
                (1, BitBuffer([1, 1, 0, 0, 0, 0, 1]))]
    check_bitmaps(bitmaps, expected)

    tile_list.append({"w-num": 0, "t-num": 4, "nb_tiles": 1})
    tile_list.append({"w-num": 0, "t-num": 2, "nb_tiles": 1})
    bitmaps = find_missing_tiles(tile_list, N, WINDOW_SIZE)
    expected = [(1, BitBuffer([1, 1, 0, 0, 0, 0, 1]))]
    check_bitmaps(bitmaps, expected)

    tile_list.append({"w-num": 1, "t-num": 4, "nb_tiles": 1})
    bitmaps = find_missing_tiles(tile_list, N, WINDOW_SIZE)
    expected = [(1, BitBuffer([1, 1, 1, 0, 0, 0, 1]))]
    check_bitmaps(bitmaps, expected)