Пример #1
0
def test_tcp_tls_verify_server():
    """
    Test TCP TLS client server connection with verify certs for server not client
    """

    certDirPath = localTestCertDirPath()
    assert os.path.exists(certDirPath)

    serverKeyPath = os.path.join(certDirPath,
                                 'server_key.pem')  # local server private key
    serverCertPath = os.path.join(
        certDirPath, 'server_cert.pem')  # local server public cert
    clientCaPath = os.path.join(certDirPath,
                                'client.pem')  # remote client public cert

    clientKeyPath = os.path.join(certDirPath,
                                 'client_key.pem')  # local client private key
    clientCertPath = os.path.join(
        certDirPath, 'client_cert.pem')  # local client public cert
    serverCaPath = os.path.join(certDirPath,
                                'server.pem')  # remote server public cert

    assert os.path.exists(serverKeyPath)
    assert os.path.exists(serverCertPath)
    assert os.path.exists(clientCaPath)
    assert os.path.exists(clientKeyPath)
    assert os.path.exists(clientCertPath)
    assert os.path.exists(serverCaPath)

    serverCertCommonName = 'localhost'  # match hostname uses servers's cert commonname

    tymist = tyming.Tymist()
    with tcp.openServer(cls=tcp.ServerTls,
                    tymth=tymist.tymen(),
                    ha=("", 6101),
                    bs=16192,
                    keypath=serverKeyPath,
                    certpath=serverCertPath,
                    cafilepath=clientCaPath,
                    certify=ssl.CERT_NONE ,) as server, \
         tcp.openClient(cls=tcp.ClientTls,
                    tymth=tymist.tymen(),
                    ha=("127.0.0.1", 6101),
                    bs=16192,
                    certedhost=serverCertCommonName,
                    keypath=clientKeyPath,
                    certpath=clientCertPath,
                    cafilepath=serverCaPath,
                    certify=ssl.CERT_REQUIRED,
                    hostify=True,) as beta:

        assert server.opened == True
        assert server.eha == ('127.0.0.1', 6101)
        assert server.ha == ('0.0.0.0', 6101)

        assert beta.opened == True
        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False

        # Connect beta to server
        while not (beta.connected and len(server.ixes) >= 1):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.01)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()
        assert beta.ha == beta.cs.getpeername()

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername()
        assert ixBeta.cs.getpeername() == beta.cs.getsockname()
        assert ixBeta.ca == beta.ca
        assert ixBeta.ha == beta.ha

        msgOut = b"Beta sends to Server\n"
        beta.tx(msgOut)
        while not (not beta.txbs and ixBeta.rxbs):
            beta.serviceSends()
            server.serviceReceivesAllIx()
            time.sleep(0.01)

        time.sleep(0.05)
        server.serviceReceivesAllIx()

        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut
        ixBeta.clearRxbs()

        msgOut = b'Server sends to Beta\n'
        ixBeta.tx(msgOut)
        while not (not ixBeta.txbs and beta.rxbs):
            server.serviceSendsAllIx()
            beta.serviceReceives()
            time.sleep(0.01)

        msgIn = bytes(beta.rxbs)
        assert msgIn == msgOut
        beta.clearRxbs()

    assert beta.opened == False
    assert server.opened == False
    """Done Test"""
Пример #2
0
def test_client_auto_reconnect():
    """
    Test client auto reconnect when  .reconnectable
    """
    tymist = tyming.Tymist(tock=0.05)
    with tcp.openServer(tymth=tymist.tymen(), ha=("", 6101)) as server, \
         tcp.openClient(tymth=tymist.tymen(), tymeout=0.2, reconnectable=True,
                    ha=("127.0.0.1", 6101)) as beta:

        # close server
        server.close()
        assert server.opened == False

        assert beta.opened == True
        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False
        assert beta.reconnectable == True

        # attempt to connect beta to serve while server down (closed)
        while tymist.tyme <= 0.25:
            beta.serviceConnect()
            tymist.tick()
            time.sleep(0.05)

        assert beta.accepted == False
        assert beta.connected == False

        assert server.reopen() == True
        assert server.ha == ('0.0.0.0', 6101)
        assert server.eha == ('127.0.0.1', 6101)

        assert beta.ha == server.eha

        # attempt to connect beta to server while server up (opened)
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            tymist.tick()  # advances clients reconnect retry tymer
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()
        assert beta.ha == beta.cs.getpeername()
        assert server.eha == beta.ha

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername()
        assert ixBeta.cs.getpeername() == beta.cs.getsockname()
        assert ixBeta.ca == beta.ca
        assert ixBeta.ha == beta.ha

        msgOut = b"Beta sends to Server on reconnect"
        beta.tx(msgOut)
        while not ixBeta.rxbs and beta.txbs:
            beta.serviceSends()
            time.sleep(0.05)
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut
        index = len(ixBeta.rxbs)

    assert beta.opened == False
    assert server.opened == False
    """Done Test"""
Пример #3
0
def test_tcp_service():
    """
    Test Classes tcp service methods
    """
    tymist = tyming.Tymist()
    with tcp.openServer(tymth=tymist.tymen(),  ha=("", 6101)) as server, \
         tcp.openClient(tymth=tymist.tymen(),  ha=("127.0.0.1", 6101)) as beta:

        assert server.opened == True
        assert server.ha == ('0.0.0.0', 6101)
        assert server.eha == ('127.0.0.1', 6101)

        assert beta.opened == True
        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False

        # connect beta to server
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()
        assert beta.ha == beta.cs.getpeername() == server.eha

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername()
        assert ixBeta.cs.getpeername() == beta.cs.getsockname()
        assert ixBeta.ca == beta.ca
        assert ixBeta.ha == beta.ha

        msgOut1 = b"Beta sends to Server first"
        beta.tx(msgOut1)
        while not ixBeta.rxbs and beta.txbs:
            beta.serviceSends()
            time.sleep(0.05)
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut1
        offset = len(ixBeta.rxbs)  # offset into .rxbs of first message

        # send multiple additional messages
        msgOut2 = b"Beta sends to Server second"
        beta.tx(msgOut2)
        msgOut3 = b"Beta sends to Server third"
        beta.tx(msgOut3)
        while len(ixBeta.rxbs) < len(msgOut1) + len(msgOut2) + len(msgOut3):
            beta.serviceSends()
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut1 + msgOut2 + msgOut3
        ixBeta.clearRxbs()  # clear out the receive buffer

        # build message too big to fit in buffer
        size = beta.actualBufSizes()[0]
        msgOutBig = bytearray()
        count = 0
        while (len(msgOutBig) <= size * 4):
            msgOutBig.extend(b"%032x_" % (count))
            count += 1
        assert len(msgOutBig) >= size * 4

        beta.tx(msgOutBig)
        while len(ixBeta.rxbs) < len(msgOutBig):
            beta.serviceSends()
            time.sleep(0.05)
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        ixBeta.clearRxbs()
        assert msgIn == msgOutBig

        # send from server to beta
        msgOut = b"Server sends to Beta"
        ixBeta.tx(msgOut)
        while len(beta.rxbs) < len(msgOut):
            server.serviceSendsAllIx()
            beta.serviceReceives()
            time.sleep(0.05)
        msgIn = bytes(beta.rxbs)
        beta.clearRxbs()
        assert msgIn == msgOut

        # send big from server to beta
        ixBeta.tx(msgOutBig)
        while len(beta.rxbs) < len(msgOutBig):
            server.serviceSendsAllIx()
            time.sleep(0.05)
            beta.serviceReceives()
            time.sleep(0.05)
        msgIn = bytes(beta.rxbs)
        beta.clearRxbs()
        assert msgIn == msgOutBig

    assert beta.opened == False
    assert server.opened == False
    """Done Test"""
Пример #4
0
def test_tcp_basic():
    """
    Test the tcp connection between client and server

    client send from and receive to port is ephemeral
    server receive to and send from port is well known

    Server listens on ist well know  receive to and send from port

    So incoming to server.
        Source address is client host and client ephemeral port
        Destination address is server host and server well known port

    Each accept socket on server is a different duple of client source, server dest
        all the dest are the same but each source is differenct so can route
        based on the source.

    Server routes incoming packets to accept socket port. The routing uses
           the clients send from ephemeral port to do the routing to the
           correct accept socket. All the accept sockets have the same local
           port but a different remote IP host .
    The servers accept socket port is the well known port so still receives to
           and sends from its well know port.
    The server sends to and receives from the clients ephemeral port number.


    """
    tymist = tyming.Tymist()
    client = tcp.Client(tymth=tymist.tymen())
    assert client.tymeout == 0.0
    assert isinstance(client.tymer, tyming.Tymer)
    assert client.tymer.duration == client.tymeout

    assert client.ha == ('127.0.0.1', 56000)
    assert (client.host, client.port) == client.ha
    assert client.hostname == client.host
    assert client.cs == None
    assert client.ca == (None, None)
    assert client.accepted == False
    assert client.cutoff == False
    assert client.reconnectable == False
    assert client.opened == False

    assert client.bs == 8096
    assert isinstance(client.txbs, bytearray)
    assert isinstance(client.rxbs, bytearray)
    assert client.wl == None

    tymist = tyming.Tymist()
    with tcp.openClient(tymth=tymist.tymen(), tymeout=0.5) as client:
        assert client.tymeout == 0.5
        assert client.ha == ('127.0.0.1', 56000)
        assert client.opened == True
        assert client.accepted == False
        assert client.cutoff == False
        assert client.reconnectable == False

    assert client.opened == False
    assert client.accepted == False
    assert client.cutoff == False

    server = tcp.Server()
    assert server.tymeout == 1.0

    assert server.ha == ('', 56000)
    assert server.eha == ('127.0.0.1', 56000)
    assert server.opened == False

    assert server.bs == 8096
    assert isinstance(server.axes, deque)
    assert isinstance(server.ixes, dict)
    assert server.wl == None

    with tcp.openServer(tymth=tymist.tymen(), tymeout=1.5) as server:
        assert server.ha == ('0.0.0.0', 56000)
        assert server.eha == ('127.0.0.1', 56000)
        assert server.opened == True

    assert server.opened == False

    tymist = tyming.Tymist()
    with tcp.openServer(tymth=tymist.tymen(), ha=("", 6101)) as server, \
         tcp.openClient(tymth=tymist.tymen(), ha=("127.0.0.1", 6101)) as beta, \
         tcp.openClient(tymth=tymist.tymen(), ha=("127.0.0.1", 6101)) as gamma:

        assert server.opened == True
        assert beta.opened == True
        assert gamma.opened == True

        assert server.ha == ('0.0.0.0', 6101)  # listen interface
        assert server.eha == ('127.0.0.1', 6101
                              )  # normalized listen/accept external interface
        assert beta.ha == (
            '127.0.0.1', 6101
        )  # server listen/accept maybe sha  (server host address)

        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False

        assert gamma.accepted == False
        assert gamma.connected == False
        assert gamma.cutoff == False

        #  connect beta to server
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()  # local connection address
        assert beta.ha == beta.cs.getpeername()  # remote connection address
        assert server.eha == beta.ha  # server external, beta external for server

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername(
        )  # ixBeta local beta remote
        assert ixBeta.cs.getpeername() == beta.cs.getsockname(
        )  # ixBeta remote beta local
        assert ixBeta.ca == beta.ca == ixBeta.cs.getpeername()
        assert ixBeta.ha == beta.ha == ixBeta.cs.getsockname()

        msgOut = b"Beta sends to Server"
        count = beta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgOut == msgIn

        # receive without sending
        msgIn = ixBeta.receive()
        assert msgIn is None

        # send multiple
        msgOut1 = b"First Message"
        count = beta.send(msgOut1)
        assert count == len(msgOut1)
        msgOut2 = b"Second Message"
        count = beta.send(msgOut2)
        assert count == len(msgOut2)
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgIn == msgOut1 + msgOut2

        # send from server to beta
        msgOut = b"Server sends to Beta"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = beta.receive()
        assert msgOut == msgIn

        # receive without sending
        msgIn = beta.receive()
        assert msgIn is None

        # build message too big to fit in buffer
        size = beta.actualBufSizes()[0]
        msgOut = bytearray()
        count = 0
        while (len(msgOut) <= size * 4):
            msgOut.extend(b"%032x_" % (count))  #  need to fix this
            count += 1
        assert len(msgOut) >= size * 4

        msgIn = bytearray()
        txbs = bytearray(msgOut)  # make copy
        size = 0
        while len(msgIn) < len(msgOut):
            #if size < len(msgOut):
            #size += beta.send(msgOut[size:])
            count = beta.send(txbs)
            del txbs[:count]
            size += count
            time.sleep(0.05)
            msgIn.extend(ixBeta.receive())
        assert size == len(msgOut)
        assert msgOut == msgIn

        #  gamma to server
        while not (gamma.connected and gamma.ca in server.ixes):
            gamma.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert gamma.accepted == True
        assert gamma.connected == True
        assert gamma.cutoff == False
        assert gamma.ca == gamma.cs.getsockname()
        assert gamma.ha == gamma.cs.getpeername()
        assert server.eha, gamma.ha
        ixGamma = server.ixes[gamma.ca]
        assert ixGamma.cs.getsockname() == gamma.cs.getpeername()
        assert ixGamma.cs.getpeername() == gamma.cs.getsockname()
        assert ixGamma.ca == gamma.ca
        assert ixGamma.ha == gamma.ha

        msgOut = b"Gamma sends to Server"
        count = gamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert msgOut == msgIn

        # receive without sending
        msgIn = ixGamma.receive()
        assert msgIn is None

        # send from server to gamma
        msgOut = b"Server sends to Gamma"
        count = ixGamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgOut == msgIn

        # recieve without sending
        msgIn = gamma.receive()
        assert msgIn is None

        # close beta and then attempt to send
        beta.close()
        msgOut = b"Beta send on closed socket"
        with pytest.raises(AttributeError):
            count = beta.send(msgOut)

        # attempt to receive on closed socket
        with pytest.raises(AttributeError):
            msgIn = beta.receive()

        # read on server after closed beta
        msgIn = ixBeta.receive()
        assert msgIn == b''

        # send on server after closed beta
        msgOut = b"Servers sends to Beta after close"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut)  #apparently works

        # close ixBeta manually
        ixBeta.close()
        del server.ixes[ixBeta.ca]
        time.sleep(0.05)
        #after close no socket .cs so can't receive
        with pytest.raises(AttributeError):
            msgIn = ixBeta.receive()
        assert ixBeta.cutoff == True

        # send on gamma to servver first then shutdown gamma sends
        msgOut = b"Gamma sends to server"
        count = gamma.send(msgOut)
        assert count == len(msgOut)
        gamma.shutdownSend()
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert msgOut == msgIn  # send before shutdown worked
        msgIn = ixGamma.receive()
        assert msgIn == b''  # gamma shutdown detected, not None
        assert ixGamma.cutoff == True

        # send from server to gamma first  then shutdown server send
        msgOut = b"Server sends to Gamma"
        count = ixGamma.send(msgOut)
        assert count == len(msgOut)
        ixGamma.shutdown()  # shutdown server connection to gamma
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgOut == msgIn
        msgIn = gamma.receive()
        if 'linux' in sys.platform:
            assert msgIn == b''  # server shutdown detected not None
            assert gamma.cutoff == True
        else:
            assert msgIn == None  # server shutdown not detected
            assert gamma.cutoff == False
        time.sleep(0.05)
        msgIn = gamma.receive()
        if 'linux' in sys.platform:
            assert msgIn == b''  # server shutdown detected not None
            assert gamma.cutoff == True
        else:
            assert msgIn == None  # server shutdown not detected
            assert gamma.cutoff == False

        ixGamma.close()  # close server connection to gamma
        del server.ixes[ixGamma.ca]
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgIn == b''  # server close is detected
        assert gamma.cutoff == True

        # reopen beta
        assert beta.reopen() == True
        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False

        # reconnect beta to server
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()
        assert beta.ha == beta.cs.getpeername()
        assert server.eha == beta.ha

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername()
        assert ixBeta.cs.getpeername() == beta.cs.getsockname()
        assert ixBeta.ca == beta.ca
        assert ixBeta.ha == beta.ha

        msgOut = b"Beta sends to server"
        count = beta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgOut == msgIn

        # send from server to beta
        msgOut = b"Server sends to Beta"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = beta.receive()
        assert msgOut == msgIn

        # send from server to beta then shutdown sever and attempt to send again
        msgOut1 = b"Server sends to Beta"
        count = ixBeta.send(msgOut)
        assert count == len(msgOut1)
        ixBeta.shutdownSend()
        msgOut2 = b"Server send again after server shutdowns socket"
        with pytest.raises(OSError) as ex:
            count = ixBeta.send(msgOut)
        assert ex.typename == 'BrokenPipeError'
        time.sleep(0.05)
        msgIn = beta.receive()
        assert msgOut1 == msgIn
        msgIn = beta.receive()
        assert msgIn == b''  # beta detects shutdown socket
        assert beta.cutoff == True

        # send from beta to server then shutdown beta
        msgOut = b"Beta sends to server"
        count = beta.send(msgOut)
        assert count == len(msgOut)
        beta.shutdown()
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgOut == msgIn
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        if 'linux' in sys.platform:
            assert ixBeta.cutoff == True
            assert msgIn == b''  # server does detect shutdown
        else:
            assert ixBeta.cutoff == False
            assert msgIn == None  # server does not detect shutdown
        beta.close()
        time.sleep(0.05)
        msgIn = ixBeta.receive()
        assert msgIn == b''  # server detects closed socket
        ixBeta.close()
        del server.ixes[ixBeta.ca]

        # reopen gamma
        assert gamma.reopen() == True
        assert gamma.accepted == False
        assert gamma.connected == False
        assert gamma.cutoff == False
        # reconnect gamma to server
        while not (gamma.connected and gamma.ca in server.ixes):
            gamma.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert gamma.accepted == True
        assert gamma.connected == True
        assert gamma.cutoff == False
        assert gamma.ca == gamma.cs.getsockname()
        assert gamma.ha == gamma.cs.getpeername()
        assert server.eha == gamma.ha

        ixGamma = server.ixes[gamma.ca]
        assert ixGamma.cs.getsockname() == gamma.cs.getpeername()
        assert ixGamma.cs.getpeername() == gamma.cs.getsockname()
        assert ixGamma.ca == gamma.ca
        assert ixGamma.ha == gamma.ha

        msgOut = b"Gamma sends to server"
        count = gamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert msgOut == msgIn

        # close both sides and reopen Gamma
        gamma.close()
        time.sleep(0.05)
        msgIn = ixGamma.receive()
        assert ixGamma.cutoff == True  # closed on other end
        assert msgIn == b''  # server detects close
        ixGamma.close()
        del server.ixes[ixGamma.ca]

        # reopen gamma
        assert gamma.reopen() == True
        assert gamma.accepted == False
        assert gamma.connected == False
        assert gamma.cutoff == False

        # reconnect gamma to server
        while not (gamma.connected and gamma.ca in server.ixes):
            gamma.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert gamma.accepted == True
        assert gamma.connected == True
        assert gamma.cutoff == False
        assert gamma.ca == gamma.cs.getsockname()
        assert gamma.ha == gamma.cs.getpeername()
        assert server.eha == gamma.ha

        ixGamma = server.ixes[gamma.ca]
        assert ixGamma.cs.getsockname() == gamma.cs.getpeername()
        assert ixGamma.cs.getpeername() == gamma.cs.getsockname()
        assert ixGamma.ca == gamma.ca
        assert ixGamma.ha == gamma.ha

        # send from server to gamma
        msgOut = b"Server sends to Gamma"
        count = ixGamma.send(msgOut)
        assert count == len(msgOut)
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgOut == msgIn

        ixGamma.close()
        del server.ixes[ixGamma.ca]
        time.sleep(0.05)
        msgIn = gamma.receive()
        assert msgIn == b''  # gamma detects close
        assert gamma.cutoff == True

    assert beta.opened == False
    assert gamma.opened == False
    assert server.opened == False
    """Done Test"""
Пример #5
0
def test_requester_respondent_echo():
    """
    Test basic nonblocking request response with
    client Requester class and client Respondent class
    use manual echo server
    """
    # Test tcp connection
    with tcp.openServer(port=6101, bufsize=131072) as alpha:
        assert alpha.ha == ('0.0.0.0', 6101)
        assert alpha.eha == ('127.0.0.1', 6101)

        with tcp.openClient(
                ha=alpha.eha,
                bufsize=131072,
        ) as beta:
            assert not beta.accepted
            assert not beta.connected
            assert not beta.cutoff

            while True:
                beta.serviceConnect()
                alpha.serviceConnects()
                if beta.connected and beta.ca in alpha.ixes:
                    break
                time.sleep(0.05)

            assert beta.accepted
            assert beta.connected
            assert not beta.cutoff
            assert beta.ca == beta.cs.getsockname()
            assert beta.ha == beta.cs.getpeername()
            assert alpha.eha == beta.ha

            ixBeta = alpha.ixes[beta.ca]
            assert ixBeta.ca is not None
            assert ixBeta.cs is not None
            assert ixBeta.cs.getsockname() == beta.cs.getpeername()
            assert ixBeta.cs.getpeername() == beta.cs.getsockname()
            assert ixBeta.ca == beta.ca
            assert ixBeta.ha == beta.ha

            # build http request
            host = '127.0.0.1'
            port = 6101
            method = 'GET'
            path = '/echo?name=fame'
            # GET /echo?name=fame from 127.0.0.1:6101
            headers = dict([('Accept', 'application/json')])
            request = clienting.Requester(hostname=host,
                                          port=port,
                                          method=method,
                                          path=path,
                                          headers=headers)
            betaMsgOut = request.rebuild()
            assert request.lines == [
                b'GET /echo?name=fame HTTP/1.1', b'Host: 127.0.0.1:6101',
                b'Accept-Encoding: identity', b'Accept: application/json', b'',
                b''
            ]

            assert request.head == betaMsgOut  # only headers no body
            assert request.head == (
                b'GET /echo?name=fame HTTP/1.1\r\nHost: 127.0.0.1:6101\r\nAccept-Encoding: ide'
                b'ntity\r\nAccept: application/json\r\n\r\n')
            assert betaMsgOut == (
                b'GET /echo?name=fame HTTP/1.1\r\nHost: 127.0.0.1:6101\r\nAccept-Encoding: ide'
                b'ntity\r\nAccept: application/json\r\n\r\n')

            # Beta sends to Alpha
            beta.tx(betaMsgOut)
            while beta.txbs and not ixBeta.rxbs:
                beta.serviceSends()
                time.sleep(0.05)
                alpha.serviceReceivesAllIx()
                time.sleep(0.05)
            alphaMsgIn = bytes(ixBeta.rxbs)
            assert alphaMsgIn == betaMsgOut
            ixBeta.clearRxbs()

            # Alpha responds to Beta
            alphaMsgOut = b'HTTP/1.1 200 OK\r\nContent-Length: 122\r\nContent-Type: application/json\r\nDate: Thu, 30 Apr 2015 19:37:17 GMT\r\nServer: IoBook.local\r\n\r\n{"content": null, "query": {"name": "fame"}, "verb": "GET", "url": "http://127.0.0.1:8080/echo?name=fame", "action": null}'
            ixBeta.tx(alphaMsgOut)
            while ixBeta.txbs or not beta.rxbs:
                alpha.serviceSendsAllIx()
                time.sleep(0.05)
                beta.serviceReceives()
                time.sleep(0.05)
            betaMsgIn = bytes(beta.rxbs)
            assert betaMsgIn == alphaMsgOut

            response = clienting.Respondent(msg=beta.rxbs, method=method)
            while response.parser:
                response.parse()
            assert not beta.rxbs  # fully extracted

            assert list(response.headers.items()) == [
                ('Content-Length', '122'),
                ('Content-Type', 'application/json'),
                ('Date', 'Thu, 30 Apr 2015 19:37:17 GMT'),
                ('Server', 'IoBook.local')
            ]

            assert response.body == (
                b'{"content": null, "query": {"name": "fame"}, "verb": "GET", "url'
                b'": "http://127.0.0.1:8080/echo?name=fame", "action": null}')

            response.dictify()  # converts response.data to dict()

            assert response.data == {
                'action': None,
                'content': None,
                'query': {
                    'name': 'fame'
                },
                'url': 'http://127.0.0.1:8080/echo?name=fame',
                'verb': 'GET'
            }
    """End Test"""
Пример #6
0
def test_tcp_service_wired():
    """
    Test Classes tcp service methods
    """
    # wire log everything to same file filed not buffered
    tymist = tyming.Tymist()
    with wiring.openWL(samed=True, filed=True) as wl, \
         tcp.openServer(tymth=tymist.tymen(),  ha=("", 6101), wl=wl) as server, \
         tcp.openClient(tymth=tymist.tymen(),  ha=("127.0.0.1", 6101), wl=wl) as beta:

        assert isinstance(wl, wiring.WireLog)
        assert wl.rxed is True
        assert wl.txed is True
        assert wl.samed is True
        assert wl.filed is True
        assert wl.name == "test"
        assert wl.temp == True
        assert wl.rxl is wl.txl
        assert wl.dirPath
        assert wl.opened

        assert server.opened == True
        assert server.ha == ('0.0.0.0', 6101)
        assert server.eha == ('127.0.0.1', 6101)
        assert server.wl == wl

        assert beta.opened == True
        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False
        assert beta.wl == wl

        # connect beta to server
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()
        assert beta.ha == beta.cs.getpeername() == server.eha

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername()
        assert ixBeta.cs.getpeername() == beta.cs.getsockname()
        assert ixBeta.ca == beta.ca
        assert ixBeta.ha == beta.ha

        msgOut1 = b"Beta sends to Server first"
        beta.tx(msgOut1)
        while not ixBeta.rxbs and beta.txbs:
            beta.serviceSends()
            time.sleep(0.05)
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut1
        offset = len(ixBeta.rxbs)  # offset into .rxbs of first message

        # send multiple additional messages
        msgOut2 = b"Beta sends to Server second"
        beta.tx(msgOut2)
        msgOut3 = b"Beta sends to Server third"
        beta.tx(msgOut3)
        while len(ixBeta.rxbs) < len(msgOut1) + len(msgOut2) + len(msgOut3):
            beta.serviceSends()
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut1 + msgOut2 + msgOut3
        ixBeta.clearRxbs()  # clear out the receive buffer

        # send from server to beta
        msgOut = b"Server sends to Beta"
        ixBeta.tx(msgOut)
        while len(beta.rxbs) < len(msgOut):
            server.serviceSendsAllIx()
            beta.serviceReceives()
            time.sleep(0.05)
        msgIn = bytes(beta.rxbs)
        beta.clearRxbs()
        assert msgIn == msgOut

        readrx = wl.readRx()
        readtx = wl.readTx()
        assert readrx == readtx
        assert len(readrx) == 357


    assert beta.opened == False
    assert server.opened == False
    assert wl.rxl.closed
    assert wl.txl.closed
    assert not wl.opened
    assert not os.path.exists(wl.dirPath)

    # wire log everything to same buffer not filed
    tymist = tyming.Tymist()
    with wiring.openWL(samed=True) as wl, \
         tcp.openServer(tymth=tymist.tymen(),  ha=("", 6101), wl=wl) as server, \
         tcp.openClient(tymth=tymist.tymen(),  ha=("127.0.0.1", 6101), wl=wl) as beta:

        assert isinstance(wl, wiring.WireLog)
        assert wl.rxed is True
        assert wl.txed is True
        assert wl.samed is True
        assert wl.filed is False
        assert wl.name == "test"
        assert wl.temp == True
        assert wl.rxl is wl.txl
        assert wl.dirPath is None  # filed == False
        assert wl.opened

        assert server.opened == True
        assert server.ha == ('0.0.0.0', 6101)
        assert server.eha == ('127.0.0.1', 6101)
        assert server.wl == wl

        assert beta.opened == True
        assert beta.accepted == False
        assert beta.connected == False
        assert beta.cutoff == False
        assert beta.wl == wl

        # connect beta to server
        while not (beta.connected and beta.ca in server.ixes):
            beta.serviceConnect()
            server.serviceConnects()
            time.sleep(0.05)

        assert beta.accepted == True
        assert beta.connected == True
        assert beta.cutoff == False
        assert beta.ca == beta.cs.getsockname()
        assert beta.ha == beta.cs.getpeername() == server.eha

        ixBeta = server.ixes[beta.ca]
        assert ixBeta.cs.getsockname() == beta.cs.getpeername()
        assert ixBeta.cs.getpeername() == beta.cs.getsockname()
        assert ixBeta.ca == beta.ca
        assert ixBeta.ha == beta.ha

        msgOut1 = b"Beta sends to Server first"
        beta.tx(msgOut1)
        while not ixBeta.rxbs and beta.txbs:
            beta.serviceSends()
            time.sleep(0.05)
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut1
        offset = len(ixBeta.rxbs)  # offset into .rxbs of first message

        # send multiple additional messages
        msgOut2 = b"Beta sends to Server second"
        beta.tx(msgOut2)
        msgOut3 = b"Beta sends to Server third"
        beta.tx(msgOut3)
        while len(ixBeta.rxbs) < len(msgOut1) + len(msgOut2) + len(msgOut3):
            beta.serviceSends()
            server.serviceReceivesAllIx()
            time.sleep(0.05)
        msgIn = bytes(ixBeta.rxbs)
        assert msgIn == msgOut1 + msgOut2 + msgOut3
        ixBeta.clearRxbs()  # clear out the receive buffer

        # send from server to beta
        msgOut = b"Server sends to Beta"
        ixBeta.tx(msgOut)
        while len(beta.rxbs) < len(msgOut):
            server.serviceSendsAllIx()
            beta.serviceReceives()
            time.sleep(0.05)
        msgIn = bytes(beta.rxbs)
        beta.clearRxbs()
        assert msgIn == msgOut

        readrx = wl.readRx()
        readtx = wl.readTx()
        assert readrx == readtx
        assert len(readrx) == 357


    assert beta.opened == False
    assert server.opened == False
    assert wl.rxl.closed
    assert wl.txl.closed
    assert not wl.opened

    """Done Test"""