2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
2022-05-31 21:53:01 +02:00
|
|
|
"net"
|
2021-02-02 12:05:47 +01:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-05-25 17:03:58 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
2022-05-31 21:53:01 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/runtimex"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSaverResolverFailure(t *testing.T) {
|
|
|
|
expected := errors.New("no such host")
|
2022-05-31 21:53:01 +02:00
|
|
|
saver := &Saver{}
|
2022-06-01 07:44:54 +02:00
|
|
|
reso := saver.WrapResolver(NewFakeResolverWithExplicitError(expected))
|
2021-02-02 12:05:47 +01:00
|
|
|
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")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[0].Value().Hostname != "www.google.com" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[0].Name() != "resolve_start" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Addresses != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Addresses")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Duration <= 0 {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !errors.Is(ev[1].Value().Err, expected) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Hostname != "www.google.com" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Name() != "resolve_done" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaverResolverSuccess(t *testing.T) {
|
|
|
|
expected := []string{"8.8.8.8", "8.8.4.4"}
|
2022-05-31 21:53:01 +02:00
|
|
|
saver := &Saver{}
|
2022-06-01 07:44:54 +02:00
|
|
|
reso := saver.WrapResolver(NewFakeResolverWithResult(expected))
|
2021-02-02 12:05:47 +01:00
|
|
|
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")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[0].Value().Hostname != "www.google.com" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[0].Name() != "resolve_start" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !reflect.DeepEqual(ev[1].Value().Addresses, expected) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Addresses")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Duration <= 0 {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Err != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Hostname != "www.google.com" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Hostname")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Name() != "resolve_done" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaverDNSTransportFailure(t *testing.T) {
|
|
|
|
expected := errors.New("no such host")
|
2022-05-31 21:53:01 +02:00
|
|
|
saver := &Saver{}
|
2022-06-01 07:44:54 +02:00
|
|
|
txp := saver.WrapDNSTransport(&mocks.DNSTransport{
|
|
|
|
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
return nil, expected
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
2022-06-01 07:44:54 +02:00
|
|
|
MockNetwork: func() string {
|
|
|
|
return "fake"
|
|
|
|
},
|
|
|
|
MockAddress: func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
})
|
2022-05-25 17:03:58 +02:00
|
|
|
rawQuery := []byte{0xde, 0xad, 0xbe, 0xef}
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return rawQuery, nil
|
|
|
|
},
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
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")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !bytes.Equal(ev[0].Value().DNSQuery, rawQuery) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[0].Name() != "dns_round_trip_start" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !bytes.Equal(ev[1].Value().DNSQuery, rawQuery) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
2022-06-01 15:20:28 +02:00
|
|
|
if ev[1].Value().DNSResponse != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected DNSReply")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Duration <= 0 {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !errors.Is(ev[1].Value().Err, expected) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Name() != "dns_round_trip_done" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaverDNSTransportSuccess(t *testing.T) {
|
2022-05-25 17:03:58 +02:00
|
|
|
expected := []byte{0xef, 0xbe, 0xad, 0xde}
|
2022-05-31 21:53:01 +02:00
|
|
|
saver := &Saver{}
|
2022-05-25 17:03:58 +02:00
|
|
|
response := &mocks.DNSResponse{
|
|
|
|
MockBytes: func() []byte {
|
|
|
|
return expected
|
|
|
|
},
|
|
|
|
}
|
2022-06-01 07:44:54 +02:00
|
|
|
txp := saver.WrapDNSTransport(&mocks.DNSTransport{
|
|
|
|
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
|
|
|
return response, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
},
|
2022-06-01 07:44:54 +02:00
|
|
|
MockNetwork: func() string {
|
|
|
|
return "fake"
|
|
|
|
},
|
|
|
|
MockAddress: func() string {
|
|
|
|
return ""
|
|
|
|
},
|
|
|
|
})
|
2022-05-25 17:03:58 +02:00
|
|
|
rawQuery := []byte{0xde, 0xad, 0xbe, 0xef}
|
|
|
|
query := &mocks.DNSQuery{
|
|
|
|
MockBytes: func() ([]byte, error) {
|
|
|
|
return rawQuery, nil
|
|
|
|
},
|
|
|
|
}
|
2021-02-02 12:05:47 +01:00
|
|
|
reply, err := txp.RoundTrip(context.Background(), query)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("we expected nil error here")
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
if !bytes.Equal(reply.Bytes(), expected) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("expected another reply here")
|
|
|
|
}
|
|
|
|
ev := saver.Read()
|
|
|
|
if len(ev) != 2 {
|
|
|
|
t.Fatal("expected number of events")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !bytes.Equal(ev[0].Value().DNSQuery, rawQuery) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[0].Name() != "dns_round_trip_start" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[0].Value().Time.Before(time.Now()) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !bytes.Equal(ev[1].Value().DNSQuery, rawQuery) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected DNSQuery")
|
|
|
|
}
|
2022-06-01 15:20:28 +02:00
|
|
|
if !bytes.Equal(ev[1].Value().DNSResponse, expected) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected DNSReply")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Duration <= 0 {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Duration")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Value().Err != nil {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected Err")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if ev[1].Name() != "dns_round_trip_done" {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("unexpected name")
|
|
|
|
}
|
2022-06-01 14:32:16 +02:00
|
|
|
if !ev[1].Value().Time.After(ev[0].Value().Time) {
|
2021-02-02 12:05:47 +01:00
|
|
|
t.Fatal("the saved time is wrong")
|
|
|
|
}
|
|
|
|
}
|
2022-05-31 21:53:01 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|