Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 3 | # |
| 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 Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 22 | from ThriftTest.ttypes import Xtruct |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 23 | from thrift.transport import TTransport |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 24 | from thrift.protocol import TBinaryProtocol |
David Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 25 | from thrift.protocol import TCompactProtocol |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 26 | import unittest |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 27 | |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 28 | |
| 29 | class TestEof(unittest.TestCase): |
| 30 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 31 | 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 Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 37 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 38 | x = Xtruct() |
| 39 | x.string_thing = "Zero" |
| 40 | x.byte_thing = 0 |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 41 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 42 | x.write(prot) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 43 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 44 | x = Xtruct() |
| 45 | x.string_thing = "One" |
| 46 | x.byte_thing = 1 |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 47 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 48 | x.write(prot) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 49 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 50 | return trans.getvalue() |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 51 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 52 | 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 Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 56 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 57 | try: |
| 58 | trans.readAll(10000) |
| 59 | except EOFError: |
| 60 | return |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 61 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 62 | self.fail("Should have gotten EOFError") |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 63 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 64 | def eofTestHelper(self, pfactory): |
| 65 | trans = TTransport.TMemoryBuffer(self.make_data(pfactory)) |
| 66 | prot = pfactory.getProtocol(trans) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 67 | |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 68 | x = Xtruct() |
| 69 | x.read(prot) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 70 | self.assertEqual(x.string_thing, "Zero") |
| 71 | self.assertEqual(x.byte_thing, 0) |
| 72 | |
| 73 | x = Xtruct() |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 74 | x.read(prot) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 75 | self.assertEqual(x.string_thing, "One") |
| 76 | self.assertEqual(x.byte_thing, 1) |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 77 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 78 | try: |
| 79 | x = Xtruct() |
| 80 | x.read(prot) |
| 81 | except EOFError: |
| 82 | return |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 83 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 84 | self.fail("Should have gotten EOFError") |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 85 | |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 86 | def eofTestHelperStress(self, pfactory): |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 87 | """Test the ability of TBinaryProtocol to deal with the removal of every byte in the file""" |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 88 | # 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 Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 108 | def testBinaryProtocolAcceleratedBinaryEof(self): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 109 | """Test that TBinaryProtocolAccelerated throws an EOFError when it reaches the end of the stream""" |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 110 | self.eofTestHelper(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False)) |
| 111 | self.eofTestHelperStress(TBinaryProtocol.TBinaryProtocolAcceleratedFactory(fallback=False)) |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 112 | |
| 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 Reiss | abafd79 | 2010-09-27 17:28:15 +0000 | [diff] [blame] | 117 | |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 118 | def testCompactProtocolAcceleratedCompactEof(self): |
| 119 | """Test that TCompactProtocolAccelerated throws an EOFError when it reaches the end of the stream""" |
Nobuaki Sukegawa | 7af189a | 2016-02-11 16:21:01 +0900 | [diff] [blame] | 120 | self.eofTestHelper(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False)) |
| 121 | self.eofTestHelperStress(TCompactProtocol.TCompactProtocolAcceleratedFactory(fallback=False)) |
Nobuaki Sukegawa | 6525f6a | 2016-02-11 13:58:39 +0900 | [diff] [blame] | 122 | |
Nobuaki Sukegawa | cacce2f | 2015-11-08 23:43:55 +0900 | [diff] [blame] | 123 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 124 | def suite(): |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 125 | suite = unittest.TestSuite() |
| 126 | loader = unittest.TestLoader() |
| 127 | suite.addTest(loader.loadTestsFromTestCase(TestEof)) |
| 128 | return suite |
Mark Slee | 53d9c0c | 2007-11-26 21:15:40 +0000 | [diff] [blame] | 129 | |
James E. King, III | 0ad20bd | 2017-09-30 15:44:16 -0700 | [diff] [blame] | 130 | |
David Reiss | 9ff3b9d | 2008-02-15 01:10:23 +0000 | [diff] [blame] | 131 | if __name__ == "__main__": |
Nobuaki Sukegawa | 10308cb | 2016-02-03 01:57:03 +0900 | [diff] [blame] | 132 | unittest.main(defaultTest="suite", testRunner=unittest.TextTestRunner(verbosity=2)) |