blob: d4755cf2a1c6602b69cc0dbe8db807239e18b26e [file] [log] [blame]
David Reiss9ff3b9d2008-02-15 01:10:23 +00001#!/usr/bin/env python
2
David Reissea2cba82009-03-30 21:35:00 +00003#
4# Licensed to the Apache Software Foundation (ASF) under one
5# or more contributor license agreements. See the NOTICE file
6# distributed with this work for additional information
7# regarding copyright ownership. The ASF licenses this file
8# to you under the Apache License, Version 2.0 (the
9# "License"); you may not use this file except in compliance
10# with the License. You may obtain a copy of the License at
11#
12# http://www.apache.org/licenses/LICENSE-2.0
13#
14# Unless required by applicable law or agreed to in writing,
15# software distributed under the License is distributed on an
16# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17# KIND, either express or implied. See the License for the
18# specific language governing permissions and limitations
19# under the License.
20#
21
David Reiss9ff3b9d2008-02-15 01:10:23 +000022from ThriftTest.ttypes import *
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000023from DebugProtoTest.ttypes import CompactProtoTestStruct, Empty
David Reiss9ff3b9d2008-02-15 01:10:23 +000024from thrift.transport import TTransport
Roger Meierad8154a2012-12-18 21:02:16 +010025from thrift.protocol import TBinaryProtocol, TCompactProtocol, TJSONProtocol
David Reiss6acc2692010-02-26 00:56:02 +000026from thrift.TSerialization import serialize, deserialize
Nobuaki Sukegawa33744b02016-01-03 14:24:39 +090027import sys
David Reiss9ff3b9d2008-02-15 01:10:23 +000028import unittest
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090029
David Reiss9ff3b9d2008-02-15 01:10:23 +000030
31class AbstractTest(unittest.TestCase):
32
33 def setUp(self):
David Reiss46bb4ae2009-01-14 22:34:15 +000034 self.v1obj = VersioningTestV1(
David Reiss9ff3b9d2008-02-15 01:10:23 +000035 begin_in_both=12345,
David Reissa528f542009-03-24 22:48:40 +000036 old_string='aaa',
David Reiss9ff3b9d2008-02-15 01:10:23 +000037 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000038 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000039
David Reiss46bb4ae2009-01-14 22:34:15 +000040 self.v2obj = VersioningTestV2(
David Reiss9ff3b9d2008-02-15 01:10:23 +000041 begin_in_both=12345,
42 newint=1,
43 newbyte=2,
44 newshort=3,
45 newlong=4,
46 newdouble=5.0,
David Reiss46bb4ae2009-01-14 22:34:15 +000047 newstruct=Bonk(message="Hello!", type=123),
David Reiss9ff3b9d2008-02-15 01:10:23 +000048 newlist=[7,8,9],
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000049 newset=set([42,1,8]),
David Reiss9ff3b9d2008-02-15 01:10:23 +000050 newmap={1:2,2:3},
51 newstring="Hola!",
52 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000053 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000054
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000055 self.bools = Bools(im_true=True, im_false=False)
56 self.bools_flipped = Bools(im_true=False, im_false=True)
57
58 self.large_deltas = LargeDeltas (
59 b1=self.bools,
60 b10=self.bools_flipped,
61 b100=self.bools,
62 check_true=True,
63 b1000=self.bools_flipped,
64 check_false=False,
65 vertwo2000=VersioningTestV2(newstruct=Bonk(message='World!', type=314)),
66 a_set2500=set(['lazy', 'brown', 'cow']),
67 vertwo3000=VersioningTestV2(newset=set([2, 3, 5, 7, 11])),
68 big_numbers=[2**8, 2**16, 2**31-1, -(2**31-1)]
69 )
70
71 self.compact_struct = CompactProtoTestStruct(
72 a_byte = 127,
73 a_i16=32000,
74 a_i32=1000000000,
75 a_i64=0xffffffffff,
76 a_double=5.6789,
77 a_string="my string",
78 true_field=True,
79 false_field=False,
80 empty_struct_field=Empty(),
81 byte_list=[-127, -1, 0, 1, 127],
82 i16_list=[-1, 0, 1, 0x7fff],
83 i32_list= [-1, 0, 0xff, 0xffff, 0xffffff, 0x7fffffff],
84 i64_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff],
85 double_list=[0.1, 0.2, 0.3],
86 string_list=["first", "second", "third"],
87 boolean_list=[True, True, True, False, False, False],
88 struct_list=[Empty(), Empty()],
89 byte_set=set([-127, -1, 0, 1, 127]),
90 i16_set=set([-1, 0, 1, 0x7fff]),
91 i32_set=set([1, 2, 3]),
92 i64_set=set([-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff]),
93 double_set=set([0.1, 0.2, 0.3]),
94 string_set=set(["first", "second", "third"]),
95 boolean_set=set([True, False]),
96 #struct_set=set([Empty()]), # unhashable instance
97 byte_byte_map={1 : 2},
98 i16_byte_map={1 : 1, -1 : 1, 0x7fff : 1},
99 i32_byte_map={1 : 1, -1 : 1, 0x7fffffff : 1},
100 i64_byte_map={0 : 1, 1 : 1, -1 : 1, 0x7fffffffffffffff : 1},
101 double_byte_map={-1.1 : 1, 1.1 : 1},
102 string_byte_map={"first" : 1, "second" : 2, "third" : 3, "" : 0},
103 boolean_byte_map={True : 1, False: 0},
104 byte_i16_map={1 : 1, 2 : -1, 3 : 0x7fff},
105 byte_i32_map={1 : 1, 2 : -1, 3 : 0x7fffffff},
106 byte_i64_map={1 : 1, 2 : -1, 3 : 0x7fffffffffffffff},
107 byte_double_map={1 : 0.1, 2 : -0.1, 3 : 1000000.1},
108 byte_string_map={1 : "", 2 : "blah", 3 : "loooooooooooooong string"},
109 byte_boolean_map={1 : True, 2 : False},
110 #list_byte_map # unhashable
111 #set_byte_map={set([1, 2, 3]) : 1, set([0, 1]) : 2, set([]) : 0}, # unhashable
112 #map_byte_map # unhashable
113 byte_map_map={0 : {}, 1 : {1 : 1}, 2 : {1 : 1, 2 : 2}},
114 byte_set_map={0 : set([]), 1 : set([1]), 2 : set([1, 2])},
115 byte_list_map={0 : [], 1 : [1], 2 : [1, 2]},
116 )
117
Roger Meierf4eec7a2011-09-11 18:16:21 +0000118 self.nested_lists_i32x2 = NestedListsI32x2(
119 [
120 [ 1, 1, 2 ],
121 [ 2, 7, 9 ],
122 [ 3, 5, 8 ]
123 ]
124 )
125
126 self.nested_lists_i32x3 = NestedListsI32x3(
127 [
128 [
129 [ 2, 7, 9 ],
130 [ 3, 5, 8 ]
131 ],
132 [
133 [ 1, 1, 2 ],
134 [ 1, 4, 9 ]
135 ]
136 ]
137 )
138
139 self.nested_mixedx2 = NestedMixedx2( int_set_list=[
140 set([1,2,3]),
141 set([1,4,9]),
142 set([1,2,3,5,8,13,21]),
143 set([-1, 0, 1])
144 ],
145 # note, the sets below are sets of chars, since the strings are iterated
146 map_int_strset={ 10:set('abc'), 20:set('def'), 30:set('GHI') },
147 map_int_strset_list=[
148 { 10:set('abc'), 20:set('def'), 30:set('GHI') },
149 { 100:set('lmn'), 200:set('opq'), 300:set('RST') },
150 { 1000:set('uvw'), 2000:set('wxy'), 3000:set('XYZ') }
151 ]
152 )
153
154 self.nested_lists_bonk = NestedListsBonk(
155 [
156 [
157 [
158 Bonk(message='inner A first', type=1),
159 Bonk(message='inner A second', type=1)
160 ],
161 [
162 Bonk(message='inner B first', type=2),
163 Bonk(message='inner B second', type=2)
164 ]
165 ]
166 ]
167 )
168
169 self.list_bonks = ListBonks(
170 [
171 Bonk(message='inner A', type=1),
172 Bonk(message='inner B', type=2),
173 Bonk(message='inner C', type=0)
174 ]
175 )
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000176
David Reiss9ff3b9d2008-02-15 01:10:23 +0000177 def _serialize(self, obj):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000178 trans = TTransport.TMemoryBuffer()
179 prot = self.protocol_factory.getProtocol(trans)
180 obj.write(prot)
181 return trans.getvalue()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000182
183 def _deserialize(self, objtype, data):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000184 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
185 ret = objtype()
186 ret.read(prot)
187 return ret
David Reiss9ff3b9d2008-02-15 01:10:23 +0000188
189 def testForwards(self):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000190 obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
191 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
192 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000193
194 def testBackwards(self):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000195 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
196 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
197 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000198
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000199 def testSerializeV1(self):
200 obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj))
201 self.assertEquals(obj, self.v1obj)
202
203 def testSerializeV2(self):
204 obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj))
205 self.assertEquals(obj, self.v2obj)
206
207 def testBools(self):
208 self.assertNotEquals(self.bools, self.bools_flipped)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000209 self.assertNotEquals(self.bools, self.v1obj)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000210 obj = self._deserialize(Bools, self._serialize(self.bools))
211 self.assertEquals(obj, self.bools)
212 obj = self._deserialize(Bools, self._serialize(self.bools_flipped))
213 self.assertEquals(obj, self.bools_flipped)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000214 rep = repr(self.bools)
215 self.assertTrue(len(rep) > 0)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000216
217 def testLargeDeltas(self):
218 # test large field deltas (meaningful in CompactProto only)
219 obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas))
220 self.assertEquals(obj, self.large_deltas)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000221 rep = repr(self.large_deltas)
222 self.assertTrue(len(rep) > 0)
223
224 def testNestedListsI32x2(self):
225 obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2))
226 self.assertEquals(obj, self.nested_lists_i32x2)
227 rep = repr(self.nested_lists_i32x2)
228 self.assertTrue(len(rep) > 0)
229
230 def testNestedListsI32x3(self):
231 obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3))
232 self.assertEquals(obj, self.nested_lists_i32x3)
233 rep = repr(self.nested_lists_i32x3)
234 self.assertTrue(len(rep) > 0)
235
236 def testNestedMixedx2(self):
237 obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2))
238 self.assertEquals(obj, self.nested_mixedx2)
239 rep = repr(self.nested_mixedx2)
240 self.assertTrue(len(rep) > 0)
241
242 def testNestedListsBonk(self):
243 obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk))
244 self.assertEquals(obj, self.nested_lists_bonk)
245 rep = repr(self.nested_lists_bonk)
246 self.assertTrue(len(rep) > 0)
247
248 def testListBonks(self):
249 obj = self._deserialize(ListBonks, self._serialize(self.list_bonks))
250 self.assertEquals(obj, self.list_bonks)
251 rep = repr(self.list_bonks)
252 self.assertTrue(len(rep) > 0)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000253
254 def testCompactStruct(self):
255 # test large field deltas (meaningful in CompactProto only)
256 obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct))
257 self.assertEquals(obj, self.compact_struct)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000258 rep = repr(self.compact_struct)
259 self.assertTrue(len(rep) > 0)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000260
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200261 def testIntegerLimits(self):
Nobuaki Sukegawa33744b02016-01-03 14:24:39 +0900262 if (sys.version_info[0] == 2 and sys.version_info[1] <= 6):
263 print('Skipping testIntegerLimits for Python 2.6')
264 return
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200265 bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129),
266 CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769),
267 CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649),
268 CompactProtoTestStruct(a_i64=9223372036854775808), CompactProtoTestStruct(a_i64=-9223372036854775809)
269 ]
270
271 for value in bad_values:
272 self.assertRaises(Exception, self._serialize, value)
273
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900274
David Reiss9ff3b9d2008-02-15 01:10:23 +0000275class NormalBinaryTest(AbstractTest):
276 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
277
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900278
David Reiss9ff3b9d2008-02-15 01:10:23 +0000279class AcceleratedBinaryTest(AbstractTest):
280 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
281
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900282
David Reissabafd792010-09-27 17:28:15 +0000283class CompactProtocolTest(AbstractTest):
284 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000285
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900286
Roger Meierad8154a2012-12-18 21:02:16 +0100287class JSONProtocolTest(AbstractTest):
288 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
289
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900290
David Reiss4c591c92009-01-31 21:39:25 +0000291class AcceleratedFramedTest(unittest.TestCase):
292 def testSplit(self):
293 """Test FramedTransport and BinaryProtocolAccelerated
294
295 Tests that TBinaryProtocolAccelerated and TFramedTransport
296 play nicely together when a read spans a frame"""
297
298 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
299 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1))
300
301 databuf = TTransport.TMemoryBuffer()
302 prot = protocol_factory.getProtocol(databuf)
303 prot.writeI32(42)
304 prot.writeString(bigstring)
305 prot.writeI16(24)
306 data = databuf.getvalue()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900307 cutpoint = len(data) // 2
David Reiss4c591c92009-01-31 21:39:25 +0000308 parts = [ data[:cutpoint], data[cutpoint:] ]
309
310 framed_buffer = TTransport.TMemoryBuffer()
311 framed_writer = TTransport.TFramedTransport(framed_buffer)
312 for part in parts:
313 framed_writer.write(part)
314 framed_writer.flush()
315 self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
316
317 # Recreate framed_buffer so we can read from it.
318 framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
319 framed_reader = TTransport.TFramedTransport(framed_buffer)
320 prot = protocol_factory.getProtocol(framed_reader)
321 self.assertEqual(prot.readI32(), 42)
322 self.assertEqual(prot.readString(), bigstring)
323 self.assertEqual(prot.readI16(), 24)
324
David Reiss6acc2692010-02-26 00:56:02 +0000325class SerializersTest(unittest.TestCase):
326
327 def testSerializeThenDeserialize(self):
328 obj = Xtruct2(i32_thing=1,
329 struct_thing=Xtruct(string_thing="foo"))
330
331 s1 = serialize(obj)
332 for i in range(10):
333 self.assertEquals(s1, serialize(obj))
334 objcopy = Xtruct2()
335 deserialize(objcopy, serialize(obj))
336 self.assertEquals(obj, objcopy)
337
338 obj = Xtruct(string_thing="bar")
339 objcopy = Xtruct()
340 deserialize(objcopy, serialize(obj))
341 self.assertEquals(obj, objcopy)
David Reiss4c591c92009-01-31 21:39:25 +0000342
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000343 # test booleans
344 obj = Bools(im_true=True, im_false=False)
345 objcopy = Bools()
346 deserialize(objcopy, serialize(obj))
347 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900348
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000349 # test enums
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900350 for num, name in Numberz._VALUES_TO_NAMES.items():
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000351 obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num)
352 objcopy = Bonk()
353 deserialize(objcopy, serialize(obj))
354 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900355
David Reiss4c591c92009-01-31 21:39:25 +0000356
David Reiss9ff3b9d2008-02-15 01:10:23 +0000357def suite():
358 suite = unittest.TestSuite()
359 loader = unittest.TestLoader()
360
361 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
362 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
David Reissabafd792010-09-27 17:28:15 +0000363 suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest))
Roger Meierad8154a2012-12-18 21:02:16 +0100364 suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest))
David Reiss4c591c92009-01-31 21:39:25 +0000365 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
David Reiss6acc2692010-02-26 00:56:02 +0000366 suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
David Reiss9ff3b9d2008-02-15 01:10:23 +0000367 return suite
368
369if __name__ == "__main__":
370 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))