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