github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/protocol/message/message_test.go (about) 1 package message 2 3 import ( 4 "errors" 5 "github.com/nyan233/littlerpc/core/container" 6 "github.com/nyan233/littlerpc/core/utils/convert" 7 "github.com/nyan233/littlerpc/core/utils/random" 8 "github.com/stretchr/testify/assert" 9 "math" 10 "sync" 11 "testing" 12 "unsafe" 13 ) 14 15 func FuzzMessageBytes(f *testing.F) { 16 bytes := make([]byte, 0) 17 msg := New() 18 msg.scope = [...]uint8{ 19 MagicNumber, 20 Call, 21 } 22 msg.msgId = 1234455 23 msg.payloadLength = 1024 24 msg.serviceName = "global/littlerpc/HelloTest.Say" 25 err := Marshal(msg, (*container.Slice[byte])(&bytes)) 26 if err != nil { 27 f.Fatal(err) 28 } 29 f.Add(bytes) 30 f.Fuzz(func(t *testing.T, data []byte) { 31 msg := New() 32 _ = Unmarshal(data, msg) 33 }) 34 } 35 36 func FuzzMessageReal(f *testing.F) { 37 f.Fuzz(func(t *testing.T, msgT uint8, codecScheme, encoderScheme []byte, msgId uint64, serviceName string, 38 key, value string, payloads []byte) { 39 msg := New() 40 msg.SetMsgType(msgT) 41 msg.MetaData.Store(CodecScheme, convert.BytesToString(codecScheme)) 42 msg.MetaData.Store(PackerScheme, convert.BytesToString(encoderScheme)) 43 msg.SetMsgId(msgId) 44 msg.SetServiceName(serviceName) 45 msg.MetaData.Store(key, value) 46 msg.AppendPayloads(payloads) 47 var bytes []byte 48 err := Marshal(msg, (*container.Slice[byte])(&bytes)) 49 if err != nil { 50 t.Log(err) 51 } 52 }) 53 } 54 55 func TestProtocol(t *testing.T) { 56 msg := New() 57 msg.SetMsgType(Return) 58 msg.MetaData.Store(CodecScheme, DefaultCodec) 59 msg.MetaData.Store(PackerScheme, DefaultPacker) 60 msg.SetMsgId(math.MaxUint64) 61 msg.SetServiceName("gps/lrpc_/HelloTest/api/v1/Say") 62 msg.AppendPayloads([]byte("hello world")) 63 msg.AppendPayloads([]byte("1378q285y45q")) 64 msg.MetaData.Store("Error", "My is Error") 65 msg.MetaData.Store("Hehe", "heheda") 66 pool := &sync.Pool{ 67 New: func() interface{} { 68 var tmp container.Slice[byte] = make([]byte, 0, 128) 69 return &tmp 70 }, 71 } 72 bytes := pool.Get().(*container.Slice[byte]) 73 defer pool.Put(bytes) 74 assert.Equal(t, Marshal(msg, bytes), nil, "Marshal failed") 75 msg.Reset() 76 assert.Equal(t, Unmarshal(*bytes, msg), nil, "Unmarshal failed") 77 var i int 78 iter := msg.PayloadsIterator() 79 for iter.Next() { 80 iter.Take() 81 i++ 82 } 83 if i != len(msg.payloadLayout) { 84 t.Fatal(errors.New("payload layout failed")) 85 } 86 assert.Equal(t, Marshal(msg, bytes), nil, "Marshal failed") 87 assert.Equal(t, msg.Length(), uint32(len(*bytes)), "MarshalAll bytes not equal") 88 ResetMsg(msg, true, true, true, 1024) 89 } 90 91 func TestProtocolReset(t *testing.T) { 92 msg := New() 93 msg.SetServiceName(random.GenStringOnAscii(100)) 94 msg.SetMsgId(uint64(random.FastRand())) 95 msg.MetaData.Store(CodecScheme, random.GenStringOnAscii(100)) 96 msg.MetaData.Store(PackerScheme, random.GenStringOnAscii(100)) 97 for i := 0; i < int(random.FastRandN(100)); i++ { 98 msg.MetaData.Store(random.GenStringOnAscii(10), random.GenStringOnAscii(10)) 99 } 100 var bytes []byte 101 assert.Equal(t, Marshal(msg, (*container.Slice[byte])(&bytes)), nil, "Marshal failed") 102 msg.Reset() 103 newMsg := New() 104 assert.Equal(t, msg.GetServiceName(), newMsg.GetServiceName()) 105 assert.Equal(t, msg.MetaData.Load(PackerScheme), newMsg.MetaData.Load(PackerScheme)) 106 assert.Equal(t, msg.MetaData.Load(CodecScheme), newMsg.MetaData.Load(CodecScheme)) 107 assert.Equal(t, msg.GetMsgType(), newMsg.GetMsgType()) 108 assert.Equal(t, msg.GetMsgId(), newMsg.GetMsgId()) 109 assert.Equal(t, msg.Length(), newMsg.Length()) 110 assert.Equal(t, *(*uint8)(unsafe.Pointer(msg)), *(*uint8)(unsafe.Pointer(newMsg))) 111 assert.Equal(t, msg.MetaData.Len(), newMsg.MetaData.Len()) 112 }