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 |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 8 | from test.support import run_unittest, TESTFN, verbose, requires, \ |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 9 | TestSkipped, unlink |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 10 | |
Martin v. Löwis | df8adcd | 2001-12-11 17:57:26 +0000 | [diff] [blame] | 11 | try: |
| 12 | import signal |
| 13 | # The default handler for SIGXFSZ is to abort the process. |
| 14 | # By ignoring it, system calls exceeding the file size resource |
| 15 | # limit will raise IOError instead of crashing the interpreter. |
| 16 | oldhandler = signal.signal(signal.SIGXFSZ, signal.SIG_IGN) |
| 17 | except (ImportError, AttributeError): |
| 18 | pass |
| 19 | |
Guido van Rossum | a31ddbb | 2001-09-10 15:03:18 +0000 | [diff] [blame] | 20 | # create >2GB file (2GB = 2147483648 bytes) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 21 | size = 2500000000 |
Guido van Rossum | a31ddbb | 2001-09-10 15:03:18 +0000 | [diff] [blame] | 22 | |
| 23 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 24 | class TestCase(unittest.TestCase): |
| 25 | """Test that each file function works as expected for a large |
| 26 | (i.e. > 2GB, do we have to check > 4GB) files. |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 27 | |
| 28 | NOTE: the order of execution of the test methods is important! test_seek |
| 29 | must run first to create the test file. File cleanup must also be handled |
| 30 | outside the test instances because of this. |
| 31 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 32 | """ |
| 33 | |
| 34 | def test_seek(self): |
| 35 | if verbose: |
| 36 | print('create large file via seek (may be sparse file) ...') |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 37 | with open(TESTFN, 'wb') as f: |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 38 | f.write(b'z') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 39 | f.seek(0) |
| 40 | f.seek(size) |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 41 | f.write(b'a') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 42 | f.flush() |
| 43 | if verbose: |
| 44 | print('check file size with os.fstat') |
| 45 | self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 46 | |
| 47 | def test_osstat(self): |
| 48 | if verbose: |
| 49 | print('check file size with os.stat') |
| 50 | self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1) |
| 51 | |
| 52 | def test_seek_read(self): |
| 53 | if verbose: |
| 54 | print('play around with seek() and read() with the built largefile') |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 55 | with open(TESTFN, 'rb') as f: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 56 | self.assertEqual(f.tell(), 0) |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 57 | self.assertEqual(f.read(1), b'z') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 58 | self.assertEqual(f.tell(), 1) |
| 59 | f.seek(0) |
| 60 | self.assertEqual(f.tell(), 0) |
| 61 | f.seek(0, 0) |
| 62 | self.assertEqual(f.tell(), 0) |
| 63 | f.seek(42) |
| 64 | self.assertEqual(f.tell(), 42) |
| 65 | f.seek(42, 0) |
| 66 | self.assertEqual(f.tell(), 42) |
| 67 | f.seek(42, 1) |
| 68 | self.assertEqual(f.tell(), 84) |
| 69 | f.seek(0, 1) |
| 70 | self.assertEqual(f.tell(), 84) |
| 71 | f.seek(0, 2) # seek from the end |
| 72 | self.assertEqual(f.tell(), size + 1 + 0) |
| 73 | f.seek(-10, 2) |
| 74 | self.assertEqual(f.tell(), size + 1 - 10) |
| 75 | f.seek(-size-1, 2) |
| 76 | self.assertEqual(f.tell(), 0) |
| 77 | f.seek(size) |
| 78 | self.assertEqual(f.tell(), size) |
| 79 | # the 'a' that was written at the end of file above |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 80 | self.assertEqual(f.read(1), b'a') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 81 | f.seek(-size-1, 1) |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 82 | self.assertEqual(f.read(1), b'z') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 83 | self.assertEqual(f.tell(), 1) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 84 | |
| 85 | def test_lseek(self): |
| 86 | if verbose: |
| 87 | print('play around with os.lseek() with the built largefile') |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 88 | with open(TESTFN, 'rb') as f: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 89 | self.assertEqual(os.lseek(f.fileno(), 0, 0), 0) |
| 90 | self.assertEqual(os.lseek(f.fileno(), 42, 0), 42) |
| 91 | self.assertEqual(os.lseek(f.fileno(), 42, 1), 84) |
| 92 | self.assertEqual(os.lseek(f.fileno(), 0, 1), 84) |
| 93 | self.assertEqual(os.lseek(f.fileno(), 0, 2), size+1+0) |
| 94 | self.assertEqual(os.lseek(f.fileno(), -10, 2), size+1-10) |
| 95 | self.assertEqual(os.lseek(f.fileno(), -size-1, 2), 0) |
| 96 | self.assertEqual(os.lseek(f.fileno(), size, 0), size) |
| 97 | # the 'a' that was written at the end of file above |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 98 | self.assertEqual(f.read(1), b'a') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 99 | |
| 100 | def test_truncate(self): |
| 101 | if verbose: |
| 102 | print('try truncate') |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 103 | with open(TESTFN, 'r+b') as f: |
| 104 | # this is already decided before start running the test suite |
| 105 | # but we do it anyway for extra protection |
| 106 | if not hasattr(f, 'truncate'): |
| 107 | raise TestSkipped("open().truncate() not available on this system") |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 108 | f.seek(0, 2) |
| 109 | # else we've lost track of the true size |
| 110 | self.assertEqual(f.tell(), size+1) |
| 111 | # Cut it back via seek + truncate with no argument. |
| 112 | newsize = size - 10 |
| 113 | f.seek(newsize) |
| 114 | f.truncate() |
| 115 | self.assertEqual(f.tell(), newsize) # else pointer moved |
| 116 | f.seek(0, 2) |
| 117 | self.assertEqual(f.tell(), newsize) # else wasn't truncated |
| 118 | # Ensure that truncate(smaller than true size) shrinks |
| 119 | # the file. |
| 120 | newsize -= 1 |
| 121 | f.seek(42) |
| 122 | f.truncate(newsize) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 123 | self.assertEqual(f.tell(), newsize) # else wasn't truncated |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 124 | f.seek(0, 2) |
| 125 | self.assertEqual(f.tell(), newsize) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 126 | # XXX truncate(larger than true size) is ill-defined |
| 127 | # across platform; cut it waaaaay back |
| 128 | f.seek(0) |
| 129 | f.truncate(1) |
Alexandre Vassalotti | 77250f4 | 2008-05-06 19:48:38 +0000 | [diff] [blame] | 130 | self.assertEqual(f.tell(), 1) # else pointer moved |
| 131 | f.seek(0) |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 132 | self.assertEqual(len(f.read()), 1) # else wasn't truncated |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 133 | |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 134 | def test_main(): |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 135 | # On Windows and Mac OSX this test comsumes large resources; It |
| 136 | # takes a long time to build the >2GB file and takes >2GB of disk |
| 137 | # space therefore the resource must be enabled to run this test. |
| 138 | # If not, nothing after this line stanza will be executed. |
| 139 | if sys.platform[:3] == 'win' or sys.platform == 'darwin': |
| 140 | requires('largefile', |
| 141 | '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] | 142 | else: |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 143 | # Only run if the current filesystem supports large files. |
| 144 | # (Skip this test on Windows, since we now always support |
| 145 | # large files.) |
| 146 | f = open(TESTFN, 'wb') |
| 147 | try: |
| 148 | # 2**31 == 2147483648 |
| 149 | f.seek(2147483649) |
| 150 | # Seeking is not enough of a test: you must write and |
| 151 | # flush, too! |
Alexandre Vassalotti | a351f77 | 2008-03-03 02:59:49 +0000 | [diff] [blame] | 152 | f.write(b'x') |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 153 | f.flush() |
| 154 | except (IOError, OverflowError): |
| 155 | f.close() |
| 156 | unlink(TESTFN) |
| 157 | raise TestSkipped("filesystem does not have largefile support") |
| 158 | else: |
| 159 | f.close() |
| 160 | suite = unittest.TestSuite() |
| 161 | suite.addTest(TestCase('test_seek')) |
| 162 | suite.addTest(TestCase('test_osstat')) |
| 163 | suite.addTest(TestCase('test_seek_read')) |
| 164 | suite.addTest(TestCase('test_lseek')) |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 165 | with open(TESTFN, 'w') as f: |
| 166 | if hasattr(f, 'truncate'): |
| 167 | suite.addTest(TestCase('test_truncate')) |
Brett Cannon | 7f757ed | 2008-03-03 02:31:43 +0000 | [diff] [blame] | 168 | unlink(TESTFN) |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 169 | try: |
| 170 | run_unittest(suite) |
| 171 | finally: |
| 172 | unlink(TESTFN) |
Guido van Rossum | 47f4034 | 2001-09-10 13:34:12 +0000 | [diff] [blame] | 173 | |
Christian Heimes | 77c02eb | 2008-02-09 02:18:51 +0000 | [diff] [blame] | 174 | if __name__ == '__main__': |
Christian Heimes | 180510d | 2008-03-03 19:15:45 +0000 | [diff] [blame] | 175 | test_main() |