go.uber.org/yarpc@v1.72.1/encoding/thrift/envelope_test.go (about) 1 // Copyright (c) 2022 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package thrift 22 23 import ( 24 "bytes" 25 "fmt" 26 "io" 27 "math/rand" 28 "reflect" 29 "testing" 30 "testing/quick" 31 "time" 32 33 "github.com/stretchr/testify/assert" 34 "github.com/stretchr/testify/require" 35 "go.uber.org/thriftrw/protocol/binary" 36 "go.uber.org/thriftrw/protocol/stream" 37 "go.uber.org/thriftrw/wire" 38 ) 39 40 func TestDisableEnveloperEncode(t *testing.T) { 41 rand := rand.New(rand.NewSource(time.Now().Unix())) 42 43 tests := []struct { 44 value wire.Value 45 want []byte 46 }{ 47 { 48 wire.NewValueStruct(wire.Struct{Fields: []wire.Field{}}), 49 []byte{0x00}, 50 }, 51 { 52 wire.NewValueStruct(wire.Struct{Fields: []wire.Field{ 53 {ID: 1, Value: wire.NewValueI32(42)}, 54 }}), 55 []byte{ 56 0x08, 0x00, 0x01, 57 0x00, 0x00, 0x00, 0x2a, 58 0x00, 59 }, 60 }, 61 } 62 63 for _, tt := range tests { 64 e := wire.Envelope{Value: tt.value, Type: wire.Call} 65 generate(&e.Name, rand) 66 generate(&e.SeqID, rand) 67 68 var buffer bytes.Buffer 69 proto := disableEnvelopingProtocol{binary.Default, wire.Reply} 70 if !assert.NoError(t, proto.EncodeEnveloped(e, &buffer)) { 71 continue 72 } 73 74 assert.Equal(t, tt.want, buffer.Bytes()) 75 76 gotE, err := proto.DecodeEnveloped(bytes.NewReader(tt.want)) 77 if !assert.NoError(t, err) { 78 continue 79 } 80 81 assert.Equal(t, wire.Reply, gotE.Type) 82 assert.True(t, wire.ValuesAreEqual(tt.value, gotE.Value)) 83 } 84 } 85 86 func TestDisableEnveloperNoWireRead(t *testing.T) { 87 cont := "some buffered contents" 88 buf := bytes.NewBuffer([]byte(cont)) 89 evnw := disableEnvelopingNoWireProtocol{Protocol: binary.Default, Type: wire.Call} 90 sr := evnw.Reader(buf) 91 evh, err := sr.ReadEnvelopeBegin() 92 require.NoError(t, err) 93 assert.Equal(t, wire.Call, evh.Type) 94 95 err = sr.ReadEnvelopeEnd() 96 require.NoError(t, err) 97 98 rem, err := io.ReadAll(buf) 99 require.NoError(t, err) 100 assert.Equal(t, cont, string(rem), "readenvelope is not supposed to read anything from the buffer") 101 } 102 103 func TestDisableEnveloperNoWireWrite(t *testing.T) { 104 buf := bytes.Buffer{} 105 evnw := disableEnvelopingNoWireProtocol{Protocol: binary.Default, Type: wire.OneWay} 106 sw := evnw.Writer(&buf) 107 108 err := sw.WriteEnvelopeBegin(stream.EnvelopeHeader{Name: "foo", Type: wire.Exception}) 109 require.NoError(t, err) 110 111 err = sw.WriteEnvelopeEnd() 112 assert.NoError(t, err) 113 assert.Zero(t, buf.Len(), "writeenvelope is not supposed to write to the buffer") 114 } 115 116 // generate generates a random value into the given pointer. 117 // 118 // var i int 119 // generate(&i, rand) 120 // 121 // If the type implements the quick.Generator interface, that is used. 122 func generate(v interface{}, r *rand.Rand) { 123 t := reflect.TypeOf(v) 124 if t.Kind() != reflect.Ptr { 125 panic(fmt.Sprintf("%v is not a pointer type", t)) 126 } 127 128 out, ok := quick.Value(t.Elem(), r) 129 if !ok { 130 panic(fmt.Sprintf("could not generate a value for %v", t)) 131 } 132 133 reflect.ValueOf(v).Elem().Set(out) 134 }