| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 1 | # |
| 2 | # Licensed to the Apache Software Foundation (ASF) under one |
| 3 | # or more contributor license agreements. See the NOTICE file |
| 4 | # distributed with this work for additional information |
| 5 | # regarding copyright ownership. The ASF licenses this file |
| 6 | # to you under the Apache License, Version 2.0 (the |
| 7 | # "License"); you may not use this file except in compliance |
| 8 | # with the License. You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, |
| 13 | # software distributed under the License is distributed on an |
| 14 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | # KIND, either express or implied. See the License for the |
| 16 | # specific language governing permissions and limitations |
| 17 | # under the License. |
| 18 | # |
| 19 | |
| 20 | import unittest |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 21 | import uuid |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 22 | |
| 23 | import _import_local_thrift # noqa |
| 24 | from thrift.protocol.TBinaryProtocol import TBinaryProtocol |
| 25 | from thrift.transport import TTransport |
| 26 | |
| 27 | |
| 28 | def testNaked(type, data): |
| 29 | buf = TTransport.TMemoryBuffer() |
| 30 | transport = TTransport.TBufferedTransportFactory().getTransport(buf) |
| 31 | protocol = TBinaryProtocol(transport) |
| 32 | if type.capitalize() == 'Byte': |
| 33 | protocol.writeByte(data) |
| 34 | |
| 35 | if type.capitalize() == 'I16': |
| 36 | protocol.writeI16(data) |
| 37 | |
| 38 | if type.capitalize() == 'I32': |
| 39 | protocol.writeI32(data) |
| 40 | |
| 41 | if type.capitalize() == 'I64': |
| 42 | protocol.writeI64(data) |
| 43 | |
| 44 | if type.capitalize() == 'String': |
| 45 | protocol.writeString(data) |
| 46 | |
| 47 | if type.capitalize() == 'Double': |
| 48 | protocol.writeDouble(data) |
| 49 | |
| 50 | if type.capitalize() == 'Binary': |
| 51 | protocol.writeBinary(data) |
| 52 | |
| 53 | if type.capitalize() == 'Bool': |
| 54 | protocol.writeBool(data) |
| 55 | |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 56 | if type.capitalize() == 'Uuid': |
| 57 | protocol.writeUuid(data) |
| 58 | |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 59 | transport.flush() |
| 60 | data_r = buf.getvalue() |
| 61 | buf = TTransport.TMemoryBuffer(data_r) |
| 62 | transport = TTransport.TBufferedTransportFactory().getTransport(buf) |
| 63 | protocol = TBinaryProtocol(transport) |
| 64 | if type.capitalize() == 'Byte': |
| 65 | return protocol.readByte() |
| 66 | |
| 67 | if type.capitalize() == 'I16': |
| 68 | return protocol.readI16() |
| 69 | |
| 70 | if type.capitalize() == 'I32': |
| 71 | return protocol.readI32() |
| 72 | |
| 73 | if type.capitalize() == 'I64': |
| 74 | return protocol.readI64() |
| 75 | |
| 76 | if type.capitalize() == 'String': |
| 77 | return protocol.readString() |
| 78 | |
| 79 | if type.capitalize() == 'Double': |
| 80 | return protocol.readDouble() |
| 81 | |
| 82 | if type.capitalize() == 'Binary': |
| 83 | return protocol.readBinary() |
| 84 | |
| 85 | if type.capitalize() == 'Bool': |
| 86 | return protocol.readBool() |
| 87 | |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 88 | if type.capitalize() == 'Uuid': |
| 89 | return protocol.readUuid() |
| 90 | |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 91 | |
| 92 | def testField(type, data): |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 93 | TType = {"Bool": 2, "Byte": 3, "Binary": 5, "I16": 6, "I32": 8, "I64": 10, "Double": 11, "String": 12, "Uuid": 13} |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 94 | buf = TTransport.TMemoryBuffer() |
| 95 | transport = TTransport.TBufferedTransportFactory().getTransport(buf) |
| 96 | protocol = TBinaryProtocol(transport) |
| 97 | protocol.writeStructBegin('struct') |
| 98 | protocol.writeFieldBegin("field", TType[type.capitalize()], 10) |
| 99 | if type.capitalize() == 'Byte': |
| 100 | protocol.writeByte(data) |
| 101 | |
| 102 | if type.capitalize() == 'I16': |
| 103 | protocol.writeI16(data) |
| 104 | |
| 105 | if type.capitalize() == 'I32': |
| 106 | protocol.writeI32(data) |
| 107 | |
| 108 | if type.capitalize() == 'I64': |
| 109 | protocol.writeI64(data) |
| 110 | |
| 111 | if type.capitalize() == 'String': |
| 112 | protocol.writeString(data) |
| 113 | |
| 114 | if type.capitalize() == 'Double': |
| 115 | protocol.writeDouble(data) |
| 116 | |
| 117 | if type.capitalize() == 'Binary': |
| 118 | protocol.writeBinary(data) |
| 119 | |
| 120 | if type.capitalize() == 'Bool': |
| 121 | protocol.writeBool(data) |
| 122 | |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 123 | if type.capitalize() == 'Uuid': |
| 124 | protocol.writeUuid(data) |
| 125 | |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 126 | protocol.writeFieldEnd() |
| 127 | protocol.writeStructEnd() |
| 128 | |
| 129 | transport.flush() |
| 130 | data_r = buf.getvalue() |
| 131 | |
| 132 | buf = TTransport.TMemoryBuffer(data_r) |
| 133 | transport = TTransport.TBufferedTransportFactory().getTransport(buf) |
| 134 | protocol = TBinaryProtocol(transport) |
| 135 | protocol.readStructBegin() |
| 136 | protocol.readFieldBegin() |
| 137 | if type.capitalize() == 'Byte': |
| 138 | return protocol.readByte() |
| 139 | |
| 140 | if type.capitalize() == 'I16': |
| 141 | return protocol.readI16() |
| 142 | |
| 143 | if type.capitalize() == 'I32': |
| 144 | return protocol.readI32() |
| 145 | |
| 146 | if type.capitalize() == 'I64': |
| 147 | return protocol.readI64() |
| 148 | |
| 149 | if type.capitalize() == 'String': |
| 150 | return protocol.readString() |
| 151 | |
| 152 | if type.capitalize() == 'Double': |
| 153 | return protocol.readDouble() |
| 154 | |
| 155 | if type.capitalize() == 'Binary': |
| 156 | return protocol.readBinary() |
| 157 | |
| 158 | if type.capitalize() == 'Bool': |
| 159 | return protocol.readBool() |
| 160 | |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 161 | if type.capitalize() == 'Uuid': |
| 162 | return protocol.readUuid() |
| 163 | |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 164 | protocol.readFieldEnd() |
| 165 | protocol.readStructEnd() |
| 166 | |
| 167 | |
| bwangelme | 58000cc | 2023-11-06 12:21:38 +0800 | [diff] [blame] | 168 | def testMessage(data, strict=True): |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 169 | message = {} |
| 170 | message['name'] = data[0] |
| 171 | message['type'] = data[1] |
| 172 | message['seqid'] = data[2] |
| 173 | |
| bwangelme | 58000cc | 2023-11-06 12:21:38 +0800 | [diff] [blame] | 174 | strictRead, strictWrite = True, True |
| 175 | if not strict: |
| 176 | strictRead, strictWrite = False, False |
| 177 | |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 178 | buf = TTransport.TMemoryBuffer() |
| 179 | transport = TTransport.TBufferedTransportFactory().getTransport(buf) |
| bwangelme | 58000cc | 2023-11-06 12:21:38 +0800 | [diff] [blame] | 180 | protocol = TBinaryProtocol(transport, strictRead=strictRead, strictWrite=strictWrite) |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 181 | protocol.writeMessageBegin(message['name'], message['type'], message['seqid']) |
| 182 | protocol.writeMessageEnd() |
| 183 | |
| 184 | transport.flush() |
| 185 | data_r = buf.getvalue() |
| 186 | |
| 187 | buf = TTransport.TMemoryBuffer(data_r) |
| 188 | transport = TTransport.TBufferedTransportFactory().getTransport(buf) |
| bwangelme | 58000cc | 2023-11-06 12:21:38 +0800 | [diff] [blame] | 189 | protocol = TBinaryProtocol(transport, strictRead=strictRead, strictWrite=strictWrite) |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 190 | result = protocol.readMessageBegin() |
| 191 | protocol.readMessageEnd() |
| 192 | return result |
| 193 | |
| 194 | |
| 195 | class TestTBinaryProtocol(unittest.TestCase): |
| 196 | |
| 197 | def test_TBinaryProtocol_write_read(self): |
| 198 | try: |
| 199 | testNaked('Byte', 123) |
| 200 | for i in range(0, 128): |
| 201 | self.assertEqual(i, testField('Byte', i)) |
| 202 | self.assertEqual(-i, testField('Byte', -i)) |
| 203 | |
| 204 | self.assertEqual(0, testNaked("I16", 0)) |
| 205 | self.assertEqual(1, testNaked("I16", 1)) |
| 206 | self.assertEqual(15000, testNaked("I16", 15000)) |
| 207 | self.assertEqual(0x7fff, testNaked("I16", 0x7fff)) |
| 208 | self.assertEqual(-1, testNaked("I16", -1)) |
| 209 | self.assertEqual(-15000, testNaked("I16", -15000)) |
| 210 | self.assertEqual(-0x7fff, testNaked("I16", -0x7fff)) |
| 211 | self.assertEqual(32767, testNaked("I16", 32767)) |
| 212 | self.assertEqual(-32768, testNaked("I16", -32768)) |
| 213 | |
| 214 | self.assertEqual(0, testField("I16", 0)) |
| 215 | self.assertEqual(1, testField("I16", 1)) |
| 216 | self.assertEqual(7, testField("I16", 7)) |
| 217 | self.assertEqual(150, testField("I16", 150)) |
| 218 | self.assertEqual(15000, testField("I16", 15000)) |
| 219 | self.assertEqual(0x7fff, testField("I16", 0x7fff)) |
| 220 | self.assertEqual(-1, testField("I16", -1)) |
| 221 | self.assertEqual(-7, testField("I16", -7)) |
| 222 | self.assertEqual(-150, testField("I16", -150)) |
| 223 | self.assertEqual(-15000, testField("I16", -15000)) |
| 224 | self.assertEqual(-0xfff, testField("I16", -0xfff)) |
| 225 | |
| 226 | self.assertEqual(0, testNaked("I32", 0)) |
| 227 | self.assertEqual(1, testNaked("I32", 1)) |
| 228 | self.assertEqual(15000, testNaked("I32", 15000)) |
| 229 | self.assertEqual(0xffff, testNaked("I32", 0xffff)) |
| 230 | self.assertEqual(-1, testNaked("I32", -1)) |
| 231 | self.assertEqual(-15000, testNaked("I32", -15000)) |
| 232 | self.assertEqual(-0xffff, testNaked("I32", -0xffff)) |
| 233 | self.assertEqual(2147483647, testNaked("I32", 2147483647)) |
| 234 | self.assertEqual(-2147483647, testNaked("I32", -2147483647)) |
| 235 | |
| 236 | self.assertEqual(0, testField("I32", 0)) |
| 237 | self.assertEqual(1, testField("I32", 1)) |
| 238 | self.assertEqual(7, testField("I32", 7)) |
| 239 | self.assertEqual(150, testField("I32", 150)) |
| 240 | self.assertEqual(15000, testField("I32", 15000)) |
| 241 | self.assertEqual(31337, testField("I32", 31337)) |
| 242 | self.assertEqual(0xffff, testField("I32", 0xffff)) |
| 243 | self.assertEqual(0xffffff, testField("I32", 0xffffff)) |
| 244 | self.assertEqual(-1, testField("I32", -1)) |
| 245 | self.assertEqual(-7, testField("I32", -7)) |
| 246 | self.assertEqual(-150, testField("I32", -150)) |
| 247 | self.assertEqual(-15000, testField("I32", -15000)) |
| 248 | self.assertEqual(-0xffff, testField("I32", -0xffff)) |
| 249 | self.assertEqual(-0xffffff, testField("I32", -0xffffff)) |
| 250 | |
| 251 | self.assertEqual(9223372036854775807, testNaked("I64", 9223372036854775807)) |
| 252 | self.assertEqual(-9223372036854775807, testNaked("I64", -9223372036854775807)) |
| 253 | self.assertEqual(-0, testNaked("I64", 0)) |
| 254 | |
| 255 | self.assertEqual(True, testNaked("Bool", True)) |
| 256 | self.assertEqual(3.14159261, testNaked("Double", 3.14159261)) |
| 257 | self.assertEqual("hello thrift", testNaked("String", "hello thrift")) |
| 258 | self.assertEqual(True, testField('Bool', True)) |
| 259 | self.assertEqual(3.1415926, testNaked("Double", 3.1415926)) |
| 260 | self.assertEqual("hello thrift", testNaked("String", "hello thrift")) |
| Carel Combrink | a715bdf | 2025-10-30 07:44:21 +0100 | [diff] [blame^] | 261 | self.assertEqual(uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}'), testNaked("Uuid", uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}'))) |
| 262 | self.assertEqual(uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}'), testField("Uuid", uuid.UUID('{00010203-0405-0607-0809-0a0b0c0d0e0f}'))) |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 263 | |
| 264 | TMessageType = {"T_CALL": 1, "T_REPLY": 2, "T_EXCEPTION": 3, "T_ONEWAY": 4} |
| 265 | test_data = [("short message name", TMessageType['T_CALL'], 0), |
| 266 | ("1", TMessageType['T_REPLY'], 12345), |
| 267 | ("loooooooooooooooooooooooooooooooooong", TMessageType['T_EXCEPTION'], 1 << 16), |
| 268 | ("one way push", TMessageType['T_ONEWAY'], 12), |
| 269 | ("Janky", TMessageType['T_CALL'], 0)] |
| 270 | |
| 271 | for dt in test_data: |
| 272 | result = testMessage(dt) |
| 273 | self.assertEqual(result[0], dt[0]) |
| 274 | self.assertEqual(result[1], dt[1]) |
| 275 | self.assertEqual(result[2], dt[2]) |
| 276 | |
| 277 | except Exception as e: |
| 278 | print("Assertion fail") |
| 279 | raise e |
| 280 | |
| bwangelme | 58000cc | 2023-11-06 12:21:38 +0800 | [diff] [blame] | 281 | def test_TBinaryProtocol_no_strict_write_read(self): |
| 282 | TMessageType = {"T_CALL": 1, "T_REPLY": 2, "T_EXCEPTION": 3, "T_ONEWAY": 4} |
| 283 | test_data = [("short message name", TMessageType['T_CALL'], 0), |
| Dmytro Shteflyuk | acbcf10 | 2026-02-13 18:25:55 -0500 | [diff] [blame] | 284 | ("1", TMessageType['T_REPLY'], 12345), |
| 285 | ("loooooooooooooooooooooooooooooooooong", TMessageType['T_EXCEPTION'], 1 << 16), |
| 286 | ("one way push", TMessageType['T_ONEWAY'], 12), |
| 287 | ("Janky", TMessageType['T_CALL'], 0)] |
| bwangelme | 58000cc | 2023-11-06 12:21:38 +0800 | [diff] [blame] | 288 | |
| 289 | try: |
| 290 | for dt in test_data: |
| 291 | result = testMessage(dt, strict=False) |
| 292 | self.assertEqual(result[0], dt[0]) |
| 293 | self.assertEqual(result[1], dt[1]) |
| 294 | self.assertEqual(result[2], dt[2]) |
| 295 | except Exception as e: |
| 296 | print("Assertion fail") |
| 297 | raise e |
| 298 | |
| Zezeng Wang | c372812 | 2020-04-27 15:48:19 +0800 | [diff] [blame] | 299 | |
| 300 | if __name__ == '__main__': |
| 301 | unittest.main() |