Skip to content

Commit 6a48ae3

Browse files
adamschmidegfjl
andauthored
cmd/devp2p: add discv4 test suite (#21163)
This adds a test suite for discovery v4. The test suite is a port of the Hive suite for discovery, and will replace the current suite on Hive soon-ish. The tests can be run locally with this command: devp2p discv4 test -remote enode//... Co-authored-by: Felix Lange <[email protected]>
1 parent e5871b9 commit 6a48ae3

File tree

6 files changed

+887
-0
lines changed

6 files changed

+887
-0
lines changed

cmd/devp2p/discv4cmd.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ package main
1919
import (
2020
"fmt"
2121
"net"
22+
"os"
2223
"strings"
2324
"time"
2425

26+
"github.com/ethereum/go-ethereum/cmd/devp2p/internal/v4test"
2527
"github.com/ethereum/go-ethereum/common"
2628
"github.com/ethereum/go-ethereum/crypto"
29+
"github.com/ethereum/go-ethereum/internal/utesting"
2730
"github.com/ethereum/go-ethereum/p2p/discover"
2831
"github.com/ethereum/go-ethereum/p2p/enode"
2932
"github.com/ethereum/go-ethereum/params"
@@ -40,6 +43,7 @@ var (
4043
discv4ResolveCommand,
4144
discv4ResolveJSONCommand,
4245
discv4CrawlCommand,
46+
discv4TestCommand,
4347
},
4448
}
4549
discv4PingCommand = cli.Command{
@@ -74,6 +78,12 @@ var (
7478
Action: discv4Crawl,
7579
Flags: []cli.Flag{bootnodesFlag, crawlTimeoutFlag},
7680
}
81+
discv4TestCommand = cli.Command{
82+
Name: "test",
83+
Usage: "Runs tests against a node",
84+
Action: discv4Test,
85+
Flags: []cli.Flag{remoteEnodeFlag, testPatternFlag, testListen1Flag, testListen2Flag},
86+
}
7787
)
7888

7989
var (
@@ -98,6 +108,25 @@ var (
98108
Usage: "Time limit for the crawl.",
99109
Value: 30 * time.Minute,
100110
}
111+
remoteEnodeFlag = cli.StringFlag{
112+
Name: "remote",
113+
Usage: "Enode of the remote node under test",
114+
EnvVar: "REMOTE_ENODE",
115+
}
116+
testPatternFlag = cli.StringFlag{
117+
Name: "run",
118+
Usage: "Pattern of test suite(s) to run",
119+
}
120+
testListen1Flag = cli.StringFlag{
121+
Name: "listen1",
122+
Usage: "IP address of the first tester",
123+
Value: v4test.Listen1,
124+
}
125+
testListen2Flag = cli.StringFlag{
126+
Name: "listen2",
127+
Usage: "IP address of the second tester",
128+
Value: v4test.Listen2,
129+
}
101130
)
102131

103132
func discv4Ping(ctx *cli.Context) error {
@@ -184,6 +213,28 @@ func discv4Crawl(ctx *cli.Context) error {
184213
return nil
185214
}
186215

216+
func discv4Test(ctx *cli.Context) error {
217+
// Configure test package globals.
218+
if !ctx.IsSet(remoteEnodeFlag.Name) {
219+
return fmt.Errorf("Missing -%v", remoteEnodeFlag.Name)
220+
}
221+
v4test.Remote = ctx.String(remoteEnodeFlag.Name)
222+
v4test.Listen1 = ctx.String(testListen1Flag.Name)
223+
v4test.Listen2 = ctx.String(testListen2Flag.Name)
224+
225+
// Filter and run test cases.
226+
tests := v4test.AllTests
227+
if ctx.IsSet(testPatternFlag.Name) {
228+
tests = utesting.MatchTests(tests, ctx.String(testPatternFlag.Name))
229+
}
230+
results := utesting.RunTests(tests, os.Stdout)
231+
if fails := utesting.CountFailures(results); fails > 0 {
232+
return fmt.Errorf("%v/%v tests passed.", len(tests)-fails, len(tests))
233+
}
234+
fmt.Printf("%v/%v passed\n", len(tests), len(tests))
235+
return nil
236+
}
237+
187238
// startV4 starts an ephemeral discovery V4 node.
188239
func startV4(ctx *cli.Context) *discover.UDPv4 {
189240
ln, config := makeDiscoveryConfig(ctx)

0 commit comments

Comments
 (0)