github.com/pion/webrtc/v4@v4.0.1/dtlstransport_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 "regexp" 11 "testing" 12 "time" 13 14 "github.com/pion/transport/v3/test" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 // An invalid fingerprint MUST cause PeerConnectionState to go to PeerConnectionStateFailed 19 func TestInvalidFingerprintCausesFailed(t *testing.T) { 20 lim := test.TimeOut(time.Second * 5) 21 defer lim.Stop() 22 23 report := test.CheckRoutines(t) 24 defer report() 25 26 pcOffer, err := NewPeerConnection(Configuration{}) 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 pcAnswer, err := NewPeerConnection(Configuration{}) 32 if err != nil { 33 t.Fatal(err) 34 } 35 36 pcAnswer.OnDataChannel(func(_ *DataChannel) { 37 t.Fatal("A DataChannel must not be created when Fingerprint verification fails") 38 }) 39 40 defer closePairNow(t, pcOffer, pcAnswer) 41 42 offerChan := make(chan SessionDescription) 43 pcOffer.OnICECandidate(func(candidate *ICECandidate) { 44 if candidate == nil { 45 offerChan <- *pcOffer.PendingLocalDescription() 46 } 47 }) 48 49 offerConnectionHasClosed := untilConnectionState(PeerConnectionStateClosed, pcOffer) 50 answerConnectionHasClosed := untilConnectionState(PeerConnectionStateClosed, pcAnswer) 51 52 if _, err = pcOffer.CreateDataChannel("unusedDataChannel", nil); err != nil { 53 t.Fatal(err) 54 } 55 56 offer, err := pcOffer.CreateOffer(nil) 57 if err != nil { 58 t.Fatal(err) 59 } else if err := pcOffer.SetLocalDescription(offer); err != nil { 60 t.Fatal(err) 61 } 62 63 select { 64 case offer := <-offerChan: 65 // Replace with invalid fingerprint 66 re := regexp.MustCompile(`sha-256 (.*?)\r`) 67 offer.SDP = re.ReplaceAllString(offer.SDP, "sha-256 AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA\r") 68 69 if err := pcAnswer.SetRemoteDescription(offer); err != nil { 70 t.Fatal(err) 71 } 72 73 answer, err := pcAnswer.CreateAnswer(nil) 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 if err = pcAnswer.SetLocalDescription(answer); err != nil { 79 t.Fatal(err) 80 } 81 82 answer.SDP = re.ReplaceAllString(answer.SDP, "sha-256 AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA:AA\r") 83 84 err = pcOffer.SetRemoteDescription(answer) 85 if err != nil { 86 t.Fatal(err) 87 } 88 case <-time.After(5 * time.Second): 89 t.Fatal("timed out waiting to receive offer") 90 } 91 92 offerConnectionHasClosed.Wait() 93 answerConnectionHasClosed.Wait() 94 95 if pcOffer.SCTP().Transport().State() != DTLSTransportStateClosed && pcOffer.SCTP().Transport().State() != DTLSTransportStateFailed { 96 t.Fail() 97 } 98 assert.Nil(t, pcOffer.SCTP().Transport().conn) 99 100 if pcAnswer.SCTP().Transport().State() != DTLSTransportStateClosed && pcAnswer.SCTP().Transport().State() != DTLSTransportStateFailed { 101 t.Fail() 102 } 103 assert.Nil(t, pcAnswer.SCTP().Transport().conn) 104 } 105 106 func TestPeerConnection_DTLSRoleSettingEngine(t *testing.T) { 107 runTest := func(r DTLSRole) { 108 s := SettingEngine{} 109 assert.NoError(t, s.SetAnsweringDTLSRole(r)) 110 111 offerPC, err := NewAPI(WithSettingEngine(s)).NewPeerConnection(Configuration{}) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 answerPC, err := NewAPI(WithSettingEngine(s)).NewPeerConnection(Configuration{}) 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 if err = signalPair(offerPC, answerPC); err != nil { 122 t.Fatal(err) 123 } 124 125 connectionComplete := untilConnectionState(PeerConnectionStateConnected, answerPC) 126 connectionComplete.Wait() 127 closePairNow(t, offerPC, answerPC) 128 } 129 130 report := test.CheckRoutines(t) 131 defer report() 132 133 t.Run("Server", func(*testing.T) { 134 runTest(DTLSRoleServer) 135 }) 136 137 t.Run("Client", func(*testing.T) { 138 runTest(DTLSRoleClient) 139 }) 140 }