blob: 7e2186221671cf67d4fe4b4a5de832ae75363940 [file] [log] [blame]
Skip Montanaroc1b41542003-08-02 15:02:33 +00001#!/usr/bin/env python
2# -*- coding: iso-8859-1 -*-
3
4from test import test_support
Tim Peters82112372001-08-29 02:28:42 +00005import marshal
6import sys
Skip Montanaroc1b41542003-08-02 15:02:33 +00007import unittest
8import os
Tim Peters82112372001-08-29 02:28:42 +00009
Skip Montanaroc1b41542003-08-02 15:02:33 +000010class IntTestCase(unittest.TestCase):
11 def test_ints(self):
12 # Test the full range of Python ints.
13 n = sys.maxint
14 while n:
15 for expected in (-n, n):
16 s = marshal.dumps(expected)
17 got = marshal.loads(s)
18 self.assertEqual(expected, got)
19 marshal.dump(expected, file(test_support.TESTFN, "wb"))
20 got = marshal.load(file(test_support.TESTFN, "rb"))
21 self.assertEqual(expected, got)
22 n = n >> 1
23 os.unlink(test_support.TESTFN)
Tim Peters82112372001-08-29 02:28:42 +000024
Skip Montanaroc1b41542003-08-02 15:02:33 +000025 def test_int64(self):
26 # Simulate int marshaling on a 64-bit box. This is most interesting if
27 # we're running the test on a 32-bit box, of course.
28
29 def to_little_endian_string(value, nbytes):
30 bytes = []
31 for i in range(nbytes):
32 bytes.append(chr(value & 0xff))
33 value >>= 8
34 return ''.join(bytes)
35
36 maxint64 = (1L << 63) - 1
37 minint64 = -maxint64-1
38
39 for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
40 while base:
41 s = 'I' + to_little_endian_string(base, 8)
42 got = marshal.loads(s)
43 self.assertEqual(base, got)
44 if base == -1: # a fixed-point for shifting right 1
45 base = 0
46 else:
47 base >>= 1
48
49 def test_bool(self):
50 for b in (True, False):
51 new = marshal.loads(marshal.dumps(b))
52 self.assertEqual(b, new)
53 self.assertEqual(type(b), type(new))
54 marshal.dump(b, file(test_support.TESTFN, "wb"))
55 new = marshal.load(file(test_support.TESTFN, "rb"))
56 self.assertEqual(b, new)
57 self.assertEqual(type(b), type(new))
Tim Peters58eb11c2004-01-18 20:29:55 +000058
Skip Montanaroc1b41542003-08-02 15:02:33 +000059class FloatTestCase(unittest.TestCase):
60 def test_floats(self):
61 # Test a few floats
62 small = 1e-25
63 n = sys.maxint * 3.7e250
64 while n > small:
65 for expected in (-n, n):
66 f = float(expected)
67 s = marshal.dumps(f)
68 got = marshal.loads(s)
69 self.assertEqual(f, got)
70 marshal.dump(f, file(test_support.TESTFN, "wb"))
71 got = marshal.load(file(test_support.TESTFN, "rb"))
72 self.assertEqual(f, got)
73 n /= 123.4567
74
75 f = 0.0
Michael W. Hudsondf888462005-06-03 14:41:55 +000076 s = marshal.dumps(f, 2)
Tim Peters82112372001-08-29 02:28:42 +000077 got = marshal.loads(s)
Skip Montanaroc1b41542003-08-02 15:02:33 +000078 self.assertEqual(f, got)
Michael W. Hudsondf888462005-06-03 14:41:55 +000079 # and with version <= 1 (floats marshalled differently then)
80 s = marshal.dumps(f, 1)
Tim Peters5d36a552005-06-03 22:40:27 +000081 got = marshal.loads(s)
82 self.assertEqual(f, got)
Tim Peters82112372001-08-29 02:28:42 +000083
Skip Montanaroc1b41542003-08-02 15:02:33 +000084 n = sys.maxint * 3.7e-250
85 while n < small:
86 for expected in (-n, n):
87 f = float(expected)
Tim Peters5d36a552005-06-03 22:40:27 +000088
Skip Montanaroc1b41542003-08-02 15:02:33 +000089 s = marshal.dumps(f)
90 got = marshal.loads(s)
91 self.assertEqual(f, got)
Tim Peters5d36a552005-06-03 22:40:27 +000092
Michael W. Hudsondf888462005-06-03 14:41:55 +000093 s = marshal.dumps(f, 1)
94 got = marshal.loads(s)
95 self.assertEqual(f, got)
Tim Peters5d36a552005-06-03 22:40:27 +000096
Skip Montanaroc1b41542003-08-02 15:02:33 +000097 marshal.dump(f, file(test_support.TESTFN, "wb"))
98 got = marshal.load(file(test_support.TESTFN, "rb"))
99 self.assertEqual(f, got)
Tim Peters5d36a552005-06-03 22:40:27 +0000100
Michael W. Hudsondf888462005-06-03 14:41:55 +0000101 marshal.dump(f, file(test_support.TESTFN, "wb"), 1)
102 got = marshal.load(file(test_support.TESTFN, "rb"))
103 self.assertEqual(f, got)
Skip Montanaroc1b41542003-08-02 15:02:33 +0000104 n *= 123.4567
105 os.unlink(test_support.TESTFN)
Tim Peters82112372001-08-29 02:28:42 +0000106
Skip Montanaroc1b41542003-08-02 15:02:33 +0000107class StringTestCase(unittest.TestCase):
108 def test_unicode(self):
109 for s in [u"", u"Andrè Previn", u"abc", u" "*10000]:
110 new = marshal.loads(marshal.dumps(s))
111 self.assertEqual(s, new)
112 self.assertEqual(type(s), type(new))
113 marshal.dump(s, file(test_support.TESTFN, "wb"))
Skip Montanaro61aa6302005-06-04 12:55:32 +0000114 new = marshal.load(file(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000115 self.assertEqual(s, new)
116 self.assertEqual(type(s), type(new))
117 os.unlink(test_support.TESTFN)
Tim Peters82112372001-08-29 02:28:42 +0000118
Skip Montanaroc1b41542003-08-02 15:02:33 +0000119 def test_string(self):
120 for s in ["", "Andrè Previn", "abc", " "*10000]:
121 new = marshal.loads(marshal.dumps(s))
122 self.assertEqual(s, new)
123 self.assertEqual(type(s), type(new))
124 marshal.dump(s, file(test_support.TESTFN, "wb"))
Skip Montanaro61aa6302005-06-04 12:55:32 +0000125 new = marshal.load(file(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000126 self.assertEqual(s, new)
127 self.assertEqual(type(s), type(new))
128 os.unlink(test_support.TESTFN)
Tim Peters82112372001-08-29 02:28:42 +0000129
Skip Montanaroc1b41542003-08-02 15:02:33 +0000130 def test_buffer(self):
131 for s in ["", "Andrè Previn", "abc", " "*10000]:
Florent Xicluna07627882010-03-21 01:14:24 +0000132 with test_support.check_py3k_warnings(("buffer.. not supported",
133 DeprecationWarning)):
134 b = buffer(s)
Skip Montanaroc1b41542003-08-02 15:02:33 +0000135 new = marshal.loads(marshal.dumps(b))
136 self.assertEqual(s, new)
137 marshal.dump(b, file(test_support.TESTFN, "wb"))
Skip Montanaro61aa6302005-06-04 12:55:32 +0000138 new = marshal.load(file(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000139 self.assertEqual(s, new)
140 os.unlink(test_support.TESTFN)
Tim Peters58eb11c2004-01-18 20:29:55 +0000141
Skip Montanaroc1b41542003-08-02 15:02:33 +0000142class ExceptionTestCase(unittest.TestCase):
143 def test_exceptions(self):
144 new = marshal.loads(marshal.dumps(StopIteration))
145 self.assertEqual(StopIteration, new)
Thomas Heller3e1c18a2002-07-30 11:40:57 +0000146
Skip Montanaroc1b41542003-08-02 15:02:33 +0000147class CodeTestCase(unittest.TestCase):
148 def test_code(self):
149 co = ExceptionTestCase.test_exceptions.func_code
150 new = marshal.loads(marshal.dumps(co))
151 self.assertEqual(co, new)
152
153class ContainerTestCase(unittest.TestCase):
154 d = {'astring': 'foo@bar.baz.spam',
155 'afloat': 7283.43,
156 'anint': 2**20,
157 'ashortlong': 2L,
158 'alist': ['.zyx.41'],
159 'atuple': ('.zyx.41',)*10,
160 'aboolean': False,
161 'aunicode': u"Andrè Previn"
162 }
163 def test_dict(self):
164 new = marshal.loads(marshal.dumps(self.d))
165 self.assertEqual(self.d, new)
166 marshal.dump(self.d, file(test_support.TESTFN, "wb"))
Skip Montanaro61aa6302005-06-04 12:55:32 +0000167 new = marshal.load(file(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000168 self.assertEqual(self.d, new)
169 os.unlink(test_support.TESTFN)
Tim Peters58eb11c2004-01-18 20:29:55 +0000170
Skip Montanaroc1b41542003-08-02 15:02:33 +0000171 def test_list(self):
172 lst = self.d.items()
173 new = marshal.loads(marshal.dumps(lst))
174 self.assertEqual(lst, new)
175 marshal.dump(lst, file(test_support.TESTFN, "wb"))
Skip Montanaro61aa6302005-06-04 12:55:32 +0000176 new = marshal.load(file(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000177 self.assertEqual(lst, new)
178 os.unlink(test_support.TESTFN)
179
180 def test_tuple(self):
181 t = tuple(self.d.keys())
182 new = marshal.loads(marshal.dumps(t))
183 self.assertEqual(t, new)
184 marshal.dump(t, file(test_support.TESTFN, "wb"))
Skip Montanaro61aa6302005-06-04 12:55:32 +0000185 new = marshal.load(file(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000186 self.assertEqual(t, new)
187 os.unlink(test_support.TESTFN)
Tim Peters58eb11c2004-01-18 20:29:55 +0000188
Raymond Hettingera422c342005-01-11 03:03:27 +0000189 def test_sets(self):
190 for constructor in (set, frozenset):
191 t = constructor(self.d.keys())
192 new = marshal.loads(marshal.dumps(t))
193 self.assertEqual(t, new)
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000194 self.assertTrue(isinstance(new, constructor))
Raymond Hettingera422c342005-01-11 03:03:27 +0000195 self.assertNotEqual(id(t), id(new))
196 marshal.dump(t, file(test_support.TESTFN, "wb"))
Skip Montanaro61aa6302005-06-04 12:55:32 +0000197 new = marshal.load(file(test_support.TESTFN, "rb"))
Raymond Hettingera422c342005-01-11 03:03:27 +0000198 self.assertEqual(t, new)
199 os.unlink(test_support.TESTFN)
200
Skip Montanaroc1b41542003-08-02 15:02:33 +0000201class BugsTestCase(unittest.TestCase):
202 def test_bug_5888452(self):
203 # Simple-minded check for SF 588452: Debug build crashes
204 marshal.dumps([128] * 1000)
205
Armin Rigo01ab2792004-03-26 15:09:27 +0000206 def test_patch_873224(self):
207 self.assertRaises(Exception, marshal.loads, '0')
208 self.assertRaises(Exception, marshal.loads, 'f')
209 self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1])
210
Armin Rigo2ccea172004-12-20 12:25:57 +0000211 def test_version_argument(self):
212 # Python 2.4.0 crashes for any call to marshal.dumps(x, y)
213 self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5)
214 self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5)
215
Michael W. Hudsonf2ca5af2005-06-13 18:28:46 +0000216 def test_fuzz(self):
217 # simple test that it's at least not *totally* trivial to
218 # crash from bad marshal data
219 for c in [chr(i) for i in range(256)]:
220 try:
221 marshal.loads(c)
222 except Exception:
223 pass
224
Neal Norwitz6eb7bed2007-05-18 05:47:16 +0000225 def test_loads_recursion(self):
Neal Norwitzb1a9b372007-05-16 20:05:11 +0000226 s = 'c' + ('X' * 4*4) + '{' * 2**20
227 self.assertRaises(ValueError, marshal.loads, s)
228
Neal Norwitz6eb7bed2007-05-18 05:47:16 +0000229 def test_recursion_limit(self):
230 # Create a deeply nested structure.
231 head = last = []
232 # The max stack depth should match the value in Python/marshal.c.
233 MAX_MARSHAL_STACK_DEPTH = 2000
234 for i in range(MAX_MARSHAL_STACK_DEPTH - 2):
235 last.append([0])
236 last = last[-1]
237
238 # Verify we don't blow out the stack with dumps/load.
239 data = marshal.dumps(head)
240 new_head = marshal.loads(data)
241 # Don't use == to compare objects, it can exceed the recursion limit.
242 self.assertEqual(len(new_head), len(head))
243 self.assertEqual(len(new_head[0]), len(head[0]))
244 self.assertEqual(len(new_head[-1]), len(head[-1]))
245
246 last.append([0])
247 self.assertRaises(ValueError, marshal.dumps, head)
248
Raymond Hettinger12e94202007-11-07 01:13:09 +0000249 def test_exact_type_match(self):
250 # Former bug:
251 # >>> class Int(int): pass
252 # >>> type(loads(dumps(Int())))
253 # <type 'int'>
254 for typ in (int, long, float, complex, tuple, list, dict, set, frozenset):
255 # Note: str and unicode sublclasses are not tested because they get handled
256 # by marshal's routines for objects supporting the buffer API.
257 subtyp = type('subtyp', (typ,), {})
258 self.assertRaises(ValueError, marshal.dumps, subtyp())
259
Andrew M. Kuchling6c029162008-05-11 13:33:56 +0000260 # Issue #1792 introduced a change in how marshal increases the size of its
261 # internal buffer; this test ensures that the new code is exercised.
262 def test_large_marshal(self):
263 size = int(1e6)
264 testString = 'abc' * size
265 marshal.dumps(testString)
266
Mark Dickinson7e7a3ec2009-09-29 19:01:06 +0000267 def test_invalid_longs(self):
268 # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs
269 invalid_string = 'l\x02\x00\x00\x00\x00\x00\x00\x00'
270 self.assertRaises(ValueError, marshal.loads, invalid_string)
271
Andrew M. Kuchling6c029162008-05-11 13:33:56 +0000272
Skip Montanaroc1b41542003-08-02 15:02:33 +0000273def test_main():
274 test_support.run_unittest(IntTestCase,
275 FloatTestCase,
276 StringTestCase,
277 CodeTestCase,
278 ContainerTestCase,
279 ExceptionTestCase,
280 BugsTestCase)
281
282if __name__ == "__main__":
283 test_main()