Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 1 | """Test largefile support on system where this makes sense. |
| 2 | """ |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 3 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 4 | import os |
| 5 | import stat |
| 6 | import sys |
| 7 | import unittest |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 8 | import socket |
| 9 | import shutil |
| 10 | import threading |
| 11 | from test.support import TESTFN, requires, unlink, bigmemtest, find_unused_port |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 12 | import io # C implementation of io |
| 13 | import _pyio as pyio # Python implementation of io |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 14 | |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 15 | # size of file to create (>2 GiB; 2 GiB == 2,147,483,648 bytes) |
Stéphane Wirtel | 74a8b6e | 2018-10-18 01:05:04 +0200 | [diff] [blame] | 16 | size = 2_500_000_000 |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 17 | TESTFN2 = TESTFN + '2' |
| 18 | |
Guido van Rossum | a31ddbb | 2001-09-10 15:03:18 +0000 | [diff] [blame] | 19 | |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 20 | class LargeFileTest: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 21 | |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 22 | def setUp(self): |
| 23 | if os.path.exists(TESTFN): |
| 24 | mode = 'r+b' |
| 25 | else: |
| 26 | mode = 'w+b' |
| 27 | |
| 28 | with self.open(TESTFN, mode) as f: |
| 29 | current_size = os.fstat(f.fileno())[stat.ST_SIZE] |
| 30 | if current_size == size+1: |
| 31 | return |
| 32 | |
| 33 | if current_size == 0: |
| 34 | f.write(b'z') |
| 35 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 36 | f.seek(0) |
| 37 | f.seek(size) |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 38 | f.write(b'a') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 39 | f.flush() |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 40 | self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 41 | |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 42 | @classmethod |
| 43 | def tearDownClass(cls): |
| 44 | with cls.open(TESTFN, 'wb'): |
| 45 | pass |
| 46 | if not os.stat(TESTFN)[stat.ST_SIZE] == 0: |
| 47 | raise cls.failureException('File was not truncated by opening ' |
| 48 | 'with mode "wb"') |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 49 | unlink(TESTFN2) |
| 50 | |
| 51 | |
| 52 | class TestFileMethods(LargeFileTest): |
| 53 | """Test that each file function works as expected for large |
| 54 | (i.e. > 2 GiB) files. |
| 55 | """ |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 56 | |
Stéphane Wirtel | 74a8b6e | 2018-10-18 01:05:04 +0200 | [diff] [blame] | 57 | # _pyio.FileIO.readall() uses a temporary bytearray then casted to bytes, |
| 58 | # so memuse=2 is needed |
| 59 | @bigmemtest(size=size, memuse=2, dry_run=False) |
| 60 | def test_large_read(self, _size): |
| 61 | # bpo-24658: Test that a read greater than 2GB does not fail. |
| 62 | with self.open(TESTFN, "rb") as f: |
| 63 | self.assertEqual(len(f.read()), size + 1) |
| 64 | self.assertEqual(f.tell(), size + 1) |
| 65 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 66 | def test_osstat(self): |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 67 | self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1) |
| 68 | |
| 69 | def test_seek_read(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 70 | with self.open(TESTFN, 'rb') as f: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 71 | self.assertEqual(f.tell(), 0) |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 72 | self.assertEqual(f.read(1), b'z') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 73 | self.assertEqual(f.tell(), 1) |
| 74 | f.seek(0) |
| 75 | self.assertEqual(f.tell(), 0) |
| 76 | f.seek(0, 0) |
| 77 | self.assertEqual(f.tell(), 0) |
| 78 | f.seek(42) |
| 79 | self.assertEqual(f.tell(), 42) |
| 80 | f.seek(42, 0) |
| 81 | self.assertEqual(f.tell(), 42) |
| 82 | f.seek(42, 1) |
| 83 | self.assertEqual(f.tell(), 84) |
| 84 | f.seek(0, 1) |
| 85 | self.assertEqual(f.tell(), 84) |
| 86 | f.seek(0, 2) # seek from the end |
| 87 | self.assertEqual(f.tell(), size + 1 + 0) |
| 88 | f.seek(-10, 2) |
| 89 | self.assertEqual(f.tell(), size + 1 - 10) |
| 90 | f.seek(-size-1, 2) |
| 91 | self.assertEqual(f.tell(), 0) |
| 92 | f.seek(size) |
| 93 | self.assertEqual(f.tell(), size) |
| 94 | # the 'a' that was written at the end of file above |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 95 | self.assertEqual(f.read(1), b'a') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 96 | f.seek(-size-1, 1) |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 97 | self.assertEqual(f.read(1), b'z') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 98 | self.assertEqual(f.tell(), 1) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 99 | |
| 100 | def test_lseek(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 101 | with self.open(TESTFN, 'rb') as f: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 102 | self.assertEqual(os.lseek(f.fileno(), 0, 0), 0) |
| 103 | self.assertEqual(os.lseek(f.fileno(), 42, 0), 42) |
| 104 | self.assertEqual(os.lseek(f.fileno(), 42, 1), 84) |
| 105 | self.assertEqual(os.lseek(f.fileno(), 0, 1), 84) |
| 106 | self.assertEqual(os.lseek(f.fileno(), 0, 2), size+1+0) |
| 107 | self.assertEqual(os.lseek(f.fileno(), -10, 2), size+1-10) |
| 108 | self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0) |
| 109 | self.assertEqual(os.lseek(f.fileno(), size, 0), size) |
| 110 | # the 'a' that was written at the end of file above |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 111 | self.assertEqual(f.read(1), b'a') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 112 | |
| 113 | def test_truncate(self): |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 114 | with self.open(TESTFN, 'r+b') as f: |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 115 | if not hasattr(f, 'truncate'): |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 116 | raise unittest.SkipTest("open().truncate() not available " |
| 117 | "on this system") |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 118 | f.seek(0, 2) |
| 119 | # else we've lost track of the true size |
| 120 | self.assertEqual(f.tell(), size+1) |
| 121 | # Cut it back via seek + truncate with no argument. |
| 122 | newsize = size - 10 |
| 123 | f.seek(newsize) |
| 124 | f.truncate() |
| 125 | self.assertEqual(f.tell(), newsize) # else pointer moved |
| 126 | f.seek(0, 2) |
| 127 | self.assertEqual(f.tell(), newsize) # else wasn't truncated |
| 128 | # Ensure that truncate(smaller than true size) shrinks |
| 129 | # the file. |
| 130 | newsize -= 1 |
| 131 | f.seek(42) |
| 132 | f.truncate(newsize) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 133 | self.assertEqual(f.tell(), 42) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 134 | f.seek(0, 2) |
| 135 | self.assertEqual(f.tell(), newsize) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 136 | # XXX truncate(larger than true size) is ill-defined |
| 137 | # across platform; cut it waaaaay back |
| 138 | f.seek(0) |
| 139 | f.truncate(1) |
Antoine Pitrou | 905a2ff | 2010-01-31 22:47:27 +0000 | [diff] [blame] | 140 | self.assertEqual(f.tell(), 0) # else pointer moved |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 141 | f.seek(0) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 142 | self.assertEqual(len(f.read()), 1) # else wasn't truncated |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 143 | |
Antoine Pitrou | a28fcfd | 2009-03-13 23:42:55 +0000 | [diff] [blame] | 144 | def test_seekable(self): |
| 145 | # Issue #5016; seekable() can return False when the current position |
| 146 | # is negative when truncated to an int. |
| 147 | for pos in (2**31-1, 2**31, 2**31+1): |
| 148 | with self.open(TESTFN, 'rb') as f: |
| 149 | f.seek(pos) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 150 | self.assertTrue(f.seekable()) |
Antoine Pitrou | a28fcfd | 2009-03-13 23:42:55 +0000 | [diff] [blame] | 151 | |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 152 | |
| 153 | class TestCopyfile(LargeFileTest, unittest.TestCase): |
| 154 | open = staticmethod(io.open) |
| 155 | |
| 156 | def test_it(self): |
| 157 | # Internally shutil.copyfile() can use "fast copy" methods like |
| 158 | # os.sendfile(). |
| 159 | size = os.path.getsize(TESTFN) |
| 160 | shutil.copyfile(TESTFN, TESTFN2) |
| 161 | self.assertEqual(os.path.getsize(TESTFN2), size) |
| 162 | with open(TESTFN2, 'rb') as f: |
| 163 | self.assertEqual(f.read(5), b'z\x00\x00\x00\x00') |
| 164 | f.seek(size - 5) |
| 165 | self.assertEqual(f.read(), b'\x00\x00\x00\x00a') |
| 166 | |
| 167 | |
| 168 | @unittest.skipIf(not hasattr(os, 'sendfile'), 'sendfile not supported') |
| 169 | class TestSocketSendfile(LargeFileTest, unittest.TestCase): |
| 170 | open = staticmethod(io.open) |
| 171 | timeout = 3 |
| 172 | |
| 173 | def setUp(self): |
| 174 | super().setUp() |
| 175 | self.thread = None |
| 176 | |
| 177 | def tearDown(self): |
| 178 | super().tearDown() |
| 179 | if self.thread is not None: |
| 180 | self.thread.join(self.timeout) |
| 181 | self.thread = None |
| 182 | |
| 183 | def tcp_server(self, sock): |
| 184 | def run(sock): |
| 185 | with sock: |
| 186 | conn, _ = sock.accept() |
| 187 | with conn, open(TESTFN2, 'wb') as f: |
| 188 | event.wait(self.timeout) |
| 189 | while True: |
| 190 | chunk = conn.recv(65536) |
| 191 | if not chunk: |
| 192 | return |
| 193 | f.write(chunk) |
| 194 | |
| 195 | event = threading.Event() |
| 196 | sock.settimeout(self.timeout) |
| 197 | self.thread = threading.Thread(target=run, args=(sock, )) |
| 198 | self.thread.start() |
| 199 | event.set() |
| 200 | |
| 201 | def test_it(self): |
| 202 | port = find_unused_port() |
| 203 | with socket.create_server(("", port)) as sock: |
| 204 | self.tcp_server(sock) |
| 205 | with socket.create_connection(("127.0.0.1", port)) as client: |
| 206 | with open(TESTFN, 'rb') as f: |
| 207 | client.sendfile(f) |
| 208 | self.tearDown() |
| 209 | |
| 210 | size = os.path.getsize(TESTFN) |
| 211 | self.assertEqual(os.path.getsize(TESTFN2), size) |
| 212 | with open(TESTFN2, 'rb') as f: |
| 213 | self.assertEqual(f.read(5), b'z\x00\x00\x00\x00') |
| 214 | f.seek(size - 5) |
| 215 | self.assertEqual(f.read(), b'\x00\x00\x00\x00a') |
| 216 | |
| 217 | |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 218 | def setUpModule(): |
| 219 | try: |
| 220 | import signal |
| 221 | # The default handler for SIGXFSZ is to abort the process. |
| 222 | # By ignoring it, system calls exceeding the file size resource |
| 223 | # limit will raise OSError instead of crashing the interpreter. |
| 224 | signal.signal(signal.SIGXFSZ, signal.SIG_IGN) |
| 225 | except (ImportError, AttributeError): |
| 226 | pass |
Antoine Pitrou | a28fcfd | 2009-03-13 23:42:55 +0000 | [diff] [blame] | 227 | |
Mike | 53f7a7c | 2017-12-14 14:04:53 +0300 | [diff] [blame] | 228 | # On Windows and Mac OSX this test consumes large resources; It |
Victor Stinner | 8c663fd | 2017-11-08 14:44:44 -0800 | [diff] [blame] | 229 | # takes a long time to build the >2 GiB file and takes >2 GiB of disk |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 230 | # space therefore the resource must be enabled to run this test. |
| 231 | # If not, nothing after this line stanza will be executed. |
Victor Stinner | 937ee9e | 2018-06-26 02:11:06 +0200 | [diff] [blame] | 232 | if sys.platform[:3] == 'win' or sys.platform == 'darwin': |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 233 | requires('largefile', |
| 234 | 'test requires %s bytes and a long time to run' % str(size)) |
Guido van Rossum | 47f4034 | 2001-09-10 13:34:12 +0000 | [diff] [blame] | 235 | else: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 236 | # Only run if the current filesystem supports large files. |
| 237 | # (Skip this test on Windows, since we now always support |
| 238 | # large files.) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 239 | f = open(TESTFN, 'wb', buffering=0) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 240 | try: |
| 241 | # 2**31 == 2147483648 |
| 242 | f.seek(2147483649) |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 243 | # Seeking is not enough of a test: you must write and flush, too! |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 244 | f.write(b'x') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 245 | f.flush() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 246 | except (OSError, OverflowError): |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 247 | raise unittest.SkipTest("filesystem does not have " |
| 248 | "largefile support") |
| 249 | finally: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 250 | f.close() |
| 251 | unlink(TESTFN) |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 252 | |
| 253 | |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 254 | class CLargeFileTest(TestFileMethods, unittest.TestCase): |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 255 | open = staticmethod(io.open) |
| 256 | |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 257 | |
| 258 | class PyLargeFileTest(TestFileMethods, unittest.TestCase): |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 259 | open = staticmethod(pyio.open) |
| 260 | |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 261 | |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 262 | def tearDownModule(): |
| 263 | unlink(TESTFN) |
Giampaolo Rodola | 5bcc6d8 | 2019-09-30 12:51:55 +0800 | [diff] [blame^] | 264 | unlink(TESTFN2) |
| 265 | |
Guido van Rossum | 47f4034 | 2001-09-10 13:34:12 +0000 | [diff] [blame] | 266 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 267 | if __name__ == '__main__': |
Serhiy Storchaka | c406a12 | 2013-07-17 13:42:24 +0300 | [diff] [blame] | 268 | unittest.main() |