~ntt-pf-lab/nova/monkey_patch_notification

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/test/test_banana.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2010 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
import StringIO
 
5
import sys
 
6
 
 
7
# Twisted Imports
 
8
from twisted.trial import unittest
 
9
from twisted.spread import banana
 
10
from twisted.python import failure
 
11
from twisted.internet import protocol, main
 
12
 
 
13
 
 
14
class MathTestCase(unittest.TestCase):
 
15
    def testInt2b128(self):
 
16
        funkylist = range(0,100) + range(1000,1100) + range(1000000,1000100) + [1024 **10l]
 
17
        for i in funkylist:
 
18
            x = StringIO.StringIO()
 
19
            banana.int2b128(i, x.write)
 
20
            v = x.getvalue()
 
21
            y = banana.b1282int(v)
 
22
            assert y == i, "y = %s; i = %s" % (y,i)
 
23
 
 
24
class BananaTestCase(unittest.TestCase):
 
25
 
 
26
    encClass = banana.Banana
 
27
 
 
28
    def setUp(self):
 
29
        self.io = StringIO.StringIO()
 
30
        self.enc = self.encClass()
 
31
        self.enc.makeConnection(protocol.FileWrapper(self.io))
 
32
        self.enc._selectDialect("none")
 
33
        self.enc.expressionReceived = self.putResult
 
34
 
 
35
    def putResult(self, result):
 
36
        self.result = result
 
37
 
 
38
    def tearDown(self):
 
39
        self.enc.connectionLost(failure.Failure(main.CONNECTION_DONE))
 
40
        del self.enc
 
41
 
 
42
    def testString(self):
 
43
        self.enc.sendEncoded("hello")
 
44
        l = []
 
45
        self.enc.dataReceived(self.io.getvalue())
 
46
        assert self.result == 'hello'
 
47
 
 
48
    def test_int(self):
 
49
        """
 
50
        A positive integer less than 2 ** 32 should round-trip through
 
51
        banana without changing value and should come out represented
 
52
        as an C{int} (regardless of the type which was encoded).
 
53
        """
 
54
        for value in (10151, 10151L):
 
55
            self.enc.sendEncoded(value)
 
56
            self.enc.dataReceived(self.io.getvalue())
 
57
            self.assertEquals(self.result, 10151)
 
58
            self.assertIsInstance(self.result, int)
 
59
 
 
60
 
 
61
    def test_largeLong(self):
 
62
        """
 
63
        Integers greater than 2 ** 32 and less than -2 ** 32 should
 
64
        round-trip through banana without changing value and should
 
65
        come out represented as C{int} instances if the value fits
 
66
        into that type on the receiving platform.
 
67
        """
 
68
        for exp in (32, 64, 128, 256):
 
69
            for add in (0, 1):
 
70
                m = 2 ** exp + add
 
71
                for n in (m, -m-1):
 
72
                    self.io.truncate(0)
 
73
                    self.enc.sendEncoded(n)
 
74
                    self.enc.dataReceived(self.io.getvalue())
 
75
                    self.assertEquals(self.result, n)
 
76
                    if n > sys.maxint or n < -sys.maxint - 1:
 
77
                        self.assertIsInstance(self.result, long)
 
78
                    else:
 
79
                        self.assertIsInstance(self.result, int)
 
80
 
 
81
 
 
82
    def _getSmallest(self):
 
83
        # How many bytes of prefix our implementation allows
 
84
        bytes = self.enc.prefixLimit
 
85
        # How many useful bits we can extract from that based on Banana's
 
86
        # base-128 representation.
 
87
        bits = bytes * 7
 
88
        # The largest number we _should_ be able to encode
 
89
        largest = 2 ** bits - 1
 
90
        # The smallest number we _shouldn't_ be able to encode
 
91
        smallest = largest + 1
 
92
        return smallest
 
93
 
 
94
 
 
95
    def test_encodeTooLargeLong(self):
 
96
        """
 
97
        Test that a long above the implementation-specific limit is rejected
 
98
        as too large to be encoded.
 
99
        """
 
100
        smallest = self._getSmallest()
 
101
        self.assertRaises(banana.BananaError, self.enc.sendEncoded, smallest)
 
102
 
 
103
 
 
104
    def test_decodeTooLargeLong(self):
 
105
        """
 
106
        Test that a long above the implementation specific limit is rejected
 
107
        as too large to be decoded.
 
108
        """
 
109
        smallest = self._getSmallest()
 
110
        self.enc.setPrefixLimit(self.enc.prefixLimit * 2)
 
111
        self.enc.sendEncoded(smallest)
 
112
        encoded = self.io.getvalue()
 
113
        self.io.truncate(0)
 
114
        self.enc.setPrefixLimit(self.enc.prefixLimit / 2)
 
115
 
 
116
        self.assertRaises(banana.BananaError, self.enc.dataReceived, encoded)
 
117
 
 
118
 
 
119
    def _getLargest(self):
 
120
        return -self._getSmallest()
 
121
 
 
122
 
 
123
    def test_encodeTooSmallLong(self):
 
124
        """
 
125
        Test that a negative long below the implementation-specific limit is
 
126
        rejected as too small to be encoded.
 
127
        """
 
128
        largest = self._getLargest()
 
129
        self.assertRaises(banana.BananaError, self.enc.sendEncoded, largest)
 
130
 
 
131
 
 
132
    def test_decodeTooSmallLong(self):
 
133
        """
 
134
        Test that a negative long below the implementation specific limit is
 
135
        rejected as too small to be decoded.
 
136
        """
 
137
        largest = self._getLargest()
 
138
        self.enc.setPrefixLimit(self.enc.prefixLimit * 2)
 
139
        self.enc.sendEncoded(largest)
 
140
        encoded = self.io.getvalue()
 
141
        self.io.truncate(0)
 
142
        self.enc.setPrefixLimit(self.enc.prefixLimit / 2)
 
143
 
 
144
        self.assertRaises(banana.BananaError, self.enc.dataReceived, encoded)
 
145
 
 
146
 
 
147
    def testNegativeLong(self):
 
148
        self.enc.sendEncoded(-1015l)
 
149
        self.enc.dataReceived(self.io.getvalue())
 
150
        assert self.result == -1015l, "should be -1015l, got %s" % self.result
 
151
 
 
152
    def testInteger(self):
 
153
        self.enc.sendEncoded(1015)
 
154
        self.enc.dataReceived(self.io.getvalue())
 
155
        assert self.result == 1015, "should be 1015, got %s" % self.result
 
156
 
 
157
    def testNegative(self):
 
158
        self.enc.sendEncoded(-1015)
 
159
        self.enc.dataReceived(self.io.getvalue())
 
160
        assert self.result == -1015, "should be -1015, got %s" % self.result
 
161
 
 
162
    def testFloat(self):
 
163
        self.enc.sendEncoded(1015.)
 
164
        self.enc.dataReceived(self.io.getvalue())
 
165
        assert self.result == 1015.
 
166
 
 
167
    def testList(self):
 
168
        foo = [1, 2, [3, 4], [30.5, 40.2], 5, ["six", "seven", ["eight", 9]], [10], []]
 
169
        self.enc.sendEncoded(foo)
 
170
        self.enc.dataReceived(self.io.getvalue())
 
171
        assert self.result == foo, "%s!=%s" % (repr(self.result), repr(self.result))
 
172
 
 
173
    def testPartial(self):
 
174
        foo = [1, 2, [3, 4], [30.5, 40.2], 5,
 
175
               ["six", "seven", ["eight", 9]], [10],
 
176
               # TODO: currently the C implementation's a bit buggy...
 
177
               sys.maxint * 3l, sys.maxint * 2l, sys.maxint * -2l]
 
178
        self.enc.sendEncoded(foo)
 
179
        for byte in self.io.getvalue():
 
180
            self.enc.dataReceived(byte)
 
181
        assert self.result == foo, "%s!=%s" % (repr(self.result), repr(foo))
 
182
 
 
183
    def feed(self, data):
 
184
        for byte in data:
 
185
            self.enc.dataReceived(byte)
 
186
    def testOversizedList(self):
 
187
        data = '\x02\x01\x01\x01\x01\x80'
 
188
        # list(size=0x0101010102, about 4.3e9)
 
189
        self.failUnlessRaises(banana.BananaError, self.feed, data)
 
190
    def testOversizedString(self):
 
191
        data = '\x02\x01\x01\x01\x01\x82'
 
192
        # string(size=0x0101010102, about 4.3e9)
 
193
        self.failUnlessRaises(banana.BananaError, self.feed, data)
 
194
 
 
195
    def testCrashString(self):
 
196
        crashString = '\x00\x00\x00\x00\x04\x80'
 
197
        # string(size=0x0400000000, about 17.2e9)
 
198
 
 
199
        #  cBanana would fold that into a 32-bit 'int', then try to allocate
 
200
        #  a list with PyList_New(). cBanana ignored the NULL return value,
 
201
        #  so it would segfault when trying to free the imaginary list.
 
202
 
 
203
        # This variant doesn't segfault straight out in my environment.
 
204
        # Instead, it takes up large amounts of CPU and memory...
 
205
        #crashString = '\x00\x00\x00\x00\x01\x80'
 
206
        # print repr(crashString)
 
207
        #self.failUnlessRaises(Exception, self.enc.dataReceived, crashString)
 
208
        try:
 
209
            # should now raise MemoryError
 
210
            self.enc.dataReceived(crashString)
 
211
        except banana.BananaError:
 
212
            pass
 
213
 
 
214
    def testCrashNegativeLong(self):
 
215
        # There was a bug in cBanana which relied on negating a negative integer
 
216
        # always giving a postive result, but for the lowest possible number in
 
217
        # 2s-complement arithmetic, that's not true, i.e.
 
218
        #     long x = -2147483648;
 
219
        #     long y = -x;
 
220
        #     x == y;  /* true! */
 
221
        # (assuming 32-bit longs)
 
222
        self.enc.sendEncoded(-2147483648)
 
223
        self.enc.dataReceived(self.io.getvalue())
 
224
        assert self.result == -2147483648, "should be -2147483648, got %s" % self.result
 
225
 
 
226
 
 
227
    def test_sizedIntegerTypes(self):
 
228
        """
 
229
        Test that integers below the maximum C{INT} token size cutoff are
 
230
        serialized as C{INT} or C{NEG} and that larger integers are
 
231
        serialized as C{LONGINT} or C{LONGNEG}.
 
232
        """
 
233
        def encoded(n):
 
234
            self.io.seek(0)
 
235
            self.io.truncate()
 
236
            self.enc.sendEncoded(n)
 
237
            return self.io.getvalue()
 
238
 
 
239
        baseIntIn = +2147483647
 
240
        baseNegIn = -2147483648
 
241
 
 
242
        baseIntOut = '\x7f\x7f\x7f\x07\x81'
 
243
        self.assertEqual(encoded(baseIntIn - 2), '\x7d' + baseIntOut)
 
244
        self.assertEqual(encoded(baseIntIn - 1), '\x7e' + baseIntOut)
 
245
        self.assertEqual(encoded(baseIntIn - 0), '\x7f' + baseIntOut)
 
246
 
 
247
        baseLongIntOut = '\x00\x00\x00\x08\x85'
 
248
        self.assertEqual(encoded(baseIntIn + 1), '\x00' + baseLongIntOut)
 
249
        self.assertEqual(encoded(baseIntIn + 2), '\x01' + baseLongIntOut)
 
250
        self.assertEqual(encoded(baseIntIn + 3), '\x02' + baseLongIntOut)
 
251
 
 
252
        baseNegOut = '\x7f\x7f\x7f\x07\x83'
 
253
        self.assertEqual(encoded(baseNegIn + 2), '\x7e' + baseNegOut)
 
254
        self.assertEqual(encoded(baseNegIn + 1), '\x7f' + baseNegOut)
 
255
        self.assertEqual(encoded(baseNegIn + 0), '\x00\x00\x00\x00\x08\x83')
 
256
 
 
257
        baseLongNegOut = '\x00\x00\x00\x08\x86'
 
258
        self.assertEqual(encoded(baseNegIn - 1), '\x01' + baseLongNegOut)
 
259
        self.assertEqual(encoded(baseNegIn - 2), '\x02' + baseLongNegOut)
 
260
        self.assertEqual(encoded(baseNegIn - 3), '\x03' + baseLongNegOut)
 
261
 
 
262
 
 
263
 
 
264
class GlobalCoderTests(unittest.TestCase):
 
265
    """
 
266
    Tests for the free functions L{banana.encode} and L{banana.decode}.
 
267
    """
 
268
    def test_statelessDecode(self):
 
269
        """
 
270
        Test that state doesn't carry over between calls to L{banana.decode}.
 
271
        """
 
272
        # Banana encoding of 2 ** 449
 
273
        undecodable = '\x7f' * 65 + '\x85'
 
274
        self.assertRaises(banana.BananaError, banana.decode, undecodable)
 
275
 
 
276
        # Banana encoding of 1
 
277
        decodable = '\x01\x81'
 
278
        self.assertEqual(banana.decode(decodable), 1)