syncthing/lib/connections/lan_test.go
Jakob Borg 9b660c1959
lib/config, lib/connections: Configurable protocol priority (ref #8626) (#8868)
This makes the various protocol priorities configurable among the other
options. With this, it's possible to prefer QUIC over TCP for WAN
connections, for example. Both sides need to be similarly configured for
this to work properly.

The default priority order remains the same as previously (TCP, QUIC,
Relay, with LAN better than WAN).

To make this happen I made each dialer & listener more priority aware,
and moved the check for whether a connection is LAN or not into the
dialer / listener -- this is the new "lanChecker" type that's passed
around.
2023-04-16 14:54:28 +02:00

50 lines
1.2 KiB
Go

// Copyright (C) 2017 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package connections
import (
"testing"
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/protocol"
)
func TestIsLANHost(t *testing.T) {
cases := []struct {
addr string
lan bool
}{
// loopback
{"127.0.0.1:22000", true},
{"127.0.0.1", true},
// local nets
{"10.20.30.40:22000", true},
{"10.20.30.40", true},
// neither
{"192.0.2.1:22000", false},
{"192.0.2.1", false},
// doesn't resolve
{"[banana::phone]:hello", false},
{"„‹›fl´fi·‰ˇ¨Á˝", false},
}
cfg := config.Wrap("/dev/null", config.Configuration{
Options: config.OptionsConfiguration{
AlwaysLocalNets: []string{"10.20.30.0/24"},
},
}, protocol.LocalDeviceID, events.NoopLogger)
s := &lanChecker{cfg: cfg}
for _, tc := range cases {
res := s.isLANHost(tc.addr)
if res != tc.lan {
t.Errorf("isLANHost(%q) => %v, expected %v", tc.addr, res, tc.lan)
}
}
}