github.com/pion/webrtc/v3@v3.2.24/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/v2/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 * 40)
    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  	offerConnectionHasFailed := untilConnectionState(PeerConnectionStateFailed, pcOffer)
    50  	answerConnectionHasFailed := untilConnectionState(PeerConnectionStateFailed, 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  	offerConnectionHasFailed.Wait()
    93  	answerConnectionHasFailed.Wait()
    94  
    95  	assert.Equal(t, pcOffer.SCTP().Transport().State(), DTLSTransportStateFailed)
    96  	assert.Nil(t, pcOffer.SCTP().Transport().conn)
    97  
    98  	assert.Equal(t, pcAnswer.SCTP().Transport().State(), DTLSTransportStateFailed)
    99  	assert.Nil(t, pcAnswer.SCTP().Transport().conn)
   100  }
   101  
   102  func TestPeerConnection_DTLSRoleSettingEngine(t *testing.T) {
   103  	runTest := func(r DTLSRole) {
   104  		s := SettingEngine{}
   105  		assert.NoError(t, s.SetAnsweringDTLSRole(r))
   106  
   107  		offerPC, err := NewAPI(WithSettingEngine(s)).NewPeerConnection(Configuration{})
   108  		if err != nil {
   109  			t.Fatal(err)
   110  		}
   111  
   112  		answerPC, err := NewAPI(WithSettingEngine(s)).NewPeerConnection(Configuration{})
   113  		if err != nil {
   114  			t.Fatal(err)
   115  		}
   116  
   117  		if err = signalPair(offerPC, answerPC); err != nil {
   118  			t.Fatal(err)
   119  		}
   120  
   121  		connectionComplete := untilConnectionState(PeerConnectionStateConnected, answerPC)
   122  		connectionComplete.Wait()
   123  		closePairNow(t, offerPC, answerPC)
   124  	}
   125  
   126  	report := test.CheckRoutines(t)
   127  	defer report()
   128  
   129  	t.Run("Server", func(t *testing.T) {
   130  		runTest(DTLSRoleServer)
   131  	})
   132  
   133  	t.Run("Client", func(t *testing.T) {
   134  		runTest(DTLSRoleClient)
   135  	})
   136  }