ooni-probe-cli/internal/netxlite/quirks_test.go
Simone Basso c1b06a2d09
fix(netxlite): prefer composition over embedding (#733)
This diff has been extracted and adapted from 8848c8c516

The reason to prefer composition over embedding is that we want the
build to break if we add new methods to interfaces we define. If the build
does not break, we may forget about wrapping methods we should
actually be wrapping. I noticed this issue inside netxlite when I was working
on websteps-illustrated and I added support for NS and PTR queries.

See https://github.com/ooni/probe/issues/2096

While there, perform comprehensive netxlite code review
and apply minor changes and improve the docs.
2022-05-15 19:25:27 +02:00

99 lines
2.1 KiB
Go

package netxlite
import (
"errors"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestQuirkReduceErrors(t *testing.T) {
t.Run("no errors", func(t *testing.T) {
result := quirkReduceErrors(nil)
if !errors.Is(result, errReduceErrorsEmptyList) {
t.Fatal("wrong result")
}
})
t.Run("single error", func(t *testing.T) {
err := errors.New("mocked error")
result := quirkReduceErrors([]error{err})
if result != err {
t.Fatal("wrong result")
}
})
t.Run("multiple errors", func(t *testing.T) {
err1 := errors.New("mocked error #1")
err2 := errors.New("mocked error #2")
result := quirkReduceErrors([]error{err1, err2})
if result.Error() != "mocked error #1" {
t.Fatal("wrong result")
}
})
t.Run("multiple errors with meaningful ones", func(t *testing.T) {
err1 := errors.New("mocked error #1")
err2 := newErrWrapper(
classifyGenericError,
CloseOperation,
errors.New("antani"),
)
err3 := newErrWrapper(
classifyGenericError,
CloseOperation,
ECONNREFUSED,
)
err4 := errors.New("mocked error #3")
result := quirkReduceErrors([]error{err1, err2, err3, err4})
if result.Error() != FailureConnectionRefused {
t.Fatal("wrong result")
}
})
}
func TestQuirkSortIPAddrs(t *testing.T) {
t.Run("with some addrs", func(t *testing.T) {
addrs := []string{
"::1",
"192.168.1.2",
"x.org", // ensure we skip non IP addrs
"2a00:1450:4002:404::2004",
"example.com", // ensure we skip non IP addrs
"142.250.184.36",
"2604:8800:5000:82:466:38ff:fecb:d46e",
"198.145.29.83",
"95.216.163.36",
}
expected := []string{
"192.168.1.2",
"142.250.184.36",
"198.145.29.83",
"95.216.163.36",
"::1",
"2a00:1450:4002:404::2004",
"2604:8800:5000:82:466:38ff:fecb:d46e",
}
out := quirkSortIPAddrs(addrs)
if diff := cmp.Diff(expected, out); diff != "" {
t.Fatal(diff)
}
})
t.Run("with an empty list", func(t *testing.T) {
if quirkSortIPAddrs(nil) != nil {
t.Fatal("expected nil output")
}
})
t.Run("with non-IP addrs", func(t *testing.T) {
addrs := []string{
"example.com",
"x.org",
}
if quirkSortIPAddrs(addrs) != nil {
t.Fatal("expected nil output")
}
})
}