@@ -3,8 +3,10 @@ package decision
33import (
44 "bytes"
55 "context"
6+ "encoding/binary"
67 "errors"
78 "fmt"
9+ "math/rand"
810 "strings"
911 "sync"
1012 "testing"
@@ -23,6 +25,7 @@ import (
2325 process "github.com/jbenet/goprocess"
2426 peer "github.com/libp2p/go-libp2p/core/peer"
2527 libp2ptest "github.com/libp2p/go-libp2p/core/test"
28+ mh "github.com/multiformats/go-multihash"
2629)
2730
2831type peerTag struct {
@@ -1051,6 +1054,12 @@ func TestWantlistForPeer(t *testing.T) {
10511054 if len (entries ) != 4 {
10521055 t .Fatal ("expected wantlist to contain all wants from parter" )
10531056 }
1057+
1058+ e .PeerDisconnected (partner )
1059+ entries = e .WantlistForPeer (partner )
1060+ if len (entries ) != 0 {
1061+ t .Fatal ("expected wantlist to be empty after disconnect" )
1062+ }
10541063}
10551064
10561065func TestTaskComparator (t * testing.T ) {
@@ -1629,3 +1638,127 @@ func stringsComplement(set, subset []string) []string {
16291638 }
16301639 return complement
16311640}
1641+
1642+ func TestWantlistDoesNotGrowPastLimit (t * testing.T ) {
1643+ ctx , cancel := context .WithCancel (context .Background ())
1644+ defer cancel ()
1645+
1646+ const limit = 32
1647+ warsaw := newTestEngine (ctx , "warsaw" , WithMaxQueuedWantlistEntriesPerPeer (limit ))
1648+ riga := newTestEngine (ctx , "riga" )
1649+
1650+ // Send in two messages to test reslicing.
1651+ for i := 2 ; i != 0 ; i -- {
1652+ m := message .New (false )
1653+ for j := limit * 3 / 4 ; j != 0 ; j -- {
1654+ m .AddEntry (blocks .NewBlock ([]byte (fmt .Sprint (i , j ))).Cid (), 0 , pb .Message_Wantlist_Block , true )
1655+ }
1656+ warsaw .Engine .MessageReceived (ctx , riga .Peer , m )
1657+ }
1658+
1659+ if warsaw .Peer == riga .Peer {
1660+ t .Fatal ("Sanity Check: Peers have same Key!" )
1661+ }
1662+
1663+ wl := warsaw .Engine .WantlistForPeer (riga .Peer )
1664+ if len (wl ) != limit {
1665+ t .Fatal ("wantlist does not match limit" , len (wl ))
1666+ }
1667+ }
1668+
1669+ func TestWantlistGrowsToLimit (t * testing.T ) {
1670+ ctx , cancel := context .WithCancel (context .Background ())
1671+ defer cancel ()
1672+
1673+ const limit = 32
1674+ warsaw := newTestEngine (ctx , "warsaw" , WithMaxQueuedWantlistEntriesPerPeer (limit ))
1675+ riga := newTestEngine (ctx , "riga" )
1676+
1677+ // Send in two messages to test reslicing.
1678+ m := message .New (false )
1679+ for j := limit ; j != 0 ; j -- {
1680+ m .AddEntry (blocks .NewBlock ([]byte (fmt .Sprint (j ))).Cid (), 0 , pb .Message_Wantlist_Block , true )
1681+ }
1682+ warsaw .Engine .MessageReceived (ctx , riga .Peer , m )
1683+
1684+ if warsaw .Peer == riga .Peer {
1685+ t .Fatal ("Sanity Check: Peers have same Key!" )
1686+ }
1687+
1688+ wl := warsaw .Engine .WantlistForPeer (riga .Peer )
1689+ if len (wl ) != limit {
1690+ t .Fatal ("wantlist does not match limit" , len (wl ))
1691+ }
1692+ }
1693+
1694+ func TestIgnoresCidsAboveLimit (t * testing.T ) {
1695+ ctx , cancel := context .WithCancel (context .Background ())
1696+ defer cancel ()
1697+
1698+ const cidLimit = 64
1699+ warsaw := newTestEngine (ctx , "warsaw" , WithMaxCidSize (cidLimit ))
1700+ riga := newTestEngine (ctx , "riga" )
1701+
1702+ // Send in two messages to test reslicing.
1703+ m := message .New (true )
1704+
1705+ m .AddEntry (blocks .NewBlock ([]byte ("Hæ" )).Cid (), 0 , pb .Message_Wantlist_Block , true )
1706+
1707+ var hash mh.Multihash
1708+ hash = binary .AppendUvarint (hash , mh .BLAKE3 )
1709+ hash = binary .AppendUvarint (hash , cidLimit )
1710+ startOfDigest := len (hash )
1711+ hash = append (hash , make (mh.Multihash , cidLimit )... )
1712+ rand .Read (hash [startOfDigest :])
1713+ m .AddEntry (cid .NewCidV1 (cid .Raw , hash ), 0 , pb .Message_Wantlist_Block , true )
1714+
1715+ warsaw .Engine .MessageReceived (ctx , riga .Peer , m )
1716+
1717+ if warsaw .Peer == riga .Peer {
1718+ t .Fatal ("Sanity Check: Peers have same Key!" )
1719+ }
1720+
1721+ wl := warsaw .Engine .WantlistForPeer (riga .Peer )
1722+ if len (wl ) != 1 {
1723+ t .Fatal ("wantlist add a CID too big" )
1724+ }
1725+ }
1726+
1727+ func TestKillConnectionForInlineCid (t * testing.T ) {
1728+ ctx , cancel := context .WithCancel (context .Background ())
1729+ defer cancel ()
1730+
1731+ warsaw := newTestEngine (ctx , "warsaw" )
1732+ riga := newTestEngine (ctx , "riga" )
1733+
1734+ if warsaw .Peer == riga .Peer {
1735+ t .Fatal ("Sanity Check: Peers have same Key!" )
1736+ }
1737+
1738+ // Send in two messages to test reslicing.
1739+ m := message .New (true )
1740+
1741+ m .AddEntry (blocks .NewBlock ([]byte ("Hæ" )).Cid (), 0 , pb .Message_Wantlist_Block , true )
1742+
1743+ var hash mh.Multihash
1744+ hash = binary .AppendUvarint (hash , mh .IDENTITY )
1745+ const digestSize = 32
1746+ hash = binary .AppendUvarint (hash , digestSize )
1747+ startOfDigest := len (hash )
1748+ hash = append (hash , make (mh.Multihash , digestSize )... )
1749+ rand .Read (hash [startOfDigest :])
1750+ m .AddEntry (cid .NewCidV1 (cid .Raw , hash ), 0 , pb .Message_Wantlist_Block , true )
1751+
1752+ if ! warsaw .Engine .MessageReceived (ctx , riga .Peer , m ) {
1753+ t .Fatal ("connection was not killed when receiving inline in cancel" )
1754+ }
1755+
1756+ m .Reset (true )
1757+
1758+ m .AddEntry (blocks .NewBlock ([]byte ("Hæ" )).Cid (), 0 , pb .Message_Wantlist_Block , true )
1759+ m .Cancel (cid .NewCidV1 (cid .Raw , hash ))
1760+
1761+ if ! warsaw .Engine .MessageReceived (ctx , riga .Peer , m ) {
1762+ t .Fatal ("connection was not killed when receiving inline in cancel" )
1763+ }
1764+ }
0 commit comments