|
10 | 10 | import os.path |
11 | 11 | import errno |
12 | 12 | import functools |
| 13 | +import socket |
13 | 14 | import subprocess |
14 | 15 | import random |
15 | 16 | import string |
|
29 | 30 | posix = None |
30 | 31 |
|
31 | 32 | from test import support |
32 | | -from test.support import os_helper |
| 33 | +from test.support import os_helper, socket_helper |
33 | 34 | from test.support.os_helper import TESTFN, FakePath |
34 | 35 |
|
35 | 36 | TESTFN2 = TESTFN + "2" |
@@ -1550,13 +1551,46 @@ def test_copyfile_named_pipe(self): |
1550 | 1551 | except PermissionError as e: |
1551 | 1552 | self.skipTest('os.mkfifo(): %s' % e) |
1552 | 1553 | try: |
1553 | | - self.assertRaises(shutil.SpecialFileError, |
1554 | | - shutil.copyfile, TESTFN, TESTFN2) |
1555 | | - self.assertRaises(shutil.SpecialFileError, |
1556 | | - shutil.copyfile, __file__, TESTFN) |
| 1554 | + self.assertRaisesRegex(shutil.SpecialFileError, 'is a named pipe', |
| 1555 | + shutil.copyfile, TESTFN, TESTFN2) |
| 1556 | + self.assertRaisesRegex(shutil.SpecialFileError, 'is a named pipe', |
| 1557 | + shutil.copyfile, __file__, TESTFN) |
1557 | 1558 | finally: |
1558 | 1559 | os.remove(TESTFN) |
1559 | 1560 |
|
| 1561 | + @socket_helper.skip_unless_bind_unix_socket |
| 1562 | + def test_copyfile_socket(self): |
| 1563 | + sock_path = os.path.join(self.mkdtemp(), 'sock') |
| 1564 | + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) |
| 1565 | + self.addCleanup(sock.close) |
| 1566 | + socket_helper.bind_unix_socket(sock, sock_path) |
| 1567 | + self.addCleanup(os_helper.unlink, sock_path) |
| 1568 | + self.assertRaisesRegex(shutil.SpecialFileError, 'is a socket', |
| 1569 | + shutil.copyfile, sock_path, sock_path + '.copy') |
| 1570 | + self.assertRaisesRegex(shutil.SpecialFileError, 'is a socket', |
| 1571 | + shutil.copyfile, __file__, sock_path) |
| 1572 | + |
| 1573 | + @unittest.skipIf(os.name == 'nt', 'requires /dev/null') |
| 1574 | + def test_copyfile_character_device(self): |
| 1575 | + self.assertRaisesRegex(shutil.SpecialFileError, 'is a character device', |
| 1576 | + shutil.copyfile, '/dev/null', TESTFN) |
| 1577 | + src_file = os.path.join(self.mkdtemp(), 'src') |
| 1578 | + create_file(src_file, 'foo') |
| 1579 | + self.assertRaisesRegex(shutil.SpecialFileError, 'is a character device', |
| 1580 | + shutil.copyfile, src_file, '/dev/null') |
| 1581 | + |
| 1582 | + def test_copyfile_block_device(self): |
| 1583 | + block_dev = None |
| 1584 | + for dev in ['/dev/loop0', '/dev/sda', '/dev/vda', '/dev/disk0']: |
| 1585 | + if os.path.exists(dev) and stat.S_ISBLK(os.stat(dev).st_mode): |
| 1586 | + if os.access(dev, os.R_OK): |
| 1587 | + block_dev = dev |
| 1588 | + break |
| 1589 | + if block_dev is None: |
| 1590 | + self.skipTest('no accessible block device found') |
| 1591 | + self.assertRaisesRegex(shutil.SpecialFileError, 'is a block device', |
| 1592 | + shutil.copyfile, block_dev, TESTFN) |
| 1593 | + |
1560 | 1594 | def test_copyfile_return_value(self): |
1561 | 1595 | # copytree returns its destination path. |
1562 | 1596 | src_dir = self.mkdtemp() |
|
0 commit comments