github.com/pion/dtls/v2@v2.2.12/pkg/protocol/recordlayer/recordlayer_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package recordlayer
     5  
     6  import (
     7  	"errors"
     8  	"reflect"
     9  	"testing"
    10  
    11  	"github.com/pion/dtls/v2/pkg/protocol"
    12  )
    13  
    14  func TestUDPDecode(t *testing.T) {
    15  	for _, test := range []struct {
    16  		Name      string
    17  		Data      []byte
    18  		Want      [][]byte
    19  		WantError error
    20  	}{
    21  		{
    22  			Name: "Change Cipher Spec, single packet",
    23  			Data: []byte{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x01},
    24  			Want: [][]byte{
    25  				{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x01},
    26  			},
    27  		},
    28  		{
    29  			Name: "Change Cipher Spec, multi packet",
    30  			Data: []byte{
    31  				0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x01,
    32  				0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x01,
    33  			},
    34  			Want: [][]byte{
    35  				{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x01},
    36  				{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13, 0x00, 0x01, 0x01},
    37  			},
    38  		},
    39  		{
    40  			Name:      "Invalid packet length",
    41  			Data:      []byte{0x14, 0xfe},
    42  			WantError: ErrInvalidPacketLength,
    43  		},
    44  		{
    45  			Name:      "Packet declared invalid length",
    46  			Data:      []byte{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0xFF, 0x01},
    47  			WantError: ErrInvalidPacketLength,
    48  		},
    49  	} {
    50  		dtlsPkts, err := UnpackDatagram(test.Data)
    51  		if !errors.Is(err, test.WantError) {
    52  			t.Errorf("Unexpected Error %q: exp: %v got: %v", test.Name, test.WantError, err)
    53  		} else if !reflect.DeepEqual(test.Want, dtlsPkts) {
    54  			t.Errorf("%q UDP decode: got %q, want %q", test.Name, dtlsPkts, test.Want)
    55  		}
    56  	}
    57  }
    58  
    59  func TestRecordLayerRoundTrip(t *testing.T) {
    60  	for _, test := range []struct {
    61  		Name               string
    62  		Data               []byte
    63  		Want               *RecordLayer
    64  		WantMarshalError   error
    65  		WantUnmarshalError error
    66  	}{
    67  		{
    68  			Name: "Change Cipher Spec, single packet",
    69  			Data: []byte{0x14, 0xfe, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x01, 0x01},
    70  			Want: &RecordLayer{
    71  				Header: Header{
    72  					ContentType:    protocol.ContentTypeChangeCipherSpec,
    73  					Version:        protocol.Version{Major: 0xfe, Minor: 0xff},
    74  					Epoch:          0,
    75  					SequenceNumber: 18,
    76  				},
    77  				Content: &protocol.ChangeCipherSpec{},
    78  			},
    79  		},
    80  	} {
    81  		r := &RecordLayer{}
    82  		if err := r.Unmarshal(test.Data); !errors.Is(err, test.WantUnmarshalError) {
    83  			t.Errorf("Unexpected Error %q: exp: %v got: %v", test.Name, test.WantUnmarshalError, err)
    84  		} else if !reflect.DeepEqual(test.Want, r) {
    85  			t.Errorf("%q recordLayer.unmarshal: got %q, want %q", test.Name, r, test.Want)
    86  		}
    87  
    88  		data, marshalErr := r.Marshal()
    89  		if !errors.Is(marshalErr, test.WantMarshalError) {
    90  			t.Errorf("Unexpected Error %q: exp: %v got: %v", test.Name, test.WantMarshalError, marshalErr)
    91  		} else if !reflect.DeepEqual(test.Data, data) {
    92  			t.Errorf("%q recordLayer.marshal: got % 02x, want % 02x", test.Name, data, test.Data)
    93  		}
    94  	}
    95  }