github.com/pion/webrtc/v4@v4.0.1/icetransport_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  	"sync"
    11  	"sync/atomic"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/pion/transport/v3/test"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestICETransport_OnConnectionStateChange(t *testing.T) {
    20  	report := test.CheckRoutines(t)
    21  	defer report()
    22  
    23  	lim := test.TimeOut(time.Second * 30)
    24  	defer lim.Stop()
    25  
    26  	pcOffer, pcAnswer, err := newPair()
    27  	assert.NoError(t, err)
    28  
    29  	var (
    30  		iceComplete             sync.WaitGroup
    31  		peerConnectionConnected sync.WaitGroup
    32  	)
    33  	iceComplete.Add(2)
    34  	peerConnectionConnected.Add(2)
    35  
    36  	onIceComplete := func(s ICETransportState) {
    37  		if s == ICETransportStateConnected {
    38  			iceComplete.Done()
    39  		}
    40  	}
    41  	pcOffer.SCTP().Transport().ICETransport().OnConnectionStateChange(onIceComplete)
    42  	pcAnswer.SCTP().Transport().ICETransport().OnConnectionStateChange(onIceComplete)
    43  
    44  	onConnected := func(s PeerConnectionState) {
    45  		if s == PeerConnectionStateConnected {
    46  			peerConnectionConnected.Done()
    47  		}
    48  	}
    49  	pcOffer.OnConnectionStateChange(onConnected)
    50  	pcAnswer.OnConnectionStateChange(onConnected)
    51  
    52  	assert.NoError(t, signalPair(pcOffer, pcAnswer))
    53  	iceComplete.Wait()
    54  	peerConnectionConnected.Wait()
    55  
    56  	closePairNow(t, pcOffer, pcAnswer)
    57  }
    58  
    59  func TestICETransport_OnSelectedCandidatePairChange(t *testing.T) {
    60  	report := test.CheckRoutines(t)
    61  	defer report()
    62  
    63  	lim := test.TimeOut(time.Second * 30)
    64  	defer lim.Stop()
    65  
    66  	pcOffer, pcAnswer, err := newPair()
    67  	assert.NoError(t, err)
    68  
    69  	iceComplete := make(chan bool)
    70  	pcAnswer.OnICEConnectionStateChange(func(iceState ICEConnectionState) {
    71  		if iceState == ICEConnectionStateConnected {
    72  			time.Sleep(3 * time.Second)
    73  			close(iceComplete)
    74  		}
    75  	})
    76  
    77  	senderCalledCandidateChange := int32(0)
    78  	pcOffer.SCTP().Transport().ICETransport().OnSelectedCandidatePairChange(func(*ICECandidatePair) {
    79  		atomic.StoreInt32(&senderCalledCandidateChange, 1)
    80  	})
    81  
    82  	assert.NoError(t, signalPair(pcOffer, pcAnswer))
    83  	<-iceComplete
    84  
    85  	if atomic.LoadInt32(&senderCalledCandidateChange) == 0 {
    86  		t.Fatalf("Sender ICETransport OnSelectedCandidateChange was never called")
    87  	}
    88  
    89  	closePairNow(t, pcOffer, pcAnswer)
    90  }
    91  
    92  func TestICETransport_GetSelectedCandidatePair(t *testing.T) {
    93  	offerer, answerer, err := newPair()
    94  	assert.NoError(t, err)
    95  
    96  	peerConnectionConnected := untilConnectionState(PeerConnectionStateConnected, offerer, answerer)
    97  
    98  	offererSelectedPair, err := offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
    99  	assert.NoError(t, err)
   100  	assert.Nil(t, offererSelectedPair)
   101  	_, statsAvailable := offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats()
   102  	assert.False(t, statsAvailable)
   103  
   104  	answererSelectedPair, err := answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
   105  	assert.NoError(t, err)
   106  	assert.Nil(t, answererSelectedPair)
   107  	_, statsAvailable = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats()
   108  	assert.False(t, statsAvailable)
   109  
   110  	assert.NoError(t, signalPair(offerer, answerer))
   111  	peerConnectionConnected.Wait()
   112  
   113  	offererSelectedPair, err = offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
   114  	assert.NoError(t, err)
   115  	assert.NotNil(t, offererSelectedPair)
   116  	_, statsAvailable = offerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats()
   117  	assert.True(t, statsAvailable)
   118  
   119  	answererSelectedPair, err = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePair()
   120  	assert.NoError(t, err)
   121  	assert.NotNil(t, answererSelectedPair)
   122  	_, statsAvailable = answerer.SCTP().Transport().ICETransport().GetSelectedCandidatePairStats()
   123  	assert.True(t, statsAvailable)
   124  
   125  	closePairNow(t, offerer, answerer)
   126  }
   127  
   128  func TestICETransport_GetLocalParameters(t *testing.T) {
   129  	offerer, answerer, err := newPair()
   130  	assert.NoError(t, err)
   131  
   132  	peerConnectionConnected := untilConnectionState(PeerConnectionStateConnected, offerer, answerer)
   133  
   134  	assert.NoError(t, signalPair(offerer, answerer))
   135  	peerConnectionConnected.Wait()
   136  
   137  	localParameters, err := offerer.SCTP().Transport().ICETransport().GetLocalParameters()
   138  	assert.NoError(t, err)
   139  	assert.NotEqual(t, localParameters.UsernameFragment, "")
   140  	assert.NotEqual(t, localParameters.Password, "")
   141  
   142  	closePairNow(t, offerer, answerer)
   143  }