def test_write_drain(name, data, config, io1str, io2str, timeout=3000, initial_write_io1=None, switch_delay=0.0): """Test that close does not loose data This function takes a name (for reporting), some data to transfer, a config file for ser2net, and writes the data and immediately closes the connection after the write succeeds and makes sure all the written data gets there. If initial_write_io1 is not None, the string is written to io1 when transferring from io2 to io1. This is a hack for UDP, ser2net will not be connected to the udp port until it receives some data from it. """ print("Write drain %s:\n config=%s io1=%s\n io2=%s" % (name, config, io1str, io2str)) ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str) try: print(" io1 to io2") utils.test_write_drain(io1, io2, data, timeout=timeout) finally: utils.finish_2_ser2net(ser2net, io1, io2) ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str) try: print(" io2 to io1") end = time.time() + switch_delay while (time.time() < end): gensio.waiter(o).wait_timeout(1, int(switch_delay / 5)) if initial_write_io1: io1.handler.set_write_data(initial_write_io1) io2.handler.set_compare(initial_write_io1) if io1.handler.wait_timeout(1000) == 0: raise Exception("%s: Timed out on dummy write completion" % io1.handler.name) if io2.handler.wait_timeout(1000) == 0: raise Exception("%s: Timed out on dummy read completion" % io1.handler.name) utils.test_write_drain(io2, io1, data, timeout=timeout) finally: utils.finish_2_ser2net(ser2net, io1, io2) print(" Success!") return
def test_ser2net_termios(name, handler, config, io1str, io2str): """Test the settings of ser2net termios Set up a ser2net daemon and two connections, call the given handler which will return a termios set. Then fetch the termios from io2 and make sure they match. """ print("termios %s:\n config=%s io1=%s\n io2=%s" % (name, config, io1str, io2str)) o = utils.o ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str) try: io1.handler.set_compare("12345") if (io1.handler.wait_timeout(1000) == 0): raise Exception("%s: %s: Timed out waiting for banner" % (name, io1.handler.name)) io1.read_cb_enable(True) io2.read_cb_enable(True) expected_termios = handler.op(io1, io2) io2_rem_termios = get_remote_termios(io2.remote_id()) c = compare_termios(expected_termios, io2_rem_termios) if (c != 0): raise Exception( "Termios mismatch at %d\nExpected: %s\nBut got %s" % (c, str(expected_termios), str(io2_rem_termios))) finally: utils.finish_2_ser2net(ser2net, io1, io2, handle_except=False) print(" Success!")
def test_transfer(name, data, config, io1str, io2str, timeout=1000, extra_args=""): """Test a transfer between two gensio objects This function takes a name (for reporting), some data to transfer, a config file for ser2net, and transfers the data one direction, then the other, then both ways at the same time. """ print("Transfer %s:\n config=%s io1=%s\n io2=%s" % (name, config, io1str, io2str)) ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str, extra_args=extra_args) try: print(" io1 to io2") utils.test_dataxfer(io1, io2, data, timeout=timeout) print(" io2 to io1") utils.test_dataxfer(io2, io1, data, timeout=timeout) print(" bidirectional") utils.test_dataxfer_simul(io1, io2, data, timeout=timeout) finally: utils.finish_2_ser2net(ser2net, io1, io2) print(" Success!") return
def test_one_xfer(name, data1, data2, config, io1str, io2str, timeout=1000, extra_args="", compare1=None, compare2=None): print(" " + name) ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str, extra_args=extra_args) try: if data1: utils.test_dataxfer(io1, io2, data1, timeout=timeout, compare=compare1) if data2: utils.test_dataxfer(io2, io1, data2, timeout=timeout, compare=compare2) finally: utils.finish_2_ser2net(ser2net, io1, io2) return
def test_connect_back(name, data, config, io1str, io2str, timeout=3000, extra_args=""): print("Connect back %s:\n config=%s io1=%s\n io2=%s" % (name, config, io1str, io2str)) ser2net, acc1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str, extra_args=extra_args, io1_is_accepter=True) try: # Send some data to start the connection. io2.handler.set_write_data(data) # Now wait for the connect back. if (acc1.handler.wait_timeout(timeout) == 0): raise Exception("%s: Timed out on connect back" % acc1.handler.name) acc1.handler.set_compare(data) if (acc1.handler.wait_timeout(timeout) == 0): raise Exception("%s: Timed out on connect back data" % acc1.handler.name) finally: utils.finish_2_ser2net(ser2net, acc1, io2) print(" Success!") return
def test_rts(): config = ("connection: &con", " accepter: telnet(rfc2217),tcp,3023", " connector: serialdev,/dev/ttyPipeA0,9600n81", " options:", " banner: 12345") io1str = "telnet(rfc2217),tcp,localhost,3023" io2str = "serialdev,/dev/ttyPipeB0,9600N81" print("serialdev rts rfc2217:\n config=%s io1=%s\n io2=%s" % (config, io1str, io2str)) o = utils.o ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str) sio1 = io1.cast_to_sergensio() io1.handler.set_compare("12345") if (io1.handler.wait_timeout(1000) == 0): raise Exception("%s: %s: Timed out waiting for banner" % (name, io1.handler.name)) io1.read_cb_enable(True); io2.read_cb_enable(True); set_remote_null_modem(utils.remote_id_int(io2), False); val = sio1.sg_rts_s(0) if (val != gensio.SERGENSIO_RTS_ON): raise Exception("Expected RTS on at start, got %d" % val); val = get_remote_modem_ctl(utils.remote_id_int(io2)) if (not (val & SERIALSIM_TIOCM_RTS)): raise Exception("Expected remote RTS on at start"); val = sio1.sg_rts_s(gensio.SERGENSIO_RTS_OFF) if (val != gensio.SERGENSIO_RTS_OFF): raise Exception("Expected RTS off"); val = get_remote_modem_ctl(utils.remote_id_int(io2)) if (val & SERIALSIM_TIOCM_RTS): raise Exception("Expected remote RTS off"); val = sio1.sg_rts_s(gensio.SERGENSIO_RTS_ON) if (val != gensio.SERGENSIO_RTS_ON): raise Exception("Expected RTS on"); val = get_remote_modem_ctl(utils.remote_id_int(io2)) if (not (val & SERIALSIM_TIOCM_RTS)): raise Exception("Expected remote RTS on"); set_remote_null_modem(utils.remote_id_int(io2), True); utils.finish_2_ser2net(ser2net, io1, io2, handle_except = False) print(" Success!") return
def test_dtr(): config = "BANNER:b:12345\n telnet(rfc2217),3023:raw:100:/dev/ttyPipeA0:b\n" io1str = "telnet(rfc2217),tcp,localhost,3023" io2str = "serialdev,/dev/ttyPipeB0,9600N81" print("serialdev dtr rfc2217:\n config=%s io1=%s\n io2=%s" % (config, io1str, io2str)) o = utils.o ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str) sio1 = io1.cast_to_sergensio() io1.handler.set_compare("12345") if (io1.handler.wait_timeout(1000) == 0): raise Exception("%s: %s: Timed out waiting for banner" % ("test dtr", io1.handler.name)) io1.read_cb_enable(True); io2.read_cb_enable(True); set_remote_null_modem(io2.remote_id(), False); val = sio1.sg_dtr_s(0) if (val != gensio.SERGENSIO_DTR_ON): raise Exception("Expected DTR on at start, got %d" % val); val = get_remote_modem_ctl(io2.remote_id()) if (not (val & SERIALSIM_TIOCM_DTR)): raise Exception("Expected remote DTR on at start"); val = sio1.sg_dtr_s(gensio.SERGENSIO_DTR_OFF) if (val != gensio.SERGENSIO_DTR_OFF): raise Exception("Expected DTR off"); val = get_remote_modem_ctl(io2.remote_id()) if (val & SERIALSIM_TIOCM_DTR): raise Exception("Expected remote DTR off"); val = sio1.sg_dtr_s(gensio.SERGENSIO_DTR_ON) if (val != gensio.SERGENSIO_DTR_ON): raise Exception("Expected DTR on"); val = get_remote_modem_ctl(io2.remote_id()) if (not (val & SERIALSIM_TIOCM_DTR)): raise Exception("Expected remote DTR on"); set_remote_null_modem(io2.remote_id(), True); utils.finish_2_ser2net(ser2net, io1, io2, handle_except = False) print(" Success!") return
def test_rts(): config = "BANNER:b:12345\n 3023:telnet:100:/dev/ttyPipeA0:b remctl\n" io1str = "telnet,tcp,localhost,3023" io2str = "termios,/dev/ttyPipeB0,9600N81" print("termios rts rfc2217:\n config=%s io1=%s\n io2=%s" % (config, io1str, io2str)) o = genio.alloc_genio_selector() ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str) sio1 = io1.cast_to_sergenio() sio2 = io2.cast_to_sergenio() io1.handler.set_compare("12345") if (io1.handler.wait_timeout(1000)): raise Exception("%s: %s: Timed out waiting for banner" % (name, io1.handler.name)) io1.read_cb_enable(True) io2.read_cb_enable(True) sio2.set_remote_null_modem(False) val = sio1.sg_rts_s(0) if (val != genio.SERGENIO_RTS_ON): raise Exception("Expected RTS on at start, got %d" % val) val = sio2.get_remote_modem_ctl() if (not (val & genio.SERGENIO_TIOCM_RTS)): raise Exception("Expected remote RTS on at start") val = sio1.sg_rts_s(genio.SERGENIO_RTS_OFF) if (val != genio.SERGENIO_RTS_OFF): raise Exception("Expected RTS off") val = sio2.get_remote_modem_ctl() if (val & genio.SERGENIO_TIOCM_RTS): raise Exception("Expected remote RTS off") val = sio1.sg_rts_s(genio.SERGENIO_RTS_ON) if (val != genio.SERGENIO_RTS_ON): raise Exception("Expected RTS on") val = sio2.get_remote_modem_ctl() if (not (val & genio.SERGENIO_TIOCM_RTS)): raise Exception("Expected remote RTS on") utils.finish_2_ser2net(ser2net, io1, io2, handle_except=False) print(" Success!") return
#!/usr/bin/python3 import gensio import utils from serialsim import * import tempfile import os print("Testing miscellaneous features") print(" kickolduser") ser2net, io1, io2 = utils.setup_2_ser2net( utils.o, ("connection: &con", " accepter: tcp,3023", " connector: serialdev,/dev/ttyPipeA0,9600N81", " options:", " kickolduser: true"), "tcp,localhost,3023", "serialdev,/dev/ttyPipeB0,9600N81") io3 = None try: io1.handler.set_expected_err("Remote end closed connection") io1.read_cb_enable(True) io3 = utils.alloc_io(utils.o, "tcp,localhost,3023") if io1.handler.wait_timeout(1000) == 0: raise Exception("kickolduser: remote end didn't close") finally: if io3 is not None: utils.io_close(io3) utils.finish_2_ser2net(ser2net, io1, io2) print(" multiple connections") ser2net, io1, io2 = utils.setup_2_ser2net( utils.o, ("connection: &con", " accepter: tcp,3023", " connector: serialdev,/dev/ttyPipeA0,9600N81", " options:",
def test_modemstate(): config = ("connection: &con", " accepter: telnet(rfc2217),tcp,3023", " connector: serialdev,/dev/ttyPipeA0,9600n81,local") io1str = "telnet(rfc2217),tcp,localhost,3023" io2str = "serialdev,/dev/ttyPipeB0,9600N81" print("serialdev modemstate rfc2217:\n config=%s io1=%s\n io2=%s" % (config, io1str, io2str)) o = utils.o ser2net, io1, io2 = utils.setup_2_ser2net(o, config, io1str, io2str, do_io1_open=False) set_remote_null_modem(io2.remote_id(), False) set_remote_modem_ctl(io2.remote_id(), (SERIALSIM_TIOCM_CAR | SERIALSIM_TIOCM_CTS | SERIALSIM_TIOCM_DSR | SERIALSIM_TIOCM_RNG) << 16) io1.handler.set_expected_modemstate(0) io1.open_s() io1.read_cb_enable(True) if (io1.handler.wait_timeout(2000) == 0): raise Exception("%s: %s: Timed out waiting for modemstate 1" % ("test dtr", io1.handler.name)) io2.read_cb_enable(True) io1.handler.set_expected_modemstate(gensio.SERGENSIO_MODEMSTATE_CD_CHANGED | gensio.SERGENSIO_MODEMSTATE_CD) set_remote_modem_ctl(io2.remote_id(), ((SERIALSIM_TIOCM_CAR << 16) | SERIALSIM_TIOCM_CAR)) if (io1.handler.wait_timeout(2000) == 0): raise Exception("%s: %s: Timed out waiting for modemstate 2" % ("test dtr", io1.handler.name)) io1.handler.set_expected_modemstate(gensio.SERGENSIO_MODEMSTATE_DSR_CHANGED | gensio.SERGENSIO_MODEMSTATE_CD | gensio.SERGENSIO_MODEMSTATE_DSR) set_remote_modem_ctl(io2.remote_id(), ((SERIALSIM_TIOCM_DSR << 16) | SERIALSIM_TIOCM_DSR)) if (io1.handler.wait_timeout(2000) == 0): raise Exception("%s: %s: Timed out waiting for modemstate 3" % ("test dtr", io1.handler.name)) io1.handler.set_expected_modemstate(gensio.SERGENSIO_MODEMSTATE_CTS_CHANGED | gensio.SERGENSIO_MODEMSTATE_CD | gensio.SERGENSIO_MODEMSTATE_DSR | gensio.SERGENSIO_MODEMSTATE_CTS) set_remote_modem_ctl(io2.remote_id(), ((SERIALSIM_TIOCM_CTS << 16) | SERIALSIM_TIOCM_CTS)) if (io1.handler.wait_timeout(2000) == 0): raise Exception("%s: %s: Timed out waiting for modemstate 4" % ("test dtr", io1.handler.name)) io1.handler.set_expected_modemstate(gensio.SERGENSIO_MODEMSTATE_RI_CHANGED | gensio.SERGENSIO_MODEMSTATE_CD | gensio.SERGENSIO_MODEMSTATE_DSR | gensio.SERGENSIO_MODEMSTATE_CTS | gensio.SERGENSIO_MODEMSTATE_RI) set_remote_modem_ctl(io2.remote_id(), ((SERIALSIM_TIOCM_RNG << 16) | SERIALSIM_TIOCM_RNG)) if (io1.handler.wait_timeout(2000) == 0): raise Exception("%s: %s: Timed out waiting for modemstate 5" % ("test dtr", io1.handler.name)) io1.handler.set_expected_modemstate( gensio.SERGENSIO_MODEMSTATE_RI_CHANGED | gensio.SERGENSIO_MODEMSTATE_CD_CHANGED | gensio.SERGENSIO_MODEMSTATE_DSR_CHANGED | gensio.SERGENSIO_MODEMSTATE_CTS_CHANGED) set_remote_modem_ctl(io2.remote_id(), (SERIALSIM_TIOCM_CAR | SERIALSIM_TIOCM_CTS | SERIALSIM_TIOCM_DSR | SERIALSIM_TIOCM_RNG) << 16) if (io1.handler.wait_timeout(2000) == 0): raise Exception("%s: %s: Timed out waiting for modemstate 6" % ("test dtr", io1.handler.name)) io1.handler.set_expected_modemstate( gensio.SERGENSIO_MODEMSTATE_CD_CHANGED | gensio.SERGENSIO_MODEMSTATE_DSR_CHANGED | gensio.SERGENSIO_MODEMSTATE_CTS_CHANGED | gensio.SERGENSIO_MODEMSTATE_CD | gensio.SERGENSIO_MODEMSTATE_DSR | gensio.SERGENSIO_MODEMSTATE_CTS) set_remote_null_modem(io2.remote_id(), True) if (io1.handler.wait_timeout(2000) == 0): raise Exception("%s: %s: Timed out waiting for modemstate 7" % ("test dtr", io1.handler.name)) utils.finish_2_ser2net(ser2net, io1, io2, handle_except=False) print(" Success!") return