2021-09-09 21:24:27 +02:00
|
|
|
package mocks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ooni/probe-cli/v3/internal/atomicx"
|
2022-05-25 17:03:58 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-09-09 21:24:27 +02:00
|
|
|
)
|
|
|
|
|
2021-09-28 10:47:59 +02:00
|
|
|
func TestDNSTransport(t *testing.T) {
|
2021-09-09 21:24:27 +02:00
|
|
|
t.Run("RoundTrip", func(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
2021-09-28 10:47:59 +02:00
|
|
|
txp := &DNSTransport{
|
2022-05-25 17:03:58 +02:00
|
|
|
MockRoundTrip: func(ctx context.Context, query model.DNSQuery) (model.DNSResponse, error) {
|
2021-09-09 21:24:27 +02:00
|
|
|
return nil, expected
|
|
|
|
},
|
|
|
|
}
|
2022-05-25 17:03:58 +02:00
|
|
|
resp, err := txp.RoundTrip(context.Background(), &DNSQuery{})
|
2021-09-09 21:24:27 +02:00
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
if resp != nil {
|
|
|
|
t.Fatal("expected nil response here")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("RequiresPadding", func(t *testing.T) {
|
2021-09-28 10:47:59 +02:00
|
|
|
txp := &DNSTransport{
|
2021-09-09 21:24:27 +02:00
|
|
|
MockRequiresPadding: func() bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if txp.RequiresPadding() != true {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Network", func(t *testing.T) {
|
2021-09-28 10:47:59 +02:00
|
|
|
txp := &DNSTransport{
|
2021-09-09 21:24:27 +02:00
|
|
|
MockNetwork: func() string {
|
|
|
|
return "antani"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if txp.Network() != "antani" {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Address", func(t *testing.T) {
|
2021-09-28 10:47:59 +02:00
|
|
|
txp := &DNSTransport{
|
2021-09-09 21:24:27 +02:00
|
|
|
MockAddress: func() string {
|
|
|
|
return "mascetti"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if txp.Address() != "mascetti" {
|
|
|
|
t.Fatal("unexpected result")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("CloseIdleConnections", func(t *testing.T) {
|
|
|
|
called := &atomicx.Int64{}
|
2021-09-28 10:47:59 +02:00
|
|
|
txp := &DNSTransport{
|
2021-09-09 21:24:27 +02:00
|
|
|
MockCloseIdleConnections: func() {
|
|
|
|
called.Add(1)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
txp.CloseIdleConnections()
|
|
|
|
if called.Load() != 1 {
|
|
|
|
t.Fatal("not called")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|