client_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright (C) 2014 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. "fmt"
  9. "net"
  10. "sync"
  11. "time"
  12. "testing"
  13. "github.com/syncthing/protocol"
  14. )
  15. var device protocol.DeviceID
  16. func init() {
  17. device, _ = protocol.DeviceIDFromString("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2")
  18. }
  19. func TestUDP4Success(t *testing.T) {
  20. conn, err := net.ListenUDP("udp4", nil)
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. port := conn.LocalAddr().(*net.UDPAddr).Port
  25. address := fmt.Sprintf("udp4://127.0.0.1:%d", port)
  26. pkt := &Announce{
  27. Magic: AnnouncementMagic,
  28. This: Device{
  29. device[:],
  30. []Address{{
  31. IP: net.IPv4(123, 123, 123, 123),
  32. Port: 1234,
  33. }},
  34. },
  35. }
  36. client, err := New(address, pkt)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. udpclient := client.(*UDPClient)
  41. if udpclient.errorRetryInterval != DefaultErrorRetryInternval {
  42. t.Fatal("Incorrect retry interval")
  43. }
  44. if udpclient.listenAddress.IP != nil || udpclient.listenAddress.Port != 0 {
  45. t.Fatal("Wrong listen IP or port", udpclient.listenAddress)
  46. }
  47. if client.Address() != address {
  48. t.Fatal("Incorrect address")
  49. }
  50. buf := make([]byte, 2048)
  51. // First announcement
  52. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  53. _, err = conn.Read(buf)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. // Announcement verification
  58. conn.SetDeadline(time.Now().Add(time.Millisecond * 1100))
  59. _, addr, err := conn.ReadFromUDP(buf)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. // Reply to it.
  64. _, err = conn.WriteToUDP(pkt.MustMarshalXDR(), addr)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. // We should get nothing else
  69. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  70. _, err = conn.Read(buf)
  71. if err == nil {
  72. t.Fatal("Expected error")
  73. }
  74. // Status should be ok
  75. if !client.StatusOK() {
  76. t.Fatal("Wrong status")
  77. }
  78. // Do a lookup in a separate routine
  79. addrs := []string{}
  80. wg := sync.WaitGroup{}
  81. wg.Add(1)
  82. go func() {
  83. addrs = client.Lookup(device)
  84. wg.Done()
  85. }()
  86. // Receive the lookup and reply
  87. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  88. _, addr, err = conn.ReadFromUDP(buf)
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. conn.WriteToUDP(pkt.MustMarshalXDR(), addr)
  93. // Wait for the lookup to arrive, verify that the number of answers is correct
  94. wg.Wait()
  95. if len(addrs) != 1 || addrs[0] != "123.123.123.123:1234" {
  96. t.Fatal("Wrong number of answers")
  97. }
  98. client.Stop()
  99. }
  100. func TestUDP4Failure(t *testing.T) {
  101. conn, err := net.ListenUDP("udp4", nil)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. port := conn.LocalAddr().(*net.UDPAddr).Port
  106. address := fmt.Sprintf("udp4://127.0.0.1:%d/?listenaddress=127.0.0.1&retry=5", port)
  107. pkt := &Announce{
  108. Magic: AnnouncementMagic,
  109. This: Device{
  110. device[:],
  111. []Address{{
  112. IP: net.IPv4(123, 123, 123, 123),
  113. Port: 1234,
  114. }},
  115. },
  116. }
  117. client, err := New(address, pkt)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. udpclient := client.(*UDPClient)
  122. if udpclient.errorRetryInterval != time.Second*5 {
  123. t.Fatal("Incorrect retry interval")
  124. }
  125. if !udpclient.listenAddress.IP.Equal(net.IPv4(127, 0, 0, 1)) || udpclient.listenAddress.Port != 0 {
  126. t.Fatal("Wrong listen IP or port", udpclient.listenAddress)
  127. }
  128. if client.Address() != address {
  129. t.Fatal("Incorrect address")
  130. }
  131. buf := make([]byte, 2048)
  132. // First announcement
  133. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  134. _, err = conn.Read(buf)
  135. if err != nil {
  136. t.Fatal(err)
  137. }
  138. // Announcement verification
  139. conn.SetDeadline(time.Now().Add(time.Millisecond * 1100))
  140. _, _, err = conn.ReadFromUDP(buf)
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. // Don't reply
  145. // We should get nothing else
  146. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  147. _, err = conn.Read(buf)
  148. if err == nil {
  149. t.Fatal("Expected error")
  150. }
  151. // Status should be failure
  152. if client.StatusOK() {
  153. t.Fatal("Wrong status")
  154. }
  155. // Do a lookup in a separate routine
  156. addrs := []string{}
  157. wg := sync.WaitGroup{}
  158. wg.Add(1)
  159. go func() {
  160. addrs = client.Lookup(device)
  161. wg.Done()
  162. }()
  163. // Receive the lookup and don't reply
  164. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  165. _, _, err = conn.ReadFromUDP(buf)
  166. if err != nil {
  167. t.Fatal(err)
  168. }
  169. // Wait for the lookup to timeout, verify that the number of answers is none
  170. wg.Wait()
  171. if len(addrs) != 0 {
  172. t.Fatal("Wrong number of answers")
  173. }
  174. client.Stop()
  175. }