blob: bd9426d7b9694803d60ade24281eb8f17f573585 [file] [log] [blame]
Jean-Paul Calderonec7b3c892011-03-02 19:40:02 -05001# Copyright (C) Jean-Paul Calderone
2# See LICENSE for details.
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -04003#
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
12from socket import socket
13from threading import Thread
14
15from OpenSSL.SSL import Connection, Context, TLSv1_METHOD
16
Alex Gaynor03737182020-07-23 20:40:46 -040017
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040018def send(conn):
19 while 1:
20 for i in xrange(1024 * 32):
Alex Gaynor03737182020-07-23 20:40:46 -040021 conn.send("x")
22 print "Sent 32KB on", hex(id(conn))
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040023
24
25def recv(conn):
26 while 1:
27 for i in xrange(1024 * 64):
28 conn.recv(1)
Alex Gaynor03737182020-07-23 20:40:46 -040029 print "Received 64KB on", hex(id(conn))
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040030
31
32def main():
33 port = socket()
Alex Gaynor03737182020-07-23 20:40:46 -040034 port.bind(("", 0))
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040035 port.listen(5)
36
37 client = socket()
38 client.setblocking(False)
39 client.connect_ex(port.getsockname())
40 client.setblocking(True)
41
42 server = port.accept()[0]
43
44 clientCtx = Context(TLSv1_METHOD)
Alex Gaynor03737182020-07-23 20:40:46 -040045 clientCtx.set_cipher_list("ALL:ADH")
46 clientCtx.load_tmp_dh("dhparam.pem")
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040047
48 sslClient = Connection(clientCtx, client)
49 sslClient.set_connect_state()
50
51 serverCtx = Context(TLSv1_METHOD)
Alex Gaynor03737182020-07-23 20:40:46 -040052 serverCtx.set_cipher_list("ALL:ADH")
53 serverCtx.load_tmp_dh("dhparam.pem")
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040054
55 sslServer = Connection(serverCtx, server)
56 sslServer.set_accept_state()
57
58 t1 = Thread(target=send, args=(sslClient,))
59 t2 = Thread(target=send, args=(sslServer,))
60 t3 = Thread(target=recv, args=(sslClient,))
61 t4 = Thread(target=recv, args=(sslServer,))
62
63 t1.start()
64 t2.start()
65 t3.start()
66 t4.start()
67 t1.join()
68 t2.join()
69 t3.join()
70 t4.join()
71
Alex Gaynor03737182020-07-23 20:40:46 -040072
Jean-Paul Calderone00db9da2008-09-21 17:42:34 -040073main()