@@ -23,6 +23,7 @@ import (
23
23
"github.com/libp2p/go-libp2p/core/peerstore"
24
24
"github.com/libp2p/go-libp2p/core/protocol"
25
25
"github.com/libp2p/go-libp2p/core/routing"
26
+ "github.com/libp2p/go-msgio"
26
27
ma "github.com/multiformats/go-multiaddr"
27
28
manet "github.com/multiformats/go-multiaddr/net"
28
29
"github.com/multiformats/go-multihash"
@@ -1490,32 +1491,48 @@ func TestInvalidServer(t *testing.T) {
1490
1491
1491
1492
s0 := setupDHT (ctx , t , false , BucketSize (2 )) // server
1492
1493
s1 := setupDHT (ctx , t , false , BucketSize (2 )) // server
1493
- c0 := setupDHT (ctx , t , true , BucketSize (2 )) // client advertising server protocols
1494
- c1 := setupDHT (ctx , t , true , BucketSize (2 )) // client advertising server protocols
1495
- // note that the bucket size is 2
1494
+ m0 := setupDHT (ctx , t , false , BucketSize (2 )) // misbehabing server
1495
+ m1 := setupDHT (ctx , t , false , BucketSize (2 )) // misbehabing server
1496
1496
1497
- // make c0 and c1 advertise all dht server protocols, but hang on all requests
1497
+ // make m0 and m1 advertise all dht server protocols, but hang on all requests
1498
1498
for _ , proto := range s0 .serverProtocols {
1499
- for _ , c := range []* IpfsDHT {c0 , c1 } {
1499
+ for _ , m := range []* IpfsDHT {m0 , m1 } {
1500
1500
// Hang on every request.
1501
- c .host .SetStreamHandler (proto , func (s network.Stream ) {
1502
- defer s .Reset () // nolint
1503
- <- ctx .Done ()
1501
+ m .host .SetStreamHandler (proto , func (s network.Stream ) {
1502
+ r := msgio .NewVarintReaderSize (s , network .MessageSizeMax )
1503
+ msgbytes , err := r .ReadMsg ()
1504
+ if err != nil {
1505
+ t .Fatal (err )
1506
+ }
1507
+ var req pb.Message
1508
+ err = req .Unmarshal (msgbytes )
1509
+ if err != nil {
1510
+ t .Fatal (err )
1511
+ }
1512
+
1513
+ // answer with an empty response message
1514
+ resp := pb .NewMessage (req .GetType (), nil , req .GetClusterLevel ())
1515
+
1516
+ // send out response msg
1517
+ err = net .WriteMsg (s , resp )
1518
+ if err != nil {
1519
+ t .Fatal (err )
1520
+ }
1504
1521
})
1505
1522
}
1506
1523
}
1507
1524
1508
- // connect s0 and c0
1509
- connectNoSync (t , ctx , s0 , c0 )
1525
+ // connect s0 and m0
1526
+ connectNoSync (t , ctx , s0 , m0 )
1510
1527
1511
1528
// add a provider (p) for a key (k) to s0
1512
1529
k := testCaseCids [0 ]
1513
1530
p := peer .ID ("TestPeer" )
1514
1531
s0 .ProviderStore ().AddProvider (ctx , k .Hash (), peer.AddrInfo {ID : p })
1515
1532
time .Sleep (time .Millisecond * 5 ) // just in case...
1516
1533
1517
- // find the provider for k from c0
1518
- provs , err := c0 .FindProviders (ctx , k )
1534
+ // find the provider for k from m0
1535
+ provs , err := m0 .FindProviders (ctx , k )
1519
1536
if err != nil {
1520
1537
t .Fatal (err )
1521
1538
}
@@ -1526,27 +1543,27 @@ func TestInvalidServer(t *testing.T) {
1526
1543
t .Fatal ("expected it to be our test peer" )
1527
1544
}
1528
1545
1529
- // verify that c0 and s0 contain each other in their routing tables
1530
- if s0 .routingTable .Find (c0 .self ) == "" {
1531
- // c0 is added to s0 routing table even though it is misbehaving, because
1546
+ // verify that m0 and s0 contain each other in their routing tables
1547
+ if s0 .routingTable .Find (m0 .self ) == "" {
1548
+ // m0 is added to s0 routing table even though it is misbehaving, because
1532
1549
// s0's routing table is not well populated, so s0 isn't picky about who it adds.
1533
1550
t .Fatal ("Misbehaving DHT servers should be added to routing table if not well populated" )
1534
1551
}
1535
- if c0 .routingTable .Find (s0 .self ) == "" {
1552
+ if m0 .routingTable .Find (s0 .self ) == "" {
1536
1553
t .Fatal ("DHT server should have been added to the misbehaving server routing table" )
1537
1554
}
1538
1555
1539
- // connect s0 to both s1 and c1
1556
+ // connect s0 to both s1 and m1
1540
1557
connectNoSync (t , ctx , s0 , s1 )
1541
- connectNoSync (t , ctx , s0 , c1 )
1558
+ connectNoSync (t , ctx , s0 , m1 )
1542
1559
1543
1560
// s1 should be added to s0's routing table. Then, because s0's routing table
1544
- // contains more than bucketSize (2) entries, lookupCheck is enabled and c1
1561
+ // contains more than bucketSize (2) entries, lookupCheck is enabled and m1
1545
1562
// shouldn't be added, because it fails the lookupCheck (hang on all requests).
1546
1563
if s0 .routingTable .Find (s1 .self ) == "" {
1547
1564
t .Fatal ("Well behaving DHT server should have been added to the server routing table" )
1548
1565
}
1549
- if s0 .routingTable .Find (c1 .self ) != "" {
1566
+ if s0 .routingTable .Find (m1 .self ) != "" {
1550
1567
t .Fatal ("Misbehaving DHT servers should not be added to routing table if well populated" )
1551
1568
}
1552
1569
}
0 commit comments