local_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 http://mozilla.org/MPL/2.0/.
  6. package discover
  7. import (
  8. "net"
  9. "testing"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. func TestRandomLocalInstanceID(t *testing.T) {
  13. c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{})
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. go c.Serve()
  18. defer c.Stop()
  19. lc := c.(*localClient)
  20. p0 := lc.announcementPkt()
  21. p1 := lc.announcementPkt()
  22. if p0.InstanceID == p1.InstanceID {
  23. t.Error("each generated packet should have a new instance id")
  24. }
  25. }
  26. func TestLocalInstanceIDShouldTriggerNew(t *testing.T) {
  27. c, err := NewLocal(protocol.LocalDeviceID, ":0", &fakeAddressLister{})
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. lc := c.(*localClient)
  32. src := &net.UDPAddr{IP: []byte{10, 20, 30, 40}, Port: 50}
  33. new := lc.registerDevice(src, Announce{
  34. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  35. Addresses: []string{"tcp://0.0.0.0:22000"},
  36. InstanceID: 1234567890,
  37. })
  38. if !new {
  39. t.Fatal("first register should be new")
  40. }
  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("second register should not be new")
  48. }
  49. new = lc.registerDevice(src, Announce{
  50. ID: protocol.DeviceID{42, 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("new device ID should be new")
  56. }
  57. new = lc.registerDevice(src, Announce{
  58. ID: protocol.DeviceID{10, 20, 30, 40, 50, 60, 70, 80, 90},
  59. Addresses: []string{"tcp://0.0.0.0:22000"},
  60. InstanceID: 91234567890,
  61. })
  62. if !new {
  63. t.Fatal("new instance ID should be new")
  64. }
  65. }