feat(netxlite): implements NS queries (#734)
This diff has been extracted from https://github.com/bassosimone/websteps-illustrated/commit/eb0bf38957e79fbad198fcdc9f9c7b36f61a8e2c. See https://github.com/ooni/probe/issues/2096. While there, skip the broken tests caused by issue https://github.com/ooni/probe/issues/2098.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
@@ -8,10 +10,9 @@ import (
|
||||
// DNSDecoder allows mocking dnsx.DNSDecoder.
|
||||
type DNSDecoder struct {
|
||||
MockDecodeLookupHost func(qtype uint16, reply []byte, queryID uint16) ([]string, error)
|
||||
|
||||
MockDecodeHTTPS func(reply []byte, queryID uint16) (*model.HTTPSSvc, error)
|
||||
|
||||
MockDecodeReply func(reply []byte) (*dns.Msg, error)
|
||||
MockDecodeHTTPS func(reply []byte, queryID uint16) (*model.HTTPSSvc, error)
|
||||
MockDecodeNS func(reply []byte, queryID uint16) ([]*net.NS, error)
|
||||
MockDecodeReply func(reply []byte) (*dns.Msg, error)
|
||||
}
|
||||
|
||||
// DecodeLookupHost calls MockDecodeLookupHost.
|
||||
@@ -24,6 +25,11 @@ func (e *DNSDecoder) DecodeHTTPS(reply []byte, queryID uint16) (*model.HTTPSSvc,
|
||||
return e.MockDecodeHTTPS(reply, queryID)
|
||||
}
|
||||
|
||||
// DecodeNS calls MockDecodeNS.
|
||||
func (e *DNSDecoder) DecodeNS(reply []byte, queryID uint16) ([]*net.NS, error) {
|
||||
return e.MockDecodeNS(reply, queryID)
|
||||
}
|
||||
|
||||
// DecodeReply calls MockDecodeReply.
|
||||
func (e *DNSDecoder) DecodeReply(reply []byte) (*dns.Msg, error) {
|
||||
return e.MockDecodeReply(reply)
|
||||
|
||||
@@ -2,6 +2,7 @@ package mocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
@@ -41,6 +42,22 @@ func TestDNSDecoder(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DecodeNS", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
e := &DNSDecoder{
|
||||
MockDecodeNS: func(reply []byte, queryID uint16) ([]*net.NS, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
out, err := e.DecodeNS(make([]byte, 17), dns.Id())
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("unexpected out")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DecodeReply", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
e := &DNSDecoder{
|
||||
|
||||
@@ -2,6 +2,7 @@ package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
@@ -13,6 +14,7 @@ type Resolver struct {
|
||||
MockAddress func() string
|
||||
MockCloseIdleConnections func()
|
||||
MockLookupHTTPS func(ctx context.Context, domain string) (*model.HTTPSSvc, error)
|
||||
MockLookupNS func(ctx context.Context, domain string) ([]*net.NS, error)
|
||||
}
|
||||
|
||||
// LookupHost calls MockLookupHost.
|
||||
@@ -39,3 +41,8 @@ func (r *Resolver) CloseIdleConnections() {
|
||||
func (r *Resolver) LookupHTTPS(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
||||
return r.MockLookupHTTPS(ctx, domain)
|
||||
}
|
||||
|
||||
// LookupNS calls MockLookupNS.
|
||||
func (r *Resolver) LookupNS(ctx context.Context, domain string) ([]*net.NS, error) {
|
||||
return r.MockLookupNS(ctx, domain)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package mocks
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
@@ -77,4 +78,21 @@ func TestResolver(t *testing.T) {
|
||||
t.Fatal("expected nil addr")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("LookupNS", func(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
r := &Resolver{
|
||||
MockLookupNS: func(ctx context.Context, domain string) ([]*net.NS, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
ns, err := r.LookupNS(ctx, "dns.google")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("unexpected error", err)
|
||||
}
|
||||
if ns != nil {
|
||||
t.Fatal("expected nil addr")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -51,6 +51,9 @@ type DNSDecoder interface {
|
||||
// an error, though, when there are no IPv4/IPv6 hints in the reply.
|
||||
DecodeHTTPS(data []byte, queryID uint16) (*HTTPSSvc, error)
|
||||
|
||||
// DecodeNS is like DecodeHTTPS but for NS queries.
|
||||
DecodeNS(data []byte, queryID uint16) ([]*net.NS, error)
|
||||
|
||||
// DecodeReply decodes a DNS reply message.
|
||||
//
|
||||
// Arguments:
|
||||
@@ -194,6 +197,9 @@ type Resolver interface {
|
||||
// LookupHTTPS issues an HTTPS query for a domain.
|
||||
LookupHTTPS(
|
||||
ctx context.Context, domain string) (*HTTPSSvc, error)
|
||||
|
||||
// LookupNS issues a NS query for a domain.
|
||||
LookupNS(ctx context.Context, domain string) ([]*net.NS, error)
|
||||
}
|
||||
|
||||
// TLSDialer is a Dialer dialing TLS connections.
|
||||
|
||||
Reference in New Issue
Block a user