discovery_test.go 3.3 KB

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