config_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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("test", 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. cfg, err := Load("testdata/"+ver+".xml", device1)
  56. if err != nil {
  57. t.Error(err)
  58. }
  59. expectedFolders := []FolderConfiguration{
  60. {
  61. ID: "test",
  62. Path: "~/Sync",
  63. Devices: []FolderDeviceConfiguration{{DeviceID: device1}, {DeviceID: device4}},
  64. ReadOnly: true,
  65. RescanIntervalS: 600,
  66. },
  67. }
  68. expectedDevices := []DeviceConfiguration{
  69. {
  70. DeviceID: device1,
  71. Name: "node one",
  72. Addresses: []string{"a"},
  73. Compression: true,
  74. },
  75. {
  76. DeviceID: device4,
  77. Name: "node two",
  78. Addresses: []string{"b"},
  79. Compression: true,
  80. },
  81. }
  82. expectedDeviceIDs := []protocol.DeviceID{device1, device4}
  83. if cfg.Version != 5 {
  84. t.Errorf("%d: Incorrect version %d != 5", i, cfg.Version)
  85. }
  86. if !reflect.DeepEqual(cfg.Folders, expectedFolders) {
  87. t.Errorf("%d: Incorrect Folders\n A: %#v\n E: %#v", i, cfg.Folders, expectedFolders)
  88. }
  89. if !reflect.DeepEqual(cfg.Devices, expectedDevices) {
  90. t.Errorf("%d: Incorrect Devices\n A: %#v\n E: %#v", i, cfg.Devices, expectedDevices)
  91. }
  92. if !reflect.DeepEqual(cfg.Folders[0].DeviceIDs(), expectedDeviceIDs) {
  93. t.Errorf("%d: Incorrect DeviceIDs\n A: %#v\n E: %#v", i, cfg.Folders[0].DeviceIDs(), expectedDeviceIDs)
  94. }
  95. if len(cfg.DeviceMap()) != len(expectedDevices) {
  96. t.Errorf("Unexpected number of DeviceMap() entries")
  97. }
  98. if len(cfg.FolderMap()) != len(expectedFolders) {
  99. t.Errorf("Unexpected number of FolderMap() entries")
  100. }
  101. }
  102. }
  103. func TestNoListenAddress(t *testing.T) {
  104. cfg, err := Load("testdata/nolistenaddress.xml", device1)
  105. if err != nil {
  106. t.Error(err)
  107. }
  108. expected := []string{""}
  109. if !reflect.DeepEqual(cfg.Options.ListenAddress, expected) {
  110. t.Errorf("Unexpected ListenAddress %#v", cfg.Options.ListenAddress)
  111. }
  112. }
  113. func TestOverriddenValues(t *testing.T) {
  114. expected := OptionsConfiguration{
  115. ListenAddress: []string{":23000"},
  116. GlobalAnnServer: "syncthing.nym.se:22026",
  117. GlobalAnnEnabled: false,
  118. LocalAnnEnabled: false,
  119. LocalAnnPort: 42123,
  120. LocalAnnMCAddr: "quux:3232",
  121. MaxSendKbps: 1234,
  122. MaxRecvKbps: 2341,
  123. ReconnectIntervalS: 6000,
  124. StartBrowser: false,
  125. UPnPEnabled: false,
  126. UPnPLease: 60,
  127. UPnPRenewal: 15,
  128. RestartOnWakeup: false,
  129. AutoUpgradeIntervalH: 24,
  130. KeepTemporariesH: 48,
  131. }
  132. cfg, err := Load("testdata/overridenvalues.xml", device1)
  133. if err != nil {
  134. t.Error(err)
  135. }
  136. if !reflect.DeepEqual(cfg.Options, expected) {
  137. t.Errorf("Overridden config differs;\n E: %#v\n A: %#v", expected, cfg.Options)
  138. }
  139. }
  140. func TestDeviceAddressesDynamic(t *testing.T) {
  141. name, _ := os.Hostname()
  142. expected := []DeviceConfiguration{
  143. {
  144. DeviceID: device1,
  145. Addresses: []string{"dynamic"},
  146. Compression: true,
  147. },
  148. {
  149. DeviceID: device2,
  150. Addresses: []string{"dynamic"},
  151. Compression: true,
  152. },
  153. {
  154. DeviceID: device3,
  155. Addresses: []string{"dynamic"},
  156. Compression: true,
  157. },
  158. {
  159. DeviceID: device4,
  160. Name: name, // Set when auto created
  161. Addresses: []string{"dynamic"},
  162. },
  163. }
  164. cfg, err := Load("testdata/deviceaddressesdynamic.xml", device4)
  165. if err != nil {
  166. t.Error(err)
  167. }
  168. if !reflect.DeepEqual(cfg.Devices, expected) {
  169. t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, cfg.Devices)
  170. }
  171. }
  172. func TestDeviceAddressesStatic(t *testing.T) {
  173. name, _ := os.Hostname()
  174. expected := []DeviceConfiguration{
  175. {
  176. DeviceID: device1,
  177. Addresses: []string{"192.0.2.1", "192.0.2.2"},
  178. },
  179. {
  180. DeviceID: device2,
  181. Addresses: []string{"192.0.2.3:6070", "[2001:db8::42]:4242"},
  182. },
  183. {
  184. DeviceID: device3,
  185. Addresses: []string{"[2001:db8::44]:4444", "192.0.2.4:6090"},
  186. },
  187. {
  188. DeviceID: device4,
  189. Name: name, // Set when auto created
  190. Addresses: []string{"dynamic"},
  191. },
  192. }
  193. cfg, err := Load("testdata/deviceaddressesstatic.xml", device4)
  194. if err != nil {
  195. t.Error(err)
  196. }
  197. if !reflect.DeepEqual(cfg.Devices, expected) {
  198. t.Errorf("Devices differ;\n E: %#v\n A: %#v", expected, cfg.Devices)
  199. }
  200. }
  201. func TestVersioningConfig(t *testing.T) {
  202. cfg, err := Load("testdata/versioningconfig.xml", device4)
  203. if err != nil {
  204. t.Error(err)
  205. }
  206. vc := cfg.Folders[0].Versioning
  207. if vc.Type != "simple" {
  208. t.Errorf(`vc.Type %q != "simple"`, vc.Type)
  209. }
  210. if l := len(vc.Params); l != 2 {
  211. t.Errorf("len(vc.Params) %d != 2", l)
  212. }
  213. expected := map[string]string{
  214. "foo": "bar",
  215. "baz": "quux",
  216. }
  217. if !reflect.DeepEqual(vc.Params, expected) {
  218. t.Errorf("vc.Params differ;\n E: %#v\n A: %#v", expected, vc.Params)
  219. }
  220. }
  221. func TestNewSaveLoad(t *testing.T) {
  222. path := "testdata/temp.xml"
  223. os.Remove(path)
  224. exists := func(path string) bool {
  225. _, err := os.Stat(path)
  226. return err == nil
  227. }
  228. cfg := New(path, device1)
  229. // To make the equality pass later
  230. cfg.XMLName.Local = "configuration"
  231. if exists(path) {
  232. t.Error(path, "exists")
  233. }
  234. err := cfg.Save()
  235. if err != nil {
  236. t.Error(err)
  237. }
  238. if !exists(path) {
  239. t.Error(path, "does not exist")
  240. }
  241. cfg2, err := Load(path, device1)
  242. if err != nil {
  243. t.Error(err)
  244. }
  245. if !reflect.DeepEqual(cfg, cfg2) {
  246. t.Errorf("Configs are not equal;\n E: %#v\n A: %#v", cfg, cfg2)
  247. }
  248. cfg.GUI.User = "test"
  249. cfg.Save()
  250. cfg2, err = Load(path, device1)
  251. if err != nil {
  252. t.Error(err)
  253. }
  254. if cfg2.GUI.User != "test" || !reflect.DeepEqual(cfg, cfg2) {
  255. t.Errorf("Configs are not equal;\n E: %#v\n A: %#v", cfg, cfg2)
  256. }
  257. os.Remove(path)
  258. }
  259. func TestPrepare(t *testing.T) {
  260. var cfg Configuration
  261. if cfg.Folders != nil || cfg.Devices != nil || cfg.Options.ListenAddress != nil {
  262. t.Error("Expected nil")
  263. }
  264. cfg.prepare(device1)
  265. if cfg.Folders == nil || cfg.Devices == nil || cfg.Options.ListenAddress == nil {
  266. t.Error("Unexpected nil")
  267. }
  268. }