config_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package config
  16. import (
  17. "os"
  18. "reflect"
  19. "testing"
  20. "github.com/syncthing/syncthing/internal/protocol"
  21. )
  22. var device1, device2, device3, device4 protocol.DeviceID
  23. func init() {
  24. device1, _ = protocol.DeviceIDFromString("AIR6LPZ7K4PTTUXQSMUUCPQ5YWOEDFIIQJUG7772YQXXR5YD6AWQ")
  25. device2, _ = protocol.DeviceIDFromString("GYRZZQB-IRNPV4Z-T7TC52W-EQYJ3TT-FDQW6MW-DFLMU42-SSSU6EM-FBK2VAY")
  26. device3, _ = protocol.DeviceIDFromString("LGFPDIT-7SKNNJL-VJZA4FC-7QNCRKA-CE753K7-2BW5QDK-2FOZ7FR-FEP57QJ")
  27. device4, _ = protocol.DeviceIDFromString("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2")
  28. }
  29. func TestDefaultValues(t *testing.T) {
  30. expected := OptionsConfiguration{
  31. ListenAddress: []string{"0.0.0.0:22000"},
  32. GlobalAnnServer: "announce.syncthing.net:22026",
  33. GlobalAnnEnabled: true,
  34. LocalAnnEnabled: true,
  35. LocalAnnPort: 21025,
  36. LocalAnnMCAddr: "[ff32::5222]:21026",
  37. MaxSendKbps: 0,
  38. MaxRecvKbps: 0,
  39. ReconnectIntervalS: 60,
  40. StartBrowser: true,
  41. UPnPEnabled: true,
  42. UPnPLease: 0,
  43. UPnPRenewal: 30,
  44. RestartOnWakeup: true,
  45. AutoUpgradeIntervalH: 12,
  46. KeepTemporariesH: 24,
  47. }
  48. cfg := New(device1)
  49. if !reflect.DeepEqual(cfg.Options, expected) {
  50. t.Errorf("Default config differs;\n E: %#v\n A: %#v", expected, cfg.Options)
  51. }
  52. }
  53. func TestDeviceConfig(t *testing.T) {
  54. for i, ver := range []string{"v1", "v2", "v3", "v4", "v5"} {
  55. wr, err := Load("testdata/"+ver+".xml", device1)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. cfg := wr.cfg
  60. expectedFolders := []FolderConfiguration{
  61. {
  62. ID: "test",
  63. Path: "~/Sync",
  64. Devices: []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
  65. ReadOnly: true,
  66. RescanIntervalS: 600,
  67. },
  68. }
  69. expectedDevices := []DeviceConfiguration{
  70. {
  71. DeviceID: device1,
  72. Name: "node one",
  73. Addresses: []string{"a"},
  74. Compression: true,
  75. },
  76. {
  77. DeviceID: device4,
  78. Name: "node two",
  79. Addresses: []string{"b"},
  80. Compression: true,
  81. },
  82. }
  83. expectedDeviceIDs := []protocol.DeviceID{device1, device4}
  84. if cfg.Version != 5 {
  85. t.Errorf("%d: Incorrect version %d != 5", i, cfg.Version)
  86. }
  87. if !reflect.DeepEqual(cfg.Folders, expectedFolders) {
  88. t.Errorf("%d: Incorrect Folders\n A: %#v\n E: %#v", i, cfg.Folders, expectedFolders)
  89. }
  90. if !reflect.DeepEqual(cfg.Devices, expectedDevices) {
  91. t.Errorf("%d: Incorrect Devices\n A: %#v\n E: %#v", i, cfg.Devices, expectedDevices)
  92. }
  93. if !reflect.DeepEqual(cfg.Folders[0].DeviceIDs(), expectedDeviceIDs) {
  94. t.Errorf("%d: Incorrect DeviceIDs\n A: %#v\n E: %#v", i, cfg.Folders[0].DeviceIDs(), expectedDeviceIDs)
  95. }
  96. }
  97. }
  98. func TestNoListenAddress(t *testing.T) {
  99. cfg, err := Load("testdata/nolistenaddress.xml", device1)
  100. if err != nil {
  101. t.Error(err)
  102. }
  103. expected := []string{""}
  104. actual := cfg.Options().ListenAddress
  105. if !reflect.DeepEqual(actual, expected) {
  106. t.Errorf("Unexpected ListenAddress %#v", actual)
  107. }
  108. }
  109. func TestOverriddenValues(t *testing.T) {
  110. expected := OptionsConfiguration{
  111. ListenAddress: []string{":23000"},
  112. GlobalAnnServer: "syncthing.nym.se:22026",
  113. GlobalAnnEnabled: false,
  114. LocalAnnEnabled: false,
  115. LocalAnnPort: 42123,
  116. LocalAnnMCAddr: "quux:3232",
  117. MaxSendKbps: 1234,
  118. MaxRecvKbps: 2341,
  119. ReconnectIntervalS: 6000,
  120. StartBrowser: false,
  121. UPnPEnabled: false,
  122. UPnPLease: 60,
  123. UPnPRenewal: 15,
  124. RestartOnWakeup: false,
  125. AutoUpgradeIntervalH: 24,
  126. KeepTemporariesH: 48,
  127. }
  128. cfg, err := Load("testdata/overridenvalues.xml", device1)
  129. if err != nil {
  130. t.Error(err)
  131. }
  132. if !reflect.DeepEqual(cfg.Options(), expected) {
  133. t.Errorf("Overridden config differs;\n E: %#v\n A: %#v", expected, cfg.Options)
  134. }
  135. }
  136. func TestDeviceAddressesDynamic(t *testing.T) {
  137. name, _ := os.Hostname()
  138. expected := map[protocol.DeviceID]DeviceConfiguration{
  139. device1: {
  140. DeviceID: device1,
  141. Addresses: []string{"dynamic"},
  142. Compression: true,
  143. },
  144. device2: {
  145. DeviceID: device2,
  146. Addresses: []string{"dynamic"},
  147. Compression: true,
  148. },
  149. device3: {
  150. DeviceID: device3,
  151. Addresses: []string{"dynamic"},
  152. Compression: true,
  153. },
  154. device4: {
  155. DeviceID: device4,
  156. Name: name, // Set when auto created
  157. Addresses: []string{"dynamic"},
  158. },
  159. }
  160. cfg, err := Load("testdata/deviceaddressesdynamic.xml", device4)
  161. if err != nil {
  162. t.Error(err)
  163. }
  164. actual := cfg.Devices()
  165. if !reflect.DeepEqual(actual, expected) {
  166. t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, actual)
  167. }
  168. }
  169. func TestDeviceAddressesStatic(t *testing.T) {
  170. name, _ := os.Hostname()
  171. expected := map[protocol.DeviceID]DeviceConfiguration{
  172. device1: {
  173. DeviceID: device1,
  174. Addresses: []string{"192.0.2.1", "192.0.2.2"},
  175. },
  176. device2: {
  177. DeviceID: device2,
  178. Addresses: []string{"192.0.2.3:6070", "[2001:db8::42]:4242"},
  179. },
  180. device3: {
  181. DeviceID: device3,
  182. Addresses: []string{"[2001:db8::44]:4444", "192.0.2.4:6090"},
  183. },
  184. device4: {
  185. DeviceID: device4,
  186. Name: name, // Set when auto created
  187. Addresses: []string{"dynamic"},
  188. },
  189. }
  190. cfg, err := Load("testdata/deviceaddressesstatic.xml", device4)
  191. if err != nil {
  192. t.Error(err)
  193. }
  194. actual := cfg.Devices()
  195. if !reflect.DeepEqual(actual, expected) {
  196. t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, actual)
  197. }
  198. }
  199. func TestVersioningConfig(t *testing.T) {
  200. cfg, err := Load("testdata/versioningconfig.xml", device4)
  201. if err != nil {
  202. t.Error(err)
  203. }
  204. vc := cfg.Folders()["test"].Versioning
  205. if vc.Type != "simple" {
  206. t.Errorf(`vc.Type %q != "simple"`, vc.Type)
  207. }
  208. if l := len(vc.Params); l != 2 {
  209. t.Errorf("len(vc.Params) %d != 2", l)
  210. }
  211. expected := map[string]string{
  212. "foo": "bar",
  213. "baz": "quux",
  214. }
  215. if !reflect.DeepEqual(vc.Params, expected) {
  216. t.Errorf("vc.Params differ;\n E: %#v\n A: %#v", expected, vc.Params)
  217. }
  218. }
  219. func TestNewSaveLoad(t *testing.T) {
  220. path := "testdata/temp.xml"
  221. os.Remove(path)
  222. exists := func(path string) bool {
  223. _, err := os.Stat(path)
  224. return err == nil
  225. }
  226. intCfg := New(device1)
  227. cfg := Wrap(path, intCfg)
  228. // To make the equality pass later
  229. cfg.cfg.XMLName.Local = "configuration"
  230. if exists(path) {
  231. t.Error(path, "exists")
  232. }
  233. err := cfg.Save()
  234. if err != nil {
  235. t.Error(err)
  236. }
  237. if !exists(path) {
  238. t.Error(path, "does not exist")
  239. }
  240. cfg2, err := Load(path, device1)
  241. if err != nil {
  242. t.Error(err)
  243. }
  244. if !reflect.DeepEqual(cfg.Raw(), cfg2.Raw()) {
  245. t.Errorf("Configs are not equal;\n E: %#v\n A: %#v", cfg.Raw(), cfg2.Raw())
  246. }
  247. os.Remove(path)
  248. }
  249. func TestPrepare(t *testing.T) {
  250. var cfg Configuration
  251. if cfg.Folders != nil || cfg.Devices != nil || cfg.Options.ListenAddress != nil {
  252. t.Error("Expected nil")
  253. }
  254. cfg.prepare(device1)
  255. if cfg.Folders == nil || cfg.Devices == nil || cfg.Options.ListenAddress == nil {
  256. t.Error("Unexpected nil")
  257. }
  258. }