feat(netxlite): support extracting the CNAME (#875)
* feat(netxlite): support extracting the CNAME Closes https://github.com/ooni/probe/issues/2225 * fix(netxlite): attempt to increase coverage and improve tests 1. dnsovergetaddrinfo: specify the behavior of a DNSResponse returned by this file to make it line with normal responses and write unit tests to make sure we adhere to expectations; 2. dnsoverudp: make sure we wait to deferred responses also w/o a custom context and post on a private channel and test that; 3. utls: recognize that we can actually write a test for NetConn and what needs to change when we'll use go1.19 by default will just be a cast that at that point can be removed.
This commit is contained in:
parent
da1c13e312
commit
cc24f28b9d
10 changed files with 390 additions and 39 deletions
|
|
@ -8,7 +8,9 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
||||
)
|
||||
|
||||
func TestDNSOverGetaddrinfo(t *testing.T) {
|
||||
|
|
@ -187,3 +189,157 @@ func TestDNSOverGetaddrinfo(t *testing.T) {
|
|||
})
|
||||
})
|
||||
}
|
||||
|
||||
func TestDNSOverGetaddrinfoResponse(t *testing.T) {
|
||||
t.Run("Query works as intended", func(t *testing.T) {
|
||||
t.Run("when query is not nil", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: &mocks.DNSQuery{},
|
||||
}
|
||||
out := resp.Query()
|
||||
if out != resp.query {
|
||||
t.Fatal("unexpected query")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("when query is nil", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: nil, // oops
|
||||
}
|
||||
panicked := false
|
||||
func() {
|
||||
defer func() {
|
||||
if recover() != nil {
|
||||
panicked = true
|
||||
}
|
||||
}()
|
||||
_ = resp.Query()
|
||||
}()
|
||||
if !panicked {
|
||||
t.Fatal("did not panic")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Bytes works as intended", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: nil,
|
||||
}
|
||||
if len(resp.Bytes()) > 0 {
|
||||
t.Fatal("unexpected bytes")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Rcode works as intended", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: nil,
|
||||
}
|
||||
if resp.Rcode() != 0 {
|
||||
t.Fatal("unexpected rcode")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DecodeHTTPS works as intended", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: nil,
|
||||
}
|
||||
out, err := resp.DecodeHTTPS()
|
||||
if !errors.Is(err, ErrNoDNSTransport) {
|
||||
t.Fatal("unexpected err")
|
||||
}
|
||||
if out != nil {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DecodeLookupHost works as intended", func(t *testing.T) {
|
||||
t.Run("on success", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{
|
||||
"1.1.1.1", "1.0.0.1",
|
||||
},
|
||||
cname: "",
|
||||
query: nil,
|
||||
}
|
||||
out, err := resp.DecodeLookupHost()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if diff := cmp.Diff(resp.addrs, out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("on failure", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: nil,
|
||||
}
|
||||
out, err := resp.DecodeLookupHost()
|
||||
if !errors.Is(err, ErrOODNSNoAnswer) {
|
||||
t.Fatal("unexpected err")
|
||||
}
|
||||
if len(out) > 0 {
|
||||
t.Fatal("unexpected addrs")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("DecodeNS works as intended", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: nil,
|
||||
}
|
||||
out, err := resp.DecodeNS()
|
||||
if !errors.Is(err, ErrNoDNSTransport) {
|
||||
t.Fatal("unexpected err")
|
||||
}
|
||||
if len(out) != 0 {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("DecodeCNAME works as intended", func(t *testing.T) {
|
||||
t.Run("on success", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "antani",
|
||||
query: nil,
|
||||
}
|
||||
out, err := resp.DecodeCNAME()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if out != resp.cname {
|
||||
t.Fatal("unexpected cname")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("on failure", func(t *testing.T) {
|
||||
resp := &dnsOverGetaddrinfoResponse{
|
||||
addrs: []string{},
|
||||
cname: "",
|
||||
query: nil,
|
||||
}
|
||||
out, err := resp.DecodeCNAME()
|
||||
if !errors.Is(err, ErrOODNSNoAnswer) {
|
||||
t.Fatal("unexpected err")
|
||||
}
|
||||
if out != "" {
|
||||
t.Fatal("unexpected cname")
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue