| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [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 | package thrift |
| 21 | |
| 22 | import ( |
| 23 | "bytes" |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 24 | "context" |
| Yuxuan 'fishy' Wang | bdfde85 | 2022-08-08 22:12:40 -0700 | [diff] [blame] | 25 | "io" |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 26 | "math" |
| 27 | "net" |
| 28 | "net/http" |
| 29 | "testing" |
| 30 | ) |
| 31 | |
| 32 | const PROTOCOL_BINARY_DATA_SIZE = 155 |
| 33 | |
| 34 | var ( |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 35 | protocol_bdata []byte // test data for writing; same as data |
| 36 | BOOL_VALUES []bool |
| Jens Geyer | 5bc8b5a | 2015-09-05 12:50:24 +0200 | [diff] [blame] | 37 | BYTE_VALUES []int8 |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 38 | INT16_VALUES []int16 |
| 39 | INT32_VALUES []int32 |
| 40 | INT64_VALUES []int64 |
| 41 | DOUBLE_VALUES []float64 |
| 42 | STRING_VALUES []string |
| Yuxuan 'fishy' Wang | 19c13b4 | 2022-10-12 14:13:15 -0700 | [diff] [blame] | 43 | UUID_VALUES []Tuuid |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 44 | ) |
| 45 | |
| 46 | func init() { |
| 47 | protocol_bdata = make([]byte, PROTOCOL_BINARY_DATA_SIZE) |
| Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 48 | for i := range PROTOCOL_BINARY_DATA_SIZE { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 49 | protocol_bdata[i] = byte((i + 'a') % 255) |
| 50 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 51 | BOOL_VALUES = []bool{false, true, false, false, true} |
| Jens Geyer | 5bc8b5a | 2015-09-05 12:50:24 +0200 | [diff] [blame] | 52 | BYTE_VALUES = []int8{117, 0, 1, 32, 127, -128, -1} |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 53 | INT16_VALUES = []int16{459, 0, 1, -1, -128, 127, 32767, -32768} |
| 54 | INT32_VALUES = []int32{459, 0, 1, -1, -128, 127, 32767, 2147483647, -2147483535} |
| 55 | INT64_VALUES = []int64{459, 0, 1, -1, -128, 127, 32767, 2147483647, -2147483535, 34359738481, -35184372088719, -9223372036854775808, 9223372036854775807} |
| 56 | DOUBLE_VALUES = []float64{459.3, 0.0, -1.0, 1.0, 0.5, 0.3333, 3.14159, 1.537e-38, 1.673e25, 6.02214179e23, -6.02214179e23, INFINITY.Float64(), NEGATIVE_INFINITY.Float64(), NAN.Float64()} |
| 57 | STRING_VALUES = []string{"", "a", "st[uf]f", "st,u:ff with spaces", "stuff\twith\nescape\\characters'...\"lots{of}fun</xml>"} |
| Yuxuan 'fishy' Wang | 19c13b4 | 2022-10-12 14:13:15 -0700 | [diff] [blame] | 58 | UUID_VALUES = []Tuuid{ |
| 59 | {}, |
| 60 | Must(ParseTuuid("6ba7b810-9dad-11d1-80b4-00c04fd430c8")), |
| 61 | Must(ParseTuuid("6ba7b811-9dad-11d1-80b4-00c04fd430c8")), |
| 62 | Must(ParseTuuid("6ba7b812-9dad-11d1-80b4-00c04fd430c8")), |
| 63 | Must(ParseTuuid("6ba7b814-9dad-11d1-80b4-00c04fd430c8")), |
| 64 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | type HTTPEchoServer struct{} |
| Jens Geyer | 706cb4e | 2014-03-19 00:37:10 +0200 | [diff] [blame] | 68 | type HTTPHeaderEchoServer struct{} |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 69 | |
| 70 | func (p *HTTPEchoServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| Yuxuan 'fishy' Wang | bdfde85 | 2022-08-08 22:12:40 -0700 | [diff] [blame] | 71 | buf, err := io.ReadAll(req.Body) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 72 | if err != nil { |
| 73 | w.WriteHeader(http.StatusBadRequest) |
| 74 | w.Write(buf) |
| 75 | } else { |
| 76 | w.WriteHeader(http.StatusOK) |
| 77 | w.Write(buf) |
| 78 | } |
| 79 | } |
| 80 | |
| Jens Geyer | 706cb4e | 2014-03-19 00:37:10 +0200 | [diff] [blame] | 81 | func (p *HTTPHeaderEchoServer) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| Yuxuan 'fishy' Wang | bdfde85 | 2022-08-08 22:12:40 -0700 | [diff] [blame] | 82 | buf, err := io.ReadAll(req.Body) |
| Jens Geyer | 706cb4e | 2014-03-19 00:37:10 +0200 | [diff] [blame] | 83 | if err != nil { |
| 84 | w.WriteHeader(http.StatusBadRequest) |
| 85 | w.Write(buf) |
| 86 | } else { |
| 87 | w.WriteHeader(http.StatusOK) |
| 88 | w.Write(buf) |
| 89 | } |
| 90 | } |
| 91 | |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 92 | func HttpClientSetupForTest(t *testing.T) (net.Listener, net.Addr) { |
| 93 | addr, err := FindAvailableTCPServerPort(40000) |
| 94 | if err != nil { |
| 95 | t.Fatalf("Unable to find available tcp port addr: %s", err) |
| 96 | return nil, addr |
| 97 | } |
| 98 | l, err := net.Listen(addr.Network(), addr.String()) |
| 99 | if err != nil { |
| 100 | t.Fatalf("Unable to setup tcp listener on %s: %s", addr.String(), err) |
| 101 | return l, addr |
| 102 | } |
| 103 | go http.Serve(l, &HTTPEchoServer{}) |
| 104 | return l, addr |
| 105 | } |
| 106 | |
| Jens Geyer | 706cb4e | 2014-03-19 00:37:10 +0200 | [diff] [blame] | 107 | func HttpClientSetupForHeaderTest(t *testing.T) (net.Listener, net.Addr) { |
| 108 | addr, err := FindAvailableTCPServerPort(40000) |
| 109 | if err != nil { |
| 110 | t.Fatalf("Unable to find available tcp port addr: %s", err) |
| 111 | return nil, addr |
| 112 | } |
| 113 | l, err := net.Listen(addr.Network(), addr.String()) |
| 114 | if err != nil { |
| 115 | t.Fatalf("Unable to setup tcp listener on %s: %s", addr.String(), err) |
| 116 | return l, addr |
| 117 | } |
| 118 | go http.Serve(l, &HTTPHeaderEchoServer{}) |
| 119 | return l, addr |
| 120 | } |
| 121 | |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 122 | func ReadWriteProtocolTest(t *testing.T, protocolFactory TProtocolFactory) { |
| 123 | buf := bytes.NewBuffer(make([]byte, 0, 1024)) |
| 124 | l, addr := HttpClientSetupForTest(t) |
| 125 | defer l.Close() |
| 126 | transports := []TTransportFactory{ |
| 127 | NewTMemoryBufferTransportFactory(1024), |
| 128 | NewStreamTransportFactory(buf, buf, true), |
| 129 | NewTFramedTransportFactory(NewTMemoryBufferTransportFactory(1024)), |
| Vyacheslav Kulakov | 6e29b19 | 2018-08-31 13:42:50 +0300 | [diff] [blame] | 130 | NewTZlibTransportFactoryWithFactory(0, NewTMemoryBufferTransportFactory(1024)), |
| 131 | NewTZlibTransportFactoryWithFactory(6, NewTMemoryBufferTransportFactory(1024)), |
| 132 | NewTZlibTransportFactoryWithFactory(9, NewTFramedTransportFactory(NewTMemoryBufferTransportFactory(1024))), |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 133 | NewTHttpPostClientTransportFactory("http://" + addr.String()), |
| 134 | } |
| 135 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 136 | trans, err := tf.GetTransport(nil) |
| 137 | if err != nil { |
| 138 | t.Error(err) |
| 139 | continue |
| 140 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 141 | p := protocolFactory.GetProtocol(trans) |
| 142 | ReadWriteBool(t, p, trans) |
| 143 | trans.Close() |
| 144 | } |
| 145 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 146 | trans, err := tf.GetTransport(nil) |
| 147 | if err != nil { |
| 148 | t.Error(err) |
| 149 | continue |
| 150 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 151 | p := protocolFactory.GetProtocol(trans) |
| 152 | ReadWriteByte(t, p, trans) |
| 153 | trans.Close() |
| 154 | } |
| 155 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 156 | trans, err := tf.GetTransport(nil) |
| 157 | if err != nil { |
| 158 | t.Error(err) |
| 159 | continue |
| 160 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 161 | p := protocolFactory.GetProtocol(trans) |
| 162 | ReadWriteI16(t, p, trans) |
| 163 | trans.Close() |
| 164 | } |
| 165 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 166 | trans, err := tf.GetTransport(nil) |
| 167 | if err != nil { |
| 168 | t.Error(err) |
| 169 | continue |
| 170 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 171 | p := protocolFactory.GetProtocol(trans) |
| 172 | ReadWriteI32(t, p, trans) |
| 173 | trans.Close() |
| 174 | } |
| 175 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 176 | trans, err := tf.GetTransport(nil) |
| 177 | if err != nil { |
| 178 | t.Error(err) |
| 179 | continue |
| 180 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 181 | p := protocolFactory.GetProtocol(trans) |
| 182 | ReadWriteI64(t, p, trans) |
| 183 | trans.Close() |
| 184 | } |
| 185 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 186 | trans, err := tf.GetTransport(nil) |
| 187 | if err != nil { |
| 188 | t.Error(err) |
| 189 | continue |
| 190 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 191 | p := protocolFactory.GetProtocol(trans) |
| 192 | ReadWriteDouble(t, p, trans) |
| 193 | trans.Close() |
| 194 | } |
| 195 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 196 | trans, err := tf.GetTransport(nil) |
| 197 | if err != nil { |
| 198 | t.Error(err) |
| 199 | continue |
| 200 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 201 | p := protocolFactory.GetProtocol(trans) |
| 202 | ReadWriteString(t, p, trans) |
| 203 | trans.Close() |
| 204 | } |
| 205 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 206 | trans, err := tf.GetTransport(nil) |
| 207 | if err != nil { |
| 208 | t.Error(err) |
| 209 | continue |
| 210 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 211 | p := protocolFactory.GetProtocol(trans) |
| 212 | ReadWriteBinary(t, p, trans) |
| 213 | trans.Close() |
| 214 | } |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 215 | for _, tf := range transports { |
| D. Can Celasun | 8da0e72 | 2017-06-02 14:33:32 +0200 | [diff] [blame] | 216 | trans, err := tf.GetTransport(nil) |
| 217 | if err != nil { |
| 218 | t.Error(err) |
| 219 | continue |
| 220 | } |
| Jens Geyer | 706cb4e | 2014-03-19 00:37:10 +0200 | [diff] [blame] | 221 | p := protocolFactory.GetProtocol(trans) |
| Yuxuan 'fishy' Wang | 19c13b4 | 2022-10-12 14:13:15 -0700 | [diff] [blame] | 222 | ReadWriteUUID(t, p, trans) |
| 223 | trans.Close() |
| 224 | } |
| 225 | for _, tf := range transports { |
| 226 | trans, err := tf.GetTransport(nil) |
| 227 | if err != nil { |
| 228 | t.Error(err) |
| 229 | continue |
| 230 | } |
| 231 | p := protocolFactory.GetProtocol(trans) |
| Jens Geyer | 706cb4e | 2014-03-19 00:37:10 +0200 | [diff] [blame] | 232 | ReadWriteI64(t, p, trans) |
| 233 | ReadWriteDouble(t, p, trans) |
| 234 | ReadWriteBinary(t, p, trans) |
| 235 | ReadWriteByte(t, p, trans) |
| Yuxuan 'fishy' Wang | 19c13b4 | 2022-10-12 14:13:15 -0700 | [diff] [blame] | 236 | ReadWriteUUID(t, p, trans) |
| Jens Geyer | 706cb4e | 2014-03-19 00:37:10 +0200 | [diff] [blame] | 237 | trans.Close() |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 238 | } |
| Yuxuan 'fishy' Wang | 64c2a4b | 2020-10-10 18:39:32 -0700 | [diff] [blame] | 239 | |
| 240 | t.Run("UnmatchedBeginEnd", func(t *testing.T) { |
| 241 | UnmatchedBeginEndProtocolTest(t, protocolFactory) |
| 242 | }) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 243 | } |
| 244 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 245 | func ReadWriteBool(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 246 | thetype := TType(BOOL) |
| 247 | thelen := len(BOOL_VALUES) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 248 | err := p.WriteListBegin(context.Background(), thetype, thelen) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 249 | if err != nil { |
| 250 | t.Errorf("%s: %T %T %q Error writing list begin: %q", "ReadWriteBool", p, trans, err, thetype) |
| 251 | } |
| 252 | for k, v := range BOOL_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 253 | err = p.WriteBool(context.Background(), v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 254 | if err != nil { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 255 | t.Errorf("%s: %T %T %v Error writing bool in list at index %v: %v", "ReadWriteBool", p, trans, err, k, v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 256 | } |
| 257 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 258 | p.WriteListEnd(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 259 | if err != nil { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 260 | t.Errorf("%s: %T %T %v Error writing list end: %v", "ReadWriteBool", p, trans, err, BOOL_VALUES) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 261 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 262 | p.Flush(context.Background()) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 263 | thetype2, thelen2, err := p.ReadListBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 264 | if err != nil { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 265 | t.Errorf("%s: %T %T %v Error reading list: %v", "ReadWriteBool", p, trans, err, BOOL_VALUES) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 266 | } |
| 267 | _, ok := p.(*TSimpleJSONProtocol) |
| 268 | if !ok { |
| 269 | if thetype != thetype2 { |
| 270 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteBool", p, trans, thetype, thetype2) |
| 271 | } |
| 272 | if thelen != thelen2 { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 273 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteBool", p, trans, thelen, thelen2) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | for k, v := range BOOL_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 277 | value, err := p.ReadBool(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 278 | if err != nil { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 279 | t.Errorf("%s: %T %T %v Error reading bool at index %v: %v", "ReadWriteBool", p, trans, err, k, v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 280 | } |
| 281 | if v != value { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 282 | t.Errorf("%s: index %v %v %v %v != %v", "ReadWriteBool", k, p, trans, v, value) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 283 | } |
| 284 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 285 | err = p.ReadListEnd(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 286 | if err != nil { |
| 287 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteBool", p, trans, err) |
| 288 | } |
| 289 | } |
| 290 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 291 | func ReadWriteByte(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 292 | thetype := TType(BYTE) |
| 293 | thelen := len(BYTE_VALUES) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 294 | err := p.WriteListBegin(context.Background(), thetype, thelen) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 295 | if err != nil { |
| 296 | t.Errorf("%s: %T %T %q Error writing list begin: %q", "ReadWriteByte", p, trans, err, thetype) |
| 297 | } |
| 298 | for k, v := range BYTE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 299 | err = p.WriteByte(context.Background(), v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 300 | if err != nil { |
| 301 | t.Errorf("%s: %T %T %q Error writing byte in list at index %d: %q", "ReadWriteByte", p, trans, err, k, v) |
| 302 | } |
| 303 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 304 | err = p.WriteListEnd(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 305 | if err != nil { |
| 306 | t.Errorf("%s: %T %T %q Error writing list end: %q", "ReadWriteByte", p, trans, err, BYTE_VALUES) |
| 307 | } |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 308 | err = p.Flush(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 309 | if err != nil { |
| 310 | t.Errorf("%s: %T %T %q Error flushing list of bytes: %q", "ReadWriteByte", p, trans, err, BYTE_VALUES) |
| 311 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 312 | thetype2, thelen2, err := p.ReadListBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 313 | if err != nil { |
| 314 | t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteByte", p, trans, err, BYTE_VALUES) |
| 315 | } |
| 316 | _, ok := p.(*TSimpleJSONProtocol) |
| 317 | if !ok { |
| 318 | if thetype != thetype2 { |
| 319 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteByte", p, trans, thetype, thetype2) |
| 320 | } |
| 321 | if thelen != thelen2 { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 322 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteByte", p, trans, thelen, thelen2) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | for k, v := range BYTE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 326 | value, err := p.ReadByte(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 327 | if err != nil { |
| 328 | t.Errorf("%s: %T %T %q Error reading byte at index %d: %q", "ReadWriteByte", p, trans, err, k, v) |
| 329 | } |
| 330 | if v != value { |
| 331 | t.Errorf("%s: %T %T %d != %d", "ReadWriteByte", p, trans, v, value) |
| 332 | } |
| 333 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 334 | err = p.ReadListEnd(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 335 | if err != nil { |
| 336 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteByte", p, trans, err) |
| 337 | } |
| 338 | } |
| 339 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 340 | func ReadWriteI16(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 341 | thetype := TType(I16) |
| 342 | thelen := len(INT16_VALUES) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 343 | p.WriteListBegin(context.Background(), thetype, thelen) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 344 | for _, v := range INT16_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 345 | p.WriteI16(context.Background(), v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 346 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 347 | p.WriteListEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 348 | p.Flush(context.Background()) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 349 | thetype2, thelen2, err := p.ReadListBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 350 | if err != nil { |
| 351 | t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteI16", p, trans, err, INT16_VALUES) |
| 352 | } |
| 353 | _, ok := p.(*TSimpleJSONProtocol) |
| 354 | if !ok { |
| 355 | if thetype != thetype2 { |
| 356 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteI16", p, trans, thetype, thetype2) |
| 357 | } |
| 358 | if thelen != thelen2 { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 359 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI16", p, trans, thelen, thelen2) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | for k, v := range INT16_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 363 | value, err := p.ReadI16(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 364 | if err != nil { |
| 365 | t.Errorf("%s: %T %T %q Error reading int16 at index %d: %q", "ReadWriteI16", p, trans, err, k, v) |
| 366 | } |
| 367 | if v != value { |
| 368 | t.Errorf("%s: %T %T %d != %d", "ReadWriteI16", p, trans, v, value) |
| 369 | } |
| 370 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 371 | err = p.ReadListEnd(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 372 | if err != nil { |
| 373 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteI16", p, trans, err) |
| 374 | } |
| 375 | } |
| 376 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 377 | func ReadWriteI32(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 378 | thetype := TType(I32) |
| 379 | thelen := len(INT32_VALUES) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 380 | p.WriteListBegin(context.Background(), thetype, thelen) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 381 | for _, v := range INT32_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 382 | p.WriteI32(context.Background(), v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 383 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 384 | p.WriteListEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 385 | p.Flush(context.Background()) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 386 | thetype2, thelen2, err := p.ReadListBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 387 | if err != nil { |
| 388 | t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteI32", p, trans, err, INT32_VALUES) |
| 389 | } |
| 390 | _, ok := p.(*TSimpleJSONProtocol) |
| 391 | if !ok { |
| 392 | if thetype != thetype2 { |
| 393 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteI32", p, trans, thetype, thetype2) |
| 394 | } |
| 395 | if thelen != thelen2 { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 396 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI32", p, trans, thelen, thelen2) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | for k, v := range INT32_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 400 | value, err := p.ReadI32(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 401 | if err != nil { |
| 402 | t.Errorf("%s: %T %T %q Error reading int32 at index %d: %q", "ReadWriteI32", p, trans, err, k, v) |
| 403 | } |
| 404 | if v != value { |
| 405 | t.Errorf("%s: %T %T %d != %d", "ReadWriteI32", p, trans, v, value) |
| 406 | } |
| 407 | } |
| 408 | if err != nil { |
| 409 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteI32", p, trans, err) |
| 410 | } |
| 411 | } |
| 412 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 413 | func ReadWriteI64(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 414 | thetype := TType(I64) |
| 415 | thelen := len(INT64_VALUES) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 416 | p.WriteListBegin(context.Background(), thetype, thelen) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 417 | for _, v := range INT64_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 418 | p.WriteI64(context.Background(), v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 419 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 420 | p.WriteListEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 421 | p.Flush(context.Background()) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 422 | thetype2, thelen2, err := p.ReadListBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 423 | if err != nil { |
| 424 | t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteI64", p, trans, err, INT64_VALUES) |
| 425 | } |
| 426 | _, ok := p.(*TSimpleJSONProtocol) |
| 427 | if !ok { |
| 428 | if thetype != thetype2 { |
| 429 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteI64", p, trans, thetype, thetype2) |
| 430 | } |
| 431 | if thelen != thelen2 { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 432 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI64", p, trans, thelen, thelen2) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | for k, v := range INT64_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 436 | value, err := p.ReadI64(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 437 | if err != nil { |
| 438 | t.Errorf("%s: %T %T %q Error reading int64 at index %d: %q", "ReadWriteI64", p, trans, err, k, v) |
| 439 | } |
| 440 | if v != value { |
| 441 | t.Errorf("%s: %T %T %q != %q", "ReadWriteI64", p, trans, v, value) |
| 442 | } |
| 443 | } |
| 444 | if err != nil { |
| 445 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteI64", p, trans, err) |
| 446 | } |
| 447 | } |
| 448 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 449 | func ReadWriteDouble(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 450 | thetype := TType(DOUBLE) |
| 451 | thelen := len(DOUBLE_VALUES) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 452 | p.WriteListBegin(context.Background(), thetype, thelen) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 453 | for _, v := range DOUBLE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 454 | p.WriteDouble(context.Background(), v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 455 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 456 | p.WriteListEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 457 | p.Flush(context.Background()) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 458 | thetype2, thelen2, err := p.ReadListBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 459 | if err != nil { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 460 | t.Errorf("%s: %T %T %v Error reading list: %v", "ReadWriteDouble", p, trans, err, DOUBLE_VALUES) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 461 | } |
| 462 | if thetype != thetype2 { |
| 463 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteDouble", p, trans, thetype, thetype2) |
| 464 | } |
| 465 | if thelen != thelen2 { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 466 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteDouble", p, trans, thelen, thelen2) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 467 | } |
| 468 | for k, v := range DOUBLE_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 469 | value, err := p.ReadDouble(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 470 | if err != nil { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 471 | t.Errorf("%s: %T %T %q Error reading double at index %d: %v", "ReadWriteDouble", p, trans, err, k, v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 472 | } |
| 473 | if math.IsNaN(v) { |
| 474 | if !math.IsNaN(value) { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 475 | t.Errorf("%s: %T %T math.IsNaN(%v) != math.IsNaN(%v)", "ReadWriteDouble", p, trans, v, value) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 476 | } |
| 477 | } else if v != value { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 478 | t.Errorf("%s: %T %T %v != %v", "ReadWriteDouble", p, trans, v, value) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 481 | err = p.ReadListEnd(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 482 | if err != nil { |
| 483 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteDouble", p, trans, err) |
| 484 | } |
| 485 | } |
| 486 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 487 | func ReadWriteString(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 488 | thetype := TType(STRING) |
| 489 | thelen := len(STRING_VALUES) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 490 | p.WriteListBegin(context.Background(), thetype, thelen) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 491 | for _, v := range STRING_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 492 | p.WriteString(context.Background(), v) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 493 | } |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 494 | p.WriteListEnd(context.Background()) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 495 | p.Flush(context.Background()) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 496 | thetype2, thelen2, err := p.ReadListBegin(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 497 | if err != nil { |
| 498 | t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteString", p, trans, err, STRING_VALUES) |
| 499 | } |
| 500 | _, ok := p.(*TSimpleJSONProtocol) |
| 501 | if !ok { |
| 502 | if thetype != thetype2 { |
| 503 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteString", p, trans, thetype, thetype2) |
| 504 | } |
| 505 | if thelen != thelen2 { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 506 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteString", p, trans, thelen, thelen2) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | for k, v := range STRING_VALUES { |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 510 | value, err := p.ReadString(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 511 | if err != nil { |
| 512 | t.Errorf("%s: %T %T %q Error reading string at index %d: %q", "ReadWriteString", p, trans, err, k, v) |
| 513 | } |
| 514 | if v != value { |
| D. Can Celasun | a9efd1a | 2018-03-15 12:52:37 +0100 | [diff] [blame] | 515 | t.Errorf("%s: %T %T %v != %v", "ReadWriteString", p, trans, v, value) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | if err != nil { |
| 519 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteString", p, trans, err) |
| 520 | } |
| 521 | } |
| 522 | |
| Jens Geyer | 0997250 | 2014-05-02 01:30:13 +0200 | [diff] [blame] | 523 | func ReadWriteBinary(t testing.TB, p TProtocol, trans TTransport) { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 524 | v := protocol_bdata |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 525 | p.WriteBinary(context.Background(), v) |
| John Boiles | 5785279 | 2018-01-05 14:37:05 -0800 | [diff] [blame] | 526 | p.Flush(context.Background()) |
| Yuxuan 'fishy' Wang | e79f764 | 2020-06-12 22:22:35 -0700 | [diff] [blame] | 527 | value, err := p.ReadBinary(context.Background()) |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 528 | if err != nil { |
| 529 | t.Errorf("%s: %T %T Unable to read binary: %s", "ReadWriteBinary", p, trans, err.Error()) |
| 530 | } |
| 531 | if len(v) != len(value) { |
| 532 | t.Errorf("%s: %T %T len(v) != len(value)... %d != %d", "ReadWriteBinary", p, trans, len(v), len(value)) |
| 533 | } else { |
| Yuxuan 'fishy' Wang | 91565d4 | 2024-08-14 09:01:15 -0700 | [diff] [blame] | 534 | for i := range v { |
| Jens Geyer | 0e87c46 | 2013-06-18 22:25:07 +0200 | [diff] [blame] | 535 | if v[i] != value[i] { |
| 536 | t.Errorf("%s: %T %T %s != %s", "ReadWriteBinary", p, trans, v, value) |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | } |
| Yuxuan 'fishy' Wang | 64c2a4b | 2020-10-10 18:39:32 -0700 | [diff] [blame] | 541 | |
| Yuxuan 'fishy' Wang | 19c13b4 | 2022-10-12 14:13:15 -0700 | [diff] [blame] | 542 | func ReadWriteUUID(t testing.TB, p TProtocol, trans TTransport) { |
| 543 | ctx := context.Background() |
| 544 | thetype := TType(UUID) |
| 545 | thelen := len(UUID_VALUES) |
| 546 | p.WriteListBegin(ctx, thetype, thelen) |
| 547 | for _, v := range UUID_VALUES { |
| 548 | p.WriteUUID(ctx, v) |
| 549 | } |
| 550 | p.WriteListEnd(ctx) |
| 551 | p.Flush(ctx) |
| 552 | thetype2, thelen2, err := p.ReadListBegin(ctx) |
| 553 | if err != nil { |
| 554 | t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteUUID", p, trans, err, STRING_VALUES) |
| 555 | } |
| 556 | _, ok := p.(*TSimpleJSONProtocol) |
| 557 | if !ok { |
| 558 | if thetype != thetype2 { |
| 559 | t.Errorf("%s: %T %T type %s != type %s", "ReadWriteUUID", p, trans, thetype, thetype2) |
| 560 | } |
| 561 | if thelen != thelen2 { |
| 562 | t.Errorf("%s: %T %T len %v != len %v", "ReadWriteUUID", p, trans, thelen, thelen2) |
| 563 | } |
| 564 | } |
| 565 | for k, v := range UUID_VALUES { |
| 566 | value, err := p.ReadUUID(ctx) |
| 567 | if err != nil { |
| 568 | t.Errorf("%s: %T %T %q Error reading UUID at index %d: %q", "ReadWriteUUID", p, trans, err, k, v) |
| 569 | } |
| 570 | if v != value { |
| 571 | t.Errorf("%s: %T %T %v != %v", "ReadWriteUUID", p, trans, v, value) |
| 572 | } |
| 573 | } |
| 574 | if err != nil { |
| 575 | t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteUUID", p, trans, err) |
| 576 | } |
| 577 | } |
| 578 | |
| Yuxuan 'fishy' Wang | 64c2a4b | 2020-10-10 18:39:32 -0700 | [diff] [blame] | 579 | func UnmatchedBeginEndProtocolTest(t *testing.T, protocolFactory TProtocolFactory) { |
| 580 | // NOTE: not all protocol implementations do strict state check to |
| 581 | // return an error on unmatched Begin/End calls. |
| 582 | // This test is only meant to make sure that those unmatched Begin/End |
| 583 | // calls won't cause panic. There's no real "test" here. |
| 584 | trans := NewTMemoryBuffer() |
| 585 | t.Run("Read", func(t *testing.T) { |
| 586 | t.Run("Message", func(t *testing.T) { |
| 587 | trans.Reset() |
| 588 | p := protocolFactory.GetProtocol(trans) |
| 589 | p.ReadMessageEnd(context.Background()) |
| 590 | p.ReadMessageEnd(context.Background()) |
| 591 | }) |
| 592 | t.Run("Struct", func(t *testing.T) { |
| 593 | trans.Reset() |
| 594 | p := protocolFactory.GetProtocol(trans) |
| 595 | p.ReadStructEnd(context.Background()) |
| 596 | p.ReadStructEnd(context.Background()) |
| 597 | }) |
| 598 | t.Run("Field", func(t *testing.T) { |
| 599 | trans.Reset() |
| 600 | p := protocolFactory.GetProtocol(trans) |
| 601 | p.ReadFieldEnd(context.Background()) |
| 602 | p.ReadFieldEnd(context.Background()) |
| 603 | }) |
| 604 | t.Run("Map", func(t *testing.T) { |
| 605 | trans.Reset() |
| 606 | p := protocolFactory.GetProtocol(trans) |
| 607 | p.ReadMapEnd(context.Background()) |
| 608 | p.ReadMapEnd(context.Background()) |
| 609 | }) |
| 610 | t.Run("List", func(t *testing.T) { |
| 611 | trans.Reset() |
| 612 | p := protocolFactory.GetProtocol(trans) |
| 613 | p.ReadListEnd(context.Background()) |
| 614 | p.ReadListEnd(context.Background()) |
| 615 | }) |
| 616 | t.Run("Set", func(t *testing.T) { |
| 617 | trans.Reset() |
| 618 | p := protocolFactory.GetProtocol(trans) |
| 619 | p.ReadSetEnd(context.Background()) |
| 620 | p.ReadSetEnd(context.Background()) |
| 621 | }) |
| 622 | }) |
| 623 | t.Run("Write", func(t *testing.T) { |
| 624 | t.Run("Message", func(t *testing.T) { |
| 625 | trans.Reset() |
| 626 | p := protocolFactory.GetProtocol(trans) |
| 627 | p.WriteMessageEnd(context.Background()) |
| 628 | p.WriteMessageEnd(context.Background()) |
| 629 | }) |
| 630 | t.Run("Struct", func(t *testing.T) { |
| 631 | trans.Reset() |
| 632 | p := protocolFactory.GetProtocol(trans) |
| 633 | p.WriteStructEnd(context.Background()) |
| 634 | p.WriteStructEnd(context.Background()) |
| 635 | }) |
| 636 | t.Run("Field", func(t *testing.T) { |
| 637 | trans.Reset() |
| 638 | p := protocolFactory.GetProtocol(trans) |
| 639 | p.WriteFieldEnd(context.Background()) |
| 640 | p.WriteFieldEnd(context.Background()) |
| 641 | }) |
| 642 | t.Run("Map", func(t *testing.T) { |
| 643 | trans.Reset() |
| 644 | p := protocolFactory.GetProtocol(trans) |
| 645 | p.WriteMapEnd(context.Background()) |
| 646 | p.WriteMapEnd(context.Background()) |
| 647 | }) |
| 648 | t.Run("List", func(t *testing.T) { |
| 649 | trans.Reset() |
| 650 | p := protocolFactory.GetProtocol(trans) |
| 651 | p.WriteListEnd(context.Background()) |
| 652 | p.WriteListEnd(context.Background()) |
| 653 | }) |
| 654 | t.Run("Set", func(t *testing.T) { |
| 655 | trans.Reset() |
| 656 | p := protocolFactory.GetProtocol(trans) |
| 657 | p.WriteSetEnd(context.Background()) |
| 658 | p.WriteSetEnd(context.Background()) |
| 659 | }) |
| 660 | }) |
| 661 | trans.Close() |
| 662 | } |