hello_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 TestVersion13Hello(t *testing.T) {
  13. // Tests that we can send and receive a version 0.13 hello message.
  14. expected := Version13HelloMessage{
  15. DeviceName: "test device",
  16. ClientName: "syncthing",
  17. ClientVersion: "v0.13.5",
  18. }
  19. msgBuf := expected.MustMarshalXDR()
  20. hdrBuf := make([]byte, 8)
  21. binary.BigEndian.PutUint32(hdrBuf, Version13HelloMagic)
  22. binary.BigEndian.PutUint32(hdrBuf[4:], uint32(len(msgBuf)))
  23. outBuf := new(bytes.Buffer)
  24. outBuf.Write(hdrBuf)
  25. outBuf.Write(msgBuf)
  26. inBuf := new(bytes.Buffer)
  27. conn := &readWriter{outBuf, inBuf}
  28. send := Version13HelloMessage{
  29. DeviceName: "this device",
  30. ClientName: "other client",
  31. ClientVersion: "v0.13.6",
  32. }
  33. res, err := ExchangeHello(conn, send)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if res.ClientName != expected.ClientName {
  38. t.Errorf("incorrect ClientName %q != expected %q", res.ClientName, expected.ClientName)
  39. }
  40. if res.ClientVersion != expected.ClientVersion {
  41. t.Errorf("incorrect ClientVersion %q != expected %q", res.ClientVersion, expected.ClientVersion)
  42. }
  43. if res.DeviceName != expected.DeviceName {
  44. t.Errorf("incorrect DeviceName %q != expected %q", res.DeviceName, expected.DeviceName)
  45. }
  46. }
  47. func TestVersion12Hello(t *testing.T) {
  48. // Tests that we can correctly interpret the lack of a hello message
  49. // from a v0.12 client.
  50. // This is the typical v0.12 connection start - our message header for a
  51. // ClusterConfig message and then the cluster config message data. Taken
  52. // from a protocol dump of a recent v0.12 client.
  53. msg, _ := hex.DecodeString(spaceRe.ReplaceAllString(`
  54. 00010001
  55. 0000014a
  56. 7802000070000000027332000100a00973796e637468696e670e00b000000876
  57. 302e31322e32352400b00000000764656661756c741e00f01603000000204794
  58. 03ffdef496b5f5e5bc9c0a15221e70073164509fa30761af63094f6f945c3800
  59. 2073312f00f20b0001000000157463703a2f2f3132372e302e302e313a323230
  60. 301f00012400080500003000001000f1122064516fb94d24e7b637d20d9846eb
  61. aeffb09556ef3968c8276fefc3fe24c144c2640002c0000034000f640002021f
  62. 00004f00090400003000001100f11220dff67945f05bdab4270acd6057f1eacf
  63. a3ac93cade07ce6a89384c181ad6b80e640010332b000fc80007021f00012400
  64. 080500046400041400f21f2dc2af5c5f28e38384295f2fc2af2052c3a46b736d
  65. c3b67267c3a57320e58aa8e4bd9c20d090d0b4d180d0b5d18136001f026c01b8
  66. 90000000000000000000`, ``))
  67. outBuf := new(bytes.Buffer)
  68. outBuf.Write(msg)
  69. inBuf := new(bytes.Buffer)
  70. conn := &readWriter{outBuf, inBuf}
  71. send := Version13HelloMessage{
  72. DeviceName: "this device",
  73. ClientName: "other client",
  74. ClientVersion: "v0.13.6",
  75. }
  76. _, err := ExchangeHello(conn, send)
  77. if err != ErrTooOldVersion12 {
  78. t.Errorf("unexpected error %v != ErrTooOld", err)
  79. }
  80. }
  81. func TestUnknownHello(t *testing.T) {
  82. // Tests that we react correctly to a completely unknown magic number.
  83. // This is an unknown magic follow byte some message data.
  84. msg, _ := hex.DecodeString(spaceRe.ReplaceAllString(`
  85. 12345678
  86. 0000014a
  87. 7802000070000000027332000100a00973796e637468696e670e00b000000876
  88. 302e31322e32352400b00000000764656661756c741e00f01603000000204794
  89. 03ffdef496b5f5e5bc9c0a15221e70073164509fa30761af63094f6f945c3800
  90. 2073312f00f20b0001000000157463703a2f2f3132372e302e302e313a323230
  91. 301f00012400080500003000001000f1122064516fb94d24e7b637d20d9846eb
  92. aeffb09556ef3968c8276fefc3fe24c144c2640002c0000034000f640002021f
  93. 00004f00090400003000001100f11220dff67945f05bdab4270acd6057f1eacf
  94. a3ac93cade07ce6a89384c181ad6b80e640010332b000fc80007021f00012400
  95. 080500046400041400f21f2dc2af5c5f28e38384295f2fc2af2052c3a46b736d
  96. c3b67267c3a57320e58aa8e4bd9c20d090d0b4d180d0b5d18136001f026c01b8
  97. 90000000000000000000`, ``))
  98. outBuf := new(bytes.Buffer)
  99. outBuf.Write(msg)
  100. inBuf := new(bytes.Buffer)
  101. conn := &readWriter{outBuf, inBuf}
  102. send := Version13HelloMessage{
  103. DeviceName: "this device",
  104. ClientName: "other client",
  105. ClientVersion: "v0.13.6",
  106. }
  107. _, err := ExchangeHello(conn, send)
  108. if err != ErrUnknownMagic {
  109. t.Errorf("unexpected error %v != ErrUnknownMagic", err)
  110. }
  111. }
  112. type readWriter struct {
  113. r io.Reader
  114. w io.Writer
  115. }
  116. func (rw *readWriter) Write(data []byte) (int, error) {
  117. return rw.w.Write(data)
  118. }
  119. func (rw *readWriter) Read(data []byte) (int, error) {
  120. return rw.r.Read(data)
  121. }