]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_gzip.py
2 """Test script for the gzip module.
6 from test
import test_support
10 gzip
= test_support
.import_module('gzip')
12 data1
= """ int length=DEFAULTALLOC, err = Z_OK;
14 int flushmode = Z_FINISH;
15 unsigned long start_total_out;
19 data2
= """/* zlibmodule.c -- gzip-compatible data compression */
20 /* See http://www.gzip.org/zlib/
21 /* See http://www.winimage.com/zLibDll for Windows */
25 class TestGzip(unittest
.TestCase
):
26 filename
= test_support
.TESTFN
29 test_support
.unlink(self
.filename
)
32 test_support
.unlink(self
.filename
)
36 with gzip
.GzipFile(self
.filename
, 'wb') as f
:
39 # Try flush and fileno.
42 if hasattr(os
, 'fsync'):
46 # Test multiple close() calls.
52 with gzip
.GzipFile(self
.filename
, 'r') as f
:
54 self
.assertEqual(d
, data1
*50)
56 def test_io_on_closed_object(self
):
57 # Test that I/O operations on closed GzipFile objects raise a
58 # ValueError, just like the corresponding functions on file objects.
60 # Write to a file, open it for reading, then close it.
62 f
= gzip
.GzipFile(self
.filename
, 'r')
64 with self
.assertRaises(ValueError):
66 with self
.assertRaises(ValueError):
68 with self
.assertRaises(ValueError):
70 # Open the file for writing, then close it.
71 f
= gzip
.GzipFile(self
.filename
, 'w')
73 with self
.assertRaises(ValueError):
75 with self
.assertRaises(ValueError):
78 def test_append(self
):
80 # Append to the previous file
81 with gzip
.GzipFile(self
.filename
, 'ab') as f
:
84 with gzip
.GzipFile(self
.filename
, 'rb') as f
:
86 self
.assertEqual(d
, (data1
*50) + (data2
*15))
88 def test_many_append(self
):
89 # Bug #1074261 was triggered when reading a file that contained
90 # many, many members. Create such a file and verify that reading it
92 with gzip
.open(self
.filename
, 'wb', 9) as f
:
94 for i
in range(0, 200):
95 with gzip
.open(self
.filename
, "ab", 9) as f
: # append
98 # Try reading the file
99 with gzip
.open(self
.filename
, "rb") as zgfile
:
102 ztxt
= zgfile
.read(8192)
105 self
.assertEqual(contents
, 'a'*201)
107 def test_buffered_reader(self
):
108 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
112 with gzip
.GzipFile(self
.filename
, 'rb') as f
:
113 with io
.BufferedReader(f
) as r
:
114 lines
= [line
for line
in r
]
116 self
.assertEqual(lines
, 50 * data1
.splitlines(True))
118 def test_readline(self
):
120 # Try .readline() with varying line lengths
122 with gzip
.GzipFile(self
.filename
, 'rb') as f
:
125 L
= f
.readline(line_length
)
126 if not L
and line_length
!= 0: break
127 self
.assertTrue(len(L
) <= line_length
)
128 line_length
= (line_length
+ 1) % 50
130 def test_readlines(self
):
134 with gzip
.GzipFile(self
.filename
, 'rb') as f
:
137 with gzip
.GzipFile(self
.filename
, 'rb') as f
:
142 def test_seek_read(self
):
144 # Try seek, read test
146 with gzip
.GzipFile(self
.filename
) as f
:
152 f
.seek(oldpos
) # negative seek
157 line2
= f
.read(amount
)
158 self
.assertEqual(line1
[:amount
], line2
)
159 f
.seek(newpos
) # positive seek
161 def test_seek_whence(self
):
163 # Try seek(whence=1), read test
165 with gzip
.GzipFile(self
.filename
) as f
:
169 self
.assertEqual(y
, data1
[20:30])
171 def test_seek_write(self
):
172 # Try seek, write test
173 with gzip
.GzipFile(self
.filename
, 'w') as f
:
174 for pos
in range(0, 256, 16):
180 with gzip
.GzipFile(self
.filename
, 'r') as f
:
181 self
.assertEqual(f
.myfileobj
.mode
, 'rb')
183 def test_1647484(self
):
184 for mode
in ('wb', 'rb'):
185 with gzip
.GzipFile(self
.filename
, mode
) as f
:
186 self
.assertTrue(hasattr(f
, "name"))
187 self
.assertEqual(f
.name
, self
.filename
)
189 def test_mtime(self
):
191 with gzip
.GzipFile(self
.filename
, 'w', mtime
= mtime
) as fWrite
:
193 with gzip
.GzipFile(self
.filename
) as fRead
:
194 dataRead
= fRead
.read()
195 self
.assertEqual(dataRead
, data1
)
196 self
.assertTrue(hasattr(fRead
, 'mtime'))
197 self
.assertEqual(fRead
.mtime
, mtime
)
199 def test_metadata(self
):
202 with gzip
.GzipFile(self
.filename
, 'w', mtime
= mtime
) as fWrite
:
205 with
open(self
.filename
, 'rb') as fRead
:
206 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
208 idBytes
= fRead
.read(2)
209 self
.assertEqual(idBytes
, '\x1f\x8b') # gzip ID
211 cmByte
= fRead
.read(1)
212 self
.assertEqual(cmByte
, '\x08') # deflate
214 flagsByte
= fRead
.read(1)
215 self
.assertEqual(flagsByte
, '\x08') # only the FNAME flag is set
217 mtimeBytes
= fRead
.read(4)
218 self
.assertEqual(mtimeBytes
, struct
.pack('<i', mtime
)) # little-endian
220 xflByte
= fRead
.read(1)
221 self
.assertEqual(xflByte
, '\x02') # maximum compression
223 osByte
= fRead
.read(1)
224 self
.assertEqual(osByte
, '\xff') # OS "unknown" (OS-independent)
226 # Since the FNAME flag is set, the zero-terminated filename follows.
227 # RFC 1952 specifies that this is the name of the input file, if any.
228 # However, the gzip module defaults to storing the name of the output
229 # file in this field.
230 expected
= self
.filename
.encode('Latin-1') + '\x00'
231 nameBytes
= fRead
.read(len(expected
))
232 self
.assertEqual(nameBytes
, expected
)
234 # Since no other flags were set, the header ends here.
235 # Rather than process the compressed data, let's seek to the trailer.
236 fRead
.seek(os
.stat(self
.filename
).st_size
- 8)
238 crc32Bytes
= fRead
.read(4) # CRC32 of uncompressed data [data1]
239 self
.assertEqual(crc32Bytes
, '\xaf\xd7d\x83')
241 isizeBytes
= fRead
.read(4)
242 self
.assertEqual(isizeBytes
, struct
.pack('<i', len(data1
)))
244 def test_with_open(self
):
245 # GzipFile supports the context management protocol
246 with gzip
.GzipFile(self
.filename
, "wb") as f
:
248 f
= gzip
.GzipFile(self
.filename
, "rb")
256 self
.fail("__enter__ on a closed file didn't raise an exception")
258 with gzip
.GzipFile(self
.filename
, "wb") as f
:
260 except ZeroDivisionError:
263 self
.fail("1 // 0 didn't raise an exception")
265 def test_zero_padded_file(self
):
266 with gzip
.GzipFile(self
.filename
, "wb") as f
:
269 # Pad the file with zeroes
270 with
open(self
.filename
, "ab") as f
:
273 with gzip
.GzipFile(self
.filename
, "rb") as f
:
275 self
.assertEqual(d
, data1
* 50, "Incorrect data in file")
277 def test_main(verbose
=None):
278 test_support
.run_unittest(TestGzip
)
280 if __name__
== "__main__":
281 test_main(verbose
=True)