blob: efe3c6d8dda9c61200102ce694f1a86d2cf0cd09 [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])),
Nobuaki Sukegawad479e232016-02-28 11:28:19 +090083 big_numbers=[2 ** 8, 2 ** 16, 2 ** 31 - 1, -(2 ** 31 - 1)]
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090084 )
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=[
Nobuaki Sukegawad479e232016-02-28 11:28:19 +0900163 {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')}]
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900166 )
167
168 self.nested_lists_bonk = NestedListsBonk(
169 [
170 [
171 [
172 Bonk(message='inner A first', type=1),
173 Bonk(message='inner A second', type=1)
174 ],
175 [
176 Bonk(message='inner B first', type=2),
177 Bonk(message='inner B second', type=2)
178 ]
179 ]
180 ]
181 )
182
183 self.list_bonks = ListBonks(
184 [
185 Bonk(message='inner A', type=1),
186 Bonk(message='inner B', type=2),
187 Bonk(message='inner C', type=0)
188 ]
189 )
190
191 def _serialize(self, obj):
192 trans = TTransport.TMemoryBuffer()
193 prot = self.protocol_factory.getProtocol(trans)
194 obj.write(prot)
195 return trans.getvalue()
196
197 def _deserialize(self, objtype, data):
198 prot = self.protocol_factory.getProtocol(TTransport.TMemoryBuffer(data))
199 ret = objtype()
200 ret.read(prot)
201 return ret
202
203 def testForwards(self):
204 obj = self._deserialize(VersioningTestV2, self._serialize(self.v1obj))
205 self.assertEquals(obj.begin_in_both, self.v1obj.begin_in_both)
206 self.assertEquals(obj.end_in_both, self.v1obj.end_in_both)
207
208 def testBackwards(self):
209 obj = self._deserialize(VersioningTestV1, self._serialize(self.v2obj))
210 self.assertEquals(obj.begin_in_both, self.v2obj.begin_in_both)
211 self.assertEquals(obj.end_in_both, self.v2obj.end_in_both)
212
213 def testSerializeV1(self):
214 obj = self._deserialize(VersioningTestV1, self._serialize(self.v1obj))
215 self.assertEquals(obj, self.v1obj)
216
217 def testSerializeV2(self):
218 obj = self._deserialize(VersioningTestV2, self._serialize(self.v2obj))
219 self.assertEquals(obj, self.v2obj)
220
221 def testBools(self):
222 self.assertNotEquals(self.bools, self.bools_flipped)
223 self.assertNotEquals(self.bools, self.v1obj)
224 obj = self._deserialize(Bools, self._serialize(self.bools))
225 self.assertEquals(obj, self.bools)
226 obj = self._deserialize(Bools, self._serialize(self.bools_flipped))
227 self.assertEquals(obj, self.bools_flipped)
228 rep = repr(self.bools)
229 self.assertTrue(len(rep) > 0)
230
231 def testLargeDeltas(self):
232 # test large field deltas (meaningful in CompactProto only)
233 obj = self._deserialize(LargeDeltas, self._serialize(self.large_deltas))
234 self.assertEquals(obj, self.large_deltas)
235 rep = repr(self.large_deltas)
236 self.assertTrue(len(rep) > 0)
237
238 def testNestedListsI32x2(self):
239 obj = self._deserialize(NestedListsI32x2, self._serialize(self.nested_lists_i32x2))
240 self.assertEquals(obj, self.nested_lists_i32x2)
241 rep = repr(self.nested_lists_i32x2)
242 self.assertTrue(len(rep) > 0)
243
244 def testNestedListsI32x3(self):
245 obj = self._deserialize(NestedListsI32x3, self._serialize(self.nested_lists_i32x3))
246 self.assertEquals(obj, self.nested_lists_i32x3)
247 rep = repr(self.nested_lists_i32x3)
248 self.assertTrue(len(rep) > 0)
249
250 def testNestedMixedx2(self):
251 obj = self._deserialize(NestedMixedx2, self._serialize(self.nested_mixedx2))
252 self.assertEquals(obj, self.nested_mixedx2)
253 rep = repr(self.nested_mixedx2)
254 self.assertTrue(len(rep) > 0)
255
256 def testNestedListsBonk(self):
257 obj = self._deserialize(NestedListsBonk, self._serialize(self.nested_lists_bonk))
258 self.assertEquals(obj, self.nested_lists_bonk)
259 rep = repr(self.nested_lists_bonk)
260 self.assertTrue(len(rep) > 0)
261
262 def testListBonks(self):
263 obj = self._deserialize(ListBonks, self._serialize(self.list_bonks))
264 self.assertEquals(obj, self.list_bonks)
265 rep = repr(self.list_bonks)
266 self.assertTrue(len(rep) > 0)
267
268 def testCompactStruct(self):
269 # test large field deltas (meaningful in CompactProto only)
270 obj = self._deserialize(CompactProtoTestStruct, self._serialize(self.compact_struct))
271 self.assertEquals(obj, self.compact_struct)
272 rep = repr(self.compact_struct)
273 self.assertTrue(len(rep) > 0)
274
275 def testIntegerLimits(self):
276 if (sys.version_info[0] == 2 and sys.version_info[1] <= 6):
277 print('Skipping testIntegerLimits for Python 2.6')
278 return
279 bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129),
280 CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769),
281 CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649),
282 CompactProtoTestStruct(a_i64=9223372036854775808), CompactProtoTestStruct(a_i64=-9223372036854775809)
283 ]
284
285 for value in bad_values:
286 self.assertRaises(Exception, self._serialize, value)
Konrad Grochowskid5f3be52014-10-08 15:32:21 +0200287
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900288
David Reiss9ff3b9d2008-02-15 01:10:23 +0000289class NormalBinaryTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900290 protocol_factory = TBinaryProtocol.TBinaryProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000291
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900292
David Reiss9ff3b9d2008-02-15 01:10:23 +0000293class AcceleratedBinaryTest(AbstractTest):
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900294 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False)
David Reiss9ff3b9d2008-02-15 01:10:23 +0000295
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900296
David Reissabafd792010-09-27 17:28:15 +0000297class CompactProtocolTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900298 protocol_factory = TCompactProtocol.TCompactProtocolFactory()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000299
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900300
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900301class AcceleratedCompactTest(AbstractTest):
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900302 protocol_factory = TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False)
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900303
304
Roger Meierad8154a2012-12-18 21:02:16 +0100305class JSONProtocolTest(AbstractTest):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900306 protocol_factory = TJSONProtocol.TJSONProtocolFactory()
Roger Meierad8154a2012-12-18 21:02:16 +0100307
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900308
David Reiss4c591c92009-01-31 21:39:25 +0000309class AcceleratedFramedTest(unittest.TestCase):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900310 def testSplit(self):
311 """Test FramedTransport and BinaryProtocolAccelerated
David Reiss4c591c92009-01-31 21:39:25 +0000312
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900313 Tests that TBinaryProtocolAccelerated and TFramedTransport
314 play nicely together when a read spans a frame"""
David Reiss4c591c92009-01-31 21:39:25 +0000315
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900316 protocol_factory = TBinaryProtocol.TBinaryProtocolAcceleratedFactory()
317 bigstring = "".join(chr(byte) for byte in range(ord("a"), ord("z") + 1))
David Reiss4c591c92009-01-31 21:39:25 +0000318
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900319 databuf = TTransport.TMemoryBuffer()
320 prot = protocol_factory.getProtocol(databuf)
321 prot.writeI32(42)
322 prot.writeString(bigstring)
323 prot.writeI16(24)
324 data = databuf.getvalue()
325 cutpoint = len(data) // 2
326 parts = [data[:cutpoint], data[cutpoint:]]
David Reiss4c591c92009-01-31 21:39:25 +0000327
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900328 framed_buffer = TTransport.TMemoryBuffer()
329 framed_writer = TTransport.TFramedTransport(framed_buffer)
330 for part in parts:
331 framed_writer.write(part)
332 framed_writer.flush()
333 self.assertEquals(len(framed_buffer.getvalue()), len(data) + 8)
David Reiss4c591c92009-01-31 21:39:25 +0000334
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900335 # Recreate framed_buffer so we can read from it.
336 framed_buffer = TTransport.TMemoryBuffer(framed_buffer.getvalue())
337 framed_reader = TTransport.TFramedTransport(framed_buffer)
338 prot = protocol_factory.getProtocol(framed_reader)
339 self.assertEqual(prot.readI32(), 42)
340 self.assertEqual(prot.readString(), bigstring)
341 self.assertEqual(prot.readI16(), 24)
342
David Reiss4c591c92009-01-31 21:39:25 +0000343
David Reiss6acc2692010-02-26 00:56:02 +0000344class SerializersTest(unittest.TestCase):
345
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900346 def testSerializeThenDeserialize(self):
347 obj = Xtruct2(i32_thing=1,
348 struct_thing=Xtruct(string_thing="foo"))
David Reiss6acc2692010-02-26 00:56:02 +0000349
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900350 s1 = serialize(obj)
351 for i in range(10):
352 self.assertEquals(s1, serialize(obj))
353 objcopy = Xtruct2()
354 deserialize(objcopy, serialize(obj))
355 self.assertEquals(obj, objcopy)
David Reiss6acc2692010-02-26 00:56:02 +0000356
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900357 obj = Xtruct(string_thing="bar")
358 objcopy = Xtruct()
359 deserialize(objcopy, serialize(obj))
360 self.assertEquals(obj, objcopy)
David Reiss4c591c92009-01-31 21:39:25 +0000361
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900362 # test booleans
363 obj = Bools(im_true=True, im_false=False)
364 objcopy = Bools()
365 deserialize(objcopy, serialize(obj))
366 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900367
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900368 # test enums
369 for num, name in Numberz._VALUES_TO_NAMES.items():
370 obj = Bonk(message='enum Numberz value %d is string %s' % (num, name), type=num)
371 objcopy = Bonk()
372 deserialize(objcopy, serialize(obj))
373 self.assertEquals(obj, objcopy)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +0900374
David Reiss4c591c92009-01-31 21:39:25 +0000375
David Reiss9ff3b9d2008-02-15 01:10:23 +0000376def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900377 suite = unittest.TestSuite()
378 loader = unittest.TestLoader()
David Reiss9ff3b9d2008-02-15 01:10:23 +0000379
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900380 suite.addTest(loader.loadTestsFromTestCase(NormalBinaryTest))
381 suite.addTest(loader.loadTestsFromTestCase(AcceleratedBinaryTest))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900382 suite.addTest(loader.loadTestsFromTestCase(AcceleratedCompactTest))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900383 suite.addTest(loader.loadTestsFromTestCase(CompactProtocolTest))
384 suite.addTest(loader.loadTestsFromTestCase(JSONProtocolTest))
385 suite.addTest(loader.loadTestsFromTestCase(AcceleratedFramedTest))
386 suite.addTest(loader.loadTestsFromTestCase(SerializersTest))
387 return suite
David Reiss9ff3b9d2008-02-15 01:10:23 +0000388
389if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900390 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))