refactor: merge dnsx and errorsx into netxlite (#517)
When preparing a tutorial for netxlite, I figured it is easier to tell people "hey, this is the package you should use for all low-level networking stuff" rather than introducing people to a set of packages working together where some piece of functionality is here and some other piece is there. Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// 2021-09-27 15:57:57.877144 +0200 CEST m=+1.353309960
|
||||
// 2021-09-28 12:05:19.526032 +0200 CEST m=+0.405934084
|
||||
// https://curl.haxx.se/ca/cacert.pem
|
||||
|
||||
package netxlite
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,4 +1,4 @@
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -6,8 +6,6 @@ import (
|
||||
"net"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
// Dialer establishes network connections.
|
||||
@@ -229,8 +227,7 @@ var _ Dialer = &dialerErrWrapper{}
|
||||
func (d *dialerErrWrapper) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
conn, err := d.Dialer.DialContext(ctx, network, address)
|
||||
if err != nil {
|
||||
return nil, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.ConnectOperation, err)
|
||||
return nil, NewErrWrapper(ClassifyGenericError, ConnectOperation, err)
|
||||
}
|
||||
return &dialerErrWrapperConn{Conn: conn}, nil
|
||||
}
|
||||
@@ -245,8 +242,7 @@ var _ net.Conn = &dialerErrWrapperConn{}
|
||||
func (c *dialerErrWrapperConn) Read(b []byte) (int, error) {
|
||||
count, err := c.Conn.Read(b)
|
||||
if err != nil {
|
||||
return 0, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.ReadOperation, err)
|
||||
return 0, NewErrWrapper(ClassifyGenericError, ReadOperation, err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -254,8 +250,7 @@ func (c *dialerErrWrapperConn) Read(b []byte) (int, error) {
|
||||
func (c *dialerErrWrapperConn) Write(b []byte) (int, error) {
|
||||
count, err := c.Conn.Write(b)
|
||||
if err != nil {
|
||||
return 0, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.WriteOperation, err)
|
||||
return 0, NewErrWrapper(ClassifyGenericError, WriteOperation, err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -263,8 +258,7 @@ func (c *dialerErrWrapperConn) Write(b []byte) (int, error) {
|
||||
func (c *dialerErrWrapperConn) Close() error {
|
||||
err := c.Conn.Close()
|
||||
if err != nil {
|
||||
return errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.CloseOperation, err)
|
||||
return NewErrWrapper(ClassifyGenericError, CloseOperation, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
@@ -254,9 +253,9 @@ func TestDialerResolver(t *testing.T) {
|
||||
mu := &sync.Mutex{}
|
||||
errorsList := []error{
|
||||
errors.New("a mocked error"),
|
||||
errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError,
|
||||
errorsx.CloseOperation,
|
||||
NewErrWrapper(
|
||||
ClassifyGenericError,
|
||||
CloseOperation,
|
||||
io.EOF,
|
||||
),
|
||||
}
|
||||
@@ -282,7 +281,7 @@ func TestDialerResolver(t *testing.T) {
|
||||
},
|
||||
}
|
||||
conn, err := d.DialContext(context.Background(), "tcp", "dot.dns:853")
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if conn != nil {
|
||||
@@ -296,9 +295,9 @@ func TestDialerResolver(t *testing.T) {
|
||||
mu := &sync.Mutex{}
|
||||
errorsList := []error{
|
||||
errors.New("a mocked error"),
|
||||
errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError,
|
||||
errorsx.CloseOperation,
|
||||
NewErrWrapper(
|
||||
ClassifyGenericError,
|
||||
CloseOperation,
|
||||
errors.New("antani"),
|
||||
),
|
||||
}
|
||||
@@ -529,7 +528,7 @@ func TestDialerErrWrapper(t *testing.T) {
|
||||
}
|
||||
ctx := context.Background()
|
||||
conn, err := d.DialContext(ctx, "", "")
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if conn != nil {
|
||||
@@ -585,7 +584,7 @@ func TestDialerErrWrapperConn(t *testing.T) {
|
||||
},
|
||||
}
|
||||
count, err := conn.Read(b)
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if count != 0 {
|
||||
@@ -624,7 +623,7 @@ func TestDialerErrWrapperConn(t *testing.T) {
|
||||
},
|
||||
}
|
||||
count, err := conn.Write(b)
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if count != 0 {
|
||||
@@ -658,7 +657,7 @@ func TestDialerErrWrapperConn(t *testing.T) {
|
||||
},
|
||||
}
|
||||
err := conn.Close()
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,13 +1,6 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/dnsx/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
// HTTPSSvc is an HTTPSSvc reply.
|
||||
type HTTPSSvc = model.HTTPSSvc
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
// The DNSDecoder decodes DNS replies.
|
||||
type DNSDecoder interface {
|
||||
@@ -32,11 +25,11 @@ func (d *DNSDecoderMiekg) parseReply(data []byte) (*dns.Msg, error) {
|
||||
case dns.RcodeSuccess:
|
||||
return reply, nil
|
||||
case dns.RcodeNameError:
|
||||
return nil, errorsx.ErrOODNSNoSuchHost
|
||||
return nil, ErrOODNSNoSuchHost
|
||||
case dns.RcodeRefused:
|
||||
return nil, errorsx.ErrOODNSRefused
|
||||
return nil, ErrOODNSRefused
|
||||
default:
|
||||
return nil, errorsx.ErrOODNSMisbehaving
|
||||
return nil, ErrOODNSMisbehaving
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +59,7 @@ func (d *DNSDecoderMiekg) DecodeHTTPS(data []byte) (*HTTPSSvc, error) {
|
||||
}
|
||||
}
|
||||
if len(out.ALPN) <= 0 {
|
||||
return nil, errorsx.ErrOODNSNoAnswer
|
||||
return nil, ErrOODNSNoAnswer
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -92,7 +85,7 @@ func (d *DNSDecoderMiekg) DecodeLookupHost(qtype uint16, data []byte) ([]string,
|
||||
}
|
||||
}
|
||||
if len(addrs) <= 0 {
|
||||
return nil, errorsx.ErrOODNSNoAnswer
|
||||
return nil, ErrOODNSNoAnswer
|
||||
}
|
||||
return addrs, nil
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
func TestDNSDecoder(t *testing.T) {
|
||||
@@ -40,7 +39,7 @@ func TestDNSDecoder(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeA, dnsGenReplyWithError(t, dns.TypeA, dns.RcodeRefused))
|
||||
if !errors.Is(err, errorsx.ErrOODNSRefused) {
|
||||
if !errors.Is(err, ErrOODNSRefused) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
@@ -51,7 +50,7 @@ func TestDNSDecoder(t *testing.T) {
|
||||
t.Run("no address", func(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(dns.TypeA, dnsGenLookupHostReplySuccess(t, dns.TypeA))
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
if !errors.Is(err, ErrOODNSNoAnswer) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
@@ -99,7 +98,7 @@ func TestDNSDecoder(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeA, dnsGenLookupHostReplySuccess(t, dns.TypeAAAA, "::1", "fe80::1"))
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
if !errors.Is(err, ErrOODNSNoAnswer) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
@@ -111,7 +110,7 @@ func TestDNSDecoder(t *testing.T) {
|
||||
d := &DNSDecoderMiekg{}
|
||||
data, err := d.DecodeLookupHost(
|
||||
dns.TypeAAAA, dnsGenLookupHostReplySuccess(t, dns.TypeA, "1.1.1.1", "8.8.4.4."))
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
if !errors.Is(err, ErrOODNSNoAnswer) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if data != nil {
|
||||
@@ -129,7 +128,7 @@ func TestDNSDecoder(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reply, err := d.parseReply(data)
|
||||
if !errors.Is(err, errorsx.ErrOODNSMisbehaving) { // catch all error
|
||||
if !errors.Is(err, ErrOODNSMisbehaving) { // catch all error
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if reply != nil {
|
||||
@@ -153,7 +152,7 @@ func TestDNSDecoder(t *testing.T) {
|
||||
data := dnsGenHTTPSReplySuccess(t, nil, nil, nil)
|
||||
d := &DNSDecoderMiekg{}
|
||||
reply, err := d.DecodeHTTPS(data)
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
if !errors.Is(err, ErrOODNSNoAnswer) {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if reply != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"strings"
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/httpheader"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
|
||||
)
|
||||
|
||||
// HTTPClient is the HTTP client expected by DNSOverHTTPS.
|
||||
@@ -63,7 +62,7 @@ func (t *DNSOverHTTPS) RoundTrip(ctx context.Context, query []byte) ([]byte, err
|
||||
if resp.Header.Get("content-type") != "application/dns-message" {
|
||||
return nil, errors.New("doh: invalid content-type")
|
||||
}
|
||||
return iox.ReadAllContext(ctx, resp.Body)
|
||||
return ReadAllContext(ctx, resp.Body)
|
||||
}
|
||||
|
||||
// RequiresPadding returns true for DoH according to RFC8467
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -1,16 +1,10 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Dialer is the network dialer interface assumed by this package.
|
||||
type Dialer interface {
|
||||
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
||||
}
|
||||
|
||||
// DNSOverUDP is a DNS over UDP RoundTripper.
|
||||
type DNSOverUDP struct {
|
||||
dialer Dialer
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
@@ -147,7 +148,7 @@ func TestDNSOverUDP(t *testing.T) {
|
||||
|
||||
t.Run("other functions okay", func(t *testing.T) {
|
||||
const address = "9.9.9.9:53"
|
||||
txp := NewDNSOverUDP(&net.Dialer{}, address)
|
||||
txp := NewDNSOverUDP(NewDialerWithoutResolver(log.Log), address)
|
||||
if txp.RequiresPadding() != false {
|
||||
t.Fatal("invalid RequiresPadding")
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import "context"
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Package model contains the dnsx model.
|
||||
package model
|
||||
// Package dnsx contains the dnsx model.
|
||||
package dnsx
|
||||
|
||||
// HTTPSSvc is an HTTPSSvc reply.
|
||||
type HTTPSSvc struct {
|
||||
@@ -1,2 +0,0 @@
|
||||
// Package mocks contains mocks for dnsx.
|
||||
package mocks
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// Generated: 2021-09-27 15:57:58.500515 +0200 CEST m=+0.304199251
|
||||
// Generated: 2021-09-28 12:05:19.868647 +0200 CEST m=+0.105301959
|
||||
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
//go:generate go run ./internal/generrno/
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// Generated: 2021-09-27 15:57:58.553101 +0200 CEST m=+0.356786917
|
||||
// Generated: 2021-09-28 12:05:19.918073 +0200 CEST m=+0.154728959
|
||||
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"io"
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// Generated: 2021-09-27 15:57:58.197535 +0200 CEST m=+0.001212709
|
||||
// Generated: 2021-09-28 12:05:19.763535 +0200 CEST m=+0.000187668
|
||||
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
import "golang.org/x/sys/unix"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Code generated by go generate; DO NOT EDIT.
|
||||
// Generated: 2021-09-27 15:57:58.455744 +0200 CEST m=+0.259427834
|
||||
// Generated: 2021-09-28 12:05:19.843209 +0200 CEST m=+0.079863168
|
||||
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
import "golang.org/x/sys/windows"
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
// Package errorsx contains code to classify errors.
|
||||
//
|
||||
// We define the ErrWrapper type, that should wrap any error
|
||||
// and map it to the corresponding OONI failure.
|
||||
//
|
||||
// See https://github.com/ooni/spec/blob/master/data-formats/df-007-errors.md
|
||||
// for a list of OONI failure strings.
|
||||
//
|
||||
// We define ClassifyXXX functions that map an `error` type to
|
||||
// the corresponding OONI failure.
|
||||
//
|
||||
// When we cannot map an error to an OONI failure we return
|
||||
// an "unknown_failure: XXX" string where the XXX part has
|
||||
// been scrubbed so to remove any network endpoints.
|
||||
//
|
||||
// The general approach we have been following for this
|
||||
// package has been to return the same strings that we used
|
||||
// with the previous measurement engine, Measurement Kit
|
||||
// available at https://github.com/measurement-kit/measurement-kit.
|
||||
package errorsx
|
||||
@@ -1,4 +1,4 @@
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"github.com/apex/log"
|
||||
oohttp "github.com/ooni/oohttp"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
@@ -84,7 +83,7 @@ func TestHTTPTransportLogger(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
iox.ReadAllContext(context.Background(), resp.Body)
|
||||
ReadAllContext(context.Background(), resp.Body)
|
||||
resp.Body.Close()
|
||||
if count < 1 {
|
||||
t.Fatal("no logs?!")
|
||||
|
||||
@@ -19,7 +19,7 @@ import (
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
var tmpl = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
|
||||
@@ -50,7 +50,7 @@ func main() {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
bundle, err := iox.ReadAllContext(context.Background(), resp.Body)
|
||||
bundle, err := netxlite.ReadAllContext(context.Background(), resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
+3
-3
@@ -143,7 +143,7 @@ func writeSystemSpecificFile(kind string) {
|
||||
filep := fileCreate(filename)
|
||||
fileWrite(filep, "// Code generated by go generate; DO NOT EDIT.\n")
|
||||
filePrintf(filep, "// Generated: %+v\n\n", time.Now())
|
||||
fileWrite(filep, "package errorsx\n\n")
|
||||
fileWrite(filep, "package netxlite\n\n")
|
||||
filePrintf(filep, "import \"golang.org/x/sys/%s\"\n\n", kind)
|
||||
fileWrite(filep, "const (\n")
|
||||
for _, spec := range Specs {
|
||||
@@ -163,7 +163,7 @@ func writeGenericFile() {
|
||||
filep := fileCreate(filename)
|
||||
fileWrite(filep, "// Code generated by go generate; DO NOT EDIT.\n")
|
||||
filePrintf(filep, "// Generated: %+v\n\n", time.Now())
|
||||
fileWrite(filep, "package errorsx\n\n")
|
||||
fileWrite(filep, "package netxlite\n\n")
|
||||
fileWrite(filep, "//go:generate go run ./internal/generrno/\n\n")
|
||||
fileWrite(filep, "import (\n")
|
||||
fileWrite(filep, "\t\"errors\"\n")
|
||||
@@ -237,7 +237,7 @@ func writeGenericTestFile() {
|
||||
|
||||
fileWrite(filep, "// Code generated by go generate; DO NOT EDIT.\n")
|
||||
filePrintf(filep, "// Generated: %+v\n\n", time.Now())
|
||||
fileWrite(filep, "package errorsx\n\n")
|
||||
fileWrite(filep, "package netxlite\n\n")
|
||||
fileWrite(filep, "import (\n")
|
||||
fileWrite(filep, "\t\"io\"\n")
|
||||
fileWrite(filep, "\t\"syscall\"\n")
|
||||
@@ -1,5 +1,4 @@
|
||||
// Package iox contains io extensions.
|
||||
package iox
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,21 +0,0 @@
|
||||
package iox_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
|
||||
)
|
||||
|
||||
func ExampleReadAllContext() {
|
||||
r := strings.NewReader("deadbeef")
|
||||
ctx := context.Background()
|
||||
out, err := iox.ReadAllContext(ctx, r)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("%d\n", len(out))
|
||||
// Output: 8
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package iox
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -1,9 +1,9 @@
|
||||
package mocks
|
||||
|
||||
import "github.com/ooni/probe-cli/v3/internal/netxlite/dnsx/model"
|
||||
import "github.com/ooni/probe-cli/v3/internal/netxlite/dnsx"
|
||||
|
||||
// HTTPSSvc is the result of HTTPS queries.
|
||||
type HTTPSSvc = model.HTTPSSvc
|
||||
type HTTPSSvc = dnsx.HTTPSSvc
|
||||
|
||||
// DNSDecoder allows mocking dnsx.DNSDecoder.
|
||||
type DNSDecoder struct {
|
||||
@@ -2,8 +2,6 @@ package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/dnsx/model"
|
||||
)
|
||||
|
||||
// Resolver is a mockable Resolver.
|
||||
@@ -35,9 +33,6 @@ func (r *Resolver) CloseIdleConnections() {
|
||||
r.MockCloseIdleConnections()
|
||||
}
|
||||
|
||||
// HTTPSSvc is an HTTPSSvc reply.
|
||||
type HTTPSSvc = model.HTTPSSvc
|
||||
|
||||
// LookupHTTPS calls MockLookupHTTPS.
|
||||
func (r *Resolver) LookupHTTPS(ctx context.Context, domain string) (*HTTPSSvc, error) {
|
||||
return r.MockLookupHTTPS(ctx, domain)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package errorsx
|
||||
package netxlite
|
||||
|
||||
// Operations that we measure. They are the possibly values of
|
||||
// the ErrWrapper.Operation field.
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
|
||||
)
|
||||
|
||||
@@ -360,8 +359,7 @@ var _ QUICListener = &quicListenerErrWrapper{}
|
||||
func (qls *quicListenerErrWrapper) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
||||
pconn, err := qls.QUICListener.Listen(addr)
|
||||
if err != nil {
|
||||
return nil, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.QUICListenOperation, err)
|
||||
return nil, NewErrWrapper(ClassifyGenericError, QUICListenOperation, err)
|
||||
}
|
||||
return &quicErrWrapperUDPLikeConn{pconn}, nil
|
||||
}
|
||||
@@ -378,8 +376,7 @@ var _ quicx.UDPLikeConn = &quicErrWrapperUDPLikeConn{}
|
||||
func (c *quicErrWrapperUDPLikeConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
||||
count, err := c.UDPLikeConn.WriteTo(p, addr)
|
||||
if err != nil {
|
||||
return 0, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.WriteToOperation, err)
|
||||
return 0, NewErrWrapper(ClassifyGenericError, WriteToOperation, err)
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
@@ -388,8 +385,7 @@ func (c *quicErrWrapperUDPLikeConn) WriteTo(p []byte, addr net.Addr) (int, error
|
||||
func (c *quicErrWrapperUDPLikeConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
n, addr, err := c.UDPLikeConn.ReadFrom(b)
|
||||
if err != nil {
|
||||
return 0, nil, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.ReadFromOperation, err)
|
||||
return 0, nil, NewErrWrapper(ClassifyGenericError, ReadFromOperation, err)
|
||||
}
|
||||
return n, addr, nil
|
||||
}
|
||||
@@ -398,8 +394,7 @@ func (c *quicErrWrapperUDPLikeConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||
func (c *quicErrWrapperUDPLikeConn) Close() error {
|
||||
err := c.UDPLikeConn.Close()
|
||||
if err != nil {
|
||||
return errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError, errorsx.ReadFromOperation, err)
|
||||
return NewErrWrapper(ClassifyGenericError, ReadFromOperation, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -415,8 +410,8 @@ func (d *quicDialerErrWrapper) DialContext(
|
||||
tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) {
|
||||
sess, err := d.QUICDialer.DialContext(ctx, network, host, tlsCfg, cfg)
|
||||
if err != nil {
|
||||
return nil, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyQUICHandshakeError, errorsx.QUICHandshakeOperation, err)
|
||||
return nil, NewErrWrapper(
|
||||
ClassifyQUICHandshakeError, QUICHandshakeOperation, err)
|
||||
}
|
||||
return sess, nil
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/lucas-clemente/quic-go"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
|
||||
)
|
||||
@@ -524,7 +523,7 @@ func TestQUICListenerErrWrapper(t *testing.T) {
|
||||
},
|
||||
}
|
||||
conn, err := ql.Listen(&net.UDPAddr{})
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if conn != nil {
|
||||
@@ -569,7 +568,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
||||
},
|
||||
}
|
||||
count, addr, err := conn.ReadFrom(p)
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if count != 0 {
|
||||
@@ -611,7 +610,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
||||
},
|
||||
}
|
||||
count, err := conn.WriteTo(p, &net.UDPAddr{})
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if count != 0 {
|
||||
@@ -645,7 +644,7 @@ func TestQUICErrWrapperUDPLikeConn(t *testing.T) {
|
||||
},
|
||||
}
|
||||
err := conn.Close()
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
})
|
||||
@@ -699,7 +698,7 @@ func TestQUICDialerErrWrapper(t *testing.T) {
|
||||
}
|
||||
ctx := context.Background()
|
||||
sess, err := d.DialContext(ctx, "", "", &tls.Config{}, &quic.Config{})
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if sess != nil {
|
||||
|
||||
@@ -3,8 +3,6 @@ package netxlite
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
// This file contains weird stuff that we carried over from
|
||||
@@ -36,7 +34,7 @@ func quirkReduceErrors(errorslist []error) error {
|
||||
return nil
|
||||
}
|
||||
for _, err := range errorslist {
|
||||
var wrapper *errorsx.ErrWrapper
|
||||
var wrapper *ErrWrapper
|
||||
if errors.As(err, &wrapper) && !strings.HasPrefix(
|
||||
err.Error(), "unknown_failure",
|
||||
) {
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
func TestQuirkReduceErrors(t *testing.T) {
|
||||
@@ -35,19 +34,19 @@ func TestQuirkReduceErrors(t *testing.T) {
|
||||
|
||||
t.Run("multiple errors with meaningful ones", func(t *testing.T) {
|
||||
err1 := errors.New("mocked error #1")
|
||||
err2 := errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError,
|
||||
errorsx.CloseOperation,
|
||||
err2 := NewErrWrapper(
|
||||
ClassifyGenericError,
|
||||
CloseOperation,
|
||||
errors.New("antani"),
|
||||
)
|
||||
err3 := errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyGenericError,
|
||||
errorsx.CloseOperation,
|
||||
errorsx.ECONNREFUSED,
|
||||
err3 := NewErrWrapper(
|
||||
ClassifyGenericError,
|
||||
CloseOperation,
|
||||
ECONNREFUSED,
|
||||
)
|
||||
err4 := errors.New("mocked error #3")
|
||||
result := quirkReduceErrors([]error{err1, err2, err3, err4})
|
||||
if result.Error() != errorsx.FailureConnectionRefused {
|
||||
if result.Error() != FailureConnectionRefused {
|
||||
t.Fatal("wrong result")
|
||||
}
|
||||
})
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/dnsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
"golang.org/x/net/idna"
|
||||
)
|
||||
|
||||
@@ -253,8 +252,7 @@ var _ Resolver = &resolverErrWrapper{}
|
||||
func (r *resolverErrWrapper) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
||||
addrs, err := r.Resolver.LookupHost(ctx, hostname)
|
||||
if err != nil {
|
||||
return nil, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyResolverError, errorsx.ResolveOperation, err)
|
||||
return nil, NewErrWrapper(ClassifyResolverError, ResolveOperation, err)
|
||||
}
|
||||
return addrs, nil
|
||||
}
|
||||
@@ -263,8 +261,7 @@ func (r *resolverErrWrapper) LookupHTTPS(
|
||||
ctx context.Context, domain string) (*HTTPSSvc, error) {
|
||||
out, err := r.Resolver.LookupHTTPS(ctx, domain)
|
||||
if err != nil {
|
||||
return nil, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyResolverError, errorsx.ResolveOperation, err)
|
||||
return nil, NewErrWrapper(ClassifyResolverError, ResolveOperation, err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
@@ -493,7 +492,7 @@ func TestResolverErrWrapper(t *testing.T) {
|
||||
}
|
||||
ctx := context.Background()
|
||||
addrs, err := reso.LookupHost(ctx, "")
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if addrs != nil {
|
||||
@@ -578,7 +577,7 @@ func TestResolverErrWrapper(t *testing.T) {
|
||||
}
|
||||
ctx := context.Background()
|
||||
https, err := reso.LookupHTTPS(ctx, "")
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if https != nil {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
+5
-6
@@ -1,4 +1,4 @@
|
||||
package dnsx
|
||||
package netxlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
@@ -9,8 +9,7 @@ import (
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/dnsx/mocks"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
func TestSerialResolver(t *testing.T) {
|
||||
@@ -81,7 +80,7 @@ func TestSerialResolver(t *testing.T) {
|
||||
}
|
||||
r := NewSerialResolver(txp)
|
||||
addrs, err := r.LookupHost(context.Background(), "www.gogle.com")
|
||||
if !errors.Is(err, errorsx.ErrOODNSNoAnswer) {
|
||||
if !errors.Is(err, ErrOODNSNoAnswer) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
if addrs != nil {
|
||||
@@ -130,7 +129,7 @@ func TestSerialResolver(t *testing.T) {
|
||||
t.Run("with timeout", func(t *testing.T) {
|
||||
txp := &mocks.DNSTransport{
|
||||
MockRoundTrip: func(ctx context.Context, query []byte) (reply []byte, err error) {
|
||||
return nil, &net.OpError{Err: errorsx.ETIMEDOUT, Op: "dial"}
|
||||
return nil, &net.OpError{Err: ETIMEDOUT, Op: "dial"}
|
||||
},
|
||||
MockRequiresPadding: func() bool {
|
||||
return true
|
||||
@@ -138,7 +137,7 @@ func TestSerialResolver(t *testing.T) {
|
||||
}
|
||||
r := NewSerialResolver(txp)
|
||||
addrs, err := r.LookupHost(context.Background(), "www.gogle.com")
|
||||
if !errors.Is(err, errorsx.ETIMEDOUT) {
|
||||
if !errors.Is(err, ETIMEDOUT) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if addrs != nil {
|
||||
@@ -10,7 +10,6 @@ import (
|
||||
"time"
|
||||
|
||||
oohttp "github.com/ooni/oohttp"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -346,8 +345,8 @@ func (h *tlsHandshakerErrWrapper) Handshake(
|
||||
) (net.Conn, tls.ConnectionState, error) {
|
||||
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
|
||||
if err != nil {
|
||||
return nil, tls.ConnectionState{}, errorsx.NewErrWrapper(
|
||||
errorsx.ClassifyTLSHandshakeError, errorsx.TLSHandshakeOperation, err)
|
||||
return nil, tls.ConnectionState{}, NewErrWrapper(
|
||||
ClassifyTLSHandshakeError, TLSHandshakeOperation, err)
|
||||
}
|
||||
return tlsconn, state, nil
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import (
|
||||
|
||||
"github.com/apex/log"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
|
||||
)
|
||||
|
||||
@@ -544,7 +543,7 @@ func TestTLSHandshakerErrWrapper(t *testing.T) {
|
||||
}
|
||||
ctx := context.Background()
|
||||
conn, _, err := th.Handshake(ctx, &mocks.Conn{}, &tls.Config{})
|
||||
if err == nil || err.Error() != errorsx.FailureEOFError {
|
||||
if err == nil || err.Error() != FailureEOFError {
|
||||
t.Fatal("unexpected err", err)
|
||||
}
|
||||
if conn != nil {
|
||||
|
||||
Reference in New Issue
Block a user