f4f3ed7c42
The code that is now into the tracex package was written a long time ago, so let's start to make it more in line with the coding style of packages that were written more recently. I didn't apply all the changes I'd like to apply in a single diff and for now I am committing just this diff. Broadly, what we need to do is: 1. improve documentation 2. ~always use pointer receivers (object receives have the issue that they are not mutable by accident meaning that you can mutate them but their state do not change after the call returns, which is potentially a source of bugs in case you later refactor to use a pointer receiver, so always use pointer receivers) 3. ~always avoid embedding (let's say we want to avoid embedding for types we define and it's instead fine to embed types that are defined in the stdlib: if later we add a new method, we will not see a broken build and we'll probably forget to add the new method to all wrappers -- conversely, if we're wrapping rather than embedding, we'll see a broken build and act accordingly) 4. prefer unit tests and group tests by type being tested rather than using a flat structure for tests There's a coverage slippage that I'll compensate in a follow-up diff where I'll focus on unit testing. Reference issue: https://github.com/ooni/probe/issues/2121
276 lines
6.6 KiB
Go
276 lines
6.6 KiB
Go
package tracex
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"net"
|
|
"reflect"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
|
)
|
|
|
|
func TestSaverResolverFailure(t *testing.T) {
|
|
expected := errors.New("no such host")
|
|
saver := &Saver{}
|
|
reso := saver.WrapResolver(NewFakeResolverWithExplicitError(expected))
|
|
addrs, err := reso.LookupHost(context.Background(), "www.google.com")
|
|
if !errors.Is(err, expected) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if addrs != nil {
|
|
t.Fatal("expected nil address here")
|
|
}
|
|
ev := saver.Read()
|
|
if len(ev) != 2 {
|
|
t.Fatal("expected number of events")
|
|
}
|
|
if ev[0].Hostname != "www.google.com" {
|
|
t.Fatal("unexpected Hostname")
|
|
}
|
|
if ev[0].Name != "resolve_start" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[0].Time.Before(time.Now()) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
if ev[1].Addresses != nil {
|
|
t.Fatal("unexpected Addresses")
|
|
}
|
|
if ev[1].Duration <= 0 {
|
|
t.Fatal("unexpected Duration")
|
|
}
|
|
if !errors.Is(ev[1].Err, expected) {
|
|
t.Fatal("unexpected Err")
|
|
}
|
|
if ev[1].Hostname != "www.google.com" {
|
|
t.Fatal("unexpected Hostname")
|
|
}
|
|
if ev[1].Name != "resolve_done" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[1].Time.After(ev[0].Time) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
}
|
|
|
|
func TestSaverResolverSuccess(t *testing.T) {
|
|
expected := []string{"8.8.8.8", "8.8.4.4"}
|
|
saver := &Saver{}
|
|
reso := saver.WrapResolver(NewFakeResolverWithResult(expected))
|
|
addrs, err := reso.LookupHost(context.Background(), "www.google.com")
|
|
if err != nil {
|
|
t.Fatal("expected nil error here")
|
|
}
|
|
if !reflect.DeepEqual(addrs, expected) {
|
|
t.Fatal("not the result we expected")
|
|
}
|
|
ev := saver.Read()
|
|
if len(ev) != 2 {
|
|
t.Fatal("expected number of events")
|
|
}
|
|
if ev[0].Hostname != "www.google.com" {
|
|
t.Fatal("unexpected Hostname")
|
|
}
|
|
if ev[0].Name != "resolve_start" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[0].Time.Before(time.Now()) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
if !reflect.DeepEqual(ev[1].Addresses, expected) {
|
|
t.Fatal("unexpected Addresses")
|
|
}
|
|
if ev[1].Duration <= 0 {
|
|
t.Fatal("unexpected Duration")
|
|
}
|
|
if ev[1].Err != nil {
|
|
t.Fatal("unexpected Err")
|
|
}
|
|
if ev[1].Hostname != "www.google.com" {
|
|
t.Fatal("unexpected Hostname")
|
|
}
|
|
if ev[1].Name != "resolve_done" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[1].Time.After(ev[0].Time) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
}
|
|
|
|
func TestSaverDNSTransportFailure(t *testing.T) {
|
|
expected := errors.New("no such host")
|
|
saver := &Saver{}
|
|
txp := saver.WrapDNSTransport(&mocks.DNSTransport{
|
|
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
return nil, expected
|
|
},
|
|
MockNetwork: func() string {
|
|
return "fake"
|
|
},
|
|
MockAddress: func() string {
|
|
return ""
|
|
},
|
|
})
|
|
rawQuery := []byte{0xde, 0xad, 0xbe, 0xef}
|
|
query := &mocks.DNSQuery{
|
|
MockBytes: func() ([]byte, error) {
|
|
return rawQuery, nil
|
|
},
|
|
}
|
|
reply, err := txp.RoundTrip(context.Background(), query)
|
|
if !errors.Is(err, expected) {
|
|
t.Fatal("not the error we expected")
|
|
}
|
|
if reply != nil {
|
|
t.Fatal("expected nil reply here")
|
|
}
|
|
ev := saver.Read()
|
|
if len(ev) != 2 {
|
|
t.Fatal("expected number of events")
|
|
}
|
|
if !bytes.Equal(ev[0].DNSQuery, rawQuery) {
|
|
t.Fatal("unexpected DNSQuery")
|
|
}
|
|
if ev[0].Name != "dns_round_trip_start" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[0].Time.Before(time.Now()) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
if !bytes.Equal(ev[1].DNSQuery, rawQuery) {
|
|
t.Fatal("unexpected DNSQuery")
|
|
}
|
|
if ev[1].DNSReply != nil {
|
|
t.Fatal("unexpected DNSReply")
|
|
}
|
|
if ev[1].Duration <= 0 {
|
|
t.Fatal("unexpected Duration")
|
|
}
|
|
if !errors.Is(ev[1].Err, expected) {
|
|
t.Fatal("unexpected Err")
|
|
}
|
|
if ev[1].Name != "dns_round_trip_done" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[1].Time.After(ev[0].Time) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
}
|
|
|
|
func TestSaverDNSTransportSuccess(t *testing.T) {
|
|
expected := []byte{0xef, 0xbe, 0xad, 0xde}
|
|
saver := &Saver{}
|
|
response := &mocks.DNSResponse{
|
|
MockBytes: func() []byte {
|
|
return expected
|
|
},
|
|
}
|
|
txp := saver.WrapDNSTransport(&mocks.DNSTransport{
|
|
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
return response, nil
|
|
},
|
|
MockNetwork: func() string {
|
|
return "fake"
|
|
},
|
|
MockAddress: func() string {
|
|
return ""
|
|
},
|
|
})
|
|
rawQuery := []byte{0xde, 0xad, 0xbe, 0xef}
|
|
query := &mocks.DNSQuery{
|
|
MockBytes: func() ([]byte, error) {
|
|
return rawQuery, nil
|
|
},
|
|
}
|
|
reply, err := txp.RoundTrip(context.Background(), query)
|
|
if err != nil {
|
|
t.Fatal("we expected nil error here")
|
|
}
|
|
if !bytes.Equal(reply.Bytes(), expected) {
|
|
t.Fatal("expected another reply here")
|
|
}
|
|
ev := saver.Read()
|
|
if len(ev) != 2 {
|
|
t.Fatal("expected number of events")
|
|
}
|
|
if !bytes.Equal(ev[0].DNSQuery, rawQuery) {
|
|
t.Fatal("unexpected DNSQuery")
|
|
}
|
|
if ev[0].Name != "dns_round_trip_start" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[0].Time.Before(time.Now()) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
if !bytes.Equal(ev[1].DNSQuery, rawQuery) {
|
|
t.Fatal("unexpected DNSQuery")
|
|
}
|
|
if !bytes.Equal(ev[1].DNSReply, expected) {
|
|
t.Fatal("unexpected DNSReply")
|
|
}
|
|
if ev[1].Duration <= 0 {
|
|
t.Fatal("unexpected Duration")
|
|
}
|
|
if ev[1].Err != nil {
|
|
t.Fatal("unexpected Err")
|
|
}
|
|
if ev[1].Name != "dns_round_trip_done" {
|
|
t.Fatal("unexpected name")
|
|
}
|
|
if !ev[1].Time.After(ev[0].Time) {
|
|
t.Fatal("the saved time is wrong")
|
|
}
|
|
}
|
|
|
|
func NewFakeResolverWithExplicitError(err error) model.Resolver {
|
|
runtimex.PanicIfNil(err, "passed nil error")
|
|
return &mocks.Resolver{
|
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
|
return nil, err
|
|
},
|
|
MockNetwork: func() string {
|
|
return "fake"
|
|
},
|
|
MockAddress: func() string {
|
|
return ""
|
|
},
|
|
MockCloseIdleConnections: func() {
|
|
// nothing
|
|
},
|
|
MockLookupHTTPS: func(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
|
return nil, errors.New("not implemented")
|
|
},
|
|
MockLookupNS: func(ctx context.Context, domain string) ([]*net.NS, error) {
|
|
return nil, errors.New("not implemented")
|
|
},
|
|
}
|
|
}
|
|
|
|
func NewFakeResolverWithResult(r []string) model.Resolver {
|
|
return &mocks.Resolver{
|
|
MockLookupHost: func(ctx context.Context, domain string) ([]string, error) {
|
|
return r, nil
|
|
},
|
|
MockNetwork: func() string {
|
|
return "fake"
|
|
},
|
|
MockAddress: func() string {
|
|
return ""
|
|
},
|
|
MockCloseIdleConnections: func() {
|
|
// nothing
|
|
},
|
|
MockLookupHTTPS: func(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
|
return nil, errors.New("not implemented")
|
|
},
|
|
MockLookupNS: func(ctx context.Context, domain string) ([]*net.NS, error) {
|
|
return nil, errors.New("not implemented")
|
|
},
|
|
}
|
|
}
|