Files
lnd/lnwire/dns_addr_test.go
Elle Mouton f0a20c9d04 netadd: split ValidatenodeAnn into sig and field checks
Check that the node ann doesnt contain more than 1 DNS addr.
This will ensure that we now start rejecting new node announcements
with multiple DNS addrs since this check is called in the gossiper
before persisting a node ann to our local graph.

It also validates the DNS fields according to BOLT #7 specs.
2025-09-03 01:11:34 +00:00

88 lines
1.9 KiB
Go
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package lnwire
import (
"fmt"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// TestValidateDNSAddr tests hostname and port validation per BOLT #7.
func TestValidateDNSAddr(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
hostname string
port uint16
err error
}{
{
name: "empty hostname",
hostname: "",
port: 9735,
err: ErrEmptyDNSHostname,
},
{
name: "zero port",
hostname: "example.com",
port: 0,
err: ErrZeroPort,
},
{
name: "hostname too long",
hostname: strings.Repeat("a", 256),
port: 9735,
err: fmt.Errorf("%w: DNS hostname length 256",
ErrHostnameTooLong),
},
{
name: "hostname with invalid ASCII space",
hostname: "exa mple.com",
port: 9735,
err: fmt.Errorf("%w: hostname 'exa mple.com' contains "+
"invalid character ' ' at position 3",
ErrInvalidHostnameCharacter),
},
{
name: "hostname with invalid ASCII underscore",
hostname: "example_node.com",
port: 9735,
err: fmt.Errorf("%w: hostname 'example_node.com' "+
"contains invalid character '_' at position 7",
ErrInvalidHostnameCharacter),
},
{
name: "hostname with non-ASCII character",
hostname: "example❄.com",
port: 9735,
err: fmt.Errorf("%w: hostname 'example❄.com' "+
"contains invalid character '❄' at position 7",
ErrInvalidHostnameCharacter),
},
{
name: "valid hostname",
hostname: "example.com",
port: 9735,
},
{
name: "valid hostname with numbers",
hostname: "node101.example.com",
port: 9735,
},
{
name: "valid hostname with hyphens",
hostname: "my-node.example-domain.com",
port: 9735,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := ValidateDNSAddr(tc.hostname, tc.port)
require.Equal(t, err, tc.err)
})
}
}