refactor: rename i/e/n/mockablex => i/netxmocks (#397)
Needed to more easily do https://github.com/ooni/probe/issues/1505
This commit is contained in:
parent
8a0beee808
commit
16aa8e5538
16 changed files with 44 additions and 44 deletions
60
internal/netxmocks/conn.go
Normal file
60
internal/netxmocks/conn.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package netxmocks
|
||||
|
||||
import (
|
||||
"net"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Conn is a mockable net.Conn.
|
||||
type Conn struct {
|
||||
MockRead func(b []byte) (int, error)
|
||||
MockWrite func(b []byte) (int, error)
|
||||
MockClose func() error
|
||||
MockLocalAddr func() net.Addr
|
||||
MockRemoteAddr func() net.Addr
|
||||
MockSetDeadline func(t time.Time) error
|
||||
MockSetReadDeadline func(t time.Time) error
|
||||
MockSetWriteDeadline func(t time.Time) error
|
||||
}
|
||||
|
||||
// Read implements net.Conn.Read
|
||||
func (c *Conn) Read(b []byte) (int, error) {
|
||||
return c.MockRead(b)
|
||||
}
|
||||
|
||||
// Write implements net.Conn.Write
|
||||
func (c *Conn) Write(b []byte) (int, error) {
|
||||
return c.MockWrite(b)
|
||||
}
|
||||
|
||||
// Close implements net.Conn.Close
|
||||
func (c *Conn) Close() error {
|
||||
return c.MockClose()
|
||||
}
|
||||
|
||||
// LocalAddr returns the local address
|
||||
func (c *Conn) LocalAddr() net.Addr {
|
||||
return c.MockLocalAddr()
|
||||
}
|
||||
|
||||
// RemoteAddr returns the remote address
|
||||
func (c *Conn) RemoteAddr() net.Addr {
|
||||
return c.MockRemoteAddr()
|
||||
}
|
||||
|
||||
// SetDeadline sets the connection deadline.
|
||||
func (c *Conn) SetDeadline(t time.Time) error {
|
||||
return c.MockSetDeadline(t)
|
||||
}
|
||||
|
||||
// SetReadDeadline sets the read deadline.
|
||||
func (c *Conn) SetReadDeadline(t time.Time) error {
|
||||
return c.MockSetReadDeadline(t)
|
||||
}
|
||||
|
||||
// SetWriteDeadline sets the write deadline.
|
||||
func (c *Conn) SetWriteDeadline(t time.Time) error {
|
||||
return c.MockSetWriteDeadline(t)
|
||||
}
|
||||
|
||||
var _ net.Conn = &Conn{}
|
||||
126
internal/netxmocks/conn_test.go
Normal file
126
internal/netxmocks/conn_test.go
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
package netxmocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
func TestConnReadWorks(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
c := &Conn{
|
||||
MockRead: func(b []byte) (int, error) {
|
||||
return 0, expected
|
||||
},
|
||||
}
|
||||
count, err := c.Read(make([]byte, 128))
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if count != 0 {
|
||||
t.Fatal("expected 0 bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnWriteWorks(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
c := &Conn{
|
||||
MockWrite: func(b []byte) (int, error) {
|
||||
return 0, expected
|
||||
},
|
||||
}
|
||||
count, err := c.Write(make([]byte, 128))
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if count != 0 {
|
||||
t.Fatal("expected 0 bytes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnCloseWorks(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
c := &Conn{
|
||||
MockClose: func() error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := c.Close()
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnLocalAddrWorks(t *testing.T) {
|
||||
expected := &net.TCPAddr{
|
||||
IP: net.IPv6loopback,
|
||||
Port: 1234,
|
||||
}
|
||||
c := &Conn{
|
||||
MockLocalAddr: func() net.Addr {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
out := c.LocalAddr()
|
||||
if diff := cmp.Diff(expected, out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnRemoteAddrWorks(t *testing.T) {
|
||||
expected := &net.TCPAddr{
|
||||
IP: net.IPv6loopback,
|
||||
Port: 1234,
|
||||
}
|
||||
c := &Conn{
|
||||
MockRemoteAddr: func() net.Addr {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
out := c.RemoteAddr()
|
||||
if diff := cmp.Diff(expected, out); diff != "" {
|
||||
t.Fatal(diff)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnSetDeadline(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
c := &Conn{
|
||||
MockSetDeadline: func(t time.Time) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := c.SetDeadline(time.Time{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnSetReadDeadline(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
c := &Conn{
|
||||
MockSetReadDeadline: func(t time.Time) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := c.SetReadDeadline(time.Time{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnSetWriteDeadline(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
c := &Conn{
|
||||
MockSetWriteDeadline: func(t time.Time) error {
|
||||
return expected
|
||||
},
|
||||
}
|
||||
err := c.SetWriteDeadline(time.Time{})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected", err)
|
||||
}
|
||||
}
|
||||
23
internal/netxmocks/dialer.go
Normal file
23
internal/netxmocks/dialer.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package netxmocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
)
|
||||
|
||||
// dialer is the interface we expect from a dialer
|
||||
type dialer interface {
|
||||
DialContext(ctx context.Context, network, address string) (net.Conn, error)
|
||||
}
|
||||
|
||||
// Dialer is a mockable Dialer.
|
||||
type Dialer struct {
|
||||
MockDialContext func(ctx context.Context, network, address string) (net.Conn, error)
|
||||
}
|
||||
|
||||
// DialContext implements Dialer.DialContext.
|
||||
func (d Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
||||
return d.MockDialContext(ctx, network, address)
|
||||
}
|
||||
|
||||
var _ dialer = Dialer{}
|
||||
25
internal/netxmocks/dialer_test.go
Normal file
25
internal/netxmocks/dialer_test.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package netxmocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDialerWorks(t *testing.T) {
|
||||
expected := errors.New("mocked error")
|
||||
d := Dialer{
|
||||
MockDialContext: func(ctx context.Context, network string, address string) (net.Conn, error) {
|
||||
return nil, expected
|
||||
},
|
||||
}
|
||||
ctx := context.Background()
|
||||
conn, err := d.DialContext(ctx, "tcp", "8.8.8.8:53")
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatal("not the error we expected")
|
||||
}
|
||||
if conn != nil {
|
||||
t.Fatal("expected nil conn")
|
||||
}
|
||||
}
|
||||
2
internal/netxmocks/doc.go
Normal file
2
internal/netxmocks/doc.go
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
// Package netxmocks contains mocks for netx types.
|
||||
package netxmocks
|
||||
Loading…
Reference in a new issue