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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
32 const PROTOCOL_BINARY_DATA_SIZE = 155
35 protocol_bdata []byte // test data for writing; same as data
41 DOUBLE_VALUES []float64
42 STRING_VALUES []string
46 protocol_bdata = make([]byte, PROTOCOL_BINARY_DATA_SIZE)
47 for i := 0; i < PROTOCOL_BINARY_DATA_SIZE; i++ {
48 protocol_bdata[i] = byte((i + 'a') % 255)
50 BOOL_VALUES = []bool{false, true, false, false, true}
51 BYTE_VALUES = []int8{117, 0, 1, 32, 127, -128, -1}
52 INT16_VALUES = []int16{459, 0, 1, -1, -128, 127, 32767, -32768}
53 INT32_VALUES = []int32{459, 0, 1, -1, -128, 127, 32767, 2147483647, -2147483535}
54 INT64_VALUES = []int64{459, 0, 1, -1, -128, 127, 32767, 2147483647, -2147483535, 34359738481, -35184372088719, -9223372036854775808, 9223372036854775807}
55 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()}
56 STRING_VALUES = []string{"", "a", "st[uf]f", "st,u:ff with spaces", "stuff\twith\nescape\\characters'...\"lots{of}fun</xml>"}
59 type HTTPEchoServer struct{}
60 type HTTPHeaderEchoServer struct{}
62 func (p *HTTPEchoServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
63 buf, err := ioutil.ReadAll(req.Body)
65 w.WriteHeader(http.StatusBadRequest)
68 w.WriteHeader(http.StatusOK)
73 func (p *HTTPHeaderEchoServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
74 buf, err := ioutil.ReadAll(req.Body)
76 w.WriteHeader(http.StatusBadRequest)
79 w.WriteHeader(http.StatusOK)
84 func HttpClientSetupForTest(t *testing.T) (net.Listener, net.Addr) {
85 addr, err := FindAvailableTCPServerPort(40000)
87 t.Fatalf("Unable to find available tcp port addr: %s", err)
90 l, err := net.Listen(addr.Network(), addr.String())
92 t.Fatalf("Unable to setup tcp listener on %s: %s", addr.String(), err)
95 go http.Serve(l, &HTTPEchoServer{})
99 func HttpClientSetupForHeaderTest(t *testing.T) (net.Listener, net.Addr) {
100 addr, err := FindAvailableTCPServerPort(40000)
102 t.Fatalf("Unable to find available tcp port addr: %s", err)
105 l, err := net.Listen(addr.Network(), addr.String())
107 t.Fatalf("Unable to setup tcp listener on %s: %s", addr.String(), err)
110 go http.Serve(l, &HTTPHeaderEchoServer{})
114 func ReadWriteProtocolTest(t *testing.T, protocolFactory TProtocolFactory) {
115 buf := bytes.NewBuffer(make([]byte, 0, 1024))
116 l, addr := HttpClientSetupForTest(t)
118 transports := []TTransportFactory{
119 NewTMemoryBufferTransportFactory(1024),
120 NewStreamTransportFactory(buf, buf, true),
121 NewTFramedTransportFactory(NewTMemoryBufferTransportFactory(1024)),
122 NewTZlibTransportFactoryWithFactory(0, NewTMemoryBufferTransportFactory(1024)),
123 NewTZlibTransportFactoryWithFactory(6, NewTMemoryBufferTransportFactory(1024)),
124 NewTZlibTransportFactoryWithFactory(9, NewTFramedTransportFactory(NewTMemoryBufferTransportFactory(1024))),
125 NewTHttpPostClientTransportFactory("http://" + addr.String()),
127 for _, tf := range transports {
128 trans, err := tf.GetTransport(nil)
133 p := protocolFactory.GetProtocol(trans)
134 ReadWriteBool(t, p, trans)
137 for _, tf := range transports {
138 trans, err := tf.GetTransport(nil)
143 p := protocolFactory.GetProtocol(trans)
144 ReadWriteByte(t, p, trans)
147 for _, tf := range transports {
148 trans, err := tf.GetTransport(nil)
153 p := protocolFactory.GetProtocol(trans)
154 ReadWriteI16(t, p, trans)
157 for _, tf := range transports {
158 trans, err := tf.GetTransport(nil)
163 p := protocolFactory.GetProtocol(trans)
164 ReadWriteI32(t, p, trans)
167 for _, tf := range transports {
168 trans, err := tf.GetTransport(nil)
173 p := protocolFactory.GetProtocol(trans)
174 ReadWriteI64(t, p, trans)
177 for _, tf := range transports {
178 trans, err := tf.GetTransport(nil)
183 p := protocolFactory.GetProtocol(trans)
184 ReadWriteDouble(t, p, trans)
187 for _, tf := range transports {
188 trans, err := tf.GetTransport(nil)
193 p := protocolFactory.GetProtocol(trans)
194 ReadWriteString(t, p, trans)
197 for _, tf := range transports {
198 trans, err := tf.GetTransport(nil)
203 p := protocolFactory.GetProtocol(trans)
204 ReadWriteBinary(t, p, trans)
207 for _, tf := range transports {
208 trans, err := tf.GetTransport(nil)
213 p := protocolFactory.GetProtocol(trans)
214 ReadWriteI64(t, p, trans)
215 ReadWriteDouble(t, p, trans)
216 ReadWriteBinary(t, p, trans)
217 ReadWriteByte(t, p, trans)
222 func ReadWriteBool(t testing.TB, p TProtocol, trans TTransport) {
223 thetype := TType(BOOL)
224 thelen := len(BOOL_VALUES)
225 err := p.WriteListBegin(thetype, thelen)
227 t.Errorf("%s: %T %T %q Error writing list begin: %q", "ReadWriteBool", p, trans, err, thetype)
229 for k, v := range BOOL_VALUES {
232 t.Errorf("%s: %T %T %v Error writing bool in list at index %v: %v", "ReadWriteBool", p, trans, err, k, v)
237 t.Errorf("%s: %T %T %v Error writing list end: %v", "ReadWriteBool", p, trans, err, BOOL_VALUES)
239 p.Flush(context.Background())
240 thetype2, thelen2, err := p.ReadListBegin()
242 t.Errorf("%s: %T %T %v Error reading list: %v", "ReadWriteBool", p, trans, err, BOOL_VALUES)
244 _, ok := p.(*TSimpleJSONProtocol)
246 if thetype != thetype2 {
247 t.Errorf("%s: %T %T type %s != type %s", "ReadWriteBool", p, trans, thetype, thetype2)
249 if thelen != thelen2 {
250 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteBool", p, trans, thelen, thelen2)
253 for k, v := range BOOL_VALUES {
254 value, err := p.ReadBool()
256 t.Errorf("%s: %T %T %v Error reading bool at index %v: %v", "ReadWriteBool", p, trans, err, k, v)
259 t.Errorf("%s: index %v %v %v %v != %v", "ReadWriteBool", k, p, trans, v, value)
262 err = p.ReadListEnd()
264 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteBool", p, trans, err)
268 func ReadWriteByte(t testing.TB, p TProtocol, trans TTransport) {
269 thetype := TType(BYTE)
270 thelen := len(BYTE_VALUES)
271 err := p.WriteListBegin(thetype, thelen)
273 t.Errorf("%s: %T %T %q Error writing list begin: %q", "ReadWriteByte", p, trans, err, thetype)
275 for k, v := range BYTE_VALUES {
278 t.Errorf("%s: %T %T %q Error writing byte in list at index %d: %q", "ReadWriteByte", p, trans, err, k, v)
281 err = p.WriteListEnd()
283 t.Errorf("%s: %T %T %q Error writing list end: %q", "ReadWriteByte", p, trans, err, BYTE_VALUES)
285 err = p.Flush(context.Background())
287 t.Errorf("%s: %T %T %q Error flushing list of bytes: %q", "ReadWriteByte", p, trans, err, BYTE_VALUES)
289 thetype2, thelen2, err := p.ReadListBegin()
291 t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteByte", p, trans, err, BYTE_VALUES)
293 _, ok := p.(*TSimpleJSONProtocol)
295 if thetype != thetype2 {
296 t.Errorf("%s: %T %T type %s != type %s", "ReadWriteByte", p, trans, thetype, thetype2)
298 if thelen != thelen2 {
299 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteByte", p, trans, thelen, thelen2)
302 for k, v := range BYTE_VALUES {
303 value, err := p.ReadByte()
305 t.Errorf("%s: %T %T %q Error reading byte at index %d: %q", "ReadWriteByte", p, trans, err, k, v)
308 t.Errorf("%s: %T %T %d != %d", "ReadWriteByte", p, trans, v, value)
311 err = p.ReadListEnd()
313 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteByte", p, trans, err)
317 func ReadWriteI16(t testing.TB, p TProtocol, trans TTransport) {
318 thetype := TType(I16)
319 thelen := len(INT16_VALUES)
320 p.WriteListBegin(thetype, thelen)
321 for _, v := range INT16_VALUES {
325 p.Flush(context.Background())
326 thetype2, thelen2, err := p.ReadListBegin()
328 t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteI16", p, trans, err, INT16_VALUES)
330 _, ok := p.(*TSimpleJSONProtocol)
332 if thetype != thetype2 {
333 t.Errorf("%s: %T %T type %s != type %s", "ReadWriteI16", p, trans, thetype, thetype2)
335 if thelen != thelen2 {
336 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI16", p, trans, thelen, thelen2)
339 for k, v := range INT16_VALUES {
340 value, err := p.ReadI16()
342 t.Errorf("%s: %T %T %q Error reading int16 at index %d: %q", "ReadWriteI16", p, trans, err, k, v)
345 t.Errorf("%s: %T %T %d != %d", "ReadWriteI16", p, trans, v, value)
348 err = p.ReadListEnd()
350 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteI16", p, trans, err)
354 func ReadWriteI32(t testing.TB, p TProtocol, trans TTransport) {
355 thetype := TType(I32)
356 thelen := len(INT32_VALUES)
357 p.WriteListBegin(thetype, thelen)
358 for _, v := range INT32_VALUES {
362 p.Flush(context.Background())
363 thetype2, thelen2, err := p.ReadListBegin()
365 t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteI32", p, trans, err, INT32_VALUES)
367 _, ok := p.(*TSimpleJSONProtocol)
369 if thetype != thetype2 {
370 t.Errorf("%s: %T %T type %s != type %s", "ReadWriteI32", p, trans, thetype, thetype2)
372 if thelen != thelen2 {
373 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI32", p, trans, thelen, thelen2)
376 for k, v := range INT32_VALUES {
377 value, err := p.ReadI32()
379 t.Errorf("%s: %T %T %q Error reading int32 at index %d: %q", "ReadWriteI32", p, trans, err, k, v)
382 t.Errorf("%s: %T %T %d != %d", "ReadWriteI32", p, trans, v, value)
386 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteI32", p, trans, err)
390 func ReadWriteI64(t testing.TB, p TProtocol, trans TTransport) {
391 thetype := TType(I64)
392 thelen := len(INT64_VALUES)
393 p.WriteListBegin(thetype, thelen)
394 for _, v := range INT64_VALUES {
398 p.Flush(context.Background())
399 thetype2, thelen2, err := p.ReadListBegin()
401 t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteI64", p, trans, err, INT64_VALUES)
403 _, ok := p.(*TSimpleJSONProtocol)
405 if thetype != thetype2 {
406 t.Errorf("%s: %T %T type %s != type %s", "ReadWriteI64", p, trans, thetype, thetype2)
408 if thelen != thelen2 {
409 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteI64", p, trans, thelen, thelen2)
412 for k, v := range INT64_VALUES {
413 value, err := p.ReadI64()
415 t.Errorf("%s: %T %T %q Error reading int64 at index %d: %q", "ReadWriteI64", p, trans, err, k, v)
418 t.Errorf("%s: %T %T %q != %q", "ReadWriteI64", p, trans, v, value)
422 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteI64", p, trans, err)
426 func ReadWriteDouble(t testing.TB, p TProtocol, trans TTransport) {
427 thetype := TType(DOUBLE)
428 thelen := len(DOUBLE_VALUES)
429 p.WriteListBegin(thetype, thelen)
430 for _, v := range DOUBLE_VALUES {
434 p.Flush(context.Background())
435 thetype2, thelen2, err := p.ReadListBegin()
437 t.Errorf("%s: %T %T %v Error reading list: %v", "ReadWriteDouble", p, trans, err, DOUBLE_VALUES)
439 if thetype != thetype2 {
440 t.Errorf("%s: %T %T type %s != type %s", "ReadWriteDouble", p, trans, thetype, thetype2)
442 if thelen != thelen2 {
443 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteDouble", p, trans, thelen, thelen2)
445 for k, v := range DOUBLE_VALUES {
446 value, err := p.ReadDouble()
448 t.Errorf("%s: %T %T %q Error reading double at index %d: %v", "ReadWriteDouble", p, trans, err, k, v)
451 if !math.IsNaN(value) {
452 t.Errorf("%s: %T %T math.IsNaN(%v) != math.IsNaN(%v)", "ReadWriteDouble", p, trans, v, value)
454 } else if v != value {
455 t.Errorf("%s: %T %T %v != %v", "ReadWriteDouble", p, trans, v, value)
458 err = p.ReadListEnd()
460 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteDouble", p, trans, err)
464 func ReadWriteString(t testing.TB, p TProtocol, trans TTransport) {
465 thetype := TType(STRING)
466 thelen := len(STRING_VALUES)
467 p.WriteListBegin(thetype, thelen)
468 for _, v := range STRING_VALUES {
472 p.Flush(context.Background())
473 thetype2, thelen2, err := p.ReadListBegin()
475 t.Errorf("%s: %T %T %q Error reading list: %q", "ReadWriteString", p, trans, err, STRING_VALUES)
477 _, ok := p.(*TSimpleJSONProtocol)
479 if thetype != thetype2 {
480 t.Errorf("%s: %T %T type %s != type %s", "ReadWriteString", p, trans, thetype, thetype2)
482 if thelen != thelen2 {
483 t.Errorf("%s: %T %T len %v != len %v", "ReadWriteString", p, trans, thelen, thelen2)
486 for k, v := range STRING_VALUES {
487 value, err := p.ReadString()
489 t.Errorf("%s: %T %T %q Error reading string at index %d: %q", "ReadWriteString", p, trans, err, k, v)
492 t.Errorf("%s: %T %T %v != %v", "ReadWriteString", p, trans, v, value)
496 t.Errorf("%s: %T %T Unable to read list end: %q", "ReadWriteString", p, trans, err)
500 func ReadWriteBinary(t testing.TB, p TProtocol, trans TTransport) {
503 p.Flush(context.Background())
504 value, err := p.ReadBinary()
506 t.Errorf("%s: %T %T Unable to read binary: %s", "ReadWriteBinary", p, trans, err.Error())
508 if len(v) != len(value) {
509 t.Errorf("%s: %T %T len(v) != len(value)... %d != %d", "ReadWriteBinary", p, trans, len(v), len(value))
511 for i := 0; i < len(v); i++ {
512 if v[i] != value[i] {
513 t.Errorf("%s: %T %T %s != %s", "ReadWriteBinary", p, trans, v, value)