2014-11-16 13:13:20 -07:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-05-14 17:18:09 -07:00
|
|
|
package config
|
2014-03-04 14:17:28 -07:00
|
|
|
|
|
|
|
import (
|
2015-04-05 08:36:52 -07:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-10-11 08:55:11 -07:00
|
|
|
"fmt"
|
2014-04-22 03:06:32 -07:00
|
|
|
"os"
|
2015-04-05 13:52:22 -07:00
|
|
|
"path/filepath"
|
2014-03-04 14:17:28 -07:00
|
|
|
"reflect"
|
2015-01-22 16:34:22 -07:00
|
|
|
"runtime"
|
2015-04-05 13:52:22 -07:00
|
|
|
"strings"
|
2014-03-04 14:17:28 -07:00
|
|
|
"testing"
|
2014-06-06 20:10:15 -07:00
|
|
|
|
2015-01-13 05:22:56 -07:00
|
|
|
"github.com/syncthing/protocol"
|
2014-03-04 14:17:28 -07:00
|
|
|
)
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
var device1, device2, device3, device4 protocol.DeviceID
|
2014-06-29 16:42:03 -07:00
|
|
|
|
|
|
|
func init() {
|
2014-09-28 04:00:38 -07: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 16:42:03 -07:00
|
|
|
}
|
|
|
|
|
2014-03-04 14:17:28 -07:00
|
|
|
func TestDefaultValues(t *testing.T) {
|
|
|
|
expected := OptionsConfiguration{
|
2014-11-16 16:18:59 -07:00
|
|
|
ListenAddress: []string{"0.0.0.0:22000"},
|
2015-03-07 12:48:53 -07:00
|
|
|
GlobalAnnServers: []string{"udp4://announce.syncthing.net:22026", "udp6://announce-v6.syncthing.net:22026"},
|
2014-11-16 16:18:59 -07:00
|
|
|
GlobalAnnEnabled: true,
|
|
|
|
LocalAnnEnabled: true,
|
|
|
|
LocalAnnPort: 21025,
|
|
|
|
LocalAnnMCAddr: "[ff32::5222]:21026",
|
|
|
|
MaxSendKbps: 0,
|
|
|
|
MaxRecvKbps: 0,
|
|
|
|
ReconnectIntervalS: 60,
|
|
|
|
StartBrowser: true,
|
|
|
|
UPnPEnabled: true,
|
2015-04-15 16:36:27 -07:00
|
|
|
UPnPLeaseM: 0,
|
|
|
|
UPnPRenewalM: 30,
|
2015-04-15 16:34:27 -07:00
|
|
|
UPnPTimeoutS: 3,
|
2014-11-16 16:18:59 -07:00
|
|
|
RestartOnWakeup: true,
|
|
|
|
AutoUpgradeIntervalH: 12,
|
|
|
|
KeepTemporariesH: 24,
|
|
|
|
CacheIgnoredFiles: true,
|
|
|
|
ProgressUpdateIntervalS: 5,
|
2014-12-01 03:26:54 -07:00
|
|
|
SymlinksEnabled: true,
|
2015-03-08 11:36:59 -07:00
|
|
|
LimitBandwidthInLan: false,
|
2014-03-04 14:17:28 -07:00
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
cfg := New(device1)
|
2014-03-04 14:17:28 -07:00
|
|
|
|
|
|
|
if !reflect.DeepEqual(cfg.Options, expected) {
|
|
|
|
t.Errorf("Default config differs;\n E: %#v\n A: %#v", expected, cfg.Options)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func TestDeviceConfig(t *testing.T) {
|
2015-04-04 12:49:15 -07:00
|
|
|
for i := OldestHandledVersion; i <= CurrentVersion; i++ {
|
2014-10-11 08:55:11 -07:00
|
|
|
os.Remove("testdata/.stfolder")
|
|
|
|
wr, err := Load(fmt.Sprintf("testdata/v%d.xml", i), device1)
|
2014-04-08 04:45:18 -07:00
|
|
|
if err != nil {
|
2014-10-06 00:25:45 -07:00
|
|
|
t.Fatal(err)
|
2014-04-08 04:45:18 -07:00
|
|
|
}
|
2014-10-11 08:55:11 -07:00
|
|
|
|
|
|
|
_, err = os.Stat("testdata/.stfolder")
|
|
|
|
if i < 6 && err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if i >= 6 && err == nil {
|
|
|
|
t.Fatal("Unexpected file")
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
cfg := wr.cfg
|
2014-04-08 04:45:18 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
expectedFolders := []FolderConfiguration{
|
2014-04-08 04:45:18 -07:00
|
|
|
{
|
2014-08-18 14:05:47 -07:00
|
|
|
ID: "test",
|
2015-04-05 13:52:22 -07:00
|
|
|
RawPath: "testdata",
|
2014-09-28 04:05:25 -07:00
|
|
|
Devices: []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
|
2014-08-18 14:05:47 -07:00
|
|
|
ReadOnly: true,
|
|
|
|
RescanIntervalS: 600,
|
2014-11-23 11:43:49 -07:00
|
|
|
Copiers: 1,
|
|
|
|
Pullers: 16,
|
2014-12-24 16:12:12 -07:00
|
|
|
Hashers: 0,
|
2015-03-21 07:33:31 -07:00
|
|
|
AutoNormalize: true,
|
2014-04-08 04:45:18 -07:00
|
|
|
},
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
expectedDevices := []DeviceConfiguration{
|
2014-04-08 04:45:18 -07:00
|
|
|
{
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device1,
|
2014-09-28 05:22:39 -07:00
|
|
|
Name: "node one",
|
2014-08-01 07:48:06 -07:00
|
|
|
Addresses: []string{"a"},
|
2015-02-23 01:44:10 -07:00
|
|
|
Compression: protocol.CompressMetadata,
|
2014-04-08 04:45:18 -07:00
|
|
|
},
|
|
|
|
{
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device4,
|
2014-09-28 05:22:39 -07:00
|
|
|
Name: "node two",
|
2014-08-01 07:48:06 -07:00
|
|
|
Addresses: []string{"b"},
|
2015-02-23 01:44:10 -07:00
|
|
|
Compression: protocol.CompressMetadata,
|
2014-04-08 04:45:18 -07:00
|
|
|
},
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
expectedDeviceIDs := []protocol.DeviceID{device1, device4}
|
2014-04-08 04:45:18 -07:00
|
|
|
|
2014-10-11 08:55:11 -07:00
|
|
|
if cfg.Version != CurrentVersion {
|
|
|
|
t.Errorf("%d: Incorrect version %d != %d", i, cfg.Version, CurrentVersion)
|
2014-04-08 04:45:18 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07: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 04:45:18 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07: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 04:45:18 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07: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 04:45:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-04 14:17:28 -07:00
|
|
|
func TestNoListenAddress(t *testing.T) {
|
2014-09-28 04:00:38 -07:00
|
|
|
cfg, err := Load("testdata/nolistenaddress.xml", device1)
|
2014-03-04 14:17:28 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{""}
|
2014-10-06 00:25:45 -07:00
|
|
|
actual := cfg.Options().ListenAddress
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Errorf("Unexpected ListenAddress %#v", actual)
|
2014-03-04 14:17:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOverriddenValues(t *testing.T) {
|
|
|
|
expected := OptionsConfiguration{
|
2014-11-16 16:18:59 -07:00
|
|
|
ListenAddress: []string{":23000"},
|
2014-11-20 14:24:11 -07:00
|
|
|
GlobalAnnServers: []string{"udp4://syncthing.nym.se:22026"},
|
2014-11-16 16:18:59 -07:00
|
|
|
GlobalAnnEnabled: false,
|
|
|
|
LocalAnnEnabled: false,
|
|
|
|
LocalAnnPort: 42123,
|
|
|
|
LocalAnnMCAddr: "quux:3232",
|
|
|
|
MaxSendKbps: 1234,
|
|
|
|
MaxRecvKbps: 2341,
|
|
|
|
ReconnectIntervalS: 6000,
|
|
|
|
StartBrowser: false,
|
|
|
|
UPnPEnabled: false,
|
2015-04-15 16:36:27 -07:00
|
|
|
UPnPLeaseM: 60,
|
|
|
|
UPnPRenewalM: 15,
|
2015-04-15 16:34:27 -07:00
|
|
|
UPnPTimeoutS: 15,
|
2014-11-16 16:18:59 -07:00
|
|
|
RestartOnWakeup: false,
|
|
|
|
AutoUpgradeIntervalH: 24,
|
|
|
|
KeepTemporariesH: 48,
|
|
|
|
CacheIgnoredFiles: false,
|
|
|
|
ProgressUpdateIntervalS: 10,
|
2014-12-01 03:26:54 -07:00
|
|
|
SymlinksEnabled: false,
|
2015-03-08 11:36:59 -07:00
|
|
|
LimitBandwidthInLan: true,
|
2014-03-04 14:17:28 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
cfg, err := Load("testdata/overridenvalues.xml", device1)
|
2014-03-04 14:17:28 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
if !reflect.DeepEqual(cfg.Options(), expected) {
|
2014-11-18 15:57:21 -07:00
|
|
|
t.Errorf("Overridden config differs;\n E: %#v\n A: %#v", expected, cfg.Options())
|
2014-03-04 14:17:28 -07:00
|
|
|
}
|
|
|
|
}
|
2014-04-22 02:46:08 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func TestDeviceAddressesDynamic(t *testing.T) {
|
2015-02-23 01:44:10 -07:00
|
|
|
name, _ := os.Hostname()
|
|
|
|
expected := map[protocol.DeviceID]DeviceConfiguration{
|
|
|
|
device1: {
|
|
|
|
DeviceID: device1,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
},
|
|
|
|
device2: {
|
|
|
|
DeviceID: device2,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
},
|
|
|
|
device3: {
|
|
|
|
DeviceID: device3,
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
},
|
|
|
|
device4: {
|
|
|
|
DeviceID: device4,
|
|
|
|
Name: name, // Set when auto created
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := Load("testdata/deviceaddressesdynamic.xml", device4)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := cfg.Devices()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeviceCompression(t *testing.T) {
|
2014-04-22 03:06:32 -07:00
|
|
|
name, _ := os.Hostname()
|
2014-10-06 00:25:45 -07:00
|
|
|
expected := map[protocol.DeviceID]DeviceConfiguration{
|
|
|
|
device1: {
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device1,
|
2014-08-01 07:48:06 -07:00
|
|
|
Addresses: []string{"dynamic"},
|
2015-02-23 01:44:10 -07:00
|
|
|
Compression: protocol.CompressMetadata,
|
2014-04-22 02:46:08 -07:00
|
|
|
},
|
2014-10-06 00:25:45 -07:00
|
|
|
device2: {
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device2,
|
2014-08-01 07:48:06 -07:00
|
|
|
Addresses: []string{"dynamic"},
|
2015-02-23 01:44:10 -07:00
|
|
|
Compression: protocol.CompressMetadata,
|
2014-04-22 02:46:08 -07:00
|
|
|
},
|
2014-10-06 00:25:45 -07:00
|
|
|
device3: {
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device3,
|
2014-08-01 07:48:06 -07:00
|
|
|
Addresses: []string{"dynamic"},
|
2015-02-23 01:44:10 -07:00
|
|
|
Compression: protocol.CompressNever,
|
2014-04-22 02:46:08 -07:00
|
|
|
},
|
2014-10-06 00:25:45 -07:00
|
|
|
device4: {
|
2015-02-23 01:44:10 -07:00
|
|
|
DeviceID: device4,
|
|
|
|
Name: name, // Set when auto created
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
2014-04-22 02:46:08 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-02-23 01:44:10 -07:00
|
|
|
cfg, err := Load("testdata/devicecompression.xml", device4)
|
2014-04-22 02:46:08 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
actual := cfg.Devices()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, actual)
|
2014-04-22 02:46:08 -07:00
|
|
|
}
|
|
|
|
}
|
2014-07-24 04:23:17 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func TestDeviceAddressesStatic(t *testing.T) {
|
2014-07-24 04:23:17 -07:00
|
|
|
name, _ := os.Hostname()
|
2014-10-06 00:25:45 -07:00
|
|
|
expected := map[protocol.DeviceID]DeviceConfiguration{
|
|
|
|
device1: {
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device1,
|
2014-07-24 04:23:17 -07:00
|
|
|
Addresses: []string{"192.0.2.1", "192.0.2.2"},
|
|
|
|
},
|
2014-10-06 00:25:45 -07:00
|
|
|
device2: {
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device2,
|
2014-07-24 04:23:17 -07:00
|
|
|
Addresses: []string{"192.0.2.3:6070", "[2001:db8::42]:4242"},
|
|
|
|
},
|
2014-10-06 00:25:45 -07:00
|
|
|
device3: {
|
2014-09-28 04:05:25 -07:00
|
|
|
DeviceID: device3,
|
2014-07-24 04:23:17 -07:00
|
|
|
Addresses: []string{"[2001:db8::44]:4444", "192.0.2.4:6090"},
|
|
|
|
},
|
2014-10-06 00:25:45 -07:00
|
|
|
device4: {
|
2015-02-23 01:44:10 -07:00
|
|
|
DeviceID: device4,
|
|
|
|
Name: name, // Set when auto created
|
|
|
|
Addresses: []string{"dynamic"},
|
|
|
|
Compression: protocol.CompressMetadata,
|
2014-07-24 04:23:17 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
cfg, err := Load("testdata/deviceaddressesstatic.xml", device4)
|
2014-07-24 04:23:17 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
actual := cfg.Devices()
|
|
|
|
if !reflect.DeepEqual(actual, expected) {
|
|
|
|
t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, actual)
|
2014-07-24 04:23:17 -07:00
|
|
|
}
|
|
|
|
}
|
2014-07-31 05:13:55 -07:00
|
|
|
|
|
|
|
func TestVersioningConfig(t *testing.T) {
|
2014-09-28 04:00:38 -07:00
|
|
|
cfg, err := Load("testdata/versioningconfig.xml", device4)
|
2014-07-31 05:13:55 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
vc := cfg.Folders()["test"].Versioning
|
2014-07-31 05:13:55 -07: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 07:50:06 -07:00
|
|
|
|
2015-01-22 16:34:22 -07:00
|
|
|
func TestIssue1262(t *testing.T) {
|
|
|
|
cfg, err := Load("testdata/issue-1262.xml", device4)
|
|
|
|
if err != nil {
|
2015-04-05 13:52:22 -07:00
|
|
|
t.Fatal(err)
|
2015-01-22 16:34:22 -07:00
|
|
|
}
|
|
|
|
|
2015-04-05 13:52:22 -07:00
|
|
|
actual := cfg.Folders()["test"].RawPath
|
2015-02-09 16:56:16 -07:00
|
|
|
expected := "e:"
|
2015-01-22 16:34:22 -07:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
expected = `e:\`
|
|
|
|
}
|
|
|
|
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("%q != %q", actual, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-05 13:52:22 -07:00
|
|
|
func TestWindowsPaths(t *testing.T) {
|
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
t.Skip("Not useful on non-Windows")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
folder := FolderConfiguration{
|
|
|
|
RawPath: `e:\`,
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := `\\?\e:\`
|
|
|
|
actual := folder.Path()
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("%q != %q", actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
folder.RawPath = `\\192.0.2.22\network\share`
|
|
|
|
expected = folder.RawPath
|
|
|
|
actual = folder.Path()
|
|
|
|
if actual != expected {
|
|
|
|
t.Errorf("%q != %q", actual, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
folder.RawPath = `relative\path`
|
|
|
|
expected = folder.RawPath
|
|
|
|
actual = folder.Path()
|
2015-04-16 13:18:17 -07:00
|
|
|
if actual == expected || !strings.HasPrefix(actual, "\\\\?\\") {
|
|
|
|
t.Errorf("%q == %q, expected absolutification", actual, expected)
|
2015-04-05 13:52:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFolderPath(t *testing.T) {
|
|
|
|
folder := FolderConfiguration{
|
|
|
|
RawPath: "~/tmp",
|
|
|
|
}
|
|
|
|
|
|
|
|
realPath := folder.Path()
|
|
|
|
if !filepath.IsAbs(realPath) {
|
|
|
|
t.Error(realPath, "should be absolute")
|
|
|
|
}
|
|
|
|
if strings.Contains(realPath, "~") {
|
|
|
|
t.Error(realPath, "should not contain ~")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-06 07:50:06 -07: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 00:25:45 -07:00
|
|
|
intCfg := New(device1)
|
|
|
|
cfg := Wrap(path, intCfg)
|
2014-09-06 07:50:06 -07:00
|
|
|
|
|
|
|
// To make the equality pass later
|
2014-10-06 00:25:45 -07:00
|
|
|
cfg.cfg.XMLName.Local = "configuration"
|
2014-09-06 07:50:06 -07: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 04:00:38 -07:00
|
|
|
cfg2, err := Load(path, device1)
|
2014-09-06 07:50:06 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2014-10-06 00:25:45 -07:00
|
|
|
if !reflect.DeepEqual(cfg.Raw(), cfg2.Raw()) {
|
|
|
|
t.Errorf("Configs are not equal;\n E: %#v\n A: %#v", cfg.Raw(), cfg2.Raw())
|
2014-09-06 07:50:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
os.Remove(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPrepare(t *testing.T) {
|
|
|
|
var cfg Configuration
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
if cfg.Folders != nil || cfg.Devices != nil || cfg.Options.ListenAddress != nil {
|
2014-09-06 07:50:06 -07:00
|
|
|
t.Error("Expected nil")
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
cfg.prepare(device1)
|
2014-09-06 07:50:06 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
if cfg.Folders == nil || cfg.Devices == nil || cfg.Options.ListenAddress == nil {
|
2014-09-06 07:50:06 -07:00
|
|
|
t.Error("Unexpected nil")
|
|
|
|
}
|
|
|
|
}
|
2014-10-07 01:46:33 -07:00
|
|
|
|
|
|
|
func TestRequiresRestart(t *testing.T) {
|
2014-10-11 08:55:11 -07:00
|
|
|
wr, err := Load("testdata/v6.xml", device1)
|
2014-10-07 01:46:33 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cfg := wr.cfg
|
|
|
|
|
|
|
|
if ChangeRequiresRestart(cfg, cfg) {
|
|
|
|
t.Error("No change does not require restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg := cfg
|
|
|
|
newCfg.Devices = append(newCfg.Devices, DeviceConfiguration{
|
|
|
|
DeviceID: device3,
|
|
|
|
})
|
|
|
|
if ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Adding a device does not require restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg = cfg
|
|
|
|
newCfg.Devices = newCfg.Devices[:len(newCfg.Devices)-1]
|
|
|
|
if !ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Removing a device requires restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg = cfg
|
|
|
|
newCfg.Folders = append(newCfg.Folders, FolderConfiguration{
|
2015-04-05 13:52:22 -07:00
|
|
|
ID: "t1",
|
|
|
|
RawPath: "t1",
|
2014-10-07 01:46:33 -07:00
|
|
|
})
|
|
|
|
if !ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Adding a folder requires restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg = cfg
|
|
|
|
newCfg.Folders = newCfg.Folders[:len(newCfg.Folders)-1]
|
|
|
|
if !ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Removing a folder requires restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg = cfg
|
|
|
|
newFolders := make([]FolderConfiguration, len(cfg.Folders))
|
|
|
|
copy(newFolders, cfg.Folders)
|
|
|
|
newCfg.Folders = newFolders
|
|
|
|
if ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("No changes done yet")
|
|
|
|
}
|
2015-04-05 13:52:22 -07:00
|
|
|
newCfg.Folders[0].RawPath = "different"
|
2014-10-07 01:46:33 -07:00
|
|
|
if !ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Changing a folder requires restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg = cfg
|
|
|
|
newDevices := make([]DeviceConfiguration, len(cfg.Devices))
|
|
|
|
copy(newDevices, cfg.Devices)
|
|
|
|
newCfg.Devices = newDevices
|
|
|
|
if ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("No changes done yet")
|
|
|
|
}
|
|
|
|
newCfg.Devices[0].Name = "different"
|
|
|
|
if ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Changing a device does not require restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg = cfg
|
|
|
|
newCfg.Options.GlobalAnnEnabled = !cfg.Options.GlobalAnnEnabled
|
|
|
|
if !ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Changing general options requires restart")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCfg = cfg
|
|
|
|
newCfg.GUI.UseTLS = !cfg.GUI.UseTLS
|
|
|
|
if !ChangeRequiresRestart(cfg, newCfg) {
|
|
|
|
t.Error("Changing GUI options requires restart")
|
|
|
|
}
|
|
|
|
}
|
2015-04-05 08:36:52 -07:00
|
|
|
|
|
|
|
func TestCopy(t *testing.T) {
|
|
|
|
wrapper, err := Load("testdata/example.xml", device1)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cfg := wrapper.Raw()
|
|
|
|
|
|
|
|
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}
|
|
|
|
cfg.Options.ListenAddress[0] = "wrong"
|
|
|
|
cfg.GUI.APIKey = "wrong"
|
|
|
|
|
|
|
|
bsChanged, err := json.MarshalIndent(cfg, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bsCopy, err := json.MarshalIndent(copy, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if bytes.Compare(bsOrig, bsChanged) == 0 {
|
|
|
|
t.Error("Config should have changed")
|
|
|
|
}
|
|
|
|
if bytes.Compare(bsOrig, bsCopy) != 0 {
|
|
|
|
//ioutil.WriteFile("a", bsOrig, 0644)
|
|
|
|
//ioutil.WriteFile("b", bsCopy, 0644)
|
|
|
|
t.Error("Copy should be unchanged")
|
|
|
|
}
|
|
|
|
}
|