2021-09-06 20:17:45 +02:00
|
|
|
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)
|
2022-01-26 12:18:36 +01:00
|
|
|
if !errors.Is(result, errReduceErrorsEmptyList) {
|
2021-09-06 20:17:45 +02:00
|
|
|
t.Fatal("wrong result")
|
|
|
|
}
|
|
|
|
})
|
2021-09-08 11:39:27 +02:00
|
|
|
|
2021-09-06 20:17:45 +02:00
|
|
|
t.Run("single error", func(t *testing.T) {
|
|
|
|
err := errors.New("mocked error")
|
|
|
|
result := quirkReduceErrors([]error{err})
|
|
|
|
if result != err {
|
|
|
|
t.Fatal("wrong result")
|
|
|
|
}
|
|
|
|
})
|
2021-09-08 11:39:27 +02:00
|
|
|
|
2021-09-06 20:17:45 +02:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
})
|
2021-09-08 11:39:27 +02:00
|
|
|
|
2021-09-06 20:17:45 +02:00
|
|
|
t.Run("multiple errors with meaningful ones", func(t *testing.T) {
|
|
|
|
err1 := errors.New("mocked error #1")
|
2022-07-01 12:22:22 +02:00
|
|
|
err2 := NewErrWrapper(
|
|
|
|
ClassifyGenericError,
|
2021-09-28 12:42:01 +02:00
|
|
|
CloseOperation,
|
2021-09-08 21:19:51 +02:00
|
|
|
errors.New("antani"),
|
|
|
|
)
|
2022-07-01 12:22:22 +02:00
|
|
|
err3 := NewErrWrapper(
|
|
|
|
ClassifyGenericError,
|
2021-09-28 12:42:01 +02:00
|
|
|
CloseOperation,
|
|
|
|
ECONNREFUSED,
|
2021-09-08 21:19:51 +02:00
|
|
|
)
|
2021-09-06 20:17:45 +02:00
|
|
|
err4 := errors.New("mocked error #3")
|
|
|
|
result := quirkReduceErrors([]error{err1, err2, err3, err4})
|
2021-09-28 12:42:01 +02:00
|
|
|
if result.Error() != FailureConnectionRefused {
|
2021-09-06 20:17:45 +02:00
|
|
|
t.Fatal("wrong result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestQuirkSortIPAddrs(t *testing.T) {
|
2021-09-08 22:48:10 +02:00
|
|
|
t.Run("with some addrs", func(t *testing.T) {
|
|
|
|
addrs := []string{
|
|
|
|
"::1",
|
|
|
|
"192.168.1.2",
|
2022-05-13 18:49:18 +02:00
|
|
|
"x.org", // ensure we skip non IP addrs
|
2021-09-08 22:48:10 +02:00
|
|
|
"2a00:1450:4002:404::2004",
|
2022-05-13 18:49:18 +02:00
|
|
|
"example.com", // ensure we skip non IP addrs
|
2021-09-08 22:48:10 +02:00
|
|
|
"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")
|
|
|
|
}
|
|
|
|
})
|
2022-05-13 18:49:18 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
})
|
2021-09-06 20:17:45 +02:00
|
|
|
}
|