/Lib/test/test_marshal.py

http://unladen-swallow.googlecode.com/ · Python · 281 lines · 220 code · 37 blank · 24 comment · 23 complexity · 1ce7ceda96c0a80c24508ddad7715d0b MD5 · raw file

  1. #!/usr/bin/env python
  2. # -*- coding: iso-8859-1 -*-
  3. from test import test_support
  4. import marshal
  5. import sys
  6. import unittest
  7. import os
  8. class IntTestCase(unittest.TestCase):
  9. def test_ints(self):
  10. # Test the full range of Python ints.
  11. n = sys.maxint
  12. while n:
  13. for expected in (-n, n):
  14. s = marshal.dumps(expected)
  15. got = marshal.loads(s)
  16. self.assertEqual(expected, got)
  17. marshal.dump(expected, file(test_support.TESTFN, "wb"))
  18. got = marshal.load(file(test_support.TESTFN, "rb"))
  19. self.assertEqual(expected, got)
  20. n = n >> 1
  21. os.unlink(test_support.TESTFN)
  22. def test_int64(self):
  23. # Simulate int marshaling on a 64-bit box. This is most interesting if
  24. # we're running the test on a 32-bit box, of course.
  25. def to_little_endian_string(value, nbytes):
  26. bytes = []
  27. for i in range(nbytes):
  28. bytes.append(chr(value & 0xff))
  29. value >>= 8
  30. return ''.join(bytes)
  31. maxint64 = (1L << 63) - 1
  32. minint64 = -maxint64-1
  33. for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
  34. while base:
  35. s = 'I' + to_little_endian_string(base, 8)
  36. got = marshal.loads(s)
  37. self.assertEqual(base, got)
  38. if base == -1: # a fixed-point for shifting right 1
  39. base = 0
  40. else:
  41. base >>= 1
  42. def test_bool(self):
  43. for b in (True, False):
  44. new = marshal.loads(marshal.dumps(b))
  45. self.assertEqual(b, new)
  46. self.assertEqual(type(b), type(new))
  47. marshal.dump(b, file(test_support.TESTFN, "wb"))
  48. new = marshal.load(file(test_support.TESTFN, "rb"))
  49. self.assertEqual(b, new)
  50. self.assertEqual(type(b), type(new))
  51. class FloatTestCase(unittest.TestCase):
  52. def test_floats(self):
  53. # Test a few floats
  54. small = 1e-25
  55. n = sys.maxint * 3.7e250
  56. while n > small:
  57. for expected in (-n, n):
  58. f = float(expected)
  59. s = marshal.dumps(f)
  60. got = marshal.loads(s)
  61. self.assertEqual(f, got)
  62. marshal.dump(f, file(test_support.TESTFN, "wb"))
  63. got = marshal.load(file(test_support.TESTFN, "rb"))
  64. self.assertEqual(f, got)
  65. n /= 123.4567
  66. f = 0.0
  67. s = marshal.dumps(f, 2)
  68. got = marshal.loads(s)
  69. self.assertEqual(f, got)
  70. # and with version <= 1 (floats marshalled differently then)
  71. s = marshal.dumps(f, 1)
  72. got = marshal.loads(s)
  73. self.assertEqual(f, got)
  74. n = sys.maxint * 3.7e-250
  75. while n < small:
  76. for expected in (-n, n):
  77. f = float(expected)
  78. s = marshal.dumps(f)
  79. got = marshal.loads(s)
  80. self.assertEqual(f, got)
  81. s = marshal.dumps(f, 1)
  82. got = marshal.loads(s)
  83. self.assertEqual(f, got)
  84. marshal.dump(f, file(test_support.TESTFN, "wb"))
  85. got = marshal.load(file(test_support.TESTFN, "rb"))
  86. self.assertEqual(f, got)
  87. marshal.dump(f, file(test_support.TESTFN, "wb"), 1)
  88. got = marshal.load(file(test_support.TESTFN, "rb"))
  89. self.assertEqual(f, got)
  90. n *= 123.4567
  91. os.unlink(test_support.TESTFN)
  92. class StringTestCase(unittest.TestCase):
  93. def test_unicode(self):
  94. for s in [u"", u"Andrč Previn", u"abc", u" "*10000]:
  95. new = marshal.loads(marshal.dumps(s))
  96. self.assertEqual(s, new)
  97. self.assertEqual(type(s), type(new))
  98. marshal.dump(s, file(test_support.TESTFN, "wb"))
  99. new = marshal.load(file(test_support.TESTFN, "rb"))
  100. self.assertEqual(s, new)
  101. self.assertEqual(type(s), type(new))
  102. os.unlink(test_support.TESTFN)
  103. def test_string(self):
  104. for s in ["", "Andrč Previn", "abc", " "*10000]:
  105. new = marshal.loads(marshal.dumps(s))
  106. self.assertEqual(s, new)
  107. self.assertEqual(type(s), type(new))
  108. marshal.dump(s, file(test_support.TESTFN, "wb"))
  109. new = marshal.load(file(test_support.TESTFN, "rb"))
  110. self.assertEqual(s, new)
  111. self.assertEqual(type(s), type(new))
  112. os.unlink(test_support.TESTFN)
  113. def test_buffer(self):
  114. for s in ["", "Andrč Previn", "abc", " "*10000]:
  115. b = buffer(s)
  116. new = marshal.loads(marshal.dumps(b))
  117. self.assertEqual(s, new)
  118. marshal.dump(b, file(test_support.TESTFN, "wb"))
  119. new = marshal.load(file(test_support.TESTFN, "rb"))
  120. self.assertEqual(s, new)
  121. os.unlink(test_support.TESTFN)
  122. class ExceptionTestCase(unittest.TestCase):
  123. def test_exceptions(self):
  124. new = marshal.loads(marshal.dumps(StopIteration))
  125. self.assertEqual(StopIteration, new)
  126. class CodeTestCase(unittest.TestCase):
  127. def test_code(self):
  128. co = ExceptionTestCase.test_exceptions.func_code
  129. new = marshal.loads(marshal.dumps(co))
  130. self.assertEqual(co, new)
  131. class ContainerTestCase(unittest.TestCase):
  132. d = {'astring': 'foo@bar.baz.spam',
  133. 'afloat': 7283.43,
  134. 'anint': 2**20,
  135. 'ashortlong': 2L,
  136. 'alist': ['.zyx.41'],
  137. 'atuple': ('.zyx.41',)*10,
  138. 'aboolean': False,
  139. 'aunicode': u"Andrč Previn"
  140. }
  141. def test_dict(self):
  142. new = marshal.loads(marshal.dumps(self.d))
  143. self.assertEqual(self.d, new)
  144. marshal.dump(self.d, file(test_support.TESTFN, "wb"))
  145. new = marshal.load(file(test_support.TESTFN, "rb"))
  146. self.assertEqual(self.d, new)
  147. os.unlink(test_support.TESTFN)
  148. def test_list(self):
  149. lst = self.d.items()
  150. new = marshal.loads(marshal.dumps(lst))
  151. self.assertEqual(lst, new)
  152. marshal.dump(lst, file(test_support.TESTFN, "wb"))
  153. new = marshal.load(file(test_support.TESTFN, "rb"))
  154. self.assertEqual(lst, new)
  155. os.unlink(test_support.TESTFN)
  156. def test_tuple(self):
  157. t = tuple(self.d.keys())
  158. new = marshal.loads(marshal.dumps(t))
  159. self.assertEqual(t, new)
  160. marshal.dump(t, file(test_support.TESTFN, "wb"))
  161. new = marshal.load(file(test_support.TESTFN, "rb"))
  162. self.assertEqual(t, new)
  163. os.unlink(test_support.TESTFN)
  164. def test_sets(self):
  165. for constructor in (set, frozenset):
  166. t = constructor(self.d.keys())
  167. new = marshal.loads(marshal.dumps(t))
  168. self.assertEqual(t, new)
  169. self.assert_(isinstance(new, constructor))
  170. self.assertNotEqual(id(t), id(new))
  171. marshal.dump(t, file(test_support.TESTFN, "wb"))
  172. new = marshal.load(file(test_support.TESTFN, "rb"))
  173. self.assertEqual(t, new)
  174. os.unlink(test_support.TESTFN)
  175. class BugsTestCase(unittest.TestCase):
  176. def test_bug_5888452(self):
  177. # Simple-minded check for SF 588452: Debug build crashes
  178. marshal.dumps([128] * 1000)
  179. def test_patch_873224(self):
  180. self.assertRaises(Exception, marshal.loads, '0')
  181. self.assertRaises(Exception, marshal.loads, 'f')
  182. self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1])
  183. def test_version_argument(self):
  184. # Python 2.4.0 crashes for any call to marshal.dumps(x, y)
  185. self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5)
  186. self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5)
  187. def test_fuzz(self):
  188. # simple test that it's at least not *totally* trivial to
  189. # crash from bad marshal data
  190. for c in [chr(i) for i in range(256)]:
  191. try:
  192. marshal.loads(c)
  193. except Exception:
  194. pass
  195. def test_loads_recursion(self):
  196. s = 'c' + ('X' * 4*4) + '{' * 2**20
  197. self.assertRaises(ValueError, marshal.loads, s)
  198. def test_recursion_limit(self):
  199. # Create a deeply nested structure.
  200. head = last = []
  201. # The max stack depth should match the value in Python/marshal.c.
  202. MAX_MARSHAL_STACK_DEPTH = 2000
  203. for i in range(MAX_MARSHAL_STACK_DEPTH - 2):
  204. last.append([0])
  205. last = last[-1]
  206. # Verify we don't blow out the stack with dumps/load.
  207. data = marshal.dumps(head)
  208. new_head = marshal.loads(data)
  209. # Don't use == to compare objects, it can exceed the recursion limit.
  210. self.assertEqual(len(new_head), len(head))
  211. self.assertEqual(len(new_head[0]), len(head[0]))
  212. self.assertEqual(len(new_head[-1]), len(head[-1]))
  213. last.append([0])
  214. self.assertRaises(ValueError, marshal.dumps, head)
  215. def test_exact_type_match(self):
  216. # Former bug:
  217. # >>> class Int(int): pass
  218. # >>> type(loads(dumps(Int())))
  219. # <type 'int'>
  220. for typ in (int, long, float, complex, tuple, list, dict, set, frozenset):
  221. # Note: str and unicode sublclasses are not tested because they get handled
  222. # by marshal's routines for objects supporting the buffer API.
  223. subtyp = type('subtyp', (typ,), {})
  224. self.assertRaises(ValueError, marshal.dumps, subtyp())
  225. # Issue #1792 introduced a change in how marshal increases the size of its
  226. # internal buffer; this test ensures that the new code is exercised.
  227. def test_large_marshal(self):
  228. size = int(1e6)
  229. testString = 'abc' * size
  230. marshal.dumps(testString)
  231. def test_invalid_longs(self):
  232. # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs
  233. invalid_string = 'l\x02\x00\x00\x00\x00\x00\x00\x00'
  234. self.assertRaises(ValueError, marshal.loads, invalid_string)
  235. def test_main():
  236. test_support.run_unittest(IntTestCase,
  237. FloatTestCase,
  238. StringTestCase,
  239. CodeTestCase,
  240. ContainerTestCase,
  241. ExceptionTestCase,
  242. BugsTestCase)
  243. if __name__ == "__main__":
  244. test_main()