Beispiel #1
0
    def test_timeout_cancel(self):
        server = listen(('0.0.0.0', 0))
        _, bound_port = server.getsockname()

        done = Event()

        def client_closer(sock):
            while True:
                (conn, addr) = sock.accept()
                conn.close()

        def go():
            desc = connect(('127.0.0.1', bound_port))
            try:
                hubs.trampoline(desc, read=True, timeout=0.1)
            except TimeoutError:
                assert False, "Timed out"

            server.close()
            desc.close()
            done.send()

        spawn_after_local(0, go)

        server_coro = spawn(client_closer, server)
        done.wait()
        kill(server_coro)

        check_hub()
    def test_send_something(self):
        listener = convenience.listen(('', 0))
        _, listener_port = listener.getsockname()

        def server():
            # accept the connection in another greenlet
            sock, addr = listener.accept()
            data = ''
            while True:
                last_data = sock.recv(1000)
                if not last_data:
                    break
                data += last_data

            return data

        def client():
            client = sockets.GreenSocket()
            client.connect(('127.0.0.1', listener_port))
            msg = 'hhheeeeelloooooo'
            total_sent = 0
            client.send(msg)
            return msg

        res = waitall(spawn(client), spawn(server))
        self.assertEquals(res[0], res[1])
    def test_send_timeout(self):
        self.reset_timeout(1000000)

        listener = convenience.listen(('', 0))
        _, port = listener.getsockname()

        evt = event.Event()

        def server():
            # accept the connection in another greenlet
            sock, addr = listener.accept()
            evt.wait()

        def client():
            client = sockets.GreenSocket()
            client.connect(('127.0.0.1', port))
            try:
                client.settimeout(0.00001)
                msg = s2b("A") * (
                    100000)  # large enough number to overwhelm most buffers

                total_sent = 0
                # want to exceed the size of the OS buffer so it'll block in a
                # single send
                for x in range(10):
                    total_sent += client.send(msg)
                self.fail("socket.timeout not raised")
            except socket.timeout, e:
                self.assert_(hasattr(e, 'args'))
                self.assertEqual(e.args[1], 'timed out')
            finally:
Beispiel #4
0
    def test_send_something (self):
        listener = convenience.listen(('', 0))
        _, listener_port = listener.getsockname()

        def server ():
            # accept the connection in another greenlet
            sock, addr = listener.accept()
            data = ''
            while True:
                last_data = sock.recv(1000)
                if not last_data:
                    break
                data += last_data

            return data

        def client ():
            client = sockets.GreenSocket()
            client.connect(('127.0.0.1', listener_port))
            msg = 'hhheeeeelloooooo'
            total_sent = 0
            client.send(msg)
            return msg

        res = waitall(spawn(client), spawn(server))
        self.assertEquals(res[0], res[1])
Beispiel #5
0
    def test_send_many (self):
        listener = convenience.listen(('', 0))
        _, listener_port = listener.getsockname()

        def server ():
            # accept the connection in another greenlet
            sock, addr = listener.accept()
            total_recv = 0
            while True:
                data = sock.recv(1000)
                if not data:
                    break
                total_recv += len(data)
            return total_recv

        def client ():
            client = sockets.GreenSocket()
            client.connect(('127.0.0.1', listener_port))
            msg = s2b("A") * (10000)  # large enough number to overwhelm most buffers

            # want to exceed the size of the OS buffer so it'll block in a single send
            total_sent = 0
            for x in range(10):
                total_sent += client.send(msg)
            return total_sent

        res = waitall(spawn(client), spawn(server))
        self.assertEqual(res[0], res[1])
Beispiel #6
0
    def test_send_timeout (self):
        self.reset_timeout(1000000)

        listener = convenience.listen(('', 0))
        _, port = listener.getsockname()

        evt = event.Event()

        def server ():
            # accept the connection in another greenlet
            sock, addr = listener.accept()
            evt.wait()

        def client ():
            client = sockets.GreenSocket()
            client.connect(('127.0.0.1', port))
            try:
                client.settimeout(0.00001)
                msg = s2b("A") * (100000)  # large enough number to overwhelm most buffers

                total_sent = 0
                # want to exceed the size of the OS buffer so it'll block in a
                # single send
                for x in range(10):
                    total_sent += client.send(msg)
                self.fail("socket.timeout not raised")
            except socket.timeout, e:
                self.assert_(hasattr(e, 'args'))
                self.assertEqual(e.args[1], 'timed out')
            finally:
    def test_send_many(self):
        listener = convenience.listen(('', 0))
        _, listener_port = listener.getsockname()

        def server():
            # accept the connection in another greenlet
            sock, addr = listener.accept()
            total_recv = 0
            while True:
                data = sock.recv(1000)
                if not data:
                    break
                total_recv += len(data)
            return total_recv

        def client():
            client = sockets.GreenSocket()
            client.connect(('127.0.0.1', listener_port))
            msg = s2b("A") * (
                10000)  # large enough number to overwhelm most buffers

            # want to exceed the size of the OS buffer so it'll block in a single send
            total_sent = 0
            for x in range(10):
                total_sent += client.send(msg)
            return total_sent

        res = waitall(spawn(client), spawn(server))
        self.assertEqual(res[0], res[1])
Beispiel #8
0
    def test_timeout_cancel (self):
        server = listen(('0.0.0.0', 0))
        _, bound_port = server.getsockname()

        done = Event()

        def client_closer (sock):
            while True:
                (conn, addr) = sock.accept()
                conn.close()

        def go ():
            desc = connect(('127.0.0.1', bound_port))
            try:
                hubs.trampoline(desc, read = True, timeout = 0.1)
            except TimeoutError:
                assert False, "Timed out"

            server.close()
            desc.close()
            done.send()

        spawn_after_local(0, go)

        server_coro = spawn(client_closer, server)
        done.wait()
        kill(server_coro)

        check_hub()
Beispiel #9
0
    def test_hub_exceptions (self):
        debug.hub_exceptions(True)
        server = convenience.listen(('0.0.0.0', 0))
        client = convenience.connect(('127.0.0.1', server.getsockname()[1]))
        client_2, addr = server.accept()

        def hurl (s):
            s.recv(1)
            {}[1]  # keyerror

        fake = StringIO()
        orig = sys.stderr
        sys.stderr = fake
        try:
            gt = evy.spawn(hurl, client_2)
            sleep(0)
            client.send(s2b(' '))
            sleep(0)
            # allow the "hurl" greenlet to trigger the KeyError
            # not sure why the extra context switch is needed
            sleep(0)
        finally:
            sys.stderr = orig
            self.assertRaises(KeyError, gt.wait)
            debug.hub_exceptions(False)
            # look for the KeyError exception in the traceback
        self.assert_('KeyError: 1' in fake.getvalue(),
                     "Traceback not in:\n" + fake.getvalue())
Beispiel #10
0
 def test_blocking(self):
     l = convenience.listen(('localhost', 0))
     x = with_timeout(0.01,
                      convenience.serve,
                      l,
                      lambda c, a: None,
                      timeout_value="timeout")
     self.assertEqual(x, "timeout")
Beispiel #11
0
    def test_raising_stopserve(self):
        def stopit(conn, addr):
            raise convenience.StopServe()

        l = convenience.listen(('localhost', 0))
        # connect to trigger a call to stopit
        gt = spawn(convenience.connect, ('localhost', l.getsockname()[1]))
        convenience.serve(l, stopit)
        gt.wait()
Beispiel #12
0
    def test_socket_reuse (self):
        lsock1 = convenience.listen(('localhost', 0))
        port = lsock1.getsockname()[1]

        def same_socket ():
            return convenience.listen(('localhost', port))

        self.assertRaises(socket.error, same_socket)
        lsock1.close()
        assert same_socket()
Beispiel #13
0
    def test_raising_stopserve (self):
        def stopit (conn, addr):
            raise convenience.StopServe()

        l = convenience.listen(('localhost', 0))
        # connect to trigger a call to stopit
        gt = spawn(convenience.connect,
                   ('localhost', l.getsockname()[1]))
        convenience.serve(l, stopit)
        gt.wait()
Beispiel #14
0
    def test_socket_reuse(self):
        lsock1 = convenience.listen(('localhost', 0))
        port = lsock1.getsockname()[1]

        def same_socket():
            return convenience.listen(('localhost', port))

        self.assertRaises(socket.error, same_socket)
        lsock1.close()
        assert same_socket()
Beispiel #15
0
    def test_exiting_server(self):
        # tests that the server closes the client sock on handle() exit
        def closer(sock, addr):
            pass

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, closer)
        client = convenience.connect(('localhost', l.getsockname()[1]))
        client.sendall(s2b('a'))
        self.assertFalse(client.recv(100))
        gt.kill()
Beispiel #16
0
    def test_exiting_server (self):
        # tests that the server closes the client sock on handle() exit
        def closer (sock, addr):
            pass

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, closer)
        client = convenience.connect(('localhost', l.getsockname()[1]))
        client.sendall(s2b('a'))
        self.assertFalse(client.recv(100))
        gt.kill()
Beispiel #17
0
    def test_excepting_server(self):
        # tests that the server closes the client sock on handle() exception
        def crasher(sock, addr):
            sock.recv(1024)
            0 // 0

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, crasher)
        _, port = l.getsockname()
        client = convenience.connect(('localhost', port))
        client.sendall(s2b('a'))
        self.assertRaises(ZeroDivisionError, gt.wait)
        self.assertFalse(client.recv(100))
Beispiel #18
0
    def test_excepting_server_already_closed (self):
        # same as above but with explicit clsoe before crash
        def crasher (sock, addr):
            sock.recv(1024)
            sock.close()
            0 // 0

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, crasher)
        client = convenience.connect(('localhost', l.getsockname()[1]))
        client.sendall(s2b('a'))
        self.assertRaises(ZeroDivisionError, gt.wait)
        self.assertFalse(client.recv(100))
Beispiel #19
0
    def test_excepting_server (self):
        # tests that the server closes the client sock on handle() exception
        def crasher (sock, addr):
            sock.recv(1024)
            0 // 0

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, crasher)
        _, port = l.getsockname()
        client = convenience.connect(('localhost', port))
        client.sendall(s2b('a'))
        self.assertRaises(ZeroDivisionError, gt.wait)
        self.assertFalse(client.recv(100))
Beispiel #20
0
    def test_excepting_server_already_closed(self):
        # same as above but with explicit clsoe before crash
        def crasher(sock, addr):
            sock.recv(1024)
            sock.close()
            0 // 0

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, crasher)
        client = convenience.connect(('localhost', l.getsockname()[1]))
        client.sendall(s2b('a'))
        self.assertRaises(ZeroDivisionError, gt.wait)
        self.assertFalse(client.recv(100))
Beispiel #21
0
    def test_wrap_ssl (self):
        server = convenience.wrap_ssl(convenience.listen(('localhost', 0)),
                                      certfile = certificate_file,
                                      keyfile = private_key_file, server_side = True)
        port = server.getsockname()[1]

        def handle (sock, addr):
            sock.sendall(sock.recv(1024))
            raise convenience.StopServe()

        spawn(convenience.serve, server, handle)
        client = convenience.wrap_ssl(convenience.connect(('localhost', port)))
        client.sendall("echo")
        self.assertEquals("echo", client.recv(1024))
Beispiel #22
0
    def test_wrap_ssl(self):
        server = convenience.wrap_ssl(convenience.listen(('localhost', 0)),
                                      certfile=certificate_file,
                                      keyfile=private_key_file,
                                      server_side=True)
        port = server.getsockname()[1]

        def handle(sock, addr):
            sock.sendall(sock.recv(1024))
            raise convenience.StopServe()

        spawn(convenience.serve, server, handle)
        client = convenience.wrap_ssl(convenience.connect(('localhost', port)))
        client.sendall("echo")
        self.assertEquals("echo", client.recv(1024))
Beispiel #23
0
    def test_called_for_each_connection(self):
        hits = [0]

        def counter(sock, addr):
            hits[0] += 1

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, counter)
        _, port = l.getsockname()

        for i in xrange(100):
            client = convenience.connect(('localhost', port))
            self.assertFalse(client.recv(100))

        gt.kill()
        self.assertEqual(100, hits[0])
Beispiel #24
0
    def test_called_for_each_connection (self):
        hits = [0]

        def counter (sock, addr):
            hits[0] += 1

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, counter)
        _, port = l.getsockname()

        for i in xrange(100):
            client = convenience.connect(('localhost', port))
            self.assertFalse(client.recv(100))

        gt.kill()
        self.assertEqual(100, hits[0])
Beispiel #25
0
    def test_concurrency(self):
        evt = event.Event()

        def waiter(sock, addr):
            sock.sendall(s2b('hi'))
            evt.wait()

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, waiter, 5)

        def test_client():
            c = convenience.connect(('localhost', l.getsockname()[1]))
            # verify the client is connected by getting data
            self.assertEquals(s2b('hi'), c.recv(2))
            return c

        clients = [test_client() for i in xrange(5)]
        # very next client should not get anything
        x = with_timeout(0.01, test_client, timeout_value="timed out")
        self.assertEquals(x, "timed out")
Beispiel #26
0
    def test_concurrency (self):
        evt = event.Event()

        def waiter (sock, addr):
            sock.sendall(s2b('hi'))
            evt.wait()

        l = convenience.listen(('localhost', 0))
        gt = spawn(convenience.serve, l, waiter, 5)

        def test_client ():
            c = convenience.connect(('localhost', l.getsockname()[1]))
            # verify the client is connected by getting data
            self.assertEquals(s2b('hi'), c.recv(2))
            return c

        clients = [test_client() for i in xrange(5)]
        # very next client should not get anything
        x = with_timeout(0.01, test_client, timeout_value = "timed out")
        self.assertEquals(x, "timed out")
Beispiel #27
0
    def test_001_trampoline_timeout (self):
        server_sock = listen(('127.0.0.1', 0))
        bound_port = server_sock.getsockname()[1]

        def server (sock):
            client, addr = sock.accept()
            sleep(0.1)

        server_evt = spawn(server, server_sock)
        sleep(0)

        try:
            desc = connect(('127.0.0.1', bound_port))
            hubs.trampoline(desc, read = True, write = False, timeout = 0.001)
        except TimeoutError:
            pass # test passed
        else:
            assert False, "Didn't timeout"

        server_evt.wait()
        check_hub()
Beispiel #28
0
    def test_raised_multiple_readers (self):

        def handle (sock, addr):
            sock.recv(1)
            sock.sendall("a")
            raise convenience.StopServe()

        listener = convenience.listen(('127.0.0.1', 0))
        server = spawn(convenience.serve, listener, handle)
        _, port = listener.getsockname()

        def reader (s):
            s.recv(1)

        sleep(0)
        s = convenience.connect(('127.0.0.1', port))
        a = spawn(reader, s)
        sleep(0)
        self.assertRaises(RuntimeError, s.recv, 1)      ## try to read from the same socket...
        s.sendall('b')
        a.wait()
Beispiel #29
0
    def test_001_trampoline_timeout(self):
        server_sock = listen(('127.0.0.1', 0))
        bound_port = server_sock.getsockname()[1]

        def server(sock):
            client, addr = sock.accept()
            sleep(0.1)

        server_evt = spawn(server, server_sock)
        sleep(0)

        try:
            desc = connect(('127.0.0.1', bound_port))
            hubs.trampoline(desc, read=True, write=False, timeout=0.001)
        except TimeoutError:
            pass  # test passed
        else:
            assert False, "Didn't timeout"

        server_evt.wait()
        check_hub()
    def test_raised_multiple_readers(self):
        def handle(sock, addr):
            sock.recv(1)
            sock.sendall("a")
            raise convenience.StopServe()

        listener = convenience.listen(('127.0.0.1', 0))
        server = spawn(convenience.serve, listener, handle)
        _, port = listener.getsockname()

        def reader(s):
            s.recv(1)

        sleep(0)
        s = convenience.connect(('127.0.0.1', port))
        a = spawn(reader, s)
        sleep(0)
        self.assertRaises(RuntimeError, s.recv,
                          1)  ## try to read from the same socket...
        s.sendall('b')
        a.wait()
Beispiel #31
0
    def test_ssl_unwrap (self):
        def serve ():
            sock, addr = listener.accept()
            self.assertEquals(sock.recv(6), 'before')
            sock_ssl = util.wrap_ssl(sock, certificate_file, private_key_file,
                                     server_side = True)
            sock_ssl.do_handshake()
            self.assertEquals(sock_ssl.read(6), 'during')
            sock2 = sock_ssl.unwrap()
            self.assertEquals(sock2.recv(5), 'after')
            sock2.close()

        listener = listen(('127.0.0.1', 0))
        server_coro = evy.spawn(serve)
        client = connect((listener.getsockname()))
        client.send('before')
        client_ssl = util.wrap_ssl(client)
        client_ssl.do_handshake()
        client_ssl.write('during')
        client2 = client_ssl.unwrap()
        client2.send('after')
        server_coro.wait()
Beispiel #32
0
    def test_ssl_sending_messages (self):
        s = evy.wrap_ssl(listen(('localhost', 0)),
                         certfile = certificate_file,
                         keyfile = private_key_file,
                         server_side = True)
        self.spawn_server(sock = s)
        connect_data = [
            "GET /echo HTTP/1.1",
            "Upgrade: WebSocket",
            "Connection: Upgrade",
            "Host: localhost:%s" % self.port,
            "Origin: http://localhost:%s" % self.port,
            "Sec-WebSocket-Protocol: ws",
            "Sec-WebSocket-Key1: 4 @1  46546xW%0l 1 5",
            "Sec-WebSocket-Key2: 12998 5 Y3 1  .P00",
        ]
        sock = evy.wrap_ssl(connect(
            ('localhost', self.port)))

        sock.sendall('\r\n'.join(connect_data) + '\r\n\r\n^n:ds[4U')
        first_resp = sock.recv(1024)
        # make sure it sets the wss: protocol on the location header
        loc_line = [x for x in first_resp.split("\r\n")
                    if x.lower().startswith('sec-websocket-location')][0]
        self.assert_("wss://localhost" in loc_line,
                     "Expecting wss protocol in location: %s" % loc_line)
        sock.sendall('\x00hello\xFF')
        result = sock.recv(1024)
        self.assertEqual(result, '\x00hello\xff')
        sock.sendall('\x00start')
        sleep(0.001)
        sock.sendall(' end\xff')
        result = sock.recv(1024)
        self.assertEqual(result, '\x00start end\xff')
        shutdown_safe(sock)
        sock.close()
        sleep(0.01)
Beispiel #33
0
    def test_makefile_closes (self):
        self.reset_timeout(100000)

        def accept_once (listenfd):
            try:
                conn, addr = listenfd.accept()
                fd = conn.makefile(mode = 'w')
                conn.close()
                fd.write('hello\n')
                fd.close()
            finally:
                listenfd.close()

        server = convenience.listen(('0.0.0.0', 0))
        spawn(accept_once, server)

        client = convenience.connect(('127.0.0.1', server.getsockname()[1]))
        fd = client.makefile()
        client.close()
        assert fd.readline() == 'hello\n'

        assert fd.read() == ''
        fd.close()
        check_hub()
Beispiel #34
0
 def test_tcp_listener (self):
     socket = convenience.listen(('0.0.0.0', 0))
     assert socket.getsockname()[0] == '0.0.0.0'
     socket.close()
     check_hub()
Beispiel #35
0
 def same_socket ():
     return convenience.listen(('localhost', port))
Beispiel #36
0
 def test_blocking (self):
     l = convenience.listen(('localhost', 0))
     x = with_timeout(0.01,
                      convenience.serve, l, lambda c, a: None,
                      timeout_value = "timeout")
     self.assertEqual(x, "timeout")
Beispiel #37
0
 def same_socket():
     return convenience.listen(('localhost', port))
Beispiel #38
0
 def test_invalid_connection(self):
     # find an unused port by creating a socket then closing it
     port = convenience.listen(('127.0.0.1', 0)).getsockname()[1]
     self.assertRaises(socket.error, convenience.connect,
                       ('127.0.0.1', port))
Beispiel #39
0
 def test_invalid_connection (self):
     # find an unused port by creating a socket then closing it
     port = convenience.listen(('127.0.0.1', 0)).getsockname()[1]
     self.assertRaises(socket.error, convenience.connect, ('127.0.0.1', port))
Beispiel #40
0
class TestGreenSocket(LimitedTestCase):
    TEST_TIMEOUT = 2

    def assertWriteToClosedFileRaises(self, fd):
        if sys.version_info[0] < 3:
            # 2.x socket._fileobjects are odd: writes don't check
            # whether the socket is closed or not, and you get an
            # AttributeError during flush if it is closed
            fd.write('a')
            self.assertRaises(Exception, fd.flush)
        else:
            # 3.x poll write to closed file-like pbject raises ValueError
            self.assertRaises(ValueError, fd.write, 'a')

    def test_crucial_constants(self):
        # Testing for mission critical constants
        socket.AF_INET
        socket.SOCK_STREAM
        socket.SOCK_DGRAM
        socket.SOCK_RAW
        socket.SOCK_RDM
        socket.SOCK_SEQPACKET
        socket.SOL_SOCKET
        socket.SO_REUSEADDR

    def test_tcp_listener(self):
        socket = convenience.listen(('0.0.0.0', 0))
        assert socket.getsockname()[0] == '0.0.0.0'
        socket.close()
        check_hub()

    def test_getsockname(self):
        listener = sockets.GreenSocket()
        listener.bind(('', 0))
        addr = listener.getsockname()
        self.assertNotEquals(addr[1], 0)

    def test_wrap_socket(self):
        try:
            import ssl
        except ImportError:
            pass  # pre-2.6
        else:
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            sock.bind(('127.0.0.1', 0))
            sock.listen(50)
            ssl_sock = ssl.wrap_socket(sock)

    @skipped
    def test_closure(self):
        def spam_to_me(address):
            sock = convenience.connect(address)
            while True:
                try:
                    sock.sendall('hello world')
                except socket.error, e:
                    if get_errno(e) == errno.EPIPE:
                        return
                    raise

        server = convenience.listen(('127.0.0.1', 0))
        sender = spawn(spam_to_me, server.getsockname())

        client, address = server.accept()
        server.close()

        def reader():
            try:
                while True:
                    data = client.recv(1024)
                    self.assert_(data)
            except socket.error, e:
                # we get an EBADF because client is closed in the same process
                # (but a different greenthread)
                if get_errno(e) != errno.EBADF:
                    raise
Beispiel #41
0
 def test_tcp_listener(self):
     socket = convenience.listen(('0.0.0.0', 0))
     assert socket.getsockname()[0] == '0.0.0.0'
     socket.close()
     check_hub()