From 69f4c5a7e9c731d76b3bedce112feb8ca8e6ffe0 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Fri, 16 Sep 2016 12:13:06 +1000 Subject: [PATCH] Minor comments and test improvement --- config.go | 5 +++-- mse/mse.go | 11 +++++++---- mse/mse_test.go | 8 +++++++- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 9ea76416..8b180fdc 100644 --- a/config.go +++ b/config.go @@ -33,8 +33,9 @@ type Config struct { DisableUTP bool // For the bittorrent protocol. DisableTCP bool `long:"disable-tcp"` - // Called to instantiate storage for each added torrent. Provided backends - // are in $REPO/data. If not set, the "file" implementation is used. + // Called to instantiate storage for each added torrent. Builtin backends + // are in the storage package. If not set, the "file" implementation is + // used. DefaultStorage storage.ClientImpl DisableEncryption bool `long:"disable-encryption"` diff --git a/mse/mse.go b/mse/mse.go index 46b59a48..d366aa75 100644 --- a/mse/mse.go +++ b/mse/mse.go @@ -183,13 +183,14 @@ func newPadLen() int64 { return ret } +// Manages state for both initiating and receiving handshakes. type handshake struct { conn io.ReadWriter s [96]byte - initer bool - skeys [][]byte - skey []byte - ia []byte // Initial payload. Only used by the initiator. + initer bool // Whether we're initiating or receiving. + skeys [][]byte // Skeys we'll accept if receiving. + skey []byte // Skey we're initiating with. + ia []byte // Initial payload. Only used by the initiator. writeMu sync.Mutex writes [][]byte @@ -311,6 +312,8 @@ func suffixMatchLen(a, b []byte) int { return 0 } +// Reads from r until b has been seen. Keeps the minimum amount of data in +// memory. func readUntil(r io.Reader, b []byte) error { b1 := make([]byte, len(b)) i := 0 diff --git a/mse/mse_test.go b/mse/mse_test.go index d74dec97..6c279954 100644 --- a/mse/mse_test.go +++ b/mse/mse_test.go @@ -10,6 +10,7 @@ import ( "testing" "github.com/bradfitz/iter" + "github.com/stretchr/testify/require" ) func TestReadUntil(t *testing.T) { @@ -117,5 +118,10 @@ func (tr *trackReader) Read(b []byte) (n int, err error) { func TestReceiveRandomData(t *testing.T) { tr := trackReader{rand.Reader, 0} - ReceiveHandshake(readWriter{&tr, ioutil.Discard}, nil) + _, err := ReceiveHandshake(readWriter{&tr, ioutil.Discard}, nil) + // No skey matches + require.Error(t, err) + // Establishing S, and then reading the maximum padding for giving up on + // synchronizing. + require.EqualValues(t, 96+532, tr.n) } -- 2.44.0