bep_hello_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "bytes"
  9. "encoding/binary"
  10. "encoding/hex"
  11. "io"
  12. "testing"
  13. "google.golang.org/protobuf/proto"
  14. )
  15. func TestVersion14Hello(t *testing.T) {
  16. // Tests that we can send and receive a version 0.14 hello message.
  17. expected := Hello{
  18. DeviceName: "test device",
  19. ClientName: "syncthing",
  20. ClientVersion: "v0.14.5",
  21. }
  22. msgBuf, err := proto.Marshal(expected.toWire())
  23. if err != nil {
  24. t.Fatal(err)
  25. }
  26. hdrBuf := make([]byte, 6)
  27. binary.BigEndian.PutUint32(hdrBuf, HelloMessageMagic)
  28. binary.BigEndian.PutUint16(hdrBuf[4:], uint16(len(msgBuf)))
  29. outBuf := new(bytes.Buffer)
  30. outBuf.Write(hdrBuf)
  31. outBuf.Write(msgBuf)
  32. inBuf := new(bytes.Buffer)
  33. conn := &readWriter{outBuf, inBuf}
  34. send := Hello{
  35. DeviceName: "this device",
  36. ClientName: "other client",
  37. ClientVersion: "v0.14.6",
  38. Timestamp: 1234567890,
  39. }
  40. res, err := ExchangeHello(conn, send)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if res.ClientName != expected.ClientName {
  45. t.Errorf("incorrect ClientName %q != expected %q", res.ClientName, expected.ClientName)
  46. }
  47. if res.ClientVersion != expected.ClientVersion {
  48. t.Errorf("incorrect ClientVersion %q != expected %q", res.ClientVersion, expected.ClientVersion)
  49. }
  50. if res.DeviceName != expected.DeviceName {
  51. t.Errorf("incorrect DeviceName %q != expected %q", res.DeviceName, expected.DeviceName)
  52. }
  53. }
  54. func TestOldHelloMsgs(t *testing.T) {
  55. // Tests that we can correctly identify old/missing/unknown hello
  56. // messages.
  57. cases := []struct {
  58. msg string
  59. err error
  60. }{
  61. {"00010001", ErrTooOldVersion}, // v12
  62. {"9F79BC40", ErrTooOldVersion}, // v13
  63. {"12345678", ErrUnknownMagic},
  64. }
  65. for _, tc := range cases {
  66. msg, _ := hex.DecodeString(tc.msg)
  67. outBuf := new(bytes.Buffer)
  68. outBuf.Write(msg)
  69. inBuf := new(bytes.Buffer)
  70. conn := &readWriter{outBuf, inBuf}
  71. send := Hello{
  72. DeviceName: "this device",
  73. ClientName: "other client",
  74. ClientVersion: "v1.0.0",
  75. Timestamp: 1234567890,
  76. }
  77. _, err := ExchangeHello(conn, send)
  78. if err != tc.err {
  79. t.Errorf("unexpected error %v != %v", err, tc.err)
  80. }
  81. }
  82. }
  83. type readWriter struct {
  84. r io.Reader
  85. w io.Writer
  86. }
  87. func (rw *readWriter) Write(data []byte) (int, error) {
  88. return rw.w.Write(data)
  89. }
  90. func (rw *readWriter) Read(data []byte) (int, error) {
  91. return rw.r.Read(data)
  92. }