MCPcopy Index your code
hub / github.com/python/cpython / test_realpath_permission

Method test_realpath_permission

Lib/test/test_ntpath.py:964–1006  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

962
963 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
964 def test_realpath_permission(self):
965 # Test whether python can resolve the real filename of a
966 # shortened file name even if it does not have permission to access it.
967 ABSTFN = ntpath.realpath(os_helper.TESTFN)
968
969 os_helper.unlink(ABSTFN)
970 os_helper.rmtree(ABSTFN)
971 os.mkdir(ABSTFN)
972 self.addCleanup(os_helper.rmtree, ABSTFN)
973
974 test_file = ntpath.join(ABSTFN, "LongFileName123.txt")
975 test_file_short = ntpath.join(ABSTFN, "LONGFI~1.TXT")
976
977 with open(test_file, "wb") as f:
978 f.write(b"content")
979 # Automatic generation of short names may be disabled on
980 # NTFS volumes for the sake of performance.
981 # They're not supported at all on ReFS and exFAT.
982 p = subprocess.run(
983 # Try to set the short name manually.
984 ['fsutil.exe', 'file', 'setShortName', test_file, 'LONGFI~1.TXT'],
985 creationflags=subprocess.DETACHED_PROCESS
986 )
987
988 if p.returncode:
989 raise unittest.SkipTest('failed to set short name')
990
991 try:
992 self.assertPathEqual(test_file, ntpath.realpath(test_file_short))
993 except AssertionError:
994 raise unittest.SkipTest('the filesystem seems to lack support for short filenames')
995
996 # Deny the right to [S]YNCHRONIZE on the file to
997 # force nt._getfinalpathname to fail with ERROR_ACCESS_DENIED.
998 p = subprocess.run(
999 ['icacls.exe', test_file, '/deny', '*S-1-5-32-545:(S)'],
1000 creationflags=subprocess.DETACHED_PROCESS
1001 )
1002
1003 if p.returncode:
1004 raise unittest.SkipTest('failed to deny access to the test file')
1005
1006 self.assertPathEqual(test_file, ntpath.realpath(test_file_short))
1007
1008 @os_helper.skip_unless_symlink
1009 @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')

Callers

nothing calls this directly

Calls 9

addCleanupMethod · 0.80
openFunction · 0.50
realpathMethod · 0.45
unlinkMethod · 0.45
mkdirMethod · 0.45
joinMethod · 0.45
writeMethod · 0.45
runMethod · 0.45
assertPathEqualMethod · 0.45

Tested by

no test coverage detected