Beispiel #1
0
 def test_unpack(self):
     self.assertEqual(S.unpack(S.DEFAULT_FMT,
         '\x01\x00\x00\x01\x00')[0][0], 2**32+2**8)
     self.assertEqual(list(S.unpack(S.ITEM_FMT,
         '\x00\x00\x04\x00\x00\x00\x00\x08')[0]), [0, 4, 8]) 
     self.assertEqual(list(S.unpack(S.FORMAT_FMT, 'u\x00\x00\x08')[0]), ['u', 8]) 
     self.assertEqual(''.join([a[0] for a in S.unpack(S.STR_FMT, 'abcde', cnt=4)]), 'abcd')
     self.assertEqual(''.join([a[0] for a in S.unpack(S.STR_FMT, 'abcde', cnt=-1)]), 'abcde')
Beispiel #2
0
    def test_iter_genpackets(self):
        heap = {0x1234: (S.DIRECTADDR, "abcdefgh"), S.HEAP_CNT_ID: (S.IMMEDIATEADDR, S.ADDRNULL)}
        pkts = [p for p in S.iter_genpackets(heap)]
        self.assertEqual(len(pkts), 1)
        pkt = pkts[0]
        self.assertEqual(list(S.unpack(S.HDR_FMT, pkt[:8])[0]), [S.MAGIC, S.VERSION, S.ITEMSIZE, S.ADDRSIZE, 0, 5])
        for i in range(1, 5):
            rv = S.unpack(S.RAW_ITEM_FMT, pkt[8 * i : 8 * i + 8])[0]
            mode, id = rv[:2]
            raw_val = "".join(rv[2:])
            if id == 0x1234:
                self.assertEqual(mode, S.DIRECTADDR)
                self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
            elif id == S.HEAP_CNT_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(raw_val, S.ADDRNULL)
            elif id == S.HEAP_LEN_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(raw_val, "\x00\x00\x00\x00\x08")
            elif id == S.PAYLOAD_LEN_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 8)
            elif id == S.PAYLOAD_OFF_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
            else:
                self.assertTrue(False)
        self.assertEqual(pkt[48:], "abcdefgh")

        heap[0x1234] = (S.DIRECTADDR, "abcdefgh" * 4000)
        pkts = [p for p in S.iter_genpackets(heap)]
        self.assertEqual(len(pkts), 4)
        payloads = []
        for cnt, pkt in enumerate(pkts):
            if cnt == 0:
                self.assertEqual(
                    list(S.unpack(S.HDR_FMT, pkt[:8])[0]), [S.MAGIC, S.VERSION, S.ITEMSIZE, S.ADDRSIZE, 0, 5]
                )
                for i in range(1, 5):
                    rv = S.unpack(S.RAW_ITEM_FMT, pkt[8 * i : 8 * i + 8])[0]
                    mode, id = rv[:2]
                    raw_val = "".join(rv[2:])
                    if id == 0x1234:
                        self.assertEqual(mode, S.DIRECTADDR)
                        self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
                    elif id == S.HEAP_CNT_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, S.ADDRNULL)
                    elif id == S.HEAP_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, "\x00\x00\x00\x7d\x00")
                    elif id == S.PAYLOAD_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], S.MAX_PACKET_LEN - S.ITEMLEN * 6)
                    elif id == S.PAYLOAD_OFF_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
                    else:
                        self.assertTrue(False)
                payloads.append(pkt[6 * S.ITEMLEN :])
            else:
                self.assertEqual(
                    list(S.unpack(S.HDR_FMT, pkt[:8])[0]), [S.MAGIC, S.VERSION, S.ITEMSIZE, S.ADDRSIZE, 0, 4]
                )
                for i in range(1, 4):
                    rv = S.unpack(S.RAW_ITEM_FMT, pkt[8 * i : 8 * i + 8])[0]
                    mode, id = rv[:2]
                    raw_val = "".join(rv[2:])
                    if id == S.HEAP_CNT_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, S.ADDRNULL)
                    elif id == S.HEAP_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, "\x00\x00\x00\x7d\x00")
                    elif id == S.PAYLOAD_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        if cnt < 3:
                            self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], S.MAX_PACKET_LEN - S.ITEMLEN * 5)
                    elif id == S.PAYLOAD_OFF_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(
                            S.unpack(S.DEFAULT_FMT, raw_val)[0][0],
                            (S.MAX_PACKET_LEN - S.ITEMLEN * 5) + (cnt - 1) * (S.MAX_PACKET_LEN - S.ITEMLEN * 4),
                        )
                    else:
                        self.assertTrue(False)
                payloads.append(pkt[5 * S.ITEMLEN :])
        heap = "".join(payloads)
        self.assertEqual(len(heap), len("abcdefgh" * 4000))
        self.assertEqual(heap, "abcdefgh" * 4000)
Beispiel #3
0
 def test_unpack(self):
     self.assertEqual(S.unpack(S.DEFAULT_FMT, "\x01\x00\x00\x01\x00")[0][0], 2 ** 32 + 2 ** 8)
     self.assertEqual(list(S.unpack(S.ITEM_FMT, "\x00\x00\x04\x00\x00\x00\x00\x08")[0]), [0, 4, 8])
     self.assertEqual(list(S.unpack(S.FORMAT_FMT, "u\x00\x00\x08")[0]), ["u", 8])
     self.assertEqual("".join([a[0] for a in S.unpack(S.STR_FMT, "abcde", cnt=4)]), "abcd")
     self.assertEqual("".join([a[0] for a in S.unpack(S.STR_FMT, "abcde", cnt=-1)]), "abcde")
Beispiel #4
0
    def test_iter_genpackets(self):
        heap = {0x1234: (S.DIRECTADDR,'abcdefgh'), S.HEAP_CNT_ID: (S.IMMEDIATEADDR, S.ADDRNULL)}
        pkts = [p for p in S.iter_genpackets(heap)]
        self.assertEqual(len(pkts), 1)
        pkt = pkts[0]
        self.assertEqual(list(S.unpack(S.HDR_FMT, pkt[:8])[0]), 
            [S.MAGIC, S.VERSION, S.ITEMSIZE, S.ADDRSIZE, 0, 5])
        for i in range(1,5):
            rv = S.unpack(S.RAW_ITEM_FMT, pkt[8*i:8*i+8])[0]
            mode, id = rv[:2]
            raw_val = ''.join(rv[2:])
            if id == 0x1234:
                self.assertEqual(mode, S.DIRECTADDR)
                self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
            elif id == S.HEAP_CNT_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(raw_val, S.ADDRNULL)
            elif id == S.HEAP_LEN_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(raw_val, '\x00\x00\x00\x00\x08')
            elif id == S.PAYLOAD_LEN_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 8)
            elif id == S.PAYLOAD_OFF_ID:
                self.assertEqual(mode, S.IMMEDIATEADDR)
                self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
            else: self.assertTrue(False)
        self.assertEqual(pkt[48:], 'abcdefgh')

        heap[0x1234] = (S.DIRECTADDR, 'abcdefgh' * 4000)
        pkts = [p for p in S.iter_genpackets(heap)]
        self.assertEqual(len(pkts), 4)
        payloads = []
        for cnt, pkt in enumerate(pkts):
            if cnt == 0:
                self.assertEqual(list(S.unpack(S.HDR_FMT, pkt[:8])[0]), 
                    [S.MAGIC, S.VERSION, S.ITEMSIZE, S.ADDRSIZE, 0, 5])
                for i in range(1,5):
                    rv = S.unpack(S.RAW_ITEM_FMT, pkt[8*i:8*i+8])[0]
                    mode, id = rv[:2]
                    raw_val = ''.join(rv[2:])
                    if id == 0x1234:
                        self.assertEqual(mode, S.DIRECTADDR)
                        self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
                    elif id == S.HEAP_CNT_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, S.ADDRNULL)
                    elif id == S.HEAP_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, '\x00\x00\x00\x7d\x00')
                    elif id == S.PAYLOAD_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], S.MAX_PACKET_LEN - S.ITEMLEN*6)
                    elif id == S.PAYLOAD_OFF_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], 0)
                    else: self.assertTrue(False)
                payloads.append(pkt[6*S.ITEMLEN:])
            else:
                self.assertEqual(list(S.unpack(S.HDR_FMT, pkt[:8])[0]), 
                    [S.MAGIC, S.VERSION, S.ITEMSIZE, S.ADDRSIZE, 0, 4])
                for i in range(1,4):
                    rv = S.unpack(S.RAW_ITEM_FMT, pkt[8*i:8*i+8])[0]
                    mode, id = rv[:2]
                    raw_val = ''.join(rv[2:])
                    if id == S.HEAP_CNT_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, S.ADDRNULL)
                    elif id == S.HEAP_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(raw_val, '\x00\x00\x00\x7d\x00')
                    elif id == S.PAYLOAD_LEN_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        if cnt < 3: self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], S.MAX_PACKET_LEN - S.ITEMLEN*5)
                    elif id == S.PAYLOAD_OFF_ID:
                        self.assertEqual(mode, S.IMMEDIATEADDR)
                        self.assertEqual(S.unpack(S.DEFAULT_FMT, raw_val)[0][0], (S.MAX_PACKET_LEN-S.ITEMLEN*5)+(cnt-1)*(S.MAX_PACKET_LEN-S.ITEMLEN*4))
                    else: self.assertTrue(False)
                payloads.append(pkt[5*S.ITEMLEN:])
        heap = ''.join(payloads)
        self.assertEqual(len(heap), len('abcdefgh' * 4000))
        self.assertEqual(heap, 'abcdefgh'*4000)