dtlsrole_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. import (
  5. "fmt"
  6. "testing"
  7. "github.com/pion/sdp/v3"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestDTLSRole_String(t *testing.T) {
  11. testCases := []struct {
  12. role DTLSRole
  13. expectedString string
  14. }{
  15. {DTLSRole(Unknown), unknownStr},
  16. {DTLSRoleAuto, "auto"},
  17. {DTLSRoleClient, "client"},
  18. {DTLSRoleServer, "server"},
  19. }
  20. for i, testCase := range testCases {
  21. assert.Equal(t,
  22. testCase.expectedString,
  23. testCase.role.String(),
  24. "testCase: %d %v", i, testCase,
  25. )
  26. }
  27. }
  28. func TestDTLSRoleFromRemoteSDP(t *testing.T) {
  29. parseSDP := func(raw string) *sdp.SessionDescription {
  30. parsed := &sdp.SessionDescription{}
  31. if err := parsed.Unmarshal([]byte(raw)); err != nil {
  32. panic(err)
  33. }
  34. return parsed
  35. }
  36. const noMedia = `v=0
  37. o=- 4596489990601351948 2 IN IP4 127.0.0.1
  38. s=-
  39. t=0 0
  40. `
  41. const mediaNoSetup = `v=0
  42. o=- 4596489990601351948 2 IN IP4 127.0.0.1
  43. s=-
  44. t=0 0
  45. m=application 47299 DTLS/SCTP 5000
  46. c=IN IP4 192.168.20.129
  47. `
  48. const mediaSetupDeclared = `v=0
  49. o=- 4596489990601351948 2 IN IP4 127.0.0.1
  50. s=-
  51. t=0 0
  52. m=application 47299 DTLS/SCTP 5000
  53. c=IN IP4 192.168.20.129
  54. a=setup:%s
  55. `
  56. testCases := []struct {
  57. test string
  58. sessionDescription *sdp.SessionDescription
  59. expectedRole DTLSRole
  60. }{
  61. {"nil SessionDescription", nil, DTLSRoleAuto},
  62. {"No MediaDescriptions", parseSDP(noMedia), DTLSRoleAuto},
  63. {"MediaDescription, no setup", parseSDP(mediaNoSetup), DTLSRoleAuto},
  64. {"MediaDescription, setup:actpass", parseSDP(fmt.Sprintf(mediaSetupDeclared, "actpass")), DTLSRoleAuto},
  65. {"MediaDescription, setup:passive", parseSDP(fmt.Sprintf(mediaSetupDeclared, "passive")), DTLSRoleServer},
  66. {"MediaDescription, setup:active", parseSDP(fmt.Sprintf(mediaSetupDeclared, "active")), DTLSRoleClient},
  67. }
  68. for _, testCase := range testCases {
  69. assert.Equal(t,
  70. testCase.expectedRole,
  71. dtlsRoleFromRemoteSDP(testCase.sessionDescription),
  72. "TestDTLSRoleFromSDP (%s)", testCase.test,
  73. )
  74. }
  75. }