blob: 99e0393b43f182e21cb4334cb24b274f06b3dc0e [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
David Reiss9ff3b9d2008-02-15 01:10:23 +000027import unittest
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090028
David Reiss9ff3b9d2008-02-15 01:10:23 +000029
30class AbstractTest(unittest.TestCase):
31
32 def setUp(self):
David Reiss46bb4ae2009-01-14 22:34:15 +000033 self.v1obj = VersioningTestV1(
David Reiss9ff3b9d2008-02-15 01:10:23 +000034 begin_in_both=12345,
David Reissa528f542009-03-24 22:48:40 +000035 old_string='aaa',
David Reiss9ff3b9d2008-02-15 01:10:23 +000036 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000037 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000038
David Reiss46bb4ae2009-01-14 22:34:15 +000039 self.v2obj = VersioningTestV2(
David Reiss9ff3b9d2008-02-15 01:10:23 +000040 begin_in_both=12345,
41 newint=1,
42 newbyte=2,
43 newshort=3,
44 newlong=4,
45 newdouble=5.0,
David Reiss46bb4ae2009-01-14 22:34:15 +000046 newstruct=Bonk(message="Hello!", type=123),
David Reiss9ff3b9d2008-02-15 01:10:23 +000047 newlist=[7,8,9],
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000048 newset=set([42,1,8]),
David Reiss9ff3b9d2008-02-15 01:10:23 +000049 newmap={1:2,2:3},
50 newstring="Hola!",
51 end_in_both=54321,
David Reiss46bb4ae2009-01-14 22:34:15 +000052 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000053
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000054 self.bools = Bools(im_true=True, im_false=False)
55 self.bools_flipped = Bools(im_true=False, im_false=True)
56
57 self.large_deltas = LargeDeltas (
58 b1=self.bools,
59 b10=self.bools_flipped,
60 b100=self.bools,
61 check_true=True,
62 b1000=self.bools_flipped,
63 check_false=False,
64 vertwo2000=VersioningTestV2(newstruct=Bonk(message='World!', type=314)),
65 a_set2500=set(['lazy', 'brown', 'cow']),
66 vertwo3000=VersioningTestV2(newset=set([2, 3, 5, 7, 11])),
67 big_numbers=[2**8, 2**16, 2**31-1, -(2**31-1)]
68 )
69
70 self.compact_struct = CompactProtoTestStruct(
71 a_byte = 127,
72 a_i16=32000,
73 a_i32=1000000000,
74 a_i64=0xffffffffff,
75 a_double=5.6789,
76 a_string="my string",
77 true_field=True,
78 false_field=False,
79 empty_struct_field=Empty(),
80 byte_list=[-127, -1, 0, 1, 127],
81 i16_list=[-1, 0, 1, 0x7fff],
82 i32_list= [-1, 0, 0xff, 0xffff, 0xffffff, 0x7fffffff],
83 i64_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff],
84 double_list=[0.1, 0.2, 0.3],
85 string_list=["first", "second", "third"],
86 boolean_list=[True, True, True, False, False, False],
87 struct_list=[Empty(), Empty()],
88 byte_set=set([-127, -1, 0, 1, 127]),
89 i16_set=set([-1, 0, 1, 0x7fff]),
90 i32_set=set([1, 2, 3]),
91 i64_set=set([-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff]),
92 double_set=set([0.1, 0.2, 0.3]),
93 string_set=set(["first", "second", "third"]),
94 boolean_set=set([True, False]),
95 #struct_set=set([Empty()]), # unhashable instance
96 byte_byte_map={1 : 2},
97 i16_byte_map={1 : 1, -1 : 1, 0x7fff : 1},
98 i32_byte_map={1 : 1, -1 : 1, 0x7fffffff : 1},
99 i64_byte_map={0 : 1, 1 : 1, -1 : 1, 0x7fffffffffffffff : 1},
100 double_byte_map={-1.1 : 1, 1.1 : 1},
101 string_byte_map={"first" : 1, "second" : 2, "third" : 3, "" : 0},
102 boolean_byte_map={True : 1, False: 0},
103 byte_i16_map={1 : 1, 2 : -1, 3 : 0x7fff},
104 byte_i32_map={1 : 1, 2 : -1, 3 : 0x7fffffff},
105 byte_i64_map={1 : 1, 2 : -1, 3 : 0x7fffffffffffffff},
106 byte_double_map={1 : 0.1, 2 : -0.1, 3 : 1000000.1},
107 byte_string_map={1 : "", 2 : "blah", 3 : "loooooooooooooong string"},
108 byte_boolean_map={1 : True, 2 : False},
109 #list_byte_map # unhashable
110 #set_byte_map={set([1, 2, 3]) : 1, set([0, 1]) : 2, set([]) : 0}, # unhashable
111 #map_byte_map # unhashable
112 byte_map_map={0 : {}, 1 : {1 : 1}, 2 : {1 : 1, 2 : 2}},
113 byte_set_map={0 : set([]), 1 : set([1]), 2 : set([1, 2])},
114 byte_list_map={0 : [], 1 : [1], 2 : [1, 2]},
115 )
116
Roger Meierf4eec7a2011-09-11 18:16:21 +0000117 self.nested_lists_i32x2 = NestedListsI32x2(
118 [
119 [ 1, 1, 2 ],
120 [ 2, 7, 9 ],
121 [ 3, 5, 8 ]
122 ]
123 )
124
125 self.nested_lists_i32x3 = NestedListsI32x3(
126 [
127 [
128 [ 2, 7, 9 ],
129 [ 3, 5, 8 ]
130 ],
131 [
132 [ 1, 1, 2 ],
133 [ 1, 4, 9 ]
134 ]
135 ]
136 )
137
138 self.nested_mixedx2 = NestedMixedx2( int_set_list=[
139 set([1,2,3]),
140 set([1,4,9]),
141 set([1,2,3,5,8,13,21]),
142 set([-1, 0, 1])
143 ],
144 # note, the sets below are sets of chars, since the strings are iterated
145 map_int_strset={ 10:set('abc'), 20:set('def'), 30:set('GHI') },
146 map_int_strset_list=[
147 { 10:set('abc'), 20:set('def'), 30:set('GHI') },
148 { 100:set('lmn'), 200:set('opq'), 300:set('RST') },
149 { 1000:set('uvw'), 2000:set('wxy'), 3000:set('XYZ') }
150 ]
151 )
152
153 self.nested_lists_bonk = NestedListsBonk(
154 [
155 [
156 [
157 Bonk(message='inner A first', type=1),
158 Bonk(message='inner A second', type=1)
159 ],
160 [
161 Bonk(message='inner B first', type=2),
162 Bonk(message='inner B second', type=2)
163 ]
164 ]
165 ]
166 )
167
168 self.list_bonks = ListBonks(
169 [
170 Bonk(message='inner A', type=1),
171 Bonk(message='inner B', type=2),
172 Bonk(message='inner C', type=0)
173 ]
174 )
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000175
David Reiss9ff3b9d2008-02-15 01:10:23 +0000176 def _serialize(self, obj):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000177 trans = TTransport.TMemoryBuffer()
178 prot = self.protocol_factory.getProtocol(trans)
179 obj.write(prot)
180 return trans.getvalue()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000181
182 def _deserialize(self, objtype, data):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000183 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
184 ret = objtype()
185 ret.read(prot)
186 return ret
David Reiss9ff3b9d2008-02-15 01:10:23 +0000187
188 def testForwards(self):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000189 obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
190 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
191 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000192
193 def testBackwards(self):
Roger Meierf4eec7a2011-09-11 18:16:21 +0000194 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
195 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
196 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000197
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000198 def testSerializeV1(self):
199 obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj))
200 self.assertEquals(obj, self.v1obj)
201
202 def testSerializeV2(self):
203 obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj))
204 self.assertEquals(obj, self.v2obj)
205
206 def testBools(self):
207 self.assertNotEquals(self.bools, self.bools_flipped)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000208 self.assertNotEquals(self.bools, self.v1obj)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000209 obj = self._deserialize(Bools, self._serialize(self.bools))
210 self.assertEquals(obj, self.bools)
211 obj = self._deserialize(Bools, self._serialize(self.bools_flipped))
212 self.assertEquals(obj, self.bools_flipped)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000213 rep = repr(self.bools)
214 self.assertTrue(len(rep) > 0)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000215
216 def testLargeDeltas(self):
217 # test large field deltas (meaningful in CompactProto only)
218 obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas))
219 self.assertEquals(obj, self.large_deltas)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000220 rep = repr(self.large_deltas)
221 self.assertTrue(len(rep) > 0)
222
223 def testNestedListsI32x2(self):
224 obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2))
225 self.assertEquals(obj, self.nested_lists_i32x2)
226 rep = repr(self.nested_lists_i32x2)
227 self.assertTrue(len(rep) > 0)
228
229 def testNestedListsI32x3(self):
230 obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3))
231 self.assertEquals(obj, self.nested_lists_i32x3)
232 rep = repr(self.nested_lists_i32x3)
233 self.assertTrue(len(rep) > 0)
234
235 def testNestedMixedx2(self):
236 obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2))
237 self.assertEquals(obj, self.nested_mixedx2)
238 rep = repr(self.nested_mixedx2)
239 self.assertTrue(len(rep) > 0)
240
241 def testNestedListsBonk(self):
242 obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk))
243 self.assertEquals(obj, self.nested_lists_bonk)
244 rep = repr(self.nested_lists_bonk)
245 self.assertTrue(len(rep) > 0)
246
247 def testListBonks(self):
248 obj = self._deserialize(ListBonks, self._serialize(self.list_bonks))
249 self.assertEquals(obj, self.list_bonks)
250 rep = repr(self.list_bonks)
251 self.assertTrue(len(rep) > 0)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000252
253 def testCompactStruct(self):
254 # test large field deltas (meaningful in CompactProto only)
255 obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct))
256 self.assertEquals(obj, self.compact_struct)
Roger Meierf4eec7a2011-09-11 18:16:21 +0000257 rep = repr(self.compact_struct)
258 self.assertTrue(len(rep) > 0)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000259
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200260 def testIntegerLimits(self):
261 bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129),
262 CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769),
263 CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649),
264 CompactProtoTestStruct(a_i64=9223372036854775808), CompactProtoTestStruct(a_i64=-9223372036854775809)
265 ]
266
267 for value in bad_values:
268 self.assertRaises(Exception, self._serialize, value)
269
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900270
David Reiss9ff3b9d2008-02-15 01:10:23 +0000271class NormalBinaryTest(AbstractTest):
272 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
273
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900274
David Reiss9ff3b9d2008-02-15 01:10:23 +0000275class AcceleratedBinaryTest(AbstractTest):
276 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
277
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900278
David Reissabafd792010-09-27 17:28:15 +0000279class CompactProtocolTest(AbstractTest):
280 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000281
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900282
Roger Meierad8154a2012-12-18 21:02:16 +0100283class JSONProtocolTest(AbstractTest):
284 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
285
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900286
David Reiss4c591c92009-01-31 21:39:25 +0000287class AcceleratedFramedTest(unittest.TestCase):
288 def testSplit(self):
289 """Test FramedTransport and BinaryProtocolAccelerated
290
291 Tests that TBinaryProtocolAccelerated and TFramedTransport
292 play nicely together when a read spans a frame"""
293
294 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
295 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z")+1))
296
297 databuf = TTransport.TMemoryBuffer()
298 prot = protocol_factory.getProtocol(databuf)
299 prot.writeI32(42)
300 prot.writeString(bigstring)
301 prot.writeI16(24)
302 data = databuf.getvalue()
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900303 cutpoint = len(data) // 2
David Reiss4c591c92009-01-31 21:39:25 +0000304 parts = [ data[:cutpoint], data[cutpoint:] ]
305
306 framed_buffer = TTransport.TMemoryBuffer()
307 framed_writer = TTransport.TFramedTransport(framed_buffer)
308 for part in parts:
309 framed_writer.write(part)
310 framed_writer.flush()
311 self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
312
313 # Recreate framed_buffer so we can read from it.
314 framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
315 framed_reader = TTransport.TFramedTransport(framed_buffer)
316 prot = protocol_factory.getProtocol(framed_reader)
317 self.assertEqual(prot.readI32(), 42)
318 self.assertEqual(prot.readString(), bigstring)
319 self.assertEqual(prot.readI16(), 24)
320
David Reiss6acc2692010-02-26 00:56:02 +0000321class SerializersTest(unittest.TestCase):
322
323 def testSerializeThenDeserialize(self):
324 obj = Xtruct2(i32_thing=1,
325 struct_thing=Xtruct(string_thing="foo"))
326
327 s1 = serialize(obj)
328 for i in range(10):
329 self.assertEquals(s1, serialize(obj))
330 objcopy = Xtruct2()
331 deserialize(objcopy, serialize(obj))
332 self.assertEquals(obj, objcopy)
333
334 obj = Xtruct(string_thing="bar")
335 objcopy = Xtruct()
336 deserialize(objcopy, serialize(obj))
337 self.assertEquals(obj, objcopy)
David Reiss4c591c92009-01-31 21:39:25 +0000338
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000339 # test booleans
340 obj = Bools(im_true=True, im_false=False)
341 objcopy = Bools()
342 deserialize(objcopy, serialize(obj))
343 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900344
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000345 # test enums
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900346 for num, name in Numberz._VALUES_TO_NAMES.items():
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000347 obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num)
348 objcopy = Bonk()
349 deserialize(objcopy, serialize(obj))
350 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900351
David Reiss4c591c92009-01-31 21:39:25 +0000352
David Reiss9ff3b9d2008-02-15 01:10:23 +0000353def suite():
354 suite = unittest.TestSuite()
355 loader = unittest.TestLoader()
356
357 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
358 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
David Reissabafd792010-09-27 17:28:15 +0000359 suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest))
Roger Meierad8154a2012-12-18 21:02:16 +0100360 suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest))
David Reiss4c591c92009-01-31 21:39:25 +0000361 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
David Reiss6acc2692010-02-26 00:56:02 +0000362 suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
David Reiss9ff3b9d2008-02-15 01:10:23 +0000363 return suite
364
365if __name__ == "__main__":
366 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))