|
| 1 | +# |
| 2 | +# sambacc: a samba container configuration tool |
| 3 | +# Copyright (C) 2022 John Mulligan |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 17 | +# |
| 18 | + |
| 19 | +import os |
| 20 | + |
| 21 | +import pytest |
| 22 | +import xattr # type: ignore |
| 23 | + |
| 24 | +import sambacc.permissions |
| 25 | + |
| 26 | + |
| 27 | +@pytest.mark.parametrize( |
| 28 | + "cls", |
| 29 | + [ |
| 30 | + sambacc.permissions.NoopPermsHandler, |
| 31 | + sambacc.permissions.InitPosixPermsHandler, |
| 32 | + sambacc.permissions.AlwaysPosixPermsHandler, |
| 33 | + ], |
| 34 | +) |
| 35 | +def test_permissions_path(cls): |
| 36 | + assert cls("/foo", "user.foo", options={}).path() == "/foo" |
| 37 | + |
| 38 | + |
| 39 | +def test_noop_handler(): |
| 40 | + nh = sambacc.permissions.NoopPermsHandler("/foo", "user.foo", options={}) |
| 41 | + assert nh.path() == "/foo" |
| 42 | + assert not nh.has_status() |
| 43 | + assert nh.status_ok() |
| 44 | + assert nh.update() is None |
| 45 | + |
| 46 | + |
| 47 | +@pytest.fixture(scope="function") |
| 48 | +def tmp_path_xattrs_ok(tmp_path_factory): |
| 49 | + tmpp = tmp_path_factory.mktemp("needs_xattrs") |
| 50 | + try: |
| 51 | + xattr.set(str(tmpp), "user.deleteme", "1") |
| 52 | + xattr.remove(str(tmpp), "user.deleteme") |
| 53 | + except OSError: |
| 54 | + raise pytest.skip( |
| 55 | + "temp dir does not support xattrs" |
| 56 | + " (try changing basetmp to a file system that supports xattrs)" |
| 57 | + ) |
| 58 | + return tmpp |
| 59 | + |
| 60 | + |
| 61 | +def test_init_handler(tmp_path_xattrs_ok): |
| 62 | + path = tmp_path_xattrs_ok / "foo" |
| 63 | + os.mkdir(path) |
| 64 | + ih = sambacc.permissions.InitPosixPermsHandler( |
| 65 | + str(path), "user.marker", options={} |
| 66 | + ) |
| 67 | + assert ih.path().endswith("/foo") |
| 68 | + assert not ih.has_status() |
| 69 | + assert not ih.status_ok() |
| 70 | + |
| 71 | + ih.update() |
| 72 | + assert ih.has_status() |
| 73 | + assert ih.status_ok() |
| 74 | + |
| 75 | + os.chmod(path, 0o755) |
| 76 | + ih.update() |
| 77 | + assert (os.stat(path).st_mode & 0o777) == 0o755 |
| 78 | + |
| 79 | + |
| 80 | +def test_always_handler(tmp_path_xattrs_ok): |
| 81 | + path = tmp_path_xattrs_ok / "foo" |
| 82 | + os.mkdir(path) |
| 83 | + ih = sambacc.permissions.AlwaysPosixPermsHandler( |
| 84 | + str(path), "user.marker", options={} |
| 85 | + ) |
| 86 | + assert ih.path().endswith("/foo") |
| 87 | + assert not ih.has_status() |
| 88 | + assert not ih.status_ok() |
| 89 | + |
| 90 | + ih.update() |
| 91 | + assert ih.has_status() |
| 92 | + assert ih.status_ok() |
| 93 | + |
| 94 | + os.chmod(path, 0o755) |
| 95 | + assert (os.stat(path).st_mode & 0o777) == 0o755 |
| 96 | + ih.update() |
| 97 | + assert (os.stat(path).st_mode & 0o777) == 0o777 |
0 commit comments