github.com/pion/dtls/v2@v2.2.12/pkg/protocol/handshake/message_client_hello_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package handshake
     5  
     6  import (
     7  	"bytes"
     8  	"reflect"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/pion/dtls/v2/pkg/crypto/elliptic"
    13  	"github.com/pion/dtls/v2/pkg/protocol"
    14  	"github.com/pion/dtls/v2/pkg/protocol/extension"
    15  )
    16  
    17  func TestHandshakeMessageClientHello(t *testing.T) {
    18  	rawClientHello := []byte{
    19  		0xfe, 0xfd, 0xb6, 0x2f, 0xce, 0x5c, 0x42, 0x54, 0xff, 0x86, 0xe1, 0x24, 0x41, 0x91, 0x42,
    20  		0x62, 0x15, 0xad, 0x16, 0xc9, 0x15, 0x8d, 0x95, 0x71, 0x8a, 0xbb, 0x22, 0xd7, 0x47, 0xec,
    21  		0xd8, 0x3d, 0xdc, 0x4b, 0x00, 0x14, 0xe6, 0x14, 0x3a, 0x1b, 0x04, 0xea, 0x9e, 0x7a, 0x14,
    22  		0xd6, 0x6c, 0x57, 0xd0, 0x0e, 0x32, 0x85, 0x76, 0x18, 0xde, 0xd8, 0x00, 0x04, 0xc0, 0x2b,
    23  		0xc0, 0x0a, 0x01, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x04, 0x00, 0x02, 0x00, 0x1d,
    24  	}
    25  	parsedClientHello := &MessageClientHello{
    26  		Version: protocol.Version{Major: 0xFE, Minor: 0xFD},
    27  		Random: Random{
    28  			GMTUnixTime: time.Unix(3056586332, 0),
    29  			RandomBytes: [28]byte{0x42, 0x54, 0xff, 0x86, 0xe1, 0x24, 0x41, 0x91, 0x42, 0x62, 0x15, 0xad, 0x16, 0xc9, 0x15, 0x8d, 0x95, 0x71, 0x8a, 0xbb, 0x22, 0xd7, 0x47, 0xec, 0xd8, 0x3d, 0xdc, 0x4b},
    30  		},
    31  		SessionID: []byte{},
    32  		Cookie:    []byte{0xe6, 0x14, 0x3a, 0x1b, 0x04, 0xea, 0x9e, 0x7a, 0x14, 0xd6, 0x6c, 0x57, 0xd0, 0x0e, 0x32, 0x85, 0x76, 0x18, 0xde, 0xd8},
    33  		CipherSuiteIDs: []uint16{
    34  			0xc02b,
    35  			0xc00a,
    36  		},
    37  		CompressionMethods: []*protocol.CompressionMethod{
    38  			{},
    39  		},
    40  		Extensions: []extension.Extension{
    41  			&extension.SupportedEllipticCurves{EllipticCurves: []elliptic.Curve{elliptic.X25519}},
    42  		},
    43  	}
    44  
    45  	c := &MessageClientHello{}
    46  	if err := c.Unmarshal(rawClientHello); err != nil {
    47  		t.Error(err)
    48  	} else if !reflect.DeepEqual(c, parsedClientHello) {
    49  		t.Errorf("handshakeMessageClientHello unmarshal: got %#v, want %#v", c, parsedClientHello)
    50  	}
    51  
    52  	raw, err := c.Marshal()
    53  	if err != nil {
    54  		t.Error(err)
    55  	} else if !reflect.DeepEqual(raw, rawClientHello) {
    56  		t.Errorf("handshakeMessageClientHello marshal: got %#v, want %#v", raw, rawClientHello)
    57  	}
    58  }
    59  
    60  func TestHandshakeMessageClientHelloSessionID(t *testing.T) {
    61  	rawClientHello := []byte{
    62  		0xfe, 0xfd, 0xb6, 0x2f, 0xce, 0x5c, 0x42, 0x54, 0xff, 0x86, 0xe1, 0x24, 0x41, 0x91, 0x42,
    63  		0x62, 0x15, 0xad, 0x16, 0xc9, 0x15, 0x8d, 0x95, 0x71, 0x8a, 0xbb, 0x22, 0xd7, 0x47, 0xec,
    64  		0xd8, 0x3d, 0xdc, 0x4b, 0x20, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
    65  		0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
    66  		0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0x14, 0xe6, 0x14, 0x3a, 0x1b, 0x04, 0xea, 0x9e,
    67  		0x7a, 0x14, 0xd6, 0x6c, 0x57, 0xd0, 0x0e, 0x32, 0x85, 0x76, 0x18, 0xde, 0xd8, 0x00, 0x04,
    68  		0xc0, 0x2b, 0xc0, 0x0a, 0x01, 0x00, 0x00, 0x08, 0x00, 0x0a, 0x00, 0x04, 0x00, 0x02, 0x00,
    69  		0x1d,
    70  	}
    71  
    72  	sessionID := []byte{
    73  		0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
    74  		0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
    75  		0xfe, 0xff,
    76  	}
    77  
    78  	c := &MessageClientHello{}
    79  	if err := c.Unmarshal(rawClientHello); err != nil {
    80  		t.Error(err)
    81  	} else if !bytes.Equal(c.SessionID, sessionID) {
    82  		t.Errorf("handshakeMessageClientHello invalid SessionID: got %#v, want %#v", c.SessionID, sessionID)
    83  	}
    84  
    85  	raw, err := c.Marshal()
    86  	if err != nil {
    87  		t.Error(err)
    88  	} else if !reflect.DeepEqual(raw, rawClientHello) {
    89  		t.Errorf("handshakeMessageClientHello marshal: got %#v, want %#v", raw, rawClientHello)
    90  	}
    91  }