File size: 2,667 Bytes
065fee7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import hashlib
from unittest import mock

import pytest

import aiohttp
from aiohttp.client_reqrep import _merge_ssl_params

ssl = pytest.importorskip("ssl")


def test_fingerprint_sha256() -> None:
    sha256 = hashlib.sha256(b"12345678" * 64).digest()
    fp = aiohttp.Fingerprint(sha256)
    assert fp.fingerprint == sha256


def test_fingerprint_sha1() -> None:
    sha1 = hashlib.sha1(b"12345678" * 64).digest()
    with pytest.raises(ValueError):
        aiohttp.Fingerprint(sha1)


def test_fingerprint_md5() -> None:
    md5 = hashlib.md5(b"12345678" * 64).digest()
    with pytest.raises(ValueError):
        aiohttp.Fingerprint(md5)


def test_fingerprint_check_no_ssl() -> None:
    sha256 = hashlib.sha256(b"12345678" * 64).digest()
    fp = aiohttp.Fingerprint(sha256)
    transport = mock.Mock()
    transport.get_extra_info.return_value = None
    assert fp.check(transport) is None


def test__merge_ssl_params_verify_ssl() -> None:
    with pytest.warns(DeprecationWarning):
        assert _merge_ssl_params(True, False, None, None) is False


def test__merge_ssl_params_verify_ssl_conflict() -> None:
    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    with pytest.warns(DeprecationWarning):
        with pytest.raises(ValueError):
            _merge_ssl_params(ctx, False, None, None)


def test__merge_ssl_params_ssl_context() -> None:
    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    with pytest.warns(DeprecationWarning):
        assert _merge_ssl_params(True, None, ctx, None) is ctx


def test__merge_ssl_params_ssl_context_conflict() -> None:
    ctx1 = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    ctx2 = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    with pytest.warns(DeprecationWarning):
        with pytest.raises(ValueError):
            _merge_ssl_params(ctx1, None, ctx2, None)


def test__merge_ssl_params_fingerprint() -> None:
    digest = hashlib.sha256(b"123").digest()
    with pytest.warns(DeprecationWarning):
        ret = _merge_ssl_params(True, None, None, digest)
        assert ret.fingerprint == digest


def test__merge_ssl_params_fingerprint_conflict() -> None:
    fingerprint = aiohttp.Fingerprint(hashlib.sha256(b"123").digest())
    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    with pytest.warns(DeprecationWarning):
        with pytest.raises(ValueError):
            _merge_ssl_params(ctx, None, None, fingerprint)


def test__merge_ssl_params_ssl() -> None:
    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    assert ctx is _merge_ssl_params(ctx, None, None, None)


def test__merge_ssl_params_invlid() -> None:
    with pytest.raises(TypeError):
        _merge_ssl_params(object(), None, None, None)