blob: 0b4a829601471cee4e2a72bf4aab286a60c73a4a [file] [log] [blame]
Mark Slee53d9c0c2007-11-26 21:15:40 +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 Sukegawacacce2f2015-11-08 23:43:55 +090022from ThriftTest.ttypes import Xtruct
Mark Slee53d9c0c2007-11-26 21:15:40 +000023from thrift.transport import TTransport
Mark Slee53d9c0c2007-11-26 21:15:40 +000024from thrift.protocol import TBinaryProtocol
David Reissabafd792010-09-27 17:28:15 +000025from thrift.protocol import TCompactProtocol
Mark Slee53d9c0c2007-11-26 21:15:40 +000026import unittest
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +090027
Mark Slee53d9c0c2007-11-26 21:15:40 +000028
29class TestEof(unittest.TestCase):
30
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090031 def make_data(self, pfactory=None):
32 trans = TTransport.TMemoryBuffer()
33 if pfactory:
34 prot = pfactory.getProtocol(trans)
35 else:
36 prot = TBinaryProtocol.TBinaryProtocol(trans)
Mark Slee53d9c0c2007-11-26 21:15:40 +000037
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090038 x = Xtruct()
39 x.string_thing = "Zero"
40 x.byte_thing = 0
Mark Slee53d9c0c2007-11-26 21:15:40 +000041
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090042 x.write(prot)
Mark Slee53d9c0c2007-11-26 21:15:40 +000043
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090044 x = Xtruct()
45 x.string_thing = "One"
46 x.byte_thing = 1
Mark Slee53d9c0c2007-11-26 21:15:40 +000047
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090048 x.write(prot)
Mark Slee53d9c0c2007-11-26 21:15:40 +000049
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090050 return trans.getvalue()
Mark Slee53d9c0c2007-11-26 21:15:40 +000051
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090052 def testTransportReadAll(self):
53 """Test that readAll on any type of transport throws an EOFError"""
54 trans = TTransport.TMemoryBuffer(self.make_data())
55 trans.readAll(1)
Mark Slee53d9c0c2007-11-26 21:15:40 +000056
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090057 try:
58 trans.readAll(10000)
59 except EOFError:
60 return
Mark Slee53d9c0c2007-11-26 21:15:40 +000061
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090062 self.fail("Should have gotten EOFError")
Mark Slee53d9c0c2007-11-26 21:15:40 +000063
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090064 def eofTestHelper(self, pfactory):
65 trans = TTransport.TMemoryBuffer(self.make_data(pfactory))
66 prot = pfactory.getProtocol(trans)
Mark Slee53d9c0c2007-11-26 21:15:40 +000067
Mark Slee53d9c0c2007-11-26 21:15:40 +000068 x = Xtruct()
69 x.read(prot)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090070 self.assertEqual(x.string_thing, "Zero")
71 self.assertEqual(x.byte_thing, 0)
72
73 x = Xtruct()
Mark Slee53d9c0c2007-11-26 21:15:40 +000074 x.read(prot)
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090075 self.assertEqual(x.string_thing, "One")
76 self.assertEqual(x.byte_thing, 1)
Mark Slee53d9c0c2007-11-26 21:15:40 +000077
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090078 try:
79 x = Xtruct()
80 x.read(prot)
81 except EOFError:
82 return
Mark Slee53d9c0c2007-11-26 21:15:40 +000083
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090084 self.fail("Should have gotten EOFError")
Mark Slee53d9c0c2007-11-26 21:15:40 +000085
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090086 def eofTestHelperStress(self, pfactory):
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +090087 """Test the ability of TBinaryProtocol to deal with the removal of every byte in the file"""
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090088 # TODO: we should make sure this covers more of the code paths
89
90 data = self.make_data(pfactory)
91 for i in range(0, len(data) + 1):
92 trans = TTransport.TMemoryBuffer(data[0:i])
93 prot = pfactory.getProtocol(trans)
94 try:
95 x = Xtruct()
96 x.read(prot)
97 x.read(prot)
98 x.read(prot)
99 except EOFError:
100 continue
101 self.fail("Should have gotten an EOFError")
102
103 def testBinaryProtocolEof(self):
104 """Test that TBinaryProtocol throws an EOFError when it reaches the end of the stream"""
105 self.eofTestHelper(TBinaryProtocol.TBinaryProtocolFactory())
106 self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolFactory())
107
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900108 def testBinaryProtocolAcceleratedBinaryEof(self):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900109 """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900110 self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False))
111 self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False))
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900112
113 def testCompactProtocolEof(self):
114 """Test that TCompactProtocol throws an EOFError when it reaches the end of the stream"""
115 self.eofTestHelper(TCompactProtocol.TCompactProtocolFactory())
116 self.eofTestHelperStress(TCompactProtocol.TCompactProtocolFactory())
David Reissabafd792010-09-27 17:28:15 +0000117
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900118 def testCompactProtocolAcceleratedCompactEof(self):
119 """Test that TCompactProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
Nobuaki Sukegawa7af189a2016-02-11 16:21:01 +0900120 self.eofTestHelper(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False))
121 self.eofTestHelperStress(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False))
Nobuaki Sukegawa6525f6a2016-02-11 13:58:39 +0900122
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900123
David Reiss9ff3b9d2008-02-15 01:10:23 +0000124def suite():
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900125 suite = unittest.TestSuite()
126 loader = unittest.TestLoader()
127 suite.addTest(loader.loadTestsFromTestCase(TestEof))
128 return suite
Mark Slee53d9c0c2007-11-26 21:15:40 +0000129
James E. King, III0ad20bd2017-09-30 15:44:16 -0700130
David Reiss9ff3b9d2008-02-15 01:10:23 +0000131if __name__ == "__main__":
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900132 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))