structs_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 http://mozilla.org/MPL/2.0/.
  6. package nat
  7. import (
  8. "net"
  9. "testing"
  10. )
  11. func TestMappingValidGateway(t *testing.T) {
  12. a := net.ParseIP("10.0.0.1")
  13. b := net.ParseIP("192.168.0.1")
  14. tests := []struct {
  15. mappingLocalIP net.IP
  16. gatewayLocalIP net.IP
  17. expected bool
  18. }{
  19. // Any of the IPs is nil or unspecified implies correct
  20. {nil, nil, true},
  21. {net.IPv4zero, net.IPv4zero, true},
  22. {nil, net.IPv4zero, true},
  23. {net.IPv4zero, nil, true},
  24. {a, nil, true},
  25. {b, nil, true},
  26. {a, net.IPv4zero, true},
  27. {b, net.IPv4zero, true},
  28. {nil, a, true},
  29. {nil, b, true},
  30. {net.IPv4zero, a, true},
  31. {net.IPv4zero, b, true},
  32. // IPs are the same implies correct
  33. {a, a, true},
  34. {b, b, true},
  35. // IPs are specified and different, implies incorrect
  36. {a, b, false},
  37. {b, a, false},
  38. }
  39. for _, test := range tests {
  40. m := Mapping{
  41. address: Address{
  42. IP: test.mappingLocalIP,
  43. },
  44. }
  45. result := m.validGateway(test.gatewayLocalIP)
  46. if result != test.expected {
  47. t.Errorf("Incorrect: local %s gateway %s result %t expected %t", test.mappingLocalIP, test.gatewayLocalIP, result, test.expected)
  48. }
  49. }
  50. }