forked from btcsuite/btcd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request btcsuite#2269 from same-id/fix-non-root-hosts
Fix non-root hosts failing on resolving DNS
- Loading branch information
Showing
2 changed files
with
179 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
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()) | ||
}) | ||
} | ||
} |