Skip to content

Commit

Permalink
test: Add test to check if peer panics on receiving malformed ping me…
Browse files Browse the repository at this point in the history
…ssage
  • Loading branch information
Rahul Ghangas committed Feb 21, 2021
1 parent facb19c commit c24c514
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions peer/peerdiscovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package peer_test

import (
"context"
"encoding/binary"
"fmt"
"go.uber.org/zap"
"time"

"github.com/renproject/aw/dht"
Expand Down Expand Up @@ -129,4 +131,66 @@ var _ = Describe("Peer Discovery", func() {
defer cancelPeerDiscoveryContext()
})
})

Context("when sending malformed pings to peer", func() {
It("peer should not panic", func() {

n := 2
opts, peers, tables, _, _, transports := setup(n)

cancelPeerContext := createRingTopology(n, opts, peers, tables, transports)
defer cancelPeerContext()

ctx, cancel := context.WithTimeout(context.Background(), 3 * time.Second)
defer cancel()
func(ctx context.Context) {
var pingData [4]byte
binary.LittleEndian.PutUint32(pingData[:], uint32(transports[0].Port()))

msg := wire.Msg{
Version: wire.MsgVersion1,
Type: wire.MsgTypePing,
}

count := 0
ticker := time.NewTicker(time.Second)
defer ticker.Stop()

sendDuration := time.Second
Outer:
for {
if count % 2 == 1 {
msg.Data = pingData[:]
} else {
msg.Data = nil
}
for _, sig := range transports[0].Table().Peers(2) {
err := func() error {
innerCtx, innerCancel := context.WithTimeout(ctx, sendDuration)
defer innerCancel()
msg.To = id.Hash(sig)
return transports[0].Send(innerCtx, sig, msg)
}()
if err != nil {
opts[0].Logger.Debug("pinging", zap.Error(err))
if err == context.Canceled || err == context.DeadlineExceeded {
break
}
}
select {
case <-ticker.C:
continue Outer
default:
}
}
select {
case <-ctx.Done():
return
case <-ticker.C:
count++
}
}
}(ctx)
})
})
})

0 comments on commit c24c514

Please sign in to comment.