github.com/segmentio/encoding@v0.4.0/thrift/protocol_test.go (about) 1 package thrift_test 2 3 import ( 4 "bytes" 5 "reflect" 6 "strings" 7 "testing" 8 9 "github.com/segmentio/encoding/thrift" 10 ) 11 12 var protocolReadWriteTests = [...]struct { 13 scenario string 14 read interface{} 15 write interface{} 16 values []interface{} 17 }{ 18 { 19 scenario: "bool", 20 read: thrift.Reader.ReadBool, 21 write: thrift.Writer.WriteBool, 22 values: []interface{}{false, true}, 23 }, 24 25 { 26 scenario: "int8", 27 read: thrift.Reader.ReadInt8, 28 write: thrift.Writer.WriteInt8, 29 values: []interface{}{int8(0), int8(1), int8(-1)}, 30 }, 31 32 { 33 scenario: "int16", 34 read: thrift.Reader.ReadInt16, 35 write: thrift.Writer.WriteInt16, 36 values: []interface{}{int16(0), int16(1), int16(-1)}, 37 }, 38 39 { 40 scenario: "int32", 41 read: thrift.Reader.ReadInt32, 42 write: thrift.Writer.WriteInt32, 43 values: []interface{}{int32(0), int32(1), int32(-1)}, 44 }, 45 46 { 47 scenario: "int64", 48 read: thrift.Reader.ReadInt64, 49 write: thrift.Writer.WriteInt64, 50 values: []interface{}{int64(0), int64(1), int64(-1)}, 51 }, 52 53 { 54 scenario: "float64", 55 read: thrift.Reader.ReadFloat64, 56 write: thrift.Writer.WriteFloat64, 57 values: []interface{}{float64(0), float64(1), float64(-1)}, 58 }, 59 60 { 61 scenario: "bytes", 62 read: thrift.Reader.ReadBytes, 63 write: thrift.Writer.WriteBytes, 64 values: []interface{}{ 65 []byte(""), 66 []byte("A"), 67 []byte("1234567890"), 68 bytes.Repeat([]byte("qwertyuiop"), 100), 69 }, 70 }, 71 72 { 73 scenario: "string", 74 read: thrift.Reader.ReadString, 75 write: thrift.Writer.WriteString, 76 values: []interface{}{ 77 "", 78 "A", 79 "1234567890", 80 strings.Repeat("qwertyuiop", 100), 81 }, 82 }, 83 84 { 85 scenario: "message", 86 read: thrift.Reader.ReadMessage, 87 write: thrift.Writer.WriteMessage, 88 values: []interface{}{ 89 thrift.Message{}, 90 thrift.Message{Type: thrift.Call, Name: "Hello", SeqID: 10}, 91 thrift.Message{Type: thrift.Reply, Name: "World", SeqID: 11}, 92 thrift.Message{Type: thrift.Exception, Name: "Foo", SeqID: 40}, 93 thrift.Message{Type: thrift.Oneway, Name: "Bar", SeqID: 42}, 94 }, 95 }, 96 97 { 98 scenario: "field", 99 read: thrift.Reader.ReadField, 100 write: thrift.Writer.WriteField, 101 values: []interface{}{ 102 thrift.Field{ID: 101, Type: thrift.TRUE}, 103 thrift.Field{ID: 102, Type: thrift.FALSE}, 104 thrift.Field{ID: 103, Type: thrift.I8}, 105 thrift.Field{ID: 104, Type: thrift.I16}, 106 thrift.Field{ID: 105, Type: thrift.I32}, 107 thrift.Field{ID: 106, Type: thrift.I64}, 108 thrift.Field{ID: 107, Type: thrift.DOUBLE}, 109 thrift.Field{ID: 108, Type: thrift.BINARY}, 110 thrift.Field{ID: 109, Type: thrift.LIST}, 111 thrift.Field{ID: 110, Type: thrift.SET}, 112 thrift.Field{ID: 111, Type: thrift.MAP}, 113 thrift.Field{ID: 112, Type: thrift.STRUCT}, 114 thrift.Field{}, 115 }, 116 }, 117 118 { 119 scenario: "list", 120 read: thrift.Reader.ReadList, 121 write: thrift.Writer.WriteList, 122 values: []interface{}{ 123 thrift.List{}, 124 thrift.List{Size: 0, Type: thrift.BOOL}, 125 thrift.List{Size: 1, Type: thrift.I8}, 126 thrift.List{Size: 1000, Type: thrift.BINARY}, 127 }, 128 }, 129 130 { 131 scenario: "map", 132 read: thrift.Reader.ReadMap, 133 write: thrift.Writer.WriteMap, 134 values: []interface{}{ 135 thrift.Map{}, 136 thrift.Map{Size: 1, Key: thrift.BINARY, Value: thrift.MAP}, 137 thrift.Map{Size: 1000, Key: thrift.BINARY, Value: thrift.LIST}, 138 }, 139 }, 140 } 141 142 var protocols = [...]struct { 143 name string 144 proto thrift.Protocol 145 }{ 146 { 147 name: "binary(default)", 148 proto: &thrift.BinaryProtocol{}, 149 }, 150 151 { 152 name: "binary(non-strict)", 153 proto: &thrift.BinaryProtocol{ 154 NonStrict: true, 155 }, 156 }, 157 158 { 159 name: "compact", 160 proto: &thrift.CompactProtocol{}, 161 }, 162 } 163 164 func TestProtocols(t *testing.T) { 165 for _, test := range protocols { 166 t.Run(test.name, func(t *testing.T) { testProtocolReadWriteValues(t, test.proto) }) 167 } 168 } 169 170 func testProtocolReadWriteValues(t *testing.T, p thrift.Protocol) { 171 for _, test := range protocolReadWriteTests { 172 t.Run(test.scenario, func(t *testing.T) { 173 b := new(bytes.Buffer) 174 r := p.NewReader(b) 175 w := p.NewWriter(b) 176 177 for _, value := range test.values { 178 ret := reflect.ValueOf(test.write).Call([]reflect.Value{ 179 reflect.ValueOf(w), 180 reflect.ValueOf(value), 181 }) 182 if err, _ := ret[0].Interface().(error); err != nil { 183 t.Fatal("encoding:", err) 184 } 185 } 186 187 for _, value := range test.values { 188 ret := reflect.ValueOf(test.read).Call([]reflect.Value{ 189 reflect.ValueOf(r), 190 }) 191 if err, _ := ret[1].Interface().(error); err != nil { 192 t.Fatal("decoding:", err) 193 } 194 if res := ret[0].Interface(); !reflect.DeepEqual(value, res) { 195 t.Errorf("value mismatch:\nwant: %#v\ngot: %#v", value, res) 196 } 197 } 198 199 if b.Len() != 0 { 200 t.Errorf("unexpected trailing bytes: %d", b.Len()) 201 } 202 }) 203 } 204 }