|
29 | 29 | socket,
|
30 | 30 | )
|
31 | 31 | from sys import getfilesystemencoding, platform
|
| 32 | +from typing import Union |
32 | 33 | from warnings import simplefilter
|
33 | 34 | from weakref import ref
|
34 | 35 |
|
@@ -621,17 +622,6 @@ def test_type(self):
|
621 | 622 | """
|
622 | 623 | assert is_consistent_type(Context, "Context", TLSv1_METHOD)
|
623 | 624 |
|
624 |
| - def test_use_privatekey(self): |
625 |
| - """ |
626 |
| - `Context.use_privatekey` takes an `OpenSSL.crypto.PKey` instance. |
627 |
| - """ |
628 |
| - key = PKey() |
629 |
| - key.generate_key(TYPE_RSA, 1024) |
630 |
| - ctx = Context(SSLv23_METHOD) |
631 |
| - ctx.use_privatekey(key) |
632 |
| - with pytest.raises(TypeError): |
633 |
| - ctx.use_privatekey("") |
634 |
| - |
635 | 625 | def test_use_privatekey_file_missing(self, tmpfile):
|
636 | 626 | """
|
637 | 627 | `Context.use_privatekey_file` raises `OpenSSL.SSL.Error` when passed
|
@@ -685,37 +675,6 @@ def test_use_privatekey_file_unicode(self, tmpfile):
|
685 | 675 | FILETYPE_PEM,
|
686 | 676 | )
|
687 | 677 |
|
688 |
| - def test_use_certificate_wrong_args(self): |
689 |
| - """ |
690 |
| - `Context.use_certificate_wrong_args` raises `TypeError` when not passed |
691 |
| - exactly one `OpenSSL.crypto.X509` instance as an argument. |
692 |
| - """ |
693 |
| - ctx = Context(SSLv23_METHOD) |
694 |
| - with pytest.raises(TypeError): |
695 |
| - ctx.use_certificate("hello, world") |
696 |
| - |
697 |
| - def test_use_certificate_uninitialized(self): |
698 |
| - """ |
699 |
| - `Context.use_certificate` raises `OpenSSL.SSL.Error` when passed a |
700 |
| - `OpenSSL.crypto.X509` instance which has not been initialized |
701 |
| - (ie, which does not actually have any certificate data). |
702 |
| - """ |
703 |
| - ctx = Context(SSLv23_METHOD) |
704 |
| - with pytest.raises(Error): |
705 |
| - ctx.use_certificate(X509()) |
706 |
| - |
707 |
| - def test_use_certificate(self): |
708 |
| - """ |
709 |
| - `Context.use_certificate` sets the certificate which will be |
710 |
| - used to identify connections created using the context. |
711 |
| - """ |
712 |
| - # TODO |
713 |
| - # Hard to assert anything. But we could set a privatekey then ask |
714 |
| - # OpenSSL if the cert and key agree using check_privatekey. Then as |
715 |
| - # long as check_privatekey works right we're good... |
716 |
| - ctx = Context(SSLv23_METHOD) |
717 |
| - ctx.use_certificate(load_certificate(FILETYPE_PEM, root_cert_pem)) |
718 |
| - |
719 | 678 | def test_use_certificate_file_wrong_args(self):
|
720 | 679 | """
|
721 | 680 | `Context.use_certificate_file` raises `TypeError` if the first
|
@@ -2180,6 +2139,76 @@ def test_construction(self):
|
2180 | 2139 | assert isinstance(new_session, Session)
|
2181 | 2140 |
|
2182 | 2141 |
|
| 2142 | +@pytest.fixture(params=["context", "connection"]) |
| 2143 | +def ctx_or_conn(request) -> Union[Context, Connection]: |
| 2144 | + ctx = Context(SSLv23_METHOD) |
| 2145 | + if request.param == "context": |
| 2146 | + return ctx |
| 2147 | + else: |
| 2148 | + return Connection(ctx, None) |
| 2149 | + |
| 2150 | + |
| 2151 | +class TestContextConnection: |
| 2152 | + """ |
| 2153 | + Unit test for methods that are exposed both by Connection and Context |
| 2154 | + objects. |
| 2155 | + """ |
| 2156 | + |
| 2157 | + def test_use_privatekey(self, ctx_or_conn): |
| 2158 | + """ |
| 2159 | + `use_privatekey` takes an `OpenSSL.crypto.PKey` instance. |
| 2160 | + """ |
| 2161 | + key = PKey() |
| 2162 | + key.generate_key(TYPE_RSA, 1024) |
| 2163 | + |
| 2164 | + ctx_or_conn.use_privatekey(key) |
| 2165 | + with pytest.raises(TypeError): |
| 2166 | + ctx_or_conn.use_privatekey("") |
| 2167 | + |
| 2168 | + def test_use_privatekey_wrong_key(self, ctx_or_conn): |
| 2169 | + """ |
| 2170 | + `use_privatekey` raises `OpenSSL.SSL.Error` when passed a |
| 2171 | + `OpenSSL.crypto.PKey` instance which has not been initialized. |
| 2172 | + """ |
| 2173 | + key = PKey() |
| 2174 | + key.generate_key(TYPE_RSA, 1024) |
| 2175 | + ctx_or_conn.use_certificate( |
| 2176 | + load_certificate(FILETYPE_PEM, root_cert_pem) |
| 2177 | + ) |
| 2178 | + with pytest.raises(Error): |
| 2179 | + ctx_or_conn.use_privatekey(key) |
| 2180 | + |
| 2181 | + def test_use_certificate(self, ctx_or_conn): |
| 2182 | + """ |
| 2183 | + `use_certificate` sets the certificate which will be |
| 2184 | + used to identify connections created using the context. |
| 2185 | + """ |
| 2186 | + # TODO |
| 2187 | + # Hard to assert anything. But we could set a privatekey then ask |
| 2188 | + # OpenSSL if the cert and key agree using check_privatekey. Then as |
| 2189 | + # long as check_privatekey works right we're good... |
| 2190 | + ctx_or_conn.use_certificate( |
| 2191 | + load_certificate(FILETYPE_PEM, root_cert_pem) |
| 2192 | + ) |
| 2193 | + |
| 2194 | + def test_use_certificate_wrong_args(self, ctx_or_conn): |
| 2195 | + """ |
| 2196 | + `use_certificate_wrong_args` raises `TypeError` when not passed |
| 2197 | + exactly one `OpenSSL.crypto.X509` instance as an argument. |
| 2198 | + """ |
| 2199 | + with pytest.raises(TypeError): |
| 2200 | + ctx_or_conn.use_certificate("hello, world") |
| 2201 | + |
| 2202 | + def test_use_certificate_uninitialized(self, ctx_or_conn): |
| 2203 | + """ |
| 2204 | + `use_certificate` raises `OpenSSL.SSL.Error` when passed a |
| 2205 | + `OpenSSL.crypto.X509` instance which has not been initialized |
| 2206 | + (ie, which does not actually have any certificate data). |
| 2207 | + """ |
| 2208 | + with pytest.raises(Error): |
| 2209 | + ctx_or_conn.use_certificate(X509()) |
| 2210 | + |
| 2211 | + |
2183 | 2212 | class TestConnection:
|
2184 | 2213 | """
|
2185 | 2214 | Unit tests for `OpenSSL.SSL.Connection`.
|
|
0 commit comments