syncthing/lib/protocol/protocol_test.go

409 lines
9.1 KiB
Go
Raw Normal View History

2015-01-13 13:31:14 +01:00
// Copyright (C) 2014 The Protocol Authors.
2014-09-22 21:42:11 +02:00
package protocol
import (
2016-01-12 12:12:06 +01:00
"encoding/binary"
2014-09-22 21:42:11 +02:00
"encoding/hex"
"encoding/json"
2016-01-12 09:19:44 +01:00
"errors"
2014-09-22 21:42:11 +02:00
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
2014-10-21 08:40:05 +02:00
"strings"
2014-09-22 21:42:11 +02:00
"testing"
"testing/quick"
2016-01-12 12:12:06 +01:00
"time"
2014-09-22 21:42:11 +02:00
"github.com/calmh/xdr"
)
var (
c0ID = NewDeviceID([]byte{1})
c1ID = NewDeviceID([]byte{2})
quickCfg = &quick.Config{}
2014-09-22 21:42:11 +02:00
)
2016-01-20 20:37:48 +01:00
func TestHeaderEncodeDecode(t *testing.T) {
2014-09-22 21:42:11 +02:00
f := func(ver, id, typ int) bool {
ver = int(uint(ver) % 16)
id = int(uint(id) % 4096)
typ = int(uint(typ) % 256)
h0 := header{version: ver, msgID: id, msgType: typ}
h1 := decodeHeader(encodeHeader(h0))
return h0 == h1
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
2016-01-20 20:37:48 +01:00
func TestHeaderMarshalUnmarshal(t *testing.T) {
f := func(ver, id, typ int) bool {
ver = int(uint(ver) % 16)
id = int(uint(id) % 4096)
typ = int(uint(typ) % 256)
2016-02-02 12:48:09 +01:00
buf := make([]byte, 4)
2016-01-20 20:37:48 +01:00
h0 := header{version: ver, msgID: id, msgType: typ}
2016-02-02 12:48:09 +01:00
h0.MarshalXDRInto(&xdr.Marshaller{Data: buf})
2016-01-20 20:37:48 +01:00
var h1 header
2016-02-02 12:48:09 +01:00
h1.UnmarshalXDRFrom(&xdr.Unmarshaller{Data: buf})
2016-01-20 20:37:48 +01:00
return h0 == h1
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
2014-09-22 21:42:11 +02:00
func TestHeaderLayout(t *testing.T) {
var e, a uint32
// Version are the first four bits
e = 0xf0000000
a = encodeHeader(header{version: 0xf})
if a != e {
t.Errorf("Header layout incorrect; %08x != %08x", a, e)
}
// Message ID are the following 12 bits
e = 0x0fff0000
a = encodeHeader(header{msgID: 0xfff})
if a != e {
t.Errorf("Header layout incorrect; %08x != %08x", a, e)
}
// Type are the last 8 bits before reserved
e = 0x0000ff00
a = encodeHeader(header{msgType: 0xff})
if a != e {
t.Errorf("Header layout incorrect; %08x != %08x", a, e)
}
}
func TestPing(t *testing.T) {
ar, aw := io.Pipe()
br, bw := io.Pipe()
c0 := NewConnection(c0ID, ar, bw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
2015-07-10 08:34:54 +02:00
c0.Start()
c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
2015-07-10 08:34:54 +02:00
c1.Start()
c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{})
2014-09-22 21:42:11 +02:00
if ok := c0.ping(); !ok {
t.Error("c0 ping failed")
}
if ok := c1.ping(); !ok {
t.Error("c1 ping failed")
}
}
func TestVersionErr(t *testing.T) {
m0 := newTestModel()
m1 := newTestModel()
ar, aw := io.Pipe()
br, bw := io.Pipe()
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
2015-07-10 08:34:54 +02:00
c0.Start()
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
2015-07-10 08:34:54 +02:00
c1.Start()
c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{})
2014-09-22 21:42:11 +02:00
2016-02-02 12:48:09 +01:00
timeoutWriteHeader(c0.cw, header{
2016-01-12 09:19:44 +01:00
version: 2, // higher than supported
2014-09-22 21:42:11 +02:00
msgID: 0,
2016-01-12 09:19:44 +01:00
msgType: messageTypeIndex,
2016-01-12 12:12:06 +01:00
})
2014-09-22 21:42:11 +02:00
2016-01-12 09:19:44 +01:00
if err := m1.closedError(); err == nil || !strings.Contains(err.Error(), "unknown protocol version") {
t.Error("Connection should close due to unknown version, not", err)
2014-09-22 21:42:11 +02:00
}
}
func TestTypeErr(t *testing.T) {
m0 := newTestModel()
m1 := newTestModel()
ar, aw := io.Pipe()
br, bw := io.Pipe()
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
2015-07-10 08:34:54 +02:00
c0.Start()
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
2015-07-10 08:34:54 +02:00
c1.Start()
c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{})
2014-09-22 21:42:11 +02:00
2016-02-02 12:48:09 +01:00
timeoutWriteHeader(c0.cw, header{
2014-09-22 21:42:11 +02:00
version: 0,
msgID: 0,
2016-01-12 12:12:06 +01:00
msgType: 42, // unknown type
})
2014-09-22 21:42:11 +02:00
2016-01-12 09:19:44 +01:00
if err := m1.closedError(); err == nil || !strings.Contains(err.Error(), "unknown message type") {
t.Error("Connection should close due to unknown message type, not", err)
2014-09-22 21:42:11 +02:00
}
}
func TestClose(t *testing.T) {
m0 := newTestModel()
m1 := newTestModel()
ar, aw := io.Pipe()
br, bw := io.Pipe()
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
2015-07-10 08:34:54 +02:00
c0.Start()
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
2015-07-10 08:34:54 +02:00
c1.Start()
c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{})
2014-09-22 21:42:11 +02:00
2016-01-12 09:19:44 +01:00
c0.close(errors.New("manual close"))
2014-09-22 21:42:11 +02:00
<-c0.closed
2016-01-12 09:19:44 +01:00
if err := m0.closedError(); err == nil || !strings.Contains(err.Error(), "manual close") {
2014-09-22 21:42:11 +02:00
t.Fatal("Connection should be closed")
}
// None of these should panic, some should return an error
if c0.ping() {
t.Error("Ping should not return true")
}
c0.Index("default", nil, 0, nil)
c0.Index("default", nil, 0, nil)
2014-09-22 21:42:11 +02:00
if _, err := c0.Request("default", "foo", 0, 0, nil, false); err == nil {
2014-09-22 21:42:11 +02:00
t.Error("Request should return an error")
}
}
func TestMarshalIndexMessage(t *testing.T) {
2016-03-08 09:07:18 +01:00
if testing.Short() {
quickCfg.MaxCount = 10
}
2014-09-22 21:42:11 +02:00
f := func(m1 IndexMessage) bool {
2016-02-02 12:48:09 +01:00
if len(m1.Options) == 0 {
m1.Options = nil
}
2016-03-04 14:16:27 +01:00
if len(m1.Files) == 0 {
m1.Files = nil
}
2015-08-28 09:01:21 +02:00
for i, f := range m1.Files {
m1.Files[i].CachedSize = 0
2016-02-02 12:48:09 +01:00
if len(f.Blocks) == 0 {
m1.Files[i].Blocks = nil
} else {
for j := range f.Blocks {
f.Blocks[j].Offset = 0
if len(f.Blocks[j].Hash) == 0 {
f.Blocks[j].Hash = nil
}
2014-09-22 21:42:11 +02:00
}
}
}
return testMarshal(t, "index", &m1, &IndexMessage{})
}
if err := quick.Check(f, quickCfg); err != nil {
t.Error(err)
}
}
func TestMarshalRequestMessage(t *testing.T) {
2016-03-08 09:07:18 +01:00
if testing.Short() {
quickCfg.MaxCount = 10
}
2014-09-22 21:42:11 +02:00
f := func(m1 RequestMessage) bool {
2016-02-02 12:48:09 +01:00
if len(m1.Options) == 0 {
m1.Options = nil
}
2016-03-04 14:16:27 +01:00
if len(m1.Hash) == 0 {
m1.Hash = nil
}
2014-09-22 21:42:11 +02:00
return testMarshal(t, "request", &m1, &RequestMessage{})
}
if err := quick.Check(f, quickCfg); err != nil {
t.Error(err)
}
}
func TestMarshalResponseMessage(t *testing.T) {
2016-03-08 09:07:18 +01:00
if testing.Short() {
quickCfg.MaxCount = 10
}
2014-09-22 21:42:11 +02:00
f := func(m1 ResponseMessage) bool {
if len(m1.Data) == 0 {
m1.Data = nil
}
return testMarshal(t, "response", &m1, &ResponseMessage{})
}
if err := quick.Check(f, quickCfg); err != nil {
t.Error(err)
}
}
func TestMarshalClusterConfigMessage(t *testing.T) {
2016-03-08 09:07:18 +01:00
if testing.Short() {
quickCfg.MaxCount = 10
}
2014-09-22 21:42:11 +02:00
f := func(m1 ClusterConfigMessage) bool {
2016-02-02 12:48:09 +01:00
if len(m1.Options) == 0 {
m1.Options = nil
}
2016-03-04 14:16:27 +01:00
if len(m1.Folders) == 0 {
m1.Folders = nil
}
for i := range m1.Folders {
if len(m1.Folders[i].Devices) == 0 {
m1.Folders[i].Devices = nil
}
if len(m1.Folders[i].Options) == 0 {
m1.Folders[i].Options = nil
}
}
2014-09-22 21:42:11 +02:00
return testMarshal(t, "clusterconfig", &m1, &ClusterConfigMessage{})
}
if err := quick.Check(f, quickCfg); err != nil {
t.Error(err)
}
}
func TestMarshalCloseMessage(t *testing.T) {
2016-03-08 09:07:18 +01:00
if testing.Short() {
quickCfg.MaxCount = 10
}
2014-09-22 21:42:11 +02:00
f := func(m1 CloseMessage) bool {
return testMarshal(t, "close", &m1, &CloseMessage{})
}
if err := quick.Check(f, quickCfg); err != nil {
t.Error(err)
}
}
type message interface {
2016-02-02 12:48:09 +01:00
MarshalXDR() ([]byte, error)
UnmarshalXDR([]byte) error
2014-09-22 21:42:11 +02:00
}
func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
failed := func(bc []byte) {
bs, _ := json.MarshalIndent(m1, "", " ")
ioutil.WriteFile(prefix+"-1.txt", bs, 0644)
bs, _ = json.MarshalIndent(m2, "", " ")
ioutil.WriteFile(prefix+"-2.txt", bs, 0644)
if len(bc) > 0 {
f, _ := os.Create(prefix + "-data.txt")
fmt.Fprint(f, hex.Dump(bc))
f.Close()
}
}
2016-02-02 12:48:09 +01:00
buf, err := m1.MarshalXDR()
2014-10-21 08:40:05 +02:00
if err != nil && strings.Contains(err.Error(), "exceeds size") {
2014-09-22 21:42:11 +02:00
return true
}
if err != nil {
failed(nil)
t.Fatal(err)
}
2016-02-02 12:48:09 +01:00
err = m2.UnmarshalXDR(buf)
2014-09-22 21:42:11 +02:00
if err != nil {
2016-02-02 12:48:09 +01:00
failed(buf)
2014-09-22 21:42:11 +02:00
t.Fatal(err)
}
ok := reflect.DeepEqual(m1, m2)
if !ok {
2016-02-02 12:48:09 +01:00
failed(buf)
2014-09-22 21:42:11 +02:00
}
return ok
}
2016-01-12 12:12:06 +01:00
2016-02-02 12:48:09 +01:00
func timeoutWriteHeader(w io.Writer, hdr header) {
2016-01-12 12:12:06 +01:00
// This tries to write a message header to w, but times out after a while.
// This is useful because in testing, with a PipeWriter, it will block
// forever if the other side isn't reading any more. On the other hand we
// can't just "go" it into the background, because if the other side is
// still there we should wait for the write to complete. Yay.
var buf [8]byte // header and message length
binary.BigEndian.PutUint32(buf[:], encodeHeader(hdr))
binary.BigEndian.PutUint32(buf[4:], 0) // zero message length, explicitly
done := make(chan struct{})
go func() {
2016-02-02 12:48:09 +01:00
w.Write(buf[:])
2016-01-12 12:12:06 +01:00
close(done)
}()
select {
case <-done:
case <-time.After(250 * time.Millisecond):
}
}
2016-01-20 20:37:48 +01:00
func TestFileInfoSize(t *testing.T) {
fi := FileInfo{
Blocks: []BlockInfo{
{Size: 42},
{Offset: 42, Size: 23},
{Offset: 42 + 23, Size: 34},
},
}
size := fi.Size()
want := int64(42 + 23 + 34)
if size != want {
t.Errorf("Incorrect size reported, got %d, want %d", size, want)
}
size = fi.Size() // Cached, this time
if size != want {
t.Errorf("Incorrect cached size reported, got %d, want %d", size, want)
}
fi.CachedSize = 8
want = 8
size = fi.Size() // Ensure it came from the cache
if size != want {
t.Errorf("Incorrect cached size reported, got %d, want %d", size, want)
}
fi.CachedSize = 0
fi.Flags = FlagDirectory
want = 128
size = fi.Size() // Directories are 128 bytes large
if size != want {
t.Errorf("Incorrect cached size reported, got %d, want %d", size, want)
}
fi.CachedSize = 0
fi.Flags = FlagDeleted
want = 128
size = fi.Size() // Also deleted files
if size != want {
t.Errorf("Incorrect cached size reported, got %d, want %d", size, want)
}
}