lan_test.go 1.1 KB

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