lan_test.go 1.2 KB

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