blob: 70b192dbd709e368bf4088f179df763e3a3a07c3 [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)
Alex Martelli01c77c62006-08-24 02:58:11 +000019 marshal.dump(expected, open(test_support.TESTFN, "wb"))
20 got = marshal.load( open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +000021 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):
Guido van Rossume6d39042007-05-09 00:01:30 +000030 b = bytes()
Skip Montanaroc1b41542003-08-02 15:02:33 +000031 for i in range(nbytes):
Guido van Rossume6d39042007-05-09 00:01:30 +000032 b.append(value & 0xff)
Skip Montanaroc1b41542003-08-02 15:02:33 +000033 value >>= 8
Guido van Rossume6d39042007-05-09 00:01:30 +000034 return b
Skip Montanaroc1b41542003-08-02 15:02:33 +000035
Guido van Rossume2a383d2007-01-15 16:59:06 +000036 maxint64 = (1 << 63) - 1
Skip Montanaroc1b41542003-08-02 15:02:33 +000037 minint64 = -maxint64-1
38
39 for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
40 while base:
Guido van Rossume6d39042007-05-09 00:01:30 +000041 s = b'I' + to_little_endian_string(base, 8)
Skip Montanaroc1b41542003-08-02 15:02:33 +000042 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))
Alex Martelli01c77c62006-08-24 02:58:11 +000054 marshal.dump(b, open(test_support.TESTFN, "wb"))
55 new = marshal.load(open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +000056 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)
Alex Martelli01c77c62006-08-24 02:58:11 +000070 marshal.dump(f, open(test_support.TESTFN, "wb"))
71 got = marshal.load(open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +000072 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
Alex Martelli01c77c62006-08-24 02:58:11 +000097 marshal.dump(f, open(test_support.TESTFN, "wb"))
98 got = marshal.load(open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +000099 self.assertEqual(f, got)
Tim Peters5d36a552005-06-03 22:40:27 +0000100
Alex Martelli01c77c62006-08-24 02:58:11 +0000101 marshal.dump(f, open(test_support.TESTFN, "wb"), 1)
102 got = marshal.load(open(test_support.TESTFN, "rb"))
Michael W. Hudsondf888462005-06-03 14:41:55 +0000103 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):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000109 for s in ["", "Andrè Previn", "abc", " "*10000]:
Skip Montanaroc1b41542003-08-02 15:02:33 +0000110 new = marshal.loads(marshal.dumps(s))
111 self.assertEqual(s, new)
112 self.assertEqual(type(s), type(new))
Alex Martelli01c77c62006-08-24 02:58:11 +0000113 marshal.dump(s, open(test_support.TESTFN, "wb"))
114 new = marshal.load(open(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))
Alex Martelli01c77c62006-08-24 02:58:11 +0000124 marshal.dump(s, open(test_support.TESTFN, "wb"))
125 new = marshal.load(open(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):
Guido van Rossume6d39042007-05-09 00:01:30 +0000131 for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]:
Skip Montanaroc1b41542003-08-02 15:02:33 +0000132 b = buffer(s)
133 new = marshal.loads(marshal.dumps(b))
134 self.assertEqual(s, new)
Alex Martelli01c77c62006-08-24 02:58:11 +0000135 marshal.dump(b, open(test_support.TESTFN, "wb"))
136 new = marshal.load(open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000137 self.assertEqual(s, new)
138 os.unlink(test_support.TESTFN)
Tim Peters58eb11c2004-01-18 20:29:55 +0000139
Skip Montanaroc1b41542003-08-02 15:02:33 +0000140class ExceptionTestCase(unittest.TestCase):
141 def test_exceptions(self):
142 new = marshal.loads(marshal.dumps(StopIteration))
143 self.assertEqual(StopIteration, new)
Thomas Heller3e1c18a2002-07-30 11:40:57 +0000144
Skip Montanaroc1b41542003-08-02 15:02:33 +0000145class CodeTestCase(unittest.TestCase):
146 def test_code(self):
Neal Norwitz221085d2007-02-25 20:55:47 +0000147 co = ExceptionTestCase.test_exceptions.__code__
Skip Montanaroc1b41542003-08-02 15:02:33 +0000148 new = marshal.loads(marshal.dumps(co))
149 self.assertEqual(co, new)
150
151class ContainerTestCase(unittest.TestCase):
152 d = {'astring': 'foo@bar.baz.spam',
153 'afloat': 7283.43,
154 'anint': 2**20,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000155 'ashortlong': 2,
Skip Montanaroc1b41542003-08-02 15:02:33 +0000156 'alist': ['.zyx.41'],
157 'atuple': ('.zyx.41',)*10,
158 'aboolean': False,
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000159 'aunicode': "Andrè Previn"
Skip Montanaroc1b41542003-08-02 15:02:33 +0000160 }
161 def test_dict(self):
162 new = marshal.loads(marshal.dumps(self.d))
163 self.assertEqual(self.d, new)
Alex Martelli01c77c62006-08-24 02:58:11 +0000164 marshal.dump(self.d, open(test_support.TESTFN, "wb"))
165 new = marshal.load(open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000166 self.assertEqual(self.d, new)
167 os.unlink(test_support.TESTFN)
Tim Peters58eb11c2004-01-18 20:29:55 +0000168
Skip Montanaroc1b41542003-08-02 15:02:33 +0000169 def test_list(self):
Guido van Rossume19aad42007-02-12 00:23:56 +0000170 lst = list(self.d.items())
Skip Montanaroc1b41542003-08-02 15:02:33 +0000171 new = marshal.loads(marshal.dumps(lst))
172 self.assertEqual(lst, new)
Alex Martelli01c77c62006-08-24 02:58:11 +0000173 marshal.dump(lst, open(test_support.TESTFN, "wb"))
174 new = marshal.load(open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000175 self.assertEqual(lst, new)
176 os.unlink(test_support.TESTFN)
177
178 def test_tuple(self):
179 t = tuple(self.d.keys())
180 new = marshal.loads(marshal.dumps(t))
181 self.assertEqual(t, new)
Alex Martelli01c77c62006-08-24 02:58:11 +0000182 marshal.dump(t, open(test_support.TESTFN, "wb"))
183 new = marshal.load(open(test_support.TESTFN, "rb"))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000184 self.assertEqual(t, new)
185 os.unlink(test_support.TESTFN)
Tim Peters58eb11c2004-01-18 20:29:55 +0000186
Raymond Hettingera422c342005-01-11 03:03:27 +0000187 def test_sets(self):
188 for constructor in (set, frozenset):
189 t = constructor(self.d.keys())
190 new = marshal.loads(marshal.dumps(t))
191 self.assertEqual(t, new)
192 self.assert_(isinstance(new, constructor))
193 self.assertNotEqual(id(t), id(new))
Alex Martelli01c77c62006-08-24 02:58:11 +0000194 marshal.dump(t, open(test_support.TESTFN, "wb"))
195 new = marshal.load(open(test_support.TESTFN, "rb"))
Raymond Hettingera422c342005-01-11 03:03:27 +0000196 self.assertEqual(t, new)
197 os.unlink(test_support.TESTFN)
198
Skip Montanaroc1b41542003-08-02 15:02:33 +0000199class BugsTestCase(unittest.TestCase):
200 def test_bug_5888452(self):
201 # Simple-minded check for SF 588452: Debug build crashes
202 marshal.dumps([128] * 1000)
203
Armin Rigo01ab2792004-03-26 15:09:27 +0000204 def test_patch_873224(self):
205 self.assertRaises(Exception, marshal.loads, '0')
206 self.assertRaises(Exception, marshal.loads, 'f')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000207 self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65)[:-1])
Armin Rigo01ab2792004-03-26 15:09:27 +0000208
Armin Rigo2ccea172004-12-20 12:25:57 +0000209 def test_version_argument(self):
210 # Python 2.4.0 crashes for any call to marshal.dumps(x, y)
211 self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5)
212 self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5)
213
Michael W. Hudsonf2ca5af2005-06-13 18:28:46 +0000214 def test_fuzz(self):
215 # simple test that it's at least not *totally* trivial to
216 # crash from bad marshal data
217 for c in [chr(i) for i in range(256)]:
218 try:
219 marshal.loads(c)
220 except Exception:
221 pass
222
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000223 def test_loads_recursion(self):
224 s = 'c' + ('X' * 4*4) + '{' * 2**20
225 self.assertRaises(ValueError, marshal.loads, s)
226
227 def test_recursion_limit(self):
228 # Create a deeply nested structure.
229 head = last = []
230 # The max stack depth should match the value in Python/marshal.c.
231 MAX_MARSHAL_STACK_DEPTH = 2000
232 for i in range(MAX_MARSHAL_STACK_DEPTH - 2):
233 last.append([0])
234 last = last[-1]
235
236 # Verify we don't blow out the stack with dumps/load.
237 data = marshal.dumps(head)
238 new_head = marshal.loads(data)
239 # Don't use == to compare objects, it can exceed the recursion limit.
240 self.assertEqual(len(new_head), len(head))
241 self.assertEqual(len(new_head[0]), len(head[0]))
242 self.assertEqual(len(new_head[-1]), len(head[-1]))
243
244 last.append([0])
245 self.assertRaises(ValueError, marshal.dumps, head)
246
Skip Montanaroc1b41542003-08-02 15:02:33 +0000247def test_main():
248 test_support.run_unittest(IntTestCase,
249 FloatTestCase,
250 StringTestCase,
251 CodeTestCase,
252 ContainerTestCase,
253 ExceptionTestCase,
254 BugsTestCase)
255
256if __name__ == "__main__":
257 test_main()