github.com/pion/webrtc/v3@v3.2.24/icegatherer_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  	"strings"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/pion/ice/v2"
    16  	"github.com/pion/transport/v2/test"
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func TestNewICEGatherer_Success(t *testing.T) {
    21  	// Limit runtime in case of deadlocks
    22  	lim := test.TimeOut(time.Second * 20)
    23  	defer lim.Stop()
    24  
    25  	report := test.CheckRoutines(t)
    26  	defer report()
    27  
    28  	opts := ICEGatherOptions{
    29  		ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
    30  	}
    31  
    32  	gatherer, err := NewAPI().NewICEGatherer(opts)
    33  	if err != nil {
    34  		t.Error(err)
    35  	}
    36  
    37  	if gatherer.State() != ICEGathererStateNew {
    38  		t.Fatalf("Expected gathering state new")
    39  	}
    40  
    41  	gatherFinished := make(chan struct{})
    42  	gatherer.OnLocalCandidate(func(i *ICECandidate) {
    43  		if i == nil {
    44  			close(gatherFinished)
    45  		}
    46  	})
    47  
    48  	if err = gatherer.Gather(); err != nil {
    49  		t.Error(err)
    50  	}
    51  
    52  	<-gatherFinished
    53  
    54  	params, err := gatherer.GetLocalParameters()
    55  	if err != nil {
    56  		t.Error(err)
    57  	}
    58  
    59  	if params.UsernameFragment == "" ||
    60  		params.Password == "" {
    61  		t.Fatalf("Empty local username or password frag")
    62  	}
    63  
    64  	candidates, err := gatherer.GetLocalCandidates()
    65  	if err != nil {
    66  		t.Error(err)
    67  	}
    68  
    69  	if len(candidates) == 0 {
    70  		t.Fatalf("No candidates gathered")
    71  	}
    72  
    73  	assert.NoError(t, gatherer.Close())
    74  }
    75  
    76  func TestICEGather_mDNSCandidateGathering(t *testing.T) {
    77  	// Limit runtime in case of deadlocks
    78  	lim := test.TimeOut(time.Second * 20)
    79  	defer lim.Stop()
    80  
    81  	report := test.CheckRoutines(t)
    82  	defer report()
    83  
    84  	s := SettingEngine{}
    85  	s.SetICEMulticastDNSMode(ice.MulticastDNSModeQueryAndGather)
    86  
    87  	gatherer, err := NewAPI(WithSettingEngine(s)).NewICEGatherer(ICEGatherOptions{})
    88  	if err != nil {
    89  		t.Error(err)
    90  	}
    91  
    92  	gotMulticastDNSCandidate, resolveFunc := context.WithCancel(context.Background())
    93  	gatherer.OnLocalCandidate(func(c *ICECandidate) {
    94  		if c != nil && strings.HasSuffix(c.Address, ".local") {
    95  			resolveFunc()
    96  		}
    97  	})
    98  
    99  	assert.NoError(t, gatherer.Gather())
   100  
   101  	<-gotMulticastDNSCandidate.Done()
   102  	assert.NoError(t, gatherer.Close())
   103  }
   104  
   105  func TestICEGatherer_AlreadyClosed(t *testing.T) {
   106  	// Limit runtime in case of deadlocks
   107  	lim := test.TimeOut(time.Second * 20)
   108  	defer lim.Stop()
   109  
   110  	report := test.CheckRoutines(t)
   111  	defer report()
   112  
   113  	opts := ICEGatherOptions{
   114  		ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
   115  	}
   116  
   117  	t.Run("Gather", func(t *testing.T) {
   118  		gatherer, err := NewAPI().NewICEGatherer(opts)
   119  		assert.NoError(t, err)
   120  
   121  		err = gatherer.createAgent()
   122  		assert.NoError(t, err)
   123  
   124  		err = gatherer.Close()
   125  		assert.NoError(t, err)
   126  
   127  		err = gatherer.Gather()
   128  		assert.ErrorIs(t, err, errICEAgentNotExist)
   129  	})
   130  
   131  	t.Run("GetLocalParameters", func(t *testing.T) {
   132  		gatherer, err := NewAPI().NewICEGatherer(opts)
   133  		assert.NoError(t, err)
   134  
   135  		err = gatherer.createAgent()
   136  		assert.NoError(t, err)
   137  
   138  		err = gatherer.Close()
   139  		assert.NoError(t, err)
   140  
   141  		_, err = gatherer.GetLocalParameters()
   142  		assert.ErrorIs(t, err, errICEAgentNotExist)
   143  	})
   144  
   145  	t.Run("GetLocalCandidates", func(t *testing.T) {
   146  		gatherer, err := NewAPI().NewICEGatherer(opts)
   147  		assert.NoError(t, err)
   148  
   149  		err = gatherer.createAgent()
   150  		assert.NoError(t, err)
   151  
   152  		err = gatherer.Close()
   153  		assert.NoError(t, err)
   154  
   155  		_, err = gatherer.GetLocalCandidates()
   156  		assert.ErrorIs(t, err, errICEAgentNotExist)
   157  	})
   158  }