blob: 1da0e7d1dc450f4e09b4ffba87558a8d72708b17 [file] [log] [blame]
Facundo Batista9521f082008-02-07 16:16:29 +00001"""Test largefile support on system where this makes sense.
2"""
Trent Mickf29f47b2000-08-11 19:02:59 +00003
Facundo Batista9521f082008-02-07 16:16:29 +00004import os
5import stat
6import sys
7import unittest
8from test.test_support import run_unittest, TESTFN, verbose, requires, \
9 TestSkipped, unlink
Trent Mickf29f47b2000-08-11 19:02:59 +000010
Martin v. Löwisdf8adcd2001-12-11 17:57:26 +000011try:
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)
17except (ImportError, AttributeError):
18 pass
19
Guido van Rossuma31ddbb2001-09-10 15:03:18 +000020# create >2GB file (2GB = 2147483648 bytes)
21size = 2500000000L
Guido van Rossuma31ddbb2001-09-10 15:03:18 +000022
23
Facundo Batista9521f082008-02-07 16:16:29 +000024class 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.
Brett Cannon6382ffc2008-03-03 02:41:40 +000027
Brett Cannonbfbf5b32008-03-03 03:24:48 +000028 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
Facundo Batista9521f082008-02-07 16:16:29 +000032 """
33
34 def test_seek(self):
35 if verbose:
36 print 'create large file via seek (may be sparse file) ...'
Brett Cannon6382ffc2008-03-03 02:41:40 +000037 with open(TESTFN, 'wb') as f:
Facundo Batista9521f082008-02-07 16:16:29 +000038 f.write('z')
39 f.seek(0)
40 f.seek(size)
41 f.write('a')
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)
Facundo Batista9521f082008-02-07 16:16:29 +000046
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'
Brett Cannon6382ffc2008-03-03 02:41:40 +000055 with open(TESTFN, 'rb') as f:
Facundo Batista9521f082008-02-07 16:16:29 +000056 self.assertEqual(f.tell(), 0)
57 self.assertEqual(f.read(1), 'z')
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
80 self.assertEqual(f.read(1), 'a')
81 f.seek(-size-1, 1)
82 self.assertEqual(f.read(1), 'z')
83 self.assertEqual(f.tell(), 1)
Facundo Batista9521f082008-02-07 16:16:29 +000084
85 def test_lseek(self):
86 if verbose:
87 print 'play around with os.lseek() with the built largefile'
Brett Cannon6382ffc2008-03-03 02:41:40 +000088 with open(TESTFN, 'rb') as f:
Facundo Batista9521f082008-02-07 16:16:29 +000089 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
98 self.assertEqual(f.read(1), 'a')
Facundo Batista9521f082008-02-07 16:16:29 +000099
100 def test_truncate(self):
101 if verbose:
102 print 'try truncate'
Brett Cannon6382ffc2008-03-03 02:41:40 +0000103 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"
Facundo Batista9521f082008-02-07 16:16:29 +0000108 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)
123 self.assertEqual(f.tell(), 42) # else pointer moved
124 f.seek(0, 2)
125 self.assertEqual(f.tell(), newsize) # else wasn't truncated
126
127 # XXX truncate(larger than true size) is ill-defined
128 # across platform; cut it waaaaay back
129 f.seek(0)
130 f.truncate(1)
131 self.assertEqual(f.tell(), 0) # else pointer moved
132 self.assertEqual(len(f.read()), 1) # else wasn't truncated
Facundo Batista9521f082008-02-07 16:16:29 +0000133
134
Brett Cannon6382ffc2008-03-03 02:41:40 +0000135def test_main():
Facundo Batista9521f082008-02-07 16:16:29 +0000136 # On Windows and Mac OSX this test comsumes large resources; It
137 # takes a long time to build the >2GB file and takes >2GB of disk
138 # space therefore the resource must be enabled to run this test.
139 # If not, nothing after this line stanza will be executed.
140 if sys.platform[:3] == 'win' or sys.platform == 'darwin':
141 requires('largefile',
142 'test requires %s bytes and a long time to run' % str(size))
Guido van Rossum47f40342001-09-10 13:34:12 +0000143 else:
Facundo Batista9521f082008-02-07 16:16:29 +0000144 # Only run if the current filesystem supports large files.
145 # (Skip this test on Windows, since we now always support
146 # large files.)
147 f = open(TESTFN, 'wb')
148 try:
149 # 2**31 == 2147483648
150 f.seek(2147483649L)
151 # Seeking is not enough of a test: you must write and
152 # flush, too!
153 f.write("x")
154 f.flush()
155 except (IOError, OverflowError):
156 f.close()
157 unlink(TESTFN)
158 raise TestSkipped, "filesystem does not have largefile support"
159 else:
160 f.close()
161 suite = unittest.TestSuite()
162 suite.addTest(TestCase('test_seek'))
163 suite.addTest(TestCase('test_osstat'))
164 suite.addTest(TestCase('test_seek_read'))
165 suite.addTest(TestCase('test_lseek'))
Brett Cannon6382ffc2008-03-03 02:41:40 +0000166 with open(TESTFN, 'w') as f:
167 if hasattr(f, 'truncate'):
168 suite.addTest(TestCase('test_truncate'))
Facundo Batista9521f082008-02-07 16:16:29 +0000169 unlink(TESTFN)
Brett Cannon6382ffc2008-03-03 02:41:40 +0000170 try:
171 run_unittest(suite)
172 finally:
173 unlink(TESTFN)
Trent Mickf29f47b2000-08-11 19:02:59 +0000174
Trent Mickf29f47b2000-08-11 19:02:59 +0000175
Facundo Batista9521f082008-02-07 16:16:29 +0000176if __name__ == '__main__':
Brett Cannon6382ffc2008-03-03 02:41:40 +0000177 test_main()