blob: f4f3a4fe673a294092a4a1370dfe726a4c7b06ec [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
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090022from ThriftTest.ttypes import (
23 Bonk,
24 Bools,
25 LargeDeltas,
26 ListBonks,
27 NestedListsBonk,
28 NestedListsI32x2,
29 NestedListsI32x3,
30 NestedMixedx2,
31 Numberz,
32 VersioningTestV1,
33 VersioningTestV2,
34 Xtruct,
35 Xtruct2,
36)
37
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000038from DebugProtoTest.ttypes import CompactProtoTestStruct, Empty
David Reiss9ff3b9d2008-02-15 01:10:23 +000039from thrift.transport import TTransport
Roger Meierad8154a2012-12-18 21:02:16 +010040from thrift.protocol import TBinaryProtocol, TCompactProtocol, TJSONProtocol
David Reiss6acc2692010-02-26 00:56:02 +000041from thrift.TSerialization import serialize, deserialize
Nobuaki Sukegawa33744b02016-01-03 14:24:39 +090042import sys
David Reiss9ff3b9d2008-02-15 01:10:23 +000043import unittest
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090044
David Reiss9ff3b9d2008-02-15 01:10:23 +000045
46class AbstractTest(unittest.TestCase):
47
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090048 def setUp(self):
49 self.v1obj = VersioningTestV1(
50 begin_in_both=12345,
51 old_string='aaa',
52 end_in_both=54321,
53 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000054
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090055 self.v2obj = VersioningTestV2(
56 begin_in_both=12345,
57 newint=1,
58 newbyte=2,
59 newshort=3,
60 newlong=4,
61 newdouble=5.0,
62 newstruct=Bonk(message="Hello!", type=123),
63 newlist=[7, 8, 9],
64 newset=set([42, 1, 8]),
65 newmap={1: 2, 2: 3},
66 newstring="Hola!",
67 end_in_both=54321,
68 )
David Reiss9ff3b9d2008-02-15 01:10:23 +000069
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090070 self.bools = Bools(im_true=True, im_false=False)
71 self.bools_flipped = Bools(im_true=False, im_false=True)
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000072
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090073 self.large_deltas = LargeDeltas(
74 b1=self.bools,
75 b10=self.bools_flipped,
76 b100=self.bools,
77 check_true=True,
78 b1000=self.bools_flipped,
79 check_false=False,
80 vertwo2000=VersioningTestV2(newstruct=Bonk(message='World!', type=314)),
81 a_set2500=set(['lazy', 'brown', 'cow']),
82 vertwo3000=VersioningTestV2(newset=set([2, 3, 5, 7, 11])),
83 big_numbers=[2**8, 2**16, 2**31 - 1, -(2**31 - 1)]
84 )
Bryan Duxburydf4cffd2011-03-15 17:16:09 +000085
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090086 self.compact_struct = CompactProtoTestStruct(
87 a_byte=127,
88 a_i16=32000,
89 a_i32=1000000000,
90 a_i64=0xffffffffff,
91 a_double=5.6789,
92 a_string="my string",
93 true_field=True,
94 false_field=False,
95 empty_struct_field=Empty(),
96 byte_list=[-127, -1, 0, 1, 127],
97 i16_list=[-1, 0, 1, 0x7fff],
98 i32_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0x7fffffff],
99 i64_list=[-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff],
100 double_list=[0.1, 0.2, 0.3],
101 string_list=["first", "second", "third"],
102 boolean_list=[True, True, True, False, False, False],
103 struct_list=[Empty(), Empty()],
104 byte_set=set([-127, -1, 0, 1, 127]),
105 i16_set=set([-1, 0, 1, 0x7fff]),
106 i32_set=set([1, 2, 3]),
107 i64_set=set([-1, 0, 0xff, 0xffff, 0xffffff, 0xffffffff, 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0x7fffffffffffffff]),
108 double_set=set([0.1, 0.2, 0.3]),
109 string_set=set(["first", "second", "third"]),
110 boolean_set=set([True, False]),
111 # struct_set=set([Empty()]), # unhashable instance
112 byte_byte_map={1: 2},
113 i16_byte_map={1: 1, -1: 1, 0x7fff: 1},
114 i32_byte_map={1: 1, -1: 1, 0x7fffffff: 1},
115 i64_byte_map={0: 1, 1: 1, -1: 1, 0x7fffffffffffffff: 1},
116 double_byte_map={-1.1: 1, 1.1: 1},
117 string_byte_map={"first": 1, "second": 2, "third": 3, "": 0},
118 boolean_byte_map={True: 1, False: 0},
119 byte_i16_map={1: 1, 2: -1, 3: 0x7fff},
120 byte_i32_map={1: 1, 2: -1, 3: 0x7fffffff},
121 byte_i64_map={1: 1, 2: -1, 3: 0x7fffffffffffffff},
122 byte_double_map={1: 0.1, 2: -0.1, 3: 1000000.1},
123 byte_string_map={1: "", 2: "blah", 3: "loooooooooooooong string"},
124 byte_boolean_map={1: True, 2: False},
125 # list_byte_map # unhashable
126 # set_byte_map={set([1, 2, 3]) : 1, set([0, 1]) : 2, set([]) : 0}, # unhashable
127 # map_byte_map # unhashable
128 byte_map_map={0: {}, 1: {1: 1}, 2: {1: 1, 2: 2}},
129 byte_set_map={0: set([]), 1: set([1]), 2: set([1, 2])},
130 byte_list_map={0: [], 1: [1], 2: [1, 2]},
131 )
Bryan Duxburydf4cffd2011-03-15 17:16:09 +0000132
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900133 self.nested_lists_i32x2 = NestedListsI32x2(
134 [
135 [1, 1, 2],
136 [2, 7, 9],
137 [3, 5, 8]
138 ]
139 )
Roger Meierf4eec7a2011-09-11 18:16:21 +0000140
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900141 self.nested_lists_i32x3 = NestedListsI32x3(
142 [
143 [
144 [2, 7, 9],
145 [3, 5, 8]
146 ],
147 [
148 [1, 1, 2],
149 [1, 4, 9]
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200150 ]
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900151 ]
152 )
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200153
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900154 self.nested_mixedx2 = NestedMixedx2(int_set_list=[
155 set([1, 2, 3]),
156 set([1, 4, 9]),
157 set([1, 2, 3, 5, 8, 13, 21]),
158 set([-1, 0, 1])
159 ],
160 # note, the sets below are sets of chars, since the strings are iterated
161 map_int_strset={10: set('abc'), 20: set('def'), 30: set('GHI')},
162 map_int_strset_list=[
163 {10: set('abc'), 20: set('def'), 30: set('GHI')},
164 {100: set('lmn'), 200: set('opq'), 300: set('RST')},
165 {1000: set('uvw'), 2000: set('wxy'), 3000: set('XYZ')}
166 ]
167 )
168
169 self.nested_lists_bonk = NestedListsBonk(
170 [
171 [
172 [
173 Bonk(message='inner A first', type=1),
174 Bonk(message='inner A second', type=1)
175 ],
176 [
177 Bonk(message='inner B first', type=2),
178 Bonk(message='inner B second', type=2)
179 ]
180 ]
181 ]
182 )
183
184 self.list_bonks = ListBonks(
185 [
186 Bonk(message='inner A', type=1),
187 Bonk(message='inner B', type=2),
188 Bonk(message='inner C', type=0)
189 ]
190 )
191
192 def _serialize(self, obj):
193 trans = TTransport.TMemoryBuffer()
194 prot = self.protocol_factory.getProtocol(trans)
195 obj.write(prot)
196 return trans.getvalue()
197
198 def _deserialize(self, objtype, data):
199 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
200 ret = objtype()
201 ret.read(prot)
202 return ret
203
204 def testForwards(self):
205 obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
206 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
207 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
208
209 def testBackwards(self):
210 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
211 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
212 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
213
214 def testSerializeV1(self):
215 obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj))
216 self.assertEquals(obj, self.v1obj)
217
218 def testSerializeV2(self):
219 obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj))
220 self.assertEquals(obj, self.v2obj)
221
222 def testBools(self):
223 self.assertNotEquals(self.bools, self.bools_flipped)
224 self.assertNotEquals(self.bools, self.v1obj)
225 obj = self._deserialize(Bools, self._serialize(self.bools))
226 self.assertEquals(obj, self.bools)
227 obj = self._deserialize(Bools, self._serialize(self.bools_flipped))
228 self.assertEquals(obj, self.bools_flipped)
229 rep = repr(self.bools)
230 self.assertTrue(len(rep) > 0)
231
232 def testLargeDeltas(self):
233 # test large field deltas (meaningful in CompactProto only)
234 obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas))
235 self.assertEquals(obj, self.large_deltas)
236 rep = repr(self.large_deltas)
237 self.assertTrue(len(rep) > 0)
238
239 def testNestedListsI32x2(self):
240 obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2))
241 self.assertEquals(obj, self.nested_lists_i32x2)
242 rep = repr(self.nested_lists_i32x2)
243 self.assertTrue(len(rep) > 0)
244
245 def testNestedListsI32x3(self):
246 obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3))
247 self.assertEquals(obj, self.nested_lists_i32x3)
248 rep = repr(self.nested_lists_i32x3)
249 self.assertTrue(len(rep) > 0)
250
251 def testNestedMixedx2(self):
252 obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2))
253 self.assertEquals(obj, self.nested_mixedx2)
254 rep = repr(self.nested_mixedx2)
255 self.assertTrue(len(rep) > 0)
256
257 def testNestedListsBonk(self):
258 obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk))
259 self.assertEquals(obj, self.nested_lists_bonk)
260 rep = repr(self.nested_lists_bonk)
261 self.assertTrue(len(rep) > 0)
262
263 def testListBonks(self):
264 obj = self._deserialize(ListBonks, self._serialize(self.list_bonks))
265 self.assertEquals(obj, self.list_bonks)
266 rep = repr(self.list_bonks)
267 self.assertTrue(len(rep) > 0)
268
269 def testCompactStruct(self):
270 # test large field deltas (meaningful in CompactProto only)
271 obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct))
272 self.assertEquals(obj, self.compact_struct)
273 rep = repr(self.compact_struct)
274 self.assertTrue(len(rep) > 0)
275
276 def testIntegerLimits(self):
277 if (sys.version_info[0] == 2 and sys.version_info[1] <= 6):
278 print('Skipping testIntegerLimits for Python 2.6')
279 return
280 bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129),
281 CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769),
282 CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649),
283 CompactProtoTestStruct(a_i64=9223372036854775808), CompactProtoTestStruct(a_i64=-9223372036854775809)
284 ]
285
286 for value in bad_values:
287 self.assertRaises(Exception, self._serialize, value)
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200288
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900289
David Reiss9ff3b9d2008-02-15 01:10:23 +0000290class NormalBinaryTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900291 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000292
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900293
David Reiss9ff3b9d2008-02-15 01:10:23 +0000294class AcceleratedBinaryTest(AbstractTest):
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900295 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000296
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900297
David Reissabafd792010-09-27 17:28:15 +0000298class CompactProtocolTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900299 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000300
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900301
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900302class AcceleratedCompactTest(AbstractTest):
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900303 protocol_factory = TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False)
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900304
305
Roger Meierad8154a2012-12-18 21:02:16 +0100306class JSONProtocolTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900307 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
Roger Meierad8154a2012-12-18 21:02:16 +0100308
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900309
David Reiss4c591c92009-01-31 21:39:25 +0000310class AcceleratedFramedTest(unittest.TestCase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900311 def testSplit(self):
312 """Test FramedTransport and BinaryProtocolAccelerated
David Reiss4c591c92009-01-31 21:39:25 +0000313
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900314 Tests that TBinaryProtocolAccelerated and TFramedTransport
315 play nicely together when a read spans a frame"""
David Reiss4c591c92009-01-31 21:39:25 +0000316
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900317 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
318 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z") + 1))
David Reiss4c591c92009-01-31 21:39:25 +0000319
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900320 databuf = TTransport.TMemoryBuffer()
321 prot = protocol_factory.getProtocol(databuf)
322 prot.writeI32(42)
323 prot.writeString(bigstring)
324 prot.writeI16(24)
325 data = databuf.getvalue()
326 cutpoint = len(data) // 2
327 parts = [data[:cutpoint], data[cutpoint:]]
David Reiss4c591c92009-01-31 21:39:25 +0000328
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900329 framed_buffer = TTransport.TMemoryBuffer()
330 framed_writer = TTransport.TFramedTransport(framed_buffer)
331 for part in parts:
332 framed_writer.write(part)
333 framed_writer.flush()
334 self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
David Reiss4c591c92009-01-31 21:39:25 +0000335
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900336 # Recreate framed_buffer so we can read from it.
337 framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
338 framed_reader = TTransport.TFramedTransport(framed_buffer)
339 prot = protocol_factory.getProtocol(framed_reader)
340 self.assertEqual(prot.readI32(), 42)
341 self.assertEqual(prot.readString(), bigstring)
342 self.assertEqual(prot.readI16(), 24)
343
David Reiss4c591c92009-01-31 21:39:25 +0000344
David Reiss6acc2692010-02-26 00:56:02 +0000345class SerializersTest(unittest.TestCase):
346
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900347 def testSerializeThenDeserialize(self):
348 obj = Xtruct2(i32_thing=1,
349 struct_thing=Xtruct(string_thing="foo"))
David Reiss6acc2692010-02-26 00:56:02 +0000350
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900351 s1 = serialize(obj)
352 for i in range(10):
353 self.assertEquals(s1, serialize(obj))
354 objcopy = Xtruct2()
355 deserialize(objcopy, serialize(obj))
356 self.assertEquals(obj, objcopy)
David Reiss6acc2692010-02-26 00:56:02 +0000357
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900358 obj = Xtruct(string_thing="bar")
359 objcopy = Xtruct()
360 deserialize(objcopy, serialize(obj))
361 self.assertEquals(obj, objcopy)
David Reiss4c591c92009-01-31 21:39:25 +0000362
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900363 # test booleans
364 obj = Bools(im_true=True, im_false=False)
365 objcopy = Bools()
366 deserialize(objcopy, serialize(obj))
367 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900368
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900369 # test enums
370 for num, name in Numberz._VALUES_TO_NAMES.items():
371 obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num)
372 objcopy = Bonk()
373 deserialize(objcopy, serialize(obj))
374 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900375
David Reiss4c591c92009-01-31 21:39:25 +0000376
David Reiss9ff3b9d2008-02-15 01:10:23 +0000377def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900378 suite = unittest.TestSuite()
379 loader = unittest.TestLoader()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000380
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900381 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
382 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900383 suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900384 suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest))
385 suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest))
386 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
387 suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
388 return suite
David Reiss9ff3b9d2008-02-15 01:10:23 +0000389
390if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900391 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))