candidatepair_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package ice
  4. import (
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func hostCandidate() *CandidateHost {
  9. return &CandidateHost{
  10. candidateBase: candidateBase{
  11. candidateType: CandidateTypeHost,
  12. component: ComponentRTP,
  13. },
  14. }
  15. }
  16. func prflxCandidate() *CandidatePeerReflexive {
  17. return &CandidatePeerReflexive{
  18. candidateBase: candidateBase{
  19. candidateType: CandidateTypePeerReflexive,
  20. component: ComponentRTP,
  21. },
  22. }
  23. }
  24. func srflxCandidate() *CandidateServerReflexive {
  25. return &CandidateServerReflexive{
  26. candidateBase: candidateBase{
  27. candidateType: CandidateTypeServerReflexive,
  28. component: ComponentRTP,
  29. },
  30. }
  31. }
  32. func relayCandidate() *CandidateRelay {
  33. return &CandidateRelay{
  34. candidateBase: candidateBase{
  35. candidateType: CandidateTypeRelay,
  36. component: ComponentRTP,
  37. },
  38. }
  39. }
  40. func TestCandidatePairPriority(t *testing.T) {
  41. for _, test := range []struct {
  42. Pair *CandidatePair
  43. WantPriority uint64
  44. }{
  45. {
  46. Pair: newCandidatePair(
  47. hostCandidate(),
  48. hostCandidate(),
  49. false,
  50. ),
  51. WantPriority: 9151314440652587007,
  52. },
  53. {
  54. Pair: newCandidatePair(
  55. hostCandidate(),
  56. hostCandidate(),
  57. true,
  58. ),
  59. WantPriority: 9151314440652587007,
  60. },
  61. {
  62. Pair: newCandidatePair(
  63. hostCandidate(),
  64. prflxCandidate(),
  65. true,
  66. ),
  67. WantPriority: 7998392936314175488,
  68. },
  69. {
  70. Pair: newCandidatePair(
  71. hostCandidate(),
  72. prflxCandidate(),
  73. false,
  74. ),
  75. WantPriority: 7998392936314175487,
  76. },
  77. {
  78. Pair: newCandidatePair(
  79. hostCandidate(),
  80. srflxCandidate(),
  81. true,
  82. ),
  83. WantPriority: 7277816996102668288,
  84. },
  85. {
  86. Pair: newCandidatePair(
  87. hostCandidate(),
  88. srflxCandidate(),
  89. false,
  90. ),
  91. WantPriority: 7277816996102668287,
  92. },
  93. {
  94. Pair: newCandidatePair(
  95. hostCandidate(),
  96. relayCandidate(),
  97. true,
  98. ),
  99. WantPriority: 72057593987596288,
  100. },
  101. {
  102. Pair: newCandidatePair(
  103. hostCandidate(),
  104. relayCandidate(),
  105. false,
  106. ),
  107. WantPriority: 72057593987596287,
  108. },
  109. } {
  110. if got, want := test.Pair.priority(), test.WantPriority; got != want {
  111. t.Fatalf("CandidatePair(%v).Priority() = %d, want %d", test.Pair, got, want)
  112. }
  113. }
  114. }
  115. func TestCandidatePairEquality(t *testing.T) {
  116. pairA := newCandidatePair(hostCandidate(), srflxCandidate(), true)
  117. pairB := newCandidatePair(hostCandidate(), srflxCandidate(), false)
  118. if !pairA.equal(pairB) {
  119. t.Fatalf("Expected %v to equal %v", pairA, pairB)
  120. }
  121. }
  122. func TestNilCandidatePairString(t *testing.T) {
  123. var nilCandidatePair *CandidatePair
  124. assert.Equal(t, nilCandidatePair.String(), "")
  125. }