okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
bcf88b738
main
1"""POSIX owner-only checks: our own folders with drifted modes are repaired; another account's folders get a message with the exact fix.""" 2 3import os 4import stat 5import tempfile 6import unittest 7from pathlib import Path 8from unittest import mock 9 10from strata_control import posix_owner 11 12posix_only = unittest .skipUnless (os .name == "posix" ,"POSIX modes" ) 13 14 15@posix_only 16class Repair (unittest .TestCase ): 17def setUp (self ): 18self .tmp = tempfile .TemporaryDirectory () 19self .root = Path (self .tmp .name ) 20self .addCleanup (self .tmp .cleanup ) 21 22def test_our_folder_with_open_modes_is_put_back_to_0700 (self ): 23d = self .root / "recipes" 24d .mkdir () 25os .chmod (d ,0o755 ) 26self .assertFalse (posix_owner .owner_only_wrong (d ,d .lstat ())) 27self .assertEqual (stat .S_IMODE (d .lstat ().st_mode ),0o700 ) 28 29def test_our_file_with_open_modes_is_put_back_to_0600 (self ): 30f = self .root / "x.json" 31f .write_text ("{}" ) 32os .chmod (f ,0o666 ) 33self .assertFalse (posix_owner .owner_only_wrong (f ,f .lstat ())) 34self .assertEqual (stat .S_IMODE (f .lstat ().st_mode ),0o600 ) 35 36def test_a_private_one_is_left_alone (self ): 37d = self .root / "ok" 38d .mkdir (mode = 0o700 ) 39self .assertFalse (posix_owner .owner_only_wrong (d ,d .lstat ())) 40 41def test_a_link_is_never_followed_or_repaired (self ): 42target = self .root / "t" 43target .mkdir () 44os .chmod (target ,0o755 ) 45link = self .root / "l" 46os .symlink (target ,link ) 47self .assertTrue (posix_owner .owner_only_wrong (link ,link .lstat ())) 48self .assertEqual (stat .S_IMODE (target .lstat ().st_mode ),0o755 ) 49 50def test_a_folder_owned_by_someone_else_is_refused_and_not_touched (self ): 51d = self .root / "root-made" 52d .mkdir () 53os .chmod (d ,0o755 ) 54with mock .patch .object (posix_owner .os ,"getuid" ,return_value = os .getuid ()+ 1 ): 55self .assertTrue (posix_owner .owner_only_wrong (d ,d .lstat ())) 56text = posix_owner .owner_only_text ("recipe directory must be owner-only (mode 0700)" ,d ) 57self .assertEqual (stat .S_IMODE (d .lstat ().st_mode ),0o755 ) 58self .assertIn ("recipe directory belongs to" ,text ) 59self .assertIn ("sudo" ,text ) 60self .assertIn ("sudo chown -R \"$USER\"" ,text ) 61self .assertNotIn ("0700" ,text ) 62 63def test_a_filesystem_that_keeps_no_modes_warns_once_and_goes_on (self ): 64d = self .root / "drvfs" 65d .mkdir () 66os .chmod (d ,0o777 ) 67posix_owner ._NOTED .discard (str (d )) 68with mock .patch .object (posix_owner .os ,"chmod" ),mock .patch ("sys.stderr" )as err : 69self .assertFalse (posix_owner .owner_only_wrong (d ,d .lstat ())) 70self .assertFalse (posix_owner .owner_only_wrong (d ,d .lstat ())) 71printed = "" .join (str (c .args [0 ])for c in err .write .call_args_list ) 72self .assertEqual (printed .count ("cannot keep files private" ),1 ) 73 74def test_the_text_of_our_own_folder_is_unchanged (self ): 75d = self .root / "mine" 76d .mkdir () 77self .assertEqual (posix_owner .owner_only_text ("x must be owner-only" ,d ),"x must be owner-only" ) 78 79 80class Windows (unittest .TestCase ): 81def test_nothing_changes_off_posix (self ): 82with mock .patch .object (posix_owner ,"POSIX" ,False ): 83self .assertFalse (posix_owner .owner_only_wrong ("." ,os .stat ("." ))) 84self .assertEqual (posix_owner .owner_only_text ("x" ,"." ),"x" ) 85 86 87if __name__ == "__main__" : 88unittest .main ()