apisrv_test.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. },
  70. }
  71. for _, tc := range cases {
  72. out := fixupAddresses(tc.remote, tc.in)
  73. if fmt.Sprint(out) != fmt.Sprint(tc.out) {
  74. t.Errorf("fixupAddresses(%v, %v) => %v, expected %v", tc.remote, tc.in, out, tc.out)
  75. }
  76. }
  77. }
  78. func addr(host string, port int) *net.TCPAddr {
  79. return &net.TCPAddr{
  80. IP: net.ParseIP(host),
  81. Port: port,
  82. }
  83. }