ooni-probe-cli/internal/netxlite/legacy_test.go

103 lines
2.3 KiB
Go
Raw Normal View History

2021-06-23 15:53:12 +02:00
package netxlite
import (
"errors"
"net"
2021-06-23 15:53:12 +02:00
"testing"
"github.com/ooni/probe-cli/v3/internal/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
2021-06-23 15:53:12 +02:00
)
func TestReduceErrors(t *testing.T) {
t.Run("no errors", func(t *testing.T) {
result := reduceErrors(nil)
2021-06-23 15:53:12 +02:00
if result != nil {
t.Fatal("wrong result")
}
})
t.Run("single error", func(t *testing.T) {
err := errors.New("mocked error")
result := reduceErrors([]error{err})
2021-06-23 15:53:12 +02:00
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 := reduceErrors([]error{err1, err2})
2021-06-23 15:53:12 +02:00
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 := &errorsx.ErrWrapper{
2021-06-23 15:53:12 +02:00
Failure: "unknown_failure: antani",
}
err3 := &errorsx.ErrWrapper{
Failure: errorsx.FailureConnectionRefused,
2021-06-23 15:53:12 +02:00
}
err4 := errors.New("mocked error #3")
result := reduceErrors([]error{err1, err2, err3, err4})
if result.Error() != errorsx.FailureConnectionRefused {
2021-06-23 15:53:12 +02:00
t.Fatal("wrong result")
}
})
}
func TestResolverLegacyAdapterWithCompatibleType(t *testing.T) {
var called bool
r := NewResolverLegacyAdapter(&mocks.Resolver{
MockNetwork: func() string {
return "network"
},
MockAddress: func() string {
return "address"
},
MockCloseIdleConnections: func() {
called = true
},
})
if r.Network() != "network" {
t.Fatal("invalid Network")
}
if r.Address() != "address" {
t.Fatal("invalid Address")
}
r.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
}
func TestResolverLegacyAdapterDefaults(t *testing.T) {
r := NewResolverLegacyAdapter(&net.Resolver{})
if r.Network() != "adapter" {
t.Fatal("invalid Network")
}
if r.Address() != "" {
t.Fatal("invalid Address")
}
r.CloseIdleConnections() // does not crash
}
func TestDialerLegacyAdapterWithCompatibleType(t *testing.T) {
var called bool
r := NewDialerLegacyAdapter(&mocks.Dialer{
MockCloseIdleConnections: func() {
called = true
},
})
r.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
}
func TestDialerLegacyAdapterDefaults(t *testing.T) {
r := NewDialerLegacyAdapter(&net.Dialer{})
r.CloseIdleConnections() // does not crash
}