blob: 4fac8013502b04d80c6ff1181c8870f2d0887e1b [file] [log] [blame]
Jens Geyer0e87c462013-06-18 22:25:07 +02001/*
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
20package thrift
21
22import (
23 "bytes"
John Boiles57852792018-01-05 14:37:05 -080024 "context"
Yuxuan 'fishy' Wangbdfde852022-08-08 22:12:40 -070025 "io"
Jens Geyer0e87c462013-06-18 22:25:07 +020026 "math"
27 "net"
28 "net/http"
29 "testing"
30)
31
32const PROTOCOL_BINARY_DATA_SIZE = 155
33
34var (
Jens Geyer0e87c462013-06-18 22:25:07 +020035 protocol_bdata []byte // test data for writing; same as data
36 BOOL_VALUES []bool
Jens Geyer5bc8b5a2015-09-05 12:50:24 +020037 BYTE_VALUES []int8
Jens Geyer0e87c462013-06-18 22:25:07 +020038 INT16_VALUES []int16
39 INT32_VALUES []int32
40 INT64_VALUES []int64
41 DOUBLE_VALUES []float64
42 STRING_VALUES []string
Yuxuan 'fishy' Wang19c13b42022-10-12 14:13:15 -070043 UUID_VALUES []Tuuid
Jens Geyer0e87c462013-06-18 22:25:07 +020044)
45
46func init() {
47 protocol_bdata = make([]byte, PROTOCOL_BINARY_DATA_SIZE)
Yuxuan 'fishy' Wang91565d42024-08-14 09:01:15 -070048 for i := range PROTOCOL_BINARY_DATA_SIZE {
Jens Geyer0e87c462013-06-18 22:25:07 +020049 protocol_bdata[i] = byte((i + 'a') % 255)
50 }
Jens Geyer0e87c462013-06-18 22:25:07 +020051 BOOL_VALUES = []bool{false, true, false, false, true}
Jens Geyer5bc8b5a2015-09-05 12:50:24 +020052 BYTE_VALUES = []int8{117, 0, 1, 32, 127, -128, -1}
Jens Geyer0e87c462013-06-18 22:25:07 +020053 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' Wang19c13b42022-10-12 14:13:15 -070058 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 Geyer0e87c462013-06-18 22:25:07 +020065}
66
67type HTTPEchoServer struct{}
Jens Geyer706cb4e2014-03-19 00:37:10 +020068type HTTPHeaderEchoServer struct{}
Jens Geyer0e87c462013-06-18 22:25:07 +020069
70func (p *HTTPEchoServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
Yuxuan 'fishy' Wangbdfde852022-08-08 22:12:40 -070071 buf, err := io.ReadAll(req.Body)
Jens Geyer0e87c462013-06-18 22:25:07 +020072 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 Geyer706cb4e2014-03-19 00:37:10 +020081func (p *HTTPHeaderEchoServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
Yuxuan 'fishy' Wangbdfde852022-08-08 22:12:40 -070082 buf, err := io.ReadAll(req.Body)
Jens Geyer706cb4e2014-03-19 00:37:10 +020083 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 Geyer0e87c462013-06-18 22:25:07 +020092func 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 Geyer706cb4e2014-03-19 00:37:10 +0200107func 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 Geyer0e87c462013-06-18 22:25:07 +0200122func 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 Kulakov6e29b192018-08-31 13:42:50 +0300130 NewTZlibTransportFactoryWithFactory(0, NewTMemoryBufferTransportFactory(1024)),
131 NewTZlibTransportFactoryWithFactory(6, NewTMemoryBufferTransportFactory(1024)),
132 NewTZlibTransportFactoryWithFactory(9, NewTFramedTransportFactory(NewTMemoryBufferTransportFactory(1024))),
Jens Geyer0e87c462013-06-18 22:25:07 +0200133 NewTHttpPostClientTransportFactory("http://" + addr.String()),
134 }
135 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200136 trans, err := tf.GetTransport(nil)
137 if err != nil {
138 t.Error(err)
139 continue
140 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200141 p := protocolFactory.GetProtocol(trans)
142 ReadWriteBool(t, p, trans)
143 trans.Close()
144 }
145 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200146 trans, err := tf.GetTransport(nil)
147 if err != nil {
148 t.Error(err)
149 continue
150 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200151 p := protocolFactory.GetProtocol(trans)
152 ReadWriteByte(t, p, trans)
153 trans.Close()
154 }
155 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200156 trans, err := tf.GetTransport(nil)
157 if err != nil {
158 t.Error(err)
159 continue
160 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200161 p := protocolFactory.GetProtocol(trans)
162 ReadWriteI16(t, p, trans)
163 trans.Close()
164 }
165 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200166 trans, err := tf.GetTransport(nil)
167 if err != nil {
168 t.Error(err)
169 continue
170 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200171 p := protocolFactory.GetProtocol(trans)
172 ReadWriteI32(t, p, trans)
173 trans.Close()
174 }
175 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200176 trans, err := tf.GetTransport(nil)
177 if err != nil {
178 t.Error(err)
179 continue
180 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200181 p := protocolFactory.GetProtocol(trans)
182 ReadWriteI64(t, p, trans)
183 trans.Close()
184 }
185 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200186 trans, err := tf.GetTransport(nil)
187 if err != nil {
188 t.Error(err)
189 continue
190 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200191 p := protocolFactory.GetProtocol(trans)
192 ReadWriteDouble(t, p, trans)
193 trans.Close()
194 }
195 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200196 trans, err := tf.GetTransport(nil)
197 if err != nil {
198 t.Error(err)
199 continue
200 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200201 p := protocolFactory.GetProtocol(trans)
202 ReadWriteString(t, p, trans)
203 trans.Close()
204 }
205 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200206 trans, err := tf.GetTransport(nil)
207 if err != nil {
208 t.Error(err)
209 continue
210 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200211 p := protocolFactory.GetProtocol(trans)
212 ReadWriteBinary(t, p, trans)
213 trans.Close()
214 }
Jens Geyer0e87c462013-06-18 22:25:07 +0200215 for _, tf := range transports {
D. Can Celasun8da0e722017-06-02 14:33:32 +0200216 trans, err := tf.GetTransport(nil)
217 if err != nil {
218 t.Error(err)
219 continue
220 }
Jens Geyer706cb4e2014-03-19 00:37:10 +0200221 p := protocolFactory.GetProtocol(trans)
Yuxuan 'fishy' Wang19c13b42022-10-12 14:13:15 -0700222 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 Geyer706cb4e2014-03-19 00:37:10 +0200232 ReadWriteI64(t, p, trans)
233 ReadWriteDouble(t, p, trans)
234 ReadWriteBinary(t, p, trans)
235 ReadWriteByte(t, p, trans)
Yuxuan 'fishy' Wang19c13b42022-10-12 14:13:15 -0700236 ReadWriteUUID(t, p, trans)
Jens Geyer706cb4e2014-03-19 00:37:10 +0200237 trans.Close()
Jens Geyer0e87c462013-06-18 22:25:07 +0200238 }
Yuxuan 'fishy' Wang64c2a4b2020-10-10 18:39:32 -0700239
240 t.Run("UnmatchedBeginEnd", func(t *testing.T) {
241 UnmatchedBeginEndProtocolTest(t, protocolFactory)
242 })
Jens Geyer0e87c462013-06-18 22:25:07 +0200243}
244
Jens Geyer09972502014-05-02 01:30:13 +0200245func ReadWriteBool(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200246 thetype := TType(BOOL)
247 thelen := len(BOOL_VALUES)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700248 err := p.WriteListBegin(context.Background(), thetype, thelen)
Jens Geyer0e87c462013-06-18 22:25:07 +0200249 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' Wange79f7642020-06-12 22:22:35 -0700253 err = p.WriteBool(context.Background(), v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200254 if err != nil {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100255 t.Errorf("%s: %T %T %v Error writing bool in list at index %v: %v", "ReadWriteBool", p, trans, err, k, v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200256 }
257 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700258 p.WriteListEnd(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200259 if err != nil {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100260 t.Errorf("%s: %T %T %v Error writing list end: %v", "ReadWriteBool", p, trans, err, BOOL_VALUES)
Jens Geyer0e87c462013-06-18 22:25:07 +0200261 }
John Boiles57852792018-01-05 14:37:05 -0800262 p.Flush(context.Background())
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700263 thetype2, thelen2, err := p.ReadListBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200264 if err != nil {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100265 t.Errorf("%s: %T %T %v Error reading list: %v", "ReadWriteBool", p, trans, err, BOOL_VALUES)
Jens Geyer0e87c462013-06-18 22:25:07 +0200266 }
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 Celasuna9efd1a2018-03-15 12:52:37 +0100273 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteBool", p, trans, thelen, thelen2)
Jens Geyer0e87c462013-06-18 22:25:07 +0200274 }
275 }
276 for k, v := range BOOL_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700277 value, err := p.ReadBool(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200278 if err != nil {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100279 t.Errorf("%s: %T %T %v Error reading bool at index %v: %v", "ReadWriteBool", p, trans, err, k, v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200280 }
281 if v != value {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100282 t.Errorf("%s: index %v %v %v %v != %v", "ReadWriteBool", k, p, trans, v, value)
Jens Geyer0e87c462013-06-18 22:25:07 +0200283 }
284 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700285 err = p.ReadListEnd(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200286 if err != nil {
287 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteBool", p, trans, err)
288 }
289}
290
Jens Geyer09972502014-05-02 01:30:13 +0200291func ReadWriteByte(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200292 thetype := TType(BYTE)
293 thelen := len(BYTE_VALUES)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700294 err := p.WriteListBegin(context.Background(), thetype, thelen)
Jens Geyer0e87c462013-06-18 22:25:07 +0200295 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' Wange79f7642020-06-12 22:22:35 -0700299 err = p.WriteByte(context.Background(), v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200300 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' Wange79f7642020-06-12 22:22:35 -0700304 err = p.WriteListEnd(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200305 if err != nil {
306 t.Errorf("%s: %T %T %q Error writing list end: %q", "ReadWriteByte", p, trans, err, BYTE_VALUES)
307 }
John Boiles57852792018-01-05 14:37:05 -0800308 err = p.Flush(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200309 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' Wange79f7642020-06-12 22:22:35 -0700312 thetype2, thelen2, err := p.ReadListBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200313 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 Celasuna9efd1a2018-03-15 12:52:37 +0100322 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteByte", p, trans, thelen, thelen2)
Jens Geyer0e87c462013-06-18 22:25:07 +0200323 }
324 }
325 for k, v := range BYTE_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700326 value, err := p.ReadByte(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200327 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' Wange79f7642020-06-12 22:22:35 -0700334 err = p.ReadListEnd(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200335 if err != nil {
336 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteByte", p, trans, err)
337 }
338}
339
Jens Geyer09972502014-05-02 01:30:13 +0200340func ReadWriteI16(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200341 thetype := TType(I16)
342 thelen := len(INT16_VALUES)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700343 p.WriteListBegin(context.Background(), thetype, thelen)
Jens Geyer0e87c462013-06-18 22:25:07 +0200344 for _, v := range INT16_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700345 p.WriteI16(context.Background(), v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200346 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700347 p.WriteListEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800348 p.Flush(context.Background())
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700349 thetype2, thelen2, err := p.ReadListBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200350 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 Celasuna9efd1a2018-03-15 12:52:37 +0100359 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI16", p, trans, thelen, thelen2)
Jens Geyer0e87c462013-06-18 22:25:07 +0200360 }
361 }
362 for k, v := range INT16_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700363 value, err := p.ReadI16(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200364 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' Wange79f7642020-06-12 22:22:35 -0700371 err = p.ReadListEnd(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200372 if err != nil {
373 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteI16", p, trans, err)
374 }
375}
376
Jens Geyer09972502014-05-02 01:30:13 +0200377func ReadWriteI32(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200378 thetype := TType(I32)
379 thelen := len(INT32_VALUES)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700380 p.WriteListBegin(context.Background(), thetype, thelen)
Jens Geyer0e87c462013-06-18 22:25:07 +0200381 for _, v := range INT32_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700382 p.WriteI32(context.Background(), v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200383 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700384 p.WriteListEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800385 p.Flush(context.Background())
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700386 thetype2, thelen2, err := p.ReadListBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200387 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 Celasuna9efd1a2018-03-15 12:52:37 +0100396 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI32", p, trans, thelen, thelen2)
Jens Geyer0e87c462013-06-18 22:25:07 +0200397 }
398 }
399 for k, v := range INT32_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700400 value, err := p.ReadI32(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200401 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 Geyer09972502014-05-02 01:30:13 +0200413func ReadWriteI64(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200414 thetype := TType(I64)
415 thelen := len(INT64_VALUES)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700416 p.WriteListBegin(context.Background(), thetype, thelen)
Jens Geyer0e87c462013-06-18 22:25:07 +0200417 for _, v := range INT64_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700418 p.WriteI64(context.Background(), v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200419 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700420 p.WriteListEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800421 p.Flush(context.Background())
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700422 thetype2, thelen2, err := p.ReadListBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200423 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 Celasuna9efd1a2018-03-15 12:52:37 +0100432 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI64", p, trans, thelen, thelen2)
Jens Geyer0e87c462013-06-18 22:25:07 +0200433 }
434 }
435 for k, v := range INT64_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700436 value, err := p.ReadI64(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200437 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 Geyer09972502014-05-02 01:30:13 +0200449func ReadWriteDouble(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200450 thetype := TType(DOUBLE)
451 thelen := len(DOUBLE_VALUES)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700452 p.WriteListBegin(context.Background(), thetype, thelen)
Jens Geyer0e87c462013-06-18 22:25:07 +0200453 for _, v := range DOUBLE_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700454 p.WriteDouble(context.Background(), v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200455 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700456 p.WriteListEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800457 p.Flush(context.Background())
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700458 thetype2, thelen2, err := p.ReadListBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200459 if err != nil {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100460 t.Errorf("%s: %T %T %v Error reading list: %v", "ReadWriteDouble", p, trans, err, DOUBLE_VALUES)
Jens Geyer0e87c462013-06-18 22:25:07 +0200461 }
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 Celasuna9efd1a2018-03-15 12:52:37 +0100466 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteDouble", p, trans, thelen, thelen2)
Jens Geyer0e87c462013-06-18 22:25:07 +0200467 }
468 for k, v := range DOUBLE_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700469 value, err := p.ReadDouble(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200470 if err != nil {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100471 t.Errorf("%s: %T %T %q Error reading double at index %d: %v", "ReadWriteDouble", p, trans, err, k, v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200472 }
473 if math.IsNaN(v) {
474 if !math.IsNaN(value) {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100475 t.Errorf("%s: %T %T math.IsNaN(%v) != math.IsNaN(%v)", "ReadWriteDouble", p, trans, v, value)
Jens Geyer0e87c462013-06-18 22:25:07 +0200476 }
477 } else if v != value {
D. Can Celasuna9efd1a2018-03-15 12:52:37 +0100478 t.Errorf("%s: %T %T %v != %v", "ReadWriteDouble", p, trans, v, value)
Jens Geyer0e87c462013-06-18 22:25:07 +0200479 }
480 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700481 err = p.ReadListEnd(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200482 if err != nil {
483 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteDouble", p, trans, err)
484 }
485}
486
Jens Geyer09972502014-05-02 01:30:13 +0200487func ReadWriteString(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200488 thetype := TType(STRING)
489 thelen := len(STRING_VALUES)
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700490 p.WriteListBegin(context.Background(), thetype, thelen)
Jens Geyer0e87c462013-06-18 22:25:07 +0200491 for _, v := range STRING_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700492 p.WriteString(context.Background(), v)
Jens Geyer0e87c462013-06-18 22:25:07 +0200493 }
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700494 p.WriteListEnd(context.Background())
John Boiles57852792018-01-05 14:37:05 -0800495 p.Flush(context.Background())
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700496 thetype2, thelen2, err := p.ReadListBegin(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200497 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 Celasuna9efd1a2018-03-15 12:52:37 +0100506 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteString", p, trans, thelen, thelen2)
Jens Geyer0e87c462013-06-18 22:25:07 +0200507 }
508 }
509 for k, v := range STRING_VALUES {
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700510 value, err := p.ReadString(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200511 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 Celasuna9efd1a2018-03-15 12:52:37 +0100515 t.Errorf("%s: %T %T %v != %v", "ReadWriteString", p, trans, v, value)
Jens Geyer0e87c462013-06-18 22:25:07 +0200516 }
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 Geyer09972502014-05-02 01:30:13 +0200523func ReadWriteBinary(t testing.TB, p TProtocol, trans TTransport) {
Jens Geyer0e87c462013-06-18 22:25:07 +0200524 v := protocol_bdata
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700525 p.WriteBinary(context.Background(), v)
John Boiles57852792018-01-05 14:37:05 -0800526 p.Flush(context.Background())
Yuxuan 'fishy' Wange79f7642020-06-12 22:22:35 -0700527 value, err := p.ReadBinary(context.Background())
Jens Geyer0e87c462013-06-18 22:25:07 +0200528 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' Wang91565d42024-08-14 09:01:15 -0700534 for i := range v {
Jens Geyer0e87c462013-06-18 22:25:07 +0200535 if v[i] != value[i] {
536 t.Errorf("%s: %T %T %s != %s", "ReadWriteBinary", p, trans, v, value)
537 }
538 }
539 }
540}
Yuxuan 'fishy' Wang64c2a4b2020-10-10 18:39:32 -0700541
Yuxuan 'fishy' Wang19c13b42022-10-12 14:13:15 -0700542func 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' Wang64c2a4b2020-10-10 18:39:32 -0700579func 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}