Jean-Paul Calderone | c7b3c89 | 2011-03-02 19:40:02 -0500 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone |
| 2 | # See LICENSE for details. |
Jean-Paul Calderone | 00db9da | 2008-09-21 17:42:34 -0400 | [diff] [blame] | 3 | # |
| 4 | # Stress tester for thread-related bugs in ssl_Connection_send and |
| 5 | # ssl_Connection_recv in src/ssl/connection.c for usage of a single |
| 6 | # Connection object simultaneously in multiple threads. In 0.7 and earlier, |
| 7 | # this will somewhat reliably cause Python to abort with a "tstate mix-up" |
| 8 | # almost immediately, due to the incorrect sharing between threads of the |
| 9 | # `tstate` field of the connection object. |
| 10 | |
| 11 | |
| 12 | from socket import socket |
| 13 | from threading import Thread |
| 14 | |
| 15 | from OpenSSL.SSL import Connection, Context, TLSv1_METHOD |
| 16 | |
| 17 | def send(conn): |
| 18 | while 1: |
| 19 | for i in xrange(1024 * 32): |
| 20 | conn.send('x') |
| 21 | print 'Sent 32KB on', hex(id(conn)) |
| 22 | |
| 23 | |
| 24 | def recv(conn): |
| 25 | while 1: |
| 26 | for i in xrange(1024 * 64): |
| 27 | conn.recv(1) |
| 28 | print 'Received 64KB on', hex(id(conn)) |
| 29 | |
| 30 | |
| 31 | def main(): |
| 32 | port = socket() |
| 33 | port.bind(('', 0)) |
| 34 | port.listen(5) |
| 35 | |
| 36 | client = socket() |
| 37 | client.setblocking(False) |
| 38 | client.connect_ex(port.getsockname()) |
| 39 | client.setblocking(True) |
| 40 | |
| 41 | server = port.accept()[0] |
| 42 | |
| 43 | clientCtx = Context(TLSv1_METHOD) |
| 44 | clientCtx.set_cipher_list('ALL:ADH') |
| 45 | clientCtx.load_tmp_dh('dhparam.pem') |
| 46 | |
| 47 | sslClient = Connection(clientCtx, client) |
| 48 | sslClient.set_connect_state() |
| 49 | |
| 50 | serverCtx = Context(TLSv1_METHOD) |
| 51 | serverCtx.set_cipher_list('ALL:ADH') |
| 52 | serverCtx.load_tmp_dh('dhparam.pem') |
| 53 | |
| 54 | sslServer = Connection(serverCtx, server) |
| 55 | sslServer.set_accept_state() |
| 56 | |
| 57 | t1 = Thread(target=send, args=(sslClient,)) |
| 58 | t2 = Thread(target=send, args=(sslServer,)) |
| 59 | t3 = Thread(target=recv, args=(sslClient,)) |
| 60 | t4 = Thread(target=recv, args=(sslServer,)) |
| 61 | |
| 62 | t1.start() |
| 63 | t2.start() |
| 64 | t3.start() |
| 65 | t4.start() |
| 66 | t1.join() |
| 67 | t2.join() |
| 68 | t3.join() |
| 69 | t4.join() |
| 70 | |
| 71 | main() |