local_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "net"
  11. "testing"
  12. "github.com/syncthing/syncthing/lib/events"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. )
  15. func TestLocalInstanceID(t *testing.T) {
  16. c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{}, events.NoopLogger)
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. ctx, cancel := context.WithCancel(context.Background())
  21. go c.Serve(ctx)
  22. defer cancel()
  23. lc := c.(*localClient)
  24. p0, ok := lc.announcementPkt(1, nil)
  25. if !ok {
  26. t.Fatal("unexpectedly not ok")
  27. }
  28. p1, ok := lc.announcementPkt(2, nil)
  29. if !ok {
  30. t.Fatal("unexpectedly not ok")
  31. }
  32. if bytes.Equal(p0, p1) {
  33. t.Error("each generated packet should have a new instance id")
  34. }
  35. }
  36. func TestLocalInstanceIDShouldTriggerNew(t *testing.T) {
  37. c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{}, events.NoopLogger)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. lc := c.(*localClient)
  42. src := &net.UDPAddr{IP: []byte{10, 20, 30, 40}, Port: 50}
  43. new := lc.registerDevice(src, Announce{
  44. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  45. Addresses: []string{"tcp://0.0.0.0:22000"},
  46. InstanceID: 1234567890,
  47. })
  48. if !new {
  49. t.Fatal("first register should be new")
  50. }
  51. new = lc.registerDevice(src, Announce{
  52. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  53. Addresses: []string{"tcp://0.0.0.0:22000"},
  54. InstanceID: 1234567890,
  55. })
  56. if new {
  57. t.Fatal("second register should not be new")
  58. }
  59. new = lc.registerDevice(src, Announce{
  60. ID: protocol.DeviceID{42, 10, 20, 30, 40, 50, 60, 70, 80, 90},
  61. Addresses: []string{"tcp://0.0.0.0:22000"},
  62. InstanceID: 1234567890,
  63. })
  64. if !new {
  65. t.Fatal("new device ID should be new")
  66. }
  67. new = lc.registerDevice(src, Announce{
  68. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  69. Addresses: []string{"tcp://0.0.0.0:22000"},
  70. InstanceID: 91234567890,
  71. })
  72. if !new {
  73. t.Fatal("new instance ID should be new")
  74. }
  75. }