github.com/pion/rtp@v1.8.5/packet_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package rtp
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/hex"
     9  	"errors"
    10  	"fmt"
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  func TestBasic(t *testing.T) {
    16  	p := &Packet{}
    17  
    18  	if err := p.Unmarshal([]byte{}); err == nil {
    19  		t.Fatal("Unmarshal did not error on zero length packet")
    20  	}
    21  
    22  	rawPkt := []byte{
    23  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
    24  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x9e,
    25  	}
    26  	parsedPacket := &Packet{
    27  		Header: Header{
    28  			Padding:          false,
    29  			Marker:           true,
    30  			Extension:        true,
    31  			ExtensionProfile: 1,
    32  			Extensions: []Extension{
    33  				{0, []byte{
    34  					0xFF, 0xFF, 0xFF, 0xFF,
    35  				}},
    36  			},
    37  			Version:        2,
    38  			PayloadType:    96,
    39  			SequenceNumber: 27023,
    40  			Timestamp:      3653407706,
    41  			SSRC:           476325762,
    42  			CSRC:           []uint32{},
    43  		},
    44  		Payload:     rawPkt[20:],
    45  		PaddingSize: 0,
    46  	}
    47  
    48  	// Unmarshal to the used Packet should work as well.
    49  	for i := 0; i < 2; i++ {
    50  		t.Run(fmt.Sprintf("Run%d", i+1), func(t *testing.T) {
    51  			if err := p.Unmarshal(rawPkt); err != nil {
    52  				t.Error(err)
    53  			} else if !reflect.DeepEqual(p, parsedPacket) {
    54  				t.Errorf("TestBasic unmarshal: got %#v, want %#v", p, parsedPacket)
    55  			}
    56  
    57  			if parsedPacket.Header.MarshalSize() != 20 {
    58  				t.Errorf("wrong computed header marshal size")
    59  			} else if parsedPacket.MarshalSize() != len(rawPkt) {
    60  				t.Errorf("wrong computed marshal size")
    61  			}
    62  
    63  			raw, err := p.Marshal()
    64  			if err != nil {
    65  				t.Error(err)
    66  			} else if !reflect.DeepEqual(raw, rawPkt) {
    67  				t.Errorf("TestBasic marshal: got %#v, want %#v", raw, rawPkt)
    68  			}
    69  		})
    70  	}
    71  
    72  	// packet with padding
    73  	rawPkt = []byte{
    74  		0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
    75  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x04,
    76  	}
    77  	parsedPacket = &Packet{
    78  		Header: Header{
    79  			Padding:          true,
    80  			Marker:           true,
    81  			Extension:        true,
    82  			ExtensionProfile: 1,
    83  			Extensions: []Extension{
    84  				{0, []byte{
    85  					0xFF, 0xFF, 0xFF, 0xFF,
    86  				}},
    87  			},
    88  			Version:        2,
    89  			PayloadType:    96,
    90  			SequenceNumber: 27023,
    91  			Timestamp:      3653407706,
    92  			SSRC:           476325762,
    93  			CSRC:           []uint32{},
    94  		},
    95  		Payload:     rawPkt[20:21],
    96  		PaddingSize: 4,
    97  	}
    98  	if err := p.Unmarshal(rawPkt); err != nil {
    99  		t.Error(err)
   100  	} else if !reflect.DeepEqual(p, parsedPacket) {
   101  		t.Errorf("TestBasic padding unmarshal: got %#v, want %#v", p, parsedPacket)
   102  	}
   103  
   104  	// packet with only padding
   105  	rawPkt = []byte{
   106  		0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   107  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x05,
   108  	}
   109  	parsedPacket = &Packet{
   110  		Header: Header{
   111  			Padding:          true,
   112  			Marker:           true,
   113  			Extension:        true,
   114  			ExtensionProfile: 1,
   115  			Extensions: []Extension{
   116  				{0, []byte{
   117  					0xFF, 0xFF, 0xFF, 0xFF,
   118  				}},
   119  			},
   120  			Version:        2,
   121  			PayloadType:    96,
   122  			SequenceNumber: 27023,
   123  			Timestamp:      3653407706,
   124  			SSRC:           476325762,
   125  			CSRC:           []uint32{},
   126  		},
   127  		Payload:     []byte{},
   128  		PaddingSize: 5,
   129  	}
   130  	if err := p.Unmarshal(rawPkt); err != nil {
   131  		t.Error(err)
   132  	} else if !reflect.DeepEqual(p, parsedPacket) {
   133  		t.Errorf("TestBasic padding only unmarshal: got %#v, want %#v", p, parsedPacket)
   134  	}
   135  	if len(p.Payload) != 0 {
   136  		t.Errorf("Unmarshal of padding only packet has payload of non-zero length: %d", len(p.Payload))
   137  	}
   138  
   139  	// packet with excessive padding
   140  	rawPkt = []byte{
   141  		0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   142  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x06,
   143  	}
   144  	parsedPacket = &Packet{
   145  		Header: Header{
   146  			Padding:          true,
   147  			Marker:           true,
   148  			Extension:        true,
   149  			ExtensionProfile: 1,
   150  			Extensions: []Extension{
   151  				{0, []byte{
   152  					0xFF, 0xFF, 0xFF, 0xFF,
   153  				}},
   154  			},
   155  			Version:        2,
   156  			PayloadType:    96,
   157  			SequenceNumber: 27023,
   158  			Timestamp:      3653407706,
   159  			SSRC:           476325762,
   160  			CSRC:           []uint32{},
   161  		},
   162  		Payload:     []byte{},
   163  		PaddingSize: 0,
   164  	}
   165  	err := p.Unmarshal(rawPkt)
   166  	if err == nil {
   167  		t.Fatal("Unmarshal did not error on packet with excessive padding")
   168  	}
   169  	if !errors.Is(err, errTooSmall) {
   170  		t.Errorf("Expected error: %v, got: %v", errTooSmall, err)
   171  	}
   172  
   173  	// marshal packet with padding
   174  	rawPkt = []byte{
   175  		0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   176  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x00, 0x00, 0x00, 0x04,
   177  	}
   178  	parsedPacket = &Packet{
   179  		Header: Header{
   180  			Padding:          true,
   181  			Marker:           true,
   182  			Extension:        true,
   183  			ExtensionProfile: 1,
   184  			Extensions: []Extension{
   185  				{0, []byte{
   186  					0xFF, 0xFF, 0xFF, 0xFF,
   187  				}},
   188  			},
   189  			Version:        2,
   190  			PayloadType:    96,
   191  			SequenceNumber: 27023,
   192  			Timestamp:      3653407706,
   193  			SSRC:           476325762,
   194  			CSRC:           []uint32{},
   195  		},
   196  		Payload:     rawPkt[20:21],
   197  		PaddingSize: 4,
   198  	}
   199  	buf, err := parsedPacket.Marshal()
   200  	if err != nil {
   201  		t.Error(err)
   202  	}
   203  	if !reflect.DeepEqual(buf, rawPkt) {
   204  		t.Errorf("TestBasic padding marshal: got %#v, want %#v", buf, rawPkt)
   205  	}
   206  
   207  	// marshal packet with padding only
   208  	rawPkt = []byte{
   209  		0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   210  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x05,
   211  	}
   212  	parsedPacket = &Packet{
   213  		Header: Header{
   214  			Padding:          true,
   215  			Marker:           true,
   216  			Extension:        true,
   217  			ExtensionProfile: 1,
   218  			Extensions: []Extension{
   219  				{0, []byte{
   220  					0xFF, 0xFF, 0xFF, 0xFF,
   221  				}},
   222  			},
   223  			Version:        2,
   224  			PayloadType:    96,
   225  			SequenceNumber: 27023,
   226  			Timestamp:      3653407706,
   227  			SSRC:           476325762,
   228  			CSRC:           []uint32{},
   229  		},
   230  		Payload:     []byte{},
   231  		PaddingSize: 5,
   232  	}
   233  	buf, err = parsedPacket.Marshal()
   234  	if err != nil {
   235  		t.Error(err)
   236  	}
   237  	if !reflect.DeepEqual(buf, rawPkt) {
   238  		t.Errorf("TestBasic padding marshal: got %#v, want %#v", buf, rawPkt)
   239  	}
   240  
   241  	// marshal packet with padding only without setting Padding explicitly in Header
   242  	rawPkt = []byte{
   243  		0xb0, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   244  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x05,
   245  	}
   246  	parsedPacket = &Packet{
   247  		Header: Header{
   248  			Marker:           true,
   249  			Extension:        true,
   250  			ExtensionProfile: 1,
   251  			Extensions: []Extension{
   252  				{0, []byte{
   253  					0xFF, 0xFF, 0xFF, 0xFF,
   254  				}},
   255  			},
   256  			Version:        2,
   257  			Padding:        true,
   258  			PayloadType:    96,
   259  			SequenceNumber: 27023,
   260  			Timestamp:      3653407706,
   261  			SSRC:           476325762,
   262  			CSRC:           []uint32{},
   263  		},
   264  		Payload:     []byte{},
   265  		PaddingSize: 5,
   266  	}
   267  	buf, err = parsedPacket.Marshal()
   268  	if err != nil {
   269  		t.Error(err)
   270  	}
   271  	if !reflect.DeepEqual(buf, rawPkt) {
   272  		t.Errorf("TestBasic padding marshal: got %#v, want %#v", buf, rawPkt)
   273  	}
   274  }
   275  
   276  func TestExtension(t *testing.T) {
   277  	p := &Packet{}
   278  
   279  	missingExtensionPkt := []byte{
   280  		0x90, 0x60, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   281  		0x27, 0x82,
   282  	}
   283  	if err := p.Unmarshal(missingExtensionPkt); err == nil {
   284  		t.Fatal("Unmarshal did not error on packet with missing extension data")
   285  	}
   286  
   287  	invalidExtensionLengthPkt := []byte{
   288  		0x90, 0x60, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   289  		0x27, 0x82, 0x99, 0x99, 0x99, 0x99,
   290  	}
   291  	if err := p.Unmarshal(invalidExtensionLengthPkt); err == nil {
   292  		t.Fatal("Unmarshal did not error on packet with invalid extension length")
   293  	}
   294  
   295  	p = &Packet{
   296  		Header: Header{
   297  			Extension:        true,
   298  			ExtensionProfile: 3,
   299  			Extensions: []Extension{
   300  				{0, []byte{
   301  					0,
   302  				}},
   303  			},
   304  		},
   305  		Payload: []byte{},
   306  	}
   307  	if _, err := p.Marshal(); err == nil {
   308  		t.Fatal("Marshal did not error on packet with invalid extension length")
   309  	}
   310  }
   311  
   312  func TestRFC8285OneByteExtension(t *testing.T) {
   313  	p := &Packet{}
   314  
   315  	rawPkt := []byte{
   316  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   317  		0x27, 0x82, 0xBE, 0xDE, 0x00, 0x01, 0x50, 0xAA, 0x00, 0x00,
   318  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   319  	}
   320  	if err := p.Unmarshal(rawPkt); err != nil {
   321  		t.Fatal("Unmarshal err for valid extension")
   322  	}
   323  
   324  	p = &Packet{
   325  		Header: Header{
   326  			Marker:           true,
   327  			Extension:        true,
   328  			ExtensionProfile: 0xBEDE,
   329  			Extensions: []Extension{
   330  				{5, []byte{
   331  					0xAA,
   332  				}},
   333  			},
   334  			Version:        2,
   335  			PayloadType:    96,
   336  			SequenceNumber: 27023,
   337  			Timestamp:      3653407706,
   338  			SSRC:           476325762,
   339  			CSRC:           []uint32{},
   340  		},
   341  		Payload: rawPkt[20:],
   342  	}
   343  
   344  	dstData, _ := p.Marshal()
   345  	if !bytes.Equal(dstData, rawPkt) {
   346  		t.Errorf("Marshal failed raw \nMarshaled:\n%s\nrawPkt:\n%s", hex.Dump(dstData), hex.Dump(rawPkt))
   347  	}
   348  }
   349  
   350  func TestRFC8285OneByteTwoExtensionOfTwoBytes(t *testing.T) {
   351  	p := &Packet{}
   352  
   353  	//  0                   1                   2                   3
   354  	//  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   355  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   356  	// |       0xBE    |    0xDE       |           length=1            |
   357  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   358  	// |  ID   | L=0   |     data      |  ID   |  L=0  |   data...
   359  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   360  	rawPkt := []byte{
   361  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   362  		0x27, 0x82, 0xBE, 0xDE, 0x00, 0x01, 0x10, 0xAA, 0x20, 0xBB,
   363  		// Payload
   364  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   365  	}
   366  	if err := p.Unmarshal(rawPkt); err != nil {
   367  		t.Fatal("Unmarshal err for valid extension")
   368  	}
   369  
   370  	ext1 := p.GetExtension(1)
   371  	ext1Expect := []byte{0xAA}
   372  	if !bytes.Equal(ext1, ext1Expect) {
   373  		t.Errorf("Extension has incorrect data. Got: %+v, Expected: %+v", ext1, ext1Expect)
   374  	}
   375  
   376  	ext2 := p.GetExtension(2)
   377  	ext2Expect := []byte{0xBB}
   378  	if !bytes.Equal(ext2, ext2Expect) {
   379  		t.Errorf("Extension has incorrect data. Got: %+v, Expected: %+v", ext2, ext2Expect)
   380  	}
   381  
   382  	// Test Marshal
   383  	p = &Packet{
   384  		Header: Header{
   385  			Marker:           true,
   386  			Extension:        true,
   387  			ExtensionProfile: 0xBEDE,
   388  			Extensions: []Extension{
   389  				{1, []byte{
   390  					0xAA,
   391  				}},
   392  				{2, []byte{
   393  					0xBB,
   394  				}},
   395  			},
   396  			Version:        2,
   397  			PayloadType:    96,
   398  			SequenceNumber: 27023,
   399  			Timestamp:      3653407706,
   400  			SSRC:           476325762,
   401  			CSRC:           []uint32{},
   402  		},
   403  		Payload: rawPkt[20:],
   404  	}
   405  
   406  	dstData, _ := p.Marshal()
   407  	if !bytes.Equal(dstData, rawPkt) {
   408  		t.Errorf("Marshal failed raw \nMarshaled:\n%s\nrawPkt:\n%s", hex.Dump(dstData), hex.Dump(rawPkt))
   409  	}
   410  }
   411  
   412  func TestRFC8285OneByteMultipleExtensionsWithPadding(t *testing.T) {
   413  	p := &Packet{}
   414  
   415  	//  0                   1                   2                   3
   416  	//  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   417  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   418  	// |       0xBE    |    0xDE       |           length=3            |
   419  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   420  	// |  ID   | L=0   |     data      |  ID   |  L=1  |   data...
   421  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   422  	//       ...data   |    0 (pad)    |    0 (pad)    |  ID   | L=3   |
   423  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   424  	// |                          data                                 |
   425  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   426  	rawPkt := []byte{
   427  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   428  		0x27, 0x82, 0xBE, 0xDE, 0x00, 0x03, 0x10, 0xAA, 0x21, 0xBB,
   429  		0xBB, 0x00, 0x00, 0x33, 0xCC, 0xCC, 0xCC, 0xCC,
   430  		// Payload
   431  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   432  	}
   433  	if err := p.Unmarshal(rawPkt); err != nil {
   434  		t.Fatal("Unmarshal err for valid extension")
   435  	}
   436  
   437  	ext1 := p.GetExtension(1)
   438  	ext1Expect := []byte{0xAA}
   439  	if !bytes.Equal(ext1, ext1Expect) {
   440  		t.Errorf("Extension has incorrect data. Got: %v+, Expected: %v+", ext1, ext1Expect)
   441  	}
   442  
   443  	ext2 := p.GetExtension(2)
   444  	ext2Expect := []byte{0xBB, 0xBB}
   445  	if !bytes.Equal(ext2, ext2Expect) {
   446  		t.Errorf("Extension has incorrect data. Got: %v+, Expected: %v+", ext2, ext2Expect)
   447  	}
   448  
   449  	ext3 := p.GetExtension(3)
   450  	ext3Expect := []byte{0xCC, 0xCC, 0xCC, 0xCC}
   451  	if !bytes.Equal(ext3, ext3Expect) {
   452  		t.Errorf("Extension has incorrect data. Got: %v+, Expected: %v+", ext3, ext3Expect)
   453  	}
   454  
   455  	rawPktReMarshal := []byte{
   456  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   457  		0x27, 0x82, 0xBE, 0xDE, 0x00, 0x03, 0x10, 0xAA, 0x21, 0xBB,
   458  		0xBB, 0x33, 0xCC, 0xCC, 0xCC, 0xCC, 0x00, 0x00, // padding is moved to the end by re-marshaling
   459  		// Payload
   460  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   461  	}
   462  	dstBuf := map[string][]byte{
   463  		"CleanBuffer": make([]byte, 1000),
   464  		"DirtyBuffer": make([]byte, 1000),
   465  	}
   466  	for i := range dstBuf["DirtyBuffer"] {
   467  		dstBuf["DirtyBuffer"][i] = 0xFF
   468  	}
   469  	for name, buf := range dstBuf {
   470  		buf := buf
   471  		t.Run(name, func(t *testing.T) {
   472  			n, err := p.MarshalTo(buf)
   473  			if err != nil {
   474  				t.Fatal(err)
   475  			}
   476  			if !bytes.Equal(buf[:n], rawPktReMarshal) {
   477  				t.Errorf("Marshal failed raw \nMarshaled:\n%s\nrawPkt:\n%s", hex.Dump(buf[:n]), hex.Dump(rawPktReMarshal))
   478  			}
   479  		})
   480  	}
   481  }
   482  
   483  func TestRFC8285OneByteMultipleExtensions(t *testing.T) {
   484  	//  0                   1                   2                   3
   485  	//  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   486  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   487  	// |       0xBE    |    0xDE       |           length=3            |
   488  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   489  	// |  ID=1 | L=0   |     data      |  ID=2 |  L=1  |   data...
   490  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   491  	//       ...data   |  ID=3 | L=3   |           data...
   492  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   493  	//             ...data             |
   494  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   495  	rawPkt := []byte{
   496  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   497  		0x27, 0x82, 0xBE, 0xDE, 0x00, 0x03, 0x10, 0xAA, 0x21, 0xBB,
   498  		0xBB, 0x33, 0xCC, 0xCC, 0xCC, 0xCC, 0x00, 0x00,
   499  		// Payload
   500  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   501  	}
   502  
   503  	p := &Packet{
   504  		Header: Header{
   505  			Marker:           true,
   506  			Extension:        true,
   507  			ExtensionProfile: 0xBEDE,
   508  			Extensions: []Extension{
   509  				{1, []byte{
   510  					0xAA,
   511  				}},
   512  				{2, []byte{
   513  					0xBB, 0xBB,
   514  				}},
   515  				{3, []byte{
   516  					0xCC, 0xCC, 0xCC, 0xCC,
   517  				}},
   518  			},
   519  			Version:        2,
   520  			PayloadType:    96,
   521  			SequenceNumber: 27023,
   522  			Timestamp:      3653407706,
   523  			SSRC:           476325762,
   524  			CSRC:           []uint32{},
   525  		},
   526  		Payload: rawPkt[28:],
   527  	}
   528  
   529  	dstData, _ := p.Marshal()
   530  	if !bytes.Equal(dstData, rawPkt) {
   531  		t.Errorf("Marshal failed raw \nMarshaled:\n%s\nrawPkt:\n%s", hex.Dump(dstData), hex.Dump(rawPkt))
   532  	}
   533  }
   534  
   535  func TestRFC8285TwoByteExtension(t *testing.T) {
   536  	p := &Packet{}
   537  
   538  	rawPkt := []byte{
   539  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   540  		0x27, 0x82, 0x10, 0x00, 0x00, 0x07, 0x05, 0x18, 0xAA, 0xAA,
   541  		0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
   542  		0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
   543  		0xAA, 0xAA, 0x00, 0x00, 0x98, 0x36, 0xbe, 0x88, 0x9e,
   544  	}
   545  	if err := p.Unmarshal(rawPkt); err != nil {
   546  		t.Fatal("Unmarshal err for valid extension")
   547  	}
   548  
   549  	p = &Packet{
   550  		Header: Header{
   551  			Marker:           true,
   552  			Extension:        true,
   553  			ExtensionProfile: 0x1000,
   554  			Extensions: []Extension{
   555  				{5, []byte{
   556  					0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
   557  					0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
   558  					0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
   559  				}},
   560  			},
   561  			Version:        2,
   562  			PayloadType:    96,
   563  			SequenceNumber: 27023,
   564  			Timestamp:      3653407706,
   565  			SSRC:           476325762,
   566  			CSRC:           []uint32{},
   567  		},
   568  		Payload: rawPkt[44:],
   569  	}
   570  
   571  	dstData, _ := p.Marshal()
   572  	if !bytes.Equal(dstData, rawPkt) {
   573  		t.Errorf("Marshal failed raw \nMarshaled:\n%s\nrawPkt:\n%s", hex.Dump(dstData), hex.Dump(rawPkt))
   574  	}
   575  }
   576  
   577  func TestRFC8285TwoByteMultipleExtensionsWithPadding(t *testing.T) {
   578  	p := &Packet{}
   579  
   580  	// 0                   1                   2                   3
   581  	// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   582  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   583  	// |       0x10    |    0x00       |           length=3            |
   584  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   585  	// |      ID=1     |     L=0       |     ID=2      |     L=1       |
   586  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   587  	// |       data    |    0 (pad)    |       ID=3    |      L=4      |
   588  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   589  	// |                          data                                 |
   590  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   591  	rawPkt := []byte{
   592  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   593  		0x27, 0x82, 0x10, 0x00, 0x00, 0x03, 0x01, 0x00, 0x02, 0x01,
   594  		0xBB, 0x00, 0x03, 0x04, 0xCC, 0xCC, 0xCC, 0xCC, 0x98, 0x36,
   595  		0xbe, 0x88, 0x9e,
   596  	}
   597  	if err := p.Unmarshal(rawPkt); err != nil {
   598  		t.Fatal("Unmarshal err for valid extension")
   599  	}
   600  
   601  	ext1 := p.GetExtension(1)
   602  	ext1Expect := []byte{}
   603  	if !bytes.Equal(ext1, ext1Expect) {
   604  		t.Errorf("Extension has incorrect data. Got: %v+, Expected: %v+", ext1, ext1Expect)
   605  	}
   606  
   607  	ext2 := p.GetExtension(2)
   608  	ext2Expect := []byte{0xBB}
   609  	if !bytes.Equal(ext2, ext2Expect) {
   610  		t.Errorf("Extension has incorrect data. Got: %v+, Expected: %v+", ext2, ext2Expect)
   611  	}
   612  
   613  	ext3 := p.GetExtension(3)
   614  	ext3Expect := []byte{0xCC, 0xCC, 0xCC, 0xCC}
   615  	if !bytes.Equal(ext3, ext3Expect) {
   616  		t.Errorf("Extension has incorrect data. Got: %v+, Expected: %v+", ext3, ext3Expect)
   617  	}
   618  }
   619  
   620  func TestRFC8285TwoByteMultipleExtensionsWithLargeExtension(t *testing.T) {
   621  	// 0                   1                   2                   3
   622  	// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
   623  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   624  	// |       0x10    |    0x00       |           length=3            |
   625  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   626  	// |      ID=1     |     L=0       |     ID=2      |     L=1       |
   627  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   628  	// |       data    |       ID=3    |      L=17      |    data...
   629  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   630  	//                            ...data...
   631  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   632  	//                            ...data...
   633  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   634  	//                            ...data...
   635  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   636  	//                            ...data...                           |
   637  	// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
   638  	rawPkt := []byte{
   639  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   640  		0x27, 0x82, 0x10, 0x00, 0x00, 0x06, 0x01, 0x00, 0x02, 0x01,
   641  		0xBB, 0x03, 0x11, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
   642  		0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
   643  		// Payload
   644  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   645  	}
   646  
   647  	p := &Packet{
   648  		Header: Header{
   649  			Marker:           true,
   650  			Extension:        true,
   651  			ExtensionProfile: 0x1000,
   652  			Extensions: []Extension{
   653  				{1, []byte{}},
   654  				{2, []byte{
   655  					0xBB,
   656  				}},
   657  				{3, []byte{
   658  					0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
   659  					0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
   660  				}},
   661  			},
   662  			Version:        2,
   663  			PayloadType:    96,
   664  			SequenceNumber: 27023,
   665  			Timestamp:      3653407706,
   666  			SSRC:           476325762,
   667  			CSRC:           []uint32{},
   668  		},
   669  		Payload: rawPkt[40:],
   670  	}
   671  
   672  	dstData, _ := p.Marshal()
   673  	if !bytes.Equal(dstData, rawPkt) {
   674  		t.Errorf("Marshal failed raw \nMarshaled: %+v,\nrawPkt:    %+v", dstData, rawPkt)
   675  	}
   676  }
   677  
   678  func TestRFC8285GetExtensionReturnsNilWhenExtensionsDisabled(t *testing.T) {
   679  	payload := []byte{
   680  		// Payload
   681  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   682  	}
   683  	p := &Packet{
   684  		Header: Header{
   685  			Marker:         true,
   686  			Extension:      false,
   687  			Version:        2,
   688  			PayloadType:    96,
   689  			SequenceNumber: 27023,
   690  			Timestamp:      3653407706,
   691  			SSRC:           476325762,
   692  			CSRC:           []uint32{},
   693  		},
   694  		Payload: payload,
   695  	}
   696  
   697  	err := p.GetExtension(1)
   698  	if err != nil {
   699  		t.Error("Should return nil on GetExtension when h.Extension: false")
   700  	}
   701  }
   702  
   703  func TestRFC8285DelExtension(t *testing.T) {
   704  	payload := []byte{
   705  		// Payload
   706  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   707  	}
   708  	p := &Packet{
   709  		Header: Header{
   710  			Marker:           true,
   711  			Extension:        true,
   712  			ExtensionProfile: 0xBEDE,
   713  			Extensions: []Extension{
   714  				{1, []byte{
   715  					0xAA,
   716  				}},
   717  			},
   718  			Version:        2,
   719  			PayloadType:    96,
   720  			SequenceNumber: 27023,
   721  			Timestamp:      3653407706,
   722  			SSRC:           476325762,
   723  			CSRC:           []uint32{},
   724  		},
   725  		Payload: payload,
   726  	}
   727  
   728  	ext := p.GetExtension(1)
   729  	if ext == nil {
   730  		t.Error("Extension should exist")
   731  	}
   732  
   733  	err := p.DelExtension(1)
   734  	if err != nil {
   735  		t.Error("Should successfully delete extension")
   736  	}
   737  
   738  	ext = p.GetExtension(1)
   739  	if ext != nil {
   740  		t.Error("Extension should not exist")
   741  	}
   742  
   743  	err = p.DelExtension(1)
   744  	if err == nil {
   745  		t.Error("Should return error when deleting extension that doesnt exist")
   746  	}
   747  }
   748  
   749  func TestRFC8285GetExtensionIDs(t *testing.T) {
   750  	payload := []byte{
   751  		// Payload
   752  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   753  	}
   754  	p := &Packet{
   755  		Header: Header{
   756  			Marker:           true,
   757  			Extension:        true,
   758  			ExtensionProfile: 0xBEDE,
   759  			Extensions: []Extension{
   760  				{1, []byte{
   761  					0xAA,
   762  				}},
   763  				{2, []byte{
   764  					0xBB,
   765  				}},
   766  			},
   767  			Version:        2,
   768  			PayloadType:    96,
   769  			SequenceNumber: 27023,
   770  			Timestamp:      3653407706,
   771  			SSRC:           476325762,
   772  			CSRC:           []uint32{},
   773  		},
   774  		Payload: payload,
   775  	}
   776  
   777  	ids := p.GetExtensionIDs()
   778  	if ids == nil {
   779  		t.Error("Extension should exist")
   780  	}
   781  	if len(ids) != len(p.Extensions) {
   782  		t.Errorf("The number of IDs should be equal to the number of extensions,want=%d,have=%d", len(p.Extensions), len(ids))
   783  	}
   784  
   785  	for _, id := range ids {
   786  		ext := p.GetExtension(id)
   787  		if ext == nil {
   788  			t.Error("Extension should exist")
   789  		}
   790  	}
   791  }
   792  
   793  func TestRFC8285GetExtensionIDsReturnsErrorWhenExtensionsDisabled(t *testing.T) {
   794  	payload := []byte{
   795  		// Payload
   796  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   797  	}
   798  	p := &Packet{
   799  		Header: Header{
   800  			Marker:         true,
   801  			Extension:      false,
   802  			Version:        2,
   803  			PayloadType:    96,
   804  			SequenceNumber: 27023,
   805  			Timestamp:      3653407706,
   806  			SSRC:           476325762,
   807  			CSRC:           []uint32{},
   808  		},
   809  		Payload: payload,
   810  	}
   811  
   812  	ids := p.GetExtensionIDs()
   813  	if ids != nil {
   814  		t.Error("Should return nil on GetExtensionIDs when h.Extensions is nil")
   815  	}
   816  }
   817  
   818  func TestRFC8285DelExtensionReturnsErrorWhenExtensionsDisabled(t *testing.T) {
   819  	payload := []byte{
   820  		// Payload
   821  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   822  	}
   823  	p := &Packet{
   824  		Header: Header{
   825  			Marker:         true,
   826  			Extension:      false,
   827  			Version:        2,
   828  			PayloadType:    96,
   829  			SequenceNumber: 27023,
   830  			Timestamp:      3653407706,
   831  			SSRC:           476325762,
   832  			CSRC:           []uint32{},
   833  		},
   834  		Payload: payload,
   835  	}
   836  
   837  	err := p.DelExtension(1)
   838  	if err == nil {
   839  		t.Error("Should return error on DelExtension when h.Extension: false")
   840  	}
   841  }
   842  
   843  func TestRFC8285OneByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T) {
   844  	payload := []byte{
   845  		// Payload
   846  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   847  	}
   848  	p := &Packet{
   849  		Header: Header{
   850  			Marker:         true,
   851  			Extension:      false,
   852  			Version:        2,
   853  			PayloadType:    96,
   854  			SequenceNumber: 27023,
   855  			Timestamp:      3653407706,
   856  			SSRC:           476325762,
   857  			CSRC:           []uint32{},
   858  		},
   859  		Payload: payload,
   860  	}
   861  
   862  	extension := []byte{0xAA, 0xAA}
   863  	err := p.SetExtension(1, extension)
   864  	if err != nil {
   865  		t.Error("Error setting extension")
   866  	}
   867  
   868  	if p.Extension != true {
   869  		t.Error("Extension should be set to true")
   870  	}
   871  
   872  	if p.ExtensionProfile != 0xBEDE {
   873  		t.Error("Extension profile should be set to 0xBEDE")
   874  	}
   875  
   876  	if len(p.Extensions) != 1 {
   877  		t.Error("Extensions should be set to 1")
   878  	}
   879  
   880  	if !bytes.Equal(p.GetExtension(1), extension) {
   881  		t.Error("Extension value is not set")
   882  	}
   883  }
   884  
   885  func TestRFC8285OneByteSetExtensionShouldSetCorrectExtensionProfileFor16ByteExtension(t *testing.T) {
   886  	payload := []byte{
   887  		// Payload
   888  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   889  	}
   890  	p := &Packet{
   891  		Header: Header{
   892  			Marker:         true,
   893  			Extension:      false,
   894  			Version:        2,
   895  			PayloadType:    96,
   896  			SequenceNumber: 27023,
   897  			Timestamp:      3653407706,
   898  			SSRC:           476325762,
   899  			CSRC:           []uint32{},
   900  		},
   901  		Payload: payload,
   902  	}
   903  
   904  	extension := []byte{
   905  		0xAA, 0xAA, 0xAA, 0xAA,
   906  		0xAA, 0xAA, 0xAA, 0xAA,
   907  		0xAA, 0xAA, 0xAA, 0xAA,
   908  		0xAA, 0xAA, 0xAA, 0xAA,
   909  	}
   910  	err := p.SetExtension(1, extension)
   911  	if err != nil {
   912  		t.Error("Error setting extension")
   913  	}
   914  
   915  	if p.ExtensionProfile != 0xBEDE {
   916  		t.Error("Extension profile should be set to 0xBEDE")
   917  	}
   918  }
   919  
   920  func TestRFC8285OneByteSetExtensionShouldUpdateExistingExension(t *testing.T) {
   921  	payload := []byte{
   922  		// Payload
   923  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   924  	}
   925  	p := &Packet{
   926  		Header: Header{
   927  			Marker:           true,
   928  			Extension:        true,
   929  			ExtensionProfile: 0xBEDE,
   930  			Extensions: []Extension{
   931  				{1, []byte{
   932  					0xAA,
   933  				}},
   934  			},
   935  			Version:        2,
   936  			PayloadType:    96,
   937  			SequenceNumber: 27023,
   938  			Timestamp:      3653407706,
   939  			SSRC:           476325762,
   940  			CSRC:           []uint32{},
   941  		},
   942  		Payload: payload,
   943  	}
   944  
   945  	if !bytes.Equal(p.GetExtension(1), []byte{0xAA}) {
   946  		t.Error("Extension value not initialize properly")
   947  	}
   948  
   949  	extension := []byte{0xBB}
   950  	err := p.SetExtension(1, extension)
   951  	if err != nil {
   952  		t.Error("Error setting extension")
   953  	}
   954  
   955  	if !bytes.Equal(p.GetExtension(1), extension) {
   956  		t.Error("Extension value was not set")
   957  	}
   958  }
   959  
   960  func TestRFC8285OneByteSetExtensionShouldErrorWhenInvalidIDProvided(t *testing.T) {
   961  	payload := []byte{
   962  		// Payload
   963  		0x98, 0x36, 0xbe, 0x88, 0x9e,
   964  	}
   965  	p := &Packet{
   966  		Header: Header{
   967  			Marker:           true,
   968  			Extension:        true,
   969  			ExtensionProfile: 0xBEDE,
   970  			Extensions: []Extension{
   971  				{1, []byte{
   972  					0xAA,
   973  				}},
   974  			},
   975  			Version:        2,
   976  			PayloadType:    96,
   977  			SequenceNumber: 27023,
   978  			Timestamp:      3653407706,
   979  			SSRC:           476325762,
   980  			CSRC:           []uint32{},
   981  		},
   982  		Payload: payload,
   983  	}
   984  
   985  	if p.SetExtension(0, []byte{0xBB}) == nil {
   986  		t.Error("SetExtension did not error on invalid id")
   987  	}
   988  
   989  	if p.SetExtension(15, []byte{0xBB}) == nil {
   990  		t.Error("SetExtension did not error on invalid id")
   991  	}
   992  }
   993  
   994  func TestRFC8285OneByteExtensionTermianteProcessingWhenReservedIDEncountered(t *testing.T) {
   995  	p := &Packet{}
   996  
   997  	reservedIDPkt := []byte{
   998  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
   999  		0x27, 0x82, 0xBE, 0xDE, 0x00, 0x01, 0xF0, 0xAA, 0x98, 0x36, 0xbe, 0x88, 0x9e,
  1000  	}
  1001  	if err := p.Unmarshal(reservedIDPkt); err != nil {
  1002  		t.Error("Unmarshal error on packet with reserved extension id")
  1003  	}
  1004  
  1005  	if len(p.Extensions) != 0 {
  1006  		t.Error("Extensions should be empty for invalid id")
  1007  	}
  1008  
  1009  	payload := reservedIDPkt[17:]
  1010  	if !bytes.Equal(p.Payload, payload) {
  1011  		t.Errorf("p.Payload must be same as payload.\n  p.Payload: %+v,\n payload: %+v",
  1012  			p.Payload, payload,
  1013  		)
  1014  	}
  1015  }
  1016  
  1017  func TestRFC8285OneByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T) {
  1018  	payload := []byte{
  1019  		// Payload
  1020  		0x98, 0x36, 0xbe, 0x88, 0x9e,
  1021  	}
  1022  	p := &Packet{
  1023  		Header: Header{
  1024  			Marker:           true,
  1025  			Extension:        true,
  1026  			ExtensionProfile: 0xBEDE,
  1027  			Extensions: []Extension{
  1028  				{1, []byte{
  1029  					0xAA,
  1030  				}},
  1031  			},
  1032  			Version:        2,
  1033  			PayloadType:    96,
  1034  			SequenceNumber: 27023,
  1035  			Timestamp:      3653407706,
  1036  			SSRC:           476325762,
  1037  			CSRC:           []uint32{},
  1038  		},
  1039  		Payload: payload,
  1040  	}
  1041  
  1042  	if p.SetExtension(1, []byte{
  1043  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1044  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1045  	}) == nil {
  1046  		t.Error("SetExtension did not error on too large payload")
  1047  	}
  1048  }
  1049  
  1050  func TestRFC8285TwoByteSetExtensionShouldEnableExensionsWhenAdding(t *testing.T) {
  1051  	payload := []byte{
  1052  		// Payload
  1053  		0x98, 0x36, 0xbe, 0x88, 0x9e,
  1054  	}
  1055  	p := &Packet{
  1056  		Header: Header{
  1057  			Marker:         true,
  1058  			Extension:      false,
  1059  			Version:        2,
  1060  			PayloadType:    96,
  1061  			SequenceNumber: 27023,
  1062  			Timestamp:      3653407706,
  1063  			SSRC:           476325762,
  1064  			CSRC:           []uint32{},
  1065  		},
  1066  		Payload: payload,
  1067  	}
  1068  
  1069  	extension := []byte{
  1070  		0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  1071  		0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
  1072  	}
  1073  	err := p.SetExtension(1, extension)
  1074  	if err != nil {
  1075  		t.Error("Error setting extension")
  1076  	}
  1077  
  1078  	if p.Extension != true {
  1079  		t.Error("Extension should be set to true")
  1080  	}
  1081  
  1082  	if p.ExtensionProfile != 0x1000 {
  1083  		t.Error("Extension profile should be set to 0xBEDE")
  1084  	}
  1085  
  1086  	if len(p.Extensions) != 1 {
  1087  		t.Error("Extensions should be set to 1")
  1088  	}
  1089  
  1090  	if !bytes.Equal(p.GetExtension(1), extension) {
  1091  		t.Error("Extension value is not set")
  1092  	}
  1093  }
  1094  
  1095  func TestRFC8285TwoByteSetExtensionShouldUpdateExistingExension(t *testing.T) {
  1096  	payload := []byte{
  1097  		// Payload
  1098  		0x98, 0x36, 0xbe, 0x88, 0x9e,
  1099  	}
  1100  	p := &Packet{
  1101  		Header: Header{
  1102  			Marker:           true,
  1103  			Extension:        true,
  1104  			ExtensionProfile: 0x1000,
  1105  			Extensions: []Extension{
  1106  				{1, []byte{
  1107  					0xAA,
  1108  				}},
  1109  			},
  1110  			Version:        2,
  1111  			PayloadType:    96,
  1112  			SequenceNumber: 27023,
  1113  			Timestamp:      3653407706,
  1114  			SSRC:           476325762,
  1115  			CSRC:           []uint32{},
  1116  		},
  1117  		Payload: payload,
  1118  	}
  1119  
  1120  	if !bytes.Equal(p.GetExtension(1), []byte{0xAA}) {
  1121  		t.Error("Extension value not initialize properly")
  1122  	}
  1123  
  1124  	extension := []byte{
  1125  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1126  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1127  	}
  1128  	err := p.SetExtension(1, extension)
  1129  	if err != nil {
  1130  		t.Error("Error setting extension")
  1131  	}
  1132  
  1133  	if !bytes.Equal(p.GetExtension(1), extension) {
  1134  		t.Error("Extension value was not set")
  1135  	}
  1136  }
  1137  
  1138  func TestRFC8285TwoByteSetExtensionShouldErrorWhenPayloadTooLarge(t *testing.T) {
  1139  	payload := []byte{
  1140  		// Payload
  1141  		0x98, 0x36, 0xbe, 0x88, 0x9e,
  1142  	}
  1143  	p := &Packet{
  1144  		Header: Header{
  1145  			Marker:           true,
  1146  			Extension:        true,
  1147  			ExtensionProfile: 0xBEDE,
  1148  			Extensions: []Extension{
  1149  				{1, []byte{
  1150  					0xAA,
  1151  				}},
  1152  			},
  1153  			Version:        2,
  1154  			PayloadType:    96,
  1155  			SequenceNumber: 27023,
  1156  			Timestamp:      3653407706,
  1157  			SSRC:           476325762,
  1158  			CSRC:           []uint32{},
  1159  		},
  1160  		Payload: payload,
  1161  	}
  1162  
  1163  	if p.SetExtension(1, []byte{
  1164  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1165  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1166  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1167  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1168  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1169  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1170  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1171  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1172  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1173  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1174  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1175  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1176  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1177  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1178  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1179  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1180  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1181  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1182  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1183  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1184  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1185  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1186  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1187  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1188  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1189  		0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
  1190  	}) == nil {
  1191  		t.Error("SetExtension did not error on too large payload")
  1192  	}
  1193  }
  1194  
  1195  func TestRFC8285Padding(t *testing.T) {
  1196  	header := &Header{}
  1197  
  1198  	for _, payload := range [][]byte{
  1199  		{
  1200  			0b00010000,                      // header.Extension = true
  1201  			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // SequenceNumber, Timestamp, SSRC
  1202  			0xBE, 0xDE, // header.ExtensionProfile = extensionProfileOneByte
  1203  			0, 1, // extensionLength
  1204  			0, 0, 0, // padding
  1205  			1, // extid
  1206  		},
  1207  		{
  1208  			0b00010000,                      // header.Extension = true
  1209  			0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // SequenceNumber, Timestamp, SSRC
  1210  			0x10, 0x00, // header.ExtensionProfile = extensionProfileOneByte
  1211  			0, 1, // extensionLength
  1212  			0, 0, 0, // padding
  1213  			1, // extid
  1214  		},
  1215  	} {
  1216  		_, err := header.Unmarshal(payload)
  1217  		if !errors.Is(err, errHeaderSizeInsufficientForExtension) {
  1218  			t.Fatal("Expected errHeaderSizeInsufficientForExtension")
  1219  		}
  1220  	}
  1221  }
  1222  
  1223  func TestRFC3550SetExtensionShouldErrorWhenNonZero(t *testing.T) {
  1224  	payload := []byte{
  1225  		// Payload
  1226  		0x98, 0x36, 0xbe, 0x88, 0x9e,
  1227  	}
  1228  	p := &Packet{
  1229  		Header: Header{
  1230  			Marker:           true,
  1231  			Extension:        true,
  1232  			ExtensionProfile: 0x1111,
  1233  			Extensions: []Extension{
  1234  				{0, []byte{
  1235  					0xAA,
  1236  				}},
  1237  			},
  1238  			Version:        2,
  1239  			PayloadType:    96,
  1240  			SequenceNumber: 27023,
  1241  			Timestamp:      3653407706,
  1242  			SSRC:           476325762,
  1243  			CSRC:           []uint32{},
  1244  		},
  1245  		Payload: payload,
  1246  	}
  1247  
  1248  	expect := []byte{0xBB}
  1249  	if p.SetExtension(0, expect) != nil {
  1250  		t.Error("SetExtension should not error on valid id")
  1251  	}
  1252  
  1253  	actual := p.GetExtension(0)
  1254  	if !bytes.Equal(actual, expect) {
  1255  		t.Error("p.GetExtension returned incorrect value.")
  1256  	}
  1257  }
  1258  
  1259  func TestRFC3550SetExtensionShouldRaiseErrorWhenSettingNonzeroID(t *testing.T) {
  1260  	payload := []byte{
  1261  		// Payload
  1262  		0x98, 0x36, 0xbe, 0x88, 0x9e,
  1263  	}
  1264  	p := &Packet{
  1265  		Header: Header{
  1266  			Marker:           true,
  1267  			Extension:        true,
  1268  			ExtensionProfile: 0x1111,
  1269  			Version:          2,
  1270  			PayloadType:      96,
  1271  			SequenceNumber:   27023,
  1272  			Timestamp:        3653407706,
  1273  			SSRC:             476325762,
  1274  			CSRC:             []uint32{},
  1275  		},
  1276  		Payload: payload,
  1277  	}
  1278  
  1279  	if p.SetExtension(1, []byte{0xBB}) == nil {
  1280  		t.Error("SetExtension did not error on invalid id")
  1281  	}
  1282  }
  1283  
  1284  func TestUnmarshal_ErrorHandling(t *testing.T) {
  1285  	cases := map[string]struct {
  1286  		input []byte
  1287  		err   error
  1288  	}{
  1289  		"ShortHeader": {
  1290  			input: []byte{
  1291  				0x80, 0xe0, 0x69, 0x8f,
  1292  				0xd9, 0xc2, 0x93, 0xda, // timestamp
  1293  				0x1c, 0x64, 0x27, // SSRC (one byte missing)
  1294  			},
  1295  			err: errHeaderSizeInsufficient,
  1296  		},
  1297  		"MissingCSRC": {
  1298  			input: []byte{
  1299  				0x81, 0xe0, 0x69, 0x8f,
  1300  				0xd9, 0xc2, 0x93, 0xda, // timestamp
  1301  				0x1c, 0x64, 0x27, 0x82, // SSRC
  1302  			},
  1303  			err: errHeaderSizeInsufficient,
  1304  		},
  1305  		"MissingExtension": {
  1306  			input: []byte{
  1307  				0x90, 0xe0, 0x69, 0x8f,
  1308  				0xd9, 0xc2, 0x93, 0xda, // timestamp
  1309  				0x1c, 0x64, 0x27, 0x82, // SSRC
  1310  			},
  1311  			err: errHeaderSizeInsufficientForExtension,
  1312  		},
  1313  		"MissingExtensionData": {
  1314  			input: []byte{
  1315  				0x90, 0xe0, 0x69, 0x8f,
  1316  				0xd9, 0xc2, 0x93, 0xda, // timestamp
  1317  				0x1c, 0x64, 0x27, 0x82, // SSRC
  1318  				0xBE, 0xDE, 0x00, 0x03, // specified to have 3 extensions, but actually not
  1319  			},
  1320  			err: errHeaderSizeInsufficientForExtension,
  1321  		},
  1322  		"MissingExtensionDataPayload": {
  1323  			input: []byte{
  1324  				0x90, 0xe0, 0x69, 0x8f,
  1325  				0xd9, 0xc2, 0x93, 0xda, // timestamp
  1326  				0x1c, 0x64, 0x27, 0x82, // SSRC
  1327  				0xBE, 0xDE, 0x00, 0x01, // have 1 extension
  1328  				0x12, 0x00, // length of the payload is expected to be 3, but actually have only 1
  1329  			},
  1330  			err: errHeaderSizeInsufficientForExtension,
  1331  		},
  1332  	}
  1333  
  1334  	for name, testCase := range cases {
  1335  		testCase := testCase
  1336  		t.Run(name, func(t *testing.T) {
  1337  			h := &Header{}
  1338  			_, err := h.Unmarshal(testCase.input)
  1339  			if !errors.Is(err, testCase.err) {
  1340  				t.Errorf("Expected error: %v, got: %v", testCase.err, err)
  1341  			}
  1342  		})
  1343  	}
  1344  }
  1345  
  1346  func TestRoundtrip(t *testing.T) {
  1347  	rawPkt := []byte{
  1348  		0x00, 0x10, 0x23, 0x45, 0x12, 0x34, 0x45, 0x67, 0xCC, 0xDD, 0xEE, 0xFF,
  1349  		0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  1350  	}
  1351  	payload := rawPkt[12:]
  1352  
  1353  	p := &Packet{}
  1354  	if err := p.Unmarshal(rawPkt); err != nil {
  1355  		t.Fatal(err)
  1356  	}
  1357  	if !bytes.Equal(payload, p.Payload) {
  1358  		t.Errorf("p.Payload must be same as payload.\n  payload: %+v,\np.Payload: %+v",
  1359  			payload, p.Payload,
  1360  		)
  1361  	}
  1362  
  1363  	buf, err := p.Marshal()
  1364  	if err != nil {
  1365  		t.Fatal(err)
  1366  	}
  1367  	if !bytes.Equal(rawPkt, buf) {
  1368  		t.Errorf("buf must be same as rawPkt.\n   buf: %+v,\nrawPkt: %+v", buf, rawPkt)
  1369  	}
  1370  	if !bytes.Equal(payload, p.Payload) {
  1371  		t.Errorf("p.Payload must be same as payload.\n  payload: %+v,\np.Payload: %+v",
  1372  			payload, p.Payload,
  1373  		)
  1374  	}
  1375  }
  1376  
  1377  func TestCloneHeader(t *testing.T) {
  1378  	h := Header{
  1379  		Marker:           true,
  1380  		Extension:        true,
  1381  		ExtensionProfile: 1,
  1382  		Extensions: []Extension{
  1383  			{0, []byte{
  1384  				0xFF, 0xFF, 0xFF, 0xFF,
  1385  			}},
  1386  		},
  1387  		Version:        2,
  1388  		PayloadType:    96,
  1389  		SequenceNumber: 27023,
  1390  		Timestamp:      3653407706,
  1391  		SSRC:           476325762,
  1392  		CSRC:           []uint32{},
  1393  	}
  1394  	clone := h.Clone()
  1395  	if !reflect.DeepEqual(h, clone) {
  1396  		t.Errorf("Cloned clone does not match the original")
  1397  	}
  1398  
  1399  	h.CSRC = append(h.CSRC, 1)
  1400  	if len(clone.CSRC) == len(h.CSRC) {
  1401  		t.Errorf("Expected CSRC to be unchanged")
  1402  	}
  1403  	h.Extensions[0].payload[0] = 0x1F
  1404  	if clone.Extensions[0].payload[0] == 0x1F {
  1405  		t.Errorf("Expected Extensions to be unchanged")
  1406  	}
  1407  }
  1408  
  1409  func TestClonePacket(t *testing.T) {
  1410  	rawPkt := []byte{
  1411  		0x90, 0xe0, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
  1412  		0x27, 0x82, 0xBE, 0xDE, 0x00, 0x01, 0x50, 0xAA, 0x00, 0x00,
  1413  		0x98, 0x36, 0xbe, 0x88, 0x9e,
  1414  	}
  1415  	p := &Packet{
  1416  		Payload: rawPkt[20:],
  1417  	}
  1418  
  1419  	clone := p.Clone()
  1420  	if !reflect.DeepEqual(p, clone) {
  1421  		t.Errorf("Cloned Packet does not match the original")
  1422  	}
  1423  
  1424  	p.Payload[0] = 0x1F
  1425  	if clone.Payload[0] == 0x1F {
  1426  		t.Errorf("Expected Payload to be unchanged")
  1427  	}
  1428  }
  1429  
  1430  func BenchmarkMarshal(b *testing.B) {
  1431  	rawPkt := []byte{
  1432  		0x90, 0x60, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
  1433  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x9e,
  1434  	}
  1435  
  1436  	p := &Packet{}
  1437  	err := p.Unmarshal(rawPkt)
  1438  	if err != nil {
  1439  		b.Fatal(err)
  1440  	}
  1441  
  1442  	b.ResetTimer()
  1443  
  1444  	for i := 0; i < b.N; i++ {
  1445  		_, err = p.Marshal()
  1446  		if err != nil {
  1447  			b.Fatal(err)
  1448  		}
  1449  	}
  1450  }
  1451  
  1452  func BenchmarkMarshalTo(b *testing.B) {
  1453  	rawPkt := []byte{
  1454  		0x90, 0x60, 0x69, 0x8f, 0xd9, 0xc2, 0x93, 0xda, 0x1c, 0x64,
  1455  		0x27, 0x82, 0x00, 0x01, 0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0x98, 0x36, 0xbe, 0x88, 0x9e,
  1456  	}
  1457  
  1458  	p := &Packet{}
  1459  
  1460  	err := p.Unmarshal(rawPkt)
  1461  	if err != nil {
  1462  		b.Fatal(err)
  1463  	}
  1464  
  1465  	buf := [100]byte{}
  1466  
  1467  	b.ResetTimer()
  1468  
  1469  	for i := 0; i < b.N; i++ {
  1470  		_, err = p.MarshalTo(buf[:])
  1471  		if err != nil {
  1472  			b.Fatal(err)
  1473  		}
  1474  	}
  1475  }
  1476  
  1477  func BenchmarkUnmarshal(b *testing.B) {
  1478  	pkt := Packet{
  1479  		Header: Header{
  1480  			Extension:        true,
  1481  			CSRC:             []uint32{1, 2},
  1482  			ExtensionProfile: extensionProfileTwoByte,
  1483  			Extensions: []Extension{
  1484  				{id: 1, payload: []byte{3, 4}},
  1485  				{id: 2, payload: []byte{5, 6}},
  1486  			},
  1487  		},
  1488  		Payload: []byte{
  1489  			0x07, 0x08, 0x09, 0x0a,
  1490  		},
  1491  	}
  1492  	rawPkt, errMarshal := pkt.Marshal()
  1493  	if errMarshal != nil {
  1494  		b.Fatal(errMarshal)
  1495  	}
  1496  
  1497  	b.Run("SharedStruct", func(b *testing.B) {
  1498  		p := &Packet{}
  1499  
  1500  		b.ReportAllocs()
  1501  		b.ResetTimer()
  1502  		for i := 0; i < b.N; i++ {
  1503  			if err := p.Unmarshal(rawPkt); err != nil {
  1504  				b.Fatal(err)
  1505  			}
  1506  		}
  1507  	})
  1508  	b.Run("NewStruct", func(b *testing.B) {
  1509  		b.ReportAllocs()
  1510  		b.ResetTimer()
  1511  		for i := 0; i < b.N; i++ {
  1512  			p := &Packet{}
  1513  			if err := p.Unmarshal(rawPkt); err != nil {
  1514  				b.Fatal(err)
  1515  			}
  1516  		}
  1517  	})
  1518  }