local_test.go 2.1 KB

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