-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
main: make --api option resolve hostnames via dns (#5249)
Resolves #5249. Calls multiaddr-dns, and picks the first result. Uses a fixed timeout of 10 seconds. Adds test cases for one, multiple, and no DNS results. License: MIT Signed-off-by: Raúl Kripalani <[email protected]>
- Loading branch information
Raúl Kripalani
committed
Aug 11, 2018
1 parent
a1375fc
commit 9a1d041
Showing
2 changed files
with
94 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
"testing" | ||
|
||
ma "gx/ipfs/QmYmsdtJ3HsodkePE3eU3TsCaP2YvPZJ4LoXnNkDE5Tpt7/go-multiaddr" | ||
madns "gx/ipfs/QmfXU2MhWoegxHoeMd3A2ytL2P6CY4FfqGWc23LTNWBwZt/go-multiaddr-dns" | ||
) | ||
|
||
var ( | ||
ctx = context.Background() | ||
testAddr, _ = ma.NewMultiaddr("/dns4/example.com/tcp/5001") | ||
) | ||
|
||
func makeResolver(n uint8) *madns.Resolver { | ||
results := make([]net.IPAddr, n) | ||
for i := uint8(0); i < n; i++ { | ||
results[i] = net.IPAddr{IP: net.ParseIP(fmt.Sprintf("192.0.2.%d", i))} | ||
} | ||
|
||
backend := &madns.MockBackend{ | ||
IP: map[string][]net.IPAddr{ | ||
"example.com": results, | ||
}} | ||
|
||
return &madns.Resolver{ | ||
Backend: backend, | ||
} | ||
} | ||
|
||
func TestApiEndpointResolveDNSOneResult(t *testing.T) { | ||
dnsResolver = makeResolver(1) | ||
|
||
addr, err := resolveAddr(ctx, testAddr) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if ref, _ := ma.NewMultiaddr("/ip4/192.0.2.0/tcp/5001"); !addr.Equal(ref) { | ||
t.Errorf("resolved address was different than expected") | ||
} | ||
} | ||
|
||
func TestApiEndpointResolveDNSMultipleResults(t *testing.T) { | ||
dnsResolver = makeResolver(4) | ||
|
||
addr, err := resolveAddr(ctx, testAddr) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
if ref, _ := ma.NewMultiaddr("/ip4/192.0.2.0/tcp/5001"); !addr.Equal(ref) { | ||
t.Errorf("resolved address was different than expected") | ||
} | ||
} | ||
|
||
func TestApiEndpointResolveDNSNoResults(t *testing.T) { | ||
dnsResolver = makeResolver(0) | ||
|
||
addr, err := resolveAddr(ctx, testAddr) | ||
if addr != nil || err == nil { | ||
t.Error("expected test address not to resolve, and to throw an error") | ||
} | ||
|
||
if !strings.HasPrefix(err.Error(), "non-resolvable API endpoint") { | ||
t.Errorf("expected error not thrown; actual: %v", err) | ||
} | ||
} |
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