From 83a8284d6aba889cc9b4d476dfc94726dd3f5c64 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 12 Jan 2022 15:01:33 +1100 Subject: [PATCH] Add tests for preferred network direction --- peerconn.go | 4 ++-- peerconn_test.go | 35 +++++++++++++++++++++++++++++++++++ torrent.go | 2 +- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/peerconn.go b/peerconn.go index 6866d450..d1ebf3a4 100644 --- a/peerconn.go +++ b/peerconn.go @@ -221,12 +221,12 @@ func (cn *PeerConn) isPreferredDirection() bool { // Returns whether the left connection should be preferred over the right one, // considering only their networking properties. If ok is false, we can't // decide. -func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) (left, ok bool) { +func (l *PeerConn) hasPreferredNetworkOver(r *PeerConn) bool { var ml multiLess ml.NextBool(l.isPreferredDirection(), r.isPreferredDirection()) ml.NextBool(!l.utp(), !r.utp()) ml.NextBool(l.ipv6(), r.ipv6()) - return ml.FinalOk() + return ml.Less() } func (cn *Peer) cumInterest() time.Duration { diff --git a/peerconn_test.go b/peerconn_test.go index 2d5aa681..b1ac1dbc 100644 --- a/peerconn_test.go +++ b/peerconn_test.go @@ -1,7 +1,9 @@ package torrent import ( + "encoding/binary" "errors" + "fmt" "io" "net" "sync" @@ -246,3 +248,36 @@ func TestApplyRequestStateWriteBufferConstraints(t *testing.T) { c.Check(maxLocalToRemoteRequests >= 8, qt.IsTrue) c.Logf("max local to remote requests: %v", maxLocalToRemoteRequests) } + +func peerConnForPreferredNetworkDirection(localPeerId, remotePeerId int, outgoing, utp, ipv6 bool) *PeerConn { + pc := PeerConn{} + pc.outgoing = outgoing + if utp { + pc.Network = "udp" + } + if ipv6 { + pc.RemoteAddr = &net.TCPAddr{IP: net.ParseIP(fmt.Sprintf("::420"))} + } else { + pc.RemoteAddr = &net.TCPAddr{IP: net.IPv4(1, 2, 3, 4)} + } + binary.BigEndian.PutUint64(pc.PeerID[:], uint64(remotePeerId)) + cl := Client{} + binary.BigEndian.PutUint64(cl.peerID[:], uint64(localPeerId)) + pc.t = &Torrent{cl: &cl} + return &pc +} + +func TestPreferredNetworkDirection(t *testing.T) { + pc := peerConnForPreferredNetworkDirection + c := qt.New(t) + // Prefer outgoing to higher peer ID + c.Assert(pc(1, 2, true, false, false).hasPreferredNetworkOver(pc(1, 2, false, false, false)), qt.IsTrue) + c.Assert(pc(1, 2, false, false, false).hasPreferredNetworkOver(pc(1, 2, true, false, false)), qt.IsFalse) + c.Assert(pc(2, 1, false, false, false).hasPreferredNetworkOver(pc(2, 1, true, false, false)), qt.IsTrue) + // Don't prefer uTP + c.Assert(pc(1, 2, false, true, false).hasPreferredNetworkOver(pc(1, 2, false, false, false)), qt.IsFalse) + // Prefer IPv6 + c.Assert(pc(1, 2, false, false, false).hasPreferredNetworkOver(pc(1, 2, false, false, true)), qt.IsFalse) + // No difference + c.Assert(pc(1, 2, false, false, false).hasPreferredNetworkOver(pc(1, 2, false, false, false)), qt.IsFalse) +} diff --git a/torrent.go b/torrent.go index fbb5f6b0..8e83d3d3 100644 --- a/torrent.go +++ b/torrent.go @@ -1866,7 +1866,7 @@ func (t *Torrent) addPeerConn(c *PeerConn) (err error) { if !t.cl.config.DropDuplicatePeerIds { continue } - if left, ok := c.hasPreferredNetworkOver(c0); ok && left { + if c.hasPreferredNetworkOver(c0) { c0.close() t.deletePeerConn(c0) } else { -- 2.48.1