handshake_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package dtls
  4. import (
  5. "reflect"
  6. "testing"
  7. "time"
  8. "github.com/pion/dtls/v2/pkg/protocol"
  9. "github.com/pion/dtls/v2/pkg/protocol/extension"
  10. "github.com/pion/dtls/v2/pkg/protocol/handshake"
  11. )
  12. func TestHandshakeMessage(t *testing.T) {
  13. rawHandshakeMessage := []byte{
  14. 0x01, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0xfe, 0xfd, 0xb6,
  15. 0x2f, 0xce, 0x5c, 0x42, 0x54, 0xff, 0x86, 0xe1, 0x24, 0x41, 0x91, 0x42, 0x62, 0x15, 0xad,
  16. 0x16, 0xc9, 0x15, 0x8d, 0x95, 0x71, 0x8a, 0xbb, 0x22, 0xd7, 0x47, 0xec, 0xd8, 0x3d, 0xdc,
  17. 0x4b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18. }
  19. parsedHandshake := &handshake.Handshake{
  20. Header: handshake.Header{
  21. Length: 0x29,
  22. FragmentLength: 0x29,
  23. Type: handshake.TypeClientHello,
  24. },
  25. Message: &handshake.MessageClientHello{
  26. Version: protocol.Version{Major: 0xFE, Minor: 0xFD},
  27. Random: handshake.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{},
  33. CipherSuiteIDs: []uint16{},
  34. CompressionMethods: []*protocol.CompressionMethod{},
  35. Extensions: []extension.Extension{},
  36. },
  37. }
  38. h := &handshake.Handshake{}
  39. if err := h.Unmarshal(rawHandshakeMessage); err != nil {
  40. t.Error(err)
  41. } else if !reflect.DeepEqual(h, parsedHandshake) {
  42. t.Errorf("handshakeMessageClientHello unmarshal: got %#v, want %#v", h, parsedHandshake)
  43. }
  44. raw, err := h.Marshal()
  45. if err != nil {
  46. t.Error(err)
  47. } else if !reflect.DeepEqual(raw, rawHandshakeMessage) {
  48. t.Errorf("handshakeMessageClientHello marshal: got %#v, want %#v", raw, rawHandshakeMessage)
  49. }
  50. }