p2p: add test for dialing with DNS hostname

This commit is contained in:
Felix Lange 2024-12-13 00:14:16 +01:00
parent 7ffb97c414
commit 3a983d3b3a
3 changed files with 45 additions and 15 deletions

View file

@ -490,6 +490,7 @@ func (d *dialScheduler) dnsResolveHostname(n *enode.Node) (*enode.Node, error) {
if foundIP6.IsValid() {
rec.Set(enr.IPv6Addr(foundIP6))
}
rec.SetSeq(n.Seq()) // ensure seq not bumped by update
newNode := enode.SignNull(rec, n.ID()).WithHostname(n.Hostname())
d.log.Trace("Node updated from DNS lookup", "id", n.ID(), "name", n.Hostname(), "ip", newNode.IP())
return newNode, nil

View file

@ -22,6 +22,7 @@ import (
"fmt"
"math/rand"
"net"
"net/netip"
"reflect"
"sync"
"testing"
@ -394,6 +395,34 @@ func TestDialSchedResolve(t *testing.T) {
})
}
func TestDialSchedDNSHostname(t *testing.T) {
t.Parallel()
config := dialConfig{
maxActiveDials: 1,
maxDialPeers: 1,
}
node := newNode(uintID(0x01), ":30303").WithHostname("node-hostname")
resolved := newNode(uintID(0x01), "1.2.3.4:30303").WithHostname("node-hostname")
runDialTest(t, config, []dialTestRound{
{
update: func(d *dialScheduler) {
d.dnsLookupFunc = func(ctx context.Context, network string, name string) ([]netip.Addr, error) {
if name != "node-hostname" {
t.Error("wrong hostname in DNS lookup:", name)
}
result := []netip.Addr{netip.MustParseAddr("1.2.3.4")}
return result, nil
}
d.addStatic(node)
},
wantNewDials: []*enode.Node{
resolved,
},
},
})
}
// -------
// Code below here is the framework for the tests above.

View file

@ -60,7 +60,6 @@ func uintID(i uint16) enode.ID {
// newNode creates a node record with the given address.
func newNode(id enode.ID, addr string) *enode.Node {
var r enr.Record
if addr != "" {
// Set the port if present.
if strings.Contains(addr, ":") {
hs, ps, err := net.SplitHostPort(addr)
@ -76,6 +75,7 @@ func newNode(id enode.ID, addr string) *enode.Node {
addr = hs
}
// Set the IP.
if addr != "" {
ip := net.ParseIP(addr)
if ip == nil {
panic(fmt.Errorf("invalid IP %q", addr))