icecandidate_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package webrtc
  4. import (
  5. "testing"
  6. "github.com/pion/ice/v2"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestICECandidate_Convert(t *testing.T) {
  10. testCases := []struct {
  11. native ICECandidate
  12. expectedType ice.CandidateType
  13. expectedNetwork string
  14. expectedAddress string
  15. expectedPort int
  16. expectedComponent uint16
  17. expectedRelatedAddress *ice.CandidateRelatedAddress
  18. }{
  19. {
  20. ICECandidate{
  21. Foundation: "foundation",
  22. Priority: 128,
  23. Address: "1.0.0.1",
  24. Protocol: ICEProtocolUDP,
  25. Port: 1234,
  26. Typ: ICECandidateTypeHost,
  27. Component: 1,
  28. },
  29. ice.CandidateTypeHost,
  30. "udp",
  31. "1.0.0.1",
  32. 1234,
  33. 1,
  34. nil,
  35. },
  36. {
  37. ICECandidate{
  38. Foundation: "foundation",
  39. Priority: 128,
  40. Address: "::1",
  41. Protocol: ICEProtocolUDP,
  42. Port: 1234,
  43. Typ: ICECandidateTypeSrflx,
  44. Component: 1,
  45. RelatedAddress: "1.0.0.1",
  46. RelatedPort: 4321,
  47. },
  48. ice.CandidateTypeServerReflexive,
  49. "udp",
  50. "::1",
  51. 1234,
  52. 1,
  53. &ice.CandidateRelatedAddress{
  54. Address: "1.0.0.1",
  55. Port: 4321,
  56. },
  57. },
  58. {
  59. ICECandidate{
  60. Foundation: "foundation",
  61. Priority: 128,
  62. Address: "::1",
  63. Protocol: ICEProtocolUDP,
  64. Port: 1234,
  65. Typ: ICECandidateTypePrflx,
  66. Component: 1,
  67. RelatedAddress: "1.0.0.1",
  68. RelatedPort: 4321,
  69. },
  70. ice.CandidateTypePeerReflexive,
  71. "udp",
  72. "::1",
  73. 1234,
  74. 1,
  75. &ice.CandidateRelatedAddress{
  76. Address: "1.0.0.1",
  77. Port: 4321,
  78. },
  79. },
  80. }
  81. for i, testCase := range testCases {
  82. var expectedICE ice.Candidate
  83. var err error
  84. switch testCase.expectedType { // nolint:exhaustive
  85. case ice.CandidateTypeHost:
  86. config := ice.CandidateHostConfig{
  87. Network: testCase.expectedNetwork,
  88. Address: testCase.expectedAddress,
  89. Port: testCase.expectedPort,
  90. Component: testCase.expectedComponent,
  91. Foundation: "foundation",
  92. Priority: 128,
  93. }
  94. expectedICE, err = ice.NewCandidateHost(&config)
  95. case ice.CandidateTypeServerReflexive:
  96. config := ice.CandidateServerReflexiveConfig{
  97. Network: testCase.expectedNetwork,
  98. Address: testCase.expectedAddress,
  99. Port: testCase.expectedPort,
  100. Component: testCase.expectedComponent,
  101. Foundation: "foundation",
  102. Priority: 128,
  103. RelAddr: testCase.expectedRelatedAddress.Address,
  104. RelPort: testCase.expectedRelatedAddress.Port,
  105. }
  106. expectedICE, err = ice.NewCandidateServerReflexive(&config)
  107. case ice.CandidateTypePeerReflexive:
  108. config := ice.CandidatePeerReflexiveConfig{
  109. Network: testCase.expectedNetwork,
  110. Address: testCase.expectedAddress,
  111. Port: testCase.expectedPort,
  112. Component: testCase.expectedComponent,
  113. Foundation: "foundation",
  114. Priority: 128,
  115. RelAddr: testCase.expectedRelatedAddress.Address,
  116. RelPort: testCase.expectedRelatedAddress.Port,
  117. }
  118. expectedICE, err = ice.NewCandidatePeerReflexive(&config)
  119. }
  120. assert.NoError(t, err)
  121. // first copy the candidate ID so it matches the new one
  122. testCase.native.statsID = expectedICE.ID()
  123. actualICE, err := testCase.native.toICE()
  124. assert.NoError(t, err)
  125. assert.Equal(t, expectedICE, actualICE, "testCase: %d ice not equal %v", i, actualICE)
  126. }
  127. }
  128. func TestConvertTypeFromICE(t *testing.T) {
  129. t.Run("host", func(t *testing.T) {
  130. ct, err := convertTypeFromICE(ice.CandidateTypeHost)
  131. if err != nil {
  132. t.Fatal("failed coverting ice.CandidateTypeHost")
  133. }
  134. if ct != ICECandidateTypeHost {
  135. t.Fatal("should be converted to ICECandidateTypeHost")
  136. }
  137. })
  138. t.Run("srflx", func(t *testing.T) {
  139. ct, err := convertTypeFromICE(ice.CandidateTypeServerReflexive)
  140. if err != nil {
  141. t.Fatal("failed coverting ice.CandidateTypeServerReflexive")
  142. }
  143. if ct != ICECandidateTypeSrflx {
  144. t.Fatal("should be converted to ICECandidateTypeSrflx")
  145. }
  146. })
  147. t.Run("prflx", func(t *testing.T) {
  148. ct, err := convertTypeFromICE(ice.CandidateTypePeerReflexive)
  149. if err != nil {
  150. t.Fatal("failed coverting ice.CandidateTypePeerReflexive")
  151. }
  152. if ct != ICECandidateTypePrflx {
  153. t.Fatal("should be converted to ICECandidateTypePrflx")
  154. }
  155. })
  156. }
  157. func TestICECandidate_ToJSON(t *testing.T) {
  158. candidate := ICECandidate{
  159. Foundation: "foundation",
  160. Priority: 128,
  161. Address: "1.0.0.1",
  162. Protocol: ICEProtocolUDP,
  163. Port: 1234,
  164. Typ: ICECandidateTypeHost,
  165. Component: 1,
  166. }
  167. candidateInit := candidate.ToJSON()
  168. assert.Equal(t, uint16(0), *candidateInit.SDPMLineIndex)
  169. assert.Equal(t, "candidate:foundation 1 udp 128 1.0.0.1 1234 typ host", candidateInit.Candidate)
  170. }