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

cmd/devp2p: fixes for eth and discv4 tests #23155

Merged
merged 6 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 7 additions & 6 deletions cmd/devp2p/internal/v4test/discv4tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"crypto/rand"
"fmt"
"net"
"reflect"
"time"

"github.com/ethereum/go-ethereum/crypto"
Expand Down Expand Up @@ -89,16 +88,18 @@ func BasicPing(t *utesting.T) {

// checkPong verifies that reply is a valid PONG matching the given ping hash.
func (te *testenv) checkPong(reply v4wire.Packet, pingHash []byte) error {
if reply == nil || reply.Kind() != v4wire.PongPacket {
return fmt.Errorf("expected PONG reply, got %v", reply)
if reply == nil {
return fmt.Errorf("expected PONG reply, got nil")
}
if reply.Kind() != v4wire.PongPacket {
return fmt.Errorf("expected PONG reply, got %v %v", reply.Name(), reply)
}
pong := reply.(*v4wire.Pong)
if !bytes.Equal(pong.ReplyTok, pingHash) {
return fmt.Errorf("PONG reply token mismatch: got %x, want %x", pong.ReplyTok, pingHash)
}
wantEndpoint := te.localEndpoint(te.l1)
if !reflect.DeepEqual(pong.To, wantEndpoint) {
return fmt.Errorf("PONG 'to' endpoint mismatch: got %+v, want %+v", pong.To, wantEndpoint)
if want := te.localEndpoint(te.l1); !want.IP.Equal(pong.To.IP) || want.UDP != pong.To.UDP {
return fmt.Errorf("PONG 'to' endpoint mismatch: got %+v, want %+v", pong.To, want)
}
if v4wire.Expired(pong.Expiration) {
return fmt.Errorf("PONG is expired (%v)", pong.Expiration)
Expand Down
89 changes: 89 additions & 0 deletions p2p/discover/v4wire/packet_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package v4wire

import (
"encoding/hex"
"testing"

"github.com/davecgh/go-spew/spew"
"github.com/ethereum/go-ethereum/rlp"
)

func TestDecode(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should keep this test. The test basically checks that RLP decoding works, but there are enough tests in package rlp to cover this, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure

for i, test := range []struct {
input string
expEnr int
}{
{
input: "f83acb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a3558082999983999999",
expEnr: 0,
},
{
input: "f83acb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a3550182999983999999",
expEnr: 1,
},
{
input: "f2cb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a355",
expEnr: 0,
},
{
input: "f3cb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a35501",
expEnr: 1,
},
{
// This is how we previously encoded ENR sequence number of 0.
input: "f3cb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a35580",
expEnr: 0,
},
{
// This input was previously accepted, it contains a non-canonical rlp rest `00`. This vector
// fails after commit '3e6f46caec51d82aef363632517eb5842eef6db6'
input: "f3cb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a35500",
expEnr: -1,
},
{ // This is how besu encodes a pong with enr sequence 0
input: "f83bcb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a355880000000000000000",
expEnr: -1,
},
{ // This is how besu encodes a pong with enr sequence 1
input: "f83bcb847f000001820cfa8215a8a000000000000000000000000000000000000000000000000000000000000012348443b9a355880000000000000001",
expEnr: -1,
},
} {
input, err := hex.DecodeString(test.input)
if err != nil {
t.Fatalf("test %d: invalid hex: %s", i, test.input)
}
var pongPacket Pong
err = rlp.DecodeBytes(input, &pongPacket)
if err != nil && test.expEnr > -1 {
t.Errorf("test %d: did not accept packet %s\n%v", i, test.input, err)
continue
}
if test.expEnr == -1 {
if err == nil {
t.Errorf("test %d: expected failure", i)
}
continue
}
if have, want := pongPacket.ENRSeq, uint64(test.expEnr); have != want {
t.Logf("test %d: got %s\n", i, spew.Sdump(pongPacket))
t.Errorf("test %d, wrong enr seq, have %d, want %d", i, have, want)
}
}
}