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