discovery_test.go 3.4 KB

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