refactor: fully move IDNAResolver to netxlite (#433)
We started doing this in https://github.com/ooni/probe-cli/pull/432. This work is part of https://github.com/ooni/probe/issues/1733.
This commit is contained in:
@@ -83,14 +83,15 @@ func (r *ResolverLogger) Address() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// IDNAResolver is to support resolving Internationalized Domain Names.
|
||||
// ResolverIDNA supports resolving Internationalized Domain Names.
|
||||
//
|
||||
// See RFC3492 for more information.
|
||||
type IDNAResolver struct {
|
||||
type ResolverIDNA struct {
|
||||
Resolver
|
||||
}
|
||||
|
||||
// LookupHost implements Resolver.LookupHost
|
||||
func (r *IDNAResolver) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
||||
// LookupHost implements Resolver.LookupHost.
|
||||
func (r *ResolverIDNA) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
||||
host, err := idna.ToASCII(hostname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -99,7 +100,7 @@ func (r *IDNAResolver) LookupHost(ctx context.Context, hostname string) ([]strin
|
||||
}
|
||||
|
||||
// Network implements Resolver.Network.
|
||||
func (r *IDNAResolver) Network() string {
|
||||
func (r *ResolverIDNA) Network() string {
|
||||
if rn, ok := r.Resolver.(resolverNetworker); ok {
|
||||
return rn.Network()
|
||||
}
|
||||
@@ -107,7 +108,7 @@ func (r *IDNAResolver) Network() string {
|
||||
}
|
||||
|
||||
// Address implements Resolver.Address.
|
||||
func (r *IDNAResolver) Address() string {
|
||||
func (r *ResolverIDNA) Address() string {
|
||||
if ra, ok := r.Resolver.(resolverAddresser); ok {
|
||||
return ra.Address()
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/apex/log"
|
||||
@@ -89,3 +90,64 @@ func TestResolverLoggerNoChildNetworkAddress(t *testing.T) {
|
||||
t.Fatal("invalid Address")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolverIDNAWorksAsIntended(t *testing.T) {
|
||||
expectedIPs := []string{"77.88.55.66"}
|
||||
r := &ResolverIDNA{
|
||||
Resolver: &netxmocks.Resolver{
|
||||
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||
if domain != "xn--d1acpjx3f.xn--p1ai" {
|
||||
return nil, errors.New("passed invalid domain")
|
||||
}
|
||||
return expectedIPs, nil
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
addrs, err := r.LookupHost(ctx, "яндекс.рф")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(expectedIPs, addrs); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIDNAResolverWithInvalidPunycode(t *testing.T) {
|
||||
r := &ResolverIDNA{Resolver: &netxmocks.Resolver{
|
||||
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
||||
return nil, errors.New("should not happen")
|
||||
},
|
||||
}}
|
||||
// See https://www.farsightsecurity.com/blog/txt-record/punycode-20180711/
|
||||
ctx := context.Background()
|
||||
addrs, err := r.LookupHost(ctx, "xn--0000h")
|
||||
if err == nil || !strings.HasPrefix(err.Error(), "idna: invalid label") {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if addrs != nil {
|
||||
t.Fatal("expected no response here")
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolverIDNAChildNetworkAddress(t *testing.T) {
|
||||
r := &ResolverIDNA{
|
||||
Resolver: DefaultResolver,
|
||||
}
|
||||
if v := r.Network(); v != "system" {
|
||||
t.Fatal("invalid network", v)
|
||||
}
|
||||
if v := r.Address(); v != "" {
|
||||
t.Fatal("invalid address", v)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolverIDNANoChildNetworkAddress(t *testing.T) {
|
||||
r := &ResolverIDNA{}
|
||||
if v := r.Network(); v != "idna" {
|
||||
t.Fatal("invalid network", v)
|
||||
}
|
||||
if v := r.Address(); v != "" {
|
||||
t.Fatal("invalid address", v)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user