hello_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright (C) 2016 The Protocol Authors.
  2. package protocol
  3. import (
  4. "bytes"
  5. "encoding/binary"
  6. "encoding/hex"
  7. "io"
  8. "regexp"
  9. "testing"
  10. )
  11. var spaceRe = regexp.MustCompile(`\s`)
  12. func TestVersion14Hello(t *testing.T) {
  13. // Tests that we can send and receive a version 0.14 hello message.
  14. expected := Hello{
  15. DeviceName: "test device",
  16. ClientName: "syncthing",
  17. ClientVersion: "v0.14.5",
  18. }
  19. msgBuf, err := expected.Marshal()
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. hdrBuf := make([]byte, 6)
  24. binary.BigEndian.PutUint32(hdrBuf, HelloMessageMagic)
  25. binary.BigEndian.PutUint16(hdrBuf[4:], uint16(len(msgBuf)))
  26. outBuf := new(bytes.Buffer)
  27. outBuf.Write(hdrBuf)
  28. outBuf.Write(msgBuf)
  29. inBuf := new(bytes.Buffer)
  30. conn := &readWriter{outBuf, inBuf}
  31. send := &Hello{
  32. DeviceName: "this device",
  33. ClientName: "other client",
  34. ClientVersion: "v0.14.6",
  35. }
  36. res, err := ExchangeHello(conn, send)
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. if res.ClientName != expected.ClientName {
  41. t.Errorf("incorrect ClientName %q != expected %q", res.ClientName, expected.ClientName)
  42. }
  43. if res.ClientVersion != expected.ClientVersion {
  44. t.Errorf("incorrect ClientVersion %q != expected %q", res.ClientVersion, expected.ClientVersion)
  45. }
  46. if res.DeviceName != expected.DeviceName {
  47. t.Errorf("incorrect DeviceName %q != expected %q", res.DeviceName, expected.DeviceName)
  48. }
  49. }
  50. func TestVersion13Hello(t *testing.T) {
  51. // Tests that we can send and receive a version 0.13 hello message.
  52. expected := Version13HelloMessage{
  53. DeviceName: "test device",
  54. ClientName: "syncthing",
  55. ClientVersion: "v0.13.5",
  56. }
  57. msgBuf := expected.MustMarshalXDR()
  58. hdrBuf := make([]byte, 8)
  59. binary.BigEndian.PutUint32(hdrBuf, Version13HelloMagic)
  60. binary.BigEndian.PutUint32(hdrBuf[4:], uint32(len(msgBuf)))
  61. outBuf := new(bytes.Buffer)
  62. outBuf.Write(hdrBuf)
  63. outBuf.Write(msgBuf)
  64. inBuf := new(bytes.Buffer)
  65. conn := &readWriter{outBuf, inBuf}
  66. send := Version13HelloMessage{
  67. DeviceName: "this device",
  68. ClientName: "other client",
  69. ClientVersion: "v0.13.6",
  70. }
  71. res, err := ExchangeHello(conn, send)
  72. if err != ErrTooOldVersion13 {
  73. t.Errorf("unexpected error %v != ErrTooOldVersion13", err)
  74. }
  75. if res.ClientName != expected.ClientName {
  76. t.Errorf("incorrect ClientName %q != expected %q", res.ClientName, expected.ClientName)
  77. }
  78. if res.ClientVersion != expected.ClientVersion {
  79. t.Errorf("incorrect ClientVersion %q != expected %q", res.ClientVersion, expected.ClientVersion)
  80. }
  81. if res.DeviceName != expected.DeviceName {
  82. t.Errorf("incorrect DeviceName %q != expected %q", res.DeviceName, expected.DeviceName)
  83. }
  84. }
  85. func TestVersion12Hello(t *testing.T) {
  86. // Tests that we can correctly interpret the lack of a hello message
  87. // from a v0.12 client.
  88. // This is the typical v0.12 connection start - our message header for a
  89. // ClusterConfig message and then the cluster config message data. Taken
  90. // from a protocol dump of a recent v0.12 client.
  91. msg, _ := hex.DecodeString(spaceRe.ReplaceAllString(`
  92. 00010001
  93. 0000014a
  94. 7802000070000000027332000100a00973796e637468696e670e00b000000876
  95. 302e31322e32352400b00000000764656661756c741e00f01603000000204794
  96. 03ffdef496b5f5e5bc9c0a15221e70073164509fa30761af63094f6f945c3800
  97. 2073312f00f20b0001000000157463703a2f2f3132372e302e302e313a323230
  98. 301f00012400080500003000001000f1122064516fb94d24e7b637d20d9846eb
  99. aeffb09556ef3968c8276fefc3fe24c144c2640002c0000034000f640002021f
  100. 00004f00090400003000001100f11220dff67945f05bdab4270acd6057f1eacf
  101. a3ac93cade07ce6a89384c181ad6b80e640010332b000fc80007021f00012400
  102. 080500046400041400f21f2dc2af5c5f28e38384295f2fc2af2052c3a46b736d
  103. c3b67267c3a57320e58aa8e4bd9c20d090d0b4d180d0b5d18136001f026c01b8
  104. 90000000000000000000`, ``))
  105. outBuf := new(bytes.Buffer)
  106. outBuf.Write(msg)
  107. inBuf := new(bytes.Buffer)
  108. conn := &readWriter{outBuf, inBuf}
  109. send := Version13HelloMessage{
  110. DeviceName: "this device",
  111. ClientName: "other client",
  112. ClientVersion: "v0.13.6",
  113. }
  114. _, err := ExchangeHello(conn, send)
  115. if err != ErrTooOldVersion12 {
  116. t.Errorf("unexpected error %v != ErrTooOldVersion12", err)
  117. }
  118. }
  119. func TestUnknownHello(t *testing.T) {
  120. // Tests that we react correctly to a completely unknown magic number.
  121. // This is an unknown magic follow byte some message data.
  122. msg, _ := hex.DecodeString(spaceRe.ReplaceAllString(`
  123. 12345678
  124. 0000014a
  125. 7802000070000000027332000100a00973796e637468696e670e00b000000876
  126. 302e31322e32352400b00000000764656661756c741e00f01603000000204794
  127. 03ffdef496b5f5e5bc9c0a15221e70073164509fa30761af63094f6f945c3800
  128. 2073312f00f20b0001000000157463703a2f2f3132372e302e302e313a323230
  129. 301f00012400080500003000001000f1122064516fb94d24e7b637d20d9846eb
  130. aeffb09556ef3968c8276fefc3fe24c144c2640002c0000034000f640002021f
  131. 00004f00090400003000001100f11220dff67945f05bdab4270acd6057f1eacf
  132. a3ac93cade07ce6a89384c181ad6b80e640010332b000fc80007021f00012400
  133. 080500046400041400f21f2dc2af5c5f28e38384295f2fc2af2052c3a46b736d
  134. c3b67267c3a57320e58aa8e4bd9c20d090d0b4d180d0b5d18136001f026c01b8
  135. 90000000000000000000`, ``))
  136. outBuf := new(bytes.Buffer)
  137. outBuf.Write(msg)
  138. inBuf := new(bytes.Buffer)
  139. conn := &readWriter{outBuf, inBuf}
  140. send := Version13HelloMessage{
  141. DeviceName: "this device",
  142. ClientName: "other client",
  143. ClientVersion: "v0.13.6",
  144. }
  145. _, err := ExchangeHello(conn, send)
  146. if err != ErrUnknownMagic {
  147. t.Errorf("unexpected error %v != ErrUnknownMagic", err)
  148. }
  149. }
  150. type readWriter struct {
  151. r io.Reader
  152. w io.Writer
  153. }
  154. func (rw *readWriter) Write(data []byte) (int, error) {
  155. return rw.w.Write(data)
  156. }
  157. func (rw *readWriter) Read(data []byte) (int, error) {
  158. return rw.r.Read(data)
  159. }