def test_make_request(self): buf = bytearray(range(256)) seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) self.assertEqual(p, expected) # Now with data. data = b'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [ seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0 ] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) expected.extend(data) expected.extend([data_crc & 0xFF, data_crc >> 8]) self.assertEqual(p, expected)
def test_send_receive(self): event = threading.Event() buf, seq, action = array.array('B'), 5, stentura._OPEN request = stentura._make_request(array.array('B'), stentura._OPEN, seq) correct_response = make_response(seq, action) wrong_seq = make_response(seq - 1, action) wrong_action = make_response(seq, action + 1) bad_response = make_response(seq, action, data="foo", length=15) # Correct response first time. responses = [correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) self.assertSequenceEqual(response, correct_response) # Timeout once then correct response. responses = ['', correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf, timeout=0.001) self.assertSequenceEqual(response, correct_response) # Wrong sequence number then correct response. responses = [wrong_seq, correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) self.assertSequenceEqual(response, correct_response) # No correct responses. Also make sure max_retries is honored. max_tries = 6 responses = [''] * max_tries port = MockPacketPort(responses) with self.assertRaises(stentura._ConnectionLostException): stentura._send_receive(port, event, request, buf, max_tries, timeout=0.0001) self.assertEqual(max_tries, port.writes) # Wrong action. responses = [wrong_action] port = MockPacketPort(responses) with self.assertRaises(stentura._ProtocolViolationException): stentura._send_receive(port, event, request, buf) # Bad packet. responses = [bad_response] port = MockPacketPort(responses) with self.assertRaises(stentura._ProtocolViolationException): stentura._send_receive(port, event, request, buf) # Stopped. responses = [] event.set() port = MockPacketPort(responses) with self.assertRaises(stentura._StopException): stentura._send_receive(port, event, request, buf)
def test_send_receive(self): event = threading.Event() buf, seq, action = bytearray(256), 5, stentura._OPEN request = stentura._make_request(bytearray(256), stentura._OPEN, seq) correct_response = make_response(seq, action) wrong_seq = make_response(seq - 1, action) wrong_action = make_response(seq, action + 1) bad_response = make_response(seq, action, data=b"foo", length=15) # Correct response first time. responses = [correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) self.assertSequenceEqual(response, correct_response) # Timeout once then correct response. responses = [b'', correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) self.assertSequenceEqual(response, correct_response) # Wrong sequence number then correct response. responses = [wrong_seq, correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) self.assertSequenceEqual(response, correct_response) # No correct responses. Also make sure max_retries is honored. max_tries = 6 responses = [b''] * max_tries port = MockPacketPort(responses) with self.assertRaises(stentura._ConnectionLostException): stentura._send_receive(port, event, request, buf, max_tries) self.assertEqual(max_tries, port.writes) # Wrong action. responses = [wrong_action] port = MockPacketPort(responses) with self.assertRaises(stentura._ProtocolViolationException): stentura._send_receive(port, event, request, buf) # Bad packet. responses = [bad_response] port = MockPacketPort(responses) with self.assertRaises(stentura._ProtocolViolationException): stentura._send_receive(port, event, request, buf) # Stopped. responses = [''] event.set() port = MockPacketPort(responses) with self.assertRaises(stentura._StopException): stentura._send_receive(port, event, request, buf)
def test_send_receive(): event = threading.Event() buf, seq, action = bytearray(256), 5, stentura._OPEN request = stentura._make_request(bytearray(256), stentura._OPEN, seq) correct_response = make_response(seq, action) wrong_seq = make_response(seq - 1, action) wrong_action = make_response(seq, action + 1) bad_response = make_response(seq, action, data=b"foo", length=15) # Correct response first time. responses = [correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) assert response == correct_response # Timeout once then correct response. responses = [b'', correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) assert response == correct_response # Wrong sequence number then correct response. responses = [wrong_seq, correct_response] port = MockPacketPort(responses) response = stentura._send_receive(port, event, request, buf) assert response == correct_response # No correct responses. Also make sure max_retries is honored. max_tries = 6 responses = [b''] * max_tries port = MockPacketPort(responses) with pytest.raises(stentura._ConnectionLostException): stentura._send_receive(port, event, request, buf, max_tries) assert max_tries == port.writes # Wrong action. responses = [wrong_action] port = MockPacketPort(responses) with pytest.raises(stentura._ProtocolViolationException): stentura._send_receive(port, event, request, buf) # Bad packet. responses = [bad_response] port = MockPacketPort(responses) with pytest.raises(stentura._ProtocolViolationException): stentura._send_receive(port, event, request, buf) # Stopped. responses = [''] event.set() port = MockPacketPort(responses) with pytest.raises(stentura._StopException): stentura._send_receive(port, event, request, buf)
def test_make_request(self): buf = array.array('B') seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected]) # Now with data. data = 'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in data] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])
def test_make_request(): buf = bytearray(range(256)) seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) assert p == expected # Now with data. data = b'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = bytearray([1] + for_crc + [crc & 0xFF, crc >> 8]) expected.extend(data) expected.extend([data_crc & 0xFF, data_crc >> 8]) assert p == expected
def test_make_request(self): buf = array.array('B') seq = 2 action = stentura._OPEN p1, p2, p3, p4, p5 = 1, 2, 3, 4, 5 p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5) for_crc = [seq, 18, 0, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0] crc = stentura._crc(for_crc) expected = [1] + for_crc + [crc & 0xFF, crc >> 8] self.assertSequenceEqual(p, [chr(b) for b in expected]) # Now with data. data = 'Testing Testing 123' p = stentura._make_request(buf, action, seq, p1, p2, p3, p4, p5, data) length = 18 + len(data) + 2 for_crc = [ seq, length & 0xFF, length >> 8, action, 0, p1, 0, p2, 0, p3, 0, p4, 0, p5, 0 ] crc = stentura._crc(for_crc) data_crc = stentura._crc(data) expected = ([1] + for_crc + [crc & 0xFF, crc >> 8] + [ord(b) for b in data] + [data_crc & 0xFF, data_crc >> 8]) self.assertSequenceEqual(p, [chr(b) for b in expected])