Esempio n. 1
0
    def test_read_packet_fail(self):
        class MockPort(object):
            def __init__(self,
                         data_section_length=0,
                         set1=False,
                         set2=False,
                         give_too_much_data=False,
                         give_timeout=False):
                self._set1 = set1
                self._set2 = set2
                self._read1 = False
                self._read2 = False
                self.event = threading.Event()
                self._give_timeout = give_timeout
                self._data = ([1, 0, data_section_length + 4, 0] +
                              [0] * data_section_length)
                if give_too_much_data:
                    self._data.append(0)
                self._data = bytearray(self._data)

            def read(self, count):
                if not self._read1:
                    self._read1 = True
                    if self._set1:
                        self.event.set()
                elif not self._read2:
                    self._read2 = True
                    if self._set2:
                        self.event.set()
                else:
                    raise Exception("Already read data.")
                if self._give_timeout and len(self._data) == count:
                    # If read() returns less bytes what was requested,
                    # it indicates a timeout.
                    count -= 1
                requested_bytes = self._data[0:count]
                self._data = self._data[count:]
                return requested_bytes

        buf = bytearray()

        with self.assertRaises(stentura._StopException):
            port = MockPort(set1=True)
            stentura._read_packet(port, port.event, buf)

        with self.assertRaises(stentura._StopException):
            port = MockPort(data_section_length=30, set2=True)
            stentura._read_packet(port, port.event, buf)

        with self.assertRaises(stentura._TimeoutException):
            port = MockPort(give_timeout=True)
            stentura._read_packet(port, port.event, buf)

        with self.assertRaises(stentura._TimeoutException):
            port = MockPort(data_section_length=30, give_timeout=True)
            stentura._read_packet(port, port.event, buf)

        with self.assertRaises(stentura._ProtocolViolationException):
            port = MockPort(give_too_much_data=True)
            stentura._read_packet(port, port.event, buf)
Esempio n. 2
0
def test_read_packet_fail():

    class MockPort:

        def __init__(self, data_section_length=0, set1=False, set2=False,
                     give_too_much_data=False, give_timeout=False):
            self._set1 = set1
            self._set2 = set2
            self._read1 = False
            self._read2 = False
            self.event = threading.Event()
            self._give_timeout = give_timeout
            self._data = ([1, 0, data_section_length + 4, 0] +
                          [0] * data_section_length)
            if give_too_much_data:
                self._data.append(0)
            self._data = bytearray(self._data)

        def read(self, count):
            if not self._read1:
                self._read1 = True
                if self._set1:
                    self.event.set()
            elif not self._read2:
                self._read2 = True
                if self._set2:
                    self.event.set()
            else:
                raise Exception("Already read data.")
            if self._give_timeout and len(self._data) == count:
                # If read() returns less bytes what was requested,
                # it indicates a timeout.
                count -= 1
            requested_bytes = self._data[0:count]
            self._data = self._data[count:]
            return requested_bytes

    buf = bytearray()

    with pytest.raises(stentura._StopException):
        port = MockPort(set1=True)
        stentura._read_packet(port, port.event, buf)

    with pytest.raises(stentura._StopException):
        port = MockPort(data_section_length=30, set2=True)
        stentura._read_packet(port, port.event, buf)

    with pytest.raises(stentura._TimeoutException):
        port = MockPort(give_timeout=True)
        stentura._read_packet(port, port.event, buf)

    with pytest.raises(stentura._TimeoutException):
        port = MockPort(data_section_length=30, give_timeout=True)
        stentura._read_packet(port, port.event, buf)

    with pytest.raises(stentura._ProtocolViolationException):
        port = MockPort(give_too_much_data=True)
        stentura._read_packet(port, port.event, buf)
Esempio n. 3
0
    def test_read_packet_parts(self):
        class MockPort(object):
            def __init__(self, packet):
                self._times = 0
                self._results = {3: packet[0:2],
                                 5: packet[2:4],
                                 7: packet[4:8],
                                 9: packet[8:]}

            def inWaiting(self):
                self._times += 1
                if self._times in self._results:
                    return len(self._results[self._times])

            def read(self, count):
                result = self._results[self._times]
                if len(result) != count:
                    raise Exception("Wrong count.")
                return result

        packet = make_response(1, 2, 3, 4, 5)
        buf = array.array('B')
        port = MockPort(packet)
        event = threading.Event()
        response = stentura._read_packet(port, event, buf, 1)
        self.assertSequenceEqual(packet, response)
Esempio n. 4
0
    def test_read_packet_parts(self):
        class MockPort(object):
            def __init__(self, packet):
                self._times = 0
                self._results = {
                    3: packet[0:2],
                    5: packet[2:4],
                    7: packet[4:8],
                    9: packet[8:]
                }

            def inWaiting(self):
                self._times += 1
                if self._times in self._results:
                    return len(self._results[self._times])

            def read(self, count):
                result = self._results[self._times]
                if len(result) != count:
                    raise Exception("Wrong count.")
                return result

        packet = make_response(1, 2, 3, 4, 5)
        buf = array.array('B')
        port = MockPort(packet)
        event = threading.Event()
        response = stentura._read_packet(port, event, buf, 1)
        self.assertSequenceEqual(packet, response)
Esempio n. 5
0
    def test_read_packet_simple(self):
        class MockPort(object):
            def __init__(self, packet):
                self._packet = packet

            def read(self, count):
                requested_bytes = self._packet[0:count]
                self._packet = self._packet[count:]
                return requested_bytes

        buf = bytearray(256)
        for packet in [make_response(1, 2, 3, 4, 5),
                       make_response(1, 2, 3, 4, 5, b"hello")]:
            port = MockPort(packet)
            response = stentura._read_packet(port, threading.Event(), buf)
            self.assertSequenceEqual(response, packet)
Esempio n. 6
0
    def test_read_packet_simple(self):
        class MockPort:
            def __init__(self, packet):
                self._packet = packet

            def read(self, count):
                requested_bytes = self._packet[0:count]
                self._packet = self._packet[count:]
                return requested_bytes

        buf = bytearray(256)
        for packet in [make_response(1, 2, 3, 4, 5),
                       make_response(1, 2, 3, 4, 5, b"hello")]:
            port = MockPort(packet)
            response = stentura._read_packet(port, threading.Event(), buf)
            self.assertSequenceEqual(response, packet)
Esempio n. 7
0
    def test_read_packet_simple(self):
        class MockPort(object):
            def __init__(self, packet):
                self._packet = packet

            def inWaiting(self):
                return len(self._packet)

            def read(self, count):
                return packet

        buf = array.array('B')
        for packet in [make_response(1, 2, 3, 4, 5),
                       make_response(1, 2, 3, 4, 5, "hello")]:
            port = MockPort(packet)
            response = stentura._read_packet(port, threading.Event(), buf, 1)
            self.assertSequenceEqual(response, packet)
Esempio n. 8
0
    def test_read_packet_simple(self):
        class MockPort(object):
            def __init__(self, packet):
                self._packet = packet

            def inWaiting(self):
                return len(self._packet)

            def read(self, count):
                return packet

        buf = array.array('B')
        for packet in [
                make_response(1, 2, 3, 4, 5),
                make_response(1, 2, 3, 4, 5, "hello")
        ]:
            port = MockPort(packet)
            response = stentura._read_packet(port, threading.Event(), buf, 1)
            self.assertSequenceEqual(response, packet)
Esempio n. 9
0
    def test_read_packet_fail(self):
        class MockPort(object):
            def __init__(self, length1, length2=0, length=None, set1=False,
                         set2=False, wrong=False):
                self._length1 = length1
                self._length2 = length2
                self._set1 = set1
                self._set2 = set2
                self._read1 = False
                self._read2 = False
                self.event = threading.Event()
                self._wrong = wrong
                if not length:
                    length = length1 + length2
                self._data = [1, 0, length, 0] + ([0] * (length - 4))
                if wrong:
                    self._data.append(0)
                self._data = ''.join([chr(b) for b in self._data])

            def inWaiting(self):
                length = 0
                if not self._read1:
                    length = self._length1
                elif not self._read2:
                    length = self._length2
                if self._wrong:
                    length += 1
                return length

            def read(self, count):
                if not self._read1:
                    self._read1 = True
                    if self._set1:
                        self.event.set()
                    return buffer(self._data, 0, count)
                elif not self._read2:
                    self._read2 = True
                    if self._set2:
                        self.event.set()
                    return buffer(self._data, self._length1, count)
                raise Exception("Alread read data.")

        buf = array.array('B')

        with self.assertRaises(stentura._StopException):
            port = MockPort(3, set1=True)
            stentura._read_packet(port, port.event, buf, 1)

        with self.assertRaises(stentura._StopException):
            port = MockPort(6, 20, length=30, set2=True)
            stentura._read_packet(port, port.event, buf, 1)

        with self.assertRaises(stentura._TimeoutException):
            port = MockPort(3)
            stentura._read_packet(port, port.event, buf, 0.001)

        with self.assertRaises(stentura._TimeoutException):
            port = MockPort(6, 20, length=30)
            stentura._read_packet(port, port.event, buf, 0.001)

        with self.assertRaises(stentura._ProtocolViolationException):
            port = MockPort(18, wrong=True)
            stentura._read_packet(port, port.event, buf, 1)
Esempio n. 10
0
    def test_read_packet_fail(self):
        class MockPort(object):
            def __init__(self,
                         length1,
                         length2=0,
                         length=None,
                         set1=False,
                         set2=False,
                         wrong=False):
                self._length1 = length1
                self._length2 = length2
                self._set1 = set1
                self._set2 = set2
                self._read1 = False
                self._read2 = False
                self.event = threading.Event()
                self._wrong = wrong
                if not length:
                    length = length1 + length2
                self._data = [1, 0, length, 0] + ([0] * (length - 4))
                if wrong:
                    self._data.append(0)
                self._data = ''.join([chr(b) for b in self._data])

            def inWaiting(self):
                length = 0
                if not self._read1:
                    length = self._length1
                elif not self._read2:
                    length = self._length2
                if self._wrong:
                    length += 1
                return length

            def read(self, count):
                if not self._read1:
                    self._read1 = True
                    if self._set1:
                        self.event.set()
                    return buffer(self._data, 0, count)
                elif not self._read2:
                    self._read2 = True
                    if self._set2:
                        self.event.set()
                    return buffer(self._data, self._length1, count)
                raise Exception("Alread read data.")

        buf = array.array('B')

        with self.assertRaises(stentura._StopException):
            port = MockPort(3, set1=True)
            stentura._read_packet(port, port.event, buf, 1)

        with self.assertRaises(stentura._StopException):
            port = MockPort(6, 20, length=30, set2=True)
            stentura._read_packet(port, port.event, buf, 1)

        with self.assertRaises(stentura._TimeoutException):
            port = MockPort(3)
            stentura._read_packet(port, port.event, buf, 0.001)

        with self.assertRaises(stentura._TimeoutException):
            port = MockPort(6, 20, length=30)
            stentura._read_packet(port, port.event, buf, 0.001)

        with self.assertRaises(stentura._ProtocolViolationException):
            port = MockPort(18, wrong=True)
            stentura._read_packet(port, port.event, buf, 1)