client_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package discover
  16. import (
  17. "fmt"
  18. "net"
  19. "sync"
  20. "time"
  21. "testing"
  22. "github.com/syncthing/protocol"
  23. )
  24. var device protocol.DeviceID
  25. func init() {
  26. device, _ = protocol.DeviceIDFromString("P56IOI7-MZJNU2Y-IQGDREY-DM2MGTI-MGL3BXN-PQ6W5BM-TBBZ4TJ-XZWICQ2")
  27. }
  28. func TestUDP4Success(t *testing.T) {
  29. conn, err := net.ListenUDP("udp4", nil)
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. port := conn.LocalAddr().(*net.UDPAddr).Port
  34. address := fmt.Sprintf("udp4://127.0.0.1:%d", port)
  35. pkt := &Announce{
  36. Magic: AnnouncementMagic,
  37. This: Device{
  38. device[:],
  39. []Address{{
  40. IP: net.IPv4(123, 123, 123, 123),
  41. Port: 1234,
  42. }},
  43. },
  44. }
  45. client, err := New(address, pkt)
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. udpclient := client.(*UDPClient)
  50. if udpclient.errorRetryInterval != DefaultErrorRetryInternval {
  51. t.Fatal("Incorrect retry interval")
  52. }
  53. if udpclient.listenAddress.IP != nil || udpclient.listenAddress.Port != 0 {
  54. t.Fatal("Wrong listen IP or port", udpclient.listenAddress)
  55. }
  56. if client.Address() != address {
  57. t.Fatal("Incorrect address")
  58. }
  59. buf := make([]byte, 2048)
  60. // First announcement
  61. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  62. _, err = conn.Read(buf)
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. // Announcement verification
  67. conn.SetDeadline(time.Now().Add(time.Millisecond * 1100))
  68. _, addr, err := conn.ReadFromUDP(buf)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. // Reply to it.
  73. _, err = conn.WriteToUDP(pkt.MustMarshalXDR(), addr)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. // We should get nothing else
  78. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  79. _, err = conn.Read(buf)
  80. if err == nil {
  81. t.Fatal("Expected error")
  82. }
  83. // Status should be ok
  84. if !client.StatusOK() {
  85. t.Fatal("Wrong status")
  86. }
  87. // Do a lookup in a separate routine
  88. addrs := []string{}
  89. wg := sync.WaitGroup{}
  90. wg.Add(1)
  91. go func() {
  92. addrs = client.Lookup(device)
  93. wg.Done()
  94. }()
  95. // Receive the lookup and reply
  96. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  97. _, addr, err = conn.ReadFromUDP(buf)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. conn.WriteToUDP(pkt.MustMarshalXDR(), addr)
  102. // Wait for the lookup to arrive, verify that the number of answers is correct
  103. wg.Wait()
  104. if len(addrs) != 1 || addrs[0] != "123.123.123.123:1234" {
  105. t.Fatal("Wrong number of answers")
  106. }
  107. client.Stop()
  108. }
  109. func TestUDP4Failure(t *testing.T) {
  110. conn, err := net.ListenUDP("udp4", nil)
  111. if err != nil {
  112. t.Fatal(err)
  113. }
  114. port := conn.LocalAddr().(*net.UDPAddr).Port
  115. address := fmt.Sprintf("udp4://127.0.0.1:%d/?listenaddress=127.0.0.1&retry=5", port)
  116. pkt := &Announce{
  117. Magic: AnnouncementMagic,
  118. This: Device{
  119. device[:],
  120. []Address{{
  121. IP: net.IPv4(123, 123, 123, 123),
  122. Port: 1234,
  123. }},
  124. },
  125. }
  126. client, err := New(address, pkt)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. udpclient := client.(*UDPClient)
  131. if udpclient.errorRetryInterval != time.Second*5 {
  132. t.Fatal("Incorrect retry interval")
  133. }
  134. if !udpclient.listenAddress.IP.Equal(net.IPv4(127, 0, 0, 1)) || udpclient.listenAddress.Port != 0 {
  135. t.Fatal("Wrong listen IP or port", udpclient.listenAddress)
  136. }
  137. if client.Address() != address {
  138. t.Fatal("Incorrect address")
  139. }
  140. buf := make([]byte, 2048)
  141. // First announcement
  142. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  143. _, err = conn.Read(buf)
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. // Announcement verification
  148. conn.SetDeadline(time.Now().Add(time.Millisecond * 1100))
  149. _, _, err = conn.ReadFromUDP(buf)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. // Don't reply
  154. // We should get nothing else
  155. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  156. _, err = conn.Read(buf)
  157. if err == nil {
  158. t.Fatal("Expected error")
  159. }
  160. // Status should be failure
  161. if client.StatusOK() {
  162. t.Fatal("Wrong status")
  163. }
  164. // Do a lookup in a separate routine
  165. addrs := []string{}
  166. wg := sync.WaitGroup{}
  167. wg.Add(1)
  168. go func() {
  169. addrs = client.Lookup(device)
  170. wg.Done()
  171. }()
  172. // Receive the lookup and don't reply
  173. conn.SetDeadline(time.Now().Add(time.Millisecond * 100))
  174. _, _, err = conn.ReadFromUDP(buf)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. // Wait for the lookup to timeout, verify that the number of answers is none
  179. wg.Wait()
  180. if len(addrs) != 0 {
  181. t.Fatal("Wrong number of answers")
  182. }
  183. client.Stop()
  184. }