Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from test import support |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 4 | import marshal |
| 5 | import sys |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | import os |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 8 | import types |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 10 | class HelperMixin: |
| 11 | def helper(self, sample, *extra): |
| 12 | new = marshal.loads(marshal.dumps(sample, *extra)) |
| 13 | self.assertEqual(sample, new) |
| 14 | try: |
Brian Curtin | 2c3563f | 2010-10-13 02:40:26 +0000 | [diff] [blame] | 15 | with open(support.TESTFN, "wb") as f: |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 16 | marshal.dump(sample, f, *extra) |
Brian Curtin | 2c3563f | 2010-10-13 02:40:26 +0000 | [diff] [blame] | 17 | with open(support.TESTFN, "rb") as f: |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 18 | new = marshal.load(f) |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 19 | self.assertEqual(sample, new) |
| 20 | finally: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 21 | support.unlink(support.TESTFN) |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 22 | |
| 23 | class IntTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 24 | def test_ints(self): |
| 25 | # Test the full range of Python ints. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 26 | n = sys.maxsize |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 27 | while n: |
| 28 | for expected in (-n, n): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 29 | self.helper(expected) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 30 | n = n >> 1 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 31 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 32 | def test_int64(self): |
| 33 | # Simulate int marshaling on a 64-bit box. This is most interesting if |
| 34 | # we're running the test on a 32-bit box, of course. |
| 35 | |
| 36 | def to_little_endian_string(value, nbytes): |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 37 | b = bytearray() |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 38 | for i in range(nbytes): |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 39 | b.append(value & 0xff) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 40 | value >>= 8 |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 41 | return b |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 43 | maxint64 = (1 << 63) - 1 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 44 | minint64 = -maxint64-1 |
| 45 | |
| 46 | for base in maxint64, minint64, -maxint64, -(minint64 >> 1): |
| 47 | while base: |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 48 | s = b'I' + to_little_endian_string(base, 8) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 49 | got = marshal.loads(s) |
| 50 | self.assertEqual(base, got) |
| 51 | if base == -1: # a fixed-point for shifting right 1 |
| 52 | base = 0 |
| 53 | else: |
| 54 | base >>= 1 |
| 55 | |
| 56 | def test_bool(self): |
| 57 | for b in (True, False): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 58 | self.helper(b) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 59 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 60 | class FloatTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 61 | def test_floats(self): |
| 62 | # Test a few floats |
| 63 | small = 1e-25 |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 64 | n = sys.maxsize * 3.7e250 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 65 | while n > small: |
| 66 | for expected in (-n, n): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 67 | self.helper(float(expected)) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 68 | n /= 123.4567 |
| 69 | |
| 70 | f = 0.0 |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 71 | s = marshal.dumps(f, 2) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 72 | got = marshal.loads(s) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 73 | self.assertEqual(f, got) |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 74 | # and with version <= 1 (floats marshalled differently then) |
| 75 | s = marshal.dumps(f, 1) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 76 | got = marshal.loads(s) |
| 77 | self.assertEqual(f, got) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 78 | |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 79 | n = sys.maxsize * 3.7e-250 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 80 | while n < small: |
| 81 | for expected in (-n, n): |
| 82 | f = float(expected) |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 83 | self.helper(f) |
| 84 | self.helper(f, 1) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 85 | n *= 123.4567 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 86 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 87 | class StringTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 88 | def test_unicode(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 89 | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
| 90 | self.helper(marshal.loads(marshal.dumps(s))) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 91 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 92 | def test_string(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 93 | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
| 94 | self.helper(s) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 95 | |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 96 | def test_bytes(self): |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 97 | for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]: |
Guido van Rossum | bae07c9 | 2007-10-08 02:46:15 +0000 | [diff] [blame] | 98 | self.helper(s) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 99 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 100 | class ExceptionTestCase(unittest.TestCase): |
| 101 | def test_exceptions(self): |
| 102 | new = marshal.loads(marshal.dumps(StopIteration)) |
| 103 | self.assertEqual(StopIteration, new) |
Thomas Heller | 3e1c18a | 2002-07-30 11:40:57 +0000 | [diff] [blame] | 104 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 105 | class CodeTestCase(unittest.TestCase): |
| 106 | def test_code(self): |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 107 | co = ExceptionTestCase.test_exceptions.__code__ |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 108 | new = marshal.loads(marshal.dumps(co)) |
| 109 | self.assertEqual(co, new) |
| 110 | |
Amaury Forgeot d'Arc | 74c71f5 | 2008-05-26 21:41:42 +0000 | [diff] [blame] | 111 | def test_many_codeobjects(self): |
| 112 | # Issue2957: bad recursion count on code objects |
| 113 | count = 5000 # more than MAX_MARSHAL_STACK_DEPTH |
| 114 | codes = (ExceptionTestCase.test_exceptions.__code__,) * count |
| 115 | marshal.loads(marshal.dumps(codes)) |
| 116 | |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 117 | def test_different_filenames(self): |
| 118 | co1 = compile("x", "f1", "exec") |
| 119 | co2 = compile("y", "f2", "exec") |
| 120 | co1, co2 = marshal.loads(marshal.dumps((co1, co2))) |
| 121 | self.assertEqual(co1.co_filename, "f1") |
| 122 | self.assertEqual(co2.co_filename, "f2") |
| 123 | |
| 124 | @support.cpython_only |
| 125 | def test_same_filename_used(self): |
| 126 | s = """def f(): pass\ndef g(): pass""" |
| 127 | co = compile(s, "myfile", "exec") |
| 128 | co = marshal.loads(marshal.dumps(co)) |
| 129 | for obj in co.co_consts: |
| 130 | if isinstance(obj, types.CodeType): |
| 131 | self.assertIs(co.co_filename, obj.co_filename) |
| 132 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 133 | class ContainerTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 134 | d = {'astring': 'foo@bar.baz.spam', |
| 135 | 'afloat': 7283.43, |
| 136 | 'anint': 2**20, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 137 | 'ashortlong': 2, |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 138 | 'alist': ['.zyx.41'], |
| 139 | 'atuple': ('.zyx.41',)*10, |
| 140 | 'aboolean': False, |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 141 | 'aunicode': "Andr\xe8 Previn" |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 142 | } |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 143 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 144 | def test_dict(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 145 | self.helper(self.d) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 146 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 147 | def test_list(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 148 | self.helper(list(self.d.items())) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 149 | |
| 150 | def test_tuple(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 151 | self.helper(tuple(self.d.keys())) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 152 | |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 153 | def test_sets(self): |
| 154 | for constructor in (set, frozenset): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 155 | self.helper(constructor(self.d.keys())) |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 156 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 157 | class BugsTestCase(unittest.TestCase): |
| 158 | def test_bug_5888452(self): |
| 159 | # Simple-minded check for SF 588452: Debug build crashes |
| 160 | marshal.dumps([128] * 1000) |
| 161 | |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 162 | def test_patch_873224(self): |
| 163 | self.assertRaises(Exception, marshal.loads, '0') |
| 164 | self.assertRaises(Exception, marshal.loads, 'f') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 165 | self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65)[:-1]) |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 166 | |
Armin Rigo | 2ccea17 | 2004-12-20 12:25:57 +0000 | [diff] [blame] | 167 | def test_version_argument(self): |
| 168 | # Python 2.4.0 crashes for any call to marshal.dumps(x, y) |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 169 | self.assertEqual(marshal.loads(marshal.dumps(5, 0)), 5) |
| 170 | self.assertEqual(marshal.loads(marshal.dumps(5, 1)), 5) |
Armin Rigo | 2ccea17 | 2004-12-20 12:25:57 +0000 | [diff] [blame] | 171 | |
Michael W. Hudson | f2ca5af | 2005-06-13 18:28:46 +0000 | [diff] [blame] | 172 | def test_fuzz(self): |
| 173 | # simple test that it's at least not *totally* trivial to |
| 174 | # crash from bad marshal data |
| 175 | for c in [chr(i) for i in range(256)]: |
| 176 | try: |
| 177 | marshal.loads(c) |
| 178 | except Exception: |
| 179 | pass |
| 180 | |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 181 | def test_loads_recursion(self): |
| 182 | s = 'c' + ('X' * 4*4) + '{' * 2**20 |
| 183 | self.assertRaises(ValueError, marshal.loads, s) |
| 184 | |
| 185 | def test_recursion_limit(self): |
| 186 | # Create a deeply nested structure. |
| 187 | head = last = [] |
| 188 | # The max stack depth should match the value in Python/marshal.c. |
Guido van Rossum | 991bf5d | 2007-08-29 18:44:54 +0000 | [diff] [blame] | 189 | if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'): |
| 190 | MAX_MARSHAL_STACK_DEPTH = 1500 |
| 191 | else: |
| 192 | MAX_MARSHAL_STACK_DEPTH = 2000 |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 193 | for i in range(MAX_MARSHAL_STACK_DEPTH - 2): |
| 194 | last.append([0]) |
| 195 | last = last[-1] |
| 196 | |
| 197 | # Verify we don't blow out the stack with dumps/load. |
| 198 | data = marshal.dumps(head) |
| 199 | new_head = marshal.loads(data) |
| 200 | # Don't use == to compare objects, it can exceed the recursion limit. |
| 201 | self.assertEqual(len(new_head), len(head)) |
| 202 | self.assertEqual(len(new_head[0]), len(head[0])) |
| 203 | self.assertEqual(len(new_head[-1]), len(head[-1])) |
| 204 | |
| 205 | last.append([0]) |
| 206 | self.assertRaises(ValueError, marshal.dumps, head) |
| 207 | |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 208 | def test_exact_type_match(self): |
| 209 | # Former bug: |
| 210 | # >>> class Int(int): pass |
| 211 | # >>> type(loads(dumps(Int()))) |
| 212 | # <type 'int'> |
| 213 | for typ in (int, float, complex, tuple, list, dict, set, frozenset): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 214 | # Note: str subclasses are not tested because they get handled |
Guido van Rossum | 58da931 | 2007-11-10 23:39:45 +0000 | [diff] [blame] | 215 | # by marshal's routines for objects supporting the buffer API. |
| 216 | subtyp = type('subtyp', (typ,), {}) |
| 217 | self.assertRaises(ValueError, marshal.dumps, subtyp()) |
| 218 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 219 | # Issue #1792 introduced a change in how marshal increases the size of its |
| 220 | # internal buffer; this test ensures that the new code is exercised. |
| 221 | def test_large_marshal(self): |
| 222 | size = int(1e6) |
| 223 | testString = 'abc' * size |
| 224 | marshal.dumps(testString) |
| 225 | |
Mark Dickinson | 2683ab0 | 2009-09-29 19:21:35 +0000 | [diff] [blame] | 226 | def test_invalid_longs(self): |
| 227 | # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs |
| 228 | invalid_string = b'l\x02\x00\x00\x00\x00\x00\x00\x00' |
| 229 | self.assertRaises(ValueError, marshal.loads, invalid_string) |
| 230 | |
Alexandre Vassalotti | 5f8ced2 | 2008-05-16 00:03:33 +0000 | [diff] [blame] | 231 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 232 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 233 | support.run_unittest(IntTestCase, |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 234 | FloatTestCase, |
| 235 | StringTestCase, |
| 236 | CodeTestCase, |
| 237 | ContainerTestCase, |
| 238 | ExceptionTestCase, |
| 239 | BugsTestCase) |
| 240 | |
| 241 | if __name__ == "__main__": |
| 242 | test_main() |