connections_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright (C) 2016 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package connections
  7. import (
  8. "net/url"
  9. "testing"
  10. "github.com/syncthing/syncthing/lib/config"
  11. "github.com/syncthing/syncthing/lib/protocol"
  12. )
  13. func TestFixupPort(t *testing.T) {
  14. cases := [][2]string{
  15. {"tcp://1.2.3.4:5", "tcp://1.2.3.4:5"},
  16. {"tcp://1.2.3.4:", "tcp://1.2.3.4:22000"},
  17. {"tcp://1.2.3.4", "tcp://1.2.3.4:22000"},
  18. }
  19. for _, tc := range cases {
  20. u0, _ := url.Parse(tc[0])
  21. u1 := fixupPort(u0, 22000).String()
  22. if u1 != tc[1] {
  23. t.Errorf("fixupPort(%q, 22000) => %q, expected %q", tc[0], u1, tc[1])
  24. }
  25. }
  26. }
  27. func TestAllowedNetworks(t *testing.T) {
  28. cases := []struct {
  29. host string
  30. allowed []string
  31. ok bool
  32. }{
  33. {
  34. "192.168.0.1",
  35. nil,
  36. false,
  37. },
  38. {
  39. "192.168.0.1",
  40. []string{},
  41. false,
  42. },
  43. {
  44. "fe80::1",
  45. nil,
  46. false,
  47. },
  48. {
  49. "fe80::1",
  50. []string{},
  51. false,
  52. },
  53. {
  54. "192.168.0.1",
  55. []string{"fe80::/48", "192.168.0.0/24"},
  56. true,
  57. },
  58. {
  59. "fe80::1",
  60. []string{"192.168.0.0/24", "fe80::/48"},
  61. true,
  62. },
  63. {
  64. "192.168.0.1",
  65. []string{"192.168.1.0/24", "fe80::/48"},
  66. false,
  67. },
  68. {
  69. "fe80::1",
  70. []string{"fe82::/48", "192.168.1.0/24"},
  71. false,
  72. },
  73. {
  74. "192.168.0.1:4242",
  75. []string{"fe80::/48", "192.168.0.0/24"},
  76. true,
  77. },
  78. {
  79. "[fe80::1]:4242",
  80. []string{"192.168.0.0/24", "fe80::/48"},
  81. true,
  82. },
  83. {
  84. "10.20.30.40",
  85. []string{"!10.20.30.0/24", "10.0.0.0/8"},
  86. false,
  87. },
  88. {
  89. "10.20.30.40",
  90. []string{"10.0.0.0/8", "!10.20.30.0/24"},
  91. true,
  92. },
  93. {
  94. "[fe80::1]:4242",
  95. []string{"192.168.0.0/24", "!fe00::/8", "fe80::/48"},
  96. false,
  97. },
  98. }
  99. for _, tc := range cases {
  100. res := IsAllowedNetwork(tc.host, tc.allowed)
  101. if res != tc.ok {
  102. t.Errorf("allowedNetwork(%q, %q) == %v, want %v", tc.host, tc.allowed, res, tc.ok)
  103. }
  104. }
  105. }
  106. func TestGetDialer(t *testing.T) {
  107. mustParseURI := func(v string) *url.URL {
  108. uri, err := url.Parse(v)
  109. if err != nil {
  110. panic(err)
  111. }
  112. return uri
  113. }
  114. cases := []struct {
  115. uri *url.URL
  116. ok bool
  117. disabled bool
  118. deprecated bool
  119. }{
  120. {mustParseURI("tcp://1.2.3.4:5678"), true, false, false}, // ok
  121. {mustParseURI("tcp4://1.2.3.4:5678"), true, false, false}, // ok
  122. {mustParseURI("kcp://1.2.3.4:5678"), false, false, true}, // deprecated
  123. {mustParseURI("relay://1.2.3.4:5678"), false, true, false}, // disabled
  124. {mustParseURI("http://1.2.3.4:5678"), false, false, false}, // generally bad
  125. {mustParseURI("bananas!"), false, false, false}, // wat
  126. }
  127. cfg := config.New(protocol.LocalDeviceID)
  128. cfg.Options.RelaysEnabled = false
  129. for _, tc := range cases {
  130. df, err := getDialerFactory(cfg, tc.uri)
  131. if tc.ok && err != nil {
  132. t.Errorf("getDialerFactory(%q) => %v, expected nil err", tc.uri, err)
  133. }
  134. if tc.ok && df == nil {
  135. t.Errorf("getDialerFactory(%q) => nil factory, expected non-nil", tc.uri)
  136. }
  137. if tc.deprecated && err != errDeprecated {
  138. t.Errorf("getDialerFactory(%q) => %v, expected %v", tc.uri, err, errDeprecated)
  139. }
  140. if tc.disabled && err != errDisabled {
  141. t.Errorf("getDialerFactory(%q) => %v, expected %v", tc.uri, err, errDisabled)
  142. }
  143. lf, err := getListenerFactory(cfg, tc.uri)
  144. if tc.ok && err != nil {
  145. t.Errorf("getListenerFactory(%q) => %v, expected nil err", tc.uri, err)
  146. }
  147. if tc.ok && lf == nil {
  148. t.Errorf("getListenerFactory(%q) => nil factory, expected non-nil", tc.uri)
  149. }
  150. if tc.deprecated && err != errDeprecated {
  151. t.Errorf("getListenerFactory(%q) => %v, expected %v", tc.uri, err, errDeprecated)
  152. }
  153. if tc.disabled && err != errDisabled {
  154. t.Errorf("getListenerFactory(%q) => %v, expected %v", tc.uri, err, errDisabled)
  155. }
  156. }
  157. }