Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxy: extract tcp logic out further #24

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 29 additions & 54 deletions pkg/proxy/backend/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
package backend

import (
"crypto/tls"
"net"
"strings"
"testing"

Expand Down Expand Up @@ -44,22 +42,17 @@ func TestTLSConnection(t *testing.T) {
},
}

tc := newTCPConnSuite(t)
cfgOverriders := getCfgCombinations(cfgs)
runTest(t, func(backendListener, proxyListener net.Listener, clientTLSConfig, backendTLSConfig *tls.Config) {
for _, cfgs := range cfgOverriders {
cfg := newTestConfig(cfgs...)
cfg.setTLSConfig(clientTLSConfig, backendTLSConfig)
ts := newTestSuite(t, cfg)
clientErr, proxyErr, backendErr := ts.authenticateFirstTime(backendListener, proxyListener)
if cfg.backendConfig.capability&mysql.ClientSSL == 0 {
require.ErrorContains(t, proxyErr, "must enable TLS")
} else {
require.NoError(t, clientErr)
require.NoError(t, proxyErr)
require.NoError(t, backendErr)
for _, cfgs := range cfgOverriders {
ts, clean := newTestSuite(t, tc, cfgs...)
ts.authenticateFirstTime(t, func(t *testing.T, _ *testSuite, _, _, perr error) {
if ts.mb.backendConfig.capability&mysql.ClientSSL == 0 {
require.ErrorContains(t, perr, "must enable TLS")
}
}
})
})
clean()
}
}

func TestAuthPlugin(t *testing.T) {
Expand Down Expand Up @@ -114,18 +107,13 @@ func TestAuthPlugin(t *testing.T) {
},
}

tc := newTCPConnSuite(t)
cfgOverriders := getCfgCombinations(cfgs)
runTest(t, func(backendListener, proxyListener net.Listener, clientTLSConfig, backendTLSConfig *tls.Config) {
for _, cfgs := range cfgOverriders {
cfg := newTestConfig(cfgs...)
cfg.setTLSConfig(clientTLSConfig, backendTLSConfig)
ts := newTestSuite(t, cfg)
clientErr, proxyErr, backendErr := ts.authenticateFirstTime(backendListener, proxyListener)
require.NoError(t, clientErr)
require.NoError(t, proxyErr)
require.NoError(t, backendErr)
}
})
for _, cfgs := range cfgOverriders {
ts, clean := newTestSuite(t, tc, cfgs...)
ts.authenticateFirstTime(t, nil)
clean()
}
}

func TestCapability(t *testing.T) {
Expand Down Expand Up @@ -165,18 +153,13 @@ func TestCapability(t *testing.T) {
},
}

tc := newTCPConnSuite(t)
cfgOverriders := getCfgCombinations(cfgs)
runTest(t, func(backendListener, proxyListener net.Listener, clientTLSConfig, backendTLSConfig *tls.Config) {
for _, cfgs := range cfgOverriders {
cfg := newTestConfig(cfgs...)
cfg.setTLSConfig(clientTLSConfig, backendTLSConfig)
ts := newTestSuite(t, cfg)
clientErr, proxyErr, backendErr := ts.authenticateFirstTime(backendListener, proxyListener)
require.NoError(t, clientErr)
require.NoError(t, proxyErr)
require.NoError(t, backendErr)
}
})
for _, cfgs := range cfgOverriders {
ts, clean := newTestSuite(t, tc, cfgs...)
ts.authenticateFirstTime(t, nil)
clean()
}
}

func TestSecondHandshake(t *testing.T) {
Expand All @@ -193,20 +176,12 @@ func TestSecondHandshake(t *testing.T) {
},
}

runTest(t, func(backendListener, proxyListener net.Listener, clientTLSConfig, backendTLSConfig *tls.Config) {
for _, hook := range hooks {
cfg := newTestConfig()
cfg.setTLSConfig(clientTLSConfig, backendTLSConfig)
ts := newTestSuite(t, cfg)
clientErr, proxyErr, backendErr := ts.authenticateFirstTime(backendListener, proxyListener)
require.NoError(t, clientErr)
require.NoError(t, proxyErr)
require.NoError(t, backendErr)
// Call the hook after first handshake.
hook(ts)
proxyErr, backendErr = ts.authenticateSecondTime(backendListener, proxyListener)
require.NoError(t, proxyErr)
require.NoError(t, backendErr)
}
})
tc := newTCPConnSuite(t)
for _, hook := range hooks {
ts, clean := newTestSuite(t, tc)
ts.authenticateFirstTime(t, nil)
hook(ts)
ts.authenticateSecondTime(t, nil)
clean()
}
}
118 changes: 118 additions & 0 deletions pkg/proxy/backend/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2022 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package backend

import (
"crypto/tls"
"net"
"testing"

pnet "github.com/pingcap/TiProxy/pkg/proxy/net"
"github.com/pingcap/TiProxy/pkg/util/security"
"github.com/pingcap/tidb/util"
"github.com/stretchr/testify/require"
)

type tcpConnSuite struct {
backendListener net.Listener
proxyListener net.Listener
backendTLSConfig *tls.Config
clientTLSConfig *tls.Config
backendIO *pnet.PacketIO
proxyBIO *pnet.PacketIO
proxyCIO *pnet.PacketIO
clientIO *pnet.PacketIO
}

func newTCPConnSuite(t *testing.T) *tcpConnSuite {
var err error

r := &tcpConnSuite{}

r.backendListener, err = net.Listen("tcp", "0.0.0.0:0")
require.NoError(t, err)
r.proxyListener, err = net.Listen("tcp", "0.0.0.0:0")
require.NoError(t, err)
r.backendTLSConfig, r.clientTLSConfig, err = security.CreateTLSConfigForTest()
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, r.backendListener.Close())
require.NoError(t, r.proxyListener.Close())
})

return r
}

func (tc *tcpConnSuite) newConn(t *testing.T) func() {
var wg util.WaitGroupWrapper
wg.Run(func() {
conn, err := tc.backendListener.Accept()
require.NoError(t, err)
tc.backendIO = pnet.NewPacketIO(conn)
})
wg.Run(func() {
backendConn, err := net.Dial("tcp", tc.backendListener.Addr().String())
require.NoError(t, err)
tc.proxyBIO = pnet.NewPacketIO(backendConn)

clientConn, err := tc.proxyListener.Accept()
require.NoError(t, err)
tc.proxyCIO = pnet.NewPacketIO(clientConn)
})
wg.Run(func() {
conn, err := net.Dial("tcp", tc.proxyListener.Addr().String())
require.NoError(t, err)
tc.clientIO = pnet.NewPacketIO(conn)
})
wg.Wait()
return func() {
// may be closed twice
_ = tc.clientIO.Close()
_ = tc.proxyCIO.Close()
_ = tc.proxyBIO.Close()
_ = tc.backendIO.Close()
}
}

func (tc *tcpConnSuite) run(t *testing.T, clientRunner, backendRunner func(*pnet.PacketIO) error, proxyRunner func(*pnet.PacketIO, *pnet.PacketIO) error) (cerr, berr, perr error) {
var wg util.WaitGroupWrapper
wg.Run(func() {
if clientRunner != nil {
xhebox marked this conversation as resolved.
Show resolved Hide resolved
cerr = clientRunner(tc.clientIO)
if cerr != nil {
require.NoError(t, tc.clientIO.Close())
}
Comment on lines +94 to +96
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be closed even if cerr==nil. Why not close them all at last?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because you may want to call tc.run() again to combine multiple methods into one test. For example, handshakeFirstTime and handshakeSecondTime.

}
})
wg.Run(func() {
if backendRunner != nil {
berr = backendRunner(tc.backendIO)
if berr != nil {
require.NoError(t, tc.backendIO.Close())
}
}
})
wg.Run(func() {
if proxyRunner != nil {
perr = proxyRunner(tc.proxyCIO, tc.proxyBIO)
if perr != nil {
require.NoError(t, tc.proxyCIO.Close())
require.NoError(t, tc.proxyBIO.Close())
}
}
})
wg.Wait()
return
}
Loading