local_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 discover
  7. import (
  8. "bytes"
  9. "context"
  10. "fmt"
  11. "net"
  12. "testing"
  13. "github.com/syncthing/syncthing/lib/events"
  14. "github.com/syncthing/syncthing/lib/protocol"
  15. )
  16. func TestLocalInstanceID(t *testing.T) {
  17. c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{}, events.NoopLogger)
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. ctx, cancel := context.WithCancel(context.Background())
  22. go c.Serve(ctx)
  23. defer cancel()
  24. lc := c.(*localClient)
  25. p0, ok := lc.announcementPkt(1, nil)
  26. if !ok {
  27. t.Fatal("unexpectedly not ok")
  28. }
  29. p1, ok := lc.announcementPkt(2, nil)
  30. if !ok {
  31. t.Fatal("unexpectedly not ok")
  32. }
  33. if bytes.Equal(p0, p1) {
  34. t.Error("each generated packet should have a new instance id")
  35. }
  36. }
  37. func TestLocalInstanceIDShouldTriggerNew(t *testing.T) {
  38. c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{}, events.NoopLogger)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. lc := c.(*localClient)
  43. src := &net.UDPAddr{IP: []byte{10, 20, 30, 40}, Port: 50}
  44. new := lc.registerDevice(src, Announce{
  45. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  46. Addresses: []string{"tcp://0.0.0.0:22000"},
  47. InstanceID: 1234567890,
  48. })
  49. if !new {
  50. t.Fatal("first register should be new")
  51. }
  52. new = lc.registerDevice(src, Announce{
  53. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  54. Addresses: []string{"tcp://0.0.0.0:22000"},
  55. InstanceID: 1234567890,
  56. })
  57. if new {
  58. t.Fatal("second register should not be new")
  59. }
  60. new = lc.registerDevice(src, Announce{
  61. ID: protocol.DeviceID{42, 10, 20, 30, 40, 50, 60, 70, 80, 90},
  62. Addresses: []string{"tcp://0.0.0.0:22000"},
  63. InstanceID: 1234567890,
  64. })
  65. if !new {
  66. t.Fatal("new device ID should be new")
  67. }
  68. new = lc.registerDevice(src, Announce{
  69. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  70. Addresses: []string{"tcp://0.0.0.0:22000"},
  71. InstanceID: 91234567890,
  72. })
  73. if !new {
  74. t.Fatal("new instance ID should be new")
  75. }
  76. }
  77. func TestFilterUndialable(t *testing.T) {
  78. addrs := []string{
  79. "quic://[2001:db8::1]:22000", // OK
  80. "tcp://192.0.2.42:22000", // OK
  81. "quic://[2001:db8::1]:0", // remove, port zero
  82. "tcp://192.0.2.42:0", // remove, port zero
  83. "quic://[::]:22000", // OK
  84. "tcp://0.0.0.0:22000", // OK
  85. "tcp://[2001:db8::1]", // remove, no port
  86. "tcp://192.0.2.42", // remove, no port
  87. "tcp://foo:bar", // remove, host/port does not resolve
  88. "tcp://127.0.0.1:22000", // remove, not usable from outside
  89. "tcp://[::1]:22000", // remove, not usable from outside
  90. "tcp://224.1.2.3:22000", // remove, not usable from outside (multicast)
  91. "tcp://[fe80::9ef:dff1:b332:5e56]:55681", // OK
  92. "pure garbage", // remove, garbage
  93. "", // remove, garbage
  94. }
  95. exp := []string{
  96. "quic://[2001:db8::1]:22000",
  97. "tcp://192.0.2.42:22000",
  98. "quic://[::]:22000",
  99. "tcp://0.0.0.0:22000",
  100. "tcp://[fe80::9ef:dff1:b332:5e56]:55681",
  101. }
  102. res := filterUndialableLocal(addrs)
  103. if fmt.Sprint(res) != fmt.Sprint(exp) {
  104. t.Log(res)
  105. t.Error("filterUndialableLocal returned invalid addresses")
  106. }
  107. }