Esempio n. 1
0
    def getStr(self):
        s = ""
        for d in self.u.dataOut._ag.data:
            ch = valToInt(d)
            s += chr(ch)

        return s
Esempio n. 2
0
    def test_struct_packUnpack(self):
        f = self.create_ICMP_echo_frame()
        asBytes = iterBits(f, bitsInOne=8, skipPadding=False)
        asBytes = [valToInt(x) for x in asBytes]

        f_out = HdlValue_unpack(echoFrame_t, asBytes, dataWidth=8)

        self.assertEqual(f, f_out)

        _f = f
        f = f_out
        asBytes = iterBits(f, bitsInOne=8, skipPadding=False)
        asBytes = [valToInt(x) for x in asBytes]

        f_out = HdlValue_unpack(echoFrame_t, asBytes, dataWidth=8)

        self.assertEqual(_f, f_out)
Esempio n. 3
0
    def assertValEqual(self, first, second, msg=None):
        try:
            first = first.read()
        except AttributeError:
            pass

        if not isinstance(first, int) and first is not None:
            first = valToInt(first)

        return unittest.TestCase.assertEqual(self, first, second, msg=msg)
Esempio n. 4
0
    def getStr(self):
        START_BIT = 0
        STOP_BIT = 1
        s = ""
        d = iter(self.u.txd._ag.data)
        for bit in d:
            self.assertEqual(bit.vld_mask, 0b1)
            _bit = valToInt(bit)

            if _bit == START_BIT:
                ch = 0
                for i in range(10 - 1):
                    b = next(d)
                    self.assertEqual(b.vld_mask, 0b1)
                    _b = valToInt(b)
                    if i == 8:
                        self.assertEqual(_b, STOP_BIT)
                    else:
                        ch |= _b << i

                s = s + chr(ch)

        return s
Esempio n. 5
0
def pingResponder_model(packetStructVal):
    """
    Modify ICMP Echo Request to an ICMP Echo Reply packet.

    :param packet: struct val of packet
    """
    packet = iterBits(packetStructVal, bitsInOne=8, skipPadding=False)
    packet = [valToInt(p) for p in packet]
    eth = 0
    # swap eht addr
    (packet[(eth + 0):(eth + 6)],
     packet[(eth + 6):(eth + 12)]) = (packet[(eth + 6):(eth + 12)],
                                      packet[(eth + 0):(eth + 6)])

    ip = 2 * 6 + 2
    # Swap source and destination address.
    (packet[(ip + 12):(ip + 16)],
     packet[(ip + 16):(ip + 20)]) = (packet[(ip + 16):(ip + 20)],
                                     packet[(ip + 12):(ip + 16)])

    icmp = ip + 20
    # Change ICMP type code to Echo Reply (0).
    packet[icmp] = ICMP_TYPE.ECHO_REPLY

    # clean checksum
    packet[icmp + 2] = 0
    packet[icmp + 3] = 0

    # Calculate new  ICMP Checksum field.
    checksum = 0
    # for every 16-bit of the ICMP payload:
    for i in range(icmp, len(packet), 2):
        half_word = (packet[i] << 8) + (packet[i + 1])
        checksum += half_word
    # Get one's complement of the checksum.
    checksum = ~checksum & 0xffff
    # Put the new checksum back into the packet. (bigendian)
    packet[icmp + 2] = checksum >> 8
    packet[icmp + 3] = checksum & ((1 << 8) - 1)

    return bytes(packet)
Esempio n. 6
0
 def getStr(self):
     return "".join([chr(valToInt(d)) for d in self.u.dout._ag.data])
Esempio n. 7
0
 def test_dot_in_hw(self):
     dot_golden = [255, 255, 255, 255, 255, 0b10111111, 255, 255]
     a = addCharToBitmap()
     for i, item in enumerate(dot_golden):
         self.assertEqual(valToInt(a[ord(".") * 8 + i]), item, i)