ooni-probe-cli/internal/netxlite/legacy_test.go
Simone Basso 273b70bacc
refactor: interfaces and data types into the model package (#642)
## Checklist

- [x] I have read the [contribution guidelines](https://github.com/ooni/probe-cli/blob/master/CONTRIBUTING.md)
- [x] reference issue for this pull request: https://github.com/ooni/probe/issues/1885
- [x] related ooni/spec pull request: N/A

Location of the issue tracker: https://github.com/ooni/probe

## Description

This PR contains a set of changes to move important interfaces and data types into the `./internal/model` package.

The criteria for including an interface or data type in here is roughly that the type should be important and used by several packages. We are especially interested to move more interfaces here to increase modularity.

An additional side effect is that, by reading this package, one should be able to understand more quickly how different parts of the codebase interact with each other.

This is what I want to move in `internal/model`:

- [x] most important interfaces from `internal/netxlite`
- [x] everything that was previously part of `internal/engine/model`
- [x] mocks from `internal/netxlite/mocks` should also be moved in here as a subpackage
2022-01-03 13:53:23 +01:00

101 lines
2.2 KiB
Go

package netxlite
import (
"context"
"errors"
"net"
"testing"
"github.com/ooni/probe-cli/v3/internal/model/mocks"
nlmocks "github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
func TestResolverLegacyAdapter(t *testing.T) {
t.Run("with compatible type", func(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")
}
})
t.Run("with incompatible type", func(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
})
t.Run("for LookupHTTPS", func(t *testing.T) {
r := NewResolverLegacyAdapter(&net.Resolver{})
https, err := r.LookupHTTPS(context.Background(), "x.org")
if !errors.Is(err, ErrNoDNSTransport) {
t.Fatal("not the error we expected")
}
if https != nil {
t.Fatal("expected nil result")
}
})
}
func TestDialerLegacyAdapter(t *testing.T) {
t.Run("with compatible type", func(t *testing.T) {
var called bool
r := NewDialerLegacyAdapter(&mocks.Dialer{
MockCloseIdleConnections: func() {
called = true
},
})
r.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
})
t.Run("with incompatible type", func(t *testing.T) {
r := NewDialerLegacyAdapter(&net.Dialer{})
r.CloseIdleConnections() // does not crash
})
}
func TestQUICContextDialerAdapter(t *testing.T) {
t.Run("with compatible type", func(t *testing.T) {
var called bool
d := NewQUICDialerFromContextDialerAdapter(&mocks.QUICDialer{
MockCloseIdleConnections: func() {
called = true
},
})
d.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
})
t.Run("with incompatible type", func(t *testing.T) {
d := NewQUICDialerFromContextDialerAdapter(&nlmocks.QUICContextDialer{})
d.CloseIdleConnections() // does not crash
})
}