local_test.go 3.5 KB

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