2016-07-04 12:53:11 +00:00
|
|
|
// Copyright (C) 2016 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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2016-07-04 12:53:11 +00:00
|
|
|
|
2016-07-04 11:16:48 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
2017-11-17 09:12:35 +00:00
|
|
|
"bytes"
|
2020-11-17 12:19:04 +00:00
|
|
|
"context"
|
2022-05-04 16:43:00 +00:00
|
|
|
"fmt"
|
2016-07-04 11:16:48 +00:00
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2016-07-04 11:16:48 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
)
|
|
|
|
|
2017-11-17 09:12:35 +00:00
|
|
|
func TestLocalInstanceID(t *testing.T) {
|
2019-08-15 14:29:37 +00:00
|
|
|
c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{}, events.NoopLogger)
|
2016-07-04 11:16:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-11-17 12:19:04 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go c.Serve(ctx)
|
|
|
|
defer cancel()
|
2016-07-04 11:16:48 +00:00
|
|
|
|
|
|
|
lc := c.(*localClient)
|
|
|
|
|
2017-11-17 09:12:35 +00:00
|
|
|
p0, ok := lc.announcementPkt(1, nil)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("unexpectedly not ok")
|
|
|
|
}
|
|
|
|
p1, ok := lc.announcementPkt(2, nil)
|
|
|
|
if !ok {
|
|
|
|
t.Fatal("unexpectedly not ok")
|
|
|
|
}
|
|
|
|
if bytes.Equal(p0, p1) {
|
2016-07-04 11:16:48 +00:00
|
|
|
t.Error("each generated packet should have a new instance id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLocalInstanceIDShouldTriggerNew(t *testing.T) {
|
2019-08-15 14:29:37 +00:00
|
|
|
c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{}, events.NoopLogger)
|
2016-07-04 11:16:48 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lc := c.(*localClient)
|
|
|
|
src := &net.UDPAddr{IP: []byte{10, 20, 30, 40}, Port: 50}
|
|
|
|
|
|
|
|
new := lc.registerDevice(src, Announce{
|
2016-10-29 21:56:24 +00:00
|
|
|
ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
2016-07-04 11:16:48 +00:00
|
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
|
|
InstanceID: 1234567890,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !new {
|
|
|
|
t.Fatal("first register should be new")
|
|
|
|
}
|
|
|
|
|
|
|
|
new = lc.registerDevice(src, Announce{
|
2016-10-29 21:56:24 +00:00
|
|
|
ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
2016-07-04 11:16:48 +00:00
|
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
|
|
InstanceID: 1234567890,
|
|
|
|
})
|
|
|
|
|
|
|
|
if new {
|
|
|
|
t.Fatal("second register should not be new")
|
|
|
|
}
|
|
|
|
|
|
|
|
new = lc.registerDevice(src, Announce{
|
2016-10-29 21:56:24 +00:00
|
|
|
ID: protocol.DeviceID{42, 10, 20, 30, 40, 50, 60, 70, 80, 90},
|
2016-07-04 11:16:48 +00:00
|
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
|
|
InstanceID: 1234567890,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !new {
|
|
|
|
t.Fatal("new device ID should be new")
|
|
|
|
}
|
|
|
|
|
|
|
|
new = lc.registerDevice(src, Announce{
|
2016-10-29 21:56:24 +00:00
|
|
|
ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
2016-07-04 11:16:48 +00:00
|
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
|
|
InstanceID: 91234567890,
|
|
|
|
})
|
|
|
|
|
|
|
|
if !new {
|
|
|
|
t.Fatal("new instance ID should be new")
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 16:43:00 +00:00
|
|
|
|
2023-09-12 12:28:17 +00:00
|
|
|
func TestFilterUndialable(t *testing.T) {
|
2022-05-04 16:43:00 +00:00
|
|
|
addrs := []string{
|
|
|
|
"quic://[2001:db8::1]:22000", // OK
|
|
|
|
"tcp://192.0.2.42:22000", // OK
|
|
|
|
"quic://[2001:db8::1]:0", // remove, port zero
|
|
|
|
"tcp://192.0.2.42:0", // remove, port zero
|
2023-09-12 12:28:17 +00:00
|
|
|
"quic://[::]:22000", // OK
|
|
|
|
"tcp://0.0.0.0:22000", // OK
|
2022-05-04 16:43:00 +00:00
|
|
|
"tcp://[2001:db8::1]", // remove, no port
|
|
|
|
"tcp://192.0.2.42", // remove, no port
|
|
|
|
"tcp://foo:bar", // remove, host/port does not resolve
|
|
|
|
"tcp://127.0.0.1:22000", // remove, not usable from outside
|
|
|
|
"tcp://[::1]:22000", // remove, not usable from outside
|
|
|
|
"tcp://224.1.2.3:22000", // remove, not usable from outside (multicast)
|
|
|
|
"tcp://[fe80::9ef:dff1:b332:5e56]:55681", // OK
|
|
|
|
"pure garbage", // remove, garbage
|
|
|
|
"", // remove, garbage
|
|
|
|
}
|
|
|
|
exp := []string{
|
|
|
|
"quic://[2001:db8::1]:22000",
|
|
|
|
"tcp://192.0.2.42:22000",
|
2023-09-12 12:28:17 +00:00
|
|
|
"quic://[::]:22000",
|
|
|
|
"tcp://0.0.0.0:22000",
|
2022-05-04 16:43:00 +00:00
|
|
|
"tcp://[fe80::9ef:dff1:b332:5e56]:55681",
|
|
|
|
}
|
2023-09-12 12:28:17 +00:00
|
|
|
res := filterUndialableLocal(addrs)
|
2022-05-04 16:43:00 +00:00
|
|
|
if fmt.Sprint(res) != fmt.Sprint(exp) {
|
|
|
|
t.Log(res)
|
2023-09-12 12:28:17 +00:00
|
|
|
t.Error("filterUndialableLocal returned invalid addresses")
|
2022-05-04 16:43:00 +00:00
|
|
|
}
|
|
|
|
}
|