trpc.group/trpc-go/trpc-go@v1.0.3/codec/serialization_test.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package codec_test
    15  
    16  import (
    17  	"testing"
    18  
    19  	flatbuffers "github.com/google/flatbuffers/go"
    20  	"github.com/stretchr/testify/assert"
    21  	trpcpb "trpc.group/trpc/trpc-protocol/pb/go/trpc"
    22  
    23  	"trpc.group/trpc-go/trpc-go/codec"
    24  	fb "trpc.group/trpc-go/trpc-go/testdata/fbstest"
    25  	pb "trpc.group/trpc-go/trpc-go/testdata/trpc/helloworld"
    26  )
    27  
    28  // go test -v -coverprofile=cover.out
    29  // go tool cover -func=cover.out
    30  
    31  func TestSerialization(t *testing.T) {
    32  	noop := &codec.NoopSerialization{}
    33  	body := &codec.Body{Data: []byte("Serializer Body")}
    34  
    35  	str := body.String()
    36  	assert.Equal(t, "[83 101 114 105 97 108 105 122 101 114 32 66 111 100 121]", str)
    37  
    38  	invalidBodyType := []byte{}
    39  	_, err := noop.Marshal(invalidBodyType)
    40  	assert.NotNil(t, err)
    41  
    42  	err = noop.Unmarshal(body.Data, invalidBodyType)
    43  	assert.NotNil(t, err)
    44  
    45  	codec.RegisterSerializer(codec.SerializationTypeNoop, noop)
    46  
    47  	s := codec.GetSerializer(-1)
    48  	assert.Nil(t, s)
    49  
    50  	s = codec.GetSerializer(codec.SerializationTypeNoop)
    51  	assert.Equal(t, noop, s)
    52  
    53  	data, err := codec.Marshal(codec.SerializationTypeNoop, body)
    54  	assert.Nil(t, err)
    55  	assert.Equal(t, body.Data, data)
    56  
    57  	err = codec.Unmarshal(codec.SerializationTypeNoop, []byte("Serializer Unmarshal Body"), body)
    58  	assert.Nil(t, err)
    59  	assert.Equal(t, []byte("Serializer Unmarshal Body"), body.Data)
    60  
    61  	data, err = codec.Marshal(codec.SerializationTypePB, body)
    62  	assert.NotNil(t, err)
    63  	assert.Nil(t, data)
    64  
    65  	err = codec.Unmarshal(codec.SerializationTypePB, []byte("Serializer Unmarshal Body"), body)
    66  	assert.NotNil(t, err)
    67  
    68  	data, err = codec.Marshal(codec.SerializationTypeFlatBuffer, body)
    69  	assert.NotNil(t, err)
    70  	assert.Nil(t, data)
    71  
    72  	err = codec.Unmarshal(codec.SerializationTypeFlatBuffer, []byte("Serializer Unmarshal Body"), body)
    73  	assert.NotNil(t, err)
    74  
    75  	data, err = codec.Marshal(codec.SerializationTypeUnsupported, body)
    76  	assert.Nil(t, err)
    77  	assert.Nil(t, data)
    78  
    79  	err = codec.Unmarshal(codec.SerializationTypeUnsupported, []byte("Serializer Unmarshal Body"), body)
    80  	assert.Nil(t, err)
    81  
    82  	empty := []byte{}
    83  	emptyBody := (*codec.Body)(nil)
    84  	err = codec.Unmarshal(codec.SerializationTypeNoop, empty, body)
    85  	assert.Nil(t, err)
    86  
    87  	err = codec.Unmarshal(codec.SerializationTypeNoop, empty, emptyBody)
    88  	assert.Nil(t, err)
    89  
    90  	err = codec.Unmarshal(codec.SerializationTypeNoop, []byte("Serializer Unmarshal Body"), emptyBody)
    91  	assert.NotNil(t, err)
    92  
    93  	data, err = codec.Marshal(codec.SerializationTypeNoop, emptyBody)
    94  	assert.NotNil(t, err)
    95  	assert.Nil(t, data)
    96  
    97  	data, err = codec.Marshal(codec.SerializationTypePB, nil)
    98  	assert.Nil(t, err)
    99  	assert.Nil(t, data)
   100  
   101  	data, err = codec.Marshal(codec.SerializationTypeUnsupported, body)
   102  	assert.Nil(t, err)
   103  	assert.Nil(t, data)
   104  
   105  	err = codec.Unmarshal(codec.SerializationTypeUnsupported, nil, body)
   106  	assert.Nil(t, err)
   107  
   108  	err = codec.Unmarshal(codec.SerializationTypeUnsupported, nil, nil)
   109  	assert.Nil(t, err)
   110  
   111  	err = codec.Unmarshal(codec.SerializationTypeUnsupported, []byte{1, 2}, body)
   112  	assert.Nil(t, err)
   113  
   114  	_, err = codec.Marshal(100009, body)
   115  	assert.NotNil(t, err)
   116  
   117  	err = codec.Unmarshal(100009, []byte{1, 2}, body)
   118  	assert.NotNil(t, err)
   119  }
   120  
   121  func TestJson(t *testing.T) {
   122  	type Data struct {
   123  		A int
   124  		B string
   125  	}
   126  	s := &codec.JSONSerialization{}
   127  	body := []byte("{\"A\":1,\"B\":\"bb\"}")
   128  	data := &Data{}
   129  
   130  	err := s.Unmarshal(body, data)
   131  	assert.Nil(t, err)
   132  	assert.Equal(t, 1, data.A)
   133  	assert.Equal(t, "bb", data.B)
   134  
   135  	bytes, err := s.Marshal(data)
   136  	assert.Nil(t, err)
   137  	assert.Equal(t, body, bytes)
   138  
   139  	// json-iterator issue https://github.com/golang/go/issues/48238#issuecomment-917321536
   140  	m := make(map[string]string)
   141  	m["a"] = "hello"
   142  	bytes, err = s.Marshal(m)
   143  	body = []byte("{\"a\":\"hello\"}")
   144  	assert.Nil(t, err)
   145  	assert.Equal(t, body, bytes)
   146  }
   147  
   148  func TestJsonPB(t *testing.T) {
   149  	s := &codec.JSONPBSerialization{}
   150  	body := []byte("{\"msg\":\"utTest\"}")
   151  	data := &pb.HelloReply{}
   152  
   153  	err := s.Unmarshal(body, data)
   154  	assert.Nil(t, err)
   155  	assert.Equal(t, "utTest", data.Msg)
   156  
   157  	bytes, err := s.Marshal(data)
   158  	assert.Nil(t, err)
   159  	assert.Equal(t, body, bytes)
   160  }
   161  
   162  func TestJsonPBNotImplProto(t *testing.T) {
   163  	type Data struct {
   164  		A int
   165  		B string
   166  	}
   167  	s := &codec.JSONPBSerialization{}
   168  	data := &Data{A: 1, B: "test"}
   169  
   170  	bytes, err := s.Marshal(data)
   171  	assert.Nil(t, err)
   172  
   173  	var data1 Data
   174  	err = s.Unmarshal(bytes, &data1)
   175  	assert.Nil(t, err)
   176  	assert.Equal(t, data.A, data1.A)
   177  	assert.Equal(t, data.B, data1.B)
   178  }
   179  
   180  func TestProto(t *testing.T) {
   181  	p := &trpcpb.RequestProtocol{
   182  		Version: 1,
   183  		Func:    []byte("/trpc.test.helloworld.Greeter/SayHello"),
   184  	}
   185  
   186  	s := &codec.PBSerialization{}
   187  	data, err := s.Marshal(p)
   188  	assert.Nil(t, err)
   189  	p1 := &trpcpb.RequestProtocol{}
   190  
   191  	err = s.Unmarshal(data, p1)
   192  	assert.Nil(t, err)
   193  	assert.Equal(t, p.Version, p1.Version)
   194  }
   195  
   196  func TestFlatbuffers(t *testing.T) {
   197  	s := &codec.FBSerialization{}
   198  	b := flatbuffers.NewBuilder(0)
   199  	i := b.CreateString("this is a string")
   200  	fb.HelloReqStart(b)
   201  	fb.HelloReqAddMessage(b, i)
   202  	b.Finish(fb.HelloReqEnd(b))
   203  
   204  	data, err := s.Marshal(b)
   205  	assert.Nil(t, err)
   206  	assert.NotNil(t, data)
   207  
   208  	req := &fb.HelloReq{}
   209  	err = s.Unmarshal(data, req)
   210  	assert.Nil(t, err)
   211  	assert.Equal(t, "this is a string", string(req.Message()))
   212  }
   213  
   214  func TestXML(t *testing.T) {
   215  	type Data struct {
   216  		A int
   217  		B string
   218  	}
   219  	var tests = []struct {
   220  		In Data
   221  	}{
   222  		{In: Data{1, "1"}},
   223  		{In: Data{2, "2"}},
   224  	}
   225  
   226  	for _, tt := range tests {
   227  		buf, err := codec.Marshal(codec.SerializationTypeXML, tt.In)
   228  		assert.Nil(t, err)
   229  
   230  		got := &Data{}
   231  		err = codec.Unmarshal(codec.SerializationTypeXML, buf, got)
   232  		assert.Nil(t, err)
   233  
   234  		assert.Equal(t, tt.In.A, got.A)
   235  		assert.Equal(t, tt.In.B, got.B)
   236  	}
   237  }