2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-05-15 00:18:09 +00:00
|
|
|
package config
|
2014-03-04 21:17:28 +00:00
|
|
|
|
|
|
|
import (
|
2015-04-05 15:36:52 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-10-11 15:55:11 +00:00
|
|
|
"fmt"
|
2017-11-26 07:51:22 +00:00
|
|
|
"io/ioutil"
|
2014-04-22 10:06:32 +00:00
|
|
|
"os"
|
2015-04-05 20:52:22 +00:00
|
|
|
"path/filepath"
|
2016-05-17 00:05:38 +00:00
|
|
|
"reflect"
|
2015-01-22 23:34:22 +00:00
|
|
|
"runtime"
|
2016-05-17 00:05:38 +00:00
|
|
|
"sort"
|
2015-04-05 20:52:22 +00:00
|
|
|
"strings"
|
2014-03-04 21:17:28 +00:00
|
|
|
"testing"
|
2014-06-07 03:10:15 +00:00
|
|
|
|
2016-03-06 20:32:10 +00:00
|
|
|
"github.com/d4l3k/messagediff"
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2017-11-26 07:51:22 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2014-03-04 21:17:28 +00:00
|
|
|
)
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
var device1, device2, device3, device4 protocol.DeviceID
|
2014-06-29 23:42:03 +00:00
|
|
|
|
|
|
|
func init() {
|
2014-09-28 11:00:38 +00:00
|
|
|
device1, _ = protocol.DeviceIDFromString("AIR6LPZ7K4PTTUXQSMUUCPQ5YWOEDFIIQJUG7772YQXXR5YD6AWQ")
|
|
|
|
device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
|
|
|
|
device3, _ = protocol.DeviceIDFromString("LGFPDIT-7SKNNJL-VJZA4FC-7QNCRKA-CE753K7-2BW5QDK-2FOZ7FR-FEP57QJ")
|
|
|
|
device4, _ = protocol.DeviceIDFromString("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2")
|
2014-06-29 23:42:03 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 21:17:28 +00:00
|
|
|
func TestDefaultValues(t *testing.T) {
|
|
|
|
expected := OptionsConfiguration{
|
2016-05-04 19:38:12 +00:00
|
|
|
ListenAddresses: []string{"default"},
|
2015-09-20 13:30:25 +00:00
|
|
|
GlobalAnnServers: []string{"default"},
|
2014-11-16 23:18:59 +00:00
|
|
|
GlobalAnnEnabled: true,
|
|
|
|
LocalAnnEnabled: true,
|
2015-08-27 14:04:13 +00:00
|
|
|
LocalAnnPort: 21027,
|
2015-08-23 12:59:38 +00:00
|
|
|
LocalAnnMCAddr: "[ff12::8384]:21027",
|
2014-11-16 23:18:59 +00:00
|
|
|
MaxSendKbps: 0,
|
|
|
|
MaxRecvKbps: 0,
|
|
|
|
ReconnectIntervalS: 60,
|
2016-05-17 00:05:38 +00:00
|
|
|
RelaysEnabled: true,
|
2015-06-28 20:09:03 +00:00
|
|
|
RelayReconnectIntervalM: 10,
|
2014-11-16 23:18:59 +00:00
|
|
|
StartBrowser: true,
|
2016-04-10 19:36:38 +00:00
|
|
|
NATEnabled: true,
|
|
|
|
NATLeaseM: 60,
|
|
|
|
NATRenewalM: 30,
|
|
|
|
NATTimeoutS: 10,
|
2014-11-16 23:18:59 +00:00
|
|
|
RestartOnWakeup: true,
|
|
|
|
AutoUpgradeIntervalH: 12,
|
|
|
|
KeepTemporariesH: 24,
|
2016-04-03 19:18:16 +00:00
|
|
|
CacheIgnoredFiles: false,
|
2014-11-16 23:18:59 +00:00
|
|
|
ProgressUpdateIntervalS: 5,
|
2015-03-08 18:36:59 +00:00
|
|
|
LimitBandwidthInLan: false,
|
2017-04-12 09:01:19 +00:00
|
|
|
MinHomeDiskFree: Size{1, "%"},
|
2015-09-10 12:08:40 +00:00
|
|
|
URURL: "https://data.syncthing.net/newdata",
|
|
|
|
URInitialDelayS: 1800,
|
|
|
|
URPostInsecurely: false,
|
2016-05-13 09:17:10 +00:00
|
|
|
ReleasesURL: "https://upgrades.syncthing.net/meta.json",
|
2015-10-27 11:04:51 +00:00
|
|
|
AlwaysLocalNets: []string{},
|
2016-05-12 08:23:18 +00:00
|
|
|
OverwriteRemoteDevNames: false,
|
2016-04-15 10:59:41 +00:00
|
|
|
TempIndexMinBlocks: 10,
|
2016-09-13 19:54:16 +00:00
|
|
|
UnackedNotificationIDs: []string{},
|
2017-02-06 10:27:11 +00:00
|
|
|
WeakHashSelectionMethod: WeakHashAuto,
|
2017-07-20 13:16:54 +00:00
|
|
|
DefaultFolderPath: "~",
|
2018-01-15 17:11:14 +00:00
|
|
|
SetLowPriority: true,
|
2014-03-04 21:17:28 +00:00
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
cfg := New(device1)
|
2014-03-04 21:17:28 +00:00
|
|
|
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, cfg.Options); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("Default config differs. Diff:\n%s", diff)
|
2014-03-04 21:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func TestDeviceConfig(t *testing.T) {
|
2015-04-04 19:49:15 +00:00
|
|
|
for i := OldestHandledVersion; i <= CurrentVersion; i++ {
|
2017-11-05 12:18:05 +00:00
|
|
|
os.RemoveAll(filepath.Join("testdata", DefaultMarkerName))
|
2014-10-11 15:55:11 +00:00
|
|
|
wr, err := Load(fmt.Sprintf("testdata/v%d.xml", i), device1)
|
2014-04-08 11:45:18 +00:00
|
|
|
if err != nil {
|
2014-10-06 07:25:45 +00:00
|
|
|
t.Fatal(err)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2014-10-11 15:55:11 +00:00
|
|
|
|
2017-11-05 12:18:05 +00:00
|
|
|
_, err = os.Stat(filepath.Join("testdata", DefaultMarkerName))
|
2014-10-11 15:55:11 +00:00
|
|
|
if i < 6 && err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if i >= 6 && err == nil {
|
|
|
|
t.Fatal("Unexpected file")
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
cfg := wr.cfg
|
2014-04-08 11:45:18 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
expectedFolders := []FolderConfiguration{
|
2014-04-08 11:45:18 +00:00
|
|
|
{
|
2017-10-20 14:52:55 +00:00
|
|
|
ID: "test",
|
|
|
|
FilesystemType: fs.FilesystemTypeBasic,
|
|
|
|
Path: "testdata",
|
|
|
|
Devices: []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
|
|
|
|
Type: FolderTypeSendOnly,
|
|
|
|
RescanIntervalS: 600,
|
|
|
|
FSWatcherEnabled: false,
|
|
|
|
FSWatcherDelayS: 10,
|
|
|
|
Copiers: 0,
|
|
|
|
Hashers: 0,
|
|
|
|
AutoNormalize: true,
|
|
|
|
MinDiskFree: Size{1, "%"},
|
|
|
|
MaxConflicts: -1,
|
2016-09-13 19:54:16 +00:00
|
|
|
Versioning: VersioningConfiguration{
|
|
|
|
Params: map[string]string{},
|
|
|
|
},
|
2017-01-04 21:04:13 +00:00
|
|
|
WeakHashThresholdPct: 25,
|
2017-11-05 12:18:05 +00:00
|
|
|
MarkerName: DefaultMarkerName,
|
2014-04-08 11:45:18 +00:00
|
|
|
},
|
|
|
|
}
|
2015-10-22 11:49:41 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
// The cachedFilesystem will have been resolved to an absolute path,
|
2015-11-05 08:31:36 +00:00
|
|
|
// depending on where the tests are running. Zero it out so we don't
|
|
|
|
// fail based on that.
|
|
|
|
for i := range cfg.Folders {
|
2017-08-19 14:36:56 +00:00
|
|
|
cfg.Folders[i].cachedFilesystem = nil
|
2015-10-22 11:49:41 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
expectedDevices := []DeviceConfiguration{
|
2014-04-08 11:45:18 +00:00
|
|
|
{
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device1,
|
|
|
|
Name: "node one",
|
|
|
|
Addresses: []string{"tcp://a"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
AllowedNetworks: []string{},
|
2014-04-08 11:45:18 +00:00
|
|
|
},
|
|
|
|
{
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device4,
|
|
|
|
Name: "node two",
|
|
|
|
Addresses: []string{"tcp://b"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
AllowedNetworks: []string{},
|
2014-04-08 11:45:18 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
expectedDeviceIDs := []protocol.DeviceID{device1, device4}
|
2014-04-08 11:45:18 +00:00
|
|
|
|
2014-10-11 15:55:11 +00:00
|
|
|
if cfg.Version != CurrentVersion {
|
|
|
|
t.Errorf("%d: Incorrect version %d != %d", i, cfg.Version, CurrentVersion)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expectedFolders, cfg.Folders); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("%d: Incorrect Folders. Diff:\n%s", i, diff)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expectedDevices, cfg.Devices); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("%d: Incorrect Devices. Diff:\n%s", i, diff)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expectedDeviceIDs, cfg.Folders[0].DeviceIDs()); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("%d: Incorrect DeviceIDs. Diff:\n%s", i, diff)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
func TestNoListenAddresses(t *testing.T) {
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg, err := Load("testdata/nolistenaddress.xml", device1)
|
2014-03-04 21:17:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{""}
|
2016-05-04 19:38:12 +00:00
|
|
|
actual := cfg.Options().ListenAddresses
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-05-04 19:38:12 +00:00
|
|
|
t.Errorf("Unexpected ListenAddresses. Diff:\n%s", diff)
|
2014-03-04 21:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverriddenValues(t *testing.T) {
|
|
|
|
expected := OptionsConfiguration{
|
2016-05-04 19:38:12 +00:00
|
|
|
ListenAddresses: []string{"tcp://:23000"},
|
2014-11-20 21:24:11 +00:00
|
|
|
GlobalAnnServers: []string{"udp4://syncthing.nym.se:22026"},
|
2014-11-16 23:18:59 +00:00
|
|
|
GlobalAnnEnabled: false,
|
|
|
|
LocalAnnEnabled: false,
|
|
|
|
LocalAnnPort: 42123,
|
|
|
|
LocalAnnMCAddr: "quux:3232",
|
|
|
|
MaxSendKbps: 1234,
|
|
|
|
MaxRecvKbps: 2341,
|
|
|
|
ReconnectIntervalS: 6000,
|
2016-05-17 00:05:38 +00:00
|
|
|
RelaysEnabled: false,
|
2015-06-28 20:09:03 +00:00
|
|
|
RelayReconnectIntervalM: 20,
|
2014-11-16 23:18:59 +00:00
|
|
|
StartBrowser: false,
|
2016-04-10 19:36:38 +00:00
|
|
|
NATEnabled: false,
|
|
|
|
NATLeaseM: 90,
|
|
|
|
NATRenewalM: 15,
|
|
|
|
NATTimeoutS: 15,
|
2014-11-16 23:18:59 +00:00
|
|
|
RestartOnWakeup: false,
|
|
|
|
AutoUpgradeIntervalH: 24,
|
|
|
|
KeepTemporariesH: 48,
|
2016-04-03 19:18:16 +00:00
|
|
|
CacheIgnoredFiles: true,
|
2014-11-16 23:18:59 +00:00
|
|
|
ProgressUpdateIntervalS: 10,
|
2015-03-08 18:36:59 +00:00
|
|
|
LimitBandwidthInLan: true,
|
2017-04-12 09:01:19 +00:00
|
|
|
MinHomeDiskFree: Size{5.2, "%"},
|
2017-10-12 06:16:46 +00:00
|
|
|
URSeen: 2,
|
2015-09-10 12:08:40 +00:00
|
|
|
URURL: "https://localhost/newdata",
|
|
|
|
URInitialDelayS: 800,
|
|
|
|
URPostInsecurely: true,
|
2015-09-10 12:16:44 +00:00
|
|
|
ReleasesURL: "https://localhost/releases",
|
2015-10-27 11:04:51 +00:00
|
|
|
AlwaysLocalNets: []string{},
|
2016-05-12 08:23:18 +00:00
|
|
|
OverwriteRemoteDevNames: true,
|
2016-04-15 10:59:41 +00:00
|
|
|
TempIndexMinBlocks: 100,
|
2017-02-04 09:45:17 +00:00
|
|
|
UnackedNotificationIDs: []string{
|
2018-03-25 20:05:47 +00:00
|
|
|
"channelNotification", // added in 17->18 migration
|
|
|
|
"fsWatcherNotification", // added in 27->28 migration
|
2017-02-04 09:45:17 +00:00
|
|
|
},
|
2017-02-06 10:27:11 +00:00
|
|
|
WeakHashSelectionMethod: WeakHashNever,
|
2017-07-20 13:16:54 +00:00
|
|
|
DefaultFolderPath: "/media/syncthing",
|
2018-01-15 17:11:14 +00:00
|
|
|
SetLowPriority: false,
|
2014-03-04 21:17:28 +00:00
|
|
|
}
|
|
|
|
|
2017-02-04 09:45:17 +00:00
|
|
|
os.Unsetenv("STNOUPGRADE")
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg, err := Load("testdata/overridenvalues.xml", device1)
|
2014-03-04 21:17:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, cfg.Options()); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("Overridden config differs. Diff:\n%s", diff)
|
2014-03-04 21:17:28 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-22 09:46:08 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func TestDeviceAddressesDynamic(t *testing.T) {
|
2015-02-23 08:44:10 +00:00
|
|
|
name, _ := os.Hostname()
|
|
|
|
expected := map[protocol.DeviceID]DeviceConfiguration{
|
|
|
|
device1: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device1,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
AllowedNetworks: []string{},
|
2015-02-23 08:44:10 +00:00
|
|
|
},
|
|
|
|
device2: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device2,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
AllowedNetworks: []string{},
|
2015-02-23 08:44:10 +00:00
|
|
|
},
|
|
|
|
device3: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device3,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
AllowedNetworks: []string{},
|
2015-02-23 08:44:10 +00:00
|
|
|
},
|
|
|
|
device4: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device4,
|
|
|
|
Name: name, // Set when auto created
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
AllowedNetworks: []string{},
|
2015-02-23 08:44:10 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := Load("testdata/deviceaddressesdynamic.xml", device4)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := cfg.Devices()
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("Devices differ. Diff:\n%s", diff)
|
2015-02-23 08:44:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeviceCompression(t *testing.T) {
|
2014-04-22 10:06:32 +00:00
|
|
|
name, _ := os.Hostname()
|
2014-10-06 07:25:45 +00:00
|
|
|
expected := map[protocol.DeviceID]DeviceConfiguration{
|
|
|
|
device1: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device1,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
AllowedNetworks: []string{},
|
2014-04-22 09:46:08 +00:00
|
|
|
},
|
2014-10-06 07:25:45 +00:00
|
|
|
device2: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device2,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
AllowedNetworks: []string{},
|
2014-04-22 09:46:08 +00:00
|
|
|
},
|
2014-10-06 07:25:45 +00:00
|
|
|
device3: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device3,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressNever,
|
|
|
|
AllowedNetworks: []string{},
|
2014-04-22 09:46:08 +00:00
|
|
|
},
|
2014-10-06 07:25:45 +00:00
|
|
|
device4: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device4,
|
|
|
|
Name: name, // Set when auto created
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
AllowedNetworks: []string{},
|
2014-04-22 09:46:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-02-23 08:44:10 +00:00
|
|
|
cfg, err := Load("testdata/devicecompression.xml", device4)
|
2014-04-22 09:46:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
actual := cfg.Devices()
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("Devices differ. Diff:\n%s", diff)
|
2014-04-22 09:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-24 11:23:17 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func TestDeviceAddressesStatic(t *testing.T) {
|
2014-07-24 11:23:17 +00:00
|
|
|
name, _ := os.Hostname()
|
2014-10-06 07:25:45 +00:00
|
|
|
expected := map[protocol.DeviceID]DeviceConfiguration{
|
|
|
|
device1: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device1,
|
|
|
|
Addresses: []string{"tcp://192.0.2.1", "tcp://192.0.2.2"},
|
|
|
|
AllowedNetworks: []string{},
|
2014-07-24 11:23:17 +00:00
|
|
|
},
|
2014-10-06 07:25:45 +00:00
|
|
|
device2: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device2,
|
|
|
|
Addresses: []string{"tcp://192.0.2.3:6070", "tcp://[2001:db8::42]:4242"},
|
|
|
|
AllowedNetworks: []string{},
|
2014-07-24 11:23:17 +00:00
|
|
|
},
|
2014-10-06 07:25:45 +00:00
|
|
|
device3: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device3,
|
|
|
|
Addresses: []string{"tcp://[2001:db8::44]:4444", "tcp://192.0.2.4:6090"},
|
|
|
|
AllowedNetworks: []string{},
|
2014-07-24 11:23:17 +00:00
|
|
|
},
|
2014-10-06 07:25:45 +00:00
|
|
|
device4: {
|
2017-04-01 09:52:31 +00:00
|
|
|
DeviceID: device4,
|
|
|
|
Name: name, // Set when auto created
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
AllowedNetworks: []string{},
|
2014-07-24 11:23:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg, err := Load("testdata/deviceaddressesstatic.xml", device4)
|
2014-07-24 11:23:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
actual := cfg.Devices()
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, actual); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("Devices differ. Diff:\n%s", diff)
|
2014-07-24 11:23:17 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-31 12:13:55 +00:00
|
|
|
|
|
|
|
func TestVersioningConfig(t *testing.T) {
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg, err := Load("testdata/versioningconfig.xml", device4)
|
2014-07-31 12:13:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
vc := cfg.Folders()["test"].Versioning
|
2014-07-31 12:13:55 +00:00
|
|
|
if vc.Type != "simple" {
|
|
|
|
t.Errorf(`vc.Type %q != "simple"`, vc.Type)
|
|
|
|
}
|
|
|
|
if l := len(vc.Params); l != 2 {
|
|
|
|
t.Errorf("len(vc.Params) %d != 2", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
"baz": "quux",
|
|
|
|
}
|
2016-03-17 07:03:29 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(expected, vc.Params); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("vc.Params differ. Diff:\n%s", diff)
|
2014-07-31 12:13:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-06 14:50:06 +00:00
|
|
|
|
2015-01-22 23:34:22 +00:00
|
|
|
func TestIssue1262(t *testing.T) {
|
2017-08-19 14:36:56 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skipf("path gets converted to absolute as part of the filesystem initialization on linux")
|
|
|
|
}
|
|
|
|
|
2015-01-22 23:34:22 +00:00
|
|
|
cfg, err := Load("testdata/issue-1262.xml", device4)
|
|
|
|
if err != nil {
|
2015-04-05 20:52:22 +00:00
|
|
|
t.Fatal(err)
|
2015-01-22 23:34:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
actual := cfg.Folders()["test"].Filesystem().URI()
|
|
|
|
expected := `e:\`
|
2015-01-22 23:34:22 +00:00
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("%q != %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 15:48:41 +00:00
|
|
|
func TestIssue1750(t *testing.T) {
|
|
|
|
cfg, err := Load("testdata/issue-1750.xml", device4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
if cfg.Options().ListenAddresses[0] != "tcp://:23000" {
|
|
|
|
t.Errorf("%q != %q", cfg.Options().ListenAddresses[0], "tcp://:23000")
|
2015-06-25 15:48:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
if cfg.Options().ListenAddresses[1] != "tcp://:23001" {
|
|
|
|
t.Errorf("%q != %q", cfg.Options().ListenAddresses[1], "tcp://:23001")
|
2015-06-25 15:48:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 13:43:35 +00:00
|
|
|
if cfg.Options().GlobalAnnServers[0] != "udp4://syncthing.nym.se:22026" {
|
|
|
|
t.Errorf("%q != %q", cfg.Options().GlobalAnnServers[0], "udp4://syncthing.nym.se:22026")
|
2015-06-25 15:48:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 13:43:35 +00:00
|
|
|
if cfg.Options().GlobalAnnServers[1] != "udp4://syncthing.nym.se:22027" {
|
|
|
|
t.Errorf("%q != %q", cfg.Options().GlobalAnnServers[1], "udp4://syncthing.nym.se:22027")
|
2015-06-25 15:48:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 20:52:22 +00:00
|
|
|
func TestFolderPath(t *testing.T) {
|
|
|
|
folder := FolderConfiguration{
|
2017-08-19 14:36:56 +00:00
|
|
|
Path: "~/tmp",
|
2015-04-05 20:52:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
realPath := folder.Filesystem().URI()
|
2015-04-05 20:52:22 +00:00
|
|
|
if !filepath.IsAbs(realPath) {
|
|
|
|
t.Error(realPath, "should be absolute")
|
|
|
|
}
|
|
|
|
if strings.Contains(realPath, "~") {
|
|
|
|
t.Error(realPath, "should not contain ~")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:51:22 +00:00
|
|
|
func TestFolderCheckPath(t *testing.T) {
|
|
|
|
n, err := ioutil.TempDir("", "")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.MkdirAll(filepath.Join(n, "dir", ".stfolder"), os.FileMode(0777))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testcases := []struct {
|
|
|
|
path string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
path: "",
|
2018-03-18 00:42:31 +00:00
|
|
|
err: ErrMarkerMissing,
|
2017-11-26 07:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "does not exist",
|
2018-03-18 00:42:31 +00:00
|
|
|
err: ErrPathMissing,
|
2017-11-26 07:51:22 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
path: "dir",
|
|
|
|
err: nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = osutil.DebugSymlinkForTestsOnly(filepath.Join(n, "dir"), filepath.Join(n, "link"))
|
|
|
|
if err == nil {
|
|
|
|
t.Log("running with symlink check")
|
|
|
|
testcases = append(testcases, struct {
|
|
|
|
path string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
path: "link",
|
|
|
|
err: nil,
|
|
|
|
})
|
|
|
|
} else if runtime.GOOS != "windows" {
|
|
|
|
t.Log("running without symlink check")
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testcase := range testcases {
|
|
|
|
cfg := FolderConfiguration{
|
|
|
|
Path: filepath.Join(n, testcase.path),
|
|
|
|
MarkerName: DefaultMarkerName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cfg.CheckPath(); testcase.err != err {
|
|
|
|
t.Errorf("unexpected error in case %s: %s != %s", testcase.path, err, testcase.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 14:50:06 +00:00
|
|
|
func TestNewSaveLoad(t *testing.T) {
|
|
|
|
path := "testdata/temp.xml"
|
|
|
|
os.Remove(path)
|
|
|
|
|
|
|
|
exists := func(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
intCfg := New(device1)
|
|
|
|
cfg := Wrap(path, intCfg)
|
2014-09-06 14:50:06 +00:00
|
|
|
|
|
|
|
// To make the equality pass later
|
2014-10-06 07:25:45 +00:00
|
|
|
cfg.cfg.XMLName.Local = "configuration"
|
2014-09-06 14:50:06 +00:00
|
|
|
|
|
|
|
if exists(path) {
|
|
|
|
t.Error(path, "exists")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cfg.Save()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if !exists(path) {
|
|
|
|
t.Error(path, "does not exist")
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg2, err := Load(path, device1)
|
2014-09-06 14:50:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:34:18 +00:00
|
|
|
if diff, equal := messagediff.PrettyDiff(cfg.RawCopy(), cfg2.RawCopy()); !equal {
|
2016-03-06 20:32:10 +00:00
|
|
|
t.Errorf("Configs are not equal. Diff:\n%s", diff)
|
2014-09-06 14:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepare(t *testing.T) {
|
|
|
|
var cfg Configuration
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
if cfg.Folders != nil || cfg.Devices != nil || cfg.Options.ListenAddresses != nil {
|
2014-09-06 14:50:06 +00:00
|
|
|
t.Error("Expected nil")
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg.prepare(device1)
|
2014-09-06 14:50:06 +00:00
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
if cfg.Folders == nil || cfg.Devices == nil || cfg.Options.ListenAddresses == nil {
|
2014-09-06 14:50:06 +00:00
|
|
|
t.Error("Unexpected nil")
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 08:46:33 +00:00
|
|
|
|
2015-04-05 15:36:52 +00:00
|
|
|
func TestCopy(t *testing.T) {
|
|
|
|
wrapper, err := Load("testdata/example.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-11-12 09:34:18 +00:00
|
|
|
cfg := wrapper.RawCopy()
|
2015-04-05 15:36:52 +00:00
|
|
|
|
|
|
|
bsOrig, err := json.MarshalIndent(cfg, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
copy := cfg.Copy()
|
|
|
|
|
|
|
|
cfg.Devices[0].Addresses[0] = "wrong"
|
|
|
|
cfg.Folders[0].Devices[0].DeviceID = protocol.DeviceID{0, 1, 2, 3}
|
2016-05-04 19:38:12 +00:00
|
|
|
cfg.Options.ListenAddresses[0] = "wrong"
|
2016-02-02 10:12:25 +00:00
|
|
|
cfg.GUI.APIKey = "wrong"
|
2015-04-05 15:36:52 +00:00
|
|
|
|
|
|
|
bsChanged, err := json.MarshalIndent(cfg, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bsCopy, err := json.MarshalIndent(copy, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-03-31 15:12:46 +00:00
|
|
|
if bytes.Equal(bsOrig, bsChanged) {
|
2015-04-05 15:36:52 +00:00
|
|
|
t.Error("Config should have changed")
|
|
|
|
}
|
2016-03-31 15:12:46 +00:00
|
|
|
if !bytes.Equal(bsOrig, bsCopy) {
|
2015-04-05 15:36:52 +00:00
|
|
|
//ioutil.WriteFile("a", bsOrig, 0644)
|
|
|
|
//ioutil.WriteFile("b", bsCopy, 0644)
|
|
|
|
t.Error("Copy should be unchanged")
|
|
|
|
}
|
|
|
|
}
|
2015-04-25 05:13:53 +00:00
|
|
|
|
|
|
|
func TestPullOrder(t *testing.T) {
|
|
|
|
wrapper, err := Load("testdata/pullorder.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
folders := wrapper.Folders()
|
|
|
|
|
|
|
|
expected := []struct {
|
|
|
|
name string
|
|
|
|
order PullOrder
|
|
|
|
}{
|
|
|
|
{"f1", OrderRandom}, // empty value, default
|
|
|
|
{"f2", OrderRandom}, // explicit
|
|
|
|
{"f3", OrderAlphabetic}, // explicit
|
|
|
|
{"f4", OrderRandom}, // unknown value, default
|
|
|
|
{"f5", OrderSmallestFirst}, // explicit
|
|
|
|
{"f6", OrderLargestFirst}, // explicit
|
|
|
|
{"f7", OrderOldestFirst}, // explicit
|
|
|
|
{"f8", OrderNewestFirst}, // explicit
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify values are deserialized correctly
|
|
|
|
|
|
|
|
for _, tc := range expected {
|
|
|
|
if actual := folders[tc.name].Order; actual != tc.order {
|
|
|
|
t.Errorf("Incorrect pull order for %q: %v != %v", tc.name, actual, tc.order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize and deserialize again to verify it survives the transformation
|
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
2016-11-12 09:34:18 +00:00
|
|
|
cfg := wrapper.RawCopy()
|
2015-04-25 05:13:53 +00:00
|
|
|
cfg.WriteXML(buf)
|
|
|
|
|
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
|
|
|
|
cfg, err = ReadXML(buf, device1)
|
2016-07-15 14:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-04-25 05:13:53 +00:00
|
|
|
wrapper = Wrap("testdata/pullorder.xml", cfg)
|
|
|
|
folders = wrapper.Folders()
|
|
|
|
|
|
|
|
for _, tc := range expected {
|
|
|
|
if actual := folders[tc.name].Order; actual != tc.order {
|
|
|
|
t.Errorf("Incorrect pull order for %q: %v != %v", tc.name, actual, tc.order)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-06-20 18:21:05 +00:00
|
|
|
|
|
|
|
func TestLargeRescanInterval(t *testing.T) {
|
|
|
|
wrapper, err := Load("testdata/largeinterval.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if wrapper.Folders()["l1"].RescanIntervalS != MaxRescanIntervalS {
|
|
|
|
t.Error("too large rescan interval should be maxed out")
|
|
|
|
}
|
|
|
|
if wrapper.Folders()["l2"].RescanIntervalS != 0 {
|
|
|
|
t.Error("negative rescan interval should become zero")
|
|
|
|
}
|
|
|
|
}
|
2015-10-12 13:27:57 +00:00
|
|
|
|
|
|
|
func TestGUIConfigURL(t *testing.T) {
|
|
|
|
testcases := [][2]string{
|
|
|
|
{"192.0.2.42:8080", "http://192.0.2.42:8080/"},
|
|
|
|
{":8080", "http://127.0.0.1:8080/"},
|
|
|
|
{"0.0.0.0:8080", "http://127.0.0.1:8080/"},
|
|
|
|
{"127.0.0.1:8080", "http://127.0.0.1:8080/"},
|
|
|
|
{"127.0.0.2:8080", "http://127.0.0.2:8080/"},
|
|
|
|
{"[::]:8080", "http://[::1]:8080/"},
|
|
|
|
{"[2001::42]:8080", "http://[2001::42]:8080/"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testcases {
|
|
|
|
c := GUIConfiguration{
|
|
|
|
RawAddress: tc[0],
|
|
|
|
}
|
|
|
|
u := c.URL()
|
|
|
|
if u != tc[1] {
|
|
|
|
t.Errorf("Incorrect URL %s != %s for addr %s", u, tc[1], tc[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-31 08:17:17 +00:00
|
|
|
|
2016-07-02 19:38:39 +00:00
|
|
|
func TestDuplicateDevices(t *testing.T) {
|
|
|
|
// Duplicate devices should be removed
|
|
|
|
|
|
|
|
wrapper, err := Load("testdata/dupdevices.xml", device1)
|
2015-12-31 08:17:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:34:18 +00:00
|
|
|
if l := len(wrapper.RawCopy().Devices); l != 3 {
|
2015-12-31 08:17:17 +00:00
|
|
|
t.Errorf("Incorrect number of devices, %d != 3", l)
|
|
|
|
}
|
|
|
|
|
|
|
|
f := wrapper.Folders()["f2"]
|
|
|
|
if l := len(f.Devices); l != 2 {
|
|
|
|
t.Errorf("Incorrect number of folder devices, %d != 2", l)
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 00:05:38 +00:00
|
|
|
|
2016-07-02 19:38:39 +00:00
|
|
|
func TestDuplicateFolders(t *testing.T) {
|
|
|
|
// Duplicate folders are a loading error
|
|
|
|
|
|
|
|
_, err := Load("testdata/dupfolders.xml", device1)
|
|
|
|
if err == nil || !strings.HasPrefix(err.Error(), "duplicate folder ID") {
|
|
|
|
t.Fatal(`Expected error to mention "duplicate folder ID":`, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyFolderPaths(t *testing.T) {
|
|
|
|
// Empty folder paths are allowed at the loading stage, and should not
|
|
|
|
// get messed up by the prepare steps (e.g., become the current dir or
|
|
|
|
// get a slash added so that it becomes the root directory or similar).
|
|
|
|
|
|
|
|
wrapper, err := Load("testdata/nopath.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
folder := wrapper.Folders()["f1"]
|
2017-08-19 14:36:56 +00:00
|
|
|
if folder.cachedFilesystem != nil {
|
|
|
|
t.Errorf("Expected %q to be empty", folder.cachedFilesystem)
|
2016-07-02 19:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 00:05:38 +00:00
|
|
|
func TestV14ListenAddressesMigration(t *testing.T) {
|
|
|
|
tcs := [][3][]string{
|
|
|
|
|
|
|
|
// Default listen plus default relays is now "default"
|
|
|
|
{
|
|
|
|
{"tcp://0.0.0.0:22000"},
|
|
|
|
{"dynamic+https://relays.syncthing.net/endpoint"},
|
|
|
|
{"default"},
|
|
|
|
},
|
|
|
|
// Default listen address without any relay addresses gets converted
|
|
|
|
// to just the listen address. It's easier this way, and frankly the
|
|
|
|
// user has gone to some trouble to get the empty string in the
|
|
|
|
// config to start with...
|
|
|
|
{
|
|
|
|
{"tcp://0.0.0.0:22000"}, // old listen addrs
|
|
|
|
{""}, // old relay addrs
|
|
|
|
{"tcp://0.0.0.0:22000"}, // new listen addrs
|
|
|
|
},
|
|
|
|
// Default listen plus non-default relays gets copied verbatim
|
|
|
|
{
|
|
|
|
{"tcp://0.0.0.0:22000"},
|
|
|
|
{"dynamic+https://other.example.com"},
|
|
|
|
{"tcp://0.0.0.0:22000", "dynamic+https://other.example.com"},
|
|
|
|
},
|
|
|
|
// Non-default listen plus default relays gets copied verbatim
|
|
|
|
{
|
|
|
|
{"tcp://1.2.3.4:22000"},
|
|
|
|
{"dynamic+https://relays.syncthing.net/endpoint"},
|
|
|
|
{"tcp://1.2.3.4:22000", "dynamic+https://relays.syncthing.net/endpoint"},
|
|
|
|
},
|
|
|
|
// Default stuff gets sucked into "default", the rest gets copied
|
|
|
|
{
|
|
|
|
{"tcp://0.0.0.0:22000", "tcp://1.2.3.4:22000"},
|
|
|
|
{"dynamic+https://relays.syncthing.net/endpoint", "relay://other.example.com"},
|
|
|
|
{"default", "tcp://1.2.3.4:22000", "relay://other.example.com"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tcs {
|
|
|
|
cfg := Configuration{
|
|
|
|
Version: 13,
|
|
|
|
Options: OptionsConfiguration{
|
|
|
|
ListenAddresses: tc[0],
|
|
|
|
DeprecatedRelayServers: tc[1],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
convertV13V14(&cfg)
|
|
|
|
if cfg.Version != 14 {
|
|
|
|
t.Error("Configuration was not converted")
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(tc[2])
|
|
|
|
if !reflect.DeepEqual(cfg.Options.ListenAddresses, tc[2]) {
|
|
|
|
t.Errorf("Migration error; actual %#v != expected %#v", cfg.Options.ListenAddresses, tc[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 09:29:49 +00:00
|
|
|
|
|
|
|
func TestIgnoredDevices(t *testing.T) {
|
|
|
|
// Verify that ignored devices that are also present in the
|
|
|
|
// configuration are not in fact ignored.
|
|
|
|
|
|
|
|
wrapper, err := Load("testdata/ignoreddevices.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if wrapper.IgnoredDevice(device1) {
|
|
|
|
t.Errorf("Device %v should not be ignored", device1)
|
|
|
|
}
|
|
|
|
if !wrapper.IgnoredDevice(device3) {
|
|
|
|
t.Errorf("Device %v should be ignored", device3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDevice(t *testing.T) {
|
|
|
|
// Verify that the Device() call does the right thing
|
|
|
|
|
|
|
|
wrapper, err := Load("testdata/ignoreddevices.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// device1 is mentioned in the config
|
|
|
|
|
|
|
|
device, ok := wrapper.Device(device1)
|
|
|
|
if !ok {
|
|
|
|
t.Error(device1, "should exist")
|
|
|
|
}
|
|
|
|
if device.DeviceID != device1 {
|
|
|
|
t.Error("Should have returned", device1, "not", device.DeviceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// device3 is not
|
|
|
|
|
|
|
|
device, ok = wrapper.Device(device3)
|
|
|
|
if ok {
|
|
|
|
t.Error(device3, "should not exist")
|
|
|
|
}
|
|
|
|
if device.DeviceID == device3 {
|
|
|
|
t.Error("Should not returned ID", device3)
|
|
|
|
}
|
|
|
|
}
|
2016-08-07 16:21:59 +00:00
|
|
|
|
|
|
|
func TestSharesRemovedOnDeviceRemoval(t *testing.T) {
|
|
|
|
wrapper, err := Load("testdata/example.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:34:18 +00:00
|
|
|
raw := wrapper.RawCopy()
|
2016-08-07 16:21:59 +00:00
|
|
|
raw.Devices = raw.Devices[:len(raw.Devices)-1]
|
|
|
|
|
|
|
|
if len(raw.Folders[0].Devices) <= len(raw.Devices) {
|
|
|
|
t.Error("Should have less devices")
|
|
|
|
}
|
|
|
|
|
2017-12-07 07:08:24 +00:00
|
|
|
_, err = wrapper.Replace(raw)
|
2016-08-07 16:21:59 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Failed: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-11-12 09:34:18 +00:00
|
|
|
raw = wrapper.RawCopy()
|
2016-08-07 16:21:59 +00:00
|
|
|
if len(raw.Folders[0].Devices) > len(raw.Devices) {
|
|
|
|
t.Error("Unexpected extra device")
|
|
|
|
}
|
|
|
|
}
|
2017-06-17 18:40:28 +00:00
|
|
|
|
|
|
|
func TestIssue4219(t *testing.T) {
|
|
|
|
// Adding a folder that was previously ignored should make it unignored.
|
|
|
|
|
|
|
|
r := bytes.NewReader([]byte(`{
|
|
|
|
"folders": [
|
|
|
|
{"id": "abcd123"}
|
|
|
|
],
|
|
|
|
"ignoredFolders": ["t1", "abcd123", "t2"]
|
|
|
|
}`))
|
|
|
|
|
|
|
|
cfg, err := ReadJSON(r, protocol.LocalDeviceID)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cfg.IgnoredFolders) != 2 {
|
|
|
|
t.Errorf("There should be two ignored folders, not %d", len(cfg.IgnoredFolders))
|
|
|
|
}
|
|
|
|
|
|
|
|
w := Wrap("/tmp/cfg", cfg)
|
|
|
|
if !w.IgnoredFolder("t1") {
|
|
|
|
t.Error("Folder t1 should be ignored")
|
|
|
|
}
|
|
|
|
if !w.IgnoredFolder("t2") {
|
|
|
|
t.Error("Folder t2 should be ignored")
|
|
|
|
}
|
|
|
|
if w.IgnoredFolder("abcd123") {
|
|
|
|
t.Error("Folder abcd123 should not be ignored")
|
|
|
|
}
|
|
|
|
}
|
2018-01-24 08:21:28 +00:00
|
|
|
|
|
|
|
func TestInvalidDeviceIDRejected(t *testing.T) {
|
|
|
|
// This test verifies that we properly reject invalid device IDs when
|
2018-01-27 09:06:37 +00:00
|
|
|
// deserializing a JSON config.
|
2018-01-24 08:21:28 +00:00
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
id string
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
// a genuine device ID
|
|
|
|
{"GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY", true},
|
|
|
|
// incorrect check digit
|
|
|
|
{"GYRZZQB-IRNPV4A-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY", false},
|
|
|
|
// missing digit
|
|
|
|
{"GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VA", false},
|
|
|
|
// clearly broken
|
|
|
|
{"invalid", false},
|
|
|
|
// accepted as the empty device ID for historical reasons...
|
|
|
|
{"", true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
cfg := defaultConfigAsMap()
|
|
|
|
|
|
|
|
// Change the device ID of the first device to "invalid". Fast and loose
|
2018-01-27 09:06:37 +00:00
|
|
|
// with the type assertions as we know what the JSON decoder returns.
|
2018-01-24 08:21:28 +00:00
|
|
|
devs := cfg["devices"].([]interface{})
|
|
|
|
dev0 := devs[0].(map[string]interface{})
|
|
|
|
dev0["deviceID"] = tc.id
|
|
|
|
devs[0] = dev0
|
|
|
|
|
|
|
|
invalidJSON, err := json.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ReadJSON(bytes.NewReader(invalidJSON), device1)
|
|
|
|
if tc.ok && err != nil {
|
2018-01-27 09:06:37 +00:00
|
|
|
t.Errorf("unexpected error for device ID %q: %v", tc.id, err)
|
2018-01-24 08:21:28 +00:00
|
|
|
} else if !tc.ok && err == nil {
|
|
|
|
t.Errorf("device ID %q, expected error but got nil", tc.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 09:06:37 +00:00
|
|
|
func TestInvalidFolderIDRejected(t *testing.T) {
|
|
|
|
// This test verifies that we properly reject invalid folder IDs when
|
|
|
|
// deserializing a JSON config.
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
id string
|
|
|
|
ok bool
|
|
|
|
}{
|
|
|
|
// a reasonable folder ID
|
|
|
|
{"foo", true},
|
|
|
|
// empty is not OK
|
|
|
|
{"", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
cfg := defaultConfigAsMap()
|
|
|
|
|
|
|
|
// Change the folder ID of the first folder to the empty string.
|
|
|
|
// Fast and loose with the type assertions as we know what the JSON
|
|
|
|
// decoder returns.
|
|
|
|
devs := cfg["folders"].([]interface{})
|
|
|
|
dev0 := devs[0].(map[string]interface{})
|
|
|
|
dev0["id"] = tc.id
|
|
|
|
devs[0] = dev0
|
|
|
|
|
|
|
|
invalidJSON, err := json.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ReadJSON(bytes.NewReader(invalidJSON), device1)
|
|
|
|
if tc.ok && err != nil {
|
|
|
|
t.Errorf("unexpected error for folder ID %q: %v", tc.id, err)
|
|
|
|
} else if !tc.ok && err == nil {
|
|
|
|
t.Errorf("folder ID %q, expected error but got nil", tc.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-09 10:40:57 +00:00
|
|
|
func TestFilterURLSchemePrefix(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
before []string
|
|
|
|
prefix string
|
|
|
|
after []string
|
|
|
|
}{
|
|
|
|
{[]string{}, "kcp", []string{}},
|
|
|
|
{[]string{"tcp://foo"}, "kcp", []string{"tcp://foo"}},
|
|
|
|
{[]string{"kcp://foo"}, "kcp", []string{}},
|
|
|
|
{[]string{"tcp6://foo", "kcp6://foo"}, "kcp", []string{"tcp6://foo"}},
|
|
|
|
{[]string{"kcp6://foo", "tcp6://foo"}, "kcp", []string{"tcp6://foo"}},
|
|
|
|
{
|
|
|
|
[]string{"tcp://foo", "tcp4://foo", "kcp://foo", "kcp4://foo", "banana://foo", "banana4://foo", "banananas!"},
|
|
|
|
"kcp",
|
|
|
|
[]string{"tcp://foo", "tcp4://foo", "banana://foo", "banana4://foo", "banananas!"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
res := filterURLSchemePrefix(tc.before, tc.prefix)
|
|
|
|
if !reflect.DeepEqual(res, tc.after) {
|
|
|
|
t.Errorf("filterURLSchemePrefix => %q, expected %q", res, tc.after)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-24 08:21:28 +00:00
|
|
|
// defaultConfigAsMap returns a valid default config as a JSON-decoded
|
|
|
|
// map[string]interface{}. This is useful to override random elements and
|
|
|
|
// re-encode into JSON.
|
|
|
|
func defaultConfigAsMap() map[string]interface{} {
|
|
|
|
cfg := New(device1)
|
|
|
|
cfg.Devices = append(cfg.Devices, NewDeviceConfiguration(device2, "name"))
|
2018-01-27 09:06:37 +00:00
|
|
|
cfg.Folders = append(cfg.Folders, NewFolderConfiguration(device1, "default", "default", fs.FilesystemTypeBasic, "/tmp"))
|
2018-01-24 08:21:28 +00:00
|
|
|
bs, err := json.Marshal(cfg)
|
|
|
|
if err != nil {
|
|
|
|
// can't happen
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
var tmp map[string]interface{}
|
|
|
|
if err := json.Unmarshal(bs, &tmp); err != nil {
|
|
|
|
// can't happen
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return tmp
|
|
|
|
}
|