local_test.go 2.0 KB

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