blob: 6614638222d805d470708c8c4b0bfae5ada4dbd0 [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
David Reissabafd792010-09-27 17:28:15 +000031 def make_data(self, pfactory=None):
Mark Slee53d9c0c2007-11-26 21:15:40 +000032 trans = TTransport.TMemoryBuffer()
David Reissabafd792010-09-27 17:28:15 +000033 if pfactory:
34 prot = pfactory.getProtocol(trans)
35 else:
36 prot = TBinaryProtocol.TBinaryProtocol(trans)
Mark Slee53d9c0c2007-11-26 21:15:40 +000037
38 x = Xtruct()
39 x.string_thing = "Zero"
40 x.byte_thing = 0
41
42 x.write(prot)
43
44 x = Xtruct()
45 x.string_thing = "One"
46 x.byte_thing = 1
47
48 x.write(prot)
49
David Reissabafd792010-09-27 17:28:15 +000050 return trans.getvalue()
Mark Slee53d9c0c2007-11-26 21:15:40 +000051
52 def testTransportReadAll(self):
53 """Test that readAll on any type of transport throws an EOFError"""
David Reissabafd792010-09-27 17:28:15 +000054 trans = TTransport.TMemoryBuffer(self.make_data())
Mark Slee53d9c0c2007-11-26 21:15:40 +000055 trans.readAll(1)
56
57 try:
58 trans.readAll(10000)
59 except EOFError:
60 return
61
62 self.fail("Should have gotten EOFError")
63
64 def eofTestHelper(self, pfactory):
David Reissabafd792010-09-27 17:28:15 +000065 trans = TTransport.TMemoryBuffer(self.make_data(pfactory))
Mark Slee53d9c0c2007-11-26 21:15:40 +000066 prot = pfactory.getProtocol(trans)
67
68 x = Xtruct()
69 x.read(prot)
70 self.assertEqual(x.string_thing, "Zero")
71 self.assertEqual(x.byte_thing, 0)
72
73 x = Xtruct()
74 x.read(prot)
75 self.assertEqual(x.string_thing, "One")
76 self.assertEqual(x.byte_thing, 1)
77
78 try:
79 x = Xtruct()
80 x.read(prot)
81 except EOFError:
82 return
83
84 self.fail("Should have gotten EOFError")
85
86 def eofTestHelperStress(self, pfactory):
87 """Teest the ability of TBinaryProtocol to deal with the removal of every byte in the file"""
88 # TODO: we should make sure this covers more of the code paths
89
David Reissabafd792010-09-27 17:28:15 +000090 data = self.make_data(pfactory)
Nobuaki Sukegawa760511f2015-11-06 21:24:16 +090091 for i in range(0, len(data) + 1):
David Reissabafd792010-09-27 17:28:15 +000092 trans = TTransport.TMemoryBuffer(data[0:i])
Mark Slee53d9c0c2007-11-26 21:15:40 +000093 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
108 def testBinaryProtocolAcceleratedEof(self):
109 """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream"""
110 self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
111 self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory())
112
David Reissabafd792010-09-27 17:28:15 +0000113 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())
117
Nobuaki Sukegawacacce2f2015-11-08 23:43:55 +0900118
David Reiss9ff3b9d2008-02-15 01:10:23 +0000119def suite():
120 suite = unittest.TestSuite()
121 loader = unittest.TestLoader()
122 suite.addTest(loader.loadTestsFromTestCase(TestEof))
123 return suite
Mark Slee53d9c0c2007-11-26 21:15:40 +0000124
David Reiss9ff3b9d2008-02-15 01:10:23 +0000125if __name__ == "__main__":
126 unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2))