structs_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package nat
  7. import (
  8. "net"
  9. "testing"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. func TestMappingValidGateway(t *testing.T) {
  13. a := net.ParseIP("10.0.0.1")
  14. b := net.ParseIP("192.168.0.1")
  15. tests := []struct {
  16. mappingLocalIP net.IP
  17. gatewayLocalIP net.IP
  18. expected bool
  19. }{
  20. // Any of the IPs is nil or unspecified implies correct
  21. {nil, nil, true},
  22. {net.IPv4zero, net.IPv4zero, true},
  23. {nil, net.IPv4zero, true},
  24. {net.IPv4zero, nil, true},
  25. {a, nil, true},
  26. {b, nil, true},
  27. {a, net.IPv4zero, true},
  28. {b, net.IPv4zero, true},
  29. {nil, a, true},
  30. {nil, b, true},
  31. {net.IPv4zero, a, true},
  32. {net.IPv4zero, b, true},
  33. // IPs are the same implies correct
  34. {a, a, true},
  35. {b, b, true},
  36. // IPs are specified and different, implies incorrect
  37. {a, b, false},
  38. {b, a, false},
  39. }
  40. for _, test := range tests {
  41. m := Mapping{
  42. address: Address{
  43. IP: test.mappingLocalIP,
  44. },
  45. }
  46. result := m.validGateway(test.gatewayLocalIP)
  47. if result != test.expected {
  48. t.Errorf("Incorrect: local %s gateway %s result %t expected %t", test.mappingLocalIP, test.gatewayLocalIP, result, test.expected)
  49. }
  50. }
  51. }
  52. func TestMappingClearAddresses(t *testing.T) {
  53. natSvc := NewService(protocol.EmptyDeviceID, nil)
  54. // Mock a mapped port; avoids the need to actually map a port
  55. ip := net.ParseIP("192.168.0.1")
  56. m := natSvc.NewMapping(TCP, ip, 1024)
  57. m.extAddresses["test"] = Address{
  58. IP: ip,
  59. Port: 1024,
  60. }
  61. // Now try and remove the mapped port; prior to #4829 this deadlocked
  62. natSvc.RemoveMapping(m)
  63. }