PageRenderTime 51ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/External.LCA_RESTRICTED/Languages/IronPython/27/Lib/test/test_gzip.py

https://github.com/rpattabi/ironruby
Python | 271 lines | 266 code | 2 blank | 3 comment | 0 complexity | 90e070091f9474d1496ab3d0f5b9064a MD5 | raw file
  1. #! /usr/bin/env python
  2. """Test script for the gzip module.
  3. """
  4. import unittest
  5. from test import test_support
  6. import os
  7. import io
  8. import struct
  9. gzip = test_support.import_module('gzip')
  10. data1 = """ int length=DEFAULTALLOC, err = Z_OK;
  11. PyObject *RetVal;
  12. int flushmode = Z_FINISH;
  13. unsigned long start_total_out;
  14. """
  15. data2 = """/* zlibmodule.c -- gzip-compatible data compression */
  16. /* See http://www.gzip.org/zlib/
  17. /* See http://www.winimage.com/zLibDll for Windows */
  18. """
  19. class TestGzip(unittest.TestCase):
  20. filename = test_support.TESTFN
  21. def setUp(self):
  22. test_support.unlink(self.filename)
  23. def tearDown(self):
  24. test_support.unlink(self.filename)
  25. def test_write(self):
  26. f = gzip.GzipFile(self.filename, 'wb') ; f.write(data1 * 50)
  27. # Try flush and fileno.
  28. f.flush()
  29. f.fileno()
  30. if hasattr(os, 'fsync'):
  31. os.fsync(f.fileno())
  32. f.close()
  33. # Test multiple close() calls.
  34. f.close()
  35. def test_read(self):
  36. self.test_write()
  37. # Try reading.
  38. f = gzip.GzipFile(self.filename, 'r') ; d = f.read() ; f.close()
  39. self.assertEqual(d, data1*50)
  40. def test_append(self):
  41. self.test_write()
  42. # Append to the previous file
  43. f = gzip.GzipFile(self.filename, 'ab') ; f.write(data2 * 15) ; f.close()
  44. f = gzip.GzipFile(self.filename, 'rb') ; d = f.read() ; f.close()
  45. self.assertEqual(d, (data1*50) + (data2*15))
  46. def test_many_append(self):
  47. # Bug #1074261 was triggered when reading a file that contained
  48. # many, many members. Create such a file and verify that reading it
  49. # works.
  50. f = gzip.open(self.filename, 'wb', 9)
  51. f.write('a')
  52. f.close()
  53. for i in range(0,200):
  54. f = gzip.open(self.filename, "ab", 9) # append
  55. f.write('a')
  56. f.close()
  57. # Try reading the file
  58. zgfile = gzip.open(self.filename, "rb")
  59. contents = ""
  60. while 1:
  61. ztxt = zgfile.read(8192)
  62. contents += ztxt
  63. if not ztxt: break
  64. zgfile.close()
  65. self.assertEquals(contents, 'a'*201)
  66. def test_buffered_reader(self):
  67. # Issue #7471: a GzipFile can be wrapped in a BufferedReader for
  68. # performance.
  69. self.test_write()
  70. f = gzip.GzipFile(self.filename, 'rb')
  71. with io.BufferedReader(f) as r:
  72. lines = [line for line in r]
  73. self.assertEqual(lines, 50 * data1.splitlines(True))
  74. def test_readline(self):
  75. self.test_write()
  76. # Try .readline() with varying line lengths
  77. f = gzip.GzipFile(self.filename, 'rb')
  78. line_length = 0
  79. while 1:
  80. L = f.readline(line_length)
  81. if L == "" and line_length != 0: break
  82. self.assertTrue(len(L) <= line_length)
  83. line_length = (line_length + 1) % 50
  84. f.close()
  85. def test_readlines(self):
  86. self.test_write()
  87. # Try .readlines()
  88. f = gzip.GzipFile(self.filename, 'rb')
  89. L = f.readlines()
  90. f.close()
  91. f = gzip.GzipFile(self.filename, 'rb')
  92. while 1:
  93. L = f.readlines(150)
  94. if L == []: break
  95. f.close()
  96. def test_seek_read(self):
  97. self.test_write()
  98. # Try seek, read test
  99. f = gzip.GzipFile(self.filename)
  100. while 1:
  101. oldpos = f.tell()
  102. line1 = f.readline()
  103. if not line1: break
  104. newpos = f.tell()
  105. f.seek(oldpos) # negative seek
  106. if len(line1)>10:
  107. amount = 10
  108. else:
  109. amount = len(line1)
  110. line2 = f.read(amount)
  111. self.assertEqual(line1[:amount], line2)
  112. f.seek(newpos) # positive seek
  113. f.close()
  114. def test_seek_whence(self):
  115. self.test_write()
  116. # Try seek(whence=1), read test
  117. f = gzip.GzipFile(self.filename)
  118. f.read(10)
  119. f.seek(10, whence=1)
  120. y = f.read(10)
  121. f.close()
  122. self.assertEquals(y, data1[20:30])
  123. def test_seek_write(self):
  124. # Try seek, write test
  125. f = gzip.GzipFile(self.filename, 'w')
  126. for pos in range(0, 256, 16):
  127. f.seek(pos)
  128. f.write('GZ\n')
  129. f.close()
  130. def test_mode(self):
  131. self.test_write()
  132. f = gzip.GzipFile(self.filename, 'r')
  133. self.assertEqual(f.myfileobj.mode, 'rb')
  134. f.close()
  135. def test_1647484(self):
  136. for mode in ('wb', 'rb'):
  137. f = gzip.GzipFile(self.filename, mode)
  138. self.assertTrue(hasattr(f, "name"))
  139. self.assertEqual(f.name, self.filename)
  140. f.close()
  141. def test_mtime(self):
  142. mtime = 123456789
  143. fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
  144. fWrite.write(data1)
  145. fWrite.close()
  146. fRead = gzip.GzipFile(self.filename)
  147. dataRead = fRead.read()
  148. self.assertEqual(dataRead, data1)
  149. self.assertTrue(hasattr(fRead, 'mtime'))
  150. self.assertEqual(fRead.mtime, mtime)
  151. fRead.close()
  152. def test_metadata(self):
  153. mtime = 123456789
  154. fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
  155. fWrite.write(data1)
  156. fWrite.close()
  157. fRead = open(self.filename, 'rb')
  158. # see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
  159. idBytes = fRead.read(2)
  160. self.assertEqual(idBytes, '\x1f\x8b') # gzip ID
  161. cmByte = fRead.read(1)
  162. self.assertEqual(cmByte, '\x08') # deflate
  163. flagsByte = fRead.read(1)
  164. self.assertEqual(flagsByte, '\x08') # only the FNAME flag is set
  165. mtimeBytes = fRead.read(4)
  166. self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
  167. xflByte = fRead.read(1)
  168. self.assertEqual(xflByte, '\x02') # maximum compression
  169. osByte = fRead.read(1)
  170. self.assertEqual(osByte, '\xff') # OS "unknown" (OS-independent)
  171. # Since the FNAME flag is set, the zero-terminated filename follows.
  172. # RFC 1952 specifies that this is the name of the input file, if any.
  173. # However, the gzip module defaults to storing the name of the output
  174. # file in this field.
  175. nameBytes = fRead.read(len(self.filename) + 1)
  176. self.assertEqual(nameBytes, self.filename + '\x00')
  177. # Since no other flags were set, the header ends here.
  178. # Rather than process the compressed data, let's seek to the trailer.
  179. fRead.seek(os.stat(self.filename).st_size - 8)
  180. crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
  181. self.assertEqual(crc32Bytes, '\xaf\xd7d\x83')
  182. isizeBytes = fRead.read(4)
  183. self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
  184. fRead.close()
  185. def test_with_open(self):
  186. # GzipFile supports the context management protocol
  187. with gzip.GzipFile(self.filename, "wb") as f:
  188. f.write(b"xxx")
  189. f = gzip.GzipFile(self.filename, "rb")
  190. f.close()
  191. try:
  192. with f:
  193. pass
  194. except ValueError:
  195. pass
  196. else:
  197. self.fail("__enter__ on a closed file didn't raise an exception")
  198. try:
  199. with gzip.GzipFile(self.filename, "wb") as f:
  200. 1 // 0
  201. except ZeroDivisionError:
  202. pass
  203. else:
  204. self.fail("1 // 0 didn't raise an exception")
  205. def test_zero_padded_file(self):
  206. with gzip.GzipFile(self.filename, "wb") as f:
  207. f.write(data1 * 50)
  208. # Pad the file with zeroes
  209. with open(self.filename, "ab") as f:
  210. f.write("\x00" * 50)
  211. with gzip.GzipFile(self.filename, "rb") as f:
  212. d = f.read()
  213. self.assertEqual(d, data1 * 50, "Incorrect data in file")
  214. def test_main(verbose=None):
  215. test_support.run_unittest(TestGzip)
  216. if __name__ == "__main__":
  217. test_main(verbose=True)