blob: 65a149599384c37559647e24085a460597860d40 [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
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090033 def setUp(self):
34 self.v1obj = VersioningTestV1(
35 begin_in_both=12345,
36 old_string='aaa',
37 end_in_both=54321,
38 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000039
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090040 self.v2obj = VersioningTestV2(
41 begin_in_both=12345,
42 newint=1,
43 newbyte=2,
44 newshort=3,
45 newlong=4,
46 newdouble=5.0,
47 newstruct=Bonk(message="Hello!", type=123),
48 newlist=[7, 8, 9],
49 newset=set([42, 1, 8]),
50 newmap={1: 2, 2: 3},
51 newstring="Hola!",
52 end_in_both=54321,
53 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000054
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090055 self.bools = Bools(im_true=True, im_false=False)
56 self.bools_flipped = Bools(im_true=False, im_false=True)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000057
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090058 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 )
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000070
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090071 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 )
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000117
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900118 self.nested_lists_i32x2 = NestedListsI32x2(
119 [
120 [1, 1, 2],
121 [2, 7, 9],
122 [3, 5, 8]
123 ]
124 )
Roger Meierf4eec7a2011-09-11 18:16:21 +0000125
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900126 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]
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200135 ]
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900136 ]
137 )
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200138
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900139 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 )
176
177 def _serialize(self, obj):
178 trans = TTransport.TMemoryBuffer()
179 prot = self.protocol_factory.getProtocol(trans)
180 obj.write(prot)
181 return trans.getvalue()
182
183 def _deserialize(self, objtype, data):
184 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
185 ret = objtype()
186 ret.read(prot)
187 return ret
188
189 def testForwards(self):
190 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)
193
194 def testBackwards(self):
195 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)
198
199 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)
209 self.assertNotEquals(self.bools, self.v1obj)
210 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)
214 rep = repr(self.bools)
215 self.assertTrue(len(rep) > 0)
216
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)
221 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)
253
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)
258 rep = repr(self.compact_struct)
259 self.assertTrue(len(rep) > 0)
260
261 def testIntegerLimits(self):
262 if (sys.version_info[0] == 2 and sys.version_info[1] <= 6):
263 print('Skipping testIntegerLimits for Python 2.6')
264 return
265 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)
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200273
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900274
David Reiss9ff3b9d2008-02-15 01:10:23 +0000275class NormalBinaryTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900276 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000277
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900278
David Reiss9ff3b9d2008-02-15 01:10:23 +0000279class AcceleratedBinaryTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900280 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000281
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900282
David Reissabafd792010-09-27 17:28:15 +0000283class CompactProtocolTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900284 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):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900288 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
Roger Meierad8154a2012-12-18 21:02:16 +0100289
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900290
David Reiss4c591c92009-01-31 21:39:25 +0000291class AcceleratedFramedTest(unittest.TestCase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900292 def testSplit(self):
293 """Test FramedTransport and BinaryProtocolAccelerated
David Reiss4c591c92009-01-31 21:39:25 +0000294
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900295 Tests that TBinaryProtocolAccelerated and TFramedTransport
296 play nicely together when a read spans a frame"""
David Reiss4c591c92009-01-31 21:39:25 +0000297
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900298 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
299 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z") + 1))
David Reiss4c591c92009-01-31 21:39:25 +0000300
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900301 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()
307 cutpoint = len(data) // 2
308 parts = [data[:cutpoint], data[cutpoint:]]
David Reiss4c591c92009-01-31 21:39:25 +0000309
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900310 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)
David Reiss4c591c92009-01-31 21:39:25 +0000316
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900317 # 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 Reiss4c591c92009-01-31 21:39:25 +0000325
David Reiss6acc2692010-02-26 00:56:02 +0000326class SerializersTest(unittest.TestCase):
327
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900328 def testSerializeThenDeserialize(self):
329 obj = Xtruct2(i32_thing=1,
330 struct_thing=Xtruct(string_thing="foo"))
David Reiss6acc2692010-02-26 00:56:02 +0000331
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900332 s1 = serialize(obj)
333 for i in range(10):
334 self.assertEquals(s1, serialize(obj))
335 objcopy = Xtruct2()
336 deserialize(objcopy, serialize(obj))
337 self.assertEquals(obj, objcopy)
David Reiss6acc2692010-02-26 00:56:02 +0000338
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900339 obj = Xtruct(string_thing="bar")
340 objcopy = Xtruct()
341 deserialize(objcopy, serialize(obj))
342 self.assertEquals(obj, objcopy)
David Reiss4c591c92009-01-31 21:39:25 +0000343
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900344 # test booleans
345 obj = Bools(im_true=True, im_false=False)
346 objcopy = Bools()
347 deserialize(objcopy, serialize(obj))
348 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900349
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900350 # test enums
351 for num, name in Numberz._VALUES_TO_NAMES.items():
352 obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num)
353 objcopy = Bonk()
354 deserialize(objcopy, serialize(obj))
355 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900356
David Reiss4c591c92009-01-31 21:39:25 +0000357
David Reiss9ff3b9d2008-02-15 01:10:23 +0000358def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900359 suite = unittest.TestSuite()
360 loader = unittest.TestLoader()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000361
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900362 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
363 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
364 suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest))
365 suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest))
366 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
367 suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
368 return suite
David Reiss9ff3b9d2008-02-15 01:10:23 +0000369
370if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900371 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))