From aa95dcbdf2eb1943da09dc731af0823ffab97d33 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Jan 2018 10:23:00 -0800 Subject: [PATCH 1/5] ping: add a useful error message when pinging self We don't allow dialing self for many (very good) reasons. fixes #4542 License: MIT Signed-off-by: Steven Allen --- core/commands/ping.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/commands/ping.go b/core/commands/ping.go index 5d48f260ac3..68dda837f66 100644 --- a/core/commands/ping.go +++ b/core/commands/ping.go @@ -3,6 +3,7 @@ package commands import ( "bytes" "context" + "errors" "fmt" "io" "strings" @@ -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.", @@ -84,6 +88,11 @@ trip latency information. return } + if peerID == n.Identity { + res.SetError(ErrPingSelf, cmdkit.ErrNormal) + return + } + if addr != nil { n.Peerstore.AddAddr(peerID, addr, pstore.TempAddrTTL) // temporary } From 28a610a13a252968e2430712962a5802328106cb Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Jan 2018 10:41:57 -0800 Subject: [PATCH 2/5] ping: return an error when ping count <= 0 License: MIT Signed-off-by: Steven Allen --- core/commands/ping.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/commands/ping.go b/core/commands/ping.go index 68dda837f66..d7f189ea542 100644 --- a/core/commands/ping.go +++ b/core/commands/ping.go @@ -103,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) }, From fd736680b5532bc349fe387d1266e58e0cbd8813 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Jan 2018 10:45:36 -0800 Subject: [PATCH 3/5] ping: better peer addr parsing error License: MIT Signed-off-by: Steven Allen --- core/commands/ping.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/commands/ping.go b/core/commands/ping.go index d7f189ea542..6a88df108ca 100644 --- a/core/commands/ping.go +++ b/core/commands/ping.go @@ -84,7 +84,7 @@ 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 } From ac551f1dfd9103a1f1d4802fe33d00f62a33c65c Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Jan 2018 10:45:46 -0800 Subject: [PATCH 4/5] ping: add sharness tests License: MIT Signed-off-by: Steven Allen --- test/sharness/t0041-ping.sh | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 test/sharness/t0041-ping.sh diff --git a/test/sharness/t0041-ping.sh b/test/sharness/t0041-ping.sh new file mode 100755 index 00000000000..acdfd000915 --- /dev/null +++ b/test/sharness/t0041-ping.sh @@ -0,0 +1,37 @@ +#!/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 From 77e27b2bc3a39210ae99292c9628b18bcf0cd85c Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Jan 2018 16:28:14 -0800 Subject: [PATCH 5/5] put ping sharness test cmds on separate lines License: MIT Signed-off-by: Steven Allen --- test/sharness/t0041-ping.sh | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/sharness/t0041-ping.sh b/test/sharness/t0041-ping.sh index acdfd000915..44949f75e91 100755 --- a/test/sharness/t0041-ping.sh +++ b/test/sharness/t0041-ping.sh @@ -19,15 +19,18 @@ test_expect_success 'peer ids' ' ' test_expect_success "test ping other" ' - ipfsi 0 ping -n2 -- "$PEERID_1" && ipfsi 1 ping -n2 -- "$PEERID_0" + 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" + ! 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" + ! ipfsi 0 ping -n0 -- "$PEERID_1" && + ! ipfsi 1 ping -n0 -- "$PEERID_0" ' test_expect_success 'stop iptb' '