Skip to content

Commit 88fa4c7

Browse files
committed
gh-154726: Test FIFO and socket symlinks in shutil.copyfile()
1 parent 1451551 commit 88fa4c7

1 file changed

Lines changed: 42 additions & 9 deletions

File tree

Lib/test/test_shutil.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,47 @@ def test_copyfile_socket(self):
15741574
self.assertRaisesRegex(shutil.SpecialFileError, 'is a socket',
15751575
shutil.copyfile, __file__, sock_path)
15761576

1577+
def _check_copyfile_symlink_to_special_file(self, target):
1578+
tmp_dir = self.mkdtemp()
1579+
src = os.path.join(tmp_dir, 'src')
1580+
dst = os.path.join(tmp_dir, 'dst')
1581+
os.symlink(target, src)
1582+
1583+
shutil.copyfile(src, dst, follow_symlinks=False)
1584+
1585+
self.assertTrue(os.path.islink(dst))
1586+
self.assertEqual(os.readlink(dst), target)
1587+
1588+
@os_helper.skip_unless_symlink
1589+
@unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()')
1590+
@unittest.skipIf(sys.platform == "vxworks",
1591+
"fifo requires special path on VxWorks")
1592+
def test_copyfile_symlink_to_named_pipe(self):
1593+
fifo_path = os.path.join(self.mkdtemp(), 'fifo')
1594+
try:
1595+
os.mkfifo(fifo_path)
1596+
except PermissionError as e:
1597+
self.skipTest('os.mkfifo(): %s' % e)
1598+
self._check_copyfile_symlink_to_special_file(fifo_path)
1599+
1600+
@os_helper.skip_unless_symlink
1601+
@socket_helper.skip_unless_bind_unix_socket
1602+
def test_copyfile_symlink_to_socket(self):
1603+
sock_path = os.path.join(self.mkdtemp(), 'sock')
1604+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
1605+
self.addCleanup(sock.close)
1606+
try:
1607+
socket_helper.bind_unix_socket(sock, sock_path)
1608+
except OSError as e:
1609+
if str(e) == "AF_UNIX path too long":
1610+
self.skipTest(
1611+
"Pathname {0!a} is too long to serve as an AF_UNIX path"
1612+
.format(sock_path))
1613+
else:
1614+
raise
1615+
self.addCleanup(os_helper.unlink, sock_path)
1616+
self._check_copyfile_symlink_to_special_file(sock_path)
1617+
15771618
@unittest.skipUnless(os.path.exists('/dev/null'), 'requires /dev/null')
15781619
def test_copyfile_character_device(self):
15791620
self.assertRaisesRegex(shutil.SpecialFileError, 'is a character device',
@@ -1586,15 +1627,7 @@ def test_copyfile_character_device(self):
15861627
@os_helper.skip_unless_symlink
15871628
@unittest.skipUnless(os.path.exists('/dev/null'), 'requires /dev/null')
15881629
def test_copyfile_symlink_to_character_device(self):
1589-
tmp_dir = self.mkdtemp()
1590-
src = os.path.join(tmp_dir, 'src')
1591-
dst = os.path.join(tmp_dir, 'dst')
1592-
os.symlink('/dev/null', src)
1593-
1594-
shutil.copyfile(src, dst, follow_symlinks=False)
1595-
1596-
self.assertTrue(os.path.islink(dst))
1597-
self.assertEqual(os.readlink(dst), '/dev/null')
1630+
self._check_copyfile_symlink_to_special_file('/dev/null')
15981631

15991632
def test_copyfile_block_device(self):
16001633
block_dev = None

0 commit comments

Comments
 (0)