apisrv_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.IP
  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: net.ParseIP("1.2.3.4"),
  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: net.ParseIP("0.0.0.0"),
  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: net.ParseIP("::"),
  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: net.ParseIP("127.0.0.1"),
  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: net.ParseIP("::1"),
  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: net.ParseIP("224.0.0.1"),
  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: net.ParseIP("ff80::42"),
  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: net.ParseIP("192.0.2.42"),
  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. },
  54. }
  55. for _, tc := range cases {
  56. out := fixupAddresses(tc.remote, tc.in)
  57. if fmt.Sprint(out) != fmt.Sprint(tc.out) {
  58. t.Errorf("fixupAddresses(%v, %v) => %v, expected %v", tc.remote, tc.in, out, tc.out)
  59. }
  60. }
  61. }