-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
net: consider dns addresses as public (#220)
* consider dns addresses as public * fix private domains list * early return * add reference to original list
- Loading branch information
Showing
2 changed files
with
95 additions
and
38 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 |
---|---|---|
@@ -1,48 +1,59 @@ | ||
package manet | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
ma "github.com/multiformats/go-multiaddr" | ||
) | ||
|
||
func TestIsPublicAddr(t *testing.T) { | ||
a, err := ma.NewMultiaddr("/ip4/192.168.1.1/tcp/80") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if IsPublicAddr(a) { | ||
t.Fatal("192.168.1.1 is not a public address!") | ||
} | ||
|
||
if !IsPrivateAddr(a) { | ||
t.Fatal("192.168.1.1 is a private address!") | ||
} | ||
|
||
a, err = ma.NewMultiaddr("/ip4/1.1.1.1/tcp/80") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if !IsPublicAddr(a) { | ||
t.Fatal("1.1.1.1 is a public address!") | ||
} | ||
|
||
if IsPrivateAddr(a) { | ||
t.Fatal("1.1.1.1 is not a private address!") | ||
} | ||
|
||
a, err = ma.NewMultiaddr("/tcp/80/ip4/1.1.1.1") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if IsPublicAddr(a) { | ||
t.Fatal("shouldn't consider an address that starts with /tcp/ as *public*") | ||
} | ||
|
||
if IsPrivateAddr(a) { | ||
t.Fatal("shouldn't consider an address that starts with /tcp/ as *private*") | ||
tests := []struct { | ||
addr ma.Multiaddr | ||
isPublic bool | ||
isPrivate bool | ||
}{ | ||
{ | ||
addr: ma.StringCast("/ip4/192.168.1.1/tcp/80"), | ||
isPublic: false, | ||
isPrivate: true, | ||
}, | ||
{ | ||
addr: ma.StringCast("/ip4/1.1.1.1/tcp/80"), | ||
isPublic: true, | ||
isPrivate: false, | ||
}, | ||
{ | ||
addr: ma.StringCast("/tcp/80/ip4/1.1.1.1"), | ||
isPublic: false, | ||
isPrivate: false, | ||
}, | ||
{ | ||
addr: ma.StringCast("/dns/node.libp2p.io/udp/1/quic-v1"), | ||
isPublic: true, | ||
isPrivate: false, | ||
}, | ||
{ | ||
addr: ma.StringCast("/dnsaddr/node.libp2p.io/udp/1/quic-v1"), | ||
isPublic: true, | ||
isPrivate: false, | ||
}, | ||
{ | ||
addr: ma.StringCast("/dns/node.libp2p.local/udp/1/quic-v1"), | ||
isPublic: false, | ||
isPrivate: false, // You can configure .local domains in local networks to return public addrs | ||
}, | ||
} | ||
for i, tt := range tests { | ||
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { | ||
isPublic := IsPublicAddr(tt.addr) | ||
isPrivate := IsPrivateAddr(tt.addr) | ||
if isPublic != tt.isPublic { | ||
t.Errorf("IsPublicAddr check failed for %s: expected %t, got %t", tt.addr, tt.isPublic, isPublic) | ||
} | ||
if isPrivate != tt.isPrivate { | ||
t.Errorf("IsPrivateAddr check failed for %s: expected %t, got %t", tt.addr, tt.isPrivate, isPrivate) | ||
} | ||
}) | ||
} | ||
} |