lan_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright (C) 2017 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 connections
  7. import (
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/config"
  10. "github.com/syncthing/syncthing/lib/events"
  11. )
  12. func TestIsLANHost(t *testing.T) {
  13. cases := []struct {
  14. addr string
  15. lan bool
  16. }{
  17. // loopback
  18. {"127.0.0.1:22000", true},
  19. {"127.0.0.1", true},
  20. // local nets
  21. {"10.20.30.40:22000", true},
  22. {"10.20.30.40", true},
  23. // neither
  24. {"192.0.2.1:22000", false},
  25. {"192.0.2.1", false},
  26. // doesn't resolve
  27. {"[banana::phone]:hello", false},
  28. {"„‹›fl´fi·‰ˇ¨Á˝", false},
  29. }
  30. cfg := config.Wrap("/dev/null", config.Configuration{
  31. Options: config.OptionsConfiguration{
  32. AlwaysLocalNets: []string{"10.20.30.0/24"},
  33. },
  34. }, events.NoopLogger)
  35. s := &service{cfg: cfg}
  36. for _, tc := range cases {
  37. res := s.isLANHost(tc.addr)
  38. if res != tc.lan {
  39. t.Errorf("isLANHost(%q) => %v, expected %v", tc.addr, res, tc.lan)
  40. }
  41. }
  42. }