github.com/aergoio/aergo@v1.3.1/p2p/p2pcommon/msgid_test.go (about) 1 /* 2 * @file 3 * @copyright defined in aergo/LICENSE.txt 4 */ 5 6 package p2pcommon 7 8 import ( 9 "reflect" 10 "testing" 11 12 "github.com/gofrs/uuid" 13 "github.com/magiconair/properties/assert" 14 ) 15 16 func TestParseBytesToMsgID(t *testing.T) { 17 sampleUUID := uuid.Must(uuid.NewV4()) 18 tests := []struct { 19 name string 20 in []byte 21 expectErr bool 22 }{ 23 {"TSucc", sampleUUID[:], false}, 24 {"TWrongSize", sampleUUID[:15], true}, 25 } 26 for _, test := range tests { 27 t.Run(test.name, func(t *testing.T) { 28 got, err := ParseBytesToMsgID(test.in) 29 assert.Equal(t, test.expectErr, err != nil, "parse byte") 30 31 got2, gotPanic := checkPanic(test.in) 32 assert.Equal(t, test.expectErr, gotPanic, "got panic") 33 if !test.expectErr && got != got2 { 34 t.Errorf("ParseBytes() and MustParse() were differ: %v , %v", got, got2) 35 } 36 37 }) 38 } 39 } 40 41 func checkPanic(in []byte) (msg MsgID, gotPanic bool) { 42 defer func() { 43 if r := recover(); r != nil { 44 gotPanic = true 45 } 46 }() 47 48 msg = MustParseBytes(in) 49 return 50 } 51 52 func TestNewMsgID(t *testing.T) { 53 idMap := make(map[string]MsgID) 54 for i := 0; i < 100; i++ { 55 gotM := NewMsgID() 56 if _, exist := idMap[gotM.String()]; exist { 57 t.Errorf("NewMsgID() made duplication = %v", gotM.String()) 58 t.FailNow() 59 } 60 } 61 } 62 63 func TestMsgID_UUID(t *testing.T) { 64 tests := []struct { 65 name string 66 id MsgID 67 want uuid.UUID 68 }{ 69 {"TEmpty", EmptyID, uuid.FromBytesOrNil(nil)}, 70 } 71 for _, tt := range tests { 72 t.Run(tt.name, func(t *testing.T) { 73 if got := tt.id.UUID(); !reflect.DeepEqual(got, tt.want) { 74 t.Errorf("MsgID.UUID() = %v, want %v", got, tt.want) 75 } 76 }) 77 } 78 }