Skip to content

Commit adf007d

Browse files
authored
p2p/enode: mock DNS resolver in URL parsing test (#20252)
1 parent 4b8f56c commit adf007d

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

‎p2p/enode/urlv4.go‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ import (
3131
"github.com/ethereum/go-ethereum/p2p/enr"
3232
)
3333

34-
var incompleteNodeURL = regexp.MustCompile("(?i)^(?:enode://)?([0-9a-f]+)$")
34+
var (
35+
incompleteNodeURL = regexp.MustCompile("(?i)^(?:enode://)?([0-9a-f]+)$")
36+
lookupIPFunc = net.LookupIP
37+
)
3538

3639
// MustParseV4 parses a node URL. It panics if the URL is not valid.
3740
func MustParseV4(rawurl string) *Node {
@@ -107,7 +110,6 @@ func isNewV4(n *Node) bool {
107110
func parseComplete(rawurl string) (*Node, error) {
108111
var (
109112
id *ecdsa.PublicKey
110-
ip net.IP
111113
tcpPort, udpPort uint64
112114
)
113115
u, err := url.Parse(rawurl)
@@ -125,11 +127,14 @@ func parseComplete(rawurl string) (*Node, error) {
125127
return nil, fmt.Errorf("invalid public key (%v)", err)
126128
}
127129
// Parse the IP address.
128-
ips, err := net.LookupIP(u.Hostname())
129-
if err != nil {
130-
return nil, err
130+
ip := net.ParseIP(u.Hostname())
131+
if ip == nil {
132+
ips, err := lookupIPFunc(u.Hostname())
133+
if err != nil {
134+
return nil, err
135+
}
136+
ip = ips[0]
131137
}
132-
ip = ips[0]
133138
// Ensure the IP is 4 bytes long for IPv4 addresses.
134139
if ipv4 := ip.To4(); ipv4 != nil {
135140
ip = ipv4

‎p2p/enode/urlv4_test.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package enode
1818

1919
import (
2020
"crypto/ecdsa"
21+
"errors"
2122
"net"
2223
"reflect"
2324
"strings"
@@ -27,6 +28,15 @@ import (
2728
"github.com/ethereum/go-ethereum/p2p/enr"
2829
)
2930

31+
func init() {
32+
lookupIPFunc = func(name string) ([]net.IP, error) {
33+
if name == "node.example.org" {
34+
return []net.IP{{33, 44, 55, 66}}, nil
35+
}
36+
return nil, errors.New("no such host")
37+
}
38+
}
39+
3040
var parseNodeTests = []struct {
3141
input string
3242
wantError string

0 commit comments

Comments
 (0)