Beispiel #1
0
 def __init__(self, data):
     """Constructor."""
     self.first_channel = c_uint8.from_buffer(data[:SIZEOF_U8]).value
     self.reg_extension_id = self.first_channel
     data = data[SIZEOF_U8:]
     self.num_channels = c_uint8.from_buffer(data[:SIZEOF_U8]).value
     self.reg_class = self.num_channels
     data = data[SIZEOF_U8:]
     self.max_power = c_int8.from_buffer(data[:SIZEOF_S8]).value
     self.coverage_class = c_uint8.from_buffer(data[:SIZEOF_U8]).value
     self.chans = self.ext = self
Beispiel #2
0
 def __init__(self, data):
     """Constructor."""
     self.first_channel = c_uint8.from_buffer(data[:SIZEOF_U8]).value
     self.reg_extension_id = self.first_channel
     data = data[SIZEOF_U8:]
     self.num_channels = c_uint8.from_buffer(data[:SIZEOF_U8]).value
     self.reg_class = self.num_channels
     data = data[SIZEOF_U8:]
     self.max_power = c_int8.from_buffer(data[:SIZEOF_S8]).value
     self.coverage_class = c_uint8.from_buffer(data[:SIZEOF_U8]).value
     self.chans = self.ext = self
Beispiel #3
0
def test_maximum():
    """Test maximum valid integer value."""
    assert 127 == c_byte.from_buffer(bytearray(c_byte(127))).value
    assert 2147483647 == c_int.from_buffer(bytearray(c_int(2147483647))).value
    assert 127 == c_int8.from_buffer(bytearray(c_int8(127))).value
    if sys.maxsize > 2**32:  # 64-bit.
        assert 9223372036854775807 == c_long.from_buffer(
            bytearray(c_long(9223372036854775807))).value
    else:
        assert 2147483647 == c_long.from_buffer(bytearray(
            c_long(2147483647))).value
    assert 9223372036854775807 == c_longlong.from_buffer(
        bytearray(c_longlong(9223372036854775807))).value
    assert 65535 == c_uint16.from_buffer(bytearray(c_uint16(65535))).value
    assert 4294967295 == c_uint32.from_buffer(bytearray(
        c_uint32(4294967295))).value
    assert 18446744073709551615 == c_uint64.from_buffer(
        bytearray(c_uint64(18446744073709551615))).value
    assert 255 == c_uint8.from_buffer(bytearray(c_uint8(255))).value
    assert 255 == c_ubyte.from_buffer(bytearray(c_ubyte(255))).value
    assert 4294967295 == c_uint.from_buffer(bytearray(
        c_uint(4294967295))).value
    if sys.maxsize > 2**32:  # 64-bit.
        assert 18446744073709551615 == c_ulong.from_buffer(
            bytearray(c_ulong(18446744073709551615))).value
    else:
        assert 4294967295 == c_ulong.from_buffer(bytearray(
            c_ulong(4294967295))).value
    assert 18446744073709551615 == c_ulonglong.from_buffer(
        bytearray(c_ulonglong(18446744073709551615))).value
    assert 65535 == c_ushort.from_buffer(bytearray(c_ushort(65535))).value
Beispiel #4
0
def test_from_buffer_large():
    """Test .from_buffer attribute."""
    data = b'\xb8~?./3\x02\x14\xb2\x89\xfc6A\xa4\x02\x05f\xdb\xb1\x04\xc3\x891\xad\x8cW\xcd\xda\x04A'
    assert -72 == c_byte.from_buffer(bytearray(data)).value
    assert 775913144 == c_int.from_buffer(bytearray(data)).value
    assert -72 == c_int8.from_buffer(bytearray(data)).value
    if sys.maxsize > 2**32:  # 64-bit.
        assert 1441771108444372664 == c_long.from_buffer(bytearray(data)).value
    else:
        assert 775913144 == c_long.from_buffer(bytearray(data)).value
    assert 1441771108444372664 == c_longlong.from_buffer(bytearray(data)).value
    assert 32440 == c_uint16.from_buffer(bytearray(data)).value
    assert 775913144 == c_uint32.from_buffer(bytearray(data)).value
    assert 1441771108444372664 == c_uint64.from_buffer(bytearray(data)).value
    assert 184 == c_uint8.from_buffer(bytearray(data)).value
    assert 184 == c_ubyte.from_buffer(bytearray(data)).value
    assert 775913144 == c_uint.from_buffer(bytearray(data)).value
    if sys.maxsize > 2**32:  # 64-bit.
        assert 1441771108444372664 == c_ulong.from_buffer(
            bytearray(data)).value
    else:
        assert 775913144 == c_ulong.from_buffer(bytearray(data)).value
    assert 1441771108444372664 == c_ulonglong.from_buffer(
        bytearray(data)).value
    assert 32440 == c_ushort.from_buffer(bytearray(data)).value
Beispiel #5
0
def nla_get_u8(nla):
    """Return value of 8 bit integer attribute as an int().

    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/attr.c#L574

    Positional arguments:
    nla -- 8 bit integer attribute (nlattr class instance).

    Returns:
    Payload as an int().
    """
    return int(c_uint8.from_buffer(nla_data(nla)[:SIZEOF_U8]).value)
Beispiel #6
0
def nla_get_u8(nla):
    """Return value of 8 bit integer attribute as an int().

    https://github.com/thom311/libnl/blob/libnl3_2_25/lib/attr.c#L574

    Positional arguments:
    nla -- 8 bit integer attribute (nlattr class instance).

    Returns:
    Payload as an int().
    """
    return int(c_uint8.from_buffer(nla_data(nla)[:SIZEOF_U8]).value)
Beispiel #7
0
def test_zero():
    """Test zero integer value."""
    assert 0 == c_byte.from_buffer(bytearray(c_byte())).value
    assert 0 == c_int.from_buffer(bytearray(c_int())).value
    assert 0 == c_int8.from_buffer(bytearray(c_int8())).value
    assert 0 == c_long.from_buffer(bytearray(c_long())).value
    assert 0 == c_longlong.from_buffer(bytearray(c_longlong())).value
    assert 0 == c_uint16.from_buffer(bytearray(c_uint16())).value
    assert 0 == c_uint32.from_buffer(bytearray(c_uint32())).value
    assert 0 == c_uint64.from_buffer(bytearray(c_uint64())).value
    assert 0 == c_uint8.from_buffer(bytearray(c_uint8())).value
    assert 0 == c_ubyte.from_buffer(bytearray(c_ubyte())).value
    assert 0 == c_uint.from_buffer(bytearray(c_uint())).value
    assert 0 == c_ulong.from_buffer(bytearray(c_ulong())).value
    assert 0 == c_ulonglong.from_buffer(bytearray(c_ulonglong())).value
    assert 0 == c_ushort.from_buffer(bytearray(c_ushort())).value
Beispiel #8
0
def test_middle():
    """Test with arbitrary positive value."""
    assert 123 == c_byte.from_buffer(bytearray(c_byte(123))).value
    assert 1234567890 == c_int.from_buffer(bytearray(c_int(1234567890))).value
    assert 123 == c_int8.from_buffer(bytearray(c_int8(123))).value
    assert 1234567890 == c_long.from_buffer(bytearray(c_long(1234567890))).value
    assert 1234567890123456789 == c_longlong.from_buffer(bytearray(c_longlong(1234567890123456789))).value
    assert 12345 == c_uint16.from_buffer(bytearray(c_uint16(12345))).value
    assert 1234567890 == c_uint32.from_buffer(bytearray(c_uint32(1234567890))).value
    assert 12345678901234567890 == c_uint64.from_buffer(bytearray(c_uint64(12345678901234567890))).value
    assert 123 == c_uint8.from_buffer(bytearray(c_uint8(123))).value
    assert 123 == c_ubyte.from_buffer(bytearray(c_ubyte(123))).value
    assert 1234567890 == c_uint.from_buffer(bytearray(c_uint(1234567890))).value
    assert 1234567890 == c_ulong.from_buffer(bytearray(c_ulong(1234567890))).value
    assert 12345678901234567890 == c_ulonglong.from_buffer(bytearray(c_ulonglong(12345678901234567890))).value
    assert 12345 == c_ushort.from_buffer(bytearray(c_ushort(12345))).value
Beispiel #9
0
def test_zero():
    """Test zero integer value."""
    assert 0 == c_byte.from_buffer(bytearray(c_byte())).value
    assert 0 == c_int.from_buffer(bytearray(c_int())).value
    assert 0 == c_int8.from_buffer(bytearray(c_int8())).value
    assert 0 == c_long.from_buffer(bytearray(c_long())).value
    assert 0 == c_longlong.from_buffer(bytearray(c_longlong())).value
    assert 0 == c_uint16.from_buffer(bytearray(c_uint16())).value
    assert 0 == c_uint32.from_buffer(bytearray(c_uint32())).value
    assert 0 == c_uint64.from_buffer(bytearray(c_uint64())).value
    assert 0 == c_uint8.from_buffer(bytearray(c_uint8())).value
    assert 0 == c_ubyte.from_buffer(bytearray(c_ubyte())).value
    assert 0 == c_uint.from_buffer(bytearray(c_uint())).value
    assert 0 == c_ulong.from_buffer(bytearray(c_ulong())).value
    assert 0 == c_ulonglong.from_buffer(bytearray(c_ulonglong())).value
    assert 0 == c_ushort.from_buffer(bytearray(c_ushort())).value
Beispiel #10
0
def test_maximum():
    """Test maximum valid integer value."""
    assert 127 == c_byte.from_buffer(bytearray(c_byte(127))).value
    assert 2147483647 == c_int.from_buffer(bytearray(c_int(2147483647))).value
    assert 127 == c_int8.from_buffer(bytearray(c_int8(127))).value
    if sys.maxsize > 2 ** 32:  # 64-bit.
        assert 9223372036854775807 == c_long.from_buffer(bytearray(c_long(9223372036854775807))).value
    else:
        assert 2147483647 == c_long.from_buffer(bytearray(c_long(2147483647))).value
    assert 9223372036854775807 == c_longlong.from_buffer(bytearray(c_longlong(9223372036854775807))).value
    assert 65535 == c_uint16.from_buffer(bytearray(c_uint16(65535))).value
    assert 4294967295 == c_uint32.from_buffer(bytearray(c_uint32(4294967295))).value
    assert 18446744073709551615 == c_uint64.from_buffer(bytearray(c_uint64(18446744073709551615))).value
    assert 255 == c_uint8.from_buffer(bytearray(c_uint8(255))).value
    assert 255 == c_ubyte.from_buffer(bytearray(c_ubyte(255))).value
    assert 4294967295 == c_uint.from_buffer(bytearray(c_uint(4294967295))).value
    if sys.maxsize > 2 ** 32:  # 64-bit.
        assert 18446744073709551615 == c_ulong.from_buffer(bytearray(c_ulong(18446744073709551615))).value
    else:
        assert 4294967295 == c_ulong.from_buffer(bytearray(c_ulong(4294967295))).value
    assert 18446744073709551615 == c_ulonglong.from_buffer(bytearray(c_ulonglong(18446744073709551615))).value
    assert 65535 == c_ushort.from_buffer(bytearray(c_ushort(65535))).value
Beispiel #11
0
def test_middle():
    """Test with arbitrary positive value."""
    assert 123 == c_byte.from_buffer(bytearray(c_byte(123))).value
    assert 1234567890 == c_int.from_buffer(bytearray(c_int(1234567890))).value
    assert 123 == c_int8.from_buffer(bytearray(c_int8(123))).value
    assert 1234567890 == c_long.from_buffer(bytearray(
        c_long(1234567890))).value
    assert 1234567890123456789 == c_longlong.from_buffer(
        bytearray(c_longlong(1234567890123456789))).value
    assert 12345 == c_uint16.from_buffer(bytearray(c_uint16(12345))).value
    assert 1234567890 == c_uint32.from_buffer(bytearray(
        c_uint32(1234567890))).value
    assert 12345678901234567890 == c_uint64.from_buffer(
        bytearray(c_uint64(12345678901234567890))).value
    assert 123 == c_uint8.from_buffer(bytearray(c_uint8(123))).value
    assert 123 == c_ubyte.from_buffer(bytearray(c_ubyte(123))).value
    assert 1234567890 == c_uint.from_buffer(bytearray(
        c_uint(1234567890))).value
    assert 1234567890 == c_ulong.from_buffer(bytearray(
        c_ulong(1234567890))).value
    assert 12345678901234567890 == c_ulonglong.from_buffer(
        bytearray(c_ulonglong(12345678901234567890))).value
    assert 12345 == c_ushort.from_buffer(bytearray(c_ushort(12345))).value
Beispiel #12
0
def test_from_buffer_large():
    """Test .from_buffer attribute."""
    data = b'\xb8~?./3\x02\x14\xb2\x89\xfc6A\xa4\x02\x05f\xdb\xb1\x04\xc3\x891\xad\x8cW\xcd\xda\x04A'
    assert -72 == c_byte.from_buffer(bytearray(data)).value
    assert 775913144 == c_int.from_buffer(bytearray(data)).value
    assert -72 == c_int8.from_buffer(bytearray(data)).value
    if sys.maxsize > 2 ** 32:  # 64-bit.
        assert 1441771108444372664 == c_long.from_buffer(bytearray(data)).value
    else:
        assert 775913144 == c_long.from_buffer(bytearray(data)).value
    assert 1441771108444372664 == c_longlong.from_buffer(bytearray(data)).value
    assert 32440 == c_uint16.from_buffer(bytearray(data)).value
    assert 775913144 == c_uint32.from_buffer(bytearray(data)).value
    assert 1441771108444372664 == c_uint64.from_buffer(bytearray(data)).value
    assert 184 == c_uint8.from_buffer(bytearray(data)).value
    assert 184 == c_ubyte.from_buffer(bytearray(data)).value
    assert 775913144 == c_uint.from_buffer(bytearray(data)).value
    if sys.maxsize > 2 ** 32:  # 64-bit.
        assert 1441771108444372664 == c_ulong.from_buffer(bytearray(data)).value
    else:
        assert 775913144 == c_ulong.from_buffer(bytearray(data)).value
    assert 1441771108444372664 == c_ulonglong.from_buffer(bytearray(data)).value
    assert 32440 == c_ushort.from_buffer(bytearray(data)).value
Beispiel #13
0
 def version(self):
     """Return version integer."""
     return c_uint8.from_buffer(self.bytearray[self._get_slicers(1)]).value
Beispiel #14
0
 def cmd(self):
     """Return command integer."""
     return c_uint8.from_buffer(self.bytearray[self._get_slicers(0)]).value