|
| 1 | +"""This module contains tests for libsodium wrapper.""" |
| 2 | + |
| 3 | +import ctypes.util |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from paseto.crypto import libsodium_wrapper |
| 9 | + |
| 10 | + |
| 11 | +def test_key_size() -> None: |
| 12 | + """Test exception when nonce size is incorrect.""" |
| 13 | + with pytest.raises(ValueError, match="key"): |
| 14 | + libsodium_wrapper.crypto_stream_xchacha20_xor(b"", b"0" * 24, b"") |
| 15 | + |
| 16 | + |
| 17 | +def test_nonce_size() -> None: |
| 18 | + """Test exception when key size is incorrect.""" |
| 19 | + with pytest.raises(ValueError, match="nonce"): |
| 20 | + libsodium_wrapper.crypto_stream_xchacha20_xor(b"", b"", b"0" * 32) |
| 21 | + |
| 22 | + |
| 23 | +@patch.object(ctypes.util, "find_library") |
| 24 | +def test_no_libsodium(mock: MagicMock) -> None: |
| 25 | + """Test that exception is raised when libsodium can is not found.""" |
| 26 | + mock.return_value = None |
| 27 | + with pytest.raises(ValueError): |
| 28 | + import importlib |
| 29 | + |
| 30 | + import paseto.crypto.libsodium_wrapper |
| 31 | + |
| 32 | + importlib.reload(paseto.crypto.libsodium_wrapper) |
| 33 | + |
| 34 | + |
| 35 | +@patch.object(libsodium_wrapper._sodium, "crypto_stream_xchacha20_xor") |
| 36 | +def test_non_zero_exit_code(mock: MagicMock) -> None: |
| 37 | + mock.return_value = 1 |
| 38 | + with pytest.raises(ValueError): |
| 39 | + libsodium_wrapper.crypto_stream_xchacha20_xor(b"", b"0" * 24, b"0" * 32) |
0 commit comments