discovery_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //go:build PSIPHON_ENABLE_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. "sync/atomic"
  24. "testing"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/internal/testutils"
  26. )
  27. func TestNATDiscovery(t *testing.T) {
  28. // TODO: run local STUN and port mapping servers to test against, along
  29. // with iptables rules to simulate NAT conditions
  30. stunServerAddress := "stun.voipgate.com:3478"
  31. var setNATTypeCallCount,
  32. setPortMappingTypesCallCount,
  33. stunServerAddressSucceededCallCount,
  34. stunServerAddressFailedCallCount int32
  35. coordinator := &testWebRTCDialCoordinator{
  36. stunServerAddress: stunServerAddress,
  37. stunServerAddressRFC5780: stunServerAddress,
  38. setNATType: func(NATType) {
  39. atomic.AddInt32(&setNATTypeCallCount, 1)
  40. },
  41. setPortMappingTypes: func(PortMappingTypes) {
  42. atomic.AddInt32(&setPortMappingTypesCallCount, 1)
  43. },
  44. stunServerAddressSucceeded: func(RFC5780 bool, address string) {
  45. atomic.AddInt32(&stunServerAddressSucceededCallCount, 1)
  46. if address != stunServerAddress {
  47. t.Errorf("unexpected STUN server address")
  48. }
  49. },
  50. stunServerAddressFailed: func(RFC5780 bool, address string) {
  51. atomic.AddInt32(&stunServerAddressFailedCallCount, 1)
  52. if address != stunServerAddress {
  53. t.Errorf("unexpected STUN server address")
  54. }
  55. },
  56. }
  57. checkCallCounts := func(a, b, c, d int32) {
  58. callCount := atomic.LoadInt32(&setNATTypeCallCount)
  59. if callCount != a {
  60. t.Errorf(
  61. "unexpected setNATType call count: %d",
  62. callCount)
  63. }
  64. callCount = atomic.LoadInt32(&setPortMappingTypesCallCount)
  65. if callCount != b {
  66. t.Errorf(
  67. "unexpected setPortMappingTypes call count: %d",
  68. callCount)
  69. }
  70. callCount = atomic.LoadInt32(&stunServerAddressSucceededCallCount)
  71. if callCount != c {
  72. t.Errorf(
  73. "unexpected stunServerAddressSucceeded call count: %d",
  74. callCount)
  75. }
  76. callCount = atomic.LoadInt32(&stunServerAddressFailedCallCount)
  77. if callCount != d {
  78. t.Errorf(
  79. "unexpected stunServerAddressFailedCallCount call count: %d",
  80. callCount)
  81. }
  82. }
  83. config := &NATDiscoverConfig{
  84. Logger: testutils.NewTestLogger(),
  85. WebRTCDialCoordinator: coordinator,
  86. }
  87. // Should do STUN only
  88. coordinator.disablePortMapping = true
  89. NATDiscover(context.Background(), config)
  90. checkCallCounts(1, 0, 1, 0)
  91. // Should do port mapping only
  92. coordinator.disableSTUN = true
  93. coordinator.disablePortMapping = false
  94. NATDiscover(context.Background(), config)
  95. checkCallCounts(1, 1, 1, 0)
  96. // Should skip both and use values cached in WebRTCDialCoordinator
  97. coordinator.disableSTUN = false
  98. coordinator.disablePortMapping = false
  99. NATDiscover(context.Background(), config)
  100. checkCallCounts(1, 1, 1, 0)
  101. t.Logf("NAT Type: %s", coordinator.NATType())
  102. t.Logf("Port Mapping Types: %s", coordinator.PortMappingTypes())
  103. }