test_marshal.py 8.24 KB
Newer Older
1 2 3 4
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-

from test import test_support
5 6
import marshal
import sys
7 8
import unittest
import os
9

10 11 12 13 14 15 16 17 18 19 20 21 22 23
class IntTestCase(unittest.TestCase):
    def test_ints(self):
        # Test the full range of Python ints.
        n = sys.maxint
        while n:
            for expected in (-n, n):
                s = marshal.dumps(expected)
                got = marshal.loads(s)
                self.assertEqual(expected, got)
                marshal.dump(expected, file(test_support.TESTFN, "wb"))
                got = marshal.load(file(test_support.TESTFN, "rb"))
                self.assertEqual(expected, got)
            n = n >> 1
        os.unlink(test_support.TESTFN)
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    def test_int64(self):
        # Simulate int marshaling on a 64-bit box.  This is most interesting if
        # we're running the test on a 32-bit box, of course.

        def to_little_endian_string(value, nbytes):
            bytes = []
            for i in range(nbytes):
                bytes.append(chr(value & 0xff))
                value >>= 8
            return ''.join(bytes)

        maxint64 = (1L << 63) - 1
        minint64 = -maxint64-1

        for base in maxint64, minint64, -maxint64, -(minint64 >> 1):
            while base:
                s = 'I' + to_little_endian_string(base, 8)
                got = marshal.loads(s)
                self.assertEqual(base, got)
                if base == -1:  # a fixed-point for shifting right 1
                    base = 0
                else:
                    base >>= 1

    def test_bool(self):
        for b in (True, False):
            new = marshal.loads(marshal.dumps(b))
            self.assertEqual(b, new)
            self.assertEqual(type(b), type(new))
            marshal.dump(b, file(test_support.TESTFN, "wb"))
            new = marshal.load(file(test_support.TESTFN, "rb"))
            self.assertEqual(b, new)
            self.assertEqual(type(b), type(new))
Tim Peters's avatar
Tim Peters committed
58

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
class FloatTestCase(unittest.TestCase):
    def test_floats(self):
        # Test a few floats
        small = 1e-25
        n = sys.maxint * 3.7e250
        while n > small:
            for expected in (-n, n):
                f = float(expected)
                s = marshal.dumps(f)
                got = marshal.loads(s)
                self.assertEqual(f, got)
                marshal.dump(f, file(test_support.TESTFN, "wb"))
                got = marshal.load(file(test_support.TESTFN, "rb"))
                self.assertEqual(f, got)
            n /= 123.4567

        f = 0.0
Michael W. Hudson's avatar
Michael W. Hudson committed
76
        s = marshal.dumps(f, 2)
77
        got = marshal.loads(s)
78
        self.assertEqual(f, got)
Michael W. Hudson's avatar
Michael W. Hudson committed
79 80
        # and with version <= 1 (floats marshalled differently then)
        s = marshal.dumps(f, 1)
Tim Peters's avatar
Tim Peters committed
81 82
        got = marshal.loads(s)
        self.assertEqual(f, got)
83 84 85 86 87

        n = sys.maxint * 3.7e-250
        while n < small:
            for expected in (-n, n):
                f = float(expected)
Tim Peters's avatar
Tim Peters committed
88

89 90 91
                s = marshal.dumps(f)
                got = marshal.loads(s)
                self.assertEqual(f, got)
Tim Peters's avatar
Tim Peters committed
92

Michael W. Hudson's avatar
Michael W. Hudson committed
93 94 95
                s = marshal.dumps(f, 1)
                got = marshal.loads(s)
                self.assertEqual(f, got)
Tim Peters's avatar
Tim Peters committed
96

97 98 99
                marshal.dump(f, file(test_support.TESTFN, "wb"))
                got = marshal.load(file(test_support.TESTFN, "rb"))
                self.assertEqual(f, got)
Tim Peters's avatar
Tim Peters committed
100

Michael W. Hudson's avatar
Michael W. Hudson committed
101 102 103
                marshal.dump(f, file(test_support.TESTFN, "wb"), 1)
                got = marshal.load(file(test_support.TESTFN, "rb"))
                self.assertEqual(f, got)
104 105 106 107 108 109 110 111 112 113
            n *= 123.4567
        os.unlink(test_support.TESTFN)

class StringTestCase(unittest.TestCase):
    def test_unicode(self):
        for s in [u"", u"Andr Previn", u"abc", u" "*10000]:
            new = marshal.loads(marshal.dumps(s))
            self.assertEqual(s, new)
            self.assertEqual(type(s), type(new))
            marshal.dump(s, file(test_support.TESTFN, "wb"))
114
            new = marshal.load(file(test_support.TESTFN, "rb"))
115 116 117 118 119 120 121 122 123 124
            self.assertEqual(s, new)
            self.assertEqual(type(s), type(new))
        os.unlink(test_support.TESTFN)

    def test_string(self):
        for s in ["", "Andr Previn", "abc", " "*10000]:
            new = marshal.loads(marshal.dumps(s))
            self.assertEqual(s, new)
            self.assertEqual(type(s), type(new))
            marshal.dump(s, file(test_support.TESTFN, "wb"))
125
            new = marshal.load(file(test_support.TESTFN, "rb"))
126 127 128 129 130 131 132 133 134 135
            self.assertEqual(s, new)
            self.assertEqual(type(s), type(new))
        os.unlink(test_support.TESTFN)

    def test_buffer(self):
        for s in ["", "Andr Previn", "abc", " "*10000]:
            b = buffer(s)
            new = marshal.loads(marshal.dumps(b))
            self.assertEqual(s, new)
            marshal.dump(b, file(test_support.TESTFN, "wb"))
136
            new = marshal.load(file(test_support.TESTFN, "rb"))
137 138
            self.assertEqual(s, new)
        os.unlink(test_support.TESTFN)
Tim Peters's avatar
Tim Peters committed
139

140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
class ExceptionTestCase(unittest.TestCase):
    def test_exceptions(self):
        new = marshal.loads(marshal.dumps(StopIteration))
        self.assertEqual(StopIteration, new)

class CodeTestCase(unittest.TestCase):
    def test_code(self):
        co = ExceptionTestCase.test_exceptions.func_code
        new = marshal.loads(marshal.dumps(co))
        self.assertEqual(co, new)

class ContainerTestCase(unittest.TestCase):
    d = {'astring': 'foo@bar.baz.spam',
         'afloat': 7283.43,
         'anint': 2**20,
         'ashortlong': 2L,
         'alist': ['.zyx.41'],
         'atuple': ('.zyx.41',)*10,
         'aboolean': False,
         'aunicode': u"Andr Previn"
         }
    def test_dict(self):
        new = marshal.loads(marshal.dumps(self.d))
        self.assertEqual(self.d, new)
        marshal.dump(self.d, file(test_support.TESTFN, "wb"))
165
        new = marshal.load(file(test_support.TESTFN, "rb"))
166 167
        self.assertEqual(self.d, new)
        os.unlink(test_support.TESTFN)
Tim Peters's avatar
Tim Peters committed
168

169 170 171 172 173
    def test_list(self):
        lst = self.d.items()
        new = marshal.loads(marshal.dumps(lst))
        self.assertEqual(lst, new)
        marshal.dump(lst, file(test_support.TESTFN, "wb"))
174
        new = marshal.load(file(test_support.TESTFN, "rb"))
175 176 177 178 179 180 181 182
        self.assertEqual(lst, new)
        os.unlink(test_support.TESTFN)

    def test_tuple(self):
        t = tuple(self.d.keys())
        new = marshal.loads(marshal.dumps(t))
        self.assertEqual(t, new)
        marshal.dump(t, file(test_support.TESTFN, "wb"))
183
        new = marshal.load(file(test_support.TESTFN, "rb"))
184 185
        self.assertEqual(t, new)
        os.unlink(test_support.TESTFN)
186 187 188 189 190 191 192 193 194

    def test_sets(self):
        for constructor in (set, frozenset):
            t = constructor(self.d.keys())
            new = marshal.loads(marshal.dumps(t))
            self.assertEqual(t, new)
            self.assert_(isinstance(new, constructor))
            self.assertNotEqual(id(t), id(new))
            marshal.dump(t, file(test_support.TESTFN, "wb"))
195
            new = marshal.load(file(test_support.TESTFN, "rb"))
196 197
            self.assertEqual(t, new)
            os.unlink(test_support.TESTFN)
Tim Peters's avatar
Tim Peters committed
198

199 200 201 202 203
class BugsTestCase(unittest.TestCase):
    def test_bug_5888452(self):
        # Simple-minded check for SF 588452: Debug build crashes
        marshal.dumps([128] * 1000)

204 205 206 207 208
    def test_patch_873224(self):
        self.assertRaises(Exception, marshal.loads, '0')
        self.assertRaises(Exception, marshal.loads, 'f')
        self.assertRaises(Exception, marshal.loads, marshal.dumps(5L)[:-1])

209 210 211 212 213
    def test_version_argument(self):
        # Python 2.4.0 crashes for any call to marshal.dumps(x, y)
        self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5)
        self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5)

Michael W. Hudson's avatar
Michael W. Hudson committed
214 215 216 217 218 219 220 221 222
    def test_fuzz(self):
        # simple test that it's at least not *totally* trivial to
        # crash from bad marshal data
        for c in [chr(i) for i in range(256)]:
            try:
                marshal.loads(c)
            except Exception:
                pass

223 224 225 226 227 228 229 230 231 232 233
def test_main():
    test_support.run_unittest(IntTestCase,
                              FloatTestCase,
                              StringTestCase,
                              CodeTestCase,
                              ContainerTestCase,
                              ExceptionTestCase,
                              BugsTestCase)

if __name__ == "__main__":
    test_main()