2015-01-13 12:31:14 +00:00
|
|
|
// Copyright (C) 2014 The Protocol Authors.
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2016-01-12 08:19:44 +00:00
|
|
|
"errors"
|
2015-10-21 12:35:59 +00:00
|
|
|
"flag"
|
2014-09-22 19:42:11 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
2014-10-21 06:40:05 +00:00
|
|
|
"strings"
|
2014-09-22 19:42:11 +00:00
|
|
|
"testing"
|
|
|
|
"testing/quick"
|
|
|
|
|
|
|
|
"github.com/calmh/xdr"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-10-21 12:35:59 +00:00
|
|
|
c0ID = NewDeviceID([]byte{1})
|
|
|
|
c1ID = NewDeviceID([]byte{2})
|
|
|
|
quickCfg = &quick.Config{}
|
2014-09-22 19:42:11 +00:00
|
|
|
)
|
|
|
|
|
2015-10-21 12:35:59 +00:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
flag.Parse()
|
2015-10-21 12:45:18 +00:00
|
|
|
if flag.Lookup("test.short").Value.String() != "false" {
|
2015-10-21 12:35:59 +00:00
|
|
|
quickCfg.MaxCount = 10
|
|
|
|
}
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2014-09-22 19:42:11 +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)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2016-01-11 15:49:44 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
|
2015-07-10 06:34:54 +00:00
|
|
|
c0.Start()
|
2016-01-11 15:49:44 +00:00
|
|
|
c1 := NewConnection(c1ID, br, aw, newTestModel(), "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
|
2015-07-10 06:34:54 +00:00
|
|
|
c1.Start()
|
2015-06-26 13:38:56 +00:00
|
|
|
c0.ClusterConfig(ClusterConfigMessage{})
|
|
|
|
c1.ClusterConfig(ClusterConfigMessage{})
|
2014-09-22 19:42:11 +00: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()
|
|
|
|
|
2016-01-11 15:49:44 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
|
2015-07-10 06:34:54 +00:00
|
|
|
c0.Start()
|
2015-06-26 13:38:56 +00:00
|
|
|
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
|
2015-07-10 06:34:54 +00:00
|
|
|
c1.Start()
|
2015-06-26 13:38:56 +00:00
|
|
|
c0.ClusterConfig(ClusterConfigMessage{})
|
|
|
|
c1.ClusterConfig(ClusterConfigMessage{})
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
w := xdr.NewWriter(c0.cw)
|
|
|
|
w.WriteUint32(encodeHeader(header{
|
2016-01-12 08:19:44 +00:00
|
|
|
version: 2, // higher than supported
|
2014-09-22 19:42:11 +00:00
|
|
|
msgID: 0,
|
2016-01-12 08:19:44 +00:00
|
|
|
msgType: messageTypeIndex,
|
2014-09-22 19:42:11 +00:00
|
|
|
}))
|
2015-01-08 21:11:10 +00:00
|
|
|
w.WriteUint32(0) // Avoids reader closing due to EOF
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-01-12 08:19:44 +00: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 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTypeErr(t *testing.T) {
|
|
|
|
m0 := newTestModel()
|
|
|
|
m1 := newTestModel()
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2016-01-11 15:49:44 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
|
2015-07-10 06:34:54 +00:00
|
|
|
c0.Start()
|
2015-06-26 13:38:56 +00:00
|
|
|
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
|
2015-07-10 06:34:54 +00:00
|
|
|
c1.Start()
|
2015-06-26 13:38:56 +00:00
|
|
|
c0.ClusterConfig(ClusterConfigMessage{})
|
|
|
|
c1.ClusterConfig(ClusterConfigMessage{})
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
w := xdr.NewWriter(c0.cw)
|
|
|
|
w.WriteUint32(encodeHeader(header{
|
|
|
|
version: 0,
|
|
|
|
msgID: 0,
|
|
|
|
msgType: 42,
|
|
|
|
}))
|
2015-01-08 21:11:10 +00:00
|
|
|
w.WriteUint32(0) // Avoids reader closing due to EOF
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-01-12 08:19:44 +00: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 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClose(t *testing.T) {
|
|
|
|
m0 := newTestModel()
|
|
|
|
m1 := newTestModel()
|
|
|
|
|
|
|
|
ar, aw := io.Pipe()
|
|
|
|
br, bw := io.Pipe()
|
|
|
|
|
2016-01-11 15:49:44 +00:00
|
|
|
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
|
2015-07-10 06:34:54 +00:00
|
|
|
c0.Start()
|
2015-06-26 13:38:56 +00:00
|
|
|
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
|
2015-07-10 06:34:54 +00:00
|
|
|
c1.Start()
|
2015-06-26 13:38:56 +00:00
|
|
|
c0.ClusterConfig(ClusterConfigMessage{})
|
|
|
|
c1.ClusterConfig(ClusterConfigMessage{})
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2016-01-12 08:19:44 +00:00
|
|
|
c0.close(errors.New("manual close"))
|
2014-09-22 19:42:11 +00:00
|
|
|
|
|
|
|
<-c0.closed
|
2016-01-12 08:19:44 +00:00
|
|
|
if err := m0.closedError(); err == nil || !strings.Contains(err.Error(), "manual close") {
|
2014-09-22 19:42:11 +00: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")
|
|
|
|
}
|
|
|
|
|
2015-02-04 22:15:17 +00:00
|
|
|
c0.Index("default", nil, 0, nil)
|
|
|
|
c0.Index("default", nil, 0, nil)
|
2014-09-22 19:42:11 +00:00
|
|
|
|
2015-01-24 21:56:12 +00:00
|
|
|
if _, err := c0.Request("default", "foo", 0, 0, nil, 0, nil); err == nil {
|
2014-09-22 19:42:11 +00:00
|
|
|
t.Error("Request should return an error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestElementSizeExceededNested(t *testing.T) {
|
|
|
|
m := ClusterConfigMessage{
|
2015-09-24 17:51:59 +00:00
|
|
|
ClientName: "longstringlongstringlongstringinglongstringlongstringlonlongstringlongstringlon",
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
_, err := m.EncodeXDR(ioutil.Discard)
|
|
|
|
if err == nil {
|
2014-09-28 11:00:38 +00:00
|
|
|
t.Errorf("ID length %d > max 64, but no error", len(m.Folders[0].ID))
|
2014-09-22 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalIndexMessage(t *testing.T) {
|
|
|
|
f := func(m1 IndexMessage) bool {
|
2015-08-28 07:01:21 +00:00
|
|
|
for i, f := range m1.Files {
|
|
|
|
m1.Files[i].CachedSize = 0
|
|
|
|
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 19:42:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return testMarshal(t, "index", &m1, &IndexMessage{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := quick.Check(f, quickCfg); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalRequestMessage(t *testing.T) {
|
|
|
|
f := func(m1 RequestMessage) bool {
|
|
|
|
return testMarshal(t, "request", &m1, &RequestMessage{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := quick.Check(f, quickCfg); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalResponseMessage(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
f := func(m1 ClusterConfigMessage) bool {
|
|
|
|
return testMarshal(t, "clusterconfig", &m1, &ClusterConfigMessage{})
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := quick.Check(f, quickCfg); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshalCloseMessage(t *testing.T) {
|
|
|
|
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 {
|
|
|
|
EncodeXDR(io.Writer) (int, error)
|
|
|
|
DecodeXDR(io.Reader) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMarshal(t *testing.T, prefix string, m1, m2 message) bool {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := m1.EncodeXDR(&buf)
|
2014-10-21 06:40:05 +00:00
|
|
|
if err != nil && strings.Contains(err.Error(), "exceeds size") {
|
2014-09-22 19:42:11 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
failed(nil)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
bc := make([]byte, len(buf.Bytes()))
|
|
|
|
copy(bc, buf.Bytes())
|
|
|
|
|
|
|
|
err = m2.DecodeXDR(&buf)
|
|
|
|
if err != nil {
|
|
|
|
failed(bc)
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ok := reflect.DeepEqual(m1, m2)
|
|
|
|
if !ok {
|
|
|
|
failed(bc)
|
|
|
|
}
|
|
|
|
return ok
|
|
|
|
}
|