Ejemplo n.º 1
0
    def test_get_raw(self):
        fake_fields = [
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8)
        ]
        fake_ordered = ('a', 'b', 'c')
        self.cls_proto._ordered_fields = fake_ordered

        old_pre = self.cls_proto._pre_raw
        old_post = self.cls_proto._post_raw

        def fake_fun(*args):
            return args[1], args[2]

        self.cls_proto._pre_raw = fake_fun
        self.cls_proto._post_raw = fake_fun

        p = self.cls_proto(fake_fields)

        # we can pass None for protocol_container and protocol_bits
        # because they are not being used here (see umit.umpa.Packet)
        assert p.get_raw(None, None) == (0x010101, 24)
        p.get_field('a').bits = 8
        p.a = 16
        assert p.get_raw(None, None) == (0x100101, 24)

        p.get_field('a').bits = 4
        py.test.raises(UMPAException, p.get_raw, None, None)

        self.cls_proto._pre_raw = old_pre
        self.cls_proto._post_raw = old_post
Ejemplo n.º 2
0
    def test_offset(self):
        fake_fields = [
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8)
        ]
        fake_ordered = ('a', 'b', 'c')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        assert p.get_offset('a') == 0
        assert p.get_offset('b') == 8
        assert p.get_offset('c') == 16

        fake_fields[1].bits = 10
        assert p.get_offset('a') == 0
        assert p.get_offset('b') == 8
        assert p.get_offset('c') == 18

        assert p.get_offset(fake_fields[0]) == 0
        assert p.get_offset(fake_fields[1]) == 8
        assert p.get_offset(fake_fields[2]) == 18

        fake_fields[1].bits = 8
        assert p.get_offset(fake_fields[0]) == 0
        assert p.get_offset(fake_fields[1]) == 8
        assert p.get_offset(fake_fields[2]) == 16

        py.test.raises(UMPAException, p.get_offset, 10)
        py.test.raises(UMPAException, p.get_offset, self.cls_proto)
        py.test.raises(UMPAException, p.get_offset, False)

        py.test.raises(UMPAAttributeException, p.get_offset, 'd')
        py.test.raises(UMPAAttributeException, 'p.get_offset(IntField("x"))')
Ejemplo n.º 3
0
    def test_set_fields(self):
        fake_fields = [
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8)
        ]
        fake_ordered = ('a', 'b', 'c')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        p.set_fields('a', 4, 'b', 0)
        assert p.a == 4
        assert p.b == 0
        p.set_fields('a', 10, c=20, b=4)
        assert p.a == 10
        assert p.b == 4
        assert p.c == 20

        py.test.raises(UMPAAttributeException, p.set_fields, 'a', 300)
        py.test.raises(UMPAAttributeException, p.set_fields, 'a', 1, c=300)
        py.test.raises(UMPAAttributeException, p.set_fields, c=300)
        py.test.raises(UMPAAttributeException, p.set_fields, a=1, c=300)

        py.test.raises(UMPAAttributeException, p.set_fields, 'd', 0)
        py.test.raises(UMPAAttributeException, p.set_fields, 'a')
Ejemplo n.º 4
0
    def test_post_raw(self):
        fake_fields = [
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8)
        ]
        fake_ordered = ('a', 'b', 'c')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        py.test.raises(NotImplementedError, p._post_raw, 1, 2, 3, 4)
Ejemplo n.º 5
0
    def test_get_field(self):
        fake_fields = [
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8)
        ]
        fake_ordered = ('a', 'b', 'c')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        for i, f in enumerate(fake_ordered):
            assert p.get_field(f) == fake_fields[i]
Ejemplo n.º 6
0
    def test_get_fields_keys(self):
        fake_fields = [
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8)
        ]
        fake_ordered = ('a', 'b', 'c')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        for i, f in enumerate(p.get_fields_keys()):
            assert f == fake_ordered[i]

        assert isinstance(p.get_fields_keys(), types.GeneratorType)
Ejemplo n.º 7
0
    def test_getattr(self):
        fake_fields = [IntField('foobar', 1, 8)]
        fake_ordered = ('a')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        assert p.a == 1
        py.test.raises(UMPAAttributeException, "p.d")
Ejemplo n.º 8
0
    def test_is_valid(self):
        fake_fields = [IntField('foobar', 1, 8)]
        fake_ordered = ('a')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        assert p._is_valid('a')
        assert not p._is_valid('b')
        assert not p._is_valid(None)
        assert not p._is_valid(1)
Ejemplo n.º 9
0
    def __init__(self, protocol_id, total_length):
        """
        Create a new PseudoHeader()

        @type protocol_id: C{int}
        @param protocol_id: id of the protocol which use PseudoHeader.

        @type total_length: C{int}
        @param total_length: length of the real header and payload.
        """

        fields_list = [
            IPv6AddrField("Source Address"),
            IPv6AddrField("Destination Address"),
            IntField("Upper layer length", total_length, bits=32),
            IntField("zero", 0, bits=24),
            IntField("Next Header ", protocol_id, bits=8)
        ]
        super(PseudoHeader6, self).__init__(fields_list)
Ejemplo n.º 10
0
    def __init__(self, protocol_id, total_length):
        """
        Create a new PseudoHeader()

        @type protocol_id: C{int}
        @param protocol_id: id of the protocol which use PseudoHeader.

        @type total_length: C{int}
        @param total_length: length of the real header and payload.
        """

        fields_list = [
            IPv4AddrField("Source Address"),
            IPv4AddrField("Destination Address"),
            IntField("Reserved", 0, bits=8),
            IntField("Protocol", protocol_id, bits=8),
            IntField("Total Length", total_length, bits=16)
        ]
        super(PseudoHeader, self).__init__(fields_list)
Ejemplo n.º 11
0
    def test_init(self):
        fake_fields = [
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8),
            IntField('foobar', 1, 8)
        ]
        fake_ordered = ('a', 'b', 'c')
        self.cls_proto._ordered_fields = ()
        p = self.cls_proto(fake_fields)
        assert p._fields == {}

        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)
        for i, k in enumerate(fake_ordered):
            assert p._fields[k] == fake_fields[i]

        p = self.cls_proto(fake_fields, a=100)
        assert p.a == 100
        assert p.b == 1
        assert p.c == 1

        assert p.get_field('a')._shortname == 'a'
Ejemplo n.º 12
0
    def test_get_flags(self):
        bits = ['x', 'y', 'z']
        fake_fields = [IntField('foobar', 1, 8), Flags('flags', bits, y=True)]
        fake_ordered = ('a', 'b')
        self.cls_proto._ordered_fields = fake_ordered
        p = self.cls_proto(fake_fields)

        assert p.get_flags('b', 'x', 'y') == [False, True]
        assert p.get_flags('b') == [False, True, False]

        py.test.raises(UMPAAttributeException, p.get_flags, 'a', 'x')
        py.test.raises(UMPAAttributeException, p.get_flags, 'b', 'g')
        py.test.raises(UMPAAttributeException, p.get_flags, 'b', 'x', 'g')