Skip to content

Commit

Permalink
Add tests to DDR experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
frcroth committed Dec 19, 2024
1 parent d19b5e0 commit 8ddec18
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 6 deletions.
19 changes: 13 additions & 6 deletions internal/experiment/ddr/ddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
78 changes: 78 additions & 0 deletions internal/experiment/ddr/ddr_test.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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
}

0 comments on commit 8ddec18

Please sign in to comment.