blob: 83ab852beca51911704af62b4203a0eaa2a2aaea [file] [log] [blame]
Skip Montanaroc1b41542003-08-02 15:02:33 +00001#!/usr/bin/env python
Skip Montanaroc1b41542003-08-02 15:02:33 +00002
Benjamin Petersonee8712c2008-05-20 21:35:26 +00003from test import support
Tim Peters82112372001-08-29 02:28:42 +00004import marshal
5import sys
Skip Montanaroc1b41542003-08-02 15:02:33 +00006import unittest
7import os
Tim Peters82112372001-08-29 02:28:42 +00008
Guido van Rossum47f17d02007-07-10 11:37:44 +00009class HelperMixin:
10 def helper(self, sample, *extra):
11 new = marshal.loads(marshal.dumps(sample, *extra))
12 self.assertEqual(sample, new)
13 try:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000014 f = open(support.TESTFN, "wb")
Guido van Rossum47f17d02007-07-10 11:37:44 +000015 try:
16 marshal.dump(sample, f, *extra)
17 finally:
18 f.close()
Benjamin Petersonee8712c2008-05-20 21:35:26 +000019 f = open(support.TESTFN, "rb")
Guido van Rossum47f17d02007-07-10 11:37:44 +000020 try:
21 new = marshal.load(f)
22 finally:
23 f.close()
24 self.assertEqual(sample, new)
25 finally:
Benjamin Petersonee8712c2008-05-20 21:35:26 +000026 support.unlink(support.TESTFN)
Guido van Rossum47f17d02007-07-10 11:37:44 +000027
28class IntTestCase(unittest.TestCase, HelperMixin):
Skip Montanaroc1b41542003-08-02 15:02:33 +000029 def test_ints(self):
30 # Test the full range of Python ints.
Christian Heimesa37d4c62007-12-04 23:02:19 +000031 n = sys.maxsize
Skip Montanaroc1b41542003-08-02 15:02:33 +000032 while n:
33 for expected in (-n, n):
Guido van Rossum47f17d02007-07-10 11:37:44 +000034 self.helper(expected)
Skip Montanaroc1b41542003-08-02 15:02:33 +000035 n = n >> 1
Tim Peters82112372001-08-29 02:28:42 +000036
Skip Montanaroc1b41542003-08-02 15:02:33 +000037 def test_int64(self):
38 # Simulate int marshaling on a 64-bit box. This is most interesting if
39 # we're running the test on a 32-bit box, of course.
40
41 def to_little_endian_string(value, nbytes):
Guido van Rossum254348e2007-11-21 19:29:53 +000042 b = bytearray()
Skip Montanaroc1b41542003-08-02 15:02:33 +000043 for i in range(nbytes):
Guido van Rossume6d39042007-05-09 00:01:30 +000044 b.append(value & 0xff)
Skip Montanaroc1b41542003-08-02 15:02:33 +000045 value >>= 8
Guido van Rossume6d39042007-05-09 00:01:30 +000046 return b
Skip Montanaroc1b41542003-08-02 15:02:33 +000047
Guido van Rossume2a383d2007-01-15 16:59:06 +000048 maxint64 = (1 << 63) - 1
Skip Montanaroc1b41542003-08-02 15:02:33 +000049 minint64 = -maxint64-1
50
51 for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
52 while base:
Guido van Rossume6d39042007-05-09 00:01:30 +000053 s = b'I' + to_little_endian_string(base, 8)
Skip Montanaroc1b41542003-08-02 15:02:33 +000054 got = marshal.loads(s)
55 self.assertEqual(base, got)
56 if base == -1: # a fixed-point for shifting right 1
57 base = 0
58 else:
59 base >>= 1
60
61 def test_bool(self):
62 for b in (True, False):
Guido van Rossum47f17d02007-07-10 11:37:44 +000063 self.helper(b)
Tim Peters58eb11c2004-01-18 20:29:55 +000064
Guido van Rossum47f17d02007-07-10 11:37:44 +000065class FloatTestCase(unittest.TestCase, HelperMixin):
Skip Montanaroc1b41542003-08-02 15:02:33 +000066 def test_floats(self):
67 # Test a few floats
68 small = 1e-25
Christian Heimesa37d4c62007-12-04 23:02:19 +000069 n = sys.maxsize * 3.7e250
Skip Montanaroc1b41542003-08-02 15:02:33 +000070 while n > small:
71 for expected in (-n, n):
Guido van Rossum47f17d02007-07-10 11:37:44 +000072 self.helper(float(expected))
Skip Montanaroc1b41542003-08-02 15:02:33 +000073 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
Christian Heimesa37d4c62007-12-04 23:02:19 +000084 n = sys.maxsize * 3.7e-250
Skip Montanaroc1b41542003-08-02 15:02:33 +000085 while n < small:
86 for expected in (-n, n):
87 f = float(expected)
Guido van Rossum47f17d02007-07-10 11:37:44 +000088 self.helper(f)
89 self.helper(f, 1)
Skip Montanaroc1b41542003-08-02 15:02:33 +000090 n *= 123.4567
Tim Peters82112372001-08-29 02:28:42 +000091
Guido van Rossum47f17d02007-07-10 11:37:44 +000092class StringTestCase(unittest.TestCase, HelperMixin):
Skip Montanaroc1b41542003-08-02 15:02:33 +000093 def test_unicode(self):
Guido van Rossum47f17d02007-07-10 11:37:44 +000094 for s in ["", "Andr\xe8 Previn", "abc", " "*10000]:
95 self.helper(marshal.loads(marshal.dumps(s)))
Tim Peters82112372001-08-29 02:28:42 +000096
Skip Montanaroc1b41542003-08-02 15:02:33 +000097 def test_string(self):
Guido van Rossum47f17d02007-07-10 11:37:44 +000098 for s in ["", "Andr\xe8 Previn", "abc", " "*10000]:
99 self.helper(s)
Tim Peters82112372001-08-29 02:28:42 +0000100
Guido van Rossumbae07c92007-10-08 02:46:15 +0000101 def test_bytes(self):
Guido van Rossume6d39042007-05-09 00:01:30 +0000102 for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]:
Guido van Rossumbae07c92007-10-08 02:46:15 +0000103 self.helper(s)
Tim Peters58eb11c2004-01-18 20:29:55 +0000104
Skip Montanaroc1b41542003-08-02 15:02:33 +0000105class ExceptionTestCase(unittest.TestCase):
106 def test_exceptions(self):
107 new = marshal.loads(marshal.dumps(StopIteration))
108 self.assertEqual(StopIteration, new)
Thomas Heller3e1c18a2002-07-30 11:40:57 +0000109
Skip Montanaroc1b41542003-08-02 15:02:33 +0000110class CodeTestCase(unittest.TestCase):
111 def test_code(self):
Neal Norwitz221085d2007-02-25 20:55:47 +0000112 co = ExceptionTestCase.test_exceptions.__code__
Skip Montanaroc1b41542003-08-02 15:02:33 +0000113 new = marshal.loads(marshal.dumps(co))
114 self.assertEqual(co, new)
115
Guido van Rossum47f17d02007-07-10 11:37:44 +0000116class ContainerTestCase(unittest.TestCase, HelperMixin):
Skip Montanaroc1b41542003-08-02 15:02:33 +0000117 d = {'astring': 'foo@bar.baz.spam',
118 'afloat': 7283.43,
119 'anint': 2**20,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000120 'ashortlong': 2,
Skip Montanaroc1b41542003-08-02 15:02:33 +0000121 'alist': ['.zyx.41'],
122 'atuple': ('.zyx.41',)*10,
123 'aboolean': False,
Guido van Rossum47f17d02007-07-10 11:37:44 +0000124 'aunicode': "Andr\xe8 Previn"
Skip Montanaroc1b41542003-08-02 15:02:33 +0000125 }
Guido van Rossum47f17d02007-07-10 11:37:44 +0000126
Skip Montanaroc1b41542003-08-02 15:02:33 +0000127 def test_dict(self):
Guido van Rossum47f17d02007-07-10 11:37:44 +0000128 self.helper(self.d)
Tim Peters58eb11c2004-01-18 20:29:55 +0000129
Skip Montanaroc1b41542003-08-02 15:02:33 +0000130 def test_list(self):
Guido van Rossum47f17d02007-07-10 11:37:44 +0000131 self.helper(list(self.d.items()))
Skip Montanaroc1b41542003-08-02 15:02:33 +0000132
133 def test_tuple(self):
Guido van Rossum47f17d02007-07-10 11:37:44 +0000134 self.helper(tuple(self.d.keys()))
Tim Peters58eb11c2004-01-18 20:29:55 +0000135
Raymond Hettingera422c342005-01-11 03:03:27 +0000136 def test_sets(self):
137 for constructor in (set, frozenset):
Guido van Rossum47f17d02007-07-10 11:37:44 +0000138 self.helper(constructor(self.d.keys()))
Raymond Hettingera422c342005-01-11 03:03:27 +0000139
Skip Montanaroc1b41542003-08-02 15:02:33 +0000140class BugsTestCase(unittest.TestCase):
141 def test_bug_5888452(self):
142 # Simple-minded check for SF 588452: Debug build crashes
143 marshal.dumps([128] * 1000)
144
Armin Rigo01ab2792004-03-26 15:09:27 +0000145 def test_patch_873224(self):
146 self.assertRaises(Exception, marshal.loads, '0')
147 self.assertRaises(Exception, marshal.loads, 'f')
Guido van Rossume2a383d2007-01-15 16:59:06 +0000148 self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65)[:-1])
Armin Rigo01ab2792004-03-26 15:09:27 +0000149
Armin Rigo2ccea172004-12-20 12:25:57 +0000150 def test_version_argument(self):
151 # Python 2.4.0 crashes for any call to marshal.dumps(x, y)
152 self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5)
153 self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5)
154
Michael W. Hudsonf2ca5af2005-06-13 18:28:46 +0000155 def test_fuzz(self):
156 # simple test that it's at least not *totally* trivial to
157 # crash from bad marshal data
158 for c in [chr(i) for i in range(256)]:
159 try:
160 marshal.loads(c)
161 except Exception:
162 pass
163
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000164 def test_loads_recursion(self):
165 s = 'c' + ('X' * 4*4) + '{' * 2**20
166 self.assertRaises(ValueError, marshal.loads, s)
167
168 def test_recursion_limit(self):
169 # Create a deeply nested structure.
170 head = last = []
171 # The max stack depth should match the value in Python/marshal.c.
Guido van Rossum991bf5d2007-08-29 18:44:54 +0000172 if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'):
173 MAX_MARSHAL_STACK_DEPTH = 1500
174 else:
175 MAX_MARSHAL_STACK_DEPTH = 2000
Guido van Rossumd59da4b2007-05-22 18:11:13 +0000176 for i in range(MAX_MARSHAL_STACK_DEPTH - 2):
177 last.append([0])
178 last = last[-1]
179
180 # Verify we don't blow out the stack with dumps/load.
181 data = marshal.dumps(head)
182 new_head = marshal.loads(data)
183 # Don't use == to compare objects, it can exceed the recursion limit.
184 self.assertEqual(len(new_head), len(head))
185 self.assertEqual(len(new_head[0]), len(head[0]))
186 self.assertEqual(len(new_head[-1]), len(head[-1]))
187
188 last.append([0])
189 self.assertRaises(ValueError, marshal.dumps, head)
190
Guido van Rossum58da9312007-11-10 23:39:45 +0000191 def test_exact_type_match(self):
192 # Former bug:
193 # >>> class Int(int): pass
194 # >>> type(loads(dumps(Int())))
195 # <type 'int'>
196 for typ in (int, float, complex, tuple, list, dict, set, frozenset):
197 # Note: str sublclasses are not tested because they get handled
198 # by marshal's routines for objects supporting the buffer API.
199 subtyp = type('subtyp', (typ,), {})
200 self.assertRaises(ValueError, marshal.dumps, subtyp())
201
Alexandre Vassalotti5f8ced22008-05-16 00:03:33 +0000202 # Issue #1792 introduced a change in how marshal increases the size of its
203 # internal buffer; this test ensures that the new code is exercised.
204 def test_large_marshal(self):
205 size = int(1e6)
206 testString = 'abc' * size
207 marshal.dumps(testString)
208
209
Skip Montanaroc1b41542003-08-02 15:02:33 +0000210def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000211 support.run_unittest(IntTestCase,
Skip Montanaroc1b41542003-08-02 15:02:33 +0000212 FloatTestCase,
213 StringTestCase,
214 CodeTestCase,
215 ContainerTestCase,
216 ExceptionTestCase,
217 BugsTestCase)
218
219if __name__ == "__main__":
220 test_main()