Esempio n. 1
0
def basicUnitTest():
    #asyncio.set_event_loop(TestLoopEx())
    # server = EchoServerProtocol()
    # client = EchoClientProtocol()
    # transportToServer = MockTransportToProtocol(server)
    # transportToClient = MockTransportToProtocol(client)
    # server.connection_made(transportToClient)
    # client.connection_made(transportToServer)
    client = EchoClientProtocol()
    server = EchoServerProtocol()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)
    client.connection_made(cTransport)
    server.connection_made(sTransport)
    firstRequest = packetClass.RequestRecommandation()
    client.Request(firstRequest)
    assert server.serverstatus == 1
    assert client.clientstatus == 1
    #client.method(firstRequest)
    OneAnswer = packetClass.Answer()
    OneAnswer.answer = "oliy"
    OneAnswer.ID = 1
    client.method(OneAnswer)
    assert server.serverstatus == 2
    assert client.clientstatus == 2
def UnitTest():
    asyncio.set_event_loop(TestLoopEx())
    clientProtocol = ClientSide()
    serverProtocol = ServerSide()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        clientProtocol, serverProtocol)
    serverProtocol.connection_made(sTransport)
    clientProtocol.connection_made(cTransport)
Esempio n. 3
0
def basicProtocolTest():
    asyncio.set_event_loop(TestLoopEx())
    clientProtocol = EchoClientProtocol()
    serverProtocol = EchoServerProtocol()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        clientProtocol, serverProtocol)
    clientProtocol.connection_made(cTransport)
    serverProtocol.connection_made(sTransport)

    clientProtocol.send_request()
Esempio n. 4
0
def UnitTest2():
    set_event_loop(TestLoopEx())
    clientProtocol = EchoClientProtocol()
    serverProtocol = EchoServerProtocol()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        clientProtocol, serverProtocol)
    clientProtocol.connection_made(cTransport)
    serverProtocol.connection_made(sTransport)

    clientProtocol.SetIdentityInfo(123, "Jack", "2017jack")
    clientProtocol.SendLoginRequest()
Esempio n. 5
0
def basicUnitTest():
    print('Begin')
    asyncio.set_event_loop(TestLoopEx())

    clientProtocol = ClientProtocol()
    serverProtocol = ServerProtocol()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        clientProtocol, serverProtocol)
    clientProtocol.connection_made(cTransport)
    serverProtocol.connection_made(sTransport)

    clientProtocol.start()
Esempio n. 6
0
def basicUnitTest():

    # asyncio.set_event_loop(TestLoopEx())
    clientProtocol = MyClientProtocol()
    serverProtocol = MyServerProtocol()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        clientProtocol, serverProtocol)
    clientProtocol.connection_made(cTransport)
    serverProtocol.connection_made(sTransport)

    packet1 = RequestConnect()
    packet1.iD = "ELROY"
    clientProtocol.transport.write(packet1.__serialize__())
Esempio n. 7
0
def unitTest():
    #for now params are picked within the protocols
    from playground.network.testing import MockTransportToProtocol
    from playground.asyncio_lib.testing import TestLoopEx

    asyncio.set_event_loop(TestLoopEx())

    server = PlaneServer()
    client = PlaneClient()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)
    server.connection_made(sTransport)
    client.connection_made(cTransport)
Esempio n. 8
0
def basicUnitTest():
    client = Protocol.ForgotPasswordClientProtocol()
    server = Protocol.ForgotPasswordServerProtocol()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)

    server.connection_made(sTransport)
    client.connection_made(cTransport)
    # client.send_initial_message()

    packet1 = Packets.RequestForgotPasswordPacket()
    packet1.userId = 'ehsia1'
    packet1Bytes = packet1.__serialize__()
    sResponse1 = server.data_received(packet1Bytes)
    assert isinstance(sResponse1, Packets.SecurityQuestionPacket)
    packet2 = Packets.SecurityQuestionPacket()
    packet2.securityQuestion = 'What was your hometown?'
    packet2Bytes = packet2.__serialize__()
    cResponse1 = client.data_received(packet2Bytes)
    cResponse1 = PacketType.Deserialize(cResponse1)
    assert isinstance(cResponse1, Packets.SecurityAnswerPacket)
    packet3 = Packets.SecurityAnswerPacket()
    packet3.securityAnswer = 'Windsor, CT'
    packet3Bytes = packet3.__serialize__()
    sResponse2 = server.data_received(packet3Bytes)
    assert isinstance(sResponse2, Packets.ForgotPasswordTokenPacket)
    packet3.securityAnswer = 'Windsor'
    packet3Bytes = packet3.__serialize__()
    sResponse2 = server.data_received(packet3Bytes)
    assert sResponse2 == "Invalid"
    packet4 = Packets.ForgotPasswordTokenPacket()
    packet4.token = 'asdf2313241SqwerXq'
    packet4Bytes = packet4.__serialize__()
    cResponse2 = client.data_received(packet4Bytes)
    cResponse2 = PacketType.Deserialize(cResponse2)
    assert isinstance(cResponse2, Packets.ResetPasswordInputPacket)
    packet5 = Packets.ResetPasswordInputPacket()
    packet5.newPassword = '******'
    packet5.passwordConfirmation = 'gronkgronkgronk'
    packet5Bytes = packet5.__serialize__()
    sResponse3 = server.data_received(packet5Bytes)
    assert isinstance(sResponse3, Packets.PasswordResetPacket)
    assert sResponse3.verification == True
    packet5.newPassword = '******'
    packet5.passwordConfirmation = 'gronkgronkgronk'
    packet5Bytes = packet5.__serialize__()
    sResponse3 = server.data_received(packet5Bytes)
    assert isinstance(sResponse3, Packets.PasswordResetPacket)
    assert sResponse3.verification == False

    print("Protocol completed! All tests passed!")
Esempio n. 9
0
def basicUnitTest():
	
	loop = 	TestLoopEx()
	asyncio.set_event_loop(TestLoopEx())
			

	clientProtocol = ClientProtocol(loop)
	serverProtocol = ServerProtocol()
	cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(clientProtocol, serverProtocol)
	clientProtocol.connection_made(cTransport)
	serverProtocol.connection_made(sTransport)

	pkt1 = DB_connect()	
	clientProtocol.send_packet(pkt1)
Esempio n. 10
0
def basicUnitTest():

    loop = asyncio.set_event_loop(TestLoopEx())
    server = EchoServerProtocol()
    server.invite('Bob', 'California', 1, 1, '10.0.0.1', 65001,
                  ['G711u', 'G729', 'G722', 'OPUS', 'G711a'])
    client = EchoClientProtocol(loop)
    client.response('Alice', 'WashingtonDC', 1, 1, '192.168.1.254', 45532,
                    ["G722a", "G729"])
    #transportToServer = MockTransportToProtocol(server)
    #transportToClient = MockTransportToProtocol(client)
    transportToServer, transportToClient = MockTransportToProtocol.CreateTransportPair(
        client, server)
    server.connection_made(transportToClient)
    client.connection_made(transportToServer)
Esempio n. 11
0
def basicUnitTest():
    from playground.asyncio_lib.testing import TestLoopEx
    from playground.network.testing import MockTransportToProtocol

    loop = asyncio.get_event_loop()
    print("Making the server choose a 'random' number of 5.")

    client_1 = RandomNumberGameClientProtocol(loop)
    server_1 = RandomNumberGameServerProtocol(seed=5)
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client_1, server_1)
    server_1.connection_made(sTransport)
    client_1.connection_made(cTransport)

    client_1.initiate_game(guess=4)

    client_2 = RandomNumberGameClientProtocol(loop)
    server_2 = RandomNumberGameServerProtocol(seed=5)
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client_2, server_2)
    server_2.connection_made(sTransport)
    client_2.connection_made(cTransport)

    client_2.initiate_game(guess=5)
Esempio n. 12
0
def basicUnitTestForPEEPProtocol(loggingFlag):
    asyncio.set_event_loop(TestLoopEx())
    loop = asyncio.get_event_loop()

    server = PEEPServerProtocol(False)
    client = PEEPClientProtocol(False)

    client.set_timeout_flag(False)
    client.set_mock_flag(True)
    server.set_mock_flag(True)

    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)

    # test for general connection_made
    client.connection_made(cTransport)
    server.connection_made(sTransport)

    if loggingFlag == True: print("- test for general connection_made SUCCESS")
    if loggingFlag == True: print("")

    # negative test for messing up packet order
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)
    client.connection_made(cTransport)
    server.connection_made(sTransport)

    MockPEEPPacket_SYN = Util.create_outbound_packet(0, 1, 1, b"data")
    packetBytes = MockPEEPPacket_SYN.__serialize__()
    server.state = "SYN_State_1"
    client.state = "SYN_ACK_State_1"
    server.data_received(packetBytes)
    assert server.state == "error_state"
    if loggingFlag == True:
        print("- negative test for messing up packet order SUCCESS")
    if loggingFlag == True: print("")
Esempio n. 13
0
def basicUnitTest():

    loop = TestLoopEx()
    asyncio.set_event_loop(loop)  # Test Loop
    client = EchoClientProtocol()  # Initialize Client Protocol
    server = EchoServerProtocol()  # Initialize Server Protocol
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)

    #transportToServer = MockTransportToProtocol(server)
    #transportToClient = MockTransportToProtocol(client)
    packet1 = AccessResource()
    packet1Bytes = packet1.__serialize__()
    server.connection_made(sTransport)
    client.connection_made(cTransport)
    server.data_received(packet1Bytes)
Esempio n. 14
0
def basicUnitTest():

    loop = asyncio.get_event_loop()
    playground.getConnector().create_playground_server(
        lambda: EchoServerProtocol(), 8000)
    playground.getConnector().create_playground_connection(
        lambda: EchoClientProtocol(), "20174.1.1.1", 8000)
    client = EchoClientProtocol()  # Initialize Client Protocol
    server = EchoServerProtocol()  # Initialize Server Protocol
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)

    #transportToServer = MockTransportToProtocol(server)
    #transportToClient = MockTransportToProtocol(client)
    packet1 = AccessResource()
    packet1Bytes = packet1.__serialize__()
    server.connection_made(sTransport)
    client.connection_made(cTransport)
    server.data_received(packet1Bytes)
Esempio n. 15
0
def basicUnitTest():
    from playground.network.testing import MockTransportToProtocol as MTP
    from playground.network.testing import MockTransportToStorageStream as MTSS

    from playground.asyncio_lib.testing import TestLoopEx

    loop = asyncio.set_event_loop(TestLoopEx())

    clientProtocol = SimpleMathProtocolClient()
    serverProtocol = SimpleMathProtocolServer()

    transportToServer, transportToClient = MTP.CreateTransportPair(
        clientProtocol, serverProtocol)

    clientProtocol.connection_made(transportToServer)
    serverProtocol.connection_made(transportToClient)

    for i in range(0, 10):
        clientProtocol.requestProblem()
Esempio n. 16
0
def basicUnitTestForAppLayerProtocol(loggingFlag):
    asyncio.set_event_loop(TestLoopEx())
    loop = asyncio.get_event_loop()

    server = VerificationCodeServerProtocol(loop, False)
    client = VerificationCodeClientProtocol(1, loop, False)
    server.set_mock_flag(True)
    client.set_mock_flag(True)
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)

    # test for general connection_made
    client.connection_made(cTransport)
    server.connection_made(sTransport)
    if loggingFlag == True: print("- test for general connection_made SUCCESS")
    if loggingFlag == True: print("")

    # test for client verification code length
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)
    client.connection_made(cTransport)
    server.connection_made(sTransport)

    MockRequestPacket = RequestPacket()
    MockRequestPacket.ID = 1
    packetBytes = MockRequestPacket.__serialize__()
    server.data_received(packetBytes)
    assert len(str(server._verificationCode)) == 6
    if loggingFlag == True:
        print("- test for client verification code length SUCCESS")
    if loggingFlag == True: print("")

    # negative test for messing up packet order
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)
    client.connection_made(cTransport)
    server.connection_made(sTransport)

    MockVerifyPacket = VerifyPacket()
    MockVerifyPacket.ID = 1
    MockVerifyPacket.answer = server._verificationCode
    packetBytes = MockVerifyPacket.__serialize__()
    server.state = "wait_for_verify_packet"
    client.state = "initial_state"
    server.data_received(packetBytes)
    assert client.state == "error_state"
    if loggingFlag == True:
        print("- negative test for messing up packet order SUCCESS")
    if loggingFlag == True: print("")

    # test for client vericifation result
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)
    client.connection_made(cTransport)
    server.connection_made(sTransport)

    MockVerifyPacket = VerifyPacket()
    MockVerifyPacket.ID = 1
    MockVerifyPacket.answer = server._verificationCode
    packetBytes = MockVerifyPacket.__serialize__()
    server.state = "wait_for_verify_packet"
    client.state = "wait_for_result_packet"
    server.data_received(packetBytes)
    assert server._result == "pass"
    if loggingFlag == True:
        print("- test for client vericifation result SUCCESS")
    if loggingFlag == True: print("")

    # negative test for client vericifation result
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        client, server)
    client.connection_made(cTransport)
    server.connection_made(sTransport)

    MockVerifyPacket = VerifyPacket()
    MockVerifyPacket.ID = 1
    MockVerifyPacket.answer = 0
    packetBytes = MockVerifyPacket.__serialize__()
    server.state = "wait_for_verify_packet"
    client.state = "wait_for_result_packet"
    server.data_received(packetBytes)
    assert server._result == "fail"
    if loggingFlag == True:
        print("- negative test for client vericifation result SUCCESS")
    if loggingFlag == True: print("")
Esempio n. 17
0
def basicUnitTest():
    from playground.network.testing import MockTransportToProtocol as MockTransport

    class MockSwitch:
        def __init__(self):
            self.addresses = {}
            self.extensionPackets = []

        def registerLink(self, address, protocol):
            self.addresses[address] = self.addresses.get(address,
                                                         []) + [protocol]

        def unregisterLink(self, protocol):
            # this is slow, but fine for testing:
            rem = None
            for address in self.addresses:
                if not protocol in self.addresses[address]: continue
                self.addresses[address].remove(protocol)
                if len(self.addresses[address]) == 0: rem = address
            if rem: del self.addresses[rem]

        def getOutboundLinks(self, source, sourcePort, destination,
                             destinationPort):
            return self.addresses[destination]

        def handleExtensionPacket(self, ep):
            self.extensionPackets.append(ep)

    class MockClient:
        def __init__(self):
            self.results = []

        def demux(self, source, sourcePort, destination, destinationPort,
                  data):
            self.results.append(
                (source, sourcePort, destination, destinationPort, data))

        def connectionMade(self):
            pass

        def connectionLost(self):
            pass

    switch = MockSwitch()
    rx1 = PlaygroundSwitchRxProtocol(switch)
    rx2 = PlaygroundSwitchRxProtocol(switch)
    rx3 = PlaygroundSwitchRxProtocol(switch)

    client1, client2, client3 = MockClient(), MockClient(), MockClient()
    c1Tx = PlaygroundSwitchTxProtocol(client1, "1.1.1.1")
    c2Tx = PlaygroundSwitchTxProtocol(client2, "2.2.2.2")
    c3Tx = PlaygroundSwitchTxProtocol(client3, "2.2.2.2")

    # Client transports send data to rx)
    c1Transport, rx1Transport = MockTransport.CreateTransportPair(c1Tx, rx1)
    c2Transport, rx2Transport = MockTransport.CreateTransportPair(c2Tx, rx2)
    c3Transport, rx3Transport = MockTransport.CreateTransportPair(c3Tx, rx3)

    # Make the switch-side connections first
    rx1.connection_made(rx1Transport)
    rx2.connection_made(rx2Transport)
    rx3.connection_made(rx3Transport)

    # Connecting the client protocols should cause an announce packet sent
    assert len(switch.addresses) == 0

    c1Tx.connection_made(c1Transport)
    assert len(switch.addresses) == 1

    c2Tx.connection_made(c2Transport)
    assert len(switch.addresses) == 2

    # Client 3 has the same address as client 2
    c3Tx.connection_made(c3Transport)
    assert len(switch.addresses) == 2

    # Send some small hello messages.
    c1Tx.write("1.1.1.1", 80, "2.2.2.2", 1000, b"Test1")
    assert len(client2.results) == 1
    assert client2.results[0] == ("1.1.1.1", 80, "2.2.2.2", 1000, b"Test1")
    assert len(client3.results) == 1
    assert client3.results[0] == client2.results[0]

    c2Tx.write("2.2.2.2", 1000, "1.1.1.1", 80, b"Response")
    assert client1.results[0] == ("2.2.2.2", 1000, "1.1.1.1", 80, b"Response")

    # Send a large packet to test fragmentation/re-assembly
    repeatingKey = b"REPEATINGKEY"
    largeData = b""
    while len(largeData) < (PlaygroundSwitchTxProtocol.MAX_MSG_SIZE + 100):
        largeData += repeatingKey

    curWriteCount = c3Transport.writeCount
    c3Tx.write("2.2.2.2", 1000, "1.1.1.1", 80, largeData)
    # should have written twice (two fragments)
    assert c3Transport.writeCount == (curWriteCount + 2)

    print("client 1 results count {}, len data {}, original len{}.".format(
        len(client1.results), len(client1.results[1][-1]), len(largeData)))
    assert client1.results[1][-1] == largeData

    c1Transport.close()
    assert c1Tx.transport == None
    assert rx1Transport.closed
    assert rx1.transport == None

    rx2Transport.close()
    assert rx2.transport == None
    assert c2Transport.closed
    assert c2Tx.transport == None
Esempio n. 18
0
def basicUnitTest():
    # initialize packets
    pktInfoSequence = []

    unlockPacket1 = UnlockPacket()
    unlockPacket1.password = "******"
    pktInfoSequence.append(
        ("Sending unlock packet #1 (1234, bad format)", unlockPacket1))

    unlockPacket2 = UnlockPacket()
    unlockPacket2.password = "******"
    pktInfoSequence.append(
        ("Sending unlock packet #2 (001, wrong password)", unlockPacket2))

    unlockPacket3 = UnlockPacket()
    unlockPacket3.password = "******"
    pktInfoSequence.append(("Sending unlock packet #3 (000)", unlockPacket3))

    unlockPacket4 = unlockPacket3
    pktInfoSequence.append(
        ("Sending unlock packet #4 (000, duplicate)", unlockPacket4))

    changePasswordPacket1 = ChangePasswordPacket()
    changePasswordPacket1.password = "******"
    pktInfoSequence.append(
        ("Sending change password packet #1 (1234, bad format)",
         changePasswordPacket1))

    changePasswordPacket2 = ChangePasswordPacket()
    changePasswordPacket2.password = "******"
    pktInfoSequence.append(
        ("Sending change password packet #2 (123)", changePasswordPacket2))

    lockPacket1 = LockPacket()
    pktInfoSequence.append(("Sending lock packet #1", lockPacket1))

    lockPacket2 = LockPacket()
    pktInfoSequence.append(("Sending lock packet #2", lockPacket2))

    changePasswordPacket3 = ChangePasswordPacket()
    changePasswordPacket3.password = "******"
    pktInfoSequence.append(
        ("Sending change password packet #3 (456, now locked)",
         changePasswordPacket3))

    unlockPacket5 = UnlockPacket()
    unlockPacket5.password = "******"
    pktInfoSequence.append(
        ("Sending unlock packet #5 (000, wrong password)", unlockPacket5))

    unlockPacket6 = UnlockPacket()
    unlockPacket6.password = "******"
    pktInfoSequence.append(("Sending unlock packet #6 (123)", unlockPacket6))

    testOptionalListPacket = TestOptionalListPacket()
    testOptionalListPacket.testlist = ["Whatever"]
    pktInfoSequence.append(
        ("Sending unknown type packet", testOptionalListPacket))

    lock = Lock("000", True)
    print("Unit test started; Lock initialized as locked, password = 000")
    # Modified from lab1c PDF
    asyncio.set_event_loop(TestLoopEx())
    clientProtocol = LockClientProtocol()
    serverProtocol = LockServerProtocol(lock)
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(
        clientProtocol, serverProtocol)
    # Initialize server first to obtain transport
    serverProtocol.connection_made(sTransport)
    clientProtocol.connection_made(cTransport)
    clientProtocol.sendPackets(pktInfoSequence)
Esempio n. 19
0
    def connection_made(self, transport):
        print('--- peep connect to client---')
        self.transport = transport
        self.deserializer = PacketType.Deserializer()

    def data_received(self, data):
        self.deserializer.update(data)
        print (data)
        # peepInfoS = PEEPPacket()
        # peepInfoS.sequenceNumber = 5
        # for pkt in self.deserializer.nextPackets():
        #     if isinstance(pkt,PEEPClient.connection_made().peepInfoC.sequenceNumber()):
        #         peepInfoS.Acknowledgement = PEEPClient.connection_made().peepInfoC.sequenceNumber()+1
        #         self.transport.write(peepInfoS.__serialize__())

    def connection_lost(self, exc):
        self.connection_lost(exc)


if __name__ == '__main__':
    client = PEEPClient()
    server = PEEPServer()
    cTransport, sTransport = MockTransportToProtocol.CreateTransportPair(client, server)
    client.connection_made(cTransport)
    server.connection_made(sTransport)