1 #! /usr/bin/env python 2 """Test script for the gzip module. 3 """ 4 5 import unittest 6 from test import test_support 7 import os 8 import io 9 import struct 10 gzip = test_support.import_module('gzip') 11 12 data1 = """ int length=DEFAULTALLOC, err = Z_OK; 13 PyObject *RetVal; 14 int flushmode = Z_FINISH; 15 unsigned long start_total_out; 16 17 """ 18 19 data2 = """/* zlibmodule.c -- gzip-compatible data compression */ 20 /* See http://www.gzip.org/zlib/ 21 /* See http://www.winimage.com/zLibDll for Windows */ 22 """ 23 24 25 class TestGzip(unittest.TestCase): 26 filename = test_support.TESTFN 27 28 def setUp(self): 29 test_support.unlink(self.filename) 30 31 def tearDown(self): 32 test_support.unlink(self.filename) 33 34 35 def test_write(self): 36 with gzip.GzipFile(self.filename, 'wb') as f: 37 f.write(data1 * 50) 38 39 # Try flush and fileno. 40 f.flush() 41 f.fileno() 42 if hasattr(os, 'fsync'): 43 os.fsync(f.fileno()) 44 f.close() 45 46 # Test multiple close() calls. 47 f.close() 48 49 def test_read(self): 50 self.test_write() 51 # Try reading. 52 with gzip.GzipFile(self.filename, 'r') as f: 53 d = f.read() 54 self.assertEqual(d, data1*50) 55 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. 59 60 # Write to a file, open it for reading, then close it. 61 self.test_write() 62 f = gzip.GzipFile(self.filename, 'r') 63 f.close() 64 with self.assertRaises(ValueError): 65 f.read(1) 66 with self.assertRaises(ValueError): 67 f.seek(0) 68 with self.assertRaises(ValueError): 69 f.tell() 70 # Open the file for writing, then close it. 71 f = gzip.GzipFile(self.filename, 'w') 72 f.close() 73 with self.assertRaises(ValueError): 74 f.write('') 75 with self.assertRaises(ValueError): 76 f.flush() 77 78 def test_append(self): 79 self.test_write() 80 # Append to the previous file 81 with gzip.GzipFile(self.filename, 'ab') as f: 82 f.write(data2 * 15) 83 84 with gzip.GzipFile(self.filename, 'rb') as f: 85 d = f.read() 86 self.assertEqual(d, (data1*50) + (data2*15)) 87 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 91 # works. 92 with gzip.open(self.filename, 'wb', 9) as f: 93 f.write('a') 94 for i in range(0, 200): 95 with gzip.open(self.filename, "ab", 9) as f: # append 96 f.write('a') 97 98 # Try reading the file 99 with gzip.open(self.filename, "rb") as zgfile: 100 contents = "" 101 while 1: 102 ztxt = zgfile.read(8192) 103 contents += ztxt 104 if not ztxt: break 105 self.assertEqual(contents, 'a'*201) 106 107 def test_buffered_reader(self): 108 # Issue #7471: a GzipFile can be wrapped in a BufferedReader for 109 # performance. 110 self.test_write() 111 112 with gzip.GzipFile(self.filename, 'rb') as f: 113 with io.BufferedReader(f) as r: 114 lines = [line for line in r] 115 116 self.assertEqual(lines, 50 * data1.splitlines(True)) 117 118 def test_readline(self): 119 self.test_write() 120 # Try .readline() with varying line lengths 121 122 with gzip.GzipFile(self.filename, 'rb') as f: 123 line_length = 0 124 while 1: 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 129 130 def test_readlines(self): 131 self.test_write() 132 # Try .readlines() 133 134 with gzip.GzipFile(self.filename, 'rb') as f: 135 L = f.readlines() 136 137 with gzip.GzipFile(self.filename, 'rb') as f: 138 while 1: 139 L = f.readlines(150) 140 if L == []: break 141 142 def test_seek_read(self): 143 self.test_write() 144 # Try seek, read test 145 146 with gzip.GzipFile(self.filename) as f: 147 while 1: 148 oldpos = f.tell() 149 line1 = f.readline() 150 if not line1: break 151 newpos = f.tell() 152 f.seek(oldpos) # negative seek 153 if len(line1)>10: 154 amount = 10 155 else: 156 amount = len(line1) 157 line2 = f.read(amount) 158 self.assertEqual(line1[:amount], line2) 159 f.seek(newpos) # positive seek 160 161 def test_seek_whence(self): 162 self.test_write() 163 # Try seek(whence=1), read test 164 165 with gzip.GzipFile(self.filename) as f: 166 f.read(10) 167 f.seek(10, whence=1) 168 y = f.read(10) 169 self.assertEqual(y, data1[20:30]) 170 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): 175 f.seek(pos) 176 f.write('GZ\n') 177 178 def test_mode(self): 179 self.test_write() 180 with gzip.GzipFile(self.filename, 'r') as f: 181 self.assertEqual(f.myfileobj.mode, 'rb') 182 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) 188 189 def test_mtime(self): 190 mtime = 123456789 191 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: 192 fWrite.write(data1) 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) 198 199 def test_metadata(self): 200 mtime = 123456789 201 202 with gzip.GzipFile(self.filename, 'w', mtime = mtime) as fWrite: 203 fWrite.write(data1) 204 205 with open(self.filename, 'rb') as fRead: 206 # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html 207 208 idBytes = fRead.read(2) 209 self.assertEqual(idBytes, '\x1f\x8b') # gzip ID 210 211 cmByte = fRead.read(1) 212 self.assertEqual(cmByte, '\x08') # deflate 213 214 flagsByte = fRead.read(1) 215 self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set 216 217 mtimeBytes = fRead.read(4) 218 self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian 219 220 xflByte = fRead.read(1) 221 self.assertEqual(xflByte, '\x02') # maximum compression 222 223 osByte = fRead.read(1) 224 self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent) 225 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) 233 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) 237 238 crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1] 239 self.assertEqual(crc32Bytes, '\xaf\xd7d\x83') 240 241 isizeBytes = fRead.read(4) 242 self.assertEqual(isizeBytes, struct.pack('<i', len(data1))) 243 244 def test_with_open(self): 245 # GzipFile supports the context management protocol 246 with gzip.GzipFile(self.filename, "wb") as f: 247 f.write(b"xxx") 248 f = gzip.GzipFile(self.filename, "rb") 249 f.close() 250 try: 251 with f: 252 pass 253 except ValueError: 254 pass 255 else: 256 self.fail("__enter__ on a closed file didn't raise an exception") 257 try: 258 with gzip.GzipFile(self.filename, "wb") as f: 259 1 // 0 260 except ZeroDivisionError: 261 pass 262 else: 263 self.fail("1 // 0 didn't raise an exception") 264 265 def test_zero_padded_file(self): 266 with gzip.GzipFile(self.filename, "wb") as f: 267 f.write(data1 * 50) 268 269 # Pad the file with zeroes 270 with open(self.filename, "ab") as f: 271 f.write("\x00" * 50) 272 273 with gzip.GzipFile(self.filename, "rb") as f: 274 d = f.read() 275 self.assertEqual(d, data1 * 50, "Incorrect data in file") 276 277 def test_main(verbose=None): 278 test_support.run_unittest(TestGzip) 279 280 if __name__ == "__main__": 281 test_main(verbose=True) 282