Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various ping fixes #4546

Merged
merged 5 commits into from
Jan 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion core/commands/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
Expand All @@ -26,6 +27,9 @@ type PingResult struct {
Text string
}

// ErrPingSelf is returned when the user attempts to ping themself.
var ErrPingSelf = errors.New("error: can't ping self")

var PingCmd = &cmds.Command{
Helptext: cmdkit.HelpText{
Tagline: "Send echo request packets to IPFS hosts.",
Expand Down Expand Up @@ -80,7 +84,12 @@ trip latency information.

addr, peerID, err := ParsePeerParam(req.Arguments()[0])
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
res.SetError(fmt.Errorf("failed to parse peer address '%s': %s", req.Arguments()[0], err), cmdkit.ErrNormal)
return
}

if peerID == n.Identity {
res.SetError(ErrPingSelf, cmdkit.ErrNormal)
return
}

Expand All @@ -94,6 +103,10 @@ trip latency information.
return
}

if numPings <= 0 {
res.SetError(fmt.Errorf("error: ping count must be greater than 0, was %d", numPings), cmdkit.ErrNormal)
}

outChan := pingPeer(ctx, n, peerID, numPings)
res.SetOutput(outChan)
},
Expand Down
40 changes: 40 additions & 0 deletions test/sharness/t0041-ping.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/sh

test_description="Test ping command"

. lib/test-lib.sh

test_init_ipfs

# start iptb + wait for peering
test_expect_success 'init iptb' '
iptb init -n 2 --bootstrap=none --port=0
'

startup_cluster 2

test_expect_success 'peer ids' '
PEERID_0=$(iptb get id 0) &&
PEERID_1=$(iptb get id 1)
'

test_expect_success "test ping other" '
ipfsi 0 ping -n2 -- "$PEERID_1" &&
ipfsi 1 ping -n2 -- "$PEERID_0"
'

test_expect_success "test ping self" '
! ipfsi 0 ping -n2 -- "$PEERID_0" &&
! ipfsi 1 ping -n2 -- "$PEERID_1"
'

test_expect_success "test ping 0" '
! ipfsi 0 ping -n0 -- "$PEERID_1" &&
! ipfsi 1 ping -n0 -- "$PEERID_0"
'

test_expect_success 'stop iptb' '
iptb stop
'

test_done