|
1 | 1 | # SPDX-License-Identifier: Apache-2.0 |
2 | 2 | """Tests for sandlock._landlock.""" |
3 | 3 |
|
| 4 | +from unittest import mock |
| 5 | + |
4 | 6 | import pytest |
5 | 7 |
|
6 | 8 | from sandlock._landlock import ( |
|
13 | 15 | _FULL_ACCESS, |
14 | 16 | _READ_ACCESS, |
15 | 17 | _WRITE_ACCESS, |
| 18 | + confine, |
16 | 19 | landlock_abi_version, |
17 | 20 | ) |
| 21 | +from sandlock.exceptions import ConfinementError |
18 | 22 |
|
19 | 23 |
|
20 | 24 | class TestAccessFlags: |
@@ -49,6 +53,30 @@ def test_scope_flags_no_overlap(self): |
49 | 53 | assert LANDLOCK_SCOPE_ABSTRACT_UNIX_SOCKET & LANDLOCK_SCOPE_SIGNAL == 0 |
50 | 54 |
|
51 | 55 |
|
| 56 | +class TestAbiVersionGuards: |
| 57 | + """confine() must error when features need a newer ABI than available.""" |
| 58 | + |
| 59 | + @mock.patch("sandlock._landlock.landlock_abi_version", return_value=3) |
| 60 | + def test_bind_ports_requires_abi4(self, _mock_abi): |
| 61 | + with pytest.raises(ConfinementError, match="ABI >= 4"): |
| 62 | + confine(readable=["/tmp"], bind_ports=[80]) |
| 63 | + |
| 64 | + @mock.patch("sandlock._landlock.landlock_abi_version", return_value=3) |
| 65 | + def test_connect_ports_requires_abi4(self, _mock_abi): |
| 66 | + with pytest.raises(ConfinementError, match="ABI >= 4"): |
| 67 | + confine(readable=["/tmp"], connect_ports=[443]) |
| 68 | + |
| 69 | + @mock.patch("sandlock._landlock.landlock_abi_version", return_value=5) |
| 70 | + def test_isolate_ipc_requires_abi6(self, _mock_abi): |
| 71 | + with pytest.raises(ConfinementError, match="ABI >= 6"): |
| 72 | + confine(readable=["/tmp"], isolate_ipc=True) |
| 73 | + |
| 74 | + @mock.patch("sandlock._landlock.landlock_abi_version", return_value=5) |
| 75 | + def test_isolate_signals_requires_abi6(self, _mock_abi): |
| 76 | + with pytest.raises(ConfinementError, match="ABI >= 6"): |
| 77 | + confine(readable=["/tmp"], isolate_signals=True) |
| 78 | + |
| 79 | + |
52 | 80 | class TestLandlockAbiVersion: |
53 | 81 | def test_returns_int(self): |
54 | 82 | ver = landlock_abi_version() |
|
0 commit comments