mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-06 05:17:49 +00:00
7ebf58f1bc
This makes it OK to not have any listeners working. Specifically, - We don't complain about an empty listener address - We don't complain about not having anything to announce to global discovery servers - We don't send local discovery packets when there is nothing to announce. The last point also fixes a thing where the list of addresses for local discovery was set at startup time and never refreshed. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4517
89 lines
2.0 KiB
Go
89 lines
2.0 KiB
Go
// 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,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package discover
|
|
|
|
import (
|
|
"bytes"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
)
|
|
|
|
func TestLocalInstanceID(t *testing.T) {
|
|
c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
go c.Serve()
|
|
defer c.Stop()
|
|
|
|
lc := c.(*localClient)
|
|
|
|
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) {
|
|
t.Error("each generated packet should have a new instance id")
|
|
}
|
|
}
|
|
|
|
func TestLocalInstanceIDShouldTriggerNew(t *testing.T) {
|
|
c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{})
|
|
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{
|
|
ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
InstanceID: 1234567890,
|
|
})
|
|
|
|
if !new {
|
|
t.Fatal("first register should be new")
|
|
}
|
|
|
|
new = lc.registerDevice(src, Announce{
|
|
ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
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{
|
|
ID: protocol.DeviceID{42, 10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
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{
|
|
ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
|
|
Addresses: []string{"tcp://0.0.0.0:22000"},
|
|
InstanceID: 91234567890,
|
|
})
|
|
|
|
if !new {
|
|
t.Fatal("new instance ID should be new")
|
|
}
|
|
}
|