def test_simple_struct(self): Data = struct.pack("<IB", 0xAABBCCDD, 0xEE) O = unpack(S0, Data) self.assertEqual(pack(S0, O), Data) Data = struct.pack("<IIB", 0xAABBCCDD, 0, 0xEE) O = unpack(S1, Data) self.assertEqual(pack(S1, O), Data)
def __getattr__(self, n): ty = self._ty attr_ty = ty.types.get(n, None) if attr_ty is None: raise KeyError("unknown attribute '%s'" % n) data = getattr(self, "_data") from purectypes.unpack import unpack return unpack(attr_ty, data[:attr_ty.size])
def test_array(self): Ref = (0, 1, 2, 3) O = pack(ArrayTy(UI64, 4), Ref) self.assertEqual(O, struct.pack("<QQQQ", *Ref)) Data = struct.pack("<IB", 0xAABBCCDD, 0xEE) O = unpack(S0, Data) P = pack(ArrayTy(S0, 2), [O, O]) self.assertEqual(len(P), 2 * 8)
def test_union0(self): Ref = [0xAA, 0xBB, 0xCC, 0xDD] Data = struct.pack("<BBBB", *Ref) O = unpack(U0, Data) self.assertEqual(O.ar, Ref) self.assertEqual(O.ip.a, 0xAA) self.assertEqual(O.ip.b, 0xBB) self.assertEqual(O.ip.c, 0xCC) self.assertEqual(O.ip.d, 0xDD) self.assertEqual(O.i32, 0xDDCCBBAA)
def test_union(self): Ref = [0xAA, 0xBB, 0xCC, 0xDD] Data = struct.pack("<BBBB", *Ref) self.assertEqual(pack(U0, Ref), Data) self.assertEqual(pack(U0, 0xDDCCBBAA), Data) self.assertEqual(pack(U0, unpack(SIpAddr, Data)), Data)
def test_ptr(self): self.assertEqual(unpack(PointerTy(UI8, "<Q"), struct.pack("<Q", 10)), 10)
def test_union1(self): Data = b"\xAA" * 10 O = unpack(U1, Data) self.assertEqual(O.ptr, 0xAAAAAAAAAAAAAAAA)
def test_array(self): Data = struct.pack("<QQQQ", 0, 1, 2, 3) O = unpack(ArrayTy(UI64, 4), Data) self.assertEqual(O, list(range(4)))
def test_ui(self): self.assertEqual(unpack(UI32, struct.pack("<I", 0xAABBCCDD)), 0xAABBCCDD) self.assertEqual(unpack(UI8, bytes((10, ))), 10)