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

test: complete basic e2e test with echo frontend/backend #3

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type ClientOpt func(c *Client)
// to the control server
func WithControlTLSConfig(tlsConfig *tls.Config) ClientOpt {
return func(c *Client) {
c.controlTTLSconfig = tlsConfig
c.controlTTLSconfig = tlsConfig.Clone()
}
}

Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func New(basename string, logger *zap.Logger) *Server {

func (s *Server) Start(laddr string, tlsConfig *tls.Config) error {
var serverName string
tlsConfig = tlsConfig.Clone()
tlsConfig.GetConfigForClient = func(info *tls.ClientHelloInfo) (*tls.Config, error) {
serverName = info.ServerName
return nil, nil
Expand Down
57 changes: 55 additions & 2 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,42 @@ func generateCertificate(cn string) (*tls.Config, error) {
return tlsConfig, nil
}

// startTCPEchoServer starts a server which echos back any content recieved
func startTCPEchoServer() (string, error) {
ln, err := net.Listen("tcp", ":0")
if err != nil {
return "", fmt.Errorf("listen: %w", err)
}

go func() {
for {
conn, err := ln.Accept()
if err != nil {
continue
}

go func(c net.Conn) {
buf := make([]byte, 1024)
for {
n, err := c.Read(buf)
if err != nil {
c.Close()
return
}
c.Write(buf[:n])
}
}(conn)
}
}()

return ln.Addr().String(), nil
}

// TestE2E tests the default options end to end.
// Reminder: frontend (browser) <-> server <-> client <-> backend (target service)
func TestE2E(t *testing.T) {
r := require.New(t)
logger := zaptest.NewLogger(t)
logger := zaptest.NewLogger(t, zaptest.Level(zap.InfoLevel))

server := server.New("localtest.me", logger)

Expand All @@ -105,15 +138,35 @@ func TestE2E(t *testing.T) {
}()
time.Sleep(time.Millisecond * 50)

// echo backend will just send back whatever it gets
backend, err := startTCPEchoServer()
r.NoError(err)

_, port, _ := net.SplitHostPort(server.Addr().String())
controlAddr := fmt.Sprintf("control.localtest.me:%s", port)
client := client.New(
controlAddr,
"localhost:1234",
backend,
client.WithControlTLSConfig(tlsConfig),
)
err = client.Start()
r.NoError(err, "client start")

r.Contains(client.IssuedAddr(), "localtest.me")

// connect a client and ensure it get the same data back
frontend, err := tls.Dial("tcp", client.IssuedAddr(), tlsConfig)
r.NoError(err)

sentData := make([]byte, 1000)
_, err = rand.Read(sentData)
r.NoError(err)
_, err = frontend.Write(sentData)
r.NoError(err)

receivedData := make([]byte, 2000)
recievedCount, err := frontend.Read(receivedData)
r.Equal(len(sentData), recievedCount)
receivedData = receivedData[:recievedCount]
r.Equal(sentData, receivedData)
}