settingengine_test.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !js
  4. // +build !js
  5. package webrtc
  6. import (
  7. "context"
  8. "net"
  9. "testing"
  10. "time"
  11. "github.com/pion/dtls/v2/pkg/crypto/elliptic"
  12. "github.com/pion/ice/v2"
  13. "github.com/pion/stun"
  14. "github.com/pion/transport/v2/test"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. func TestSetEphemeralUDPPortRange(t *testing.T) {
  18. s := SettingEngine{}
  19. if s.ephemeralUDP.PortMin != 0 ||
  20. s.ephemeralUDP.PortMax != 0 {
  21. t.Fatalf("SettingEngine defaults aren't as expected.")
  22. }
  23. // set bad ephemeral ports
  24. if err := s.SetEphemeralUDPPortRange(3000, 2999); err == nil {
  25. t.Fatalf("Setting engine should fail bad ephemeral ports.")
  26. }
  27. if err := s.SetEphemeralUDPPortRange(3000, 4000); err != nil {
  28. t.Fatalf("Setting engine failed valid port range: %s", err)
  29. }
  30. if s.ephemeralUDP.PortMin != 3000 ||
  31. s.ephemeralUDP.PortMax != 4000 {
  32. t.Fatalf("Setting engine ports do not reflect expected range")
  33. }
  34. }
  35. func TestSetConnectionTimeout(t *testing.T) {
  36. s := SettingEngine{}
  37. var nilDuration *time.Duration
  38. assert.Equal(t, s.timeout.ICEDisconnectedTimeout, nilDuration)
  39. assert.Equal(t, s.timeout.ICEFailedTimeout, nilDuration)
  40. assert.Equal(t, s.timeout.ICEKeepaliveInterval, nilDuration)
  41. s.SetICETimeouts(1*time.Second, 2*time.Second, 3*time.Second)
  42. assert.Equal(t, *s.timeout.ICEDisconnectedTimeout, 1*time.Second)
  43. assert.Equal(t, *s.timeout.ICEFailedTimeout, 2*time.Second)
  44. assert.Equal(t, *s.timeout.ICEKeepaliveInterval, 3*time.Second)
  45. }
  46. func TestDetachDataChannels(t *testing.T) {
  47. s := SettingEngine{}
  48. if s.detach.DataChannels {
  49. t.Fatalf("SettingEngine defaults aren't as expected.")
  50. }
  51. s.DetachDataChannels()
  52. if !s.detach.DataChannels {
  53. t.Fatalf("Failed to enable detached data channels.")
  54. }
  55. }
  56. func TestSetNAT1To1IPs(t *testing.T) {
  57. s := SettingEngine{}
  58. if s.candidates.NAT1To1IPs != nil {
  59. t.Errorf("Invalid default value")
  60. }
  61. if s.candidates.NAT1To1IPCandidateType != 0 {
  62. t.Errorf("Invalid default value")
  63. }
  64. ips := []string{"1.2.3.4"}
  65. typ := ICECandidateTypeHost
  66. s.SetNAT1To1IPs(ips, typ)
  67. if len(s.candidates.NAT1To1IPs) != 1 || s.candidates.NAT1To1IPs[0] != "1.2.3.4" {
  68. t.Fatalf("Failed to set NAT1To1IPs")
  69. }
  70. if s.candidates.NAT1To1IPCandidateType != typ {
  71. t.Fatalf("Failed to set NAT1To1IPCandidateType")
  72. }
  73. }
  74. func TestSetAnsweringDTLSRole(t *testing.T) {
  75. s := SettingEngine{}
  76. assert.Error(t, s.SetAnsweringDTLSRole(DTLSRoleAuto), "SetAnsweringDTLSRole can only be called with DTLSRoleClient or DTLSRoleServer")
  77. assert.Error(t, s.SetAnsweringDTLSRole(DTLSRole(0)), "SetAnsweringDTLSRole can only be called with DTLSRoleClient or DTLSRoleServer")
  78. }
  79. func TestSetReplayProtection(t *testing.T) {
  80. s := SettingEngine{}
  81. if s.replayProtection.DTLS != nil ||
  82. s.replayProtection.SRTP != nil ||
  83. s.replayProtection.SRTCP != nil {
  84. t.Fatalf("SettingEngine defaults aren't as expected.")
  85. }
  86. s.SetDTLSReplayProtectionWindow(128)
  87. s.SetSRTPReplayProtectionWindow(64)
  88. s.SetSRTCPReplayProtectionWindow(32)
  89. if s.replayProtection.DTLS == nil ||
  90. *s.replayProtection.DTLS != 128 {
  91. t.Errorf("Failed to set DTLS replay protection window")
  92. }
  93. if s.replayProtection.SRTP == nil ||
  94. *s.replayProtection.SRTP != 64 {
  95. t.Errorf("Failed to set SRTP replay protection window")
  96. }
  97. if s.replayProtection.SRTCP == nil ||
  98. *s.replayProtection.SRTCP != 32 {
  99. t.Errorf("Failed to set SRTCP replay protection window")
  100. }
  101. }
  102. func TestSettingEngine_SetICETCP(t *testing.T) {
  103. report := test.CheckRoutines(t)
  104. defer report()
  105. listener, err := net.ListenTCP("tcp", &net.TCPAddr{})
  106. if err != nil {
  107. panic(err)
  108. }
  109. defer func() {
  110. _ = listener.Close()
  111. }()
  112. tcpMux := NewICETCPMux(nil, listener, 8)
  113. defer func() {
  114. _ = tcpMux.Close()
  115. }()
  116. settingEngine := SettingEngine{}
  117. settingEngine.SetICETCPMux(tcpMux)
  118. assert.Equal(t, tcpMux, settingEngine.iceTCPMux)
  119. }
  120. func TestSettingEngine_SetDisableMediaEngineCopy(t *testing.T) {
  121. t.Run("Copy", func(t *testing.T) {
  122. m := &MediaEngine{}
  123. assert.NoError(t, m.RegisterDefaultCodecs())
  124. api := NewAPI(WithMediaEngine(m))
  125. offerer, answerer, err := api.newPair(Configuration{})
  126. assert.NoError(t, err)
  127. _, err = offerer.AddTransceiverFromKind(RTPCodecTypeVideo)
  128. assert.NoError(t, err)
  129. assert.NoError(t, signalPair(offerer, answerer))
  130. // Assert that the MediaEngine the user created isn't modified
  131. assert.False(t, m.negotiatedVideo)
  132. assert.Empty(t, m.negotiatedVideoCodecs)
  133. // Assert that the internal MediaEngine is modified
  134. assert.True(t, offerer.api.mediaEngine.negotiatedVideo)
  135. assert.NotEmpty(t, offerer.api.mediaEngine.negotiatedVideoCodecs)
  136. closePairNow(t, offerer, answerer)
  137. newOfferer, newAnswerer, err := api.newPair(Configuration{})
  138. assert.NoError(t, err)
  139. // Assert that the first internal MediaEngine hasn't been cleared
  140. assert.True(t, offerer.api.mediaEngine.negotiatedVideo)
  141. assert.NotEmpty(t, offerer.api.mediaEngine.negotiatedVideoCodecs)
  142. // Assert that the new internal MediaEngine isn't modified
  143. assert.False(t, newOfferer.api.mediaEngine.negotiatedVideo)
  144. assert.Empty(t, newAnswerer.api.mediaEngine.negotiatedVideoCodecs)
  145. closePairNow(t, newOfferer, newAnswerer)
  146. })
  147. t.Run("No Copy", func(t *testing.T) {
  148. m := &MediaEngine{}
  149. assert.NoError(t, m.RegisterDefaultCodecs())
  150. s := SettingEngine{}
  151. s.DisableMediaEngineCopy(true)
  152. api := NewAPI(WithMediaEngine(m), WithSettingEngine(s))
  153. offerer, answerer, err := api.newPair(Configuration{})
  154. assert.NoError(t, err)
  155. _, err = offerer.AddTransceiverFromKind(RTPCodecTypeVideo)
  156. assert.NoError(t, err)
  157. assert.NoError(t, signalPair(offerer, answerer))
  158. // Assert that the user MediaEngine was modified, so no copy happened
  159. assert.True(t, m.negotiatedVideo)
  160. assert.NotEmpty(t, m.negotiatedVideoCodecs)
  161. closePairNow(t, offerer, answerer)
  162. offerer, answerer, err = api.newPair(Configuration{})
  163. assert.NoError(t, err)
  164. // Assert that the new internal MediaEngine was modified, so no copy happened
  165. assert.True(t, offerer.api.mediaEngine.negotiatedVideo)
  166. assert.NotEmpty(t, offerer.api.mediaEngine.negotiatedVideoCodecs)
  167. closePairNow(t, offerer, answerer)
  168. })
  169. }
  170. func TestSetDTLSRetransmissionInterval(t *testing.T) {
  171. s := SettingEngine{}
  172. if s.dtls.retransmissionInterval != 0 {
  173. t.Fatalf("SettingEngine defaults aren't as expected.")
  174. }
  175. s.SetDTLSRetransmissionInterval(100 * time.Millisecond)
  176. if s.dtls.retransmissionInterval == 0 ||
  177. s.dtls.retransmissionInterval != 100*time.Millisecond {
  178. t.Errorf("Failed to set DTLS retransmission interval")
  179. }
  180. s.SetDTLSRetransmissionInterval(1 * time.Second)
  181. if s.dtls.retransmissionInterval == 0 ||
  182. s.dtls.retransmissionInterval != 1*time.Second {
  183. t.Errorf("Failed to set DTLS retransmission interval")
  184. }
  185. }
  186. func TestSetDTLSEllipticCurves(t *testing.T) {
  187. s := SettingEngine{}
  188. if len(s.dtls.ellipticCurves) != 0 {
  189. t.Fatalf("SettingEngine defaults aren't as expected.")
  190. }
  191. s.SetDTLSEllipticCurves(elliptic.P256)
  192. if len(s.dtls.ellipticCurves) == 0 ||
  193. s.dtls.ellipticCurves[0] != elliptic.P256 {
  194. t.Errorf("Failed to set DTLS elliptic curves")
  195. }
  196. }
  197. func TestSetDTLSHandShakeTimeout(*testing.T) {
  198. s := SettingEngine{}
  199. s.SetDTLSConnectContextMaker(func() (context.Context, func()) {
  200. return context.WithTimeout(context.Background(), 60*time.Second)
  201. })
  202. }
  203. func TestSetSCTPMaxReceiverBufferSize(t *testing.T) {
  204. s := SettingEngine{}
  205. assert.Equal(t, uint32(0), s.sctp.maxReceiveBufferSize)
  206. expSize := uint32(4 * 1024 * 1024)
  207. s.SetSCTPMaxReceiveBufferSize(expSize)
  208. assert.Equal(t, expSize, s.sctp.maxReceiveBufferSize)
  209. }
  210. func TestSetICEBindingRequestHandler(t *testing.T) {
  211. seenICEControlled, seenICEControlledCancel := context.WithCancel(context.Background())
  212. seenICEControlling, seenICEControllingCancel := context.WithCancel(context.Background())
  213. s := SettingEngine{}
  214. s.SetICEBindingRequestHandler(func(m *stun.Message, _, _ ice.Candidate, _ *ice.CandidatePair) bool {
  215. for _, a := range m.Attributes {
  216. switch a.Type {
  217. case stun.AttrICEControlled:
  218. seenICEControlledCancel()
  219. case stun.AttrICEControlling:
  220. seenICEControllingCancel()
  221. default:
  222. }
  223. }
  224. return false
  225. })
  226. pcOffer, pcAnswer, err := NewAPI(WithSettingEngine(s)).newPair(Configuration{})
  227. assert.NoError(t, err)
  228. assert.NoError(t, signalPair(pcOffer, pcAnswer))
  229. <-seenICEControlled.Done()
  230. <-seenICEControlling.Done()
  231. closePairNow(t, pcOffer, pcAnswer)
  232. }