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