mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-11-10 14:17:56 +01:00
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.
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
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)
|
||
})
|
||
}
|
||
}
|