ooni-probe-cli/internal/engine/experiment/dht/dht_test.go
2022-11-22 17:06:02 +01:00

126 lines
2.8 KiB
Go

package dht
import (
"context"
"errors"
"fmt"
"log"
"testing"
"github.com/anacrolix/dht/v2"
"github.com/ooni/probe-cli/v3/internal/engine/mockable"
"github.com/ooni/probe-cli/v3/internal/model"
)
func TestMeasurer_run(t *testing.T) {
// expectedPings is the expected number of pings
const expectedPings = 4
// runHelper is an helper function to run this set of tests.
runHelper := func(input string) (*model.Measurement, model.ExperimentMeasurer, error) {
m := NewExperimentMeasurer(Config{})
if m.ExperimentName() != "dht" {
t.Fatal("invalid experiment name")
}
if m.ExperimentVersion() != "0.0.1" {
t.Fatal("invalid experiment version")
}
ctx := context.Background()
meas := &model.Measurement{
Input: model.MeasurementTarget(input),
}
sess := &mockable.Session{
MockableLogger: model.DiscardLogger,
}
callbacks := model.NewPrinterCallbacks(model.DiscardLogger)
err := m.Run(ctx, sess, meas, callbacks)
return meas, m, err
}
t.Run("with empty input", func(t *testing.T) {
_, _, err := runHelper("")
if !errors.Is(err, errNoInputProvided) {
t.Fatal("unexpected error", err)
}
})
t.Run("with invalid URL", func(t *testing.T) {
_, _, err := runHelper("\t")
if !errors.Is(err, errInputIsNotAnURL) {
t.Fatal("unexpected error", err)
}
})
t.Run("with invalid scheme", func(t *testing.T) {
_, _, err := runHelper("https://8.8.8.8:443/")
if !errors.Is(err, errInvalidScheme) {
t.Fatal("unexpected error", err)
}
})
t.Run("with missing port", func(t *testing.T) {
_, _, err := runHelper("dht://8.8.8.8")
if !errors.Is(err, errMissingPort) {
t.Fatal("unexpected error", err)
}
})
t.Run("with local listener", func(t *testing.T) {
conf := new(dht.ServerConfig)
conf.StartingNodes = func() (addrs []dht.Addr, err error) {
return []dht.Addr{}, nil
}
conf.Passive = false
dht, err := dht.NewServer(conf)
if err != nil {
log.Fatal(err)
}
defer dht.Close()
_, _ = dht.Bootstrap()
println(dht.Addr().String())
url := fmt.Sprintf("dht://%s", dht.Addr().String())
meas, m, err := runHelper(url)
if err != nil {
t.Fatal(err)
}
tk := meas.TestKeys.(*TestKeys)
if tk.Failure != "" {
t.Fatal(tk.Failure)
}
if len(tk.Runs) != 1 {
t.Fatal("Expected one DHT run")
}
run := tk.Runs[0]
if run.Failure != "" {
t.Fatal(run.Failure)
}
if run.BootstrapNum != 1 {
t.Fatal("Expected only one bootstrap node")
}
if run.PeersResponded != 1 {
t.Fatal("Expected bootstrap node to respond")
}
if tk.Failed == true {
t.Fatal("Found TestKeys.Failed but found no global/run failure")
}
ask, err := m.GetSummaryKeys(meas)
if err != nil {
t.Fatal("cannot obtain summary")
}
summary := ask.(SummaryKeys)
if summary.IsAnomaly {
t.Fatal("expected no anomaly")
}
})
}