273b70bacc
## 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
88 lines
2.1 KiB
Go
88 lines
2.1 KiB
Go
package quicdialer_test
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"errors"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
|
|
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
|
"github.com/ooni/probe-cli/v3/internal/model/mocks"
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/quictesting"
|
|
)
|
|
|
|
func TestQUICListenerSaverCannotListen(t *testing.T) {
|
|
expected := errors.New("mocked error")
|
|
qls := &quicdialer.QUICListenerSaver{
|
|
QUICListener: &mocks.QUICListener{
|
|
MockListen: func(addr *net.UDPAddr) (model.UDPLikeConn, error) {
|
|
return nil, expected
|
|
},
|
|
},
|
|
Saver: &trace.Saver{},
|
|
}
|
|
pconn, err := qls.Listen(&net.UDPAddr{
|
|
IP: []byte{},
|
|
Port: 8080,
|
|
Zone: "",
|
|
})
|
|
if !errors.Is(err, expected) {
|
|
t.Fatal("unexpected error", err)
|
|
}
|
|
if pconn != nil {
|
|
t.Fatal("expected nil pconn here")
|
|
}
|
|
}
|
|
|
|
func TestSystemDialerSuccessWithReadWrite(t *testing.T) {
|
|
// This is the most common use case for collecting reads, writes
|
|
tlsConf := &tls.Config{
|
|
NextProtos: []string{"h3"},
|
|
ServerName: quictesting.Domain,
|
|
}
|
|
saver := &trace.Saver{}
|
|
systemdialer := &netxlite.QUICDialerQUICGo{
|
|
QUICListener: &quicdialer.QUICListenerSaver{
|
|
QUICListener: &netxlite.QUICListenerStdlib{},
|
|
Saver: saver,
|
|
},
|
|
}
|
|
_, err := systemdialer.DialContext(context.Background(), "udp",
|
|
quictesting.Endpoint("443"), tlsConf, &quic.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ev := saver.Read()
|
|
if len(ev) < 2 {
|
|
t.Fatal("unexpected number of events")
|
|
}
|
|
last := len(ev) - 1
|
|
for idx := 1; idx < last; idx++ {
|
|
if ev[idx].Data == nil {
|
|
t.Fatal("unexpected Data")
|
|
}
|
|
if ev[idx].Duration <= 0 {
|
|
t.Fatal("unexpected Duration")
|
|
}
|
|
if ev[idx].Err != nil {
|
|
t.Fatal("unexpected Err")
|
|
}
|
|
if ev[idx].NumBytes <= 0 {
|
|
t.Fatal("unexpected NumBytes")
|
|
}
|
|
switch ev[idx].Name {
|
|
case netxlite.ReadFromOperation, netxlite.WriteToOperation:
|
|
default:
|
|
t.Fatal("unexpected Name")
|
|
}
|
|
if ev[idx].Time.Before(ev[idx-1].Time) {
|
|
t.Fatal("unexpected Time", ev[idx].Time, ev[idx-1].Time)
|
|
}
|
|
}
|
|
}
|