@@ -41,6 +41,9 @@ def test_is_notimplemented(self):
4141only_posix = unittest .skipIf (os .name == 'nt' ,
4242 'test requires a POSIX-compatible system' )
4343
44+ root_in_posix = False
45+ if hasattr (os , 'geteuid' ):
46+ root_in_posix = (os .geteuid () == 0 )
4447
4548#
4649# Tests for the pure classes.
@@ -2975,27 +2978,75 @@ def test_chmod_follow_symlinks_true(self):
29752978
29762979 # XXX also need a test for lchmod.
29772980
2978- @unittest .skipUnless (pwd , "the pwd module is needed for this test" )
2979- def test_owner (self ):
2980- p = self .cls (BASE ) / 'fileA'
2981- uid = p .stat ().st_uid
2981+ def _get_pw_name_or_skip_test (self , uid ):
29822982 try :
2983- name = pwd .getpwuid (uid ).pw_name
2983+ return pwd .getpwuid (uid ).pw_name
29842984 except KeyError :
29852985 self .skipTest (
29862986 "user %d doesn't have an entry in the system database" % uid )
2987- self .assertEqual (name , p .owner ())
29882987
2989- @unittest .skipUnless (grp , "the grp module is needed for this test" )
2990- def test_group (self ):
2988+ @unittest .skipUnless (pwd , "the pwd module is needed for this test" )
2989+ def test_owner (self ):
29912990 p = self .cls (BASE ) / 'fileA'
2992- gid = p .stat ().st_gid
2991+ expected_uid = p .stat ().st_uid
2992+ expected_name = self ._get_pw_name_or_skip_test (expected_uid )
2993+
2994+ self .assertEqual (expected_name , p .owner ())
2995+
2996+ @unittest .skipUnless (pwd , "the pwd module is needed for this test" )
2997+ @unittest .skipUnless (root_in_posix , "test needs root privilege" )
2998+ def test_owner_no_follow_symlinks (self ):
2999+ all_users = [u .pw_uid for u in pwd .getpwall ()]
3000+ if len (all_users ) < 2 :
3001+ self .skipTest ("test needs more than one user" )
3002+
3003+ target = self .cls (BASE ) / 'fileA'
3004+ link = self .cls (BASE ) / 'linkA'
3005+
3006+ uid_1 , uid_2 = all_users [:2 ]
3007+ os .chown (target , uid_1 , - 1 )
3008+ os .chown (link , uid_2 , - 1 , follow_symlinks = False )
3009+
3010+ expected_uid = link .stat (follow_symlinks = False ).st_uid
3011+ expected_name = self ._get_pw_name_or_skip_test (expected_uid )
3012+
3013+ self .assertEqual (expected_uid , uid_2 )
3014+ self .assertEqual (expected_name , link .owner (follow_symlinks = False ))
3015+
3016+ def _get_gr_name_or_skip_test (self , gid ):
29933017 try :
2994- name = grp .getgrgid (gid ).gr_name
3018+ return grp .getgrgid (gid ).gr_name
29953019 except KeyError :
29963020 self .skipTest (
29973021 "group %d doesn't have an entry in the system database" % gid )
2998- self .assertEqual (name , p .group ())
3022+
3023+ @unittest .skipUnless (grp , "the grp module is needed for this test" )
3024+ def test_group (self ):
3025+ p = self .cls (BASE ) / 'fileA'
3026+ expected_gid = p .stat ().st_gid
3027+ expected_name = self ._get_gr_name_or_skip_test (expected_gid )
3028+
3029+ self .assertEqual (expected_name , p .group ())
3030+
3031+ @unittest .skipUnless (grp , "the grp module is needed for this test" )
3032+ @unittest .skipUnless (root_in_posix , "test needs root privilege" )
3033+ def test_group_no_follow_symlinks (self ):
3034+ all_groups = [g .gr_gid for g in grp .getgrall ()]
3035+ if len (all_groups ) < 2 :
3036+ self .skipTest ("test needs more than one group" )
3037+
3038+ target = self .cls (BASE ) / 'fileA'
3039+ link = self .cls (BASE ) / 'linkA'
3040+
3041+ gid_1 , gid_2 = all_groups [:2 ]
3042+ os .chown (target , - 1 , gid_1 )
3043+ os .chown (link , - 1 , gid_2 , follow_symlinks = False )
3044+
3045+ expected_gid = link .stat (follow_symlinks = False ).st_gid
3046+ expected_name = self ._get_pw_name_or_skip_test (expected_gid )
3047+
3048+ self .assertEqual (expected_gid , gid_2 )
3049+ self .assertEqual (expected_name , link .group (follow_symlinks = False ))
29993050
30003051 def test_unlink (self ):
30013052 p = self .cls (BASE ) / 'fileA'
0 commit comments