icegatherer_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. //go:build !js
  4. // +build !js
  5. package webrtc
  6. import (
  7. "context"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/pion/ice/v2"
  12. "github.com/pion/transport/v2/test"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func TestNewICEGatherer_Success(t *testing.T) {
  16. // Limit runtime in case of deadlocks
  17. lim := test.TimeOut(time.Second * 20)
  18. defer lim.Stop()
  19. report := test.CheckRoutines(t)
  20. defer report()
  21. opts := ICEGatherOptions{
  22. ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
  23. }
  24. gatherer, err := NewAPI().NewICEGatherer(opts)
  25. if err != nil {
  26. t.Error(err)
  27. }
  28. if gatherer.State() != ICEGathererStateNew {
  29. t.Fatalf("Expected gathering state new")
  30. }
  31. gatherFinished := make(chan struct{})
  32. gatherer.OnLocalCandidate(func(i *ICECandidate) {
  33. if i == nil {
  34. close(gatherFinished)
  35. }
  36. })
  37. if err = gatherer.Gather(); err != nil {
  38. t.Error(err)
  39. }
  40. <-gatherFinished
  41. params, err := gatherer.GetLocalParameters()
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. if params.UsernameFragment == "" ||
  46. params.Password == "" {
  47. t.Fatalf("Empty local username or password frag")
  48. }
  49. candidates, err := gatherer.GetLocalCandidates()
  50. if err != nil {
  51. t.Error(err)
  52. }
  53. if len(candidates) == 0 {
  54. t.Fatalf("No candidates gathered")
  55. }
  56. assert.NoError(t, gatherer.Close())
  57. }
  58. func TestICEGather_mDNSCandidateGathering(t *testing.T) {
  59. // Limit runtime in case of deadlocks
  60. lim := test.TimeOut(time.Second * 20)
  61. defer lim.Stop()
  62. report := test.CheckRoutines(t)
  63. defer report()
  64. s := SettingEngine{}
  65. s.SetICEMulticastDNSMode(ice.MulticastDNSModeQueryAndGather)
  66. gatherer, err := NewAPI(WithSettingEngine(s)).NewICEGatherer(ICEGatherOptions{})
  67. if err != nil {
  68. t.Error(err)
  69. }
  70. gotMulticastDNSCandidate, resolveFunc := context.WithCancel(context.Background())
  71. gatherer.OnLocalCandidate(func(c *ICECandidate) {
  72. if c != nil && strings.HasSuffix(c.Address, ".local") {
  73. resolveFunc()
  74. }
  75. })
  76. assert.NoError(t, gatherer.Gather())
  77. <-gotMulticastDNSCandidate.Done()
  78. assert.NoError(t, gatherer.Close())
  79. }
  80. func TestICEGatherer_AlreadyClosed(t *testing.T) {
  81. // Limit runtime in case of deadlocks
  82. lim := test.TimeOut(time.Second * 20)
  83. defer lim.Stop()
  84. report := test.CheckRoutines(t)
  85. defer report()
  86. opts := ICEGatherOptions{
  87. ICEServers: []ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}},
  88. }
  89. t.Run("Gather", func(t *testing.T) {
  90. gatherer, err := NewAPI().NewICEGatherer(opts)
  91. assert.NoError(t, err)
  92. err = gatherer.createAgent()
  93. assert.NoError(t, err)
  94. err = gatherer.Close()
  95. assert.NoError(t, err)
  96. err = gatherer.Gather()
  97. assert.ErrorIs(t, err, errICEAgentNotExist)
  98. })
  99. t.Run("GetLocalParameters", func(t *testing.T) {
  100. gatherer, err := NewAPI().NewICEGatherer(opts)
  101. assert.NoError(t, err)
  102. err = gatherer.createAgent()
  103. assert.NoError(t, err)
  104. err = gatherer.Close()
  105. assert.NoError(t, err)
  106. _, err = gatherer.GetLocalParameters()
  107. assert.ErrorIs(t, err, errICEAgentNotExist)
  108. })
  109. t.Run("GetLocalCandidates", func(t *testing.T) {
  110. gatherer, err := NewAPI().NewICEGatherer(opts)
  111. assert.NoError(t, err)
  112. err = gatherer.createAgent()
  113. assert.NoError(t, err)
  114. err = gatherer.Close()
  115. assert.NoError(t, err)
  116. _, err = gatherer.GetLocalCandidates()
  117. assert.ErrorIs(t, err, errICEAgentNotExist)
  118. })
  119. }