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:
Simone Basso
2022-05-16 10:46:53 +02:00
committed by GitHub
parent c1b06a2d09
commit ce052b665e
26 changed files with 857 additions and 73 deletions
+10 -4
View File
@@ -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)
+17
View File
@@ -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{
+7
View File
@@ -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)
}
+18
View File
@@ -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")
}
})
}