2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2013-12-19 23:01:34 +00:00
|
|
|
"errors"
|
|
|
|
"io"
|
2013-12-15 10:43:31 +00:00
|
|
|
"testing"
|
|
|
|
"testing/quick"
|
2014-07-28 10:16:15 +00:00
|
|
|
|
|
|
|
"github.com/calmh/syncthing/xdr"
|
2013-12-15 10:43:31 +00:00
|
|
|
)
|
|
|
|
|
2014-06-29 23:42:03 +00:00
|
|
|
var (
|
|
|
|
c0ID = NewNodeID([]byte{1})
|
|
|
|
c1ID = NewNodeID([]byte{2})
|
|
|
|
)
|
|
|
|
|
2013-12-15 10:43:31 +00:00
|
|
|
func TestHeaderFunctions(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)
|
2014-07-28 10:16:15 +00:00
|
|
|
h0 := header{version: ver, msgID: id, msgType: typ}
|
2013-12-15 10:43:31 +00:00
|
|
|
h1 := decodeHeader(encodeHeader(h0))
|
|
|
|
return h0 == h1
|
|
|
|
}
|
|
|
|
if err := quick.Check(f, nil); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 14:27:39 +00:00
|
|
|
func TestHeaderLayout(t *testing.T) {
|
|
|
|
var e, a uint32
|
|
|
|
|
|
|
|
// Version are the first four bits
|
|
|
|
e = 0xf0000000
|
2014-07-28 10:16:15 +00:00
|
|
|
a = encodeHeader(header{version: 0xf})
|
2014-06-11 14:27:39 +00:00
|
|
|
if a != e {
|
|
|
|
t.Errorf("Header layout incorrect; %08x != %08x", a, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message ID are the following 12 bits
|
|
|
|
e = 0x0fff0000
|
2014-07-28 10:16:15 +00:00
|
|
|
a = encodeHeader(header{msgID: 0xfff})
|
2014-06-11 14:27:39 +00:00
|
|
|
if a != e {
|
|
|
|
t.Errorf("Header layout incorrect; %08x != %08x", a, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type are the last 8 bits before reserved
|
|
|
|
e = 0x0000ff00
|
2014-07-28 10:16:15 +00:00
|
|
|
a = encodeHeader(header{msgType: 0xff})
|
2014-06-11 14:27:39 +00:00
|
|
|
if a != e {
|
|
|
|
t.Errorf("Header layout incorrect; %08x != %08x", a, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-19 23:01:34 +00:00
|
|
|
func TestPing(t *testing.T) {
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-07-28 11:11:09 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, nil, "name", true).(wireFormatConnection).next.(*rawConnection)
|
|
|
|
c1 := NewConnection(c1ID, br, aw, nil, "name", true).(wireFormatConnection).next.(*rawConnection)
|
2013-12-19 23:01:34 +00:00
|
|
|
|
2014-01-09 12:58:35 +00:00
|
|
|
if ok := c0.ping(); !ok {
|
2013-12-19 23:01:34 +00:00
|
|
|
t.Error("c0 ping failed")
|
|
|
|
}
|
2014-01-09 12:58:35 +00:00
|
|
|
if ok := c1.ping(); !ok {
|
2013-12-19 23:01:34 +00:00
|
|
|
t.Error("c1 ping failed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPingErr(t *testing.T) {
|
2014-02-24 12:29:30 +00:00
|
|
|
e := errors.New("something broke")
|
2013-12-19 23:01:34 +00:00
|
|
|
|
2014-07-25 13:16:23 +00:00
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
for j := 0; j < 16; j++ {
|
2014-03-22 16:06:15 +00:00
|
|
|
m0 := newTestModel()
|
|
|
|
m1 := newTestModel()
|
2013-12-19 23:01:34 +00:00
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
|
|
|
|
ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
|
|
|
|
|
2014-07-28 11:11:09 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, ebw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
|
|
|
|
NewConnection(c1ID, br, eaw, m1, "name", true)
|
2013-12-19 23:01:34 +00:00
|
|
|
|
2014-01-09 12:58:35 +00:00
|
|
|
res := c0.ping()
|
2014-07-25 13:16:23 +00:00
|
|
|
if (i < 8 || j < 8) && res {
|
2013-12-19 23:01:34 +00:00
|
|
|
t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
|
2014-07-25 13:16:23 +00:00
|
|
|
} else if (i >= 12 && j >= 12) && !res {
|
2013-12-19 23:01:34 +00:00
|
|
|
t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 13:36:57 +00:00
|
|
|
// func TestRequestResponseErr(t *testing.T) {
|
|
|
|
// e := errors.New("something broke")
|
|
|
|
|
|
|
|
// var pass bool
|
|
|
|
// for i := 0; i < 48; i++ {
|
|
|
|
// for j := 0; j < 38; j++ {
|
|
|
|
// m0 := newTestModel()
|
|
|
|
// m0.data = []byte("response data")
|
|
|
|
// m1 := newTestModel()
|
|
|
|
|
|
|
|
// ar, aw := io.Pipe()
|
|
|
|
// br, bw := io.Pipe()
|
|
|
|
// eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
|
|
|
|
// ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
|
|
|
|
|
2014-06-29 23:42:03 +00:00
|
|
|
// NewConnection(c0ID, ar, ebw, m0, nil)
|
|
|
|
// c1 := NewConnection(c1ID, br, eaw, m1, nil).(wireFormatConnection).next.(*rawConnection)
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
// d, err := c1.Request("default", "tn", 1234, 5678)
|
|
|
|
// if err == e || err == ErrClosed {
|
|
|
|
// t.Logf("Error at %d+%d bytes", i, j)
|
|
|
|
// if !m1.isClosed() {
|
|
|
|
// t.Fatal("c1 not closed")
|
|
|
|
// }
|
|
|
|
// if !m0.isClosed() {
|
|
|
|
// t.Fatal("c0 not closed")
|
|
|
|
// }
|
|
|
|
// continue
|
|
|
|
// }
|
|
|
|
// if err != nil {
|
|
|
|
// t.Fatal(err)
|
|
|
|
// }
|
|
|
|
// if string(d) != "response data" {
|
|
|
|
// t.Fatalf("Incorrect response data %q", string(d))
|
|
|
|
// }
|
|
|
|
// if m0.repo != "default" {
|
|
|
|
// t.Fatalf("Incorrect repo %q", m0.repo)
|
|
|
|
// }
|
|
|
|
// if m0.name != "tn" {
|
|
|
|
// t.Fatalf("Incorrect name %q", m0.name)
|
|
|
|
// }
|
|
|
|
// if m0.offset != 1234 {
|
|
|
|
// t.Fatalf("Incorrect offset %d", m0.offset)
|
|
|
|
// }
|
|
|
|
// if m0.size != 5678 {
|
|
|
|
// t.Fatalf("Incorrect size %d", m0.size)
|
|
|
|
// }
|
|
|
|
// t.Logf("Pass at %d+%d bytes", i, j)
|
|
|
|
// pass = true
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// if !pass {
|
|
|
|
// t.Fatal("Never passed")
|
|
|
|
// }
|
|
|
|
// }
|
2013-12-21 07:06:54 +00:00
|
|
|
|
|
|
|
func TestVersionErr(t *testing.T) {
|
2014-03-22 16:06:15 +00:00
|
|
|
m0 := newTestModel()
|
|
|
|
m1 := newTestModel()
|
2013-12-21 07:06:54 +00:00
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-07-28 11:11:09 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
|
|
|
|
NewConnection(c1ID, br, aw, m1, "name", true)
|
2013-12-21 07:06:54 +00:00
|
|
|
|
2014-07-28 10:16:15 +00:00
|
|
|
w := xdr.NewWriter(c0.cw)
|
|
|
|
w.WriteUint32(encodeHeader(header{
|
2013-12-21 07:06:54 +00:00
|
|
|
version: 2,
|
|
|
|
msgID: 0,
|
|
|
|
msgType: 0,
|
2014-02-20 16:40:15 +00:00
|
|
|
}))
|
2014-07-28 10:16:15 +00:00
|
|
|
w.WriteUint32(0)
|
2013-12-21 07:06:54 +00:00
|
|
|
|
2014-03-22 16:06:15 +00:00
|
|
|
if !m1.isClosed() {
|
2013-12-21 07:06:54 +00:00
|
|
|
t.Error("Connection should close due to unknown version")
|
|
|
|
}
|
|
|
|
}
|
2013-12-21 07:15:19 +00:00
|
|
|
|
|
|
|
func TestTypeErr(t *testing.T) {
|
2014-03-22 16:06:15 +00:00
|
|
|
m0 := newTestModel()
|
|
|
|
m1 := newTestModel()
|
2013-12-21 07:15:19 +00:00
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-07-28 11:11:09 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
|
|
|
|
NewConnection(c1ID, br, aw, m1, "name", true)
|
2013-12-21 07:15:19 +00:00
|
|
|
|
2014-07-28 10:16:15 +00:00
|
|
|
w := xdr.NewWriter(c0.cw)
|
|
|
|
w.WriteUint32(encodeHeader(header{
|
2013-12-21 07:15:19 +00:00
|
|
|
version: 0,
|
|
|
|
msgID: 0,
|
|
|
|
msgType: 42,
|
2014-02-20 16:40:15 +00:00
|
|
|
}))
|
2014-07-28 10:16:15 +00:00
|
|
|
w.WriteUint32(0)
|
2013-12-21 07:15:19 +00:00
|
|
|
|
2014-03-22 16:06:15 +00:00
|
|
|
if !m1.isClosed() {
|
2013-12-21 07:15:19 +00:00
|
|
|
t.Error("Connection should close due to unknown message type")
|
|
|
|
}
|
|
|
|
}
|
2013-12-28 15:30:02 +00:00
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
2014-03-22 16:06:15 +00:00
|
|
|
m0 := newTestModel()
|
|
|
|
m1 := newTestModel()
|
2013-12-28 15:30:02 +00:00
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2014-07-28 11:11:09 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, m0, "name", true).(wireFormatConnection).next.(*rawConnection)
|
|
|
|
NewConnection(c1ID, br, aw, m1, "name", true)
|
2013-12-28 15:30:02 +00:00
|
|
|
|
2014-05-04 06:16:45 +00:00
|
|
|
c0.close(nil)
|
2013-12-28 15:30:02 +00:00
|
|
|
|
2014-05-11 17:30:29 +00:00
|
|
|
<-c0.closed
|
2014-03-22 16:10:26 +00:00
|
|
|
if !m0.isClosed() {
|
2013-12-28 15:30:02 +00:00
|
|
|
t.Fatal("Connection should be closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of these should panic, some should return an error
|
|
|
|
|
2014-03-22 16:10:26 +00:00
|
|
|
if c0.ping() {
|
2013-12-28 15:30:02 +00:00
|
|
|
t.Error("Ping should not return true")
|
|
|
|
}
|
|
|
|
|
2014-02-13 11:41:25 +00:00
|
|
|
c0.Index("default", nil)
|
|
|
|
c0.Index("default", nil)
|
2013-12-28 15:30:02 +00:00
|
|
|
|
2014-03-22 16:10:26 +00:00
|
|
|
if _, err := c0.Request("default", "foo", 0, 0); err == nil {
|
2013-12-28 15:30:02 +00:00
|
|
|
t.Error("Request should return an error")
|
|
|
|
}
|
|
|
|
}
|