cba72d1ca3
Here we're refactoring stunreachability to not provide internally a default input and to take in input an URL rather than a string. The related ooni/spec change is https://github.com/ooni/spec/pull/227. This diff has been extracted from https://github.com/ooni/probe-cli/pull/539. Because the original diff was large, I'm splitting it in a set of more easily manageable diffs. The reference issue is https://github.com/ooni/probe/issues/1814, which is complex enough to require us to proceed incrementally. This diff WILL need to be backported to release/3.11.
64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package stunreachability
|
|
|
|
import (
|
|
"io"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// TODO(bassosimone): we should use internal/netxlite/mocks rather
|
|
// than rolling out a custom type private to this package.
|
|
|
|
type FakeConn struct {
|
|
ReadError error
|
|
ReadData []byte
|
|
SetDeadlineError error
|
|
SetReadDeadlineError error
|
|
SetWriteDeadlineError error
|
|
WriteError error
|
|
}
|
|
|
|
func (c *FakeConn) Read(b []byte) (int, error) {
|
|
if len(c.ReadData) > 0 {
|
|
n := copy(b, c.ReadData)
|
|
c.ReadData = c.ReadData[n:]
|
|
return n, nil
|
|
}
|
|
if c.ReadError != nil {
|
|
return 0, c.ReadError
|
|
}
|
|
return 0, io.EOF
|
|
}
|
|
|
|
func (c *FakeConn) Write(b []byte) (n int, err error) {
|
|
if c.WriteError != nil {
|
|
return 0, c.WriteError
|
|
}
|
|
n = len(b)
|
|
return
|
|
}
|
|
|
|
func (*FakeConn) Close() (err error) {
|
|
return
|
|
}
|
|
|
|
func (*FakeConn) LocalAddr() net.Addr {
|
|
return &net.TCPAddr{}
|
|
}
|
|
|
|
func (*FakeConn) RemoteAddr() net.Addr {
|
|
return &net.TCPAddr{}
|
|
}
|
|
|
|
func (c *FakeConn) SetDeadline(t time.Time) (err error) {
|
|
return c.SetDeadlineError
|
|
}
|
|
|
|
func (c *FakeConn) SetReadDeadline(t time.Time) (err error) {
|
|
return c.SetReadDeadlineError
|
|
}
|
|
|
|
func (c *FakeConn) SetWriteDeadline(t time.Time) (err error) {
|
|
return c.SetWriteDeadlineError
|
|
}
|