blob: ba71655da9de2126e8e6686efe97c6c346324872 [file] [log] [blame]
Jean-Paul Calderonec7b3c892011-03-02 19:40:02 -05001# Copyright (C) Jean-Paul Calderone
2# See LICENSE for details.
Jean-Paul Calderone828c9cb2008-04-26 18:06:54 -04003#
4# Stress tester for thread-related bugs in global_passphrase_callback in
5# src/ssl/context.c. In 0.7 and earlier, this will somewhat reliably
6# segfault or abort after a few dozen to a few thousand iterations on an SMP
7# machine (generally not on a UP machine) due to uses of Python/C API
8# without holding the GIL.
9
10from itertools import count
11from threading import Thread
12
13from OpenSSL.SSL import Context, TLSv1_METHOD
14from OpenSSL.crypto import TYPE_RSA, FILETYPE_PEM, PKey, dump_privatekey
15
16k = PKey()
17k.generate_key(TYPE_RSA, 128)
18file('pkey.pem', 'w').write(dump_privatekey(FILETYPE_PEM, k, "blowfish", "foobar"))
19
20count = count()
21def go():
22 def cb(a, b, c):
23 print count.next()
24 return "foobar"
25 c = Context(TLSv1_METHOD)
26 c.set_passwd_cb(cb)
27 while 1:
28 c.use_privatekey_file('pkey.pem')
29
30threads = [Thread(target=go, args=()) for i in xrange(2)]
31for th in threads:
32 th.start()
33for th in threads:
34 th.join()