Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 2 | |
| 3 | from test import test_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 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 9 | class HelperMixin: |
| 10 | def helper(self, sample, *extra): |
| 11 | new = marshal.loads(marshal.dumps(sample, *extra)) |
| 12 | self.assertEqual(sample, new) |
| 13 | try: |
| 14 | f = open(test_support.TESTFN, "wb") |
| 15 | try: |
| 16 | marshal.dump(sample, f, *extra) |
| 17 | finally: |
| 18 | f.close() |
| 19 | f = open(test_support.TESTFN, "rb") |
| 20 | try: |
| 21 | new = marshal.load(f) |
| 22 | finally: |
| 23 | f.close() |
| 24 | self.assertEqual(sample, new) |
| 25 | finally: |
| 26 | test_support.unlink(test_support.TESTFN) |
| 27 | |
| 28 | class IntTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 29 | def test_ints(self): |
| 30 | # Test the full range of Python ints. |
| 31 | n = sys.maxint |
| 32 | while n: |
| 33 | for expected in (-n, n): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 34 | self.helper(expected) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 35 | n = n >> 1 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 36 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 37 | 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 Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 42 | b = bytes() |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 43 | for i in range(nbytes): |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 44 | b.append(value & 0xff) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 45 | value >>= 8 |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 46 | return b |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 47 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 48 | maxint64 = (1 << 63) - 1 |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 49 | minint64 = -maxint64-1 |
| 50 | |
| 51 | for base in maxint64, minint64, -maxint64, -(minint64 >> 1): |
| 52 | while base: |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 53 | s = b'I' + to_little_endian_string(base, 8) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 54 | 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 Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 63 | self.helper(b) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 65 | class FloatTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 66 | def test_floats(self): |
| 67 | # Test a few floats |
| 68 | small = 1e-25 |
| 69 | n = sys.maxint * 3.7e250 |
| 70 | while n > small: |
| 71 | for expected in (-n, n): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 72 | self.helper(float(expected)) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 73 | n /= 123.4567 |
| 74 | |
| 75 | f = 0.0 |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 76 | s = marshal.dumps(f, 2) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 77 | got = marshal.loads(s) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 78 | self.assertEqual(f, got) |
Michael W. Hudson | df88846 | 2005-06-03 14:41:55 +0000 | [diff] [blame] | 79 | # and with version <= 1 (floats marshalled differently then) |
| 80 | s = marshal.dumps(f, 1) |
Tim Peters | 5d36a55 | 2005-06-03 22:40:27 +0000 | [diff] [blame] | 81 | got = marshal.loads(s) |
| 82 | self.assertEqual(f, got) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 83 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 84 | n = sys.maxint * 3.7e-250 |
| 85 | while n < small: |
| 86 | for expected in (-n, n): |
| 87 | f = float(expected) |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 88 | self.helper(f) |
| 89 | self.helper(f, 1) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 90 | n *= 123.4567 |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 92 | class StringTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 93 | def test_unicode(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 94 | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
| 95 | self.helper(marshal.loads(marshal.dumps(s))) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 96 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 97 | def test_string(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 98 | for s in ["", "Andr\xe8 Previn", "abc", " "*10000]: |
| 99 | self.helper(s) |
Tim Peters | 8211237 | 2001-08-29 02:28:42 +0000 | [diff] [blame] | 100 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 101 | def test_buffer(self): |
Guido van Rossum | e6d3904 | 2007-05-09 00:01:30 +0000 | [diff] [blame] | 102 | for s in [b"", b"Andr\xe8 Previn", b"abc", b" "*10000]: |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 103 | self.helper(buffer(s)) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 104 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 105 | class ExceptionTestCase(unittest.TestCase): |
| 106 | def test_exceptions(self): |
| 107 | new = marshal.loads(marshal.dumps(StopIteration)) |
| 108 | self.assertEqual(StopIteration, new) |
Thomas Heller | 3e1c18a | 2002-07-30 11:40:57 +0000 | [diff] [blame] | 109 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 110 | class CodeTestCase(unittest.TestCase): |
| 111 | def test_code(self): |
Neal Norwitz | 221085d | 2007-02-25 20:55:47 +0000 | [diff] [blame] | 112 | co = ExceptionTestCase.test_exceptions.__code__ |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 113 | new = marshal.loads(marshal.dumps(co)) |
| 114 | self.assertEqual(co, new) |
| 115 | |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 116 | class ContainerTestCase(unittest.TestCase, HelperMixin): |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 117 | d = {'astring': 'foo@bar.baz.spam', |
| 118 | 'afloat': 7283.43, |
| 119 | 'anint': 2**20, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 120 | 'ashortlong': 2, |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 121 | 'alist': ['.zyx.41'], |
| 122 | 'atuple': ('.zyx.41',)*10, |
| 123 | 'aboolean': False, |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 124 | 'aunicode': "Andr\xe8 Previn" |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 125 | } |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 126 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 127 | def test_dict(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 128 | self.helper(self.d) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 129 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 130 | def test_list(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 131 | self.helper(list(self.d.items())) |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 132 | |
| 133 | def test_tuple(self): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 134 | self.helper(tuple(self.d.keys())) |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 135 | |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 136 | def test_sets(self): |
| 137 | for constructor in (set, frozenset): |
Guido van Rossum | 47f17d0 | 2007-07-10 11:37:44 +0000 | [diff] [blame] | 138 | self.helper(constructor(self.d.keys())) |
Raymond Hettinger | a422c34 | 2005-01-11 03:03:27 +0000 | [diff] [blame] | 139 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 140 | class 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 Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 145 | def test_patch_873224(self): |
| 146 | self.assertRaises(Exception, marshal.loads, '0') |
| 147 | self.assertRaises(Exception, marshal.loads, 'f') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 148 | self.assertRaises(Exception, marshal.loads, marshal.dumps(2**65)[:-1]) |
Armin Rigo | 01ab279 | 2004-03-26 15:09:27 +0000 | [diff] [blame] | 149 | |
Armin Rigo | 2ccea17 | 2004-12-20 12:25:57 +0000 | [diff] [blame] | 150 | 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. Hudson | f2ca5af | 2005-06-13 18:28:46 +0000 | [diff] [blame] | 155 | 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 Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 164 | 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 Rossum | 991bf5d | 2007-08-29 18:44:54 +0000 | [diff] [blame^] | 172 | if os.name == 'nt' and hasattr(sys, 'gettotalrefcount'): |
| 173 | MAX_MARSHAL_STACK_DEPTH = 1500 |
| 174 | else: |
| 175 | MAX_MARSHAL_STACK_DEPTH = 2000 |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 176 | 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 | |
Skip Montanaro | c1b4154 | 2003-08-02 15:02:33 +0000 | [diff] [blame] | 191 | def test_main(): |
| 192 | test_support.run_unittest(IntTestCase, |
| 193 | FloatTestCase, |
| 194 | StringTestCase, |
| 195 | CodeTestCase, |
| 196 | ContainerTestCase, |
| 197 | ExceptionTestCase, |
| 198 | BugsTestCase) |
| 199 | |
| 200 | if __name__ == "__main__": |
| 201 | test_main() |