connections_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "testing"
  8. import "net/url"
  9. func TestFixupPort(t *testing.T) {
  10. cases := [][2]string{
  11. {"tcp://1.2.3.4:5", "tcp://1.2.3.4:5"},
  12. {"tcp://1.2.3.4:", "tcp://1.2.3.4:22000"},
  13. {"tcp://1.2.3.4", "tcp://1.2.3.4:22000"},
  14. }
  15. for _, tc := range cases {
  16. u0, _ := url.Parse(tc[0])
  17. u1 := fixupPort(u0, 22000).String()
  18. if u1 != tc[1] {
  19. t.Errorf("fixupPort(%q, 22000) => %q, expected %q", tc[0], u1, tc[1])
  20. }
  21. }
  22. }
  23. func TestAllowedNetworks(t *testing.T) {
  24. cases := []struct {
  25. host string
  26. allowed []string
  27. ok bool
  28. }{
  29. {
  30. "192.168.0.1",
  31. nil,
  32. false,
  33. },
  34. {
  35. "192.168.0.1",
  36. []string{},
  37. false,
  38. },
  39. {
  40. "fe80::1",
  41. nil,
  42. false,
  43. },
  44. {
  45. "fe80::1",
  46. []string{},
  47. false,
  48. },
  49. {
  50. "192.168.0.1",
  51. []string{"fe80::/48", "192.168.0.0/24"},
  52. true,
  53. },
  54. {
  55. "fe80::1",
  56. []string{"192.168.0.0/24", "fe80::/48"},
  57. true,
  58. },
  59. {
  60. "192.168.0.1",
  61. []string{"192.168.1.0/24", "fe80::/48"},
  62. false,
  63. },
  64. {
  65. "fe80::1",
  66. []string{"fe82::/48", "192.168.1.0/24"},
  67. false,
  68. },
  69. {
  70. "192.168.0.1:4242",
  71. []string{"fe80::/48", "192.168.0.0/24"},
  72. true,
  73. },
  74. {
  75. "[fe80::1]:4242",
  76. []string{"192.168.0.0/24", "fe80::/48"},
  77. true,
  78. },
  79. {
  80. "10.20.30.40",
  81. []string{"!10.20.30.0/24", "10.0.0.0/8"},
  82. false,
  83. },
  84. {
  85. "10.20.30.40",
  86. []string{"10.0.0.0/8", "!10.20.30.0/24"},
  87. true,
  88. },
  89. {
  90. "[fe80::1]:4242",
  91. []string{"192.168.0.0/24", "!fe00::/8", "fe80::/48"},
  92. false,
  93. },
  94. }
  95. for _, tc := range cases {
  96. res := IsAllowedNetwork(tc.host, tc.allowed)
  97. if res != tc.ok {
  98. t.Errorf("allowedNetwork(%q, %q) == %v, want %v", tc.host, tc.allowed, res, tc.ok)
  99. }
  100. }
  101. }