def test_ssl_connect2 (self): def accept_once (listenfd): try: conn, addr = listenfd.accept() conn.write('hello\r\n') shutdown_safe(conn) conn.close() finally: shutdown_safe(listenfd) listenfd.close() server = ssl_listener(('0.0.0.0', 0), self.certificate_file, self.private_key_file) greenthread.spawn(accept_once, server) raw_client = connect(('127.0.0.1', server.getsockname()[1])) client = util.wrap_ssl(raw_client) fd = socket._fileobject(client, 'rb', 8192) assert fd.readline() == 'hello\r\n' try: self.assertEquals('', fd.read(10)) except SSL.ZeroReturnError: # if it's a GreenSSL object it'll do this pass shutdown_safe(client) client.close()
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()
def test_duplex_response (self): def serve (listener): sock, addr = listener.accept() stuff = sock.read(8192) sock.write('response') sock = listen_ssl_socket() server_coro = evy.spawn(serve, sock) client = util.wrap_ssl(connect(('127.0.0.1', sock.getsockname()[1]))) client.write('line 1\r\nline 2\r\n\r\n') self.assertEquals(client.read(8192), 'response') server_coro.wait()
def test_ssl_connect (self): def serve (listener): sock, addr = listener.accept() stuff = sock.read(8192) sock = listen_ssl_socket() server_coro = evy.spawn(serve, sock) raw_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ssl_client = util.wrap_ssl(raw_client) ssl_client.connect(('127.0.0.1', sock.getsockname()[1])) ssl_client.write('abc') shutdown_safe(ssl_client) ssl_client.close() server_coro.wait()
def test_ssl_close (self): def serve (listener): sock, addr = listener.accept() stuff = sock.read(8192) try: self.assertEquals("", sock.read(8192)) except SSL.ZeroReturnError: pass sock = listen_ssl_socket() server_coro = evy.spawn(serve, sock) raw_client = connect(('127.0.0.1', sock.getsockname()[1])) client = util.wrap_ssl(raw_client) client.write('X') shutdown_safe(client) client.close() server_coro.wait()
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()
def listen_ssl_socket (address = ('127.0.0.1', 0)): sock = util.wrap_ssl(socket.socket(), certificate_file, private_key_file, True) sock.bind(address) sock.listen(50) return sock
def ssl (sock, certificate = None, private_key = None): from evy import util wrapped = util.wrap_ssl(sock, certificate, private_key) return GreenSSLObject(wrapped)