2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
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 (
|
2014-04-22 10:06:32 +00:00
|
|
|
"os"
|
2014-03-04 21:17:28 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
2014-06-07 03:10:15 +00:00
|
|
|
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/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{
|
2014-09-28 20:38:10 +00:00
|
|
|
ListenAddress: []string{"0.0.0.0:22000"},
|
|
|
|
GlobalAnnServer: "announce.syncthing.net:22026",
|
|
|
|
GlobalAnnEnabled: true,
|
|
|
|
LocalAnnEnabled: true,
|
|
|
|
LocalAnnPort: 21025,
|
|
|
|
LocalAnnMCAddr: "[ff32::5222]:21026",
|
|
|
|
MaxSendKbps: 0,
|
|
|
|
MaxRecvKbps: 0,
|
|
|
|
ReconnectIntervalS: 60,
|
|
|
|
StartBrowser: true,
|
|
|
|
UPnPEnabled: true,
|
|
|
|
UPnPLease: 0,
|
|
|
|
UPnPRenewal: 30,
|
|
|
|
RestartOnWakeup: true,
|
|
|
|
AutoUpgradeIntervalH: 12,
|
2014-03-04 21:17:28 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg := New("test", device1)
|
2014-03-04 21:17:28 +00:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(cfg.Options, expected) {
|
|
|
|
t.Errorf("Default config differs;\n E: %#v\n A: %#v", expected, cfg.Options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func TestDeviceConfig(t *testing.T) {
|
2014-09-28 12:22:39 +00:00
|
|
|
for i, ver := range []string{"v1", "v2", "v3", "v4", "v5"} {
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg, err := Load("testdata/"+ver+".xml", device1)
|
2014-04-08 11:45:18 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
expectedFolders := []FolderConfiguration{
|
2014-04-08 11:45:18 +00:00
|
|
|
{
|
2014-08-18 21:05:47 +00:00
|
|
|
ID: "test",
|
2014-09-28 11:56:35 +00:00
|
|
|
Path: "~/Sync",
|
2014-09-28 11:05:25 +00:00
|
|
|
Devices: []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
|
2014-08-18 21:05:47 +00:00
|
|
|
ReadOnly: true,
|
|
|
|
RescanIntervalS: 600,
|
2014-04-08 11:45:18 +00:00
|
|
|
},
|
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
expectedDevices := []DeviceConfiguration{
|
2014-04-08 11:45:18 +00:00
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device1,
|
2014-09-28 12:22:39 +00:00
|
|
|
Name: "node one",
|
2014-08-01 14:48:06 +00:00
|
|
|
Addresses: []string{"a"},
|
|
|
|
Compression: true,
|
2014-04-08 11:45:18 +00:00
|
|
|
},
|
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device4,
|
2014-09-28 12:22:39 +00:00
|
|
|
Name: "node two",
|
2014-08-01 14:48:06 +00:00
|
|
|
Addresses: []string{"b"},
|
|
|
|
Compression: true,
|
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-09-28 12:22:39 +00:00
|
|
|
if cfg.Version != 5 {
|
|
|
|
t.Errorf("%d: Incorrect version %d != 5", i, cfg.Version)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
if !reflect.DeepEqual(cfg.Folders, expectedFolders) {
|
|
|
|
t.Errorf("%d: Incorrect Folders\n A: %#v\n E: %#v", i, cfg.Folders, expectedFolders)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
if !reflect.DeepEqual(cfg.Devices, expectedDevices) {
|
|
|
|
t.Errorf("%d: Incorrect Devices\n A: %#v\n E: %#v", i, cfg.Devices, expectedDevices)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
if !reflect.DeepEqual(cfg.Folders[0].DeviceIDs(), expectedDeviceIDs) {
|
|
|
|
t.Errorf("%d: Incorrect DeviceIDs\n A: %#v\n E: %#v", i, cfg.Folders[0].DeviceIDs(), expectedDeviceIDs)
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
2014-07-31 12:13:55 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
if len(cfg.DeviceMap()) != len(expectedDevices) {
|
|
|
|
t.Errorf("Unexpected number of DeviceMap() entries")
|
2014-07-31 12:13:55 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
if len(cfg.FolderMap()) != len(expectedFolders) {
|
|
|
|
t.Errorf("Unexpected number of FolderMap() entries")
|
2014-07-31 12:13:55 +00:00
|
|
|
}
|
2014-04-08 11:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 21:17:28 +00:00
|
|
|
func TestNoListenAddress(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{""}
|
|
|
|
if !reflect.DeepEqual(cfg.Options.ListenAddress, expected) {
|
|
|
|
t.Errorf("Unexpected ListenAddress %#v", cfg.Options.ListenAddress)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverriddenValues(t *testing.T) {
|
|
|
|
expected := OptionsConfiguration{
|
2014-09-28 20:38:10 +00:00
|
|
|
ListenAddress: []string{":23000"},
|
|
|
|
GlobalAnnServer: "syncthing.nym.se:22026",
|
|
|
|
GlobalAnnEnabled: false,
|
|
|
|
LocalAnnEnabled: false,
|
|
|
|
LocalAnnPort: 42123,
|
|
|
|
LocalAnnMCAddr: "quux:3232",
|
|
|
|
MaxSendKbps: 1234,
|
|
|
|
MaxRecvKbps: 2341,
|
|
|
|
ReconnectIntervalS: 6000,
|
|
|
|
StartBrowser: false,
|
|
|
|
UPnPEnabled: false,
|
|
|
|
UPnPLease: 60,
|
|
|
|
UPnPRenewal: 15,
|
|
|
|
RestartOnWakeup: false,
|
|
|
|
AutoUpgradeIntervalH: 24,
|
2014-03-04 21:17:28 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(cfg.Options, expected) {
|
|
|
|
t.Errorf("Overridden config differs;\n E: %#v\n A: %#v", expected, cfg.Options)
|
|
|
|
}
|
|
|
|
}
|
2014-04-22 09:46:08 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func TestDeviceAddressesDynamic(t *testing.T) {
|
2014-04-22 10:06:32 +00:00
|
|
|
name, _ := os.Hostname()
|
2014-09-28 11:00:38 +00:00
|
|
|
expected := []DeviceConfiguration{
|
2014-04-22 09:46:08 +00:00
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device1,
|
2014-08-01 14:48:06 +00:00
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: true,
|
2014-04-22 09:46:08 +00:00
|
|
|
},
|
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device2,
|
2014-08-01 14:48:06 +00:00
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: true,
|
2014-04-22 09:46:08 +00:00
|
|
|
},
|
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device3,
|
2014-08-01 14:48:06 +00:00
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: true,
|
2014-04-22 09:46:08 +00:00
|
|
|
},
|
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device4,
|
2014-04-22 10:06:32 +00:00
|
|
|
Name: name, // Set when auto created
|
2014-04-22 09:46:08 +00:00
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
cfg, err := Load("testdata/deviceaddressesdynamic.xml", device4)
|
2014-04-22 09:46:08 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
if !reflect.DeepEqual(cfg.Devices, expected) {
|
|
|
|
t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, cfg.Devices)
|
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-09-28 11:00:38 +00:00
|
|
|
expected := []DeviceConfiguration{
|
2014-07-24 11:23:17 +00:00
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device1,
|
2014-07-24 11:23:17 +00:00
|
|
|
Addresses: []string{"192.0.2.1", "192.0.2.2"},
|
|
|
|
},
|
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device2,
|
2014-07-24 11:23:17 +00:00
|
|
|
Addresses: []string{"192.0.2.3:6070", "[2001:db8::42]:4242"},
|
|
|
|
},
|
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device3,
|
2014-07-24 11:23:17 +00:00
|
|
|
Addresses: []string{"[2001:db8::44]:4444", "192.0.2.4:6090"},
|
|
|
|
},
|
|
|
|
{
|
2014-09-28 11:05:25 +00:00
|
|
|
DeviceID: device4,
|
2014-07-24 11:23:17 +00:00
|
|
|
Name: name, // Set when auto created
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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-09-28 11:00:38 +00:00
|
|
|
if !reflect.DeepEqual(cfg.Devices, expected) {
|
|
|
|
t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, cfg.Devices)
|
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-09-28 11:00:38 +00:00
|
|
|
vc := cfg.Folders[0].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",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(vc.Params, expected) {
|
|
|
|
t.Errorf("vc.Params differ;\n E: %#v\n A: %#v", expected, vc.Params)
|
|
|
|
}
|
|
|
|
}
|
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-09-28 11:00:38 +00:00
|
|
|
cfg := New(path, device1)
|
2014-09-06 14:50:06 +00:00
|
|
|
|
|
|
|
// To make the equality pass later
|
|
|
|
cfg.XMLName.Local = "configuration"
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(cfg, cfg2) {
|
|
|
|
t.Errorf("Configs are not equal;\n E: %#v\n A: %#v", cfg, cfg2)
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.GUI.User = "test"
|
|
|
|
cfg.Save()
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg2.GUI.User != "test" || !reflect.DeepEqual(cfg, cfg2) {
|
|
|
|
t.Errorf("Configs are not equal;\n E: %#v\n A: %#v", cfg, cfg2)
|
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepare(t *testing.T) {
|
|
|
|
var cfg Configuration
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
if cfg.Folders != nil || cfg.Devices != nil || cfg.Options.ListenAddress != 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
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
if cfg.Folders == nil || cfg.Devices == nil || cfg.Options.ListenAddress == nil {
|
2014-09-06 14:50:06 +00:00
|
|
|
t.Error("Unexpected nil")
|
|
|
|
}
|
|
|
|
}
|