2020-04-08 16:03:29 +00:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/anacrolix/dht/v2/krpc"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
pp "github.com/anacrolix/torrent/peer_protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPexConnState(t *testing.T) {
|
2021-09-14 13:01:20 +00:00
|
|
|
var cl Client
|
|
|
|
cl.init(TestingConfig(t))
|
2020-04-08 16:03:29 +00:00
|
|
|
cl.initLogger()
|
|
|
|
torrent := cl.newTorrent(metainfo.Hash{}, nil)
|
|
|
|
addr := &net.TCPAddr{IP: net.IPv6loopback, Port: 4747}
|
2022-07-11 08:22:23 +00:00
|
|
|
c := cl.newConnection(nil, newConnectionOpts{
|
|
|
|
remoteAddr: addr,
|
|
|
|
network: addr.Network(),
|
|
|
|
})
|
2020-04-08 16:03:29 +00:00
|
|
|
c.PeerExtensionIDs = make(map[pp.ExtensionName]pp.ExtensionNumber)
|
|
|
|
c.PeerExtensionIDs[pp.ExtensionNamePex] = pexExtendedId
|
2021-05-20 08:51:08 +00:00
|
|
|
c.messageWriter.mu.Lock()
|
2020-04-08 16:03:29 +00:00
|
|
|
c.setTorrent(torrent)
|
2021-09-14 13:01:20 +00:00
|
|
|
if err := torrent.addPeerConn(c); err != nil {
|
|
|
|
t.Log(err)
|
|
|
|
}
|
2020-04-08 16:03:29 +00:00
|
|
|
|
2021-12-16 00:07:19 +00:00
|
|
|
connWriteCond := c.messageWriter.writeCond.Signaled()
|
2020-04-08 16:03:29 +00:00
|
|
|
c.pex.Init(c)
|
|
|
|
require.True(t, c.pex.IsEnabled(), "should get enabled")
|
|
|
|
defer c.pex.Close()
|
|
|
|
|
|
|
|
var out pp.Message
|
|
|
|
writerCalled := false
|
|
|
|
testWriter := func(m pp.Message) bool {
|
|
|
|
writerCalled = true
|
|
|
|
out = m
|
|
|
|
return true
|
|
|
|
}
|
2021-12-16 00:07:19 +00:00
|
|
|
<-connWriteCond
|
2020-04-08 16:03:29 +00:00
|
|
|
c.pex.Share(testWriter)
|
|
|
|
require.True(t, writerCalled)
|
|
|
|
require.EqualValues(t, pp.Extended, out.Type)
|
|
|
|
require.EqualValues(t, pexExtendedId, out.ExtendedID)
|
|
|
|
|
|
|
|
x, err := pp.LoadPexMsg(out.ExtendedPayload)
|
|
|
|
require.NoError(t, err)
|
2020-10-22 21:58:55 +00:00
|
|
|
targx := pp.PexMsg{
|
2020-04-08 16:03:29 +00:00
|
|
|
Added: krpc.CompactIPv4NodeAddrs(nil),
|
|
|
|
AddedFlags: []pp.PexPeerFlags{},
|
|
|
|
Added6: krpc.CompactIPv6NodeAddrs{
|
2023-05-16 07:55:53 +00:00
|
|
|
krpcNodeAddrFromNetAddr(addr),
|
2020-04-08 16:03:29 +00:00
|
|
|
},
|
|
|
|
Added6Flags: []pp.PexPeerFlags{0},
|
|
|
|
}
|
|
|
|
require.EqualValues(t, targx, x)
|
|
|
|
}
|