Пример #1
0
    def test_server_connect_invalid_key(self):
        # test that a client does not connect if the public key does
        # not match the server public key
        server_sock, client_sock = MockUDPSocket.mkpair()
        client = TestClient(client_sock)
        key = EllipticCurvePrivateKey.new()
        key2 = EllipticCurvePrivateKey.new().getPublicKey()

        client.conn.setServerPublicKey(key2)

        ctxt = ServerContext(TestHandler(), key)
        thread = UdpServerThread(server_sock, ctxt)

        thread.start()

        timedout=None
        def callback(success):
            nonlocal timedout
            timedout = not success

        client.conn.outgoing_timeout = .25
        client.conn.connection_callback = callback

        t0 = time.time()
        connected = False

        with self.assertRaises(EllipticCurvePublicKey.InvalidSignature):

            while not connected:
                datagram, addr = server_sock.recvfrom(Packet.RECV_SIZE)
                if datagram:
                    hdr = PacketHeader.from_bytes(True, datagram)
                    thread.append(addr, hdr, datagram)
                client.update()
                time.sleep(1/60)

                if time.time() - t0 > .5:
                    self.fail("failed to connect")

                # test that both sides are connected
                if client.conn.status == ConnectionStatus.CONNECTED:
                    for addr, other in ctxt.connections.items():
                        if other.status == ConnectionStatus.CONNECTED:
                            connected = True

        self.assertEqual(client.conn.status, ConnectionStatus.DISCONNECTED)
        self.assertFalse(timedout)

        ctxt._active = False

        # append an invalid packet to wake up the server
        thread.append(("0.0.0.0", 0), None, b"")
        thread.join()
Пример #2
0
    def test_server_start_stop(self):
        # test that a server can be started then stopped

        server_sock, client_sock = MockUDPSocket.mkpair()

        ctxt = ServerContext(TestHandler())
        thread = UdpServerThread(server_sock, ctxt)

        thread.start()

        ctxt._active = False

        # append an invalid packet to wake up the server
        thread.append(("0.0.0.0", 0), None, b"")
        thread.join()
Пример #3
0
    def setUp(self):
        # launch a server thread, connect one client
        self.server_sock, self.client_sock = MockUDPSocket.mkpair()
        self.client = TestClient(self.client_sock)

        self.ctxt = ServerContext(TestHandler())
        self.thread = UdpServerThread(self.server_sock, self.ctxt)

        self.thread.start()

        t0 = time.time()
        connected = False
        while not connected:
            datagram, addr = self.server_sock.recvfrom(Packet.RECV_SIZE)
            if datagram:
                hdr = PacketHeader.from_bytes(True, datagram)
                self.thread.append(addr, hdr, datagram)
            self.client.update()
            time.sleep(1 / 60)

            if time.time() - t0 > .5:
                self.fail("failed to connect")

            # test that both sides are connected
            if self.client.conn.status == ConnectionStatus.CONNECTED:
                for addr, other in self.ctxt.connections.items():
                    if other.status == ConnectionStatus.CONNECTED:
                        self.server_client = other
                        connected = True

        self.assertEqual(self.client.conn.status, ConnectionStatus.CONNECTED)
Пример #4
0
def main():

    host = "0.0.0.0"
    port = 1474

    format = '%(asctime)-15s %(levelname)s %(filename)s:%(funcName)s():%(lineno)d:%(message)s'
    logging.basicConfig(level=logging.DEBUG, format=format)

    # read the root key from stdin or use a default development key
    key = None
    if not sys.stdin.isatty():
        r, _, _ = select.select([sys.stdin], [], [], 0)
        if r:
            key = EllipticCurvePrivateKey.fromPEM(sys.stdin.read())
    if key is None:
        sys.stderr.write("MpGameServer using unsafe test key\n")
        key = EllipticCurvePrivateKey.unsafeTestKey()

    handler = ShipHandler()

    ctxt = ServerContext(handler, key)

    if '--gui' in sys.argv:
        server = GuiServer(ctxt, (host, port))
    else:
        server = TwistedServer(ctxt, (host, port))

    server.run()
Пример #5
0
    def test_server_connect(self):
        # test that a client can connect to the server
        server_sock, client_sock = MockUDPSocket.mkpair()
        client = TestClient(client_sock)

        ctxt = ServerContext(TestHandler())
        thread = UdpServerThread(server_sock, ctxt)

        thread.start()

        timedout = None

        def callback(success):
            nonlocal timedout
            timedout = not success

        client.conn.outgoing_timeout = .25
        client.conn.connection_callback = callback

        t0 = time.time()
        connected = False
        while not connected:
            datagram, addr = server_sock.recvfrom(Packet.RECV_SIZE)
            if datagram:
                hdr = PacketHeader.from_bytes(True, datagram)
                thread.append(addr, hdr, datagram)
            client.update()
            time.sleep(1 / 60)

            if time.time() - t0 > .5:
                self.fail("failed to connect")

            # test that both sides are connected
            if client.conn.status == ConnectionStatus.CONNECTED:
                for addr, other in ctxt.connections.items():
                    if other.status == ConnectionStatus.CONNECTED:
                        connected = True

        self.assertEqual(client.conn.status, ConnectionStatus.CONNECTED)
        self.assertFalse(timedout)

        ctxt._active = False

        # append an invalid packet to wake up the server
        thread.append(("0.0.0.0", 0), None, b"")
        thread.join()
Пример #6
0
    def setUpClass(cls):

        cls.addr = ('0.0.0.0', 14740)
        cls.ctxt = ServerContext(TestHandler())


        cls.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        cls.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        cls.sock.bind(cls.addr)

        cls.thread = UdpServerThread(cls.sock, cls.ctxt)
        cls.thread.start()
Пример #7
0
def main():

    logging.basicConfig(level=logging.DEBUG)

    host = "0.0.0.0"
    port = 1474

    ctxt = ServerContext(EchoHandler())

    server = TwistedServer(ctxt, (host, port))

    server.run()
Пример #8
0
    def test_1_run(self):
        global server, ctxt, hdlr

        addr = ('0.0.0.0', 14741)

        hdlr = TestHandler()
        ctxt = ServerContext(hdlr)
        server = ThreadedServer(ctxt, addr)
        server.start()

        time.sleep(.5)

        self.assertTrue(hdlr._starting)
Пример #9
0
def main():

    host = "0.0.0.0"
    port = 1474

    format = '%(asctime)-15s %(levelname)s %(filename)s:%(funcName)s():%(lineno)d:%(message)s'
    logging.basicConfig(level=logging.DEBUG, format=format)

    handler = ShipHandler()

    ctxt = ServerContext(handler)

    if '--gui' in sys.argv:
        server = GuiServer(ctxt, (host, port))
    else:
        server = TwistedServer(ctxt, (host, port))

    server.run()
Пример #10
0
def main():

    host = "0.0.0.0"
    port = 1474

    format = '%(asctime)-15s %(levelname)s %(filename)s:%(funcName)s():%(lineno)d:%(message)s'
    logging.basicConfig(level=logging.DEBUG, format=format)

    handler = PongHandler()

    ctxt = ServerContext(handler)

    if '--gui' in sys.argv:
        server = GuiServer(ctxt, (host, port))
    else:
        server = TwistedServer(ctxt, (host, port))

    router = Router()
    router.registerEndpoints(SampleResource().endpoints())
    server.listenTCP(router, (host, port+1))

    server.run()