github.com/pion/dtls/v2@v2.2.12/pkg/protocol/handshake/message_server_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/protocol"
    13  	"github.com/pion/dtls/v2/pkg/protocol/extension"
    14  )
    15  
    16  func TestHandshakeMessageServerHello(t *testing.T) {
    17  	rawServerHello := []byte{
    18  		0xfe, 0xfd, 0x21, 0x63, 0x32, 0x21, 0x81, 0x0e, 0x98, 0x6c,
    19  		0x85, 0x3d, 0xa4, 0x39, 0xaf, 0x5f, 0xd6, 0x5c, 0xcc, 0x20,
    20  		0x7f, 0x7c, 0x78, 0xf1, 0x5f, 0x7e, 0x1c, 0xb7, 0xa1, 0x1e,
    21  		0xcf, 0x63, 0x84, 0x28, 0x00, 0xc0, 0x2b, 0x00, 0x00, 0x00,
    22  	}
    23  
    24  	cipherSuiteID := uint16(0xc02b)
    25  
    26  	parsedServerHello := &MessageServerHello{
    27  		Version: protocol.Version{Major: 0xFE, Minor: 0xFD},
    28  		Random: Random{
    29  			GMTUnixTime: time.Unix(560149025, 0),
    30  			RandomBytes: [28]byte{0x81, 0x0e, 0x98, 0x6c, 0x85, 0x3d, 0xa4, 0x39, 0xaf, 0x5f, 0xd6, 0x5c, 0xcc, 0x20, 0x7f, 0x7c, 0x78, 0xf1, 0x5f, 0x7e, 0x1c, 0xb7, 0xa1, 0x1e, 0xcf, 0x63, 0x84, 0x28},
    31  		},
    32  		SessionID:         []byte{},
    33  		CipherSuiteID:     &cipherSuiteID,
    34  		CompressionMethod: &protocol.CompressionMethod{},
    35  		Extensions:        []extension.Extension{},
    36  	}
    37  
    38  	c := &MessageServerHello{}
    39  	if err := c.Unmarshal(rawServerHello); err != nil {
    40  		t.Error(err)
    41  	} else if !reflect.DeepEqual(c, parsedServerHello) {
    42  		t.Errorf("handshakeMessageServerHello unmarshal: got %#v, want %#v", c, parsedServerHello)
    43  	}
    44  
    45  	raw, err := c.Marshal()
    46  	if err != nil {
    47  		t.Error(err)
    48  	} else if !reflect.DeepEqual(raw, rawServerHello) {
    49  		t.Errorf("handshakeMessageServerHello marshal: got %#v, want %#v", raw, rawServerHello)
    50  	}
    51  }
    52  
    53  func TestHandshakeMessageServerHelloSessionID(t *testing.T) {
    54  	rawServerHello := []byte{
    55  		0xfe, 0xfd, 0x21, 0x63, 0x32, 0x21, 0x81, 0x0e, 0x98, 0x6c,
    56  		0x85, 0x3d, 0xa4, 0x39, 0xaf, 0x5f, 0xd6, 0x5c, 0xcc, 0x20,
    57  		0x7f, 0x7c, 0x78, 0xf1, 0x5f, 0x7e, 0x1c, 0xb7, 0xa1, 0x1e,
    58  		0xcf, 0x63, 0x84, 0x28, 0x20, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4,
    59  		0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee,
    60  		0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
    61  		0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xc0, 0x2b, 0x00,
    62  		0x00, 0x00,
    63  	}
    64  
    65  	sessionID := []byte{
    66  		0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
    67  		0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,
    68  		0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd,
    69  		0xfe, 0xff,
    70  	}
    71  
    72  	c := &MessageServerHello{}
    73  	if err := c.Unmarshal(rawServerHello); err != nil {
    74  		t.Error(err)
    75  	} else if !bytes.Equal(c.SessionID, sessionID) {
    76  		t.Errorf("handshakeMessageServerHello invalid SessionID: got %#v, want %#v", c.SessionID, sessionID)
    77  	}
    78  
    79  	raw, err := c.Marshal()
    80  	if err != nil {
    81  		t.Error(err)
    82  	} else if !reflect.DeepEqual(raw, rawServerHello) {
    83  		t.Errorf("handshakeMessageServerHello marshal: got %#v, want %#v", raw, rawServerHello)
    84  	}
    85  }