2016-06-09 10:50:14 +00:00
|
|
|
// Copyright (C) 2016 The Protocol Authors.
|
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
func TestVersion14Hello(t *testing.T) {
|
|
|
|
// Tests that we can send and receive a version 0.14 hello message.
|
|
|
|
|
|
|
|
expected := Hello{
|
|
|
|
DeviceName: "test device",
|
|
|
|
ClientName: "syncthing",
|
|
|
|
ClientVersion: "v0.14.5",
|
|
|
|
}
|
|
|
|
msgBuf, err := expected.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2016-07-17 21:41:20 +00:00
|
|
|
hdrBuf := make([]byte, 6)
|
2016-07-04 10:40:29 +00:00
|
|
|
binary.BigEndian.PutUint32(hdrBuf, HelloMessageMagic)
|
2016-07-17 21:41:20 +00:00
|
|
|
binary.BigEndian.PutUint16(hdrBuf[4:], uint16(len(msgBuf)))
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
outBuf := new(bytes.Buffer)
|
|
|
|
outBuf.Write(hdrBuf)
|
|
|
|
outBuf.Write(msgBuf)
|
|
|
|
|
|
|
|
inBuf := new(bytes.Buffer)
|
|
|
|
|
|
|
|
conn := &readWriter{outBuf, inBuf}
|
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
send := Hello{
|
2016-07-04 10:40:29 +00:00
|
|
|
DeviceName: "this device",
|
|
|
|
ClientName: "other client",
|
|
|
|
ClientVersion: "v0.14.6",
|
2023-09-06 10:52:01 +00:00
|
|
|
Timestamp: 1234567890,
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res, err := ExchangeHello(conn, send)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.ClientName != expected.ClientName {
|
|
|
|
t.Errorf("incorrect ClientName %q != expected %q", res.ClientName, expected.ClientName)
|
|
|
|
}
|
|
|
|
if res.ClientVersion != expected.ClientVersion {
|
|
|
|
t.Errorf("incorrect ClientVersion %q != expected %q", res.ClientVersion, expected.ClientVersion)
|
|
|
|
}
|
|
|
|
if res.DeviceName != expected.DeviceName {
|
|
|
|
t.Errorf("incorrect DeviceName %q != expected %q", res.DeviceName, expected.DeviceName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
func TestOldHelloMsgs(t *testing.T) {
|
|
|
|
// Tests that we can correctly identify old/missing/unknown hello
|
|
|
|
// messages.
|
2016-06-09 10:50:14 +00:00
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
cases := []struct {
|
|
|
|
msg string
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{"00010001", ErrTooOldVersion}, // v12
|
|
|
|
{"9F79BC40", ErrTooOldVersion}, // v13
|
|
|
|
{"12345678", ErrUnknownMagic},
|
2016-06-09 10:50:14 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
msg, _ := hex.DecodeString(tc.msg)
|
2016-06-09 10:50:14 +00:00
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
outBuf := new(bytes.Buffer)
|
|
|
|
outBuf.Write(msg)
|
2016-06-09 10:50:14 +00:00
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
inBuf := new(bytes.Buffer)
|
2016-06-09 10:50:14 +00:00
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
conn := &readWriter{outBuf, inBuf}
|
2016-06-09 10:50:14 +00:00
|
|
|
|
2023-09-06 10:52:01 +00:00
|
|
|
send := Hello{
|
2019-01-17 19:48:43 +00:00
|
|
|
DeviceName: "this device",
|
|
|
|
ClientName: "other client",
|
|
|
|
ClientVersion: "v1.0.0",
|
2023-09-06 10:52:01 +00:00
|
|
|
Timestamp: 1234567890,
|
2019-01-17 19:48:43 +00:00
|
|
|
}
|
2016-06-09 10:50:14 +00:00
|
|
|
|
2019-01-17 19:48:43 +00:00
|
|
|
_, err := ExchangeHello(conn, send)
|
|
|
|
if err != tc.err {
|
|
|
|
t.Errorf("unexpected error %v != %v", err, tc.err)
|
|
|
|
}
|
2016-06-09 10:50:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type readWriter struct {
|
|
|
|
r io.Reader
|
|
|
|
w io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *readWriter) Write(data []byte) (int, error) {
|
|
|
|
return rw.w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rw *readWriter) Read(data []byte) (int, error) {
|
|
|
|
return rw.r.Read(data)
|
|
|
|
}
|