btcd/rpcclient/infrastructure_test.go
Sam Eiderman 42d6eba84b Fix non-root hosts failing on resolving DNS
A fix for a bug introduced by #2168

Previously, config.Host worked in the following way:
1. Documented as supporting ip addresses only
2. In fact supported "host/path" syntax
3. Did not support "scheme" prefixes, i.e. https://

Not sure this is the desired approach, probably the best thing would
have been to extend config to contain "Scheme" and "Path" fields as well.

However, this was the way it worked.

1. Now Host can contain scheme prefixes "unix://..."
2. Host can no longer contain ".../path"

This PR solves this behavior while maintaining support of the "unix://" flow
as well.

For some reason, "scheme" is named "network" in #2168 - I did not change that.

Also remove disambiguation in "network:address:port", where it parsed
"myhost:8888" as network:address instead address:port.
2024-12-05 09:01:21 +02:00

110 lines
2.4 KiB
Go

package rpcclient
import (
"testing"
"github.com/stretchr/testify/require"
)
// TestParseAddressString checks different variation of supported and
// unsupported addresses.
func TestParseAddressString(t *testing.T) {
t.Parallel()
// Using localhost only to avoid network calls.
testCases := []struct {
name string
addressString string
expNetwork string
expAddress string
expErrStr string
}{
{
name: "localhost",
addressString: "localhost",
expNetwork: "tcp",
expAddress: "127.0.0.1:0",
},
{
name: "localhost ip",
addressString: "127.0.0.1",
expNetwork: "tcp",
expAddress: "127.0.0.1:0",
},
{
name: "localhost ipv6",
addressString: "::1",
expNetwork: "tcp",
expAddress: "[::1]:0",
},
{
name: "localhost and port",
addressString: "localhost:80",
expNetwork: "tcp",
expAddress: "127.0.0.1:80",
},
{
name: "localhost ipv6 and port",
addressString: "[::1]:80",
expNetwork: "tcp",
expAddress: "[::1]:80",
},
{
name: "colon and port",
addressString: ":80",
expNetwork: "tcp",
expAddress: ":80",
},
{
name: "colon only",
addressString: ":",
expNetwork: "tcp",
expAddress: ":0",
},
{
name: "localhost and path",
addressString: "localhost/path",
expNetwork: "tcp",
expAddress: "127.0.0.1:0",
},
{
name: "localhost port and path",
addressString: "localhost:80/path",
expNetwork: "tcp",
expAddress: "127.0.0.1:80",
},
{
name: "unix prefix",
addressString: "unix://the/rest/of/the/path",
expNetwork: "unix",
expAddress: "the/rest/of/the/path",
},
{
name: "unix prefix",
addressString: "unixpacket://the/rest/of/the/path",
expNetwork: "unixpacket",
expAddress: "the/rest/of/the/path",
},
{
name: "error http prefix",
addressString: "http://localhost:1010",
expErrStr: "unsupported protocol in address",
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
addr, err := ParseAddressString(tc.addressString)
if tc.expErrStr != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expErrStr)
return
}
require.NoError(t, err)
require.Equal(t, tc.expNetwork, addr.Network())
require.Equal(t, tc.expAddress, addr.String())
})
}
}