FedP2P/peerconn_test.go

211 lines
5.9 KiB
Go
Raw Normal View History

package torrent
import (
"io"
"net"
"sync"
"testing"
"time"
2014-12-26 14:17:00 +08:00
"github.com/anacrolix/missinggo/pubsub"
2019-08-21 18:58:40 +08:00
"github.com/bradfitz/iter"
"github.com/frankban/quicktest"
2019-08-21 18:58:40 +08:00
"github.com/stretchr/testify/require"
"github.com/anacrolix/torrent/metainfo"
pp "github.com/anacrolix/torrent/peer_protocol"
"github.com/anacrolix/torrent/storage"
)
// Ensure that no race exists between sending a bitfield, and a subsequent
// Have that would potentially alter it.
func TestSendBitfieldThenHave(t *testing.T) {
cl := Client{
config: TestingConfig(t),
}
cl.initLogger()
c := cl.newConnection(nil, false, nil, "io.Pipe", "")
c.setTorrent(cl.newTorrent(metainfo.Hash{}, nil))
2018-02-04 16:10:25 +08:00
c.t.setInfo(&metainfo.Info{
Pieces: make([]byte, metainfo.HashSize*3),
})
r, w := io.Pipe()
//c.r = r
c.w = w
go c.writer(time.Minute)
2020-02-21 08:51:24 +08:00
c.locker().Lock()
c.t._completedPieces.Add(1)
c.postBitfield( /*[]bool{false, true, false}*/ )
2020-02-21 08:51:24 +08:00
c.locker().Unlock()
c.locker().Lock()
c.have(2)
2020-02-21 08:51:24 +08:00
c.locker().Unlock()
b := make([]byte, 15)
n, err := io.ReadFull(r, b)
2020-02-21 08:51:24 +08:00
c.locker().Lock()
// This will cause connection.writer to terminate.
c.closed.Set()
2020-02-21 08:51:24 +08:00
c.locker().Unlock()
require.NoError(t, err)
require.EqualValues(t, 15, n)
// Here we see that the bitfield doesn't have piece 2 set, as that should
// arrive in the following Have message.
require.EqualValues(t, "\x00\x00\x00\x02\x05@\x00\x00\x00\x05\x04\x00\x00\x00\x02", string(b))
}
type torrentStorage struct {
writeSem sync.Mutex
}
func (me *torrentStorage) Close() error { return nil }
func (me *torrentStorage) Piece(mp metainfo.Piece) storage.PieceImpl {
return me
}
func (me *torrentStorage) Completion() storage.Completion {
return storage.Completion{}
}
func (me *torrentStorage) MarkComplete() error {
return nil
}
func (me *torrentStorage) MarkNotComplete() error {
return nil
}
func (me *torrentStorage) ReadAt([]byte, int64) (int, error) {
panic("shouldn't be called")
}
func (me *torrentStorage) WriteAt(b []byte, _ int64) (int, error) {
if len(b) != defaultChunkSize {
panic(len(b))
}
me.writeSem.Unlock()
return len(b), nil
}
func BenchmarkConnectionMainReadLoop(b *testing.B) {
c := quicktest.New(b)
cl := &Client{
config: &ClientConfig{
DownloadRateLimiter: unlimited,
},
}
cl.initLogger()
ts := &torrentStorage{}
t := &Torrent{
2018-01-25 14:10:37 +08:00
cl: cl,
2020-01-13 03:26:29 +08:00
storage: &storage.Torrent{TorrentImpl: ts},
pieceStateChanges: pubsub.NewPubSub(),
}
2018-01-25 14:10:37 +08:00
require.NoError(b, t.setInfo(&metainfo.Info{
Pieces: make([]byte, 20),
Length: 1 << 20,
PieceLength: 1 << 20,
}))
t.setChunkSize(defaultChunkSize)
t._pendingPieces.Set(0, PiecePriorityNormal.BitmapPriority())
r, w := net.Pipe()
cn := cl.newConnection(r, true, r.RemoteAddr(), r.RemoteAddr().Network(), regularNetConnPeerConnConnString(r))
cn.setTorrent(t)
mrlErr := make(chan error)
2018-06-25 12:09:08 +08:00
msg := pp.Message{
Type: pp.Piece,
Piece: make([]byte, defaultChunkSize),
}
go func() {
2018-07-25 11:41:50 +08:00
cl.lock()
err := cn.mainReadLoop()
if err != nil {
mrlErr <- err
}
close(mrlErr)
}()
2018-06-25 12:09:08 +08:00
wb := msg.MustMarshalBinary()
b.SetBytes(int64(len(msg.Piece)))
2018-06-25 12:09:08 +08:00
go func() {
defer w.Close()
ts.writeSem.Lock()
2018-06-25 12:09:08 +08:00
for range iter.N(b.N) {
2018-07-25 11:41:50 +08:00
cl.lock()
2018-06-25 12:09:08 +08:00
// The chunk must be written to storage everytime, to ensure the
// writeSem is unlocked.
t.pieces[0]._dirtyChunks.Clear()
cn.validReceiveChunks = map[Request]int{newRequestFromMessage(&msg): 1}
2018-07-25 11:41:50 +08:00
cl.unlock()
2018-06-25 12:09:08 +08:00
n, err := w.Write(wb)
require.NoError(b, err)
require.EqualValues(b, len(wb), n)
ts.writeSem.Lock()
}
}()
c.Assert([]error{nil, io.EOF}, quicktest.Contains, <-mrlErr)
c.Assert(cn._stats.ChunksReadUseful.Int64(), quicktest.Equals, int64(b.N))
}
2020-04-09 00:03:29 +08:00
func TestConnPexPeerFlags(t *testing.T) {
var (
tcpAddr = &net.TCPAddr{IP: net.IPv6loopback, Port: 4848}
udpAddr = &net.UDPAddr{IP: net.IPv6loopback, Port: 4848}
)
var testcases = []struct {
conn *PeerConn
f pp.PexPeerFlags
}{
2021-01-20 10:10:32 +08:00
{&PeerConn{Peer: Peer{outgoing: false, PeerPrefersEncryption: false}}, 0},
{&PeerConn{Peer: Peer{outgoing: false, PeerPrefersEncryption: true}}, pp.PexPrefersEncryption},
{&PeerConn{Peer: Peer{outgoing: true, PeerPrefersEncryption: false}}, pp.PexOutgoingConn},
{&PeerConn{Peer: Peer{outgoing: true, PeerPrefersEncryption: true}}, pp.PexOutgoingConn | pp.PexPrefersEncryption},
{&PeerConn{Peer: Peer{RemoteAddr: udpAddr, Network: udpAddr.Network()}}, pp.PexSupportsUtp},
{&PeerConn{Peer: Peer{RemoteAddr: udpAddr, Network: udpAddr.Network(), outgoing: true}}, pp.PexOutgoingConn | pp.PexSupportsUtp},
{&PeerConn{Peer: Peer{RemoteAddr: tcpAddr, Network: tcpAddr.Network(), outgoing: true}}, pp.PexOutgoingConn},
{&PeerConn{Peer: Peer{RemoteAddr: tcpAddr, Network: tcpAddr.Network()}}, 0},
}
for i, tc := range testcases {
f := tc.conn.pexPeerFlags()
require.EqualValues(t, tc.f, f, i)
}
}
2020-04-09 00:03:29 +08:00
func TestConnPexEvent(t *testing.T) {
var (
udpAddr = &net.UDPAddr{IP: net.IPv6loopback, Port: 4848}
tcpAddr = &net.TCPAddr{IP: net.IPv6loopback, Port: 4848}
dialTcpAddr = &net.TCPAddr{IP: net.IPv6loopback, Port: 4747}
dialUdpAddr = &net.UDPAddr{IP: net.IPv6loopback, Port: 4747}
)
var testcases = []struct {
t pexEventType
c *PeerConn
e pexEvent
}{
{
pexAdd,
&PeerConn{Peer: Peer{RemoteAddr: udpAddr, Network: udpAddr.Network()}},
2020-04-09 00:03:29 +08:00
pexEvent{pexAdd, udpAddr, pp.PexSupportsUtp},
},
{
pexDrop,
&PeerConn{Peer: Peer{RemoteAddr: tcpAddr, Network: tcpAddr.Network(), outgoing: true, PeerListenPort: dialTcpAddr.Port}},
2020-04-09 00:03:29 +08:00
pexEvent{pexDrop, tcpAddr, pp.PexOutgoingConn},
},
{
pexAdd,
&PeerConn{Peer: Peer{RemoteAddr: tcpAddr, Network: tcpAddr.Network(), PeerListenPort: dialTcpAddr.Port}},
2020-04-09 00:03:29 +08:00
pexEvent{pexAdd, dialTcpAddr, 0},
},
{
pexDrop,
&PeerConn{Peer: Peer{RemoteAddr: udpAddr, Network: udpAddr.Network(), PeerListenPort: dialUdpAddr.Port}},
2020-04-09 00:03:29 +08:00
pexEvent{pexDrop, dialUdpAddr, pp.PexSupportsUtp},
},
}
for i, tc := range testcases {
e := tc.c.pexEvent(tc.t)
require.EqualValues(t, tc.e, e, i)
}
}