Jean-Paul Calderone | 8671c85 | 2011-03-02 19:26:20 -0500 | [diff] [blame] | 1 | # Copyright (C) Jean-Paul Calderone |
| 2 | # See LICENSE for details. |
Jean-Paul Calderone | 8b63d45 | 2008-03-21 18:31:12 -0400 | [diff] [blame] | 3 | |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 4 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 5 | Unit tests for :py:obj:`OpenSSL.SSL`. |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 6 | """ |
| 7 | |
Jean-Paul Calderone | 4c1aacd | 2014-01-11 08:15:17 -0500 | [diff] [blame] | 8 | from gc import collect, get_referrers |
Konstantinos Koukopoulos | 541150d | 2014-01-31 01:00:19 +0200 | [diff] [blame] | 9 | from errno import ECONNREFUSED, EINPROGRESS, EWOULDBLOCK, EPIPE, ESHUTDOWN |
Jean-Paul Calderone | 516c12c | 2015-04-13 20:34:57 -0400 | [diff] [blame] | 10 | from sys import platform, getfilesystemencoding |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 11 | from socket import MSG_PEEK, SHUT_RDWR, error, socket |
Jean-Paul Calderone | a65cf6c | 2009-07-19 10:26:52 -0400 | [diff] [blame] | 12 | from os import makedirs |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 13 | from os.path import join |
Jean-Paul Calderone | 0b88b6a | 2009-07-05 12:44:41 -0400 | [diff] [blame] | 14 | from unittest import main |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 15 | from weakref import ref |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 16 | from warnings import catch_warnings, simplefilter |
Jean-Paul Calderone | 460cc1f | 2009-03-07 11:31:12 -0500 | [diff] [blame] | 17 | |
Alex Gaynor | 1aabb2e | 2015-09-05 13:35:18 -0400 | [diff] [blame] | 18 | import pytest |
| 19 | |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 20 | from six import PY3, text_type, u |
Jean-Paul Calderone | c76c61c | 2014-01-18 13:21:52 -0500 | [diff] [blame] | 21 | |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 22 | from OpenSSL.crypto import TYPE_RSA, FILETYPE_PEM |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 23 | from OpenSSL.crypto import PKey, X509, X509Extension, X509Store |
Jean-Paul Calderone | 7526d17 | 2010-09-09 17:55:31 -0400 | [diff] [blame] | 24 | from OpenSSL.crypto import dump_privatekey, load_privatekey |
| 25 | from OpenSSL.crypto import dump_certificate, load_certificate |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 26 | from OpenSSL.crypto import get_elliptic_curves |
Jean-Paul Calderone | 7526d17 | 2010-09-09 17:55:31 -0400 | [diff] [blame] | 27 | |
Jean-Paul Calderone | 9f2e38e | 2011-04-14 09:36:55 -0400 | [diff] [blame] | 28 | from OpenSSL.SSL import OPENSSL_VERSION_NUMBER, SSLEAY_VERSION, SSLEAY_CFLAGS |
| 29 | from OpenSSL.SSL import SSLEAY_PLATFORM, SSLEAY_DIR, SSLEAY_BUILT_ON |
Jean-Paul Calderone | e4f6b47 | 2010-07-29 22:50:58 -0400 | [diff] [blame] | 30 | from OpenSSL.SSL import SENT_SHUTDOWN, RECEIVED_SHUTDOWN |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 31 | from OpenSSL.SSL import ( |
| 32 | SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, TLSv1_METHOD, |
| 33 | TLSv1_1_METHOD, TLSv1_2_METHOD) |
| 34 | from OpenSSL.SSL import OP_SINGLE_DH_USE, OP_NO_SSLv2, OP_NO_SSLv3 |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 35 | from OpenSSL.SSL import ( |
| 36 | VERIFY_PEER, VERIFY_FAIL_IF_NO_PEER_CERT, VERIFY_CLIENT_ONCE, VERIFY_NONE) |
Jean-Paul Calderone | 0222a49 | 2011-09-08 18:26:03 -0400 | [diff] [blame] | 37 | |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 38 | from OpenSSL.SSL import ( |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 39 | SESS_CACHE_OFF, SESS_CACHE_CLIENT, SESS_CACHE_SERVER, SESS_CACHE_BOTH, |
| 40 | SESS_CACHE_NO_AUTO_CLEAR, SESS_CACHE_NO_INTERNAL_LOOKUP, |
| 41 | SESS_CACHE_NO_INTERNAL_STORE, SESS_CACHE_NO_INTERNAL) |
| 42 | |
| 43 | from OpenSSL.SSL import ( |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 44 | Error, SysCallError, WantReadError, WantWriteError, ZeroReturnError) |
Jean-Paul Calderone | e0fcf51 | 2012-02-13 09:10:15 -0500 | [diff] [blame] | 45 | from OpenSSL.SSL import ( |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 46 | Context, ContextType, Session, Connection, ConnectionType, SSLeay_version) |
Jean-Paul Calderone | 7526d17 | 2010-09-09 17:55:31 -0400 | [diff] [blame] | 47 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 48 | from OpenSSL._util import lib as _lib |
| 49 | |
Jean-Paul Calderone | 17eca48 | 2015-04-13 20:31:07 -0400 | [diff] [blame] | 50 | from OpenSSL.test.util import WARNING_TYPE_EXPECTED, NON_ASCII, TestCase, b |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 51 | from OpenSSL.test.test_crypto import ( |
Jean-Paul Calderone | da6399a | 2015-04-13 20:52:29 -0400 | [diff] [blame] | 52 | cleartextCertificatePEM, cleartextPrivateKeyPEM, |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 53 | client_cert_pem, client_key_pem, server_cert_pem, server_key_pem, |
| 54 | root_cert_pem) |
| 55 | |
Jean-Paul Calderone | 4bccf5e | 2008-12-28 22:50:42 -0500 | [diff] [blame] | 56 | try: |
| 57 | from OpenSSL.SSL import OP_NO_QUERY_MTU |
| 58 | except ImportError: |
| 59 | OP_NO_QUERY_MTU = None |
| 60 | try: |
| 61 | from OpenSSL.SSL import OP_COOKIE_EXCHANGE |
| 62 | except ImportError: |
| 63 | OP_COOKIE_EXCHANGE = None |
| 64 | try: |
| 65 | from OpenSSL.SSL import OP_NO_TICKET |
| 66 | except ImportError: |
| 67 | OP_NO_TICKET = None |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 68 | |
Jean-Paul Calderone | 0222a49 | 2011-09-08 18:26:03 -0400 | [diff] [blame] | 69 | try: |
Jean-Paul Calderone | c62d4c1 | 2011-09-08 18:29:32 -0400 | [diff] [blame] | 70 | from OpenSSL.SSL import OP_NO_COMPRESSION |
| 71 | except ImportError: |
| 72 | OP_NO_COMPRESSION = None |
| 73 | |
| 74 | try: |
Jean-Paul Calderone | 0222a49 | 2011-09-08 18:26:03 -0400 | [diff] [blame] | 75 | from OpenSSL.SSL import MODE_RELEASE_BUFFERS |
| 76 | except ImportError: |
| 77 | MODE_RELEASE_BUFFERS = None |
| 78 | |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 79 | try: |
| 80 | from OpenSSL.SSL import OP_NO_TLSv1, OP_NO_TLSv1_1, OP_NO_TLSv1_2 |
| 81 | except ImportError: |
| 82 | OP_NO_TLSv1 = OP_NO_TLSv1_1 = OP_NO_TLSv1_2 = None |
| 83 | |
Jean-Paul Calderone | 31e85a8 | 2011-03-21 19:13:35 -0400 | [diff] [blame] | 84 | from OpenSSL.SSL import ( |
| 85 | SSL_ST_CONNECT, SSL_ST_ACCEPT, SSL_ST_MASK, SSL_ST_INIT, SSL_ST_BEFORE, |
| 86 | SSL_ST_OK, SSL_ST_RENEGOTIATE, |
| 87 | SSL_CB_LOOP, SSL_CB_EXIT, SSL_CB_READ, SSL_CB_WRITE, SSL_CB_ALERT, |
| 88 | SSL_CB_READ_ALERT, SSL_CB_WRITE_ALERT, SSL_CB_ACCEPT_LOOP, |
| 89 | SSL_CB_ACCEPT_EXIT, SSL_CB_CONNECT_LOOP, SSL_CB_CONNECT_EXIT, |
| 90 | SSL_CB_HANDSHAKE_START, SSL_CB_HANDSHAKE_DONE) |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 91 | |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 92 | # openssl dhparam 128 -out dh-128.pem (note that 128 is a small number of bits |
| 93 | # to use) |
| 94 | dhparam = """\ |
| 95 | -----BEGIN DH PARAMETERS----- |
| 96 | MBYCEQCobsg29c9WZP/54oAPcwiDAgEC |
| 97 | -----END DH PARAMETERS----- |
| 98 | """ |
| 99 | |
| 100 | |
Jean-Paul Calderone | 0582673 | 2015-04-12 11:38:49 -0400 | [diff] [blame] | 101 | def join_bytes_or_unicode(prefix, suffix): |
| 102 | """ |
| 103 | Join two path components of either ``bytes`` or ``unicode``. |
| 104 | |
| 105 | The return type is the same as the type of ``prefix``. |
| 106 | """ |
| 107 | # If the types are the same, nothing special is necessary. |
| 108 | if type(prefix) == type(suffix): |
| 109 | return join(prefix, suffix) |
| 110 | |
| 111 | # Otherwise, coerce suffix to the type of prefix. |
| 112 | if isinstance(prefix, text_type): |
| 113 | return join(prefix, suffix.decode(getfilesystemencoding())) |
| 114 | else: |
| 115 | return join(prefix, suffix.encode(getfilesystemencoding())) |
| 116 | |
| 117 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 118 | def verify_cb(conn, cert, errnum, depth, ok): |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 119 | return ok |
| 120 | |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 121 | |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 122 | def socket_pair(): |
Jean-Paul Calderone | 1a9613b | 2009-07-16 12:13:36 -0400 | [diff] [blame] | 123 | """ |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 124 | Establish and return a pair of network sockets connected to each other. |
Jean-Paul Calderone | 1a9613b | 2009-07-16 12:13:36 -0400 | [diff] [blame] | 125 | """ |
| 126 | # Connect a pair of sockets |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 127 | port = socket() |
| 128 | port.bind(('', 0)) |
| 129 | port.listen(1) |
| 130 | client = socket() |
| 131 | client.setblocking(False) |
Jean-Paul Calderone | f23c5d9 | 2009-07-23 17:58:15 -0400 | [diff] [blame] | 132 | client.connect_ex(("127.0.0.1", port.getsockname()[1])) |
Jean-Paul Calderone | 94b24a8 | 2009-07-16 19:11:38 -0400 | [diff] [blame] | 133 | client.setblocking(True) |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 134 | server = port.accept()[0] |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 135 | |
Jean-Paul Calderone | 1a9613b | 2009-07-16 12:13:36 -0400 | [diff] [blame] | 136 | # Let's pass some unencrypted data to make sure our socket connection is |
| 137 | # fine. Just one byte, so we don't have to worry about buffers getting |
| 138 | # filled up or fragmentation. |
Jean-Paul Calderone | 9e4eeae | 2010-08-22 21:32:52 -0400 | [diff] [blame] | 139 | server.send(b("x")) |
| 140 | assert client.recv(1024) == b("x") |
| 141 | client.send(b("y")) |
| 142 | assert server.recv(1024) == b("y") |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 143 | |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 144 | # Most of our callers want non-blocking sockets, make it easy for them. |
Jean-Paul Calderone | 94b24a8 | 2009-07-16 19:11:38 -0400 | [diff] [blame] | 145 | server.setblocking(False) |
| 146 | client.setblocking(False) |
Jim Shaver | 46f2891 | 2015-05-29 19:32:16 -0400 | [diff] [blame] | 147 | |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 148 | return (server, client) |
| 149 | |
| 150 | |
Jean-Paul Calderone | 94b24a8 | 2009-07-16 19:11:38 -0400 | [diff] [blame] | 151 | |
Jean-Paul Calderone | f874203 | 2010-09-25 00:00:32 -0400 | [diff] [blame] | 152 | def handshake(client, server): |
| 153 | conns = [client, server] |
| 154 | while conns: |
| 155 | for conn in conns: |
| 156 | try: |
| 157 | conn.do_handshake() |
| 158 | except WantReadError: |
| 159 | pass |
| 160 | else: |
| 161 | conns.remove(conn) |
| 162 | |
| 163 | |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 164 | def _create_certificate_chain(): |
| 165 | """ |
| 166 | Construct and return a chain of certificates. |
| 167 | |
| 168 | 1. A new self-signed certificate authority certificate (cacert) |
| 169 | 2. A new intermediate certificate signed by cacert (icert) |
| 170 | 3. A new server certificate signed by icert (scert) |
| 171 | """ |
| 172 | caext = X509Extension(b('basicConstraints'), False, b('CA:true')) |
| 173 | |
| 174 | # Step 1 |
| 175 | cakey = PKey() |
| 176 | cakey.generate_key(TYPE_RSA, 512) |
| 177 | cacert = X509() |
| 178 | cacert.get_subject().commonName = "Authority Certificate" |
| 179 | cacert.set_issuer(cacert.get_subject()) |
| 180 | cacert.set_pubkey(cakey) |
| 181 | cacert.set_notBefore(b("20000101000000Z")) |
| 182 | cacert.set_notAfter(b("20200101000000Z")) |
| 183 | cacert.add_extensions([caext]) |
| 184 | cacert.set_serial_number(0) |
| 185 | cacert.sign(cakey, "sha1") |
| 186 | |
| 187 | # Step 2 |
| 188 | ikey = PKey() |
| 189 | ikey.generate_key(TYPE_RSA, 512) |
| 190 | icert = X509() |
| 191 | icert.get_subject().commonName = "Intermediate Certificate" |
| 192 | icert.set_issuer(cacert.get_subject()) |
| 193 | icert.set_pubkey(ikey) |
| 194 | icert.set_notBefore(b("20000101000000Z")) |
| 195 | icert.set_notAfter(b("20200101000000Z")) |
| 196 | icert.add_extensions([caext]) |
| 197 | icert.set_serial_number(0) |
| 198 | icert.sign(cakey, "sha1") |
| 199 | |
| 200 | # Step 3 |
| 201 | skey = PKey() |
| 202 | skey.generate_key(TYPE_RSA, 512) |
| 203 | scert = X509() |
| 204 | scert.get_subject().commonName = "Server Certificate" |
| 205 | scert.set_issuer(icert.get_subject()) |
| 206 | scert.set_pubkey(skey) |
| 207 | scert.set_notBefore(b("20000101000000Z")) |
| 208 | scert.set_notAfter(b("20200101000000Z")) |
| 209 | scert.add_extensions([ |
| 210 | X509Extension(b('basicConstraints'), True, b('CA:false'))]) |
| 211 | scert.set_serial_number(0) |
| 212 | scert.sign(ikey, "sha1") |
| 213 | |
| 214 | return [(cakey, cacert), (ikey, icert), (skey, scert)] |
| 215 | |
| 216 | |
| 217 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 218 | class _LoopbackMixin: |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 219 | """ |
| 220 | Helper mixin which defines methods for creating a connected socket pair and |
| 221 | for forcing two connected SSL sockets to talk to each other via memory BIOs. |
| 222 | """ |
Jean-Paul Calderone | fef5c4b | 2012-02-14 16:31:52 -0500 | [diff] [blame] | 223 | def _loopbackClientFactory(self, socket): |
| 224 | client = Connection(Context(TLSv1_METHOD), socket) |
| 225 | client.set_connect_state() |
| 226 | return client |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 227 | |
Jean-Paul Calderone | fef5c4b | 2012-02-14 16:31:52 -0500 | [diff] [blame] | 228 | |
| 229 | def _loopbackServerFactory(self, socket): |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 230 | ctx = Context(TLSv1_METHOD) |
| 231 | ctx.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 232 | ctx.use_certificate(load_certificate(FILETYPE_PEM, server_cert_pem)) |
Jean-Paul Calderone | fef5c4b | 2012-02-14 16:31:52 -0500 | [diff] [blame] | 233 | server = Connection(ctx, socket) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 234 | server.set_accept_state() |
Jean-Paul Calderone | fef5c4b | 2012-02-14 16:31:52 -0500 | [diff] [blame] | 235 | return server |
| 236 | |
| 237 | |
| 238 | def _loopback(self, serverFactory=None, clientFactory=None): |
| 239 | if serverFactory is None: |
| 240 | serverFactory = self._loopbackServerFactory |
| 241 | if clientFactory is None: |
| 242 | clientFactory = self._loopbackClientFactory |
| 243 | |
| 244 | (server, client) = socket_pair() |
| 245 | server = serverFactory(server) |
| 246 | client = clientFactory(client) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 247 | |
Jean-Paul Calderone | f874203 | 2010-09-25 00:00:32 -0400 | [diff] [blame] | 248 | handshake(client, server) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 249 | |
| 250 | server.setblocking(True) |
| 251 | client.setblocking(True) |
| 252 | return server, client |
| 253 | |
| 254 | |
| 255 | def _interactInMemory(self, client_conn, server_conn): |
| 256 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 257 | Try to read application bytes from each of the two :py:obj:`Connection` |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 258 | objects. Copy bytes back and forth between their send/receive buffers |
| 259 | for as long as there is anything to copy. When there is nothing more |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 260 | to copy, return :py:obj:`None`. If one of them actually manages to deliver |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 261 | some application bytes, return a two-tuple of the connection from which |
| 262 | the bytes were read and the bytes themselves. |
| 263 | """ |
| 264 | wrote = True |
| 265 | while wrote: |
| 266 | # Loop until neither side has anything to say |
| 267 | wrote = False |
| 268 | |
| 269 | # Copy stuff from each side's send buffer to the other side's |
| 270 | # receive buffer. |
| 271 | for (read, write) in [(client_conn, server_conn), |
| 272 | (server_conn, client_conn)]: |
| 273 | |
| 274 | # Give the side a chance to generate some more bytes, or |
| 275 | # succeed. |
| 276 | try: |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 277 | data = read.recv(2 ** 16) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 278 | except WantReadError: |
| 279 | # It didn't succeed, so we'll hope it generated some |
| 280 | # output. |
| 281 | pass |
| 282 | else: |
| 283 | # It did succeed, so we'll stop now and let the caller deal |
| 284 | # with it. |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 285 | return (read, data) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 286 | |
| 287 | while True: |
| 288 | # Keep copying as long as there's more stuff there. |
| 289 | try: |
| 290 | dirty = read.bio_read(4096) |
| 291 | except WantReadError: |
| 292 | # Okay, nothing more waiting to be sent. Stop |
| 293 | # processing this send buffer. |
| 294 | break |
| 295 | else: |
| 296 | # Keep track of the fact that someone generated some |
| 297 | # output. |
| 298 | wrote = True |
| 299 | write.bio_write(dirty) |
| 300 | |
| 301 | |
Jean-Paul Calderone | 6a8cd11 | 2014-04-02 21:09:08 -0400 | [diff] [blame] | 302 | def _handshakeInMemory(self, client_conn, server_conn): |
Jean-Paul Calderone | b2b4078 | 2014-05-06 08:47:40 -0400 | [diff] [blame] | 303 | """ |
| 304 | Perform the TLS handshake between two :py:class:`Connection` instances |
| 305 | connected to each other via memory BIOs. |
| 306 | """ |
Jean-Paul Calderone | 6a8cd11 | 2014-04-02 21:09:08 -0400 | [diff] [blame] | 307 | client_conn.set_connect_state() |
| 308 | server_conn.set_accept_state() |
| 309 | |
| 310 | for conn in [client_conn, server_conn]: |
| 311 | try: |
| 312 | conn.do_handshake() |
| 313 | except WantReadError: |
| 314 | pass |
| 315 | |
| 316 | self._interactInMemory(client_conn, server_conn) |
| 317 | |
| 318 | |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 319 | |
Jean-Paul Calderone | 9f2e38e | 2011-04-14 09:36:55 -0400 | [diff] [blame] | 320 | class VersionTests(TestCase): |
| 321 | """ |
| 322 | Tests for version information exposed by |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 323 | :py:obj:`OpenSSL.SSL.SSLeay_version` and |
| 324 | :py:obj:`OpenSSL.SSL.OPENSSL_VERSION_NUMBER`. |
Jean-Paul Calderone | 9f2e38e | 2011-04-14 09:36:55 -0400 | [diff] [blame] | 325 | """ |
| 326 | def test_OPENSSL_VERSION_NUMBER(self): |
| 327 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 328 | :py:obj:`OPENSSL_VERSION_NUMBER` is an integer with status in the low |
Jean-Paul Calderone | 9f2e38e | 2011-04-14 09:36:55 -0400 | [diff] [blame] | 329 | byte and the patch, fix, minor, and major versions in the |
| 330 | nibbles above that. |
| 331 | """ |
| 332 | self.assertTrue(isinstance(OPENSSL_VERSION_NUMBER, int)) |
| 333 | |
| 334 | |
| 335 | def test_SSLeay_version(self): |
| 336 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 337 | :py:obj:`SSLeay_version` takes a version type indicator and returns |
Jean-Paul Calderone | 9f2e38e | 2011-04-14 09:36:55 -0400 | [diff] [blame] | 338 | one of a number of version strings based on that indicator. |
| 339 | """ |
| 340 | versions = {} |
| 341 | for t in [SSLEAY_VERSION, SSLEAY_CFLAGS, SSLEAY_BUILT_ON, |
| 342 | SSLEAY_PLATFORM, SSLEAY_DIR]: |
| 343 | version = SSLeay_version(t) |
| 344 | versions[version] = t |
| 345 | self.assertTrue(isinstance(version, bytes)) |
| 346 | self.assertEqual(len(versions), 5) |
| 347 | |
| 348 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 349 | |
| 350 | class ContextTests(TestCase, _LoopbackMixin): |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 351 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 352 | Unit tests for :py:obj:`OpenSSL.SSL.Context`. |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 353 | """ |
| 354 | def test_method(self): |
| 355 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 356 | :py:obj:`Context` can be instantiated with one of :py:obj:`SSLv2_METHOD`, |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 357 | :py:obj:`SSLv3_METHOD`, :py:obj:`SSLv23_METHOD`, :py:obj:`TLSv1_METHOD`, |
| 358 | :py:obj:`TLSv1_1_METHOD`, or :py:obj:`TLSv1_2_METHOD`. |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 359 | """ |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 360 | methods = [ |
| 361 | SSLv3_METHOD, SSLv23_METHOD, TLSv1_METHOD] |
| 362 | for meth in methods: |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 363 | Context(meth) |
Jean-Paul Calderone | 9f2e38e | 2011-04-14 09:36:55 -0400 | [diff] [blame] | 364 | |
Jean-Paul Calderone | 1461c49 | 2013-10-03 16:05:00 -0400 | [diff] [blame] | 365 | |
| 366 | maybe = [SSLv2_METHOD, TLSv1_1_METHOD, TLSv1_2_METHOD] |
| 367 | for meth in maybe: |
| 368 | try: |
| 369 | Context(meth) |
| 370 | except (Error, ValueError): |
| 371 | # Some versions of OpenSSL have SSLv2 / TLSv1.1 / TLSv1.2, some |
| 372 | # don't. Difficult to say in advance. |
| 373 | pass |
Jean-Paul Calderone | 9f2e38e | 2011-04-14 09:36:55 -0400 | [diff] [blame] | 374 | |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 375 | self.assertRaises(TypeError, Context, "") |
| 376 | self.assertRaises(ValueError, Context, 10) |
| 377 | |
| 378 | |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 379 | if not PY3: |
| 380 | def test_method_long(self): |
| 381 | """ |
| 382 | On Python 2 :py:class:`Context` accepts values of type |
| 383 | :py:obj:`long` as well as :py:obj:`int`. |
| 384 | """ |
| 385 | Context(long(TLSv1_METHOD)) |
| 386 | |
| 387 | |
| 388 | |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 389 | def test_type(self): |
| 390 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 391 | :py:obj:`Context` and :py:obj:`ContextType` refer to the same type object and can be |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 392 | used to create instances of that type. |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 393 | """ |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 394 | self.assertIdentical(Context, ContextType) |
| 395 | self.assertConsistentType(Context, 'Context', TLSv1_METHOD) |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 396 | |
| 397 | |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 398 | def test_use_privatekey(self): |
| 399 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 400 | :py:obj:`Context.use_privatekey` takes an :py:obj:`OpenSSL.crypto.PKey` instance. |
Jean-Paul Calderone | 30c09ea | 2008-03-21 17:04:05 -0400 | [diff] [blame] | 401 | """ |
| 402 | key = PKey() |
| 403 | key.generate_key(TYPE_RSA, 128) |
| 404 | ctx = Context(TLSv1_METHOD) |
| 405 | ctx.use_privatekey(key) |
| 406 | self.assertRaises(TypeError, ctx.use_privatekey, "") |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 407 | |
| 408 | |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 409 | def test_use_privatekey_file_missing(self): |
| 410 | """ |
| 411 | :py:obj:`Context.use_privatekey_file` raises :py:obj:`OpenSSL.SSL.Error` |
| 412 | when passed the name of a file which does not exist. |
| 413 | """ |
| 414 | ctx = Context(TLSv1_METHOD) |
| 415 | self.assertRaises(Error, ctx.use_privatekey_file, self.mktemp()) |
| 416 | |
| 417 | |
Jean-Paul Calderone | 69a4e5b | 2015-04-12 10:04:28 -0400 | [diff] [blame] | 418 | def _use_privatekey_file_test(self, pemfile, filetype): |
| 419 | """ |
| 420 | Verify that calling ``Context.use_privatekey_file`` with the given |
| 421 | arguments does not raise an exception. |
| 422 | """ |
| 423 | key = PKey() |
| 424 | key.generate_key(TYPE_RSA, 128) |
| 425 | |
| 426 | with open(pemfile, "wt") as pem: |
| 427 | pem.write( |
| 428 | dump_privatekey(FILETYPE_PEM, key).decode("ascii") |
| 429 | ) |
| 430 | |
| 431 | ctx = Context(TLSv1_METHOD) |
| 432 | ctx.use_privatekey_file(pemfile, filetype) |
| 433 | |
| 434 | |
| 435 | def test_use_privatekey_file_bytes(self): |
| 436 | """ |
| 437 | A private key can be specified from a file by passing a ``bytes`` |
| 438 | instance giving the file name to ``Context.use_privatekey_file``. |
| 439 | """ |
| 440 | self._use_privatekey_file_test( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 441 | self.mktemp() + NON_ASCII.encode(getfilesystemencoding()), |
Jean-Paul Calderone | 69a4e5b | 2015-04-12 10:04:28 -0400 | [diff] [blame] | 442 | FILETYPE_PEM, |
| 443 | ) |
| 444 | |
| 445 | |
| 446 | def test_use_privatekey_file_unicode(self): |
| 447 | """ |
| 448 | A private key can be specified from a file by passing a ``unicode`` |
| 449 | instance giving the file name to ``Context.use_privatekey_file``. |
| 450 | """ |
| 451 | self._use_privatekey_file_test( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 452 | self.mktemp().decode(getfilesystemencoding()) + NON_ASCII, |
Jean-Paul Calderone | 69a4e5b | 2015-04-12 10:04:28 -0400 | [diff] [blame] | 453 | FILETYPE_PEM, |
| 454 | ) |
| 455 | |
| 456 | |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 457 | if not PY3: |
| 458 | def test_use_privatekey_file_long(self): |
| 459 | """ |
| 460 | On Python 2 :py:obj:`Context.use_privatekey_file` accepts a |
| 461 | filetype of type :py:obj:`long` as well as :py:obj:`int`. |
| 462 | """ |
Jean-Paul Calderone | 69a4e5b | 2015-04-12 10:04:28 -0400 | [diff] [blame] | 463 | self._use_privatekey_file_test(self.mktemp(), long(FILETYPE_PEM)) |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 464 | |
| 465 | |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 466 | def test_use_certificate_wrong_args(self): |
| 467 | """ |
| 468 | :py:obj:`Context.use_certificate_wrong_args` raises :py:obj:`TypeError` |
| 469 | when not passed exactly one :py:obj:`OpenSSL.crypto.X509` instance as an |
| 470 | argument. |
| 471 | """ |
| 472 | ctx = Context(TLSv1_METHOD) |
| 473 | self.assertRaises(TypeError, ctx.use_certificate) |
| 474 | self.assertRaises(TypeError, ctx.use_certificate, "hello, world") |
| 475 | self.assertRaises(TypeError, ctx.use_certificate, X509(), "hello, world") |
| 476 | |
| 477 | |
| 478 | def test_use_certificate_uninitialized(self): |
| 479 | """ |
| 480 | :py:obj:`Context.use_certificate` raises :py:obj:`OpenSSL.SSL.Error` |
| 481 | when passed a :py:obj:`OpenSSL.crypto.X509` instance which has not been |
| 482 | initialized (ie, which does not actually have any certificate data). |
| 483 | """ |
| 484 | ctx = Context(TLSv1_METHOD) |
| 485 | self.assertRaises(Error, ctx.use_certificate, X509()) |
| 486 | |
| 487 | |
| 488 | def test_use_certificate(self): |
| 489 | """ |
| 490 | :py:obj:`Context.use_certificate` sets the certificate which will be |
| 491 | used to identify connections created using the context. |
| 492 | """ |
| 493 | # TODO |
| 494 | # Hard to assert anything. But we could set a privatekey then ask |
| 495 | # OpenSSL if the cert and key agree using check_privatekey. Then as |
| 496 | # long as check_privatekey works right we're good... |
| 497 | ctx = Context(TLSv1_METHOD) |
| 498 | ctx.use_certificate(load_certificate(FILETYPE_PEM, cleartextCertificatePEM)) |
| 499 | |
| 500 | |
| 501 | def test_use_certificate_file_wrong_args(self): |
| 502 | """ |
| 503 | :py:obj:`Context.use_certificate_file` raises :py:obj:`TypeError` if |
| 504 | called with zero arguments or more than two arguments, or if the first |
| 505 | argument is not a byte string or the second argumnent is not an integer. |
| 506 | """ |
| 507 | ctx = Context(TLSv1_METHOD) |
| 508 | self.assertRaises(TypeError, ctx.use_certificate_file) |
| 509 | self.assertRaises(TypeError, ctx.use_certificate_file, b"somefile", object()) |
| 510 | self.assertRaises( |
| 511 | TypeError, ctx.use_certificate_file, b"somefile", FILETYPE_PEM, object()) |
| 512 | self.assertRaises( |
| 513 | TypeError, ctx.use_certificate_file, object(), FILETYPE_PEM) |
| 514 | self.assertRaises( |
| 515 | TypeError, ctx.use_certificate_file, b"somefile", object()) |
| 516 | |
| 517 | |
| 518 | def test_use_certificate_file_missing(self): |
| 519 | """ |
| 520 | :py:obj:`Context.use_certificate_file` raises |
| 521 | `:py:obj:`OpenSSL.SSL.Error` if passed the name of a file which does not |
| 522 | exist. |
| 523 | """ |
| 524 | ctx = Context(TLSv1_METHOD) |
| 525 | self.assertRaises(Error, ctx.use_certificate_file, self.mktemp()) |
| 526 | |
| 527 | |
Jean-Paul Calderone | d57a7b6 | 2015-04-12 09:57:36 -0400 | [diff] [blame] | 528 | def _use_certificate_file_test(self, certificate_file): |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 529 | """ |
Jean-Paul Calderone | d57a7b6 | 2015-04-12 09:57:36 -0400 | [diff] [blame] | 530 | Verify that calling ``Context.use_certificate_file`` with the given |
| 531 | filename doesn't raise an exception. |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 532 | """ |
| 533 | # TODO |
| 534 | # Hard to assert anything. But we could set a privatekey then ask |
| 535 | # OpenSSL if the cert and key agree using check_privatekey. Then as |
| 536 | # long as check_privatekey works right we're good... |
Jean-Paul Calderone | d57a7b6 | 2015-04-12 09:57:36 -0400 | [diff] [blame] | 537 | with open(certificate_file, "wb") as pem_file: |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 538 | pem_file.write(cleartextCertificatePEM) |
| 539 | |
| 540 | ctx = Context(TLSv1_METHOD) |
Jean-Paul Calderone | d57a7b6 | 2015-04-12 09:57:36 -0400 | [diff] [blame] | 541 | ctx.use_certificate_file(certificate_file) |
| 542 | |
| 543 | |
| 544 | def test_use_certificate_file_bytes(self): |
| 545 | """ |
| 546 | :py:obj:`Context.use_certificate_file` sets the certificate (given as a |
| 547 | ``bytes`` filename) which will be used to identify connections created |
| 548 | using the context. |
| 549 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 550 | filename = self.mktemp() + NON_ASCII.encode(getfilesystemencoding()) |
Jean-Paul Calderone | d57a7b6 | 2015-04-12 09:57:36 -0400 | [diff] [blame] | 551 | self._use_certificate_file_test(filename) |
| 552 | |
| 553 | |
| 554 | def test_use_certificate_file_unicode(self): |
| 555 | """ |
| 556 | :py:obj:`Context.use_certificate_file` sets the certificate (given as a |
| 557 | ``bytes`` filename) which will be used to identify connections created |
| 558 | using the context. |
| 559 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 560 | filename = self.mktemp().decode(getfilesystemencoding()) + NON_ASCII |
Jean-Paul Calderone | d57a7b6 | 2015-04-12 09:57:36 -0400 | [diff] [blame] | 561 | self._use_certificate_file_test(filename) |
Jean-Paul Calderone | 173cff9 | 2013-03-06 10:29:21 -0800 | [diff] [blame] | 562 | |
| 563 | |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 564 | if not PY3: |
| 565 | def test_use_certificate_file_long(self): |
| 566 | """ |
| 567 | On Python 2 :py:obj:`Context.use_certificate_file` accepts a |
| 568 | filetype of type :py:obj:`long` as well as :py:obj:`int`. |
| 569 | """ |
| 570 | pem_filename = self.mktemp() |
| 571 | with open(pem_filename, "wb") as pem_file: |
| 572 | pem_file.write(cleartextCertificatePEM) |
| 573 | |
| 574 | ctx = Context(TLSv1_METHOD) |
| 575 | ctx.use_certificate_file(pem_filename, long(FILETYPE_PEM)) |
| 576 | |
| 577 | |
Jean-Paul Calderone | 932f5cc | 2014-12-11 13:58:00 -0500 | [diff] [blame] | 578 | def test_check_privatekey_valid(self): |
| 579 | """ |
| 580 | :py:obj:`Context.check_privatekey` returns :py:obj:`None` if the |
| 581 | :py:obj:`Context` instance has been configured to use a matched key and |
| 582 | certificate pair. |
| 583 | """ |
| 584 | key = load_privatekey(FILETYPE_PEM, client_key_pem) |
| 585 | cert = load_certificate(FILETYPE_PEM, client_cert_pem) |
| 586 | context = Context(TLSv1_METHOD) |
| 587 | context.use_privatekey(key) |
| 588 | context.use_certificate(cert) |
| 589 | self.assertIs(None, context.check_privatekey()) |
| 590 | |
| 591 | |
| 592 | def test_check_privatekey_invalid(self): |
| 593 | """ |
| 594 | :py:obj:`Context.check_privatekey` raises :py:obj:`Error` if the |
| 595 | :py:obj:`Context` instance has been configured to use a key and |
| 596 | certificate pair which don't relate to each other. |
| 597 | """ |
| 598 | key = load_privatekey(FILETYPE_PEM, client_key_pem) |
| 599 | cert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 600 | context = Context(TLSv1_METHOD) |
| 601 | context.use_privatekey(key) |
| 602 | context.use_certificate(cert) |
| 603 | self.assertRaises(Error, context.check_privatekey) |
| 604 | |
| 605 | |
| 606 | def test_check_privatekey_wrong_args(self): |
| 607 | """ |
| 608 | :py:obj:`Context.check_privatekey` raises :py:obj:`TypeError` if called |
| 609 | with other than no arguments. |
| 610 | """ |
| 611 | context = Context(TLSv1_METHOD) |
| 612 | self.assertRaises(TypeError, context.check_privatekey, object()) |
| 613 | |
| 614 | |
Jean-Paul Calderone | bd47916 | 2010-07-30 18:03:25 -0400 | [diff] [blame] | 615 | def test_set_app_data_wrong_args(self): |
| 616 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 617 | :py:obj:`Context.set_app_data` raises :py:obj:`TypeError` if called with other than |
Jean-Paul Calderone | bd47916 | 2010-07-30 18:03:25 -0400 | [diff] [blame] | 618 | one argument. |
| 619 | """ |
| 620 | context = Context(TLSv1_METHOD) |
| 621 | self.assertRaises(TypeError, context.set_app_data) |
| 622 | self.assertRaises(TypeError, context.set_app_data, None, None) |
| 623 | |
| 624 | |
| 625 | def test_get_app_data_wrong_args(self): |
| 626 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 627 | :py:obj:`Context.get_app_data` raises :py:obj:`TypeError` if called with any |
Jean-Paul Calderone | bd47916 | 2010-07-30 18:03:25 -0400 | [diff] [blame] | 628 | arguments. |
| 629 | """ |
| 630 | context = Context(TLSv1_METHOD) |
| 631 | self.assertRaises(TypeError, context.get_app_data, None) |
| 632 | |
| 633 | |
| 634 | def test_app_data(self): |
| 635 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 636 | :py:obj:`Context.set_app_data` stores an object for later retrieval using |
| 637 | :py:obj:`Context.get_app_data`. |
Jean-Paul Calderone | bd47916 | 2010-07-30 18:03:25 -0400 | [diff] [blame] | 638 | """ |
| 639 | app_data = object() |
| 640 | context = Context(TLSv1_METHOD) |
| 641 | context.set_app_data(app_data) |
| 642 | self.assertIdentical(context.get_app_data(), app_data) |
| 643 | |
| 644 | |
Jean-Paul Calderone | 40569ca | 2010-07-30 18:04:58 -0400 | [diff] [blame] | 645 | def test_set_options_wrong_args(self): |
| 646 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 647 | :py:obj:`Context.set_options` raises :py:obj:`TypeError` if called with the wrong |
| 648 | number of arguments or a non-:py:obj:`int` argument. |
Jean-Paul Calderone | 40569ca | 2010-07-30 18:04:58 -0400 | [diff] [blame] | 649 | """ |
| 650 | context = Context(TLSv1_METHOD) |
| 651 | self.assertRaises(TypeError, context.set_options) |
| 652 | self.assertRaises(TypeError, context.set_options, None) |
| 653 | self.assertRaises(TypeError, context.set_options, 1, None) |
| 654 | |
| 655 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 656 | def test_set_options(self): |
| 657 | """ |
| 658 | :py:obj:`Context.set_options` returns the new options value. |
| 659 | """ |
| 660 | context = Context(TLSv1_METHOD) |
| 661 | options = context.set_options(OP_NO_SSLv2) |
| 662 | self.assertTrue(OP_NO_SSLv2 & options) |
| 663 | |
| 664 | |
| 665 | if not PY3: |
| 666 | def test_set_options_long(self): |
| 667 | """ |
| 668 | On Python 2 :py:obj:`Context.set_options` accepts values of type |
| 669 | :py:obj:`long` as well as :py:obj:`int`. |
| 670 | """ |
| 671 | context = Context(TLSv1_METHOD) |
| 672 | options = context.set_options(long(OP_NO_SSLv2)) |
| 673 | self.assertTrue(OP_NO_SSLv2 & options) |
| 674 | |
| 675 | |
Guillermo Gonzalez | 74a2c29 | 2011-08-29 16:16:58 -0300 | [diff] [blame] | 676 | def test_set_mode_wrong_args(self): |
| 677 | """ |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 678 | :py:obj:`Context.set`mode} raises :py:obj:`TypeError` if called with the wrong |
| 679 | number of arguments or a non-:py:obj:`int` argument. |
Guillermo Gonzalez | 74a2c29 | 2011-08-29 16:16:58 -0300 | [diff] [blame] | 680 | """ |
| 681 | context = Context(TLSv1_METHOD) |
| 682 | self.assertRaises(TypeError, context.set_mode) |
| 683 | self.assertRaises(TypeError, context.set_mode, None) |
| 684 | self.assertRaises(TypeError, context.set_mode, 1, None) |
| 685 | |
| 686 | |
Jean-Paul Calderone | 0222a49 | 2011-09-08 18:26:03 -0400 | [diff] [blame] | 687 | if MODE_RELEASE_BUFFERS is not None: |
| 688 | def test_set_mode(self): |
| 689 | """ |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 690 | :py:obj:`Context.set_mode` accepts a mode bitvector and returns the newly |
Jean-Paul Calderone | 0222a49 | 2011-09-08 18:26:03 -0400 | [diff] [blame] | 691 | set mode. |
| 692 | """ |
| 693 | context = Context(TLSv1_METHOD) |
| 694 | self.assertTrue( |
| 695 | MODE_RELEASE_BUFFERS & context.set_mode(MODE_RELEASE_BUFFERS)) |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 696 | |
| 697 | if not PY3: |
| 698 | def test_set_mode_long(self): |
| 699 | """ |
| 700 | On Python 2 :py:obj:`Context.set_mode` accepts values of type |
| 701 | :py:obj:`long` as well as :py:obj:`int`. |
| 702 | """ |
| 703 | context = Context(TLSv1_METHOD) |
| 704 | mode = context.set_mode(long(MODE_RELEASE_BUFFERS)) |
| 705 | self.assertTrue(MODE_RELEASE_BUFFERS & mode) |
Jean-Paul Calderone | 0222a49 | 2011-09-08 18:26:03 -0400 | [diff] [blame] | 706 | else: |
| 707 | "MODE_RELEASE_BUFFERS unavailable - OpenSSL version may be too old" |
| 708 | |
| 709 | |
Jean-Paul Calderone | 5ebb44a | 2010-07-30 18:09:41 -0400 | [diff] [blame] | 710 | def test_set_timeout_wrong_args(self): |
| 711 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 712 | :py:obj:`Context.set_timeout` raises :py:obj:`TypeError` if called with the wrong |
| 713 | number of arguments or a non-:py:obj:`int` argument. |
Jean-Paul Calderone | 5ebb44a | 2010-07-30 18:09:41 -0400 | [diff] [blame] | 714 | """ |
| 715 | context = Context(TLSv1_METHOD) |
| 716 | self.assertRaises(TypeError, context.set_timeout) |
| 717 | self.assertRaises(TypeError, context.set_timeout, None) |
| 718 | self.assertRaises(TypeError, context.set_timeout, 1, None) |
| 719 | |
| 720 | |
| 721 | def test_get_timeout_wrong_args(self): |
| 722 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 723 | :py:obj:`Context.get_timeout` raises :py:obj:`TypeError` if called with any arguments. |
Jean-Paul Calderone | 5ebb44a | 2010-07-30 18:09:41 -0400 | [diff] [blame] | 724 | """ |
| 725 | context = Context(TLSv1_METHOD) |
| 726 | self.assertRaises(TypeError, context.get_timeout, None) |
| 727 | |
| 728 | |
| 729 | def test_timeout(self): |
| 730 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 731 | :py:obj:`Context.set_timeout` sets the session timeout for all connections |
| 732 | created using the context object. :py:obj:`Context.get_timeout` retrieves this |
Jean-Paul Calderone | 5ebb44a | 2010-07-30 18:09:41 -0400 | [diff] [blame] | 733 | value. |
| 734 | """ |
| 735 | context = Context(TLSv1_METHOD) |
| 736 | context.set_timeout(1234) |
| 737 | self.assertEquals(context.get_timeout(), 1234) |
| 738 | |
| 739 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 740 | if not PY3: |
| 741 | def test_timeout_long(self): |
| 742 | """ |
| 743 | On Python 2 :py:obj:`Context.set_timeout` accepts values of type |
| 744 | `long` as well as int. |
| 745 | """ |
| 746 | context = Context(TLSv1_METHOD) |
| 747 | context.set_timeout(long(1234)) |
| 748 | self.assertEquals(context.get_timeout(), 1234) |
| 749 | |
| 750 | |
Jean-Paul Calderone | e2b6951 | 2010-07-30 18:22:06 -0400 | [diff] [blame] | 751 | def test_set_verify_depth_wrong_args(self): |
| 752 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 753 | :py:obj:`Context.set_verify_depth` raises :py:obj:`TypeError` if called with the wrong |
| 754 | number of arguments or a non-:py:obj:`int` argument. |
Jean-Paul Calderone | e2b6951 | 2010-07-30 18:22:06 -0400 | [diff] [blame] | 755 | """ |
| 756 | context = Context(TLSv1_METHOD) |
| 757 | self.assertRaises(TypeError, context.set_verify_depth) |
| 758 | self.assertRaises(TypeError, context.set_verify_depth, None) |
| 759 | self.assertRaises(TypeError, context.set_verify_depth, 1, None) |
| 760 | |
| 761 | |
| 762 | def test_get_verify_depth_wrong_args(self): |
| 763 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 764 | :py:obj:`Context.get_verify_depth` raises :py:obj:`TypeError` if called with any arguments. |
Jean-Paul Calderone | e2b6951 | 2010-07-30 18:22:06 -0400 | [diff] [blame] | 765 | """ |
| 766 | context = Context(TLSv1_METHOD) |
| 767 | self.assertRaises(TypeError, context.get_verify_depth, None) |
| 768 | |
| 769 | |
| 770 | def test_verify_depth(self): |
| 771 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 772 | :py:obj:`Context.set_verify_depth` sets the number of certificates in a chain |
Jean-Paul Calderone | e2b6951 | 2010-07-30 18:22:06 -0400 | [diff] [blame] | 773 | to follow before giving up. The value can be retrieved with |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 774 | :py:obj:`Context.get_verify_depth`. |
Jean-Paul Calderone | e2b6951 | 2010-07-30 18:22:06 -0400 | [diff] [blame] | 775 | """ |
| 776 | context = Context(TLSv1_METHOD) |
| 777 | context.set_verify_depth(11) |
| 778 | self.assertEquals(context.get_verify_depth(), 11) |
| 779 | |
| 780 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 781 | if not PY3: |
| 782 | def test_verify_depth_long(self): |
| 783 | """ |
| 784 | On Python 2 :py:obj:`Context.set_verify_depth` accepts values of |
| 785 | type `long` as well as int. |
| 786 | """ |
| 787 | context = Context(TLSv1_METHOD) |
| 788 | context.set_verify_depth(long(11)) |
| 789 | self.assertEquals(context.get_verify_depth(), 11) |
| 790 | |
| 791 | |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 792 | def _write_encrypted_pem(self, passphrase): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 793 | """ |
| 794 | Write a new private key out to a new file, encrypted using the given |
| 795 | passphrase. Return the path to the new file. |
| 796 | """ |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 797 | key = PKey() |
| 798 | key.generate_key(TYPE_RSA, 128) |
| 799 | pemFile = self.mktemp() |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 800 | fObj = open(pemFile, 'w') |
| 801 | pem = dump_privatekey(FILETYPE_PEM, key, "blowfish", passphrase) |
| 802 | fObj.write(pem.decode('ascii')) |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 803 | fObj.close() |
| 804 | return pemFile |
| 805 | |
| 806 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 807 | def test_set_passwd_cb_wrong_args(self): |
| 808 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 809 | :py:obj:`Context.set_passwd_cb` raises :py:obj:`TypeError` if called with the |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 810 | wrong arguments or with a non-callable first argument. |
| 811 | """ |
| 812 | context = Context(TLSv1_METHOD) |
| 813 | self.assertRaises(TypeError, context.set_passwd_cb) |
| 814 | self.assertRaises(TypeError, context.set_passwd_cb, None) |
| 815 | self.assertRaises(TypeError, context.set_passwd_cb, lambda: None, None, None) |
| 816 | |
| 817 | |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 818 | def test_set_passwd_cb(self): |
| 819 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 820 | :py:obj:`Context.set_passwd_cb` accepts a callable which will be invoked when |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 821 | a private key is loaded from an encrypted PEM. |
| 822 | """ |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 823 | passphrase = b("foobar") |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 824 | pemFile = self._write_encrypted_pem(passphrase) |
Jean-Paul Calderone | 828c9cb | 2008-04-26 18:06:54 -0400 | [diff] [blame] | 825 | calledWith = [] |
| 826 | def passphraseCallback(maxlen, verify, extra): |
| 827 | calledWith.append((maxlen, verify, extra)) |
| 828 | return passphrase |
| 829 | context = Context(TLSv1_METHOD) |
| 830 | context.set_passwd_cb(passphraseCallback) |
| 831 | context.use_privatekey_file(pemFile) |
| 832 | self.assertTrue(len(calledWith), 1) |
| 833 | self.assertTrue(isinstance(calledWith[0][0], int)) |
| 834 | self.assertTrue(isinstance(calledWith[0][1], int)) |
| 835 | self.assertEqual(calledWith[0][2], None) |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 836 | |
| 837 | |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 838 | def test_passwd_callback_exception(self): |
| 839 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 840 | :py:obj:`Context.use_privatekey_file` propagates any exception raised by the |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 841 | passphrase callback. |
| 842 | """ |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 843 | pemFile = self._write_encrypted_pem(b("monkeys are nice")) |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 844 | def passphraseCallback(maxlen, verify, extra): |
| 845 | raise RuntimeError("Sorry, I am a fail.") |
| 846 | |
| 847 | context = Context(TLSv1_METHOD) |
| 848 | context.set_passwd_cb(passphraseCallback) |
| 849 | self.assertRaises(RuntimeError, context.use_privatekey_file, pemFile) |
| 850 | |
| 851 | |
| 852 | def test_passwd_callback_false(self): |
| 853 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 854 | :py:obj:`Context.use_privatekey_file` raises :py:obj:`OpenSSL.SSL.Error` if the |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 855 | passphrase callback returns a false value. |
| 856 | """ |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 857 | pemFile = self._write_encrypted_pem(b("monkeys are nice")) |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 858 | def passphraseCallback(maxlen, verify, extra): |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 859 | return b"" |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 860 | |
| 861 | context = Context(TLSv1_METHOD) |
| 862 | context.set_passwd_cb(passphraseCallback) |
| 863 | self.assertRaises(Error, context.use_privatekey_file, pemFile) |
| 864 | |
| 865 | |
| 866 | def test_passwd_callback_non_string(self): |
| 867 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 868 | :py:obj:`Context.use_privatekey_file` raises :py:obj:`OpenSSL.SSL.Error` if the |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 869 | passphrase callback returns a true non-string value. |
| 870 | """ |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 871 | pemFile = self._write_encrypted_pem(b("monkeys are nice")) |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 872 | def passphraseCallback(maxlen, verify, extra): |
| 873 | return 10 |
| 874 | |
| 875 | context = Context(TLSv1_METHOD) |
| 876 | context.set_passwd_cb(passphraseCallback) |
Jean-Paul Calderone | 8a1bea5 | 2013-03-05 07:57:57 -0800 | [diff] [blame] | 877 | self.assertRaises(ValueError, context.use_privatekey_file, pemFile) |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 878 | |
| 879 | |
| 880 | def test_passwd_callback_too_long(self): |
| 881 | """ |
| 882 | If the passphrase returned by the passphrase callback returns a string |
| 883 | longer than the indicated maximum length, it is truncated. |
| 884 | """ |
| 885 | # A priori knowledge! |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 886 | passphrase = b("x") * 1024 |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 887 | pemFile = self._write_encrypted_pem(passphrase) |
| 888 | def passphraseCallback(maxlen, verify, extra): |
| 889 | assert maxlen == 1024 |
Jean-Paul Calderone | b1f7f5f | 2010-08-22 21:40:52 -0400 | [diff] [blame] | 890 | return passphrase + b("y") |
Jean-Paul Calderone | 389d76d | 2010-07-30 17:57:53 -0400 | [diff] [blame] | 891 | |
| 892 | context = Context(TLSv1_METHOD) |
| 893 | context.set_passwd_cb(passphraseCallback) |
| 894 | # This shall succeed because the truncated result is the correct |
| 895 | # passphrase. |
| 896 | context.use_privatekey_file(pemFile) |
| 897 | |
| 898 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 899 | def test_set_info_callback(self): |
| 900 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 901 | :py:obj:`Context.set_info_callback` accepts a callable which will be invoked |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 902 | when certain information about an SSL connection is available. |
| 903 | """ |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 904 | (server, client) = socket_pair() |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 905 | |
| 906 | clientSSL = Connection(Context(TLSv1_METHOD), client) |
| 907 | clientSSL.set_connect_state() |
| 908 | |
| 909 | called = [] |
| 910 | def info(conn, where, ret): |
| 911 | called.append((conn, where, ret)) |
| 912 | context = Context(TLSv1_METHOD) |
| 913 | context.set_info_callback(info) |
| 914 | context.use_certificate( |
| 915 | load_certificate(FILETYPE_PEM, cleartextCertificatePEM)) |
| 916 | context.use_privatekey( |
| 917 | load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)) |
| 918 | |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 919 | serverSSL = Connection(context, server) |
| 920 | serverSSL.set_accept_state() |
| 921 | |
Jean-Paul Calderone | f2bbc9c | 2014-02-02 10:59:14 -0500 | [diff] [blame] | 922 | handshake(clientSSL, serverSSL) |
Jean-Paul Calderone | 5ef8651 | 2008-04-26 19:06:28 -0400 | [diff] [blame] | 923 | |
Jean-Paul Calderone | 3835e52 | 2014-02-02 11:12:30 -0500 | [diff] [blame] | 924 | # The callback must always be called with a Connection instance as the |
| 925 | # first argument. It would probably be better to split this into |
| 926 | # separate tests for client and server side info callbacks so we could |
| 927 | # assert it is called with the right Connection instance. It would |
| 928 | # also be good to assert *something* about `where` and `ret`. |
Jean-Paul Calderone | f2bbc9c | 2014-02-02 10:59:14 -0500 | [diff] [blame] | 929 | notConnections = [ |
| 930 | conn for (conn, where, ret) in called |
| 931 | if not isinstance(conn, Connection)] |
| 932 | self.assertEqual( |
| 933 | [], notConnections, |
| 934 | "Some info callback arguments were not Connection instaces.") |
Jean-Paul Calderone | e1bd432 | 2008-09-07 20:17:17 -0400 | [diff] [blame] | 935 | |
| 936 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 937 | def _load_verify_locations_test(self, *args): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 938 | """ |
| 939 | Create a client context which will verify the peer certificate and call |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 940 | its :py:obj:`load_verify_locations` method with the given arguments. |
| 941 | Then connect it to a server and ensure that the handshake succeeds. |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 942 | """ |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 943 | (server, client) = socket_pair() |
Jean-Paul Calderone | e1bd432 | 2008-09-07 20:17:17 -0400 | [diff] [blame] | 944 | |
Jean-Paul Calderone | e1bd432 | 2008-09-07 20:17:17 -0400 | [diff] [blame] | 945 | clientContext = Context(TLSv1_METHOD) |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 946 | clientContext.load_verify_locations(*args) |
Jean-Paul Calderone | e1bd432 | 2008-09-07 20:17:17 -0400 | [diff] [blame] | 947 | # Require that the server certificate verify properly or the |
| 948 | # connection will fail. |
| 949 | clientContext.set_verify( |
| 950 | VERIFY_PEER, |
| 951 | lambda conn, cert, errno, depth, preverify_ok: preverify_ok) |
| 952 | |
| 953 | clientSSL = Connection(clientContext, client) |
| 954 | clientSSL.set_connect_state() |
| 955 | |
Jean-Paul Calderone | e1bd432 | 2008-09-07 20:17:17 -0400 | [diff] [blame] | 956 | serverContext = Context(TLSv1_METHOD) |
| 957 | serverContext.use_certificate( |
| 958 | load_certificate(FILETYPE_PEM, cleartextCertificatePEM)) |
| 959 | serverContext.use_privatekey( |
| 960 | load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)) |
| 961 | |
| 962 | serverSSL = Connection(serverContext, server) |
| 963 | serverSSL.set_accept_state() |
| 964 | |
Jean-Paul Calderone | f874203 | 2010-09-25 00:00:32 -0400 | [diff] [blame] | 965 | # Without load_verify_locations above, the handshake |
| 966 | # will fail: |
| 967 | # Error: [('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', |
| 968 | # 'certificate verify failed')] |
| 969 | handshake(clientSSL, serverSSL) |
Jean-Paul Calderone | e1bd432 | 2008-09-07 20:17:17 -0400 | [diff] [blame] | 970 | |
| 971 | cert = clientSSL.get_peer_certificate() |
Jean-Paul Calderone | 20131f5 | 2009-04-01 12:05:45 -0400 | [diff] [blame] | 972 | self.assertEqual(cert.get_subject().CN, 'Testing Root CA') |
Jean-Paul Calderone | 5075fce | 2008-09-07 20:18:55 -0400 | [diff] [blame] | 973 | |
Jean-Paul Calderone | 12608a8 | 2009-11-07 10:35:15 -0500 | [diff] [blame] | 974 | |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 975 | def _load_verify_cafile(self, cafile): |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 976 | """ |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 977 | Verify that if path to a file containing a certificate is passed to |
| 978 | ``Context.load_verify_locations`` for the ``cafile`` parameter, that |
| 979 | certificate is used as a trust root for the purposes of verifying |
| 980 | connections created using that ``Context``. |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 981 | """ |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 982 | fObj = open(cafile, 'w') |
Jean-Paul Calderone | b1f7f5f | 2010-08-22 21:40:52 -0400 | [diff] [blame] | 983 | fObj.write(cleartextCertificatePEM.decode('ascii')) |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 984 | fObj.close() |
| 985 | |
| 986 | self._load_verify_locations_test(cafile) |
| 987 | |
Jean-Paul Calderone | 5075fce | 2008-09-07 20:18:55 -0400 | [diff] [blame] | 988 | |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 989 | def test_load_verify_bytes_cafile(self): |
| 990 | """ |
| 991 | :py:obj:`Context.load_verify_locations` accepts a file name as a |
| 992 | ``bytes`` instance and uses the certificates within for verification |
| 993 | purposes. |
| 994 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 995 | cafile = self.mktemp() + NON_ASCII.encode(getfilesystemencoding()) |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 996 | self._load_verify_cafile(cafile) |
| 997 | |
| 998 | |
| 999 | def test_load_verify_unicode_cafile(self): |
| 1000 | """ |
| 1001 | :py:obj:`Context.load_verify_locations` accepts a file name as a |
| 1002 | ``unicode`` instance and uses the certificates within for verification |
| 1003 | purposes. |
| 1004 | """ |
Jean-Paul Calderone | 4f70c80 | 2015-04-12 11:26:47 -0400 | [diff] [blame] | 1005 | self._load_verify_cafile( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1006 | self.mktemp().decode(getfilesystemencoding()) + NON_ASCII |
Jean-Paul Calderone | 4f70c80 | 2015-04-12 11:26:47 -0400 | [diff] [blame] | 1007 | ) |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 1008 | |
| 1009 | |
Jean-Paul Calderone | 5075fce | 2008-09-07 20:18:55 -0400 | [diff] [blame] | 1010 | def test_load_verify_invalid_file(self): |
| 1011 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1012 | :py:obj:`Context.load_verify_locations` raises :py:obj:`Error` when passed a |
Jean-Paul Calderone | 5075fce | 2008-09-07 20:18:55 -0400 | [diff] [blame] | 1013 | non-existent cafile. |
| 1014 | """ |
| 1015 | clientContext = Context(TLSv1_METHOD) |
| 1016 | self.assertRaises( |
| 1017 | Error, clientContext.load_verify_locations, self.mktemp()) |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1018 | |
| 1019 | |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 1020 | def _load_verify_directory_locations_capath(self, capath): |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1021 | """ |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 1022 | Verify that if path to a directory containing certificate files is |
| 1023 | passed to ``Context.load_verify_locations`` for the ``capath`` |
| 1024 | parameter, those certificates are used as trust roots for the purposes |
| 1025 | of verifying connections created using that ``Context``. |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1026 | """ |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1027 | makedirs(capath) |
Jean-Paul Calderone | 24dfb33 | 2011-05-04 18:10:26 -0400 | [diff] [blame] | 1028 | # Hash values computed manually with c_rehash to avoid depending on |
| 1029 | # c_rehash in the test suite. One is from OpenSSL 0.9.8, the other |
| 1030 | # from OpenSSL 1.0.0. |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1031 | for name in [b'c7adac82.0', b'c3705638.0']: |
Jean-Paul Calderone | 0582673 | 2015-04-12 11:38:49 -0400 | [diff] [blame] | 1032 | cafile = join_bytes_or_unicode(capath, name) |
| 1033 | with open(cafile, 'w') as fObj: |
| 1034 | fObj.write(cleartextCertificatePEM.decode('ascii')) |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1035 | |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1036 | self._load_verify_locations_test(None, capath) |
| 1037 | |
| 1038 | |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 1039 | def test_load_verify_directory_bytes_capath(self): |
| 1040 | """ |
| 1041 | :py:obj:`Context.load_verify_locations` accepts a directory name as a |
| 1042 | ``bytes`` instance and uses the certificates within for verification |
| 1043 | purposes. |
| 1044 | """ |
| 1045 | self._load_verify_directory_locations_capath( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1046 | self.mktemp() + NON_ASCII.encode(getfilesystemencoding()) |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 1047 | ) |
| 1048 | |
| 1049 | |
| 1050 | def test_load_verify_directory_unicode_capath(self): |
| 1051 | """ |
| 1052 | :py:obj:`Context.load_verify_locations` accepts a directory name as a |
| 1053 | ``unicode`` instance and uses the certificates within for verification |
| 1054 | purposes. |
| 1055 | """ |
Jean-Paul Calderone | 4f70c80 | 2015-04-12 11:26:47 -0400 | [diff] [blame] | 1056 | self._load_verify_directory_locations_capath( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1057 | self.mktemp().decode(getfilesystemencoding()) + NON_ASCII |
Jean-Paul Calderone | 4f70c80 | 2015-04-12 11:26:47 -0400 | [diff] [blame] | 1058 | ) |
Jean-Paul Calderone | 210c0f3 | 2015-04-12 09:20:31 -0400 | [diff] [blame] | 1059 | |
| 1060 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1061 | def test_load_verify_locations_wrong_args(self): |
| 1062 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1063 | :py:obj:`Context.load_verify_locations` raises :py:obj:`TypeError` if called with |
| 1064 | the wrong number of arguments or with non-:py:obj:`str` arguments. |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1065 | """ |
| 1066 | context = Context(TLSv1_METHOD) |
| 1067 | self.assertRaises(TypeError, context.load_verify_locations) |
| 1068 | self.assertRaises(TypeError, context.load_verify_locations, object()) |
| 1069 | self.assertRaises(TypeError, context.load_verify_locations, object(), object()) |
| 1070 | self.assertRaises(TypeError, context.load_verify_locations, None, None, None) |
| 1071 | |
| 1072 | |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 1073 | if platform == "win32": |
| 1074 | "set_default_verify_paths appears not to work on Windows. " |
Jean-Paul Calderone | 28fb8f0 | 2009-07-24 18:01:31 -0400 | [diff] [blame] | 1075 | "See LP#404343 and LP#404344." |
| 1076 | else: |
| 1077 | def test_set_default_verify_paths(self): |
| 1078 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1079 | :py:obj:`Context.set_default_verify_paths` causes the platform-specific CA |
Jean-Paul Calderone | 28fb8f0 | 2009-07-24 18:01:31 -0400 | [diff] [blame] | 1080 | certificate locations to be used for verification purposes. |
| 1081 | """ |
| 1082 | # Testing this requires a server with a certificate signed by one of |
| 1083 | # the CAs in the platform CA location. Getting one of those costs |
| 1084 | # money. Fortunately (or unfortunately, depending on your |
| 1085 | # perspective), it's easy to think of a public server on the |
| 1086 | # internet which has such a certificate. Connecting to the network |
| 1087 | # in a unit test is bad, but it's the only way I can think of to |
| 1088 | # really test this. -exarkun |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1089 | |
Alex Gaynor | b586da3 | 2014-11-15 09:22:21 -0800 | [diff] [blame] | 1090 | # Arg, verisign.com doesn't speak anything newer than TLS 1.0 |
| 1091 | context = Context(TLSv1_METHOD) |
Jean-Paul Calderone | 28fb8f0 | 2009-07-24 18:01:31 -0400 | [diff] [blame] | 1092 | context.set_default_verify_paths() |
| 1093 | context.set_verify( |
Ziga Seilnacht | 44611bf | 2009-08-31 20:49:30 +0200 | [diff] [blame] | 1094 | VERIFY_PEER, |
Jean-Paul Calderone | 28fb8f0 | 2009-07-24 18:01:31 -0400 | [diff] [blame] | 1095 | lambda conn, cert, errno, depth, preverify_ok: preverify_ok) |
Jean-Paul Calderone | 1cb5d02 | 2008-09-07 20:58:50 -0400 | [diff] [blame] | 1096 | |
Jean-Paul Calderone | 28fb8f0 | 2009-07-24 18:01:31 -0400 | [diff] [blame] | 1097 | client = socket() |
| 1098 | client.connect(('verisign.com', 443)) |
| 1099 | clientSSL = Connection(context, client) |
| 1100 | clientSSL.set_connect_state() |
| 1101 | clientSSL.do_handshake() |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1102 | clientSSL.send(b"GET / HTTP/1.0\r\n\r\n") |
Jean-Paul Calderone | 28fb8f0 | 2009-07-24 18:01:31 -0400 | [diff] [blame] | 1103 | self.assertTrue(clientSSL.recv(1024)) |
Jean-Paul Calderone | 9eadb96 | 2008-09-07 21:20:44 -0400 | [diff] [blame] | 1104 | |
| 1105 | |
| 1106 | def test_set_default_verify_paths_signature(self): |
| 1107 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1108 | :py:obj:`Context.set_default_verify_paths` takes no arguments and raises |
| 1109 | :py:obj:`TypeError` if given any. |
Jean-Paul Calderone | 9eadb96 | 2008-09-07 21:20:44 -0400 | [diff] [blame] | 1110 | """ |
| 1111 | context = Context(TLSv1_METHOD) |
| 1112 | self.assertRaises(TypeError, context.set_default_verify_paths, None) |
| 1113 | self.assertRaises(TypeError, context.set_default_verify_paths, 1) |
| 1114 | self.assertRaises(TypeError, context.set_default_verify_paths, "") |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 1115 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1116 | |
Jean-Paul Calderone | 12608a8 | 2009-11-07 10:35:15 -0500 | [diff] [blame] | 1117 | def test_add_extra_chain_cert_invalid_cert(self): |
| 1118 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1119 | :py:obj:`Context.add_extra_chain_cert` raises :py:obj:`TypeError` if called with |
Jean-Paul Calderone | 12608a8 | 2009-11-07 10:35:15 -0500 | [diff] [blame] | 1120 | other than one argument or if called with an object which is not an |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1121 | instance of :py:obj:`X509`. |
Jean-Paul Calderone | 12608a8 | 2009-11-07 10:35:15 -0500 | [diff] [blame] | 1122 | """ |
| 1123 | context = Context(TLSv1_METHOD) |
| 1124 | self.assertRaises(TypeError, context.add_extra_chain_cert) |
| 1125 | self.assertRaises(TypeError, context.add_extra_chain_cert, object()) |
| 1126 | self.assertRaises(TypeError, context.add_extra_chain_cert, object(), object()) |
| 1127 | |
| 1128 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1129 | def _handshake_test(self, serverContext, clientContext): |
| 1130 | """ |
| 1131 | Verify that a client and server created with the given contexts can |
| 1132 | successfully handshake and communicate. |
| 1133 | """ |
| 1134 | serverSocket, clientSocket = socket_pair() |
| 1135 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 1136 | server = Connection(serverContext, serverSocket) |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 1137 | server.set_accept_state() |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 1138 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1139 | client = Connection(clientContext, clientSocket) |
| 1140 | client.set_connect_state() |
| 1141 | |
| 1142 | # Make them talk to each other. |
| 1143 | # self._interactInMemory(client, server) |
| 1144 | for i in range(3): |
| 1145 | for s in [client, server]: |
| 1146 | try: |
| 1147 | s.do_handshake() |
| 1148 | except WantReadError: |
| 1149 | pass |
| 1150 | |
| 1151 | |
Jean-Paul Calderone | 6a8cd11 | 2014-04-02 21:09:08 -0400 | [diff] [blame] | 1152 | def test_set_verify_callback_connection_argument(self): |
| 1153 | """ |
| 1154 | The first argument passed to the verify callback is the |
| 1155 | :py:class:`Connection` instance for which verification is taking place. |
| 1156 | """ |
| 1157 | serverContext = Context(TLSv1_METHOD) |
| 1158 | serverContext.use_privatekey( |
| 1159 | load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)) |
| 1160 | serverContext.use_certificate( |
| 1161 | load_certificate(FILETYPE_PEM, cleartextCertificatePEM)) |
| 1162 | serverConnection = Connection(serverContext, None) |
| 1163 | |
| 1164 | class VerifyCallback(object): |
| 1165 | def callback(self, connection, *args): |
| 1166 | self.connection = connection |
| 1167 | return 1 |
| 1168 | |
| 1169 | verify = VerifyCallback() |
| 1170 | clientContext = Context(TLSv1_METHOD) |
| 1171 | clientContext.set_verify(VERIFY_PEER, verify.callback) |
| 1172 | clientConnection = Connection(clientContext, None) |
| 1173 | clientConnection.set_connect_state() |
| 1174 | |
| 1175 | self._handshakeInMemory(clientConnection, serverConnection) |
| 1176 | |
| 1177 | self.assertIdentical(verify.connection, clientConnection) |
| 1178 | |
| 1179 | |
Jean-Paul Calderone | 7e166fe | 2013-03-06 20:54:38 -0800 | [diff] [blame] | 1180 | def test_set_verify_callback_exception(self): |
| 1181 | """ |
| 1182 | If the verify callback passed to :py:obj:`Context.set_verify` raises an |
| 1183 | exception, verification fails and the exception is propagated to the |
| 1184 | caller of :py:obj:`Connection.do_handshake`. |
| 1185 | """ |
| 1186 | serverContext = Context(TLSv1_METHOD) |
| 1187 | serverContext.use_privatekey( |
| 1188 | load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM)) |
| 1189 | serverContext.use_certificate( |
| 1190 | load_certificate(FILETYPE_PEM, cleartextCertificatePEM)) |
| 1191 | |
| 1192 | clientContext = Context(TLSv1_METHOD) |
| 1193 | def verify_callback(*args): |
| 1194 | raise Exception("silly verify failure") |
| 1195 | clientContext.set_verify(VERIFY_PEER, verify_callback) |
| 1196 | |
| 1197 | exc = self.assertRaises( |
| 1198 | Exception, self._handshake_test, serverContext, clientContext) |
| 1199 | self.assertEqual("silly verify failure", str(exc)) |
| 1200 | |
| 1201 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1202 | def test_add_extra_chain_cert(self): |
| 1203 | """ |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1204 | :py:obj:`Context.add_extra_chain_cert` accepts an :py:obj:`X509` |
| 1205 | instance to add to the certificate chain. |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1206 | |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1207 | See :py:obj:`_create_certificate_chain` for the details of the |
| 1208 | certificate chain tested. |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1209 | |
| 1210 | The chain is tested by starting a server with scert and connecting |
| 1211 | to it with a client which trusts cacert and requires verification to |
| 1212 | succeed. |
| 1213 | """ |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 1214 | chain = _create_certificate_chain() |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1215 | [(cakey, cacert), (ikey, icert), (skey, scert)] = chain |
| 1216 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 1217 | # Dump the CA certificate to a file because that's the only way to load |
| 1218 | # it as a trusted CA in the client context. |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1219 | for cert, name in [(cacert, 'ca.pem'), |
| 1220 | (icert, 'i.pem'), |
| 1221 | (scert, 's.pem')]: |
Hynek Schlawack | e90680f | 2015-04-16 15:09:27 -0400 | [diff] [blame] | 1222 | with open(join(self.tmpdir, name), 'w') as f: |
| 1223 | f.write(dump_certificate(FILETYPE_PEM, cert).decode('ascii')) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 1224 | |
Hynek Schlawack | 1902c01 | 2015-04-16 15:06:41 -0400 | [diff] [blame] | 1225 | for key, name in [(cakey, 'ca.key'), |
| 1226 | (ikey, 'i.key'), |
| 1227 | (skey, 's.key')]: |
Hynek Schlawack | e90680f | 2015-04-16 15:09:27 -0400 | [diff] [blame] | 1228 | with open(join(self.tmpdir, name), 'w') as f: |
| 1229 | f.write(dump_privatekey(FILETYPE_PEM, key).decode('ascii')) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 1230 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1231 | # Create the server context |
| 1232 | serverContext = Context(TLSv1_METHOD) |
| 1233 | serverContext.use_privatekey(skey) |
| 1234 | serverContext.use_certificate(scert) |
Jean-Paul Calderone | 16cf03d | 2010-09-08 18:53:39 -0400 | [diff] [blame] | 1235 | # The client already has cacert, we only need to give them icert. |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1236 | serverContext.add_extra_chain_cert(icert) |
| 1237 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 1238 | # Create the client |
| 1239 | clientContext = Context(TLSv1_METHOD) |
| 1240 | clientContext.set_verify( |
| 1241 | VERIFY_PEER | VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb) |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1242 | clientContext.load_verify_locations(join(self.tmpdir, "ca.pem")) |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 1243 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1244 | # Try it out. |
| 1245 | self._handshake_test(serverContext, clientContext) |
| 1246 | |
| 1247 | |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1248 | def _use_certificate_chain_file_test(self, certdir): |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1249 | """ |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1250 | Verify that :py:obj:`Context.use_certificate_chain_file` reads a |
| 1251 | certificate chain from a specified file. |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1252 | |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1253 | The chain is tested by starting a server with scert and connecting to |
| 1254 | it with a client which trusts cacert and requires verification to |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1255 | succeed. |
| 1256 | """ |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 1257 | chain = _create_certificate_chain() |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1258 | [(cakey, cacert), (ikey, icert), (skey, scert)] = chain |
| 1259 | |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1260 | makedirs(certdir) |
| 1261 | |
Jean-Paul Calderone | 0582673 | 2015-04-12 11:38:49 -0400 | [diff] [blame] | 1262 | chainFile = join_bytes_or_unicode(certdir, "chain.pem") |
| 1263 | caFile = join_bytes_or_unicode(certdir, "ca.pem") |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1264 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1265 | # Write out the chain file. |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1266 | with open(chainFile, 'wb') as fObj: |
| 1267 | # Most specific to least general. |
| 1268 | fObj.write(dump_certificate(FILETYPE_PEM, scert)) |
| 1269 | fObj.write(dump_certificate(FILETYPE_PEM, icert)) |
| 1270 | fObj.write(dump_certificate(FILETYPE_PEM, cacert)) |
| 1271 | |
| 1272 | with open(caFile, 'w') as fObj: |
| 1273 | fObj.write(dump_certificate(FILETYPE_PEM, cacert).decode('ascii')) |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1274 | |
| 1275 | serverContext = Context(TLSv1_METHOD) |
| 1276 | serverContext.use_certificate_chain_file(chainFile) |
| 1277 | serverContext.use_privatekey(skey) |
| 1278 | |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1279 | clientContext = Context(TLSv1_METHOD) |
| 1280 | clientContext.set_verify( |
| 1281 | VERIFY_PEER | VERIFY_FAIL_IF_NO_PEER_CERT, verify_cb) |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1282 | clientContext.load_verify_locations(caFile) |
Jean-Paul Calderone | f448062 | 2010-08-02 18:25:03 -0400 | [diff] [blame] | 1283 | |
| 1284 | self._handshake_test(serverContext, clientContext) |
| 1285 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1286 | |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1287 | def test_use_certificate_chain_file_bytes(self): |
| 1288 | """ |
| 1289 | ``Context.use_certificate_chain_file`` accepts the name of a file (as |
| 1290 | an instance of ``bytes``) to specify additional certificates to use to |
| 1291 | construct and verify a trust chain. |
| 1292 | """ |
| 1293 | self._use_certificate_chain_file_test( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1294 | self.mktemp() + NON_ASCII.encode(getfilesystemencoding()) |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1295 | ) |
| 1296 | |
| 1297 | |
| 1298 | def test_use_certificate_chain_file_unicode(self): |
| 1299 | """ |
| 1300 | ``Context.use_certificate_chain_file`` accepts the name of a file (as |
| 1301 | an instance of ``unicode``) to specify additional certificates to use |
| 1302 | to construct and verify a trust chain. |
| 1303 | """ |
| 1304 | self._use_certificate_chain_file_test( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1305 | self.mktemp().decode(getfilesystemencoding()) + NON_ASCII |
Jean-Paul Calderone | aac43a3 | 2015-04-12 09:51:21 -0400 | [diff] [blame] | 1306 | ) |
| 1307 | |
| 1308 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1309 | def test_use_certificate_chain_file_wrong_args(self): |
| 1310 | """ |
| 1311 | :py:obj:`Context.use_certificate_chain_file` raises :py:obj:`TypeError` |
| 1312 | if passed zero or more than one argument or when passed a non-byte |
| 1313 | string single argument. It also raises :py:obj:`OpenSSL.SSL.Error` when |
| 1314 | passed a bad chain file name (for example, the name of a file which does |
| 1315 | not exist). |
| 1316 | """ |
| 1317 | context = Context(TLSv1_METHOD) |
| 1318 | self.assertRaises(TypeError, context.use_certificate_chain_file) |
| 1319 | self.assertRaises(TypeError, context.use_certificate_chain_file, object()) |
| 1320 | self.assertRaises(TypeError, context.use_certificate_chain_file, b"foo", object()) |
| 1321 | |
| 1322 | self.assertRaises(Error, context.use_certificate_chain_file, self.mktemp()) |
| 1323 | |
Jean-Paul Calderone | e0d7936 | 2010-08-03 18:46:46 -0400 | [diff] [blame] | 1324 | # XXX load_client_ca |
| 1325 | # XXX set_session_id |
Jean-Paul Calderone | 0294e3d | 2010-09-09 18:17:48 -0400 | [diff] [blame] | 1326 | |
| 1327 | def test_get_verify_mode_wrong_args(self): |
| 1328 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1329 | :py:obj:`Context.get_verify_mode` raises :py:obj:`TypeError` if called with any |
Jean-Paul Calderone | 0294e3d | 2010-09-09 18:17:48 -0400 | [diff] [blame] | 1330 | arguments. |
| 1331 | """ |
| 1332 | context = Context(TLSv1_METHOD) |
| 1333 | self.assertRaises(TypeError, context.get_verify_mode, None) |
| 1334 | |
| 1335 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1336 | def test_set_verify_mode(self): |
Jean-Paul Calderone | 0294e3d | 2010-09-09 18:17:48 -0400 | [diff] [blame] | 1337 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1338 | :py:obj:`Context.get_verify_mode` returns the verify mode flags previously |
| 1339 | passed to :py:obj:`Context.set_verify`. |
Jean-Paul Calderone | 0294e3d | 2010-09-09 18:17:48 -0400 | [diff] [blame] | 1340 | """ |
| 1341 | context = Context(TLSv1_METHOD) |
| 1342 | self.assertEquals(context.get_verify_mode(), 0) |
| 1343 | context.set_verify( |
| 1344 | VERIFY_PEER | VERIFY_CLIENT_ONCE, lambda *args: None) |
| 1345 | self.assertEquals( |
| 1346 | context.get_verify_mode(), VERIFY_PEER | VERIFY_CLIENT_ONCE) |
| 1347 | |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1348 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1349 | if not PY3: |
| 1350 | def test_set_verify_mode_long(self): |
| 1351 | """ |
| 1352 | On Python 2 :py:obj:`Context.set_verify_mode` accepts values of |
| 1353 | type :py:obj:`long` as well as :py:obj:`int`. |
| 1354 | """ |
| 1355 | context = Context(TLSv1_METHOD) |
| 1356 | self.assertEquals(context.get_verify_mode(), 0) |
| 1357 | context.set_verify( |
| 1358 | long(VERIFY_PEER | VERIFY_CLIENT_ONCE), lambda *args: None) |
| 1359 | self.assertEquals( |
| 1360 | context.get_verify_mode(), VERIFY_PEER | VERIFY_CLIENT_ONCE) |
| 1361 | |
| 1362 | |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1363 | def test_load_tmp_dh_wrong_args(self): |
| 1364 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1365 | :py:obj:`Context.load_tmp_dh` raises :py:obj:`TypeError` if called with the wrong |
| 1366 | number of arguments or with a non-:py:obj:`str` argument. |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1367 | """ |
| 1368 | context = Context(TLSv1_METHOD) |
| 1369 | self.assertRaises(TypeError, context.load_tmp_dh) |
| 1370 | self.assertRaises(TypeError, context.load_tmp_dh, "foo", None) |
| 1371 | self.assertRaises(TypeError, context.load_tmp_dh, object()) |
| 1372 | |
| 1373 | |
| 1374 | def test_load_tmp_dh_missing_file(self): |
| 1375 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1376 | :py:obj:`Context.load_tmp_dh` raises :py:obj:`OpenSSL.SSL.Error` if the specified file |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1377 | does not exist. |
| 1378 | """ |
| 1379 | context = Context(TLSv1_METHOD) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1380 | self.assertRaises(Error, context.load_tmp_dh, b"hello") |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1381 | |
| 1382 | |
Jean-Paul Calderone | 9e1c1dd | 2015-04-12 10:13:13 -0400 | [diff] [blame] | 1383 | def _load_tmp_dh_test(self, dhfilename): |
Jean-Paul Calderone | 4e0c43f | 2015-04-13 10:15:17 -0400 | [diff] [blame] | 1384 | """ |
| 1385 | Verify that calling ``Context.load_tmp_dh`` with the given filename |
| 1386 | does not raise an exception. |
| 1387 | """ |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1388 | context = Context(TLSv1_METHOD) |
Jean-Paul Calderone | 9e1c1dd | 2015-04-12 10:13:13 -0400 | [diff] [blame] | 1389 | with open(dhfilename, "w") as dhfile: |
| 1390 | dhfile.write(dhparam) |
| 1391 | |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1392 | context.load_tmp_dh(dhfilename) |
| 1393 | # XXX What should I assert here? -exarkun |
| 1394 | |
| 1395 | |
Jean-Paul Calderone | 9e1c1dd | 2015-04-12 10:13:13 -0400 | [diff] [blame] | 1396 | def test_load_tmp_dh_bytes(self): |
| 1397 | """ |
| 1398 | :py:obj:`Context.load_tmp_dh` loads Diffie-Hellman parameters from the |
| 1399 | specified file (given as ``bytes``). |
| 1400 | """ |
| 1401 | self._load_tmp_dh_test( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1402 | self.mktemp() + NON_ASCII.encode(getfilesystemencoding()), |
Jean-Paul Calderone | 9e1c1dd | 2015-04-12 10:13:13 -0400 | [diff] [blame] | 1403 | ) |
| 1404 | |
| 1405 | |
| 1406 | def test_load_tmp_dh_unicode(self): |
| 1407 | """ |
| 1408 | :py:obj:`Context.load_tmp_dh` loads Diffie-Hellman parameters from the |
| 1409 | specified file (given as ``unicode``). |
| 1410 | """ |
| 1411 | self._load_tmp_dh_test( |
Hynek Schlawack | 4813c0e | 2015-04-16 13:38:01 -0400 | [diff] [blame] | 1412 | self.mktemp().decode(getfilesystemencoding()) + NON_ASCII, |
Jean-Paul Calderone | 9e1c1dd | 2015-04-12 10:13:13 -0400 | [diff] [blame] | 1413 | ) |
| 1414 | |
| 1415 | |
Jean-Paul Calderone | 3e4e335 | 2014-04-19 09:28:28 -0400 | [diff] [blame] | 1416 | def test_set_tmp_ecdh(self): |
Andy Lutomirski | f05a273 | 2014-03-13 17:22:25 -0700 | [diff] [blame] | 1417 | """ |
Jean-Paul Calderone | 3e4e335 | 2014-04-19 09:28:28 -0400 | [diff] [blame] | 1418 | :py:obj:`Context.set_tmp_ecdh` sets the elliptic curve for |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 1419 | Diffie-Hellman to the specified curve. |
Andy Lutomirski | f05a273 | 2014-03-13 17:22:25 -0700 | [diff] [blame] | 1420 | """ |
| 1421 | context = Context(TLSv1_METHOD) |
Jean-Paul Calderone | c09fd58 | 2014-04-18 22:00:10 -0400 | [diff] [blame] | 1422 | for curve in get_elliptic_curves(): |
| 1423 | # The only easily "assertable" thing is that it does not raise an |
| 1424 | # exception. |
Jean-Paul Calderone | 3e4e335 | 2014-04-19 09:28:28 -0400 | [diff] [blame] | 1425 | context.set_tmp_ecdh(curve) |
Alex Gaynor | 12dc084 | 2014-01-17 12:51:31 -0600 | [diff] [blame] | 1426 | |
| 1427 | |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 1428 | def test_set_cipher_list_bytes(self): |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1429 | """ |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 1430 | :py:obj:`Context.set_cipher_list` accepts a :py:obj:`bytes` naming the |
| 1431 | ciphers which connections created with the context object will be able |
| 1432 | to choose from. |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1433 | """ |
| 1434 | context = Context(TLSv1_METHOD) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 1435 | context.set_cipher_list(b"hello world:EXP-RC4-MD5") |
Jean-Paul Calderone | 6ace478 | 2010-09-09 18:43:40 -0400 | [diff] [blame] | 1436 | conn = Connection(context, None) |
| 1437 | self.assertEquals(conn.get_cipher_list(), ["EXP-RC4-MD5"]) |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 1438 | |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 1439 | |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 1440 | def test_set_cipher_list_text(self): |
| 1441 | """ |
| 1442 | :py:obj:`Context.set_cipher_list` accepts a :py:obj:`unicode` naming |
| 1443 | the ciphers which connections created with the context object will be |
| 1444 | able to choose from. |
| 1445 | """ |
| 1446 | context = Context(TLSv1_METHOD) |
Jean-Paul Calderone | c76c61c | 2014-01-18 13:21:52 -0500 | [diff] [blame] | 1447 | context.set_cipher_list(u("hello world:EXP-RC4-MD5")) |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 1448 | conn = Connection(context, None) |
| 1449 | self.assertEquals(conn.get_cipher_list(), ["EXP-RC4-MD5"]) |
| 1450 | |
| 1451 | |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1452 | def test_set_cipher_list_wrong_args(self): |
| 1453 | """ |
Jean-Paul Calderone | 1704483 | 2014-01-18 10:20:11 -0500 | [diff] [blame] | 1454 | :py:obj:`Context.set_cipher_list` raises :py:obj:`TypeError` when |
| 1455 | passed zero arguments or more than one argument or when passed a |
| 1456 | non-string single argument and raises :py:obj:`OpenSSL.SSL.Error` when |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1457 | passed an incorrect cipher list string. |
| 1458 | """ |
| 1459 | context = Context(TLSv1_METHOD) |
| 1460 | self.assertRaises(TypeError, context.set_cipher_list) |
| 1461 | self.assertRaises(TypeError, context.set_cipher_list, object()) |
| 1462 | self.assertRaises(TypeError, context.set_cipher_list, b"EXP-RC4-MD5", object()) |
| 1463 | |
Jean-Paul Calderone | 63eab69 | 2014-01-18 10:19:56 -0500 | [diff] [blame] | 1464 | self.assertRaises(Error, context.set_cipher_list, "imaginary-cipher") |
Jean-Paul Calderone | 131052e | 2013-03-05 11:56:19 -0800 | [diff] [blame] | 1465 | |
| 1466 | |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 1467 | def test_set_session_cache_mode_wrong_args(self): |
| 1468 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1469 | :py:obj:`Context.set_session_cache_mode` raises :py:obj:`TypeError` if |
| 1470 | called with other than one integer argument. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 1471 | """ |
| 1472 | context = Context(TLSv1_METHOD) |
| 1473 | self.assertRaises(TypeError, context.set_session_cache_mode) |
| 1474 | self.assertRaises(TypeError, context.set_session_cache_mode, object()) |
| 1475 | |
| 1476 | |
| 1477 | def test_get_session_cache_mode_wrong_args(self): |
| 1478 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1479 | :py:obj:`Context.get_session_cache_mode` raises :py:obj:`TypeError` if |
| 1480 | called with any arguments. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 1481 | """ |
| 1482 | context = Context(TLSv1_METHOD) |
| 1483 | self.assertRaises(TypeError, context.get_session_cache_mode, 1) |
| 1484 | |
| 1485 | |
| 1486 | def test_session_cache_mode(self): |
| 1487 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1488 | :py:obj:`Context.set_session_cache_mode` specifies how sessions are |
| 1489 | cached. The setting can be retrieved via |
| 1490 | :py:obj:`Context.get_session_cache_mode`. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 1491 | """ |
| 1492 | context = Context(TLSv1_METHOD) |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1493 | context.set_session_cache_mode(SESS_CACHE_OFF) |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 1494 | off = context.set_session_cache_mode(SESS_CACHE_BOTH) |
| 1495 | self.assertEqual(SESS_CACHE_OFF, off) |
| 1496 | self.assertEqual(SESS_CACHE_BOTH, context.get_session_cache_mode()) |
| 1497 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 1498 | if not PY3: |
| 1499 | def test_session_cache_mode_long(self): |
| 1500 | """ |
| 1501 | On Python 2 :py:obj:`Context.set_session_cache_mode` accepts values |
| 1502 | of type :py:obj:`long` as well as :py:obj:`int`. |
| 1503 | """ |
| 1504 | context = Context(TLSv1_METHOD) |
| 1505 | context.set_session_cache_mode(long(SESS_CACHE_BOTH)) |
| 1506 | self.assertEqual( |
| 1507 | SESS_CACHE_BOTH, context.get_session_cache_mode()) |
| 1508 | |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 1509 | |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 1510 | def test_get_cert_store(self): |
| 1511 | """ |
| 1512 | :py:obj:`Context.get_cert_store` returns a :py:obj:`X509Store` instance. |
| 1513 | """ |
| 1514 | context = Context(TLSv1_METHOD) |
| 1515 | store = context.get_cert_store() |
| 1516 | self.assertIsInstance(store, X509Store) |
| 1517 | |
| 1518 | |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 1519 | |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1520 | class ServerNameCallbackTests(TestCase, _LoopbackMixin): |
| 1521 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1522 | Tests for :py:obj:`Context.set_tlsext_servername_callback` and its interaction with |
| 1523 | :py:obj:`Connection`. |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1524 | """ |
| 1525 | def test_wrong_args(self): |
| 1526 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1527 | :py:obj:`Context.set_tlsext_servername_callback` raises :py:obj:`TypeError` if called |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1528 | with other than one argument. |
| 1529 | """ |
| 1530 | context = Context(TLSv1_METHOD) |
| 1531 | self.assertRaises(TypeError, context.set_tlsext_servername_callback) |
| 1532 | self.assertRaises( |
| 1533 | TypeError, context.set_tlsext_servername_callback, 1, 2) |
| 1534 | |
Jean-Paul Calderone | 4c1aacd | 2014-01-11 08:15:17 -0500 | [diff] [blame] | 1535 | |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1536 | def test_old_callback_forgotten(self): |
| 1537 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1538 | If :py:obj:`Context.set_tlsext_servername_callback` is used to specify a new |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1539 | callback, the one it replaces is dereferenced. |
| 1540 | """ |
| 1541 | def callback(connection): |
| 1542 | pass |
| 1543 | |
| 1544 | def replacement(connection): |
| 1545 | pass |
| 1546 | |
| 1547 | context = Context(TLSv1_METHOD) |
| 1548 | context.set_tlsext_servername_callback(callback) |
| 1549 | |
| 1550 | tracker = ref(callback) |
| 1551 | del callback |
| 1552 | |
| 1553 | context.set_tlsext_servername_callback(replacement) |
Jean-Paul Calderone | d4033eb | 2014-01-11 08:21:46 -0500 | [diff] [blame] | 1554 | |
| 1555 | # One run of the garbage collector happens to work on CPython. PyPy |
| 1556 | # doesn't collect the underlying object until a second run for whatever |
| 1557 | # reason. That's fine, it still demonstrates our code has properly |
| 1558 | # dropped the reference. |
| 1559 | collect() |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1560 | collect() |
Jean-Paul Calderone | 4c1aacd | 2014-01-11 08:15:17 -0500 | [diff] [blame] | 1561 | |
| 1562 | callback = tracker() |
| 1563 | if callback is not None: |
| 1564 | referrers = get_referrers(callback) |
Jean-Paul Calderone | 7de3956 | 2014-01-11 08:17:59 -0500 | [diff] [blame] | 1565 | if len(referrers) > 1: |
Jean-Paul Calderone | 4c1aacd | 2014-01-11 08:15:17 -0500 | [diff] [blame] | 1566 | self.fail("Some references remain: %r" % (referrers,)) |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1567 | |
| 1568 | |
| 1569 | def test_no_servername(self): |
| 1570 | """ |
| 1571 | When a client specifies no server name, the callback passed to |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1572 | :py:obj:`Context.set_tlsext_servername_callback` is invoked and the result of |
| 1573 | :py:obj:`Connection.get_servername` is :py:obj:`None`. |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1574 | """ |
| 1575 | args = [] |
| 1576 | def servername(conn): |
| 1577 | args.append((conn, conn.get_servername())) |
| 1578 | context = Context(TLSv1_METHOD) |
| 1579 | context.set_tlsext_servername_callback(servername) |
| 1580 | |
| 1581 | # Lose our reference to it. The Context is responsible for keeping it |
| 1582 | # alive now. |
| 1583 | del servername |
| 1584 | collect() |
| 1585 | |
| 1586 | # Necessary to actually accept the connection |
| 1587 | context.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1588 | context.use_certificate(load_certificate(FILETYPE_PEM, server_cert_pem)) |
| 1589 | |
| 1590 | # Do a little connection to trigger the logic |
| 1591 | server = Connection(context, None) |
| 1592 | server.set_accept_state() |
| 1593 | |
| 1594 | client = Connection(Context(TLSv1_METHOD), None) |
| 1595 | client.set_connect_state() |
| 1596 | |
| 1597 | self._interactInMemory(server, client) |
| 1598 | |
| 1599 | self.assertEqual([(server, None)], args) |
| 1600 | |
| 1601 | |
| 1602 | def test_servername(self): |
| 1603 | """ |
| 1604 | When a client specifies a server name in its hello message, the callback |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 1605 | passed to :py:obj:`Contexts.set_tlsext_servername_callback` is invoked and the |
| 1606 | result of :py:obj:`Connection.get_servername` is that server name. |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1607 | """ |
| 1608 | args = [] |
| 1609 | def servername(conn): |
| 1610 | args.append((conn, conn.get_servername())) |
| 1611 | context = Context(TLSv1_METHOD) |
| 1612 | context.set_tlsext_servername_callback(servername) |
| 1613 | |
| 1614 | # Necessary to actually accept the connection |
| 1615 | context.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1616 | context.use_certificate(load_certificate(FILETYPE_PEM, server_cert_pem)) |
| 1617 | |
| 1618 | # Do a little connection to trigger the logic |
| 1619 | server = Connection(context, None) |
| 1620 | server.set_accept_state() |
| 1621 | |
| 1622 | client = Connection(Context(TLSv1_METHOD), None) |
| 1623 | client.set_connect_state() |
| 1624 | client.set_tlsext_host_name(b("foo1.example.com")) |
| 1625 | |
| 1626 | self._interactInMemory(server, client) |
| 1627 | |
| 1628 | self.assertEqual([(server, b("foo1.example.com"))], args) |
| 1629 | |
| 1630 | |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1631 | class NextProtoNegotiationTests(TestCase, _LoopbackMixin): |
| 1632 | """ |
| 1633 | Test for Next Protocol Negotiation in PyOpenSSL. |
| 1634 | """ |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1635 | if _lib.Cryptography_HAS_NEXTPROTONEG: |
| 1636 | def test_npn_success(self): |
| 1637 | """ |
| 1638 | Tests that clients and servers that agree on the negotiated next |
| 1639 | protocol can correct establish a connection, and that the agreed |
| 1640 | protocol is reported by the connections. |
| 1641 | """ |
| 1642 | advertise_args = [] |
| 1643 | select_args = [] |
| 1644 | def advertise(conn): |
| 1645 | advertise_args.append((conn,)) |
| 1646 | return [b'http/1.1', b'spdy/2'] |
| 1647 | def select(conn, options): |
| 1648 | select_args.append((conn, options)) |
| 1649 | return b'spdy/2' |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1650 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1651 | server_context = Context(TLSv1_METHOD) |
| 1652 | server_context.set_npn_advertise_callback(advertise) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1653 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1654 | client_context = Context(TLSv1_METHOD) |
| 1655 | client_context.set_npn_select_callback(select) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1656 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1657 | # Necessary to actually accept the connection |
| 1658 | server_context.use_privatekey( |
| 1659 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1660 | server_context.use_certificate( |
| 1661 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1662 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1663 | # Do a little connection to trigger the logic |
| 1664 | server = Connection(server_context, None) |
| 1665 | server.set_accept_state() |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1666 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1667 | client = Connection(client_context, None) |
| 1668 | client.set_connect_state() |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1669 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1670 | self._interactInMemory(server, client) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1671 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1672 | self.assertEqual([(server,)], advertise_args) |
| 1673 | self.assertEqual([(client, [b'http/1.1', b'spdy/2'])], select_args) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1674 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1675 | self.assertEqual(server.get_next_proto_negotiated(), b'spdy/2') |
| 1676 | self.assertEqual(client.get_next_proto_negotiated(), b'spdy/2') |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1677 | |
| 1678 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1679 | def test_npn_client_fail(self): |
| 1680 | """ |
| 1681 | Tests that when clients and servers cannot agree on what protocol |
| 1682 | to use next that the TLS connection does not get established. |
| 1683 | """ |
| 1684 | advertise_args = [] |
| 1685 | select_args = [] |
| 1686 | def advertise(conn): |
| 1687 | advertise_args.append((conn,)) |
| 1688 | return [b'http/1.1', b'spdy/2'] |
| 1689 | def select(conn, options): |
| 1690 | select_args.append((conn, options)) |
| 1691 | return b'' |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1692 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1693 | server_context = Context(TLSv1_METHOD) |
| 1694 | server_context.set_npn_advertise_callback(advertise) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1695 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1696 | client_context = Context(TLSv1_METHOD) |
| 1697 | client_context.set_npn_select_callback(select) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1698 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1699 | # Necessary to actually accept the connection |
| 1700 | server_context.use_privatekey( |
| 1701 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1702 | server_context.use_certificate( |
| 1703 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1704 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1705 | # Do a little connection to trigger the logic |
| 1706 | server = Connection(server_context, None) |
| 1707 | server.set_accept_state() |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1708 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1709 | client = Connection(client_context, None) |
| 1710 | client.set_connect_state() |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1711 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1712 | # If the client doesn't return anything, the connection will fail. |
| 1713 | self.assertRaises(Error, self._interactInMemory, server, client) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1714 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1715 | self.assertEqual([(server,)], advertise_args) |
| 1716 | self.assertEqual([(client, [b'http/1.1', b'spdy/2'])], select_args) |
Cory Benfield | 84a121e | 2014-03-31 20:30:25 +0100 | [diff] [blame] | 1717 | |
| 1718 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1719 | def test_npn_select_error(self): |
| 1720 | """ |
| 1721 | Test that we can handle exceptions in the select callback. If |
| 1722 | select fails it should be fatal to the connection. |
| 1723 | """ |
| 1724 | advertise_args = [] |
| 1725 | def advertise(conn): |
| 1726 | advertise_args.append((conn,)) |
| 1727 | return [b'http/1.1', b'spdy/2'] |
| 1728 | def select(conn, options): |
| 1729 | raise TypeError |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1730 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1731 | server_context = Context(TLSv1_METHOD) |
| 1732 | server_context.set_npn_advertise_callback(advertise) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1733 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1734 | client_context = Context(TLSv1_METHOD) |
| 1735 | client_context.set_npn_select_callback(select) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1736 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1737 | # Necessary to actually accept the connection |
| 1738 | server_context.use_privatekey( |
| 1739 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1740 | server_context.use_certificate( |
| 1741 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1742 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1743 | # Do a little connection to trigger the logic |
| 1744 | server = Connection(server_context, None) |
| 1745 | server.set_accept_state() |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1746 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1747 | client = Connection(client_context, None) |
| 1748 | client.set_connect_state() |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1749 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1750 | # If the callback throws an exception it should be raised here. |
| 1751 | self.assertRaises( |
| 1752 | TypeError, self._interactInMemory, server, client |
| 1753 | ) |
| 1754 | self.assertEqual([(server,)], advertise_args) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1755 | |
| 1756 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1757 | def test_npn_advertise_error(self): |
| 1758 | """ |
| 1759 | Test that we can handle exceptions in the advertise callback. If |
| 1760 | advertise fails no NPN is advertised to the client. |
| 1761 | """ |
| 1762 | select_args = [] |
| 1763 | def advertise(conn): |
| 1764 | raise TypeError |
| 1765 | def select(conn, options): |
| 1766 | select_args.append((conn, options)) |
| 1767 | return b'' |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1768 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1769 | server_context = Context(TLSv1_METHOD) |
| 1770 | server_context.set_npn_advertise_callback(advertise) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1771 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1772 | client_context = Context(TLSv1_METHOD) |
| 1773 | client_context.set_npn_select_callback(select) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1774 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1775 | # Necessary to actually accept the connection |
| 1776 | server_context.use_privatekey( |
| 1777 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1778 | server_context.use_certificate( |
| 1779 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1780 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1781 | # Do a little connection to trigger the logic |
| 1782 | server = Connection(server_context, None) |
| 1783 | server.set_accept_state() |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1784 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1785 | client = Connection(client_context, None) |
| 1786 | client.set_connect_state() |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1787 | |
Cory Benfield | ba1820d | 2015-04-13 17:39:12 -0400 | [diff] [blame] | 1788 | # If the client doesn't return anything, the connection will fail. |
| 1789 | self.assertRaises( |
| 1790 | TypeError, self._interactInMemory, server, client |
| 1791 | ) |
| 1792 | self.assertEqual([], select_args) |
| 1793 | |
| 1794 | else: |
| 1795 | # No NPN. |
| 1796 | def test_npn_not_implemented(self): |
| 1797 | # Test the context methods first. |
| 1798 | context = Context(TLSv1_METHOD) |
| 1799 | fail_methods = [ |
| 1800 | context.set_npn_advertise_callback, |
| 1801 | context.set_npn_select_callback, |
| 1802 | ] |
| 1803 | for method in fail_methods: |
| 1804 | self.assertRaises( |
| 1805 | NotImplementedError, method, None |
| 1806 | ) |
| 1807 | |
| 1808 | # Now test a connection. |
| 1809 | conn = Connection(context) |
| 1810 | fail_methods = [ |
| 1811 | conn.get_next_proto_negotiated, |
| 1812 | ] |
| 1813 | for method in fail_methods: |
| 1814 | self.assertRaises(NotImplementedError, method) |
Cory Benfield | 0ea76e7 | 2015-03-22 09:05:28 +0000 | [diff] [blame] | 1815 | |
| 1816 | |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 1817 | |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1818 | class ApplicationLayerProtoNegotiationTests(TestCase, _LoopbackMixin): |
| 1819 | """ |
| 1820 | Tests for ALPN in PyOpenSSL. |
| 1821 | """ |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1822 | # Skip tests on versions that don't support ALPN. |
| 1823 | if _lib.Cryptography_HAS_ALPN: |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1824 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1825 | def test_alpn_success(self): |
| 1826 | """ |
Cory Benfield | bb8516b | 2015-04-13 18:12:53 -0400 | [diff] [blame] | 1827 | Clients and servers that agree on the negotiated ALPN protocol can |
| 1828 | correct establish a connection, and the agreed protocol is reported |
| 1829 | by the connections. |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1830 | """ |
| 1831 | select_args = [] |
| 1832 | def select(conn, options): |
| 1833 | select_args.append((conn, options)) |
| 1834 | return b'spdy/2' |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1835 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1836 | client_context = Context(TLSv1_METHOD) |
| 1837 | client_context.set_alpn_protos([b'http/1.1', b'spdy/2']) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1838 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1839 | server_context = Context(TLSv1_METHOD) |
| 1840 | server_context.set_alpn_select_callback(select) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1841 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1842 | # Necessary to actually accept the connection |
| 1843 | server_context.use_privatekey( |
| 1844 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1845 | server_context.use_certificate( |
| 1846 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1847 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1848 | # Do a little connection to trigger the logic |
| 1849 | server = Connection(server_context, None) |
| 1850 | server.set_accept_state() |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1851 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1852 | client = Connection(client_context, None) |
| 1853 | client.set_connect_state() |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1854 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1855 | self._interactInMemory(server, client) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1856 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1857 | self.assertEqual([(server, [b'http/1.1', b'spdy/2'])], select_args) |
| 1858 | |
| 1859 | self.assertEqual(server.get_alpn_proto_negotiated(), b'spdy/2') |
| 1860 | self.assertEqual(client.get_alpn_proto_negotiated(), b'spdy/2') |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1861 | |
| 1862 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1863 | def test_alpn_set_on_connection(self): |
| 1864 | """ |
| 1865 | The same as test_alpn_success, but setting the ALPN protocols on |
| 1866 | the connection rather than the context. |
| 1867 | """ |
| 1868 | select_args = [] |
| 1869 | def select(conn, options): |
| 1870 | select_args.append((conn, options)) |
| 1871 | return b'spdy/2' |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1872 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1873 | # Setup the client context but don't set any ALPN protocols. |
| 1874 | client_context = Context(TLSv1_METHOD) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1875 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1876 | server_context = Context(TLSv1_METHOD) |
| 1877 | server_context.set_alpn_select_callback(select) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1878 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1879 | # Necessary to actually accept the connection |
| 1880 | server_context.use_privatekey( |
| 1881 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1882 | server_context.use_certificate( |
| 1883 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1884 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1885 | # Do a little connection to trigger the logic |
| 1886 | server = Connection(server_context, None) |
| 1887 | server.set_accept_state() |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1888 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1889 | # Set the ALPN protocols on the client connection. |
| 1890 | client = Connection(client_context, None) |
| 1891 | client.set_alpn_protos([b'http/1.1', b'spdy/2']) |
| 1892 | client.set_connect_state() |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1893 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1894 | self._interactInMemory(server, client) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1895 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1896 | self.assertEqual([(server, [b'http/1.1', b'spdy/2'])], select_args) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1897 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1898 | self.assertEqual(server.get_alpn_proto_negotiated(), b'spdy/2') |
| 1899 | self.assertEqual(client.get_alpn_proto_negotiated(), b'spdy/2') |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1900 | |
| 1901 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1902 | def test_alpn_server_fail(self): |
| 1903 | """ |
Cory Benfield | bb8516b | 2015-04-13 18:12:53 -0400 | [diff] [blame] | 1904 | When clients and servers cannot agree on what protocol to use next |
| 1905 | the TLS connection does not get established. |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1906 | """ |
| 1907 | select_args = [] |
| 1908 | def select(conn, options): |
| 1909 | select_args.append((conn, options)) |
| 1910 | return b'' |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1911 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1912 | client_context = Context(TLSv1_METHOD) |
| 1913 | client_context.set_alpn_protos([b'http/1.1', b'spdy/2']) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1914 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1915 | server_context = Context(TLSv1_METHOD) |
| 1916 | server_context.set_alpn_select_callback(select) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1917 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1918 | # Necessary to actually accept the connection |
| 1919 | server_context.use_privatekey( |
| 1920 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1921 | server_context.use_certificate( |
| 1922 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1923 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1924 | # Do a little connection to trigger the logic |
| 1925 | server = Connection(server_context, None) |
| 1926 | server.set_accept_state() |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1927 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1928 | client = Connection(client_context, None) |
| 1929 | client.set_connect_state() |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1930 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1931 | # If the client doesn't return anything, the connection will fail. |
| 1932 | self.assertRaises(Error, self._interactInMemory, server, client) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1933 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1934 | self.assertEqual([(server, [b'http/1.1', b'spdy/2'])], select_args) |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 1935 | |
| 1936 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1937 | def test_alpn_no_server(self): |
| 1938 | """ |
Cory Benfield | bb8516b | 2015-04-13 18:12:53 -0400 | [diff] [blame] | 1939 | When clients and servers cannot agree on what protocol to use next |
| 1940 | because the server doesn't offer ALPN, no protocol is negotiated. |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1941 | """ |
| 1942 | client_context = Context(TLSv1_METHOD) |
| 1943 | client_context.set_alpn_protos([b'http/1.1', b'spdy/2']) |
Cory Benfield | e3d5715 | 2015-04-11 17:57:35 -0400 | [diff] [blame] | 1944 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1945 | server_context = Context(TLSv1_METHOD) |
Cory Benfield | e3d5715 | 2015-04-11 17:57:35 -0400 | [diff] [blame] | 1946 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1947 | # Necessary to actually accept the connection |
| 1948 | server_context.use_privatekey( |
| 1949 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1950 | server_context.use_certificate( |
| 1951 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | e3d5715 | 2015-04-11 17:57:35 -0400 | [diff] [blame] | 1952 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1953 | # Do a little connection to trigger the logic |
| 1954 | server = Connection(server_context, None) |
| 1955 | server.set_accept_state() |
Cory Benfield | e3d5715 | 2015-04-11 17:57:35 -0400 | [diff] [blame] | 1956 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1957 | client = Connection(client_context, None) |
| 1958 | client.set_connect_state() |
Cory Benfield | e3d5715 | 2015-04-11 17:57:35 -0400 | [diff] [blame] | 1959 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1960 | # Do the dance. |
| 1961 | self._interactInMemory(server, client) |
Cory Benfield | e3d5715 | 2015-04-11 17:57:35 -0400 | [diff] [blame] | 1962 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1963 | self.assertEqual(client.get_alpn_proto_negotiated(), b'') |
Cory Benfield | e3d5715 | 2015-04-11 17:57:35 -0400 | [diff] [blame] | 1964 | |
| 1965 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1966 | def test_alpn_callback_exception(self): |
| 1967 | """ |
Cory Benfield | bb8516b | 2015-04-13 18:12:53 -0400 | [diff] [blame] | 1968 | We can handle exceptions in the ALPN select callback. |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1969 | """ |
| 1970 | select_args = [] |
| 1971 | def select(conn, options): |
| 1972 | select_args.append((conn, options)) |
Cory Benfield | ef40145 | 2015-04-13 18:17:36 -0400 | [diff] [blame] | 1973 | raise TypeError() |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1974 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1975 | client_context = Context(TLSv1_METHOD) |
| 1976 | client_context.set_alpn_protos([b'http/1.1', b'spdy/2']) |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1977 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1978 | server_context = Context(TLSv1_METHOD) |
| 1979 | server_context.set_alpn_select_callback(select) |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1980 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1981 | # Necessary to actually accept the connection |
| 1982 | server_context.use_privatekey( |
| 1983 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 1984 | server_context.use_certificate( |
| 1985 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1986 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1987 | # Do a little connection to trigger the logic |
| 1988 | server = Connection(server_context, None) |
| 1989 | server.set_accept_state() |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1990 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1991 | client = Connection(client_context, None) |
| 1992 | client.set_connect_state() |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1993 | |
Cory Benfield | e46fa84 | 2015-04-13 16:50:49 -0400 | [diff] [blame] | 1994 | self.assertRaises( |
| 1995 | TypeError, self._interactInMemory, server, client |
| 1996 | ) |
| 1997 | self.assertEqual([(server, [b'http/1.1', b'spdy/2'])], select_args) |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 1998 | |
Cory Benfield | 0f7b04c | 2015-04-13 17:51:12 -0400 | [diff] [blame] | 1999 | else: |
| 2000 | # No ALPN. |
| 2001 | def test_alpn_not_implemented(self): |
Cory Benfield | ef40145 | 2015-04-13 18:17:36 -0400 | [diff] [blame] | 2002 | """ |
| 2003 | If ALPN is not in OpenSSL, we should raise NotImplementedError. |
| 2004 | """ |
Cory Benfield | 0f7b04c | 2015-04-13 17:51:12 -0400 | [diff] [blame] | 2005 | # Test the context methods first. |
| 2006 | context = Context(TLSv1_METHOD) |
Cory Benfield | 307e9e6 | 2015-04-13 18:21:12 -0400 | [diff] [blame] | 2007 | self.assertRaises( |
| 2008 | NotImplementedError, context.set_alpn_protos, None |
| 2009 | ) |
| 2010 | self.assertRaises( |
| 2011 | NotImplementedError, context.set_alpn_select_callback, None |
| 2012 | ) |
Cory Benfield | 0f7b04c | 2015-04-13 17:51:12 -0400 | [diff] [blame] | 2013 | |
| 2014 | # Now test a connection. |
| 2015 | conn = Connection(context) |
Cory Benfield | 307e9e6 | 2015-04-13 18:21:12 -0400 | [diff] [blame] | 2016 | self.assertRaises( |
Alex Gaynor | 6841723 | 2015-09-04 07:56:29 -0400 | [diff] [blame] | 2017 | NotImplementedError, conn.set_alpn_protos, None |
Cory Benfield | 307e9e6 | 2015-04-13 18:21:12 -0400 | [diff] [blame] | 2018 | ) |
Cory Benfield | 0f7b04c | 2015-04-13 17:51:12 -0400 | [diff] [blame] | 2019 | |
Cory Benfield | f1177e7 | 2015-04-12 09:11:49 -0400 | [diff] [blame] | 2020 | |
Cory Benfield | 12eae89 | 2014-06-07 15:42:56 +0100 | [diff] [blame] | 2021 | |
Jean-Paul Calderone | e0fcf51 | 2012-02-13 09:10:15 -0500 | [diff] [blame] | 2022 | class SessionTests(TestCase): |
| 2023 | """ |
| 2024 | Unit tests for :py:obj:`OpenSSL.SSL.Session`. |
| 2025 | """ |
| 2026 | def test_construction(self): |
| 2027 | """ |
| 2028 | :py:class:`Session` can be constructed with no arguments, creating a new |
| 2029 | instance of that type. |
| 2030 | """ |
| 2031 | new_session = Session() |
| 2032 | self.assertTrue(isinstance(new_session, Session)) |
| 2033 | |
| 2034 | |
| 2035 | def test_construction_wrong_args(self): |
| 2036 | """ |
| 2037 | If any arguments are passed to :py:class:`Session`, :py:obj:`TypeError` |
| 2038 | is raised. |
| 2039 | """ |
| 2040 | self.assertRaises(TypeError, Session, 123) |
| 2041 | self.assertRaises(TypeError, Session, "hello") |
| 2042 | self.assertRaises(TypeError, Session, object()) |
| 2043 | |
| 2044 | |
| 2045 | |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 2046 | class ConnectionTests(TestCase, _LoopbackMixin): |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 2047 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2048 | Unit tests for :py:obj:`OpenSSL.SSL.Connection`. |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 2049 | """ |
Jean-Paul Calderone | 541eaf2 | 2010-09-09 18:55:08 -0400 | [diff] [blame] | 2050 | # XXX get_peer_certificate -> None |
| 2051 | # XXX sock_shutdown |
| 2052 | # XXX master_key -> TypeError |
| 2053 | # XXX server_random -> TypeError |
| 2054 | # XXX state_string |
| 2055 | # XXX connect -> TypeError |
| 2056 | # XXX connect_ex -> TypeError |
| 2057 | # XXX set_connect_state -> TypeError |
| 2058 | # XXX set_accept_state -> TypeError |
| 2059 | # XXX renegotiate_pending |
| 2060 | # XXX do_handshake -> TypeError |
| 2061 | # XXX bio_read -> TypeError |
| 2062 | # XXX recv -> TypeError |
| 2063 | # XXX send -> TypeError |
| 2064 | # XXX bio_write -> TypeError |
| 2065 | |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 2066 | def test_type(self): |
| 2067 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2068 | :py:obj:`Connection` and :py:obj:`ConnectionType` refer to the same type object and |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2069 | can be used to create instances of that type. |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 2070 | """ |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2071 | self.assertIdentical(Connection, ConnectionType) |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 2072 | ctx = Context(TLSv1_METHOD) |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2073 | self.assertConsistentType(Connection, 'Connection', ctx, None) |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 2074 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2075 | |
Jean-Paul Calderone | 4fd058a | 2009-11-22 11:46:42 -0500 | [diff] [blame] | 2076 | def test_get_context(self): |
| 2077 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2078 | :py:obj:`Connection.get_context` returns the :py:obj:`Context` instance used to |
| 2079 | construct the :py:obj:`Connection` instance. |
Jean-Paul Calderone | 4fd058a | 2009-11-22 11:46:42 -0500 | [diff] [blame] | 2080 | """ |
| 2081 | context = Context(TLSv1_METHOD) |
| 2082 | connection = Connection(context, None) |
| 2083 | self.assertIdentical(connection.get_context(), context) |
| 2084 | |
| 2085 | |
| 2086 | def test_get_context_wrong_args(self): |
| 2087 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2088 | :py:obj:`Connection.get_context` raises :py:obj:`TypeError` if called with any |
Jean-Paul Calderone | 4fd058a | 2009-11-22 11:46:42 -0500 | [diff] [blame] | 2089 | arguments. |
| 2090 | """ |
| 2091 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2092 | self.assertRaises(TypeError, connection.get_context, None) |
| 2093 | |
| 2094 | |
Jean-Paul Calderone | 95613b7 | 2011-05-25 22:30:21 -0400 | [diff] [blame] | 2095 | def test_set_context_wrong_args(self): |
| 2096 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2097 | :py:obj:`Connection.set_context` raises :py:obj:`TypeError` if called with a |
| 2098 | non-:py:obj:`Context` instance argument or with any number of arguments other |
Jean-Paul Calderone | 95613b7 | 2011-05-25 22:30:21 -0400 | [diff] [blame] | 2099 | than 1. |
| 2100 | """ |
| 2101 | ctx = Context(TLSv1_METHOD) |
| 2102 | connection = Connection(ctx, None) |
| 2103 | self.assertRaises(TypeError, connection.set_context) |
| 2104 | self.assertRaises(TypeError, connection.set_context, object()) |
| 2105 | self.assertRaises(TypeError, connection.set_context, "hello") |
| 2106 | self.assertRaises(TypeError, connection.set_context, 1) |
| 2107 | self.assertRaises(TypeError, connection.set_context, 1, 2) |
| 2108 | self.assertRaises( |
| 2109 | TypeError, connection.set_context, Context(TLSv1_METHOD), 2) |
| 2110 | self.assertIdentical(ctx, connection.get_context()) |
| 2111 | |
| 2112 | |
| 2113 | def test_set_context(self): |
| 2114 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2115 | :py:obj:`Connection.set_context` specifies a new :py:obj:`Context` instance to be used |
Jean-Paul Calderone | 95613b7 | 2011-05-25 22:30:21 -0400 | [diff] [blame] | 2116 | for the connection. |
| 2117 | """ |
| 2118 | original = Context(SSLv23_METHOD) |
| 2119 | replacement = Context(TLSv1_METHOD) |
| 2120 | connection = Connection(original, None) |
| 2121 | connection.set_context(replacement) |
| 2122 | self.assertIdentical(replacement, connection.get_context()) |
| 2123 | # Lose our references to the contexts, just in case the Connection isn't |
| 2124 | # properly managing its own contributions to their reference counts. |
| 2125 | del original, replacement |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 2126 | collect() |
| 2127 | |
| 2128 | |
| 2129 | def test_set_tlsext_host_name_wrong_args(self): |
| 2130 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2131 | If :py:obj:`Connection.set_tlsext_host_name` is called with a non-byte string |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 2132 | argument or a byte string with an embedded NUL or other than one |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2133 | argument, :py:obj:`TypeError` is raised. |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 2134 | """ |
| 2135 | conn = Connection(Context(TLSv1_METHOD), None) |
| 2136 | self.assertRaises(TypeError, conn.set_tlsext_host_name) |
| 2137 | self.assertRaises(TypeError, conn.set_tlsext_host_name, object()) |
| 2138 | self.assertRaises(TypeError, conn.set_tlsext_host_name, 123, 456) |
| 2139 | self.assertRaises( |
| 2140 | TypeError, conn.set_tlsext_host_name, b("with\0null")) |
| 2141 | |
Abraham Martin | c5484ba | 2015-03-25 15:33:05 +0000 | [diff] [blame] | 2142 | if PY3: |
Jean-Paul Calderone | c4cb658 | 2011-05-26 18:47:00 -0400 | [diff] [blame] | 2143 | # On Python 3.x, don't accidentally implicitly convert from text. |
| 2144 | self.assertRaises( |
| 2145 | TypeError, |
| 2146 | conn.set_tlsext_host_name, b("example.com").decode("ascii")) |
Jean-Paul Calderone | 95613b7 | 2011-05-25 22:30:21 -0400 | [diff] [blame] | 2147 | |
| 2148 | |
Jean-Paul Calderone | 871a4d8 | 2011-05-26 19:24:02 -0400 | [diff] [blame] | 2149 | def test_get_servername_wrong_args(self): |
| 2150 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2151 | :py:obj:`Connection.get_servername` raises :py:obj:`TypeError` if called with any |
Jean-Paul Calderone | 871a4d8 | 2011-05-26 19:24:02 -0400 | [diff] [blame] | 2152 | arguments. |
| 2153 | """ |
| 2154 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2155 | self.assertRaises(TypeError, connection.get_servername, object()) |
| 2156 | self.assertRaises(TypeError, connection.get_servername, 1) |
| 2157 | self.assertRaises(TypeError, connection.get_servername, "hello") |
| 2158 | |
| 2159 | |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 2160 | def test_pending(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2161 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2162 | :py:obj:`Connection.pending` returns the number of bytes available for |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2163 | immediate read. |
| 2164 | """ |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 2165 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2166 | self.assertEquals(connection.pending(), 0) |
| 2167 | |
| 2168 | |
| 2169 | def test_pending_wrong_args(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2170 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2171 | :py:obj:`Connection.pending` raises :py:obj:`TypeError` if called with any arguments. |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2172 | """ |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 2173 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2174 | self.assertRaises(TypeError, connection.pending, None) |
| 2175 | |
| 2176 | |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 2177 | def test_peek(self): |
| 2178 | """ |
| 2179 | :py:obj:`Connection.recv` peeks into the connection if :py:obj:`socket.MSG_PEEK` is passed. |
| 2180 | """ |
| 2181 | server, client = self._loopback() |
| 2182 | server.send(b('xy')) |
| 2183 | self.assertEqual(client.recv(2, MSG_PEEK), b('xy')) |
| 2184 | self.assertEqual(client.recv(2, MSG_PEEK), b('xy')) |
| 2185 | self.assertEqual(client.recv(2), b('xy')) |
| 2186 | |
| 2187 | |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2188 | def test_connect_wrong_args(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2189 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2190 | :py:obj:`Connection.connect` raises :py:obj:`TypeError` if called with a non-address |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2191 | argument or with the wrong number of arguments. |
| 2192 | """ |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2193 | connection = Connection(Context(TLSv1_METHOD), socket()) |
| 2194 | self.assertRaises(TypeError, connection.connect, None) |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2195 | self.assertRaises(TypeError, connection.connect) |
| 2196 | self.assertRaises(TypeError, connection.connect, ("127.0.0.1", 1), None) |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2197 | |
kjav | fe508d6 | 2015-09-02 12:20:35 +0100 | [diff] [blame] | 2198 | def test_connection_undefined_attr(self): |
| 2199 | """ |
| 2200 | :py:obj:`Connection.connect` raises :py:obj:`TypeError` if called with a non-address |
| 2201 | argument or with the wrong number of arguments. |
| 2202 | """ |
Alex Gaynor | 1aabb2e | 2015-09-05 13:35:18 -0400 | [diff] [blame] | 2203 | |
kjav | fe508d6 | 2015-09-02 12:20:35 +0100 | [diff] [blame] | 2204 | def attr_access_test(connection): |
| 2205 | return connection.an_attribute_which_is_not_defined |
Alex Gaynor | 1aabb2e | 2015-09-05 13:35:18 -0400 | [diff] [blame] | 2206 | |
kjav | fe508d6 | 2015-09-02 12:20:35 +0100 | [diff] [blame] | 2207 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2208 | self.assertRaises(AttributeError, attr_access_test, connection) |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2209 | |
Jean-Paul Calderone | 8bdeba2 | 2010-07-29 09:45:07 -0400 | [diff] [blame] | 2210 | def test_connect_refused(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2211 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2212 | :py:obj:`Connection.connect` raises :py:obj:`socket.error` if the underlying socket |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2213 | connect method raises it. |
| 2214 | """ |
Jean-Paul Calderone | 8bdeba2 | 2010-07-29 09:45:07 -0400 | [diff] [blame] | 2215 | client = socket() |
| 2216 | context = Context(TLSv1_METHOD) |
| 2217 | clientSSL = Connection(context, client) |
Alex Gaynor | e69c278 | 2015-09-05 14:07:48 -0400 | [diff] [blame^] | 2218 | try: |
Alex Gaynor | 1aabb2e | 2015-09-05 13:35:18 -0400 | [diff] [blame] | 2219 | clientSSL.connect(("127.0.0.1", 1)) |
Alex Gaynor | e69c278 | 2015-09-05 14:07:48 -0400 | [diff] [blame^] | 2220 | except error as e: |
| 2221 | pass |
| 2222 | assert e.args[0] == ECONNREFUSED |
Jean-Paul Calderone | 8bdeba2 | 2010-07-29 09:45:07 -0400 | [diff] [blame] | 2223 | |
| 2224 | def test_connect(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2225 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2226 | :py:obj:`Connection.connect` establishes a connection to the specified address. |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2227 | """ |
Jean-Paul Calderone | 0bcb0e0 | 2010-07-29 09:49:59 -0400 | [diff] [blame] | 2228 | port = socket() |
| 2229 | port.bind(('', 0)) |
| 2230 | port.listen(3) |
| 2231 | |
| 2232 | clientSSL = Connection(Context(TLSv1_METHOD), socket()) |
Jean-Paul Calderone | b6e0fd9 | 2010-09-19 09:22:13 -0400 | [diff] [blame] | 2233 | clientSSL.connect(('127.0.0.1', port.getsockname()[1])) |
| 2234 | # XXX An assertion? Or something? |
Jean-Paul Calderone | 0bcb0e0 | 2010-07-29 09:49:59 -0400 | [diff] [blame] | 2235 | |
| 2236 | |
Jean-Paul Calderone | 7b61487 | 2010-09-24 17:43:44 -0400 | [diff] [blame] | 2237 | if platform == "darwin": |
| 2238 | "connect_ex sometimes causes a kernel panic on OS X 10.6.4" |
| 2239 | else: |
| 2240 | def test_connect_ex(self): |
| 2241 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2242 | If there is a connection error, :py:obj:`Connection.connect_ex` returns the |
Jean-Paul Calderone | 7b61487 | 2010-09-24 17:43:44 -0400 | [diff] [blame] | 2243 | errno instead of raising an exception. |
| 2244 | """ |
| 2245 | port = socket() |
| 2246 | port.bind(('', 0)) |
| 2247 | port.listen(3) |
Jean-Paul Calderone | 55d91f4 | 2010-07-29 19:22:17 -0400 | [diff] [blame] | 2248 | |
Jean-Paul Calderone | 7b61487 | 2010-09-24 17:43:44 -0400 | [diff] [blame] | 2249 | clientSSL = Connection(Context(TLSv1_METHOD), socket()) |
| 2250 | clientSSL.setblocking(False) |
| 2251 | result = clientSSL.connect_ex(port.getsockname()) |
| 2252 | expected = (EINPROGRESS, EWOULDBLOCK) |
| 2253 | self.assertTrue( |
| 2254 | result in expected, "%r not in %r" % (result, expected)) |
Jean-Paul Calderone | 55d91f4 | 2010-07-29 19:22:17 -0400 | [diff] [blame] | 2255 | |
| 2256 | |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2257 | def test_accept_wrong_args(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2258 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2259 | :py:obj:`Connection.accept` raises :py:obj:`TypeError` if called with any arguments. |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2260 | """ |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2261 | connection = Connection(Context(TLSv1_METHOD), socket()) |
| 2262 | self.assertRaises(TypeError, connection.accept, None) |
| 2263 | |
| 2264 | |
Jean-Paul Calderone | 0bcb0e0 | 2010-07-29 09:49:59 -0400 | [diff] [blame] | 2265 | def test_accept(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2266 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2267 | :py:obj:`Connection.accept` accepts a pending connection attempt and returns a |
| 2268 | tuple of a new :py:obj:`Connection` (the accepted client) and the address the |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2269 | connection originated from. |
| 2270 | """ |
Jean-Paul Calderone | 8bdeba2 | 2010-07-29 09:45:07 -0400 | [diff] [blame] | 2271 | ctx = Context(TLSv1_METHOD) |
| 2272 | ctx.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 2273 | ctx.use_certificate(load_certificate(FILETYPE_PEM, server_cert_pem)) |
Jean-Paul Calderone | 0bcb0e0 | 2010-07-29 09:49:59 -0400 | [diff] [blame] | 2274 | port = socket() |
| 2275 | portSSL = Connection(ctx, port) |
| 2276 | portSSL.bind(('', 0)) |
| 2277 | portSSL.listen(3) |
Jean-Paul Calderone | 8bdeba2 | 2010-07-29 09:45:07 -0400 | [diff] [blame] | 2278 | |
Jean-Paul Calderone | 0bcb0e0 | 2010-07-29 09:49:59 -0400 | [diff] [blame] | 2279 | clientSSL = Connection(Context(TLSv1_METHOD), socket()) |
Jean-Paul Calderone | b6e0fd9 | 2010-09-19 09:22:13 -0400 | [diff] [blame] | 2280 | |
| 2281 | # Calling portSSL.getsockname() here to get the server IP address sounds |
| 2282 | # great, but frequently fails on Windows. |
| 2283 | clientSSL.connect(('127.0.0.1', portSSL.getsockname()[1])) |
Jean-Paul Calderone | 8bdeba2 | 2010-07-29 09:45:07 -0400 | [diff] [blame] | 2284 | |
Jean-Paul Calderone | 0bcb0e0 | 2010-07-29 09:49:59 -0400 | [diff] [blame] | 2285 | serverSSL, address = portSSL.accept() |
| 2286 | |
| 2287 | self.assertTrue(isinstance(serverSSL, Connection)) |
| 2288 | self.assertIdentical(serverSSL.get_context(), ctx) |
| 2289 | self.assertEquals(address, clientSSL.getsockname()) |
Jean-Paul Calderone | 8bdeba2 | 2010-07-29 09:45:07 -0400 | [diff] [blame] | 2290 | |
| 2291 | |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2292 | def test_shutdown_wrong_args(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2293 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2294 | :py:obj:`Connection.shutdown` raises :py:obj:`TypeError` if called with the wrong |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2295 | number of arguments or with arguments other than integers. |
| 2296 | """ |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2297 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2298 | self.assertRaises(TypeError, connection.shutdown, None) |
Jean-Paul Calderone | 1d3e022 | 2010-07-29 22:52:45 -0400 | [diff] [blame] | 2299 | self.assertRaises(TypeError, connection.get_shutdown, None) |
| 2300 | self.assertRaises(TypeError, connection.set_shutdown) |
| 2301 | self.assertRaises(TypeError, connection.set_shutdown, None) |
| 2302 | self.assertRaises(TypeError, connection.set_shutdown, 0, 1) |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2303 | |
| 2304 | |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 2305 | def test_shutdown(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2306 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2307 | :py:obj:`Connection.shutdown` performs an SSL-level connection shutdown. |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2308 | """ |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 2309 | server, client = self._loopback() |
Jean-Paul Calderone | e4f6b47 | 2010-07-29 22:50:58 -0400 | [diff] [blame] | 2310 | self.assertFalse(server.shutdown()) |
| 2311 | self.assertEquals(server.get_shutdown(), SENT_SHUTDOWN) |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 2312 | self.assertRaises(ZeroReturnError, client.recv, 1024) |
Jean-Paul Calderone | e4f6b47 | 2010-07-29 22:50:58 -0400 | [diff] [blame] | 2313 | self.assertEquals(client.get_shutdown(), RECEIVED_SHUTDOWN) |
| 2314 | client.shutdown() |
Alex Gaynor | 4330778 | 2015-09-04 09:05:45 -0400 | [diff] [blame] | 2315 | self.assertEquals(client.get_shutdown(), SENT_SHUTDOWN | RECEIVED_SHUTDOWN) |
Jean-Paul Calderone | e4f6b47 | 2010-07-29 22:50:58 -0400 | [diff] [blame] | 2316 | self.assertRaises(ZeroReturnError, server.recv, 1024) |
Alex Gaynor | 4330778 | 2015-09-04 09:05:45 -0400 | [diff] [blame] | 2317 | self.assertEquals(server.get_shutdown(), SENT_SHUTDOWN | RECEIVED_SHUTDOWN) |
Jean-Paul Calderone | 9485f2c | 2010-07-29 22:38:42 -0400 | [diff] [blame] | 2318 | |
| 2319 | |
Paul Aurich | c85e086 | 2015-01-08 08:34:33 -0800 | [diff] [blame] | 2320 | def test_shutdown_closed(self): |
| 2321 | """ |
| 2322 | If the underlying socket is closed, :py:obj:`Connection.shutdown` propagates the |
| 2323 | write error from the low level write call. |
| 2324 | """ |
| 2325 | server, client = self._loopback() |
| 2326 | server.sock_shutdown(2) |
| 2327 | exc = self.assertRaises(SysCallError, server.shutdown) |
| 2328 | if platform == "win32": |
| 2329 | self.assertEqual(exc.args[0], ESHUTDOWN) |
| 2330 | else: |
| 2331 | self.assertEqual(exc.args[0], EPIPE) |
| 2332 | |
| 2333 | |
Glyph | 8938947 | 2015-04-14 17:29:26 -0400 | [diff] [blame] | 2334 | def test_shutdown_truncated(self): |
Glyph | 6064ec3 | 2015-04-14 16:38:22 -0400 | [diff] [blame] | 2335 | """ |
Glyph | 8938947 | 2015-04-14 17:29:26 -0400 | [diff] [blame] | 2336 | If the underlying connection is truncated, :obj:`Connection.shutdown` |
| 2337 | raises an :obj:`Error`. |
Glyph | 6064ec3 | 2015-04-14 16:38:22 -0400 | [diff] [blame] | 2338 | """ |
Glyph | 8938947 | 2015-04-14 17:29:26 -0400 | [diff] [blame] | 2339 | server_ctx = Context(TLSv1_METHOD) |
| 2340 | client_ctx = Context(TLSv1_METHOD) |
| 2341 | server_ctx.use_privatekey( |
| 2342 | load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 2343 | server_ctx.use_certificate( |
| 2344 | load_certificate(FILETYPE_PEM, server_cert_pem)) |
| 2345 | server = Connection(server_ctx, None) |
| 2346 | client = Connection(client_ctx, None) |
| 2347 | self._handshakeInMemory(client, server) |
| 2348 | self.assertEqual(server.shutdown(), False) |
| 2349 | self.assertRaises(WantReadError, server.shutdown) |
| 2350 | server.bio_shutdown() |
Glyph | 6064ec3 | 2015-04-14 16:38:22 -0400 | [diff] [blame] | 2351 | self.assertRaises(Error, server.shutdown) |
| 2352 | |
| 2353 | |
Jean-Paul Calderone | c89eef2 | 2010-07-29 22:51:58 -0400 | [diff] [blame] | 2354 | def test_set_shutdown(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2355 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2356 | :py:obj:`Connection.set_shutdown` sets the state of the SSL connection shutdown |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2357 | process. |
| 2358 | """ |
Jean-Paul Calderone | c89eef2 | 2010-07-29 22:51:58 -0400 | [diff] [blame] | 2359 | connection = Connection(Context(TLSv1_METHOD), socket()) |
| 2360 | connection.set_shutdown(RECEIVED_SHUTDOWN) |
| 2361 | self.assertEquals(connection.get_shutdown(), RECEIVED_SHUTDOWN) |
| 2362 | |
| 2363 | |
Jean-Paul Calderone | f73a3cb | 2014-02-09 08:49:06 -0500 | [diff] [blame] | 2364 | if not PY3: |
| 2365 | def test_set_shutdown_long(self): |
| 2366 | """ |
| 2367 | On Python 2 :py:obj:`Connection.set_shutdown` accepts an argument |
| 2368 | of type :py:obj:`long` as well as :py:obj:`int`. |
| 2369 | """ |
| 2370 | connection = Connection(Context(TLSv1_METHOD), socket()) |
| 2371 | connection.set_shutdown(long(RECEIVED_SHUTDOWN)) |
| 2372 | self.assertEquals(connection.get_shutdown(), RECEIVED_SHUTDOWN) |
| 2373 | |
| 2374 | |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2375 | def test_app_data_wrong_args(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2376 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2377 | :py:obj:`Connection.set_app_data` raises :py:obj:`TypeError` if called with other than |
| 2378 | one argument. :py:obj:`Connection.get_app_data` raises :py:obj:`TypeError` if called |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2379 | with any arguments. |
| 2380 | """ |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2381 | conn = Connection(Context(TLSv1_METHOD), None) |
| 2382 | self.assertRaises(TypeError, conn.get_app_data, None) |
| 2383 | self.assertRaises(TypeError, conn.set_app_data) |
| 2384 | self.assertRaises(TypeError, conn.set_app_data, None, None) |
| 2385 | |
| 2386 | |
Jean-Paul Calderone | 1bd8c79 | 2010-07-29 09:51:06 -0400 | [diff] [blame] | 2387 | def test_app_data(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2388 | """ |
| 2389 | Any object can be set as app data by passing it to |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2390 | :py:obj:`Connection.set_app_data` and later retrieved with |
| 2391 | :py:obj:`Connection.get_app_data`. |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2392 | """ |
Jean-Paul Calderone | 1bd8c79 | 2010-07-29 09:51:06 -0400 | [diff] [blame] | 2393 | conn = Connection(Context(TLSv1_METHOD), None) |
| 2394 | app_data = object() |
| 2395 | conn.set_app_data(app_data) |
| 2396 | self.assertIdentical(conn.get_app_data(), app_data) |
| 2397 | |
| 2398 | |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2399 | def test_makefile(self): |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2400 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2401 | :py:obj:`Connection.makefile` is not implemented and calling that method raises |
| 2402 | :py:obj:`NotImplementedError`. |
Jean-Paul Calderone | 93dba22 | 2010-09-08 22:59:37 -0400 | [diff] [blame] | 2403 | """ |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 2404 | conn = Connection(Context(TLSv1_METHOD), None) |
| 2405 | self.assertRaises(NotImplementedError, conn.makefile) |
| 2406 | |
| 2407 | |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 2408 | def test_get_peer_cert_chain_wrong_args(self): |
| 2409 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2410 | :py:obj:`Connection.get_peer_cert_chain` raises :py:obj:`TypeError` if called with any |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 2411 | arguments. |
| 2412 | """ |
| 2413 | conn = Connection(Context(TLSv1_METHOD), None) |
| 2414 | self.assertRaises(TypeError, conn.get_peer_cert_chain, 1) |
| 2415 | self.assertRaises(TypeError, conn.get_peer_cert_chain, "foo") |
| 2416 | self.assertRaises(TypeError, conn.get_peer_cert_chain, object()) |
| 2417 | self.assertRaises(TypeError, conn.get_peer_cert_chain, []) |
| 2418 | |
| 2419 | |
| 2420 | def test_get_peer_cert_chain(self): |
| 2421 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2422 | :py:obj:`Connection.get_peer_cert_chain` returns a list of certificates which |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 2423 | the connected server returned for the certification verification. |
| 2424 | """ |
| 2425 | chain = _create_certificate_chain() |
| 2426 | [(cakey, cacert), (ikey, icert), (skey, scert)] = chain |
| 2427 | |
| 2428 | serverContext = Context(TLSv1_METHOD) |
| 2429 | serverContext.use_privatekey(skey) |
| 2430 | serverContext.use_certificate(scert) |
| 2431 | serverContext.add_extra_chain_cert(icert) |
| 2432 | serverContext.add_extra_chain_cert(cacert) |
| 2433 | server = Connection(serverContext, None) |
| 2434 | server.set_accept_state() |
| 2435 | |
| 2436 | # Create the client |
| 2437 | clientContext = Context(TLSv1_METHOD) |
| 2438 | clientContext.set_verify(VERIFY_NONE, verify_cb) |
| 2439 | client = Connection(clientContext, None) |
| 2440 | client.set_connect_state() |
| 2441 | |
| 2442 | self._interactInMemory(client, server) |
| 2443 | |
| 2444 | chain = client.get_peer_cert_chain() |
| 2445 | self.assertEqual(len(chain), 3) |
Jean-Paul Calderone | e33300c | 2011-05-19 21:44:38 -0400 | [diff] [blame] | 2446 | self.assertEqual( |
Jean-Paul Calderone | 24a4243 | 2011-05-19 22:21:18 -0400 | [diff] [blame] | 2447 | "Server Certificate", chain[0].get_subject().CN) |
Jean-Paul Calderone | e33300c | 2011-05-19 21:44:38 -0400 | [diff] [blame] | 2448 | self.assertEqual( |
Jean-Paul Calderone | 24a4243 | 2011-05-19 22:21:18 -0400 | [diff] [blame] | 2449 | "Intermediate Certificate", chain[1].get_subject().CN) |
Jean-Paul Calderone | e33300c | 2011-05-19 21:44:38 -0400 | [diff] [blame] | 2450 | self.assertEqual( |
Jean-Paul Calderone | 24a4243 | 2011-05-19 22:21:18 -0400 | [diff] [blame] | 2451 | "Authority Certificate", chain[2].get_subject().CN) |
| 2452 | |
| 2453 | |
| 2454 | def test_get_peer_cert_chain_none(self): |
| 2455 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2456 | :py:obj:`Connection.get_peer_cert_chain` returns :py:obj:`None` if the peer sends no |
Jean-Paul Calderone | 24a4243 | 2011-05-19 22:21:18 -0400 | [diff] [blame] | 2457 | certificate chain. |
| 2458 | """ |
| 2459 | ctx = Context(TLSv1_METHOD) |
| 2460 | ctx.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 2461 | ctx.use_certificate(load_certificate(FILETYPE_PEM, server_cert_pem)) |
| 2462 | server = Connection(ctx, None) |
| 2463 | server.set_accept_state() |
| 2464 | client = Connection(Context(TLSv1_METHOD), None) |
| 2465 | client.set_connect_state() |
| 2466 | self._interactInMemory(client, server) |
| 2467 | self.assertIdentical(None, server.get_peer_cert_chain()) |
Jean-Paul Calderone | 20222ae | 2011-05-19 21:43:46 -0400 | [diff] [blame] | 2468 | |
| 2469 | |
Jean-Paul Calderone | 64eaffc | 2012-02-13 11:53:49 -0500 | [diff] [blame] | 2470 | def test_get_session_wrong_args(self): |
| 2471 | """ |
| 2472 | :py:obj:`Connection.get_session` raises :py:obj:`TypeError` if called |
| 2473 | with any arguments. |
| 2474 | """ |
| 2475 | ctx = Context(TLSv1_METHOD) |
| 2476 | server = Connection(ctx, None) |
| 2477 | self.assertRaises(TypeError, server.get_session, 123) |
| 2478 | self.assertRaises(TypeError, server.get_session, "hello") |
| 2479 | self.assertRaises(TypeError, server.get_session, object()) |
| 2480 | |
| 2481 | |
| 2482 | def test_get_session_unconnected(self): |
| 2483 | """ |
| 2484 | :py:obj:`Connection.get_session` returns :py:obj:`None` when used with |
| 2485 | an object which has not been connected. |
| 2486 | """ |
| 2487 | ctx = Context(TLSv1_METHOD) |
| 2488 | server = Connection(ctx, None) |
| 2489 | session = server.get_session() |
| 2490 | self.assertIdentical(None, session) |
| 2491 | |
| 2492 | |
| 2493 | def test_server_get_session(self): |
| 2494 | """ |
| 2495 | On the server side of a connection, :py:obj:`Connection.get_session` |
| 2496 | returns a :py:class:`Session` instance representing the SSL session for |
| 2497 | that connection. |
| 2498 | """ |
| 2499 | server, client = self._loopback() |
| 2500 | session = server.get_session() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2501 | self.assertIsInstance(session, Session) |
Jean-Paul Calderone | 64eaffc | 2012-02-13 11:53:49 -0500 | [diff] [blame] | 2502 | |
| 2503 | |
| 2504 | def test_client_get_session(self): |
| 2505 | """ |
| 2506 | On the client side of a connection, :py:obj:`Connection.get_session` |
| 2507 | returns a :py:class:`Session` instance representing the SSL session for |
| 2508 | that connection. |
| 2509 | """ |
| 2510 | server, client = self._loopback() |
| 2511 | session = client.get_session() |
Jean-Paul Calderone | a63714c | 2013-03-05 17:02:26 -0800 | [diff] [blame] | 2512 | self.assertIsInstance(session, Session) |
Jean-Paul Calderone | 64eaffc | 2012-02-13 11:53:49 -0500 | [diff] [blame] | 2513 | |
| 2514 | |
Jean-Paul Calderone | fef5c4b | 2012-02-14 16:31:52 -0500 | [diff] [blame] | 2515 | def test_set_session_wrong_args(self): |
| 2516 | """ |
| 2517 | If called with an object that is not an instance of :py:class:`Session`, |
| 2518 | or with other than one argument, :py:obj:`Connection.set_session` raises |
| 2519 | :py:obj:`TypeError`. |
| 2520 | """ |
| 2521 | ctx = Context(TLSv1_METHOD) |
| 2522 | connection = Connection(ctx, None) |
| 2523 | self.assertRaises(TypeError, connection.set_session) |
| 2524 | self.assertRaises(TypeError, connection.set_session, 123) |
| 2525 | self.assertRaises(TypeError, connection.set_session, "hello") |
| 2526 | self.assertRaises(TypeError, connection.set_session, object()) |
| 2527 | self.assertRaises( |
| 2528 | TypeError, connection.set_session, Session(), Session()) |
| 2529 | |
| 2530 | |
| 2531 | def test_client_set_session(self): |
| 2532 | """ |
| 2533 | :py:obj:`Connection.set_session`, when used prior to a connection being |
| 2534 | established, accepts a :py:class:`Session` instance and causes an |
| 2535 | attempt to re-use the session it represents when the SSL handshake is |
| 2536 | performed. |
| 2537 | """ |
| 2538 | key = load_privatekey(FILETYPE_PEM, server_key_pem) |
| 2539 | cert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 2540 | ctx = Context(TLSv1_METHOD) |
| 2541 | ctx.use_privatekey(key) |
| 2542 | ctx.use_certificate(cert) |
| 2543 | ctx.set_session_id("unity-test") |
| 2544 | |
| 2545 | def makeServer(socket): |
| 2546 | server = Connection(ctx, socket) |
| 2547 | server.set_accept_state() |
| 2548 | return server |
| 2549 | |
| 2550 | originalServer, originalClient = self._loopback( |
| 2551 | serverFactory=makeServer) |
| 2552 | originalSession = originalClient.get_session() |
| 2553 | |
| 2554 | def makeClient(socket): |
| 2555 | client = self._loopbackClientFactory(socket) |
| 2556 | client.set_session(originalSession) |
| 2557 | return client |
| 2558 | resumedServer, resumedClient = self._loopback( |
| 2559 | serverFactory=makeServer, |
| 2560 | clientFactory=makeClient) |
| 2561 | |
| 2562 | # This is a proxy: in general, we have no access to any unique |
| 2563 | # identifier for the session (new enough versions of OpenSSL expose a |
| 2564 | # hash which could be usable, but "new enough" is very, very new). |
| 2565 | # Instead, exploit the fact that the master key is re-used if the |
| 2566 | # session is re-used. As long as the master key for the two connections |
| 2567 | # is the same, the session was re-used! |
| 2568 | self.assertEqual( |
| 2569 | originalServer.master_key(), resumedServer.master_key()) |
| 2570 | |
| 2571 | |
Jean-Paul Calderone | 5ea4149 | 2012-02-14 16:51:35 -0500 | [diff] [blame] | 2572 | def test_set_session_wrong_method(self): |
| 2573 | """ |
Jean-Paul Calderone | 068cb59 | 2012-02-14 16:52:43 -0500 | [diff] [blame] | 2574 | If :py:obj:`Connection.set_session` is passed a :py:class:`Session` |
| 2575 | instance associated with a context using a different SSL method than the |
| 2576 | :py:obj:`Connection` is using, a :py:class:`OpenSSL.SSL.Error` is |
| 2577 | raised. |
Jean-Paul Calderone | 5ea4149 | 2012-02-14 16:51:35 -0500 | [diff] [blame] | 2578 | """ |
| 2579 | key = load_privatekey(FILETYPE_PEM, server_key_pem) |
| 2580 | cert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 2581 | ctx = Context(TLSv1_METHOD) |
| 2582 | ctx.use_privatekey(key) |
| 2583 | ctx.use_certificate(cert) |
| 2584 | ctx.set_session_id("unity-test") |
| 2585 | |
| 2586 | def makeServer(socket): |
| 2587 | server = Connection(ctx, socket) |
| 2588 | server.set_accept_state() |
| 2589 | return server |
| 2590 | |
| 2591 | originalServer, originalClient = self._loopback( |
| 2592 | serverFactory=makeServer) |
| 2593 | originalSession = originalClient.get_session() |
| 2594 | |
| 2595 | def makeClient(socket): |
| 2596 | # Intentionally use a different, incompatible method here. |
| 2597 | client = Connection(Context(SSLv3_METHOD), socket) |
| 2598 | client.set_connect_state() |
| 2599 | client.set_session(originalSession) |
| 2600 | return client |
| 2601 | |
| 2602 | self.assertRaises( |
| 2603 | Error, |
| 2604 | self._loopback, clientFactory=makeClient, serverFactory=makeServer) |
| 2605 | |
| 2606 | |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 2607 | def test_wantWriteError(self): |
| 2608 | """ |
| 2609 | :py:obj:`Connection` methods which generate output raise |
| 2610 | :py:obj:`OpenSSL.SSL.WantWriteError` if writing to the connection's BIO |
| 2611 | fail indicating a should-write state. |
| 2612 | """ |
| 2613 | client_socket, server_socket = socket_pair() |
| 2614 | # Fill up the client's send buffer so Connection won't be able to write |
Jean-Paul Calderone | bbff8b9 | 2014-03-22 14:20:30 -0400 | [diff] [blame] | 2615 | # anything. Only write a single byte at a time so we can be sure we |
| 2616 | # completely fill the buffer. Even though the socket API is allowed to |
| 2617 | # signal a short write via its return value it seems this doesn't |
| 2618 | # always happen on all platforms (FreeBSD and OS X particular) for the |
| 2619 | # very last bit of available buffer space. |
| 2620 | msg = b"x" |
| 2621 | for i in range(1024 * 1024 * 4): |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 2622 | try: |
| 2623 | client_socket.send(msg) |
| 2624 | except error as e: |
| 2625 | if e.errno == EWOULDBLOCK: |
| 2626 | break |
| 2627 | raise |
| 2628 | else: |
| 2629 | self.fail( |
| 2630 | "Failed to fill socket buffer, cannot test BIO want write") |
| 2631 | |
| 2632 | ctx = Context(TLSv1_METHOD) |
| 2633 | conn = Connection(ctx, client_socket) |
| 2634 | # Client's speak first, so make it an SSL client |
| 2635 | conn.set_connect_state() |
| 2636 | self.assertRaises(WantWriteError, conn.do_handshake) |
| 2637 | |
| 2638 | # XXX want_read |
| 2639 | |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2640 | def test_get_finished_before_connect(self): |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2641 | """ |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2642 | :py:obj:`Connection.get_finished` returns :py:obj:`None` before TLS |
| 2643 | handshake is completed. |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2644 | """ |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2645 | ctx = Context(TLSv1_METHOD) |
| 2646 | connection = Connection(ctx, None) |
| 2647 | self.assertEqual(connection.get_finished(), None) |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2648 | |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2649 | |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2650 | def test_get_peer_finished_before_connect(self): |
| 2651 | """ |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2652 | :py:obj:`Connection.get_peer_finished` returns :py:obj:`None` before |
| 2653 | TLS handshake is completed. |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2654 | """ |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2655 | ctx = Context(TLSv1_METHOD) |
| 2656 | connection = Connection(ctx, None) |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2657 | self.assertEqual(connection.get_peer_finished(), None) |
| 2658 | |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2659 | |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2660 | def test_get_finished(self): |
| 2661 | """ |
| 2662 | :py:obj:`Connection.get_finished` method returns the TLS Finished |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2663 | message send from client, or server. Finished messages are send during |
| 2664 | TLS handshake. |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2665 | """ |
| 2666 | |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2667 | server, client = self._loopback() |
| 2668 | |
| 2669 | self.assertNotEqual(server.get_finished(), None) |
| 2670 | self.assertTrue(len(server.get_finished()) > 0) |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2671 | |
Jean-Paul Calderone | d979f23 | 2014-03-30 11:58:00 -0400 | [diff] [blame] | 2672 | |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2673 | def test_get_peer_finished(self): |
| 2674 | """ |
| 2675 | :py:obj:`Connection.get_peer_finished` method returns the TLS Finished |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2676 | message received from client, or server. Finished messages are send |
| 2677 | during TLS handshake. |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2678 | """ |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2679 | server, client = self._loopback() |
| 2680 | |
| 2681 | self.assertNotEqual(server.get_peer_finished(), None) |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2682 | self.assertTrue(len(server.get_peer_finished()) > 0) |
| 2683 | |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2684 | |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2685 | def test_tls_finished_message_symmetry(self): |
| 2686 | """ |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2687 | The TLS Finished message send by server must be the TLS Finished message |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2688 | received by client. |
| 2689 | |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2690 | The TLS Finished message send by client must be the TLS Finished message |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2691 | received by server. |
| 2692 | """ |
Fedor Brunner | 416f4a1 | 2014-03-28 13:18:38 +0100 | [diff] [blame] | 2693 | server, client = self._loopback() |
| 2694 | |
Fedor Brunner | 5747b93 | 2014-03-05 14:22:34 +0100 | [diff] [blame] | 2695 | self.assertEqual(server.get_finished(), client.get_peer_finished()) |
| 2696 | self.assertEqual(client.get_finished(), server.get_peer_finished()) |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2697 | |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2698 | |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2699 | def test_get_cipher_name_before_connect(self): |
| 2700 | """ |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2701 | :py:obj:`Connection.get_cipher_name` returns :py:obj:`None` if no |
| 2702 | connection has been established. |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2703 | """ |
| 2704 | ctx = Context(TLSv1_METHOD) |
| 2705 | conn = Connection(ctx, None) |
Jean-Paul Calderone | 069e2bf | 2014-03-29 18:21:58 -0400 | [diff] [blame] | 2706 | self.assertIdentical(conn.get_cipher_name(), None) |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2707 | |
Jean-Paul Calderone | dbd7627 | 2014-03-29 18:09:40 -0400 | [diff] [blame] | 2708 | |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2709 | def test_get_cipher_name(self): |
| 2710 | """ |
Jean-Paul Calderone | 7f0ded4 | 2014-03-30 10:34:17 -0400 | [diff] [blame] | 2711 | :py:obj:`Connection.get_cipher_name` returns a :py:class:`unicode` |
| 2712 | string giving the name of the currently used cipher. |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2713 | """ |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2714 | server, client = self._loopback() |
| 2715 | server_cipher_name, client_cipher_name = \ |
| 2716 | server.get_cipher_name(), client.get_cipher_name() |
| 2717 | |
Jean-Paul Calderone | 7f0ded4 | 2014-03-30 10:34:17 -0400 | [diff] [blame] | 2718 | self.assertIsInstance(server_cipher_name, text_type) |
| 2719 | self.assertIsInstance(client_cipher_name, text_type) |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2720 | |
| 2721 | self.assertEqual(server_cipher_name, client_cipher_name) |
| 2722 | |
Jean-Paul Calderone | dbd7627 | 2014-03-29 18:09:40 -0400 | [diff] [blame] | 2723 | |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2724 | def test_get_cipher_version_before_connect(self): |
| 2725 | """ |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2726 | :py:obj:`Connection.get_cipher_version` returns :py:obj:`None` if no |
| 2727 | connection has been established. |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2728 | """ |
| 2729 | ctx = Context(TLSv1_METHOD) |
| 2730 | conn = Connection(ctx, None) |
Jean-Paul Calderone | 069e2bf | 2014-03-29 18:21:58 -0400 | [diff] [blame] | 2731 | self.assertIdentical(conn.get_cipher_version(), None) |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2732 | |
Jean-Paul Calderone | dbd7627 | 2014-03-29 18:09:40 -0400 | [diff] [blame] | 2733 | |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2734 | def test_get_cipher_version(self): |
| 2735 | """ |
Jean-Paul Calderone | 7f0ded4 | 2014-03-30 10:34:17 -0400 | [diff] [blame] | 2736 | :py:obj:`Connection.get_cipher_version` returns a :py:class:`unicode` |
| 2737 | string giving the protocol name of the currently used cipher. |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2738 | """ |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2739 | server, client = self._loopback() |
| 2740 | server_cipher_version, client_cipher_version = \ |
| 2741 | server.get_cipher_version(), client.get_cipher_version() |
| 2742 | |
Jean-Paul Calderone | 7f0ded4 | 2014-03-30 10:34:17 -0400 | [diff] [blame] | 2743 | self.assertIsInstance(server_cipher_version, text_type) |
| 2744 | self.assertIsInstance(client_cipher_version, text_type) |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2745 | |
| 2746 | self.assertEqual(server_cipher_version, client_cipher_version) |
| 2747 | |
Jean-Paul Calderone | dbd7627 | 2014-03-29 18:09:40 -0400 | [diff] [blame] | 2748 | |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2749 | def test_get_cipher_bits_before_connect(self): |
| 2750 | """ |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2751 | :py:obj:`Connection.get_cipher_bits` returns :py:obj:`None` if no |
| 2752 | connection has been established. |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2753 | """ |
| 2754 | ctx = Context(TLSv1_METHOD) |
| 2755 | conn = Connection(ctx, None) |
Jean-Paul Calderone | 069e2bf | 2014-03-29 18:21:58 -0400 | [diff] [blame] | 2756 | self.assertIdentical(conn.get_cipher_bits(), None) |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2757 | |
Jean-Paul Calderone | dbd7627 | 2014-03-29 18:09:40 -0400 | [diff] [blame] | 2758 | |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2759 | def test_get_cipher_bits(self): |
| 2760 | """ |
Jean-Paul Calderone | 5b05b48 | 2014-03-30 11:28:54 -0400 | [diff] [blame] | 2761 | :py:obj:`Connection.get_cipher_bits` returns the number of secret bits |
| 2762 | of the currently used cipher. |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2763 | """ |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2764 | server, client = self._loopback() |
| 2765 | server_cipher_bits, client_cipher_bits = \ |
| 2766 | server.get_cipher_bits(), client.get_cipher_bits() |
| 2767 | |
Fedor Brunner | 2cffdbc | 2014-03-10 10:35:23 +0100 | [diff] [blame] | 2768 | self.assertIsInstance(server_cipher_bits, int) |
| 2769 | self.assertIsInstance(client_cipher_bits, int) |
Fedor Brunner | d95014a | 2014-03-03 17:34:41 +0100 | [diff] [blame] | 2770 | |
| 2771 | self.assertEqual(server_cipher_bits, client_cipher_bits) |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 2772 | |
Jean-Paul Calderone | dbd7627 | 2014-03-29 18:09:40 -0400 | [diff] [blame] | 2773 | |
Jim Shaver | abff188 | 2015-05-27 09:15:55 -0400 | [diff] [blame] | 2774 | def test_get_protocol_version_name(self): |
| 2775 | """ |
| 2776 | :py:obj:`Connection.get_protocol_version_name()` returns a string |
| 2777 | giving the protocol version of the current connection. |
| 2778 | """ |
| 2779 | server, client = self._loopback() |
| 2780 | client_protocol_version_name = client.get_protocol_version_name() |
| 2781 | server_protocol_version_name = server.get_protocol_version_name() |
| 2782 | |
Jim Shaver | 58d2573 | 2015-05-28 11:52:32 -0400 | [diff] [blame] | 2783 | self.assertIsInstance(server_protocol_version_name, text_type) |
| 2784 | self.assertIsInstance(client_protocol_version_name, text_type) |
Jim Shaver | abff188 | 2015-05-27 09:15:55 -0400 | [diff] [blame] | 2785 | |
| 2786 | self.assertEqual(server_protocol_version_name, client_protocol_version_name) |
| 2787 | |
| 2788 | |
Richard J. Moore | 5d85fca | 2015-01-11 17:04:43 +0000 | [diff] [blame] | 2789 | def test_get_protocol_version(self): |
| 2790 | """ |
Alex Gaynor | 4330778 | 2015-09-04 09:05:45 -0400 | [diff] [blame] | 2791 | :py:obj:`Connection.get_protocol_version()` returns an integer |
Richard J. Moore | 5d85fca | 2015-01-11 17:04:43 +0000 | [diff] [blame] | 2792 | giving the protocol version of the current connection. |
| 2793 | """ |
| 2794 | server, client = self._loopback() |
Jim Shaver | 85a4dff | 2015-04-27 17:42:46 -0400 | [diff] [blame] | 2795 | client_protocol_version = client.get_protocol_version() |
| 2796 | server_protocol_version = server.get_protocol_version() |
Richard J. Moore | 5d85fca | 2015-01-11 17:04:43 +0000 | [diff] [blame] | 2797 | |
Jim Shaver | abff188 | 2015-05-27 09:15:55 -0400 | [diff] [blame] | 2798 | self.assertIsInstance(server_protocol_version, int) |
| 2799 | self.assertIsInstance(client_protocol_version, int) |
Richard J. Moore | 5d85fca | 2015-01-11 17:04:43 +0000 | [diff] [blame] | 2800 | |
| 2801 | self.assertEqual(server_protocol_version, client_protocol_version) |
| 2802 | |
Jean-Paul Calderone | dbd7627 | 2014-03-29 18:09:40 -0400 | [diff] [blame] | 2803 | |
Jim Shaver | 208438c | 2015-05-28 09:52:38 -0400 | [diff] [blame] | 2804 | |
Jean-Paul Calderone | f135e62 | 2010-07-28 19:14:16 -0400 | [diff] [blame] | 2805 | class ConnectionGetCipherListTests(TestCase): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 2806 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2807 | Tests for :py:obj:`Connection.get_cipher_list`. |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 2808 | """ |
Jean-Paul Calderone | 54a2bc0 | 2010-09-09 17:52:38 -0400 | [diff] [blame] | 2809 | def test_wrong_args(self): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 2810 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2811 | :py:obj:`Connection.get_cipher_list` raises :py:obj:`TypeError` if called with any |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 2812 | arguments. |
| 2813 | """ |
Jean-Paul Calderone | f135e62 | 2010-07-28 19:14:16 -0400 | [diff] [blame] | 2814 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2815 | self.assertRaises(TypeError, connection.get_cipher_list, None) |
| 2816 | |
| 2817 | |
| 2818 | def test_result(self): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 2819 | """ |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 2820 | :py:obj:`Connection.get_cipher_list` returns a :py:obj:`list` of |
| 2821 | :py:obj:`bytes` giving the names of the ciphers which might be used. |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 2822 | """ |
Jean-Paul Calderone | f135e62 | 2010-07-28 19:14:16 -0400 | [diff] [blame] | 2823 | connection = Connection(Context(TLSv1_METHOD), None) |
| 2824 | ciphers = connection.get_cipher_list() |
| 2825 | self.assertTrue(isinstance(ciphers, list)) |
| 2826 | for cipher in ciphers: |
| 2827 | self.assertTrue(isinstance(cipher, str)) |
| 2828 | |
| 2829 | |
| 2830 | |
Jean-Paul Calderone | 9cbbe26 | 2011-01-05 14:53:43 -0500 | [diff] [blame] | 2831 | class ConnectionSendTests(TestCase, _LoopbackMixin): |
| 2832 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2833 | Tests for :py:obj:`Connection.send` |
Jean-Paul Calderone | 9cbbe26 | 2011-01-05 14:53:43 -0500 | [diff] [blame] | 2834 | """ |
| 2835 | def test_wrong_args(self): |
| 2836 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 2837 | When called with arguments other than string argument for its first |
| 2838 | parameter or more than two arguments, :py:obj:`Connection.send` raises |
| 2839 | :py:obj:`TypeError`. |
Jean-Paul Calderone | 9cbbe26 | 2011-01-05 14:53:43 -0500 | [diff] [blame] | 2840 | """ |
| 2841 | connection = Connection(Context(TLSv1_METHOD), None) |
Jean-Paul Calderone | 77fa260 | 2011-01-05 18:23:39 -0500 | [diff] [blame] | 2842 | self.assertRaises(TypeError, connection.send) |
| 2843 | self.assertRaises(TypeError, connection.send, object()) |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 2844 | self.assertRaises(TypeError, connection.send, "foo", object(), "bar") |
Jean-Paul Calderone | 9cbbe26 | 2011-01-05 14:53:43 -0500 | [diff] [blame] | 2845 | |
| 2846 | |
| 2847 | def test_short_bytes(self): |
| 2848 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2849 | When passed a short byte string, :py:obj:`Connection.send` transmits all of it |
Jean-Paul Calderone | 9cbbe26 | 2011-01-05 14:53:43 -0500 | [diff] [blame] | 2850 | and returns the number of bytes sent. |
| 2851 | """ |
| 2852 | server, client = self._loopback() |
| 2853 | count = server.send(b('xy')) |
| 2854 | self.assertEquals(count, 2) |
| 2855 | self.assertEquals(client.recv(2), b('xy')) |
| 2856 | |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 2857 | |
| 2858 | def test_text(self): |
| 2859 | """ |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 2860 | When passed a text, :py:obj:`Connection.send` transmits all of it and |
| 2861 | returns the number of bytes sent. It also raises a DeprecationWarning. |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 2862 | """ |
| 2863 | server, client = self._loopback() |
| 2864 | with catch_warnings(record=True) as w: |
| 2865 | simplefilter("always") |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 2866 | count = server.send(b"xy".decode("ascii")) |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 2867 | self.assertEqual( |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 2868 | "{0} for buf is no longer accepted, use bytes".format( |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 2869 | WARNING_TYPE_EXPECTED |
| 2870 | ), |
| 2871 | str(w[-1].message) |
| 2872 | ) |
| 2873 | self.assertIs(w[-1].category, DeprecationWarning) |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 2874 | self.assertEquals(count, 2) |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 2875 | self.assertEquals(client.recv(2), b"xy") |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 2876 | |
Jean-Paul Calderone | 9cbbe26 | 2011-01-05 14:53:43 -0500 | [diff] [blame] | 2877 | try: |
| 2878 | memoryview |
| 2879 | except NameError: |
| 2880 | "cannot test sending memoryview without memoryview" |
| 2881 | else: |
| 2882 | def test_short_memoryview(self): |
| 2883 | """ |
| 2884 | When passed a memoryview onto a small number of bytes, |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 2885 | :py:obj:`Connection.send` transmits all of them and returns the number of |
Jean-Paul Calderone | 9cbbe26 | 2011-01-05 14:53:43 -0500 | [diff] [blame] | 2886 | bytes sent. |
| 2887 | """ |
| 2888 | server, client = self._loopback() |
| 2889 | count = server.send(memoryview(b('xy'))) |
| 2890 | self.assertEquals(count, 2) |
| 2891 | self.assertEquals(client.recv(2), b('xy')) |
| 2892 | |
| 2893 | |
Markus Unterwaditzer | 8e41d02 | 2014-04-19 12:27:11 +0200 | [diff] [blame] | 2894 | try: |
| 2895 | buffer |
| 2896 | except NameError: |
| 2897 | "cannot test sending buffer without buffer" |
| 2898 | else: |
| 2899 | def test_short_buffer(self): |
| 2900 | """ |
| 2901 | When passed a buffer containing a small number of bytes, |
| 2902 | :py:obj:`Connection.send` transmits all of them and returns the number of |
| 2903 | bytes sent. |
| 2904 | """ |
| 2905 | server, client = self._loopback() |
| 2906 | count = server.send(buffer(b('xy'))) |
| 2907 | self.assertEquals(count, 2) |
| 2908 | self.assertEquals(client.recv(2), b('xy')) |
| 2909 | |
| 2910 | |
Jean-Paul Calderone | 691e6c9 | 2011-01-21 22:04:35 -0500 | [diff] [blame] | 2911 | |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2912 | def _make_memoryview(size): |
| 2913 | """ |
| 2914 | Create a new ``memoryview`` wrapped around a ``bytearray`` of the given |
| 2915 | size. |
| 2916 | """ |
| 2917 | return memoryview(bytearray(size)) |
| 2918 | |
| 2919 | |
| 2920 | |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2921 | class ConnectionRecvIntoTests(TestCase, _LoopbackMixin): |
| 2922 | """ |
| 2923 | Tests for :py:obj:`Connection.recv_into` |
| 2924 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2925 | def _no_length_test(self, factory): |
Jean-Paul Calderone | 61559d8 | 2015-03-15 17:04:50 -0400 | [diff] [blame] | 2926 | """ |
| 2927 | Assert that when the given buffer is passed to |
| 2928 | ``Connection.recv_into``, whatever bytes are available to be received |
| 2929 | that fit into that buffer are written into that buffer. |
| 2930 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2931 | output_buffer = factory(5) |
| 2932 | |
Jean-Paul Calderone | 332a85e | 2015-03-15 17:02:40 -0400 | [diff] [blame] | 2933 | server, client = self._loopback() |
| 2934 | server.send(b('xy')) |
| 2935 | |
Jean-Paul Calderone | 320af1e | 2015-03-15 17:20:13 -0400 | [diff] [blame] | 2936 | self.assertEqual(client.recv_into(output_buffer), 2) |
| 2937 | self.assertEqual(output_buffer, bytearray(b('xy\x00\x00\x00'))) |
Jean-Paul Calderone | 332a85e | 2015-03-15 17:02:40 -0400 | [diff] [blame] | 2938 | |
| 2939 | |
Jean-Paul Calderone | d335b27 | 2015-03-15 17:26:38 -0400 | [diff] [blame] | 2940 | def test_bytearray_no_length(self): |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2941 | """ |
Jean-Paul Calderone | 61559d8 | 2015-03-15 17:04:50 -0400 | [diff] [blame] | 2942 | :py:obj:`Connection.recv_into` can be passed a ``bytearray`` instance |
| 2943 | and data in the receive buffer is written to it. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2944 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2945 | self._no_length_test(bytearray) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2946 | |
| 2947 | |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2948 | def _respects_length_test(self, factory): |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2949 | """ |
Jean-Paul Calderone | c295a2f | 2015-03-15 17:11:18 -0400 | [diff] [blame] | 2950 | Assert that when the given buffer is passed to ``Connection.recv_into`` |
| 2951 | along with a value for ``nbytes`` that is less than the size of that |
| 2952 | buffer, only ``nbytes`` bytes are written into the buffer. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2953 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2954 | output_buffer = factory(10) |
| 2955 | |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2956 | server, client = self._loopback() |
| 2957 | server.send(b('abcdefghij')) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2958 | |
Jean-Paul Calderone | 320af1e | 2015-03-15 17:20:13 -0400 | [diff] [blame] | 2959 | self.assertEqual(client.recv_into(output_buffer, 5), 5) |
| 2960 | self.assertEqual( |
Jean-Paul Calderone | c295a2f | 2015-03-15 17:11:18 -0400 | [diff] [blame] | 2961 | output_buffer, bytearray(b('abcde\x00\x00\x00\x00\x00')) |
| 2962 | ) |
| 2963 | |
| 2964 | |
Jean-Paul Calderone | d335b27 | 2015-03-15 17:26:38 -0400 | [diff] [blame] | 2965 | def test_bytearray_respects_length(self): |
Jean-Paul Calderone | c295a2f | 2015-03-15 17:11:18 -0400 | [diff] [blame] | 2966 | """ |
| 2967 | When called with a ``bytearray`` instance, |
| 2968 | :py:obj:`Connection.recv_into` respects the ``nbytes`` parameter and |
| 2969 | doesn't copy in more than that number of bytes. |
| 2970 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2971 | self._respects_length_test(bytearray) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2972 | |
| 2973 | |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2974 | def _doesnt_overfill_test(self, factory): |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2975 | """ |
Jean-Paul Calderone | 2b41ad3 | 2015-03-15 17:19:39 -0400 | [diff] [blame] | 2976 | Assert that if there are more bytes available to be read from the |
| 2977 | receive buffer than would fit into the buffer passed to |
| 2978 | :py:obj:`Connection.recv_into`, only as many as fit are written into |
| 2979 | it. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2980 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2981 | output_buffer = factory(5) |
| 2982 | |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2983 | server, client = self._loopback() |
| 2984 | server.send(b('abcdefghij')) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2985 | |
Jean-Paul Calderone | 320af1e | 2015-03-15 17:20:13 -0400 | [diff] [blame] | 2986 | self.assertEqual(client.recv_into(output_buffer), 5) |
| 2987 | self.assertEqual(output_buffer, bytearray(b('abcde'))) |
Jean-Paul Calderone | 2b41ad3 | 2015-03-15 17:19:39 -0400 | [diff] [blame] | 2988 | rest = client.recv(5) |
| 2989 | self.assertEqual(b('fghij'), rest) |
| 2990 | |
| 2991 | |
Jean-Paul Calderone | d335b27 | 2015-03-15 17:26:38 -0400 | [diff] [blame] | 2992 | def test_bytearray_doesnt_overfill(self): |
Jean-Paul Calderone | 2b41ad3 | 2015-03-15 17:19:39 -0400 | [diff] [blame] | 2993 | """ |
| 2994 | When called with a ``bytearray`` instance, |
| 2995 | :py:obj:`Connection.recv_into` respects the size of the array and |
| 2996 | doesn't write more bytes into it than will fit. |
| 2997 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 2998 | self._doesnt_overfill_test(bytearray) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 2999 | |
| 3000 | |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 3001 | def _really_doesnt_overfill_test(self, factory): |
Jean-Paul Calderone | 4bec59a | 2015-03-15 17:25:57 -0400 | [diff] [blame] | 3002 | """ |
| 3003 | Assert that if the value given by ``nbytes`` is greater than the actual |
| 3004 | size of the output buffer passed to :py:obj:`Connection.recv_into`, the |
| 3005 | behavior is as if no value was given for ``nbytes`` at all. |
| 3006 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 3007 | output_buffer = factory(5) |
| 3008 | |
Jean-Paul Calderone | 4bec59a | 2015-03-15 17:25:57 -0400 | [diff] [blame] | 3009 | server, client = self._loopback() |
| 3010 | server.send(b('abcdefghij')) |
| 3011 | |
| 3012 | self.assertEqual(client.recv_into(output_buffer, 50), 5) |
| 3013 | self.assertEqual(output_buffer, bytearray(b('abcde'))) |
| 3014 | rest = client.recv(5) |
| 3015 | self.assertEqual(b('fghij'), rest) |
| 3016 | |
| 3017 | |
Jean-Paul Calderone | d335b27 | 2015-03-15 17:26:38 -0400 | [diff] [blame] | 3018 | def test_bytearray_really_doesnt_overfill(self): |
Jean-Paul Calderone | 4bec59a | 2015-03-15 17:25:57 -0400 | [diff] [blame] | 3019 | """ |
| 3020 | When called with a ``bytearray`` instance and an ``nbytes`` value that |
| 3021 | is too large, :py:obj:`Connection.recv_into` respects the size of the |
| 3022 | array and not the ``nbytes`` value and doesn't write more bytes into |
| 3023 | the buffer than will fit. |
| 3024 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 3025 | self._doesnt_overfill_test(bytearray) |
Jean-Paul Calderone | 4bec59a | 2015-03-15 17:25:57 -0400 | [diff] [blame] | 3026 | |
| 3027 | |
Maximilian Hils | 1d95dea | 2015-08-17 19:27:20 +0200 | [diff] [blame] | 3028 | def test_peek(self): |
| 3029 | |
| 3030 | server, client = self._loopback() |
| 3031 | server.send(b('xy')) |
| 3032 | |
| 3033 | for _ in range(2): |
| 3034 | output_buffer = bytearray(5) |
| 3035 | self.assertEqual(client.recv_into(output_buffer, flags=MSG_PEEK), 2) |
| 3036 | self.assertEqual(output_buffer, bytearray(b('xy\x00\x00\x00'))) |
| 3037 | |
| 3038 | |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3039 | try: |
| 3040 | memoryview |
| 3041 | except NameError: |
| 3042 | "cannot test recv_into memoryview without memoryview" |
| 3043 | else: |
Jean-Paul Calderone | 332a85e | 2015-03-15 17:02:40 -0400 | [diff] [blame] | 3044 | def test_memoryview_no_length(self): |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3045 | """ |
Jean-Paul Calderone | 61559d8 | 2015-03-15 17:04:50 -0400 | [diff] [blame] | 3046 | :py:obj:`Connection.recv_into` can be passed a ``memoryview`` |
| 3047 | instance and data in the receive buffer is written to it. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3048 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 3049 | self._no_length_test(_make_memoryview) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3050 | |
| 3051 | |
Jean-Paul Calderone | c295a2f | 2015-03-15 17:11:18 -0400 | [diff] [blame] | 3052 | def test_memoryview_respects_length(self): |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3053 | """ |
Jean-Paul Calderone | c295a2f | 2015-03-15 17:11:18 -0400 | [diff] [blame] | 3054 | When called with a ``memoryview`` instance, |
| 3055 | :py:obj:`Connection.recv_into` respects the ``nbytes`` parameter |
| 3056 | and doesn't copy more than that number of bytes in. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3057 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 3058 | self._respects_length_test(_make_memoryview) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3059 | |
| 3060 | |
Jean-Paul Calderone | 2b41ad3 | 2015-03-15 17:19:39 -0400 | [diff] [blame] | 3061 | def test_memoryview_doesnt_overfill(self): |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3062 | """ |
Jean-Paul Calderone | 8fd8255 | 2015-03-15 17:21:07 -0400 | [diff] [blame] | 3063 | When called with a ``memoryview`` instance, |
| 3064 | :py:obj:`Connection.recv_into` respects the size of the array and |
| 3065 | doesn't write more bytes into it than will fit. |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3066 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 3067 | self._doesnt_overfill_test(_make_memoryview) |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3068 | |
| 3069 | |
Jean-Paul Calderone | 4bec59a | 2015-03-15 17:25:57 -0400 | [diff] [blame] | 3070 | def test_memoryview_really_doesnt_overfill(self): |
| 3071 | """ |
| 3072 | When called with a ``memoryview`` instance and an ``nbytes`` value |
| 3073 | that is too large, :py:obj:`Connection.recv_into` respects the size |
| 3074 | of the array and not the ``nbytes`` value and doesn't write more |
| 3075 | bytes into the buffer than will fit. |
| 3076 | """ |
Jean-Paul Calderone | 6c84010 | 2015-03-15 17:32:15 -0400 | [diff] [blame] | 3077 | self._doesnt_overfill_test(_make_memoryview) |
Jean-Paul Calderone | 4bec59a | 2015-03-15 17:25:57 -0400 | [diff] [blame] | 3078 | |
| 3079 | |
Cory Benfield | 62d1033 | 2014-06-15 10:03:41 +0100 | [diff] [blame] | 3080 | |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3081 | class ConnectionSendallTests(TestCase, _LoopbackMixin): |
| 3082 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3083 | Tests for :py:obj:`Connection.sendall`. |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3084 | """ |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3085 | def test_wrong_args(self): |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3086 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3087 | When called with arguments other than a string argument for its first |
| 3088 | parameter or with more than two arguments, :py:obj:`Connection.sendall` |
| 3089 | raises :py:obj:`TypeError`. |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3090 | """ |
| 3091 | connection = Connection(Context(TLSv1_METHOD), None) |
| 3092 | self.assertRaises(TypeError, connection.sendall) |
| 3093 | self.assertRaises(TypeError, connection.sendall, object()) |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3094 | self.assertRaises( |
| 3095 | TypeError, connection.sendall, "foo", object(), "bar") |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3096 | |
| 3097 | |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3098 | def test_short(self): |
| 3099 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3100 | :py:obj:`Connection.sendall` transmits all of the bytes in the string passed to |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3101 | it. |
| 3102 | """ |
| 3103 | server, client = self._loopback() |
Jean-Paul Calderone | a986883 | 2010-08-22 21:38:34 -0400 | [diff] [blame] | 3104 | server.sendall(b('x')) |
| 3105 | self.assertEquals(client.recv(1), b('x')) |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3106 | |
| 3107 | |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 3108 | def test_text(self): |
| 3109 | """ |
Jean-Paul Calderone | 9347742 | 2015-04-13 20:38:57 -0400 | [diff] [blame] | 3110 | :py:obj:`Connection.sendall` transmits all the content in the string |
| 3111 | passed to it raising a DeprecationWarning in case of this being a text. |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 3112 | """ |
| 3113 | server, client = self._loopback() |
| 3114 | with catch_warnings(record=True) as w: |
| 3115 | simplefilter("always") |
Jean-Paul Calderone | 8dc37a1 | 2015-03-29 08:16:52 -0400 | [diff] [blame] | 3116 | server.sendall(b"x".decode("ascii")) |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 3117 | self.assertEqual( |
Jean-Paul Calderone | 13a0e65 | 2015-03-29 07:58:51 -0400 | [diff] [blame] | 3118 | "{0} for buf is no longer accepted, use bytes".format( |
Jean-Paul Calderone | 6462b07 | 2015-03-29 07:03:11 -0400 | [diff] [blame] | 3119 | WARNING_TYPE_EXPECTED |
| 3120 | ), |
| 3121 | str(w[-1].message) |
| 3122 | ) |
| 3123 | self.assertIs(w[-1].category, DeprecationWarning) |
| 3124 | self.assertEquals(client.recv(1), b"x") |
Abraham Martin | ef06348 | 2015-03-25 14:06:24 +0000 | [diff] [blame] | 3125 | |
| 3126 | |
Jean-Paul Calderone | 691e6c9 | 2011-01-21 22:04:35 -0500 | [diff] [blame] | 3127 | try: |
| 3128 | memoryview |
| 3129 | except NameError: |
| 3130 | "cannot test sending memoryview without memoryview" |
| 3131 | else: |
| 3132 | def test_short_memoryview(self): |
| 3133 | """ |
| 3134 | When passed a memoryview onto a small number of bytes, |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3135 | :py:obj:`Connection.sendall` transmits all of them. |
Jean-Paul Calderone | 691e6c9 | 2011-01-21 22:04:35 -0500 | [diff] [blame] | 3136 | """ |
| 3137 | server, client = self._loopback() |
| 3138 | server.sendall(memoryview(b('x'))) |
| 3139 | self.assertEquals(client.recv(1), b('x')) |
| 3140 | |
| 3141 | |
Markus Unterwaditzer | 8e41d02 | 2014-04-19 12:27:11 +0200 | [diff] [blame] | 3142 | try: |
| 3143 | buffer |
| 3144 | except NameError: |
| 3145 | "cannot test sending buffers without buffers" |
| 3146 | else: |
| 3147 | def test_short_buffers(self): |
| 3148 | """ |
| 3149 | When passed a buffer containing a small number of bytes, |
| 3150 | :py:obj:`Connection.sendall` transmits all of them. |
| 3151 | """ |
| 3152 | server, client = self._loopback() |
| 3153 | server.sendall(buffer(b('x'))) |
| 3154 | self.assertEquals(client.recv(1), b('x')) |
| 3155 | |
| 3156 | |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3157 | def test_long(self): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3158 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3159 | :py:obj:`Connection.sendall` transmits all of the bytes in the string passed to |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3160 | it even if this requires multiple calls of an underlying write function. |
| 3161 | """ |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3162 | server, client = self._loopback() |
Jean-Paul Calderone | 6241c70 | 2010-09-16 19:59:24 -0400 | [diff] [blame] | 3163 | # Should be enough, underlying SSL_write should only do 16k at a time. |
| 3164 | # On Windows, after 32k of bytes the write will block (forever - because |
| 3165 | # no one is yet reading). |
Jean-Paul Calderone | 9e4c135 | 2010-09-19 09:34:43 -0400 | [diff] [blame] | 3166 | message = b('x') * (1024 * 32 - 1) + b('y') |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3167 | server.sendall(message) |
| 3168 | accum = [] |
| 3169 | received = 0 |
| 3170 | while received < len(message): |
Jean-Paul Calderone | b1f7f5f | 2010-08-22 21:40:52 -0400 | [diff] [blame] | 3171 | data = client.recv(1024) |
| 3172 | accum.append(data) |
| 3173 | received += len(data) |
Jean-Paul Calderone | f404785 | 2010-09-19 09:45:57 -0400 | [diff] [blame] | 3174 | self.assertEquals(message, b('').join(accum)) |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3175 | |
| 3176 | |
Jean-Paul Calderone | 974bdc0 | 2010-07-28 19:06:10 -0400 | [diff] [blame] | 3177 | def test_closed(self): |
| 3178 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3179 | If the underlying socket is closed, :py:obj:`Connection.sendall` propagates the |
Jean-Paul Calderone | 974bdc0 | 2010-07-28 19:06:10 -0400 | [diff] [blame] | 3180 | write error from the low level write call. |
| 3181 | """ |
| 3182 | server, client = self._loopback() |
Jean-Paul Calderone | 05d43e8 | 2010-09-24 18:01:36 -0400 | [diff] [blame] | 3183 | server.sock_shutdown(2) |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 3184 | exc = self.assertRaises(SysCallError, server.sendall, b"hello, world") |
Konstantinos Koukopoulos | 541150d | 2014-01-31 01:00:19 +0200 | [diff] [blame] | 3185 | if platform == "win32": |
| 3186 | self.assertEqual(exc.args[0], ESHUTDOWN) |
| 3187 | else: |
| 3188 | self.assertEqual(exc.args[0], EPIPE) |
Jean-Paul Calderone | 974bdc0 | 2010-07-28 19:06:10 -0400 | [diff] [blame] | 3189 | |
Jean-Paul Calderone | 7ca48b5 | 2010-07-28 18:57:21 -0400 | [diff] [blame] | 3190 | |
Jean-Paul Calderone | 1d69a72 | 2010-07-29 09:05:53 -0400 | [diff] [blame] | 3191 | |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3192 | class ConnectionRenegotiateTests(TestCase, _LoopbackMixin): |
| 3193 | """ |
| 3194 | Tests for SSL renegotiation APIs. |
| 3195 | """ |
| 3196 | def test_renegotiate_wrong_args(self): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3197 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3198 | :py:obj:`Connection.renegotiate` raises :py:obj:`TypeError` if called with any |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3199 | arguments. |
| 3200 | """ |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3201 | connection = Connection(Context(TLSv1_METHOD), None) |
| 3202 | self.assertRaises(TypeError, connection.renegotiate, None) |
| 3203 | |
| 3204 | |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 3205 | def test_total_renegotiations_wrong_args(self): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3206 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3207 | :py:obj:`Connection.total_renegotiations` raises :py:obj:`TypeError` if called with |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3208 | any arguments. |
| 3209 | """ |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 3210 | connection = Connection(Context(TLSv1_METHOD), None) |
| 3211 | self.assertRaises(TypeError, connection.total_renegotiations, None) |
| 3212 | |
| 3213 | |
| 3214 | def test_total_renegotiations(self): |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3215 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3216 | :py:obj:`Connection.total_renegotiations` returns :py:obj:`0` before any |
Jean-Paul Calderone | a8fb0c8 | 2010-09-09 18:04:56 -0400 | [diff] [blame] | 3217 | renegotiations have happened. |
| 3218 | """ |
Jean-Paul Calderone | cfecc24 | 2010-07-29 22:47:06 -0400 | [diff] [blame] | 3219 | connection = Connection(Context(TLSv1_METHOD), None) |
| 3220 | self.assertEquals(connection.total_renegotiations(), 0) |
| 3221 | |
| 3222 | |
Jean-Paul Calderone | 8ea2252 | 2010-07-29 09:39:39 -0400 | [diff] [blame] | 3223 | # def test_renegotiate(self): |
| 3224 | # """ |
| 3225 | # """ |
| 3226 | # server, client = self._loopback() |
| 3227 | |
| 3228 | # server.send("hello world") |
| 3229 | # self.assertEquals(client.recv(len("hello world")), "hello world") |
| 3230 | |
| 3231 | # self.assertEquals(server.total_renegotiations(), 0) |
| 3232 | # self.assertTrue(server.renegotiate()) |
| 3233 | |
| 3234 | # server.setblocking(False) |
| 3235 | # client.setblocking(False) |
| 3236 | # while server.renegotiate_pending(): |
| 3237 | # client.do_handshake() |
| 3238 | # server.do_handshake() |
| 3239 | |
| 3240 | # self.assertEquals(server.total_renegotiations(), 1) |
| 3241 | |
| 3242 | |
| 3243 | |
| 3244 | |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 3245 | class ErrorTests(TestCase): |
| 3246 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3247 | Unit tests for :py:obj:`OpenSSL.SSL.Error`. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 3248 | """ |
| 3249 | def test_type(self): |
| 3250 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3251 | :py:obj:`Error` is an exception type. |
Jean-Paul Calderone | 6864905 | 2009-07-17 21:14:27 -0400 | [diff] [blame] | 3252 | """ |
| 3253 | self.assertTrue(issubclass(Error, Exception)) |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 3254 | self.assertEqual(Error.__name__, 'Error') |
Rick Dean | e15b147 | 2009-07-09 15:53:42 -0500 | [diff] [blame] | 3255 | |
| 3256 | |
| 3257 | |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 3258 | class ConstantsTests(TestCase): |
| 3259 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3260 | Tests for the values of constants exposed in :py:obj:`OpenSSL.SSL`. |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 3261 | |
| 3262 | These are values defined by OpenSSL intended only to be used as flags to |
| 3263 | OpenSSL APIs. The only assertions it seems can be made about them is |
| 3264 | their values. |
| 3265 | """ |
Jean-Paul Calderone | d811b68 | 2008-12-28 22:59:15 -0500 | [diff] [blame] | 3266 | # unittest.TestCase has no skip mechanism |
| 3267 | if OP_NO_QUERY_MTU is not None: |
| 3268 | def test_op_no_query_mtu(self): |
| 3269 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3270 | The value of :py:obj:`OpenSSL.SSL.OP_NO_QUERY_MTU` is 0x1000, the value of |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 3271 | :py:const:`SSL_OP_NO_QUERY_MTU` defined by :file:`openssl/ssl.h`. |
Jean-Paul Calderone | d811b68 | 2008-12-28 22:59:15 -0500 | [diff] [blame] | 3272 | """ |
| 3273 | self.assertEqual(OP_NO_QUERY_MTU, 0x1000) |
| 3274 | else: |
| 3275 | "OP_NO_QUERY_MTU unavailable - OpenSSL version may be too old" |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 3276 | |
| 3277 | |
Jean-Paul Calderone | d811b68 | 2008-12-28 22:59:15 -0500 | [diff] [blame] | 3278 | if OP_COOKIE_EXCHANGE is not None: |
| 3279 | def test_op_cookie_exchange(self): |
| 3280 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3281 | The value of :py:obj:`OpenSSL.SSL.OP_COOKIE_EXCHANGE` is 0x2000, the value |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 3282 | of :py:const:`SSL_OP_COOKIE_EXCHANGE` defined by :file:`openssl/ssl.h`. |
Jean-Paul Calderone | d811b68 | 2008-12-28 22:59:15 -0500 | [diff] [blame] | 3283 | """ |
| 3284 | self.assertEqual(OP_COOKIE_EXCHANGE, 0x2000) |
| 3285 | else: |
| 3286 | "OP_COOKIE_EXCHANGE unavailable - OpenSSL version may be too old" |
Jean-Paul Calderone | 327d8f9 | 2008-12-28 21:55:56 -0500 | [diff] [blame] | 3287 | |
| 3288 | |
Jean-Paul Calderone | d811b68 | 2008-12-28 22:59:15 -0500 | [diff] [blame] | 3289 | if OP_NO_TICKET is not None: |
| 3290 | def test_op_no_ticket(self): |
| 3291 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3292 | The value of :py:obj:`OpenSSL.SSL.OP_NO_TICKET` is 0x4000, the value of |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 3293 | :py:const:`SSL_OP_NO_TICKET` defined by :file:`openssl/ssl.h`. |
Jean-Paul Calderone | d811b68 | 2008-12-28 22:59:15 -0500 | [diff] [blame] | 3294 | """ |
| 3295 | self.assertEqual(OP_NO_TICKET, 0x4000) |
Jean-Paul Calderone | cebd178 | 2011-09-11 10:01:31 -0400 | [diff] [blame] | 3296 | else: |
Jean-Paul Calderone | d811b68 | 2008-12-28 22:59:15 -0500 | [diff] [blame] | 3297 | "OP_NO_TICKET unavailable - OpenSSL version may be too old" |
Rick Dean | 5b7b637 | 2009-04-01 11:34:06 -0500 | [diff] [blame] | 3298 | |
| 3299 | |
Jean-Paul Calderone | c62d4c1 | 2011-09-08 18:29:32 -0400 | [diff] [blame] | 3300 | if OP_NO_COMPRESSION is not None: |
| 3301 | def test_op_no_compression(self): |
| 3302 | """ |
Jean-Paul Calderone | 64efa2c | 2011-09-11 10:00:09 -0400 | [diff] [blame] | 3303 | The value of :py:obj:`OpenSSL.SSL.OP_NO_COMPRESSION` is 0x20000, the value |
| 3304 | of :py:const:`SSL_OP_NO_COMPRESSION` defined by :file:`openssl/ssl.h`. |
Jean-Paul Calderone | c62d4c1 | 2011-09-08 18:29:32 -0400 | [diff] [blame] | 3305 | """ |
| 3306 | self.assertEqual(OP_NO_COMPRESSION, 0x20000) |
| 3307 | else: |
| 3308 | "OP_NO_COMPRESSION unavailable - OpenSSL version may be too old" |
| 3309 | |
| 3310 | |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3311 | def test_sess_cache_off(self): |
| 3312 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3313 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_OFF` 0x0, the value of |
| 3314 | :py:obj:`SSL_SESS_CACHE_OFF` defined by ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3315 | """ |
| 3316 | self.assertEqual(0x0, SESS_CACHE_OFF) |
| 3317 | |
| 3318 | |
| 3319 | def test_sess_cache_client(self): |
| 3320 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3321 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_CLIENT` 0x1, the value of |
| 3322 | :py:obj:`SSL_SESS_CACHE_CLIENT` defined by ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3323 | """ |
| 3324 | self.assertEqual(0x1, SESS_CACHE_CLIENT) |
| 3325 | |
| 3326 | |
| 3327 | def test_sess_cache_server(self): |
| 3328 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3329 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_SERVER` 0x2, the value of |
| 3330 | :py:obj:`SSL_SESS_CACHE_SERVER` defined by ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3331 | """ |
| 3332 | self.assertEqual(0x2, SESS_CACHE_SERVER) |
| 3333 | |
| 3334 | |
| 3335 | def test_sess_cache_both(self): |
| 3336 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3337 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_BOTH` 0x3, the value of |
| 3338 | :py:obj:`SSL_SESS_CACHE_BOTH` defined by ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3339 | """ |
| 3340 | self.assertEqual(0x3, SESS_CACHE_BOTH) |
| 3341 | |
| 3342 | |
| 3343 | def test_sess_cache_no_auto_clear(self): |
| 3344 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3345 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_NO_AUTO_CLEAR` 0x80, the |
| 3346 | value of :py:obj:`SSL_SESS_CACHE_NO_AUTO_CLEAR` defined by |
| 3347 | ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3348 | """ |
| 3349 | self.assertEqual(0x80, SESS_CACHE_NO_AUTO_CLEAR) |
| 3350 | |
| 3351 | |
| 3352 | def test_sess_cache_no_internal_lookup(self): |
| 3353 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3354 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_NO_INTERNAL_LOOKUP` 0x100, |
| 3355 | the value of :py:obj:`SSL_SESS_CACHE_NO_INTERNAL_LOOKUP` defined by |
| 3356 | ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3357 | """ |
| 3358 | self.assertEqual(0x100, SESS_CACHE_NO_INTERNAL_LOOKUP) |
| 3359 | |
| 3360 | |
| 3361 | def test_sess_cache_no_internal_store(self): |
| 3362 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3363 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_NO_INTERNAL_STORE` 0x200, |
| 3364 | the value of :py:obj:`SSL_SESS_CACHE_NO_INTERNAL_STORE` defined by |
| 3365 | ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3366 | """ |
| 3367 | self.assertEqual(0x200, SESS_CACHE_NO_INTERNAL_STORE) |
| 3368 | |
| 3369 | |
| 3370 | def test_sess_cache_no_internal(self): |
| 3371 | """ |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3372 | The value of :py:obj:`OpenSSL.SSL.SESS_CACHE_NO_INTERNAL` 0x300, the |
| 3373 | value of :py:obj:`SSL_SESS_CACHE_NO_INTERNAL` defined by |
| 3374 | ``openssl/ssl.h``. |
Jean-Paul Calderone | 313bf01 | 2012-02-08 13:02:49 -0500 | [diff] [blame] | 3375 | """ |
| 3376 | self.assertEqual(0x300, SESS_CACHE_NO_INTERNAL) |
| 3377 | |
| 3378 | |
Jean-Paul Calderone | eeee26a | 2009-04-27 11:24:30 -0400 | [diff] [blame] | 3379 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 3380 | class MemoryBIOTests(TestCase, _LoopbackMixin): |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3381 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3382 | Tests for :py:obj:`OpenSSL.SSL.Connection` using a memory BIO. |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3383 | """ |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3384 | def _server(self, sock): |
| 3385 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3386 | Create a new server-side SSL :py:obj:`Connection` object wrapped around |
| 3387 | :py:obj:`sock`. |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3388 | """ |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3389 | # Create the server side Connection. This is mostly setup boilerplate |
| 3390 | # - use TLSv1, use a particular certificate, etc. |
| 3391 | server_ctx = Context(TLSv1_METHOD) |
Alex Gaynor | 4330778 | 2015-09-04 09:05:45 -0400 | [diff] [blame] | 3392 | server_ctx.set_options(OP_NO_SSLv2 | OP_NO_SSLv3 | OP_SINGLE_DH_USE) |
| 3393 | server_ctx.set_verify(VERIFY_PEER | VERIFY_FAIL_IF_NO_PEER_CERT | VERIFY_CLIENT_ONCE, verify_cb) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3394 | server_store = server_ctx.get_cert_store() |
| 3395 | server_ctx.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem)) |
| 3396 | server_ctx.use_certificate(load_certificate(FILETYPE_PEM, server_cert_pem)) |
| 3397 | server_ctx.check_privatekey() |
| 3398 | server_store.add_cert(load_certificate(FILETYPE_PEM, root_cert_pem)) |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 3399 | # Here the Connection is actually created. If None is passed as the 2nd |
| 3400 | # parameter, it indicates a memory BIO should be created. |
| 3401 | server_conn = Connection(server_ctx, sock) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3402 | server_conn.set_accept_state() |
| 3403 | return server_conn |
| 3404 | |
| 3405 | |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3406 | def _client(self, sock): |
| 3407 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3408 | Create a new client-side SSL :py:obj:`Connection` object wrapped around |
| 3409 | :py:obj:`sock`. |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3410 | """ |
| 3411 | # Now create the client side Connection. Similar boilerplate to the |
| 3412 | # above. |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3413 | client_ctx = Context(TLSv1_METHOD) |
Alex Gaynor | 4330778 | 2015-09-04 09:05:45 -0400 | [diff] [blame] | 3414 | client_ctx.set_options(OP_NO_SSLv2 | OP_NO_SSLv3 | OP_SINGLE_DH_USE) |
| 3415 | client_ctx.set_verify(VERIFY_PEER | VERIFY_FAIL_IF_NO_PEER_CERT | VERIFY_CLIENT_ONCE, verify_cb) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3416 | client_store = client_ctx.get_cert_store() |
| 3417 | client_ctx.use_privatekey(load_privatekey(FILETYPE_PEM, client_key_pem)) |
| 3418 | client_ctx.use_certificate(load_certificate(FILETYPE_PEM, client_cert_pem)) |
| 3419 | client_ctx.check_privatekey() |
| 3420 | client_store.add_cert(load_certificate(FILETYPE_PEM, root_cert_pem)) |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 3421 | client_conn = Connection(client_ctx, sock) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3422 | client_conn.set_connect_state() |
| 3423 | return client_conn |
| 3424 | |
| 3425 | |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3426 | def test_memoryConnect(self): |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3427 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3428 | Two :py:obj:`Connection`s which use memory BIOs can be manually connected by |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3429 | reading from the output of each and writing those bytes to the input of |
| 3430 | the other and in this way establish a connection and exchange |
| 3431 | application-level bytes with each other. |
| 3432 | """ |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3433 | server_conn = self._server(None) |
| 3434 | client_conn = self._client(None) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3435 | |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3436 | # There should be no key or nonces yet. |
| 3437 | self.assertIdentical(server_conn.master_key(), None) |
| 3438 | self.assertIdentical(server_conn.client_random(), None) |
| 3439 | self.assertIdentical(server_conn.server_random(), None) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3440 | |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3441 | # First, the handshake needs to happen. We'll deliver bytes back and |
| 3442 | # forth between the client and server until neither of them feels like |
| 3443 | # speaking any more. |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 3444 | self.assertIdentical( |
| 3445 | self._interactInMemory(client_conn, server_conn), None) |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3446 | |
| 3447 | # Now that the handshake is done, there should be a key and nonces. |
| 3448 | self.assertNotIdentical(server_conn.master_key(), None) |
| 3449 | self.assertNotIdentical(server_conn.client_random(), None) |
| 3450 | self.assertNotIdentical(server_conn.server_random(), None) |
Jean-Paul Calderone | 6c051e6 | 2009-07-05 13:50:34 -0400 | [diff] [blame] | 3451 | self.assertEquals(server_conn.client_random(), client_conn.client_random()) |
| 3452 | self.assertEquals(server_conn.server_random(), client_conn.server_random()) |
| 3453 | self.assertNotEquals(server_conn.client_random(), server_conn.server_random()) |
| 3454 | self.assertNotEquals(client_conn.client_random(), client_conn.server_random()) |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3455 | |
| 3456 | # Here are the bytes we'll try to send. |
Jean-Paul Calderone | a441fdc | 2010-08-22 21:33:57 -0400 | [diff] [blame] | 3457 | important_message = b('One if by land, two if by sea.') |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3458 | |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3459 | server_conn.write(important_message) |
| 3460 | self.assertEquals( |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 3461 | self._interactInMemory(client_conn, server_conn), |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3462 | (client_conn, important_message)) |
| 3463 | |
| 3464 | client_conn.write(important_message[::-1]) |
| 3465 | self.assertEquals( |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 3466 | self._interactInMemory(client_conn, server_conn), |
Jean-Paul Calderone | 958299e | 2009-04-27 12:59:12 -0400 | [diff] [blame] | 3467 | (server_conn, important_message[::-1])) |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3468 | |
| 3469 | |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3470 | def test_socketConnect(self): |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 3471 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3472 | Just like :py:obj:`test_memoryConnect` but with an actual socket. |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3473 | |
| 3474 | This is primarily to rule out the memory BIO code as the source of |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3475 | any problems encountered while passing data over a :py:obj:`Connection` (if |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3476 | this test fails, there must be a problem outside the memory BIO |
| 3477 | code, as no memory BIO is involved here). Even though this isn't a |
| 3478 | memory BIO test, it's convenient to have it here. |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 3479 | """ |
Jean-Paul Calderone | 06c7cc9 | 2010-09-24 23:52:00 -0400 | [diff] [blame] | 3480 | server_conn, client_conn = self._loopback() |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 3481 | |
Jean-Paul Calderone | a441fdc | 2010-08-22 21:33:57 -0400 | [diff] [blame] | 3482 | important_message = b("Help me Obi Wan Kenobi, you're my only hope.") |
Rick Dean | b1ccd56 | 2009-07-09 23:52:39 -0500 | [diff] [blame] | 3483 | client_conn.send(important_message) |
| 3484 | msg = server_conn.recv(1024) |
| 3485 | self.assertEqual(msg, important_message) |
| 3486 | |
| 3487 | # Again in the other direction, just for fun. |
| 3488 | important_message = important_message[::-1] |
| 3489 | server_conn.send(important_message) |
| 3490 | msg = client_conn.recv(1024) |
| 3491 | self.assertEqual(msg, important_message) |
| 3492 | |
| 3493 | |
Jean-Paul Calderone | fc4ed0f | 2009-04-27 11:51:27 -0400 | [diff] [blame] | 3494 | def test_socketOverridesMemory(self): |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3495 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3496 | Test that :py:obj:`OpenSSL.SSL.bio_read` and :py:obj:`OpenSSL.SSL.bio_write` don't |
| 3497 | work on :py:obj:`OpenSSL.SSL.Connection`() that use sockets. |
Rick Dean | b71c0d2 | 2009-04-01 14:09:23 -0500 | [diff] [blame] | 3498 | """ |
| 3499 | context = Context(SSLv3_METHOD) |
| 3500 | client = socket() |
| 3501 | clientSSL = Connection(context, client) |
Alex Gaynor | 4330778 | 2015-09-04 09:05:45 -0400 | [diff] [blame] | 3502 | self.assertRaises(TypeError, clientSSL.bio_read, 100) |
| 3503 | self.assertRaises(TypeError, clientSSL.bio_write, "foo") |
Alex Gaynor | 7e8b61e | 2015-09-04 13:13:05 -0400 | [diff] [blame] | 3504 | self.assertRaises(TypeError, clientSSL.bio_shutdown) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3505 | |
| 3506 | |
| 3507 | def test_outgoingOverflow(self): |
| 3508 | """ |
| 3509 | If more bytes than can be written to the memory BIO are passed to |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3510 | :py:obj:`Connection.send` at once, the number of bytes which were written is |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3511 | returned and that many bytes from the beginning of the input can be |
| 3512 | read from the other end of the connection. |
| 3513 | """ |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3514 | server = self._server(None) |
| 3515 | client = self._client(None) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3516 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 3517 | self._interactInMemory(client, server) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3518 | |
| 3519 | size = 2 ** 15 |
Jean-Paul Calderone | 4f0467a | 2014-01-11 11:58:41 -0500 | [diff] [blame] | 3520 | sent = client.send(b"x" * size) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3521 | # Sanity check. We're trying to test what happens when the entire |
| 3522 | # input can't be sent. If the entire input was sent, this test is |
| 3523 | # meaningless. |
| 3524 | self.assertTrue(sent < size) |
| 3525 | |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 3526 | receiver, received = self._interactInMemory(client, server) |
Jean-Paul Calderone | aff0fc4 | 2009-04-27 17:13:34 -0400 | [diff] [blame] | 3527 | self.assertIdentical(receiver, server) |
| 3528 | |
| 3529 | # We can rely on all of these bytes being received at once because |
| 3530 | # _loopback passes 2 ** 16 to recv - more than 2 ** 15. |
| 3531 | self.assertEquals(len(received), sent) |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 3532 | |
| 3533 | |
| 3534 | def test_shutdown(self): |
| 3535 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3536 | :py:obj:`Connection.bio_shutdown` signals the end of the data stream from |
| 3537 | which the :py:obj:`Connection` reads. |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 3538 | """ |
Jean-Paul Calderone | ce8324d | 2009-07-16 12:22:52 -0400 | [diff] [blame] | 3539 | server = self._server(None) |
Jean-Paul Calderone | 3ad85d4 | 2009-04-30 20:24:35 -0400 | [diff] [blame] | 3540 | server.bio_shutdown() |
| 3541 | e = self.assertRaises(Error, server.recv, 1024) |
| 3542 | # We don't want WantReadError or ZeroReturnError or anything - it's a |
| 3543 | # handshake failure. |
| 3544 | self.assertEquals(e.__class__, Error) |
Jean-Paul Calderone | 0b88b6a | 2009-07-05 12:44:41 -0400 | [diff] [blame] | 3545 | |
| 3546 | |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 3547 | def test_unexpectedEndOfFile(self): |
| 3548 | """ |
| 3549 | If the connection is lost before an orderly SSL shutdown occurs, |
| 3550 | :py:obj:`OpenSSL.SSL.SysCallError` is raised with a message of |
| 3551 | "Unexpected EOF". |
| 3552 | """ |
| 3553 | server_conn, client_conn = self._loopback() |
| 3554 | client_conn.sock_shutdown(SHUT_RDWR) |
| 3555 | exc = self.assertRaises(SysCallError, server_conn.recv, 1024) |
| 3556 | self.assertEqual(exc.args, (-1, "Unexpected EOF")) |
| 3557 | |
| 3558 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3559 | def _check_client_ca_list(self, func): |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3560 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3561 | Verify the return value of the :py:obj:`get_client_ca_list` method for server and client connections. |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3562 | |
Jonathan Ballet | 78b92a2 | 2011-07-16 08:07:26 +0900 | [diff] [blame] | 3563 | :param func: A function which will be called with the server context |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3564 | before the client and server are connected to each other. This |
| 3565 | function should specify a list of CAs for the server to send to the |
| 3566 | client and return that same list. The list will be used to verify |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3567 | that :py:obj:`get_client_ca_list` returns the proper value at various |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3568 | times. |
| 3569 | """ |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3570 | server = self._server(None) |
| 3571 | client = self._client(None) |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3572 | self.assertEqual(client.get_client_ca_list(), []) |
| 3573 | self.assertEqual(server.get_client_ca_list(), []) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3574 | ctx = server.get_context() |
| 3575 | expected = func(ctx) |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3576 | self.assertEqual(client.get_client_ca_list(), []) |
| 3577 | self.assertEqual(server.get_client_ca_list(), expected) |
Jean-Paul Calderone | bf37f0f | 2010-07-31 14:56:20 -0400 | [diff] [blame] | 3578 | self._interactInMemory(client, server) |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3579 | self.assertEqual(client.get_client_ca_list(), expected) |
| 3580 | self.assertEqual(server.get_client_ca_list(), expected) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3581 | |
| 3582 | |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3583 | def test_set_client_ca_list_errors(self): |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3584 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3585 | :py:obj:`Context.set_client_ca_list` raises a :py:obj:`TypeError` if called with a |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3586 | non-list or a list that contains objects other than X509Names. |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3587 | """ |
| 3588 | ctx = Context(TLSv1_METHOD) |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3589 | self.assertRaises(TypeError, ctx.set_client_ca_list, "spam") |
| 3590 | self.assertRaises(TypeError, ctx.set_client_ca_list, ["spam"]) |
| 3591 | self.assertIdentical(ctx.set_client_ca_list([]), None) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3592 | |
| 3593 | |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3594 | def test_set_empty_ca_list(self): |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3595 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3596 | If passed an empty list, :py:obj:`Context.set_client_ca_list` configures the |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3597 | context to send no CA names to the client and, on both the server and |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3598 | client sides, :py:obj:`Connection.get_client_ca_list` returns an empty list |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3599 | after the connection is set up. |
| 3600 | """ |
| 3601 | def no_ca(ctx): |
| 3602 | ctx.set_client_ca_list([]) |
| 3603 | return [] |
| 3604 | self._check_client_ca_list(no_ca) |
| 3605 | |
| 3606 | |
| 3607 | def test_set_one_ca_list(self): |
| 3608 | """ |
| 3609 | If passed a list containing a single X509Name, |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3610 | :py:obj:`Context.set_client_ca_list` configures the context to send that CA |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3611 | name to the client and, on both the server and client sides, |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3612 | :py:obj:`Connection.get_client_ca_list` returns a list containing that |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3613 | X509Name after the connection is set up. |
| 3614 | """ |
| 3615 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3616 | cadesc = cacert.get_subject() |
| 3617 | def single_ca(ctx): |
| 3618 | ctx.set_client_ca_list([cadesc]) |
| 3619 | return [cadesc] |
| 3620 | self._check_client_ca_list(single_ca) |
| 3621 | |
| 3622 | |
| 3623 | def test_set_multiple_ca_list(self): |
| 3624 | """ |
| 3625 | If passed a list containing multiple X509Name objects, |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3626 | :py:obj:`Context.set_client_ca_list` configures the context to send those CA |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3627 | names to the client and, on both the server and client sides, |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3628 | :py:obj:`Connection.get_client_ca_list` returns a list containing those |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3629 | X509Names after the connection is set up. |
| 3630 | """ |
| 3631 | secert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3632 | clcert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3633 | |
| 3634 | sedesc = secert.get_subject() |
| 3635 | cldesc = clcert.get_subject() |
| 3636 | |
| 3637 | def multiple_ca(ctx): |
| 3638 | L = [sedesc, cldesc] |
| 3639 | ctx.set_client_ca_list(L) |
| 3640 | return L |
| 3641 | self._check_client_ca_list(multiple_ca) |
| 3642 | |
| 3643 | |
| 3644 | def test_reset_ca_list(self): |
| 3645 | """ |
| 3646 | If called multiple times, only the X509Names passed to the final call |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3647 | of :py:obj:`Context.set_client_ca_list` are used to configure the CA names |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3648 | sent to the client. |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3649 | """ |
| 3650 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3651 | secert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3652 | clcert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3653 | |
| 3654 | cadesc = cacert.get_subject() |
| 3655 | sedesc = secert.get_subject() |
| 3656 | cldesc = clcert.get_subject() |
| 3657 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3658 | def changed_ca(ctx): |
| 3659 | ctx.set_client_ca_list([sedesc, cldesc]) |
| 3660 | ctx.set_client_ca_list([cadesc]) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3661 | return [cadesc] |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3662 | self._check_client_ca_list(changed_ca) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3663 | |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3664 | |
| 3665 | def test_mutated_ca_list(self): |
| 3666 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3667 | If the list passed to :py:obj:`Context.set_client_ca_list` is mutated |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3668 | afterwards, this does not affect the list of CA names sent to the |
| 3669 | client. |
| 3670 | """ |
| 3671 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3672 | secert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3673 | |
| 3674 | cadesc = cacert.get_subject() |
| 3675 | sedesc = secert.get_subject() |
| 3676 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3677 | def mutated_ca(ctx): |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3678 | L = [cadesc] |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3679 | ctx.set_client_ca_list([cadesc]) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3680 | L.append(sedesc) |
| 3681 | return [cadesc] |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3682 | self._check_client_ca_list(mutated_ca) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3683 | |
| 3684 | |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3685 | def test_add_client_ca_errors(self): |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3686 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3687 | :py:obj:`Context.add_client_ca` raises :py:obj:`TypeError` if called with a non-X509 |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3688 | object or with a number of arguments other than one. |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3689 | """ |
| 3690 | ctx = Context(TLSv1_METHOD) |
| 3691 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3692 | self.assertRaises(TypeError, ctx.add_client_ca) |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3693 | self.assertRaises(TypeError, ctx.add_client_ca, "spam") |
Jean-Paul Calderone | 911c911 | 2009-10-24 11:12:00 -0400 | [diff] [blame] | 3694 | self.assertRaises(TypeError, ctx.add_client_ca, cacert, cacert) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3695 | |
| 3696 | |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3697 | def test_one_add_client_ca(self): |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3698 | """ |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3699 | A certificate's subject can be added as a CA to be sent to the client |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3700 | with :py:obj:`Context.add_client_ca`. |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3701 | """ |
| 3702 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3703 | cadesc = cacert.get_subject() |
| 3704 | def single_ca(ctx): |
| 3705 | ctx.add_client_ca(cacert) |
| 3706 | return [cadesc] |
| 3707 | self._check_client_ca_list(single_ca) |
| 3708 | |
| 3709 | |
| 3710 | def test_multiple_add_client_ca(self): |
| 3711 | """ |
| 3712 | Multiple CA names can be sent to the client by calling |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3713 | :py:obj:`Context.add_client_ca` with multiple X509 objects. |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3714 | """ |
| 3715 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3716 | secert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3717 | |
| 3718 | cadesc = cacert.get_subject() |
| 3719 | sedesc = secert.get_subject() |
| 3720 | |
| 3721 | def multiple_ca(ctx): |
| 3722 | ctx.add_client_ca(cacert) |
| 3723 | ctx.add_client_ca(secert) |
| 3724 | return [cadesc, sedesc] |
| 3725 | self._check_client_ca_list(multiple_ca) |
| 3726 | |
| 3727 | |
| 3728 | def test_set_and_add_client_ca(self): |
| 3729 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3730 | A call to :py:obj:`Context.set_client_ca_list` followed by a call to |
| 3731 | :py:obj:`Context.add_client_ca` results in using the CA names from the first |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3732 | call and the CA name from the second call. |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3733 | """ |
| 3734 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3735 | secert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3736 | clcert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3737 | |
| 3738 | cadesc = cacert.get_subject() |
| 3739 | sedesc = secert.get_subject() |
| 3740 | cldesc = clcert.get_subject() |
| 3741 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3742 | def mixed_set_add_ca(ctx): |
| 3743 | ctx.set_client_ca_list([cadesc, sedesc]) |
| 3744 | ctx.add_client_ca(clcert) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3745 | return [cadesc, sedesc, cldesc] |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3746 | self._check_client_ca_list(mixed_set_add_ca) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3747 | |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3748 | |
| 3749 | def test_set_after_add_client_ca(self): |
| 3750 | """ |
Jonathan Ballet | 648875f | 2011-07-16 14:14:58 +0900 | [diff] [blame] | 3751 | A call to :py:obj:`Context.set_client_ca_list` after a call to |
| 3752 | :py:obj:`Context.add_client_ca` replaces the CA name specified by the former |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3753 | call with the names specified by the latter cal. |
| 3754 | """ |
| 3755 | cacert = load_certificate(FILETYPE_PEM, root_cert_pem) |
| 3756 | secert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3757 | clcert = load_certificate(FILETYPE_PEM, server_cert_pem) |
| 3758 | |
| 3759 | cadesc = cacert.get_subject() |
| 3760 | sedesc = secert.get_subject() |
Jean-Paul Calderone | 055a917 | 2009-10-24 13:45:11 -0400 | [diff] [blame] | 3761 | |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3762 | def set_replaces_add_ca(ctx): |
| 3763 | ctx.add_client_ca(clcert) |
| 3764 | ctx.set_client_ca_list([cadesc]) |
| 3765 | ctx.add_client_ca(secert) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3766 | return [cadesc, sedesc] |
Ziga Seilnacht | f93bf10 | 2009-10-23 09:51:07 +0200 | [diff] [blame] | 3767 | self._check_client_ca_list(set_replaces_add_ca) |
Ziga Seilnacht | 679c426 | 2009-09-01 01:32:29 +0200 | [diff] [blame] | 3768 | |
Jean-Paul Calderone | 0b88b6a | 2009-07-05 12:44:41 -0400 | [diff] [blame] | 3769 | |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 3770 | |
| 3771 | class ConnectionBIOTests(TestCase): |
| 3772 | """ |
| 3773 | Tests for :py:obj:`Connection.bio_read` and :py:obj:`Connection.bio_write`. |
| 3774 | """ |
| 3775 | def test_wantReadError(self): |
| 3776 | """ |
| 3777 | :py:obj:`Connection.bio_read` raises :py:obj:`OpenSSL.SSL.WantReadError` |
| 3778 | if there are no bytes available to be read from the BIO. |
| 3779 | """ |
| 3780 | ctx = Context(TLSv1_METHOD) |
| 3781 | conn = Connection(ctx, None) |
| 3782 | self.assertRaises(WantReadError, conn.bio_read, 1024) |
| 3783 | |
| 3784 | |
Jean-Paul Calderone | bef4f4c | 2014-02-02 18:13:31 -0500 | [diff] [blame] | 3785 | def test_buffer_size(self): |
| 3786 | """ |
| 3787 | :py:obj:`Connection.bio_read` accepts an integer giving the maximum |
| 3788 | number of bytes to read and return. |
| 3789 | """ |
| 3790 | ctx = Context(TLSv1_METHOD) |
| 3791 | conn = Connection(ctx, None) |
| 3792 | conn.set_connect_state() |
| 3793 | try: |
| 3794 | conn.do_handshake() |
| 3795 | except WantReadError: |
| 3796 | pass |
| 3797 | data = conn.bio_read(2) |
| 3798 | self.assertEqual(2, len(data)) |
| 3799 | |
| 3800 | |
| 3801 | if not PY3: |
| 3802 | def test_buffer_size_long(self): |
| 3803 | """ |
| 3804 | On Python 2 :py:obj:`Connection.bio_read` accepts values of type |
| 3805 | :py:obj:`long` as well as :py:obj:`int`. |
| 3806 | """ |
| 3807 | ctx = Context(TLSv1_METHOD) |
| 3808 | conn = Connection(ctx, None) |
| 3809 | conn.set_connect_state() |
| 3810 | try: |
| 3811 | conn.do_handshake() |
| 3812 | except WantReadError: |
| 3813 | pass |
| 3814 | data = conn.bio_read(long(2)) |
| 3815 | self.assertEqual(2, len(data)) |
| 3816 | |
| 3817 | |
| 3818 | |
Jean-Paul Calderone | d899af0 | 2013-03-19 22:10:37 -0700 | [diff] [blame] | 3819 | |
Jean-Paul Calderone | 31e85a8 | 2011-03-21 19:13:35 -0400 | [diff] [blame] | 3820 | class InfoConstantTests(TestCase): |
| 3821 | """ |
| 3822 | Tests for assorted constants exposed for use in info callbacks. |
| 3823 | """ |
| 3824 | def test_integers(self): |
| 3825 | """ |
| 3826 | All of the info constants are integers. |
| 3827 | |
| 3828 | This is a very weak test. It would be nice to have one that actually |
| 3829 | verifies that as certain info events happen, the value passed to the |
| 3830 | info callback matches up with the constant exposed by OpenSSL.SSL. |
| 3831 | """ |
| 3832 | for const in [ |
| 3833 | SSL_ST_CONNECT, SSL_ST_ACCEPT, SSL_ST_MASK, SSL_ST_INIT, |
| 3834 | SSL_ST_BEFORE, SSL_ST_OK, SSL_ST_RENEGOTIATE, |
| 3835 | SSL_CB_LOOP, SSL_CB_EXIT, SSL_CB_READ, SSL_CB_WRITE, SSL_CB_ALERT, |
| 3836 | SSL_CB_READ_ALERT, SSL_CB_WRITE_ALERT, SSL_CB_ACCEPT_LOOP, |
| 3837 | SSL_CB_ACCEPT_EXIT, SSL_CB_CONNECT_LOOP, SSL_CB_CONNECT_EXIT, |
| 3838 | SSL_CB_HANDSHAKE_START, SSL_CB_HANDSHAKE_DONE]: |
| 3839 | |
| 3840 | self.assertTrue(isinstance(const, int)) |
| 3841 | |
Ziga Seilnacht | 44611bf | 2009-08-31 20:49:30 +0200 | [diff] [blame] | 3842 | |
Jean-Paul Calderone | 0b88b6a | 2009-07-05 12:44:41 -0400 | [diff] [blame] | 3843 | if __name__ == '__main__': |
| 3844 | main() |