ooni-probe-cli/internal/netxlite/quirks_test.go
Simone Basso 1c057d322d
cleanup: merge legacy errorsx in netxlite and hide classifiers (#655)
This diff implements the first two cleanups defined at https://github.com/ooni/probe/issues/1956:

> - [ ] observe that `netxlite` and `netx` differ in error wrapping only in the way in which we set `ErrWrapper.Operation`. Observe that the code using `netxlite` does not care about such a field. Therefore, we can modify `netxlite` to set such a field using the code of `netx` and we can remove `netx` specific code for errors (which currently lives inside of the `./internal/engine/legacy/errorsx` package
>
> - [ ] after we've done the previous cleanup, we can make all the classifiers code private, since there's no code outside `netxlite` that needs them

A subsequent diff will address the remaining cleanup.

While there, notice that there are failing, unrelated obfs4 tests, so disable them in short mode. (I am confident these tests are unrelated because they fail for me when running test locally from the `master` branch.)
2022-01-07 17:31:21 +01:00

87 lines
1.8 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 result != nil {
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",
"2a00:1450:4002:404::2004",
"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")
}
})
}