1package sctp 2 3import ( 4 "bytes" 5 "testing" 6 7 "github.com/pkg/errors" 8) 9 10func TestPacketUnmarshal(t *testing.T) { 11 pkt := &packet{} 12 13 if err := pkt.unmarshal([]byte{}); err == nil { 14 t.Errorf("Unmarshal should fail when a packet is too small to be SCTP") 15 } 16 17 headerOnly := []byte{0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x06, 0xa9, 0x00, 0xe1} 18 err := pkt.unmarshal(headerOnly) 19 switch { 20 case err != nil: 21 t.Error(errors.Wrap(err, "Unmarshal failed for SCTP packet with no chunks")) 22 case pkt.sourcePort != 5000: 23 t.Error(errors.Errorf("Unmarshal passed for SCTP packet, but got incorrect source port exp: %d act: %d", 5000, pkt.sourcePort)) 24 case pkt.destinationPort != 5000: 25 t.Error(errors.Errorf("Unmarshal passed for SCTP packet, but got incorrect destination port exp: %d act: %d", 5000, pkt.destinationPort)) 26 case pkt.verificationTag != 0: 27 t.Error(errors.Errorf("Unmarshal passed for SCTP packet, but got incorrect verification tag exp: %d act: %d", 0, pkt.verificationTag)) 28 } 29 30 rawChunk := []byte{ 31 0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x81, 0x46, 0x9d, 0xfc, 0x01, 0x00, 0x00, 0x56, 0x55, 32 0xb9, 0x64, 0xa5, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0xe8, 0x6d, 0x10, 0x30, 0xc0, 0x00, 0x00, 0x04, 0x80, 33 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24, 0x9f, 0xeb, 0xbb, 0x5c, 0x50, 34 0xc9, 0xbf, 0x75, 0x9c, 0xb1, 0x2c, 0x57, 0x4f, 0xa4, 0x5a, 0x51, 0xba, 0x60, 0x17, 0x78, 0x27, 0x94, 0x5c, 0x31, 0xe6, 35 0x5d, 0x5b, 0x09, 0x47, 0xe2, 0x22, 0x06, 0x80, 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00, 36 } 37 38 if err := pkt.unmarshal(rawChunk); err != nil { 39 t.Error(errors.Wrap(err, "Unmarshal failed, has chunk")) 40 } 41} 42 43func TestPacketMarshal(t *testing.T) { 44 pkt := &packet{} 45 46 headerOnly := []byte{0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x06, 0xa9, 0x00, 0xe1} 47 if err := pkt.unmarshal(headerOnly); err != nil { 48 t.Error(errors.Wrap(err, "Unmarshal failed for SCTP packet with no chunks")) 49 } 50 51 headerOnlyMarshaled, err := pkt.marshal() 52 if err != nil { 53 t.Error(errors.Wrap(err, "Marshal failed for SCTP packet with no chunks")) 54 } else if !bytes.Equal(headerOnly, headerOnlyMarshaled) { 55 t.Error(errors.Errorf("Unmarshal/Marshaled header only packet did not match \nheaderOnly: % 02x \nheaderOnlyMarshaled % 02x", headerOnly, headerOnlyMarshaled)) 56 } 57} 58 59func BenchmarkPacketGenerateChecksum(b *testing.B) { 60 var data [1024]byte 61 62 for i := 0; i < b.N; i++ { 63 _ = generatePacketChecksum(data[:]) 64 } 65} 66