1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
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
14 class MathTestCase(unittest.TestCase):
15 def testInt2b128(self):
16 funkylist = range(0,100) + range(1000,1100) + range(1000000,1000100) + [1024 **10l]
18 x = StringIO.StringIO()
19 banana.int2b128(i, x.write)
21 y = banana.b1282int(v)
22 assert y == i, "y = %s; i = %s" % (y,i)
24 class BananaTestCase(unittest.TestCase):
26 encClass = banana.Banana
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
35 def putResult(self, result):
39 self.enc.connectionLost(failure.Failure(main.CONNECTION_DONE))
43 self.enc.sendEncoded("hello")
45 self.enc.dataReceived(self.io.getvalue())
46 assert self.result == 'hello'
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).
54 for value in (10151, 10151L):
55 self.enc.sendEncoded(value)
56 self.enc.dataReceived(self.io.getvalue())
57 self.assertEqual(self.result, 10151)
58 self.assertIsInstance(self.result, int)
61 def test_largeLong(self):
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.
68 for exp in (32, 64, 128, 256):
73 self.enc.sendEncoded(n)
74 self.enc.dataReceived(self.io.getvalue())
75 self.assertEqual(self.result, n)
76 if n > sys.maxint or n < -sys.maxint - 1:
77 self.assertIsInstance(self.result, long)
79 self.assertIsInstance(self.result, int)
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.
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
95 def test_encodeTooLargeLong(self):
97 Test that a long above the implementation-specific limit is rejected
98 as too large to be encoded.
100 smallest = self._getSmallest()
101 self.assertRaises(banana.BananaError, self.enc.sendEncoded, smallest)
104 def test_decodeTooLargeLong(self):
106 Test that a long above the implementation specific limit is rejected
107 as too large to be decoded.
109 smallest = self._getSmallest()
110 self.enc.setPrefixLimit(self.enc.prefixLimit * 2)
111 self.enc.sendEncoded(smallest)
112 encoded = self.io.getvalue()
114 self.enc.setPrefixLimit(self.enc.prefixLimit / 2)
116 self.assertRaises(banana.BananaError, self.enc.dataReceived, encoded)
119 def _getLargest(self):
120 return -self._getSmallest()
123 def test_encodeTooSmallLong(self):
125 Test that a negative long below the implementation-specific limit is
126 rejected as too small to be encoded.
128 largest = self._getLargest()
129 self.assertRaises(banana.BananaError, self.enc.sendEncoded, largest)
132 def test_decodeTooSmallLong(self):
134 Test that a negative long below the implementation specific limit is
135 rejected as too small to be decoded.
137 largest = self._getLargest()
138 self.enc.setPrefixLimit(self.enc.prefixLimit * 2)
139 self.enc.sendEncoded(largest)
140 encoded = self.io.getvalue()
142 self.enc.setPrefixLimit(self.enc.prefixLimit / 2)
144 self.assertRaises(banana.BananaError, self.enc.dataReceived, encoded)
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
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
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
163 self.enc.sendEncoded(1015.)
164 self.enc.dataReceived(self.io.getvalue())
165 assert self.result == 1015.
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))
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))
183 def feed(self, 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)
195 def testCrashString(self):
196 crashString = '\x00\x00\x00\x00\x04\x80'
197 # string(size=0x0400000000, about 17.2e9)
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.
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)
209 # should now raise MemoryError
210 self.enc.dataReceived(crashString)
211 except banana.BananaError:
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;
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
227 def test_sizedIntegerTypes(self):
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}.
236 self.enc.sendEncoded(n)
237 return self.io.getvalue()
239 baseIntIn = +2147483647
240 baseNegIn = -2147483648
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)
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)
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')
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)
264 class GlobalCoderTests(unittest.TestCase):
266 Tests for the free functions L{banana.encode} and L{banana.decode}.
268 def test_statelessDecode(self):
270 Test that state doesn't carry over between calls to L{banana.decode}.
272 # Banana encoding of 2 ** 449
273 undecodable = '\x7f' * 65 + '\x85'
274 self.assertRaises(banana.BananaError, banana.decode, undecodable)
276 # Banana encoding of 1
277 decodable = '\x01\x81'
278 self.assertEqual(banana.decode(decodable), 1)