-
-
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.
Merge pull request #5372 from raulk/dns-resolution-api-endpoint
Run DNS lookup for --api endpoint provided in CLI
- Loading branch information
Showing
4 changed files
with
132 additions
and
5 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
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,24 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Copyright (c) 2015 Jeromy Johnson | ||
# MIT Licensed; see the LICENSE file in this repository. | ||
# | ||
|
||
test_description="test dns resolution of api endpoint by cli" | ||
|
||
. lib/test-lib.sh | ||
|
||
test_init_ipfs | ||
|
||
test_expect_success "can make http request against dns resolved nc server" ' | ||
nc -ld 5005 > nc_out & | ||
NCPID=$! | ||
go-sleep 0.5s && kill "$NCPID" & | ||
ipfs cat /ipfs/Qmabcdef --api /dns4/localhost/tcp/5005 || true | ||
' | ||
|
||
test_expect_success "request was received by local nc server" ' | ||
grep "POST /api/v0/cat" nc_out | ||
' | ||
|
||
test_done |