Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 1 | # 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 | |
| 9 | from itertools import count |
| 10 | from threading import Thread |
| 11 | |
| 12 | from OpenSSL.SSL import Context, TLSv1_METHOD |
| 13 | from OpenSSL.crypto import TYPE_RSA, FILETYPE_PEM, PKey, dump_privatekey |
| 14 | |
| 15 | k = PKey() |
| 16 | k.generate_key(TYPE_RSA, 128) |
| 17 | file('pkey.pem', 'w').write(dump_privatekey(FILETYPE_PEM, k, "blowfish", "foobar")) |
| 18 | |
| 19 | count = count() |
| 20 | def 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 | |
| 29 | threads = [Thread(target=go, args=()) for i in xrange(2)] |
| 30 | for th in threads: |
| 31 | th.start() |
| 32 | for th in threads: |
| 33 | th.join() |