syncthing/lib/protocol/hello_test.go
Jakob Borg c6334e61aa
all: Support multiple device connections (fixes #141) (#8918)
This adds the ability to have multiple concurrent connections to a single device. This is primarily useful when the network has multiple physical links for aggregated bandwidth. A single connection will never see a higher rate than a single link can give, but multiple connections are load-balanced over multiple links.

It is also incidentally useful for older multi-core CPUs, where bandwidth could be limited by the TLS performance of a single CPU core -- using multiple connections achieves concurrency in the required crypto calculations...

Co-authored-by: Simon Frei <freisim93@gmail.com>
Co-authored-by: tomasz1986 <twilczynski@naver.com>
Co-authored-by: bt90 <btom1990@googlemail.com>
2023-09-06 12:52:01 +02:00

110 lines
2.3 KiB
Go

// Copyright (C) 2016 The Protocol Authors.
package protocol
import (
"bytes"
"encoding/binary"
"encoding/hex"
"io"
"testing"
)
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)
}
hdrBuf := make([]byte, 6)
binary.BigEndian.PutUint32(hdrBuf, HelloMessageMagic)
binary.BigEndian.PutUint16(hdrBuf[4:], uint16(len(msgBuf)))
outBuf := new(bytes.Buffer)
outBuf.Write(hdrBuf)
outBuf.Write(msgBuf)
inBuf := new(bytes.Buffer)
conn := &readWriter{outBuf, inBuf}
send := Hello{
DeviceName: "this device",
ClientName: "other client",
ClientVersion: "v0.14.6",
Timestamp: 1234567890,
}
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)
}
}
func TestOldHelloMsgs(t *testing.T) {
// Tests that we can correctly identify old/missing/unknown hello
// messages.
cases := []struct {
msg string
err error
}{
{"00010001", ErrTooOldVersion}, // v12
{"9F79BC40", ErrTooOldVersion}, // v13
{"12345678", ErrUnknownMagic},
}
for _, tc := range cases {
msg, _ := hex.DecodeString(tc.msg)
outBuf := new(bytes.Buffer)
outBuf.Write(msg)
inBuf := new(bytes.Buffer)
conn := &readWriter{outBuf, inBuf}
send := Hello{
DeviceName: "this device",
ClientName: "other client",
ClientVersion: "v1.0.0",
Timestamp: 1234567890,
}
_, err := ExchangeHello(conn, send)
if err != tc.err {
t.Errorf("unexpected error %v != %v", err, tc.err)
}
}
}
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)
}