apisrv_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2018 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 main
  7. import (
  8. "fmt"
  9. "net"
  10. "testing"
  11. )
  12. func TestFixupAddresses(t *testing.T) {
  13. cases := []struct {
  14. remote *net.TCPAddr
  15. in []string
  16. out []string
  17. }{
  18. { // verbatim passthrough
  19. in: []string{"tcp://1.2.3.4:22000"},
  20. out: []string{"tcp://1.2.3.4:22000"},
  21. }, { // unspecified replaced by remote
  22. remote: addr("1.2.3.4", 22000),
  23. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  24. out: []string{"tcp://1.2.3.4:22000", "tcp://192.0.2.42:22000"},
  25. }, { // unspecified not used as replacement
  26. remote: addr("0.0.0.0", 22000),
  27. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  28. out: []string{"tcp://192.0.2.42:22000"},
  29. }, { // unspecified not used as replacement
  30. remote: addr("::", 22000),
  31. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  32. out: []string{"tcp://192.0.2.42:22000"},
  33. }, { // localhost not used as replacement
  34. remote: addr("127.0.0.1", 22000),
  35. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  36. out: []string{"tcp://192.0.2.42:22000"},
  37. }, { // localhost not used as replacement
  38. remote: addr("::1", 22000),
  39. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  40. out: []string{"tcp://192.0.2.42:22000"},
  41. }, { // multicast not used as replacement
  42. remote: addr("224.0.0.1", 22000),
  43. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  44. out: []string{"tcp://192.0.2.42:22000"},
  45. }, { // multicast not used as replacement
  46. remote: addr("ff80::42", 22000),
  47. in: []string{"tcp://:22000", "tcp://192.0.2.42:22000"},
  48. out: []string{"tcp://192.0.2.42:22000"},
  49. }, { // explicitly announced weirdness is also filtered
  50. remote: addr("192.0.2.42", 22000),
  51. in: []string{"tcp://:22000", "tcp://127.1.2.3:22000", "tcp://[::1]:22000", "tcp://[ff80::42]:22000"},
  52. out: []string{"tcp://192.0.2.42:22000"},
  53. }, { // port remapping
  54. remote: addr("123.123.123.123", 9000),
  55. in: []string{"tcp://0.0.0.0:0"},
  56. out: []string{"tcp://123.123.123.123:9000"},
  57. }, { // unspecified port remapping
  58. remote: addr("123.123.123.123", 9000),
  59. in: []string{"tcp://:0"},
  60. out: []string{"tcp://123.123.123.123:9000"},
  61. }, { // empty remapping
  62. remote: addr("123.123.123.123", 9000),
  63. in: []string{"tcp://"},
  64. out: []string{},
  65. }, { // port only remapping
  66. remote: addr("123.123.123.123", 9000),
  67. in: []string{"tcp://44.44.44.44:0"},
  68. out: []string{"tcp://44.44.44.44:9000"},
  69. }, { // remote ip nil
  70. remote: addr("", 9000),
  71. in: []string{"tcp://:22000", "tcp://44.44.44.44:9000"},
  72. out: []string{"tcp://44.44.44.44:9000"},
  73. }, { // remote port 0
  74. remote: addr("123.123.123.123", 0),
  75. in: []string{"tcp://:22000", "tcp://44.44.44.44"},
  76. out: []string{"tcp://123.123.123.123:22000"},
  77. },
  78. }
  79. for _, tc := range cases {
  80. out := fixupAddresses(tc.remote, tc.in)
  81. if fmt.Sprint(out) != fmt.Sprint(tc.out) {
  82. t.Errorf("fixupAddresses(%v, %v) => %v, expected %v", tc.remote, tc.in, out, tc.out)
  83. }
  84. }
  85. }
  86. func addr(host string, port int) *net.TCPAddr {
  87. return &net.TCPAddr{
  88. IP: net.ParseIP(host),
  89. Port: port,
  90. }
  91. }