forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerconnection_go_test.go
559 lines (478 loc) · 15.8 KB
/
peerconnection_go_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
// +build !js
package webrtc
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"fmt"
"math/big"
"reflect"
"regexp"
"testing"
"time"
"github.com/hcm007/ice"
"github.com/pion/transport/test"
"github.com/hcm007/webrtc/v2/pkg/rtcerr"
"github.com/stretchr/testify/assert"
)
// newPair creates two new peer connections (an offerer and an answerer) using
// the api.
func (api *API) newPair() (pcOffer *PeerConnection, pcAnswer *PeerConnection, err error) {
pca, err := api.NewPeerConnection(Configuration{})
if err != nil {
return nil, nil, err
}
pcb, err := api.NewPeerConnection(Configuration{})
if err != nil {
return nil, nil, err
}
return pca, pcb, nil
}
func TestNew_Go(t *testing.T) {
api := NewAPI()
t.Run("Success", func(t *testing.T) {
secretKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
certificate, err := GenerateCertificate(secretKey)
assert.Nil(t, err)
pc, err := api.NewPeerConnection(Configuration{
ICEServers: []ICEServer{
{
URLs: []string{
"stun:stun.l.google.com:19302",
"turns:google.de?transport=tcp",
},
Username: "unittest",
Credential: OAuthCredential{
MACKey: "WmtzanB3ZW9peFhtdm42NzUzNG0=",
AccessToken: "AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ==",
},
CredentialType: ICECredentialTypeOauth,
},
},
ICETransportPolicy: ICETransportPolicyRelay,
BundlePolicy: BundlePolicyMaxCompat,
RTCPMuxPolicy: RTCPMuxPolicyNegotiate,
PeerIdentity: "unittest",
Certificates: []Certificate{*certificate},
ICECandidatePoolSize: 5,
})
assert.Nil(t, err)
assert.NotNil(t, pc)
})
t.Run("Failure", func(t *testing.T) {
testCases := []struct {
initialize func() (*PeerConnection, error)
expectedErr error
}{
{func() (*PeerConnection, error) {
secretKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
certificate, err := NewCertificate(secretKey, x509.Certificate{
Version: 2,
SerialNumber: big.NewInt(1653),
NotBefore: time.Now().AddDate(0, -2, 0),
NotAfter: time.Now().AddDate(0, -1, 0),
})
assert.Nil(t, err)
return api.NewPeerConnection(Configuration{
Certificates: []Certificate{*certificate},
})
}, &rtcerr.InvalidAccessError{Err: ErrCertificateExpired}},
{func() (*PeerConnection, error) {
return api.NewPeerConnection(Configuration{
ICEServers: []ICEServer{
{
URLs: []string{
"stun:stun.l.google.com:19302",
"turns:google.de?transport=tcp",
},
Username: "unittest",
},
},
})
}, &rtcerr.InvalidAccessError{Err: ErrNoTurnCredencials}},
}
for i, testCase := range testCases {
_, err := testCase.initialize()
assert.EqualError(t, err, testCase.expectedErr.Error(),
"testCase: %d %v", i, testCase,
)
}
})
}
func TestPeerConnection_SetConfiguration_Go(t *testing.T) {
// Note: this test includes all SetConfiguration features that are supported
// by Go but not the WASM bindings, namely: ICEServer.Credential,
// ICEServer.CredentialType, and Certificates.
api := NewAPI()
secretKey1, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
certificate1, err := GenerateCertificate(secretKey1)
assert.Nil(t, err)
secretKey2, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
assert.Nil(t, err)
certificate2, err := GenerateCertificate(secretKey2)
assert.Nil(t, err)
for _, test := range []struct {
name string
init func() (*PeerConnection, error)
config Configuration
wantErr error
}{
{
name: "valid",
init: func() (*PeerConnection, error) {
pc, err := api.NewPeerConnection(Configuration{
PeerIdentity: "unittest",
Certificates: []Certificate{*certificate1},
ICECandidatePoolSize: 5,
})
if err != nil {
return pc, err
}
err = pc.SetConfiguration(Configuration{
ICEServers: []ICEServer{
{
URLs: []string{
"stun:stun.l.google.com:19302",
"turns:google.de?transport=tcp",
},
Username: "unittest",
Credential: OAuthCredential{
MACKey: "WmtzanB3ZW9peFhtdm42NzUzNG0=",
AccessToken: "AAwg3kPHWPfvk9bDFL936wYvkoctMADzQ==",
},
CredentialType: ICECredentialTypeOauth,
},
},
ICETransportPolicy: ICETransportPolicyAll,
BundlePolicy: BundlePolicyBalanced,
RTCPMuxPolicy: RTCPMuxPolicyRequire,
PeerIdentity: "unittest",
Certificates: []Certificate{*certificate1},
ICECandidatePoolSize: 5,
})
if err != nil {
return pc, err
}
return pc, nil
},
config: Configuration{},
wantErr: nil,
},
{
name: "update multiple certificates",
init: func() (*PeerConnection, error) {
return api.NewPeerConnection(Configuration{})
},
config: Configuration{
Certificates: []Certificate{*certificate1, *certificate2},
},
wantErr: &rtcerr.InvalidModificationError{Err: ErrModifyingCertificates},
},
{
name: "update certificate",
init: func() (*PeerConnection, error) {
return api.NewPeerConnection(Configuration{})
},
config: Configuration{
Certificates: []Certificate{*certificate1},
},
wantErr: &rtcerr.InvalidModificationError{Err: ErrModifyingCertificates},
},
{
name: "update ICEServers, no TURN credentials",
init: func() (*PeerConnection, error) {
return NewPeerConnection(Configuration{})
},
config: Configuration{
ICEServers: []ICEServer{
{
URLs: []string{
"stun:stun.l.google.com:19302",
"turns:google.de?transport=tcp",
},
Username: "unittest",
},
},
},
wantErr: &rtcerr.InvalidAccessError{Err: ErrNoTurnCredencials},
},
} {
pc, err := test.init()
if err != nil {
t.Errorf("SetConfiguration %q: init failed: %v", test.name, err)
}
err = pc.SetConfiguration(test.config)
if got, want := err, test.wantErr; !reflect.DeepEqual(got, want) {
t.Errorf("SetConfiguration %q: err = %v, want %v", test.name, got, want)
}
}
}
// TODO - This unittest needs to be completed when CreateDataChannel is complete
// func TestPeerConnection_CreateDataChannel(t *testing.T) {
// pc, err := New(Configuration{})
// assert.Nil(t, err)
//
// _, err = pc.CreateDataChannel("data", &DataChannelInit{
//
// })
// assert.Nil(t, err)
// }
func TestPeerConnection_EventHandlers_Go(t *testing.T) {
// Note: When testing the Go event handlers we peer into the state a bit more
// than what is possible for the environment agnostic (Go or WASM/JavaScript)
// EventHandlers test.
api := NewAPI()
pc, err := api.NewPeerConnection(Configuration{})
assert.Nil(t, err)
onTrackCalled := make(chan bool)
onICEConnectionStateChangeCalled := make(chan bool)
onDataChannelCalled := make(chan bool)
// Verify that the noop case works
assert.NotPanics(t, func() { pc.onTrack(nil, nil) })
assert.NotPanics(t, func() { pc.onICEConnectionStateChange(ice.ConnectionStateNew) })
pc.OnTrack(func(t *Track, r *RTPReceiver) {
onTrackCalled <- true
})
pc.OnICEConnectionStateChange(func(cs ICEConnectionState) {
onICEConnectionStateChangeCalled <- true
})
pc.OnDataChannel(func(dc *DataChannel) {
onDataChannelCalled <- true
})
// Verify that the handlers deal with nil inputs
assert.NotPanics(t, func() { pc.onTrack(nil, nil) })
assert.NotPanics(t, func() { go pc.onDataChannelHandler(nil) })
// Verify that the set handlers are called
assert.NotPanics(t, func() { pc.onTrack(&Track{}, &RTPReceiver{}) })
assert.NotPanics(t, func() { pc.onICEConnectionStateChange(ice.ConnectionStateNew) })
assert.NotPanics(t, func() { go pc.onDataChannelHandler(&DataChannel{api: api}) })
allTrue := func(vals []bool) bool {
for _, val := range vals {
if !val {
return false
}
}
return true
}
assert.True(t, allTrue([]bool{
<-onTrackCalled,
<-onICEConnectionStateChangeCalled,
<-onDataChannelCalled,
}))
}
// This test asserts that nothing deadlocks we try to shutdown when DTLS is in flight
// We ensure that DTLS is in flight by removing the mux func for it, so all inbound DTLS is lost
func TestPeerConnection_ShutdownNoDTLS(t *testing.T) {
lim := test.TimeOut(time.Second * 10)
defer lim.Stop()
api := NewAPI()
offerPC, answerPC, err := api.newPair()
if err != nil {
t.Fatal(err)
}
// Drop all incoming DTLS traffic
dropAllDTLS := func([]byte) bool {
return false
}
offerPC.dtlsTransport.dtlsMatcher = dropAllDTLS
answerPC.dtlsTransport.dtlsMatcher = dropAllDTLS
if err = signalPair(offerPC, answerPC); err != nil {
t.Fatal(err)
}
iceComplete := make(chan interface{})
answerPC.OnICEConnectionStateChange(func(iceState ICEConnectionState) {
if iceState == ICEConnectionStateConnected {
time.Sleep(time.Second) // Give time for DTLS to start
select {
case <-iceComplete:
default:
close(iceComplete)
}
}
})
<-iceComplete
if err = offerPC.Close(); err != nil {
t.Fatal(err)
} else if err = answerPC.Close(); err != nil {
t.Fatal(err)
}
}
func TestPeerConnection_PeropertyGetters(t *testing.T) {
pc := &PeerConnection{
currentLocalDescription: &SessionDescription{},
pendingLocalDescription: &SessionDescription{},
currentRemoteDescription: &SessionDescription{},
pendingRemoteDescription: &SessionDescription{},
signalingState: SignalingStateHaveLocalOffer,
iceConnectionState: ICEConnectionStateChecking,
connectionState: PeerConnectionStateConnecting,
}
assert.Equal(t, pc.currentLocalDescription, pc.CurrentLocalDescription(), "should match")
assert.Equal(t, pc.pendingLocalDescription, pc.PendingLocalDescription(), "should match")
assert.Equal(t, pc.currentRemoteDescription, pc.CurrentRemoteDescription(), "should match")
assert.Equal(t, pc.pendingRemoteDescription, pc.PendingRemoteDescription(), "should match")
assert.Equal(t, pc.signalingState, pc.SignalingState(), "should match")
assert.Equal(t, pc.iceConnectionState, pc.ICEConnectionState(), "should match")
assert.Equal(t, pc.connectionState, pc.ConnectionState(), "should match")
}
func TestPeerConnection_AnswerWithoutOffer(t *testing.T) {
pc, err := NewPeerConnection(Configuration{})
if err != nil {
t.Errorf("New PeerConnection: got error: %v", err)
}
_, err = pc.CreateAnswer(nil)
if !reflect.DeepEqual(&rtcerr.InvalidStateError{Err: ErrNoRemoteDescription}, err) {
t.Errorf("CreateAnswer without RemoteDescription: got error: %v", err)
}
}
func TestPeerConnection_satisfyTypeAndDirection(t *testing.T) {
createTransceiver := func(kind RTPCodecType, direction RTPTransceiverDirection) *RTPTransceiver {
return &RTPTransceiver{kind: kind, Direction: direction}
}
for _, test := range []struct {
name string
kinds []RTPCodecType
directions []RTPTransceiverDirection
localTransceivers []*RTPTransceiver
want []*RTPTransceiver
}{
{
"Audio and Video Transceivers can not satisfy each other",
[]RTPCodecType{RTPCodecTypeVideo},
[]RTPTransceiverDirection{RTPTransceiverDirectionSendrecv},
[]*RTPTransceiver{createTransceiver(RTPCodecTypeAudio, RTPTransceiverDirectionSendrecv)},
[]*RTPTransceiver{createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionInactive)},
},
{
"No local Transceivers, every remote should get an inactive",
[]RTPCodecType{RTPCodecTypeVideo, RTPCodecTypeAudio, RTPCodecTypeVideo, RTPCodecTypeVideo},
[]RTPTransceiverDirection{RTPTransceiverDirectionSendrecv, RTPTransceiverDirectionRecvonly, RTPTransceiverDirectionSendonly, RTPTransceiverDirectionInactive},
[]*RTPTransceiver{},
[]*RTPTransceiver{
createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionInactive),
createTransceiver(RTPCodecTypeAudio, RTPTransceiverDirectionInactive),
createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionInactive),
createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionInactive),
},
},
{
"Local Recv can satisfy remote SendRecv",
[]RTPCodecType{RTPCodecTypeVideo},
[]RTPTransceiverDirection{RTPTransceiverDirectionSendrecv},
[]*RTPTransceiver{createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionRecvonly)},
[]*RTPTransceiver{createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionRecvonly)},
},
{
"Don't satisify a Sendonly with a SendRecv, later SendRecv will be marked as Inactive",
[]RTPCodecType{RTPCodecTypeVideo, RTPCodecTypeVideo},
[]RTPTransceiverDirection{RTPTransceiverDirectionSendonly, RTPTransceiverDirectionSendrecv},
[]*RTPTransceiver{
createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionSendrecv),
createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionRecvonly),
},
[]*RTPTransceiver{
createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionRecvonly),
createTransceiver(RTPCodecTypeVideo, RTPTransceiverDirectionSendrecv),
},
},
} {
if len(test.kinds) != len(test.directions) {
t.Fatal("Kinds and Directions must be the same length")
}
got := []*RTPTransceiver{}
for i := range test.kinds {
res, filteredLocalTransceivers := satisfyTypeAndDirection(test.kinds[i], test.directions[i], test.localTransceivers)
got = append(got, res)
test.localTransceivers = filteredLocalTransceivers
}
if !reflect.DeepEqual(got, test.want) {
gotStr := ""
for _, t := range got {
gotStr += fmt.Sprintf("%+v\n", t)
}
wantStr := ""
for _, t := range test.want {
wantStr += fmt.Sprintf("%+v\n", t)
}
t.Errorf("satisfyTypeAndDirection %q: \ngot\n%s \nwant\n%s", test.name, gotStr, wantStr)
}
}
}
func TestOneAttrKeyConnectionSetupPerMediaDescriptionInSDP(t *testing.T) {
pc, err := NewPeerConnection(Configuration{})
assert.NoError(t, err)
_, err = pc.AddTransceiver(RTPCodecTypeVideo)
assert.NoError(t, err)
_, err = pc.AddTransceiver(RTPCodecTypeAudio)
assert.NoError(t, err)
_, err = pc.AddTransceiver(RTPCodecTypeAudio)
assert.NoError(t, err)
_, err = pc.AddTransceiver(RTPCodecTypeVideo)
assert.NoError(t, err)
sdp, err := pc.CreateOffer(nil)
assert.NoError(t, err)
re := regexp.MustCompile(`a=setup:[[:alpha:]]+`)
matches := re.FindAllStringIndex(sdp.SDP, -1)
// 5 because a datachannel is always added
assert.Len(t, matches, 5)
}
// Assert that candidates are gathered by calling SetLocalDescription, not SetRemoteDescription
// When trickle in on by default we can move this to peerconnection_test.go
func TestGatherOnSetLocalDescription(t *testing.T) {
lim := test.TimeOut(time.Second * 30)
defer lim.Stop()
pcOfferGathered := make(chan SessionDescription)
pcAnswerGathered := make(chan SessionDescription)
s := SettingEngine{}
s.SetTrickle(true)
api := NewAPI(WithSettingEngine(s))
pcOffer, err := api.NewPeerConnection(Configuration{})
if err != nil {
t.Error(err.Error())
}
// We need to create a data channel in order to trigger ICE
if _, err = pcOffer.CreateDataChannel("initial_data_channel", nil); err != nil {
t.Error(err.Error())
}
pcOffer.OnICECandidate(func(i *ICECandidate) {
if i == nil {
close(pcOfferGathered)
}
})
offer, err := pcOffer.CreateOffer(nil)
if err != nil {
t.Error(err.Error())
} else if err = pcOffer.SetLocalDescription(offer); err != nil {
t.Error(err.Error())
}
<-pcOfferGathered
pcAnswer, err := api.NewPeerConnection(Configuration{})
if err != nil {
t.Error(err.Error())
}
pcAnswer.OnICECandidate(func(i *ICECandidate) {
if i == nil {
close(pcAnswerGathered)
}
})
if err = pcAnswer.SetRemoteDescription(offer); err != nil {
t.Error(err.Error())
}
select {
case <-pcAnswerGathered:
t.Fatal("pcAnswer started gathering with no SetLocalDescription")
// Gathering is async, not sure of a better way to catch this currently
case <-time.After(3 * time.Second):
}
answer, err := pcAnswer.CreateAnswer(nil)
if err != nil {
t.Error(err.Error())
} else if err = pcAnswer.SetLocalDescription(answer); err != nil {
t.Error(err.Error())
}
<-pcAnswerGathered
}