Skip to content

Commit

Permalink
Merge pull request #8 from ooni/issue/2211
Browse files Browse the repository at this point in the history
fix: ensure the conn we export implements NetConn

Part of ooni/probe#2211
  • Loading branch information
bassosimone authored Aug 19, 2022
2 parents 6cfeb2a + b56ea5a commit 0e8e89c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion tls/stdlibwrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,17 @@ func NewClientConnStdlib(conn net.Conn, config *stdlibtls.Config) (*ConnStdlib,
// We use this type instead of directly using *Conn to simplify unit
// testing of the ConnStdlib.ConnectionState method.
type connStdlibUnderlyingConn interface {
// We need to have the same methods of a net.Conn
net.Conn

// We need to be able to handshake
HandshakeContext(ctx context.Context) error

// We need to get the TLS connection state
ConnectionState() ConnectionState

// We need to get the underlying net.Conn
NetConn() net.Conn
}

// ConnStdlib is the Conn-like type returned by NewClientConnStdlib. This type
Expand All @@ -106,7 +114,9 @@ type connStdlibOOHTTPTLSLikeConn interface {

HandshakeContext(ctx context.Context) error

ConnectionState() stdlibtls.ConnectionState
ConnectionState() stdlibtls.ConnectionState // We need to cast to stdlib

NetConn() net.Conn
}

var _ connStdlibOOHTTPTLSLikeConn = &ConnStdlib{} // ensure we implement this interface
Expand Down
8 changes: 8 additions & 0 deletions tls/stdlibwrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ type connStdlibUnderlyingConnMockable struct {

// MockHandshakeContext allows to mock the HandshakeContext method.
MockHandshakeContext func(ctx context.Context) error

// MockNetConn allows to mock the NetConn method
MockNetConn func() net.Conn
}

// ConnectionState calls MockConnectionState.
Expand All @@ -79,6 +82,11 @@ func (c *connStdlibUnderlyingConnMockable) HandshakeContext(ctx context.Context)
return c.MockHandshakeContext(ctx)
}

// NetConn calls MockNetConn
func (c *connStdlibUnderlyingConnMockable) NetConn() net.Conn {
return c.MockNetConn()
}

func TestConnStdlib_ConnectionState(t *testing.T) {
type fields struct {
connLike connStdlibUnderlyingConn
Expand Down

0 comments on commit 0e8e89c

Please sign in to comment.