From 8ddec182caff7c1f9d49aa76a69d2fd10733e613 Mon Sep 17 00:00:00 2001 From: frcroth Date: Thu, 19 Dec 2024 15:59:47 +0100 Subject: [PATCH] Add tests to DDR experiment --- internal/experiment/ddr/ddr.go | 19 ++++--- internal/experiment/ddr/ddr_test.go | 78 +++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 6 deletions(-) diff --git a/internal/experiment/ddr/ddr.go b/internal/experiment/ddr/ddr.go index c12ba5ac4..946d12178 100644 --- a/internal/experiment/ddr/ddr.go +++ b/internal/experiment/ddr/ddr.go @@ -19,6 +19,9 @@ const ( ) type Config struct { + // CustomResolver is the custom resolver to use. + // If empty, the system resolver is used. + CustomResolver *string } // Measurer performs the measurement. @@ -70,19 +73,23 @@ func (m *Measurer) Run( ctx, cancel := context.WithTimeout(ctx, timeout) defer cancel() - systemResolver := getSystemResolverAddress() - if systemResolver == "" { - return errors.New("could not get system resolver") + if m.config.CustomResolver == nil { + systemResolver := getSystemResolverAddress() + if systemResolver == "" { + return errors.New("could not get system resolver") + } + log.Infof("Using system resolver: %s", systemResolver) + tk.Resolver = systemResolver + } else { + tk.Resolver = *m.config.CustomResolver } - log.Infof("Using system resolver: %s", systemResolver) - tk.Resolver = systemResolver // DDR queries are queries of the SVCB type for the _dns.resolver.arpa. domain. netx := &netxlite.Netx{} dialer := netx.NewDialerWithoutResolver(log.Log) transport := netxlite.NewUnwrappedDNSOverUDPTransport( - dialer, systemResolver) + dialer, tk.Resolver) encoder := &netxlite.DNSEncoderMiekg{} query := encoder.Encode( "_dns.resolver.arpa.", // As specified in RFC 9462 diff --git a/internal/experiment/ddr/ddr_test.go b/internal/experiment/ddr/ddr_test.go index ee848efeb..28d930621 100644 --- a/internal/experiment/ddr/ddr_test.go +++ b/internal/experiment/ddr/ddr_test.go @@ -1,7 +1,12 @@ package ddr import ( + "context" "testing" + + "github.com/apex/log" + "github.com/ooni/probe-cli/v3/internal/mocks" + "github.com/ooni/probe-cli/v3/internal/model" ) func TestMeasurerExperimentNameVersion(t *testing.T) { @@ -13,3 +18,76 @@ func TestMeasurerExperimentNameVersion(t *testing.T) { t.Fatal("unexpected ExperimentVersion") } } + +func TestMeasurerRun(t *testing.T) { + if testing.Short() { + t.Skip("skip test in short mode") + } + + oneOneOneOneResolver := "1.1.1.1:53" + + measurer := NewExperimentMeasurer(Config{ + CustomResolver: &oneOneOneOneResolver, + }) + args := &model.ExperimentArgs{ + Callbacks: model.NewPrinterCallbacks(log.Log), + Measurement: new(model.Measurement), + Session: &mocks.Session{ + MockLogger: func() model.Logger { + return log.Log + }, + }, + } + if err := measurer.Run(context.Background(), args); err != nil { + t.Fatal(err) + } + tk := args.Measurement.TestKeys.(*TestKeys) + if tk.Failure != nil { + t.Fatal("unexpected Failure") + } + + if tk.Resolver != oneOneOneOneResolver { + t.Fatal("Resolver should be written to TestKeys") + } + + // 1.1.1.1 supports DDR + if tk.SupportsDDR != true { + t.Fatal("unexpected value for Supports DDR") + } +} + +// This test fails because the resolver is a domain name and not an IP address. +func TestMeasurerFailsWithDomainResolver(t *testing.T) { + invalidResolver := "invalid-resolver.example:53" + + tk, _ := runExperiment(invalidResolver) + if tk.Failure == nil { + t.Fatal("expected Failure") + } +} + +func TestMeasurerFailsWithNoPort(t *testing.T) { + invalidResolver := "1.1.1.1" + + tk, _ := runExperiment(invalidResolver) + if tk.Failure == nil { + t.Fatal("expected Failure") + } +} + +func runExperiment(resolver string) (*TestKeys, error) { + measurer := NewExperimentMeasurer(Config{ + CustomResolver: &resolver, + }) + args := &model.ExperimentArgs{ + Callbacks: model.NewPrinterCallbacks(log.Log), + Measurement: new(model.Measurement), + Session: &mocks.Session{ + MockLogger: func() model.Logger { + return log.Log + }, + }, + } + err := measurer.Run(context.Background(), args) + return args.Measurement.TestKeys.(*TestKeys), err +}