| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import errno |
| 4 |
import pytest |
| 5 |
from pytest_mock import MockerFixture |
| 6 |
|
| 7 |
from ansible._internal._encryption._crypt import _CryptLib, CryptFacade, _FAILURE_TOKENS |
| 8 |
|
| 9 |
|
| 10 |
class TestCryptFacade: |
| 11 |
|
| 12 |
def test_unsupported_platform(self, mocker: MockerFixture) -> None: |
| 13 |
"""Test that unsupported platforms are skipped.""" |
| 14 |
mock_libs = ( |
| 15 |
_CryptLib('foo', include_platforms=frozenset({'fake_platform'})), |
| 16 |
) |
| 17 |
mocker.patch('ansible._internal._encryption._crypt._CRYPT_LIBS', mock_libs) |
| 18 |
|
| 19 |
with pytest.raises(ImportError, match=r'Cannot find crypt implementation'): |
| 20 |
CryptFacade() |
| 21 |
|
| 22 |
def test_libc_fallback(self, mocker: MockerFixture) -> None: |
| 23 |
"""Test that a library name of None will load the libc library.""" |
| 24 |
mock_libs = ( |
| 25 |
_CryptLib(None), |
| 26 |
) |
| 27 |
mocker.patch('ansible._internal._encryption._crypt._CRYPT_LIBS', mock_libs) |
| 28 |
load_lib_mock = mocker.patch('ctypes.cdll.LoadLibrary') |
| 29 |
|
| 30 |
crypt_facade = CryptFacade() |
| 31 |
|
| 32 |
load_lib_mock.assert_called_once_with(None) |
| 33 |
assert crypt_facade._crypt_name is None |
| 34 |
|
| 35 |
def test_library_with_no_crypt_methods(self, mocker: MockerFixture) -> None: |
| 36 |
"""Test that a library without crypt() and crypt_r() is skipped.""" |
| 37 |
mock_libs = ( |
| 38 |
_CryptLib(None), |
| 39 |
) |
| 40 |
|
| 41 |
class MockCDLL: |
| 42 |
pass |
| 43 |
|
| 44 |
mocker.patch('ansible._internal._encryption._crypt._CRYPT_LIBS', mock_libs) |
| 45 |
mocker.patch('ctypes.cdll.LoadLibrary', return_value=MockCDLL()) |
| 46 |
|
| 47 |
with pytest.raises(ImportError, match=r'Cannot find crypt implementation'): |
| 48 |
CryptFacade() |
| 49 |
|
| 50 |
def test_library_with_no_crypt_r_or_crypt_gensalt_rn(self, mocker: MockerFixture) -> None: |
| 51 |
"""Test that a library without crypt_r() or crypt_gensalt_rn() is prepped correctly.""" |
| 52 |
mock_libs = ( |
| 53 |
_CryptLib(None), |
| 54 |
) |
| 55 |
|
| 56 |
class MockCDLL: |
| 57 |
|
| 58 |
class MockCrypt: |
| 59 |
def __init__(self): |
| 60 |
self.argtypes = None |
| 61 |
self.restype = None |
| 62 |
|
| 63 |
def __init__(self): |
| 64 |
self.crypt = self.MockCrypt() |
| 65 |
self.crypt_gensalt = self.MockCrypt() |
| 66 |
|
| 67 |
mocker.patch('ansible._internal._encryption._crypt._CRYPT_LIBS', mock_libs) |
| 68 |
mocker.patch('ctypes.cdll.LoadLibrary', return_value=MockCDLL()) |
| 69 |
|
| 70 |
crypt_facade = CryptFacade() |
| 71 |
|
| 72 |
assert crypt_facade._crypt_impl is not None |
| 73 |
assert crypt_facade._crypt_impl.argtypes is not None |
| 74 |
assert crypt_facade._crypt_impl.restype is not None |
| 75 |
assert crypt_facade._use_crypt_r is False |
| 76 |
|
| 77 |
assert crypt_facade._crypt_gensalt_impl is not None |
| 78 |
assert crypt_facade._crypt_gensalt_impl.argtypes is not None |
| 79 |
assert crypt_facade._crypt_gensalt_impl.restype is not None |
| 80 |
assert crypt_facade._use_crypt_gensalt_rn is False |
| 81 |
assert crypt_facade.has_crypt_gensalt |
| 82 |
|
| 83 |
def test_crypt_fail_errno(self, mocker: MockerFixture) -> None: |
| 84 |
"""Test crypt() setting failure errno raises OSError.""" |
| 85 |
mocker.patch('ctypes.get_errno', return_value=errno.EBADF) |
| 86 |
crypt_facade = CryptFacade() |
| 87 |
|
| 88 |
with pytest.raises(OSError, match=r'crypt failed:'): |
| 89 |
crypt_facade.crypt(b"test", b"123") |
| 90 |
|
| 91 |
def test_crypt_result_none(self, mocker: MockerFixture) -> None: |
| 92 |
"""Test crypt() implementation returning None raises ValueError.""" |
| 93 |
crypt_facade = CryptFacade() |
| 94 |
mocker.patch.object(crypt_facade, '_crypt_impl', return_value=None) |
| 95 |
|
| 96 |
with pytest.raises(ValueError, match=r'crypt failed: invalid salt or unsupported algorithm'): |
| 97 |
crypt_facade.crypt(b"test", b"123") |
| 98 |
|
| 99 |
@pytest.mark.parametrize('failure_token', sorted(list(_FAILURE_TOKENS))) |
| 100 |
def test_crypt_result_failure(self, mocker: MockerFixture, failure_token: bytes) -> None: |
| 101 |
"""Test crypt() implementation returning failure token raises ValueError.""" |
| 102 |
crypt_facade = CryptFacade() |
| 103 |
mocker.patch.object(crypt_facade, '_crypt_impl', return_value=failure_token) |
| 104 |
|
| 105 |
with pytest.raises(ValueError, match=r'crypt failed: invalid salt or unsupported algorithm'): |
| 106 |
crypt_facade.crypt(b"test", b"123") |
| 107 |
|
| 108 |
def test_crypt_gensalt_called_with_no_impl(self, mocker: MockerFixture) -> None: |
| 109 |
"""Calling crypt_gensalt() without impl should raise NotImplementedError.""" |
| 110 |
crypt_facade = CryptFacade() |
| 111 |
mock_prop = mocker.patch('ansible._internal._encryption._crypt.CryptFacade.has_crypt_gensalt', new_callable=mocker.PropertyMock) |
| 112 |
mock_prop.return_value = False |
| 113 |
|
| 114 |
with pytest.raises(NotImplementedError, match=r'crypt_gensalt not available \(requires libxcrypt\)'): |
| 115 |
crypt_facade.crypt_gensalt(b"", 1, b"") |
| 116 |
|
| 117 |
def test_crypt_gensalt(self, mocker: MockerFixture) -> None: |
| 118 |
"""Test the NOT _use_crypt_gensalt_rn code path of crypt_gensalt().""" |
| 119 |
crypt_facade = CryptFacade() |
| 120 |
crypt_facade._use_crypt_gensalt_rn = False |
| 121 |
mock_impl = mocker.patch.object(crypt_facade, '_crypt_gensalt_impl', return_value='') |
| 122 |
|
| 123 |
crypt_facade.crypt_gensalt(b'', 1, b'') |
| 124 |
mock_impl.assert_called_once_with(b'', 1, b'', 0) |
| 125 |
|
| 126 |
def test_crypt_gensalt_fail_errno(self, mocker: MockerFixture) -> None: |
| 127 |
"""Test crypt_gensalt() setting failure errno raises OSError.""" |
| 128 |
mocker.patch('ctypes.get_errno', return_value=errno.EBADF) |
| 129 |
crypt_facade = CryptFacade() |
| 130 |
|
| 131 |
with pytest.raises(OSError, match=r'crypt_gensalt failed:'): |
| 132 |
crypt_facade.crypt_gensalt(b'', 1, b'') |
| 133 |
|
| 134 |
def test_crypt_gensalt_result_none(self, mocker: MockerFixture) -> None: |
| 135 |
"""Test crypt_gensalt() implementation returning None raises ValueError.""" |
| 136 |
crypt_facade = CryptFacade() |
| 137 |
mocker.patch.object(crypt_facade, '_crypt_gensalt_impl', return_value=None) |
| 138 |
|
| 139 |
with pytest.raises(ValueError, match=r'crypt_gensalt failed: unable to generate salt'): |
| 140 |
crypt_facade.crypt_gensalt(b'', 1, b'') |
| 141 |
|
| 142 |
def test_crypt_gensalt_result_failure(self, mocker: MockerFixture) -> None: |
| 143 |
"""Test crypt_gensalt() implementation returning failure token raises ValueError.""" |
| 144 |
crypt_facade = CryptFacade() |
| 145 |
# Skip the _rn version as it modifies impl return value |
| 146 |
crypt_facade._use_crypt_gensalt_rn = False |
| 147 |
mocker.patch.object(crypt_facade, '_crypt_gensalt_impl', return_value=list(_FAILURE_TOKENS)[0]) |
| 148 |
|
| 149 |
with pytest.raises(ValueError, match=r'crypt_gensalt failed: invalid prefix or unsupported algorithm'): |
| 150 |
crypt_facade.crypt_gensalt(b'', 1, b'') |