def test_terminate_on_illegal_message(): with create_upstream_context() as ctxt: msg_bytes = b"<Hermes Timestamp='2020-04-28T10:01:20.768'><ThisIsNotAKnownMessage /></Hermes>" ctxt.send_tag_and_bytes(None, msg_bytes) # other end has to close connection so check if socked is dead now, optionally a Notification can be sent before closing try: ctxt._socket.recv(0) uc.expect_message("Notification") ctxt.close() raise ValueError("illegal message erroneously accepted") except: # try the same after initial handshake ctxt.close() with create_upstream_context() as ctxt: ctxt.send_msg(Message.ServiceDescription("AcceptanceTest", 2)) ctxt.expect_message("ServiceDescription") ctxt.send_tag_and_bytes(None, msg_bytes) # other end has to close connection so check if socked is dead now, optionally a Notification can be sent before closing try: ctxt._socket.recv(0) uc.expect_message("Notification") ctxt.close() raise ValueError("illegal message erroneously accepted after handshake") except: pass
def test_multiple_messages_per_packet(): with create_upstream_context() as ctxt: check_alive = Message.CheckAlive() service_description = Message.ServiceDescription("DownstreamId", 1) msg_bytes = check_alive.to_bytes() + service_description.to_bytes() + check_alive.to_bytes() ctxt.send_tag_and_bytes(service_description.tag, msg_bytes) ctxt.expect_message("ServiceDescription")
def create_upstream_context_with_handshake(host = "localhost", port = 50101): uc = UpstreamConnection(log) try: uc.connect(host, port) uc.send_msg(Message.ServiceDescription("AcceptanceTest", 2)) uc.expect_message("ServiceDescription") yield uc uc.close() except: uc.close() raise
def test_maximum_message_size(): with create_upstream_context() as ctxt: msg = Message.ServiceDescription("DownstreamId", 1) msg_bytes = msg.to_bytes() splitat = msg_bytes.find(b"LaneId=") dummy_attr = b'HermesAcceptanceTestDummyAttributeId="" ' msg_bytes = msg_bytes[:splitat] + dummy_attr + msg_bytes[splitat:] splitat += len(dummy_attr) - 2 extend_by = MAXMESSAGE - len(msg_bytes) msg_bytes = msg_bytes[:splitat] + extend_by * b"x" + msg_bytes[splitat:] ctxt.send_tag_and_bytes(msg.tag, msg_bytes) ctxt.expect_message("ServiceDescription")
def create_upstream_context_with_board_available(host="localhost", port=50101): uc = UpstreamConnection(TestDecorator.log) try: uc.connect(host, port) uc.send_msg(Message.ServiceDescription("AcceptanceTest", 2)) uc.expect_message("ServiceDescription") uc.expect_message("BoardAvailable") yield uc uc.close() except: uc.close() raise
def test_terminate_on_unexpected_transport_finished(): with create_upstream_context() as ctxt: ctxt.send_msg(Message.ServiceDescription("AcceptanceTest", 2)) ctxt.expect_message("ServiceDescription") msg = Message.TransportFinished(TransferState.COMPLETE, "some_guid") ctxt.send_msg(msg) # other end has to close connection so check if socked is dead now, optionally a Notification can be sent before closing try: ctxt._socket.recv(0) uc.expect_message("Notification") ctxt.close() raise ValueError("TransportFinished erroneously accepted after handshake") except: pass
def test_terminate_on_unexpected_revoke_machine_ready(): with create_upstream_context() as ctxt: ctxt.send_msg(Message.ServiceDescription("AcceptanceTest", 2)) ctxt.expect_message("ServiceDescription") msg = Message.RevokeMachineReady() ctxt.send_msg(msg) # other end has to close connection so check if socked is dead now, optionally a Notification can be sent before closing try: ctxt._socket.recv(0) uc.expect_message("Notification") ctxt.close() raise ValueError("RevokeMachineReady erroneously accepted after handshake") except: pass
def test_complete_cycle(): with create_upstream_context() as ctxt: ctxt.send_msg(Message.ServiceDescription("AcceptanceTestCompleteCycle", 2)) ctxt.expect_message("ServiceDescription") msg = Message.MachineReady() ctxt.send_msg(msg) board_available = ctxt.expect_message("BoardAvailable") board_id = board_available.data.get("BoardId") msg = Message.StartTransport(board_id) ctxt.send_msg(msg) ctxt.expect_message("TransportFinished") msg = Message.StopTransport(TransferState.COMPLETE, board_id) ctxt.send_msg(msg) ctxt.close();
def connect_service_description_disconnect_n_times(): for _ in range(10): with create_upstream_context() as ctxt: ctxt.send_msg(Message.ServiceDescription("AcceptanceTest", 2))