github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/testdata/binary_test.go (about)

     1  /**
     2   * Copyright 2023 CloudWeGo Authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package testdata
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/hex"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/cloudwego/dynamicgo/meta"
    25  	"github.com/cloudwego/dynamicgo/thrift"
    26  	thrift_iter "github.com/thrift-iterator/go"
    27  	"github.com/thrift-iterator/go/general"
    28  	"github.com/thrift-iterator/go/protocol"
    29  )
    30  
    31  func TestBinaryUnwrapReply(t *testing.T) {
    32  
    33  	type args struct {
    34  		proto meta.Encoding
    35  		msg   general.Message
    36  	}
    37  	tests := []struct {
    38  		name    string
    39  		args    args
    40  		wantErr bool
    41  	}{
    42  		{
    43  			name: "simple",
    44  			args: args{
    45  				proto: meta.EncodingThriftBinary,
    46  				msg: general.Message{
    47  					MessageHeader: protocol.MessageHeader{
    48  						MessageName: "GetFavoriteMethod",
    49  						MessageType: protocol.MessageTypeReply,
    50  					},
    51  					Arguments: general.Struct{
    52  						1: general.Struct{
    53  							1: int32(7749),
    54  						},
    55  					},
    56  				},
    57  			},
    58  		},
    59  	}
    60  	for _, tt := range tests {
    61  		t.Run(tt.name, func(t *testing.T) {
    62  			// pair of buffer
    63  			inputBytes, err := thrift_iter.Marshal(tt.args.msg)
    64  			if err != nil {
    65  				t.Fatalf("failed to marshal input %s", err)
    66  			}
    67  			wantBytes, err := thrift_iter.Marshal(tt.args.msg.Arguments[1].(general.Struct))
    68  			if err != nil {
    69  				t.Fatalf("failed to marshal want bytes %s", err)
    70  			}
    71  			//
    72  			_, _, _, _, got, err := thrift.UnwrapBinaryMessage(inputBytes)
    73  			if (err != nil) != tt.wantErr {
    74  				t.Errorf("UnwrapReply() error = %v, wantErr %v", err, tt.wantErr)
    75  				return
    76  			}
    77  			if !reflect.DeepEqual(got, wantBytes) {
    78  				t.Errorf("UnwrapReply() = %s, want %s", hex.EncodeToString(got), hex.EncodeToString(wantBytes))
    79  			}
    80  			header, footer, err := thrift.GetBinaryMessageHeaderAndFooter(tt.args.msg.MessageHeader.MessageName, thrift.TMessageType(tt.args.msg.MessageHeader.MessageType), 1, 0)
    81  			if err != nil {
    82  				t.Fatalf("failed to wrap message %s", err)
    83  			}
    84  			header = append(header, got...)
    85  			header = append(header, footer...)
    86  			if !bytes.Equal(header, inputBytes) {
    87  				t.Errorf("WrapBinaryMessage() = %s, want %s", hex.EncodeToString(header), hex.EncodeToString(inputBytes))
    88  			}
    89  
    90  		})
    91  	}
    92  }