discovery_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //go:build !PSIPHON_DISABLE_INPROXY
  2. /*
  3. * Copyright (c) 2023, Psiphon Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package inproxy
  21. import (
  22. "context"
  23. "fmt"
  24. "sync/atomic"
  25. "testing"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/internal/testutils"
  28. )
  29. func TestNATDiscovery(t *testing.T) {
  30. // Since this test can fail due to external network conditions, retry.
  31. var err error
  32. for try := 0; try < 2; try++ {
  33. err = runTestNATDiscovery()
  34. if err == nil {
  35. return
  36. }
  37. }
  38. t.Error(err.Error())
  39. }
  40. func runTestNATDiscovery() error {
  41. // TODO: run local STUN and port mapping servers to test against, along
  42. // with iptables rules to simulate NAT conditions
  43. stunServerAddress := "stun.voipgate.com:3478"
  44. var setNATTypeCallCount,
  45. setPortMappingTypesCallCount,
  46. stunServerAddressSucceededCallCount,
  47. stunServerAddressFailedCallCount int32
  48. coordinator := &testWebRTCDialCoordinator{
  49. stunServerAddress: stunServerAddress,
  50. stunServerAddressRFC5780: stunServerAddress,
  51. setNATType: func(NATType) {
  52. atomic.AddInt32(&setNATTypeCallCount, 1)
  53. },
  54. setPortMappingTypes: func(PortMappingTypes) {
  55. atomic.AddInt32(&setPortMappingTypesCallCount, 1)
  56. },
  57. stunServerAddressSucceeded: func(RFC5780 bool, address string) {
  58. if address == stunServerAddress {
  59. atomic.AddInt32(&stunServerAddressSucceededCallCount, 1)
  60. }
  61. },
  62. stunServerAddressFailed: func(RFC5780 bool, address string) {
  63. if address == stunServerAddress {
  64. atomic.AddInt32(&stunServerAddressFailedCallCount, 1)
  65. }
  66. },
  67. }
  68. checkCallCounts := func(a, b, c, d int32) error {
  69. callCount := atomic.LoadInt32(&setNATTypeCallCount)
  70. if callCount != a {
  71. return errors.Tracef(
  72. "unexpected setNATType call count: %d",
  73. callCount)
  74. }
  75. callCount = atomic.LoadInt32(&setPortMappingTypesCallCount)
  76. if callCount != b {
  77. return errors.Tracef(
  78. "unexpected setPortMappingTypes call count: %d",
  79. callCount)
  80. }
  81. callCount = atomic.LoadInt32(&stunServerAddressSucceededCallCount)
  82. if callCount != c {
  83. return errors.Tracef(
  84. "unexpected stunServerAddressSucceeded call count: %d",
  85. callCount)
  86. }
  87. callCount = atomic.LoadInt32(&stunServerAddressFailedCallCount)
  88. if callCount != d {
  89. return errors.Tracef(
  90. "unexpected stunServerAddressFailedCallCount call count: %d",
  91. callCount)
  92. }
  93. return nil
  94. }
  95. config := &NATDiscoverConfig{
  96. Logger: testutils.NewTestLogger(),
  97. WebRTCDialCoordinator: coordinator,
  98. }
  99. // Should do STUN only
  100. coordinator.disablePortMapping = true
  101. NATDiscover(context.Background(), config)
  102. err := checkCallCounts(1, 0, 1, 0)
  103. if err != nil {
  104. return errors.Trace(err)
  105. }
  106. // Should do port mapping only
  107. coordinator.disableSTUN = true
  108. coordinator.disablePortMapping = false
  109. NATDiscover(context.Background(), config)
  110. err = checkCallCounts(1, 1, 1, 0)
  111. if err != nil {
  112. return errors.Trace(err)
  113. }
  114. // Should skip both and use values cached in WebRTCDialCoordinator
  115. coordinator.disableSTUN = false
  116. coordinator.disablePortMapping = false
  117. NATDiscover(context.Background(), config)
  118. err = checkCallCounts(1, 1, 1, 0)
  119. if err != nil {
  120. return errors.Trace(err)
  121. }
  122. fmt.Printf("NAT Type: %s\n", coordinator.NATType())
  123. fmt.Printf("Port Mapping Types: %s\n", coordinator.PortMappingTypes())
  124. return nil
  125. }