|
16 | 16 | package quickfix |
17 | 17 |
|
18 | 18 | import ( |
| 19 | + "crypto/tls" |
19 | 20 | "net" |
20 | 21 | "testing" |
21 | 22 |
|
22 | 23 | "github.com/quickfixgo/quickfix/config" |
23 | 24 |
|
24 | 25 | proxyproto "github.com/pires/go-proxyproto" |
25 | 26 | "github.com/stretchr/testify/assert" |
| 27 | + "github.com/stretchr/testify/require" |
26 | 28 | ) |
27 | 29 |
|
28 | 30 | func TestAcceptor_Start(t *testing.T) { |
@@ -83,3 +85,44 @@ func TestAcceptor_Start(t *testing.T) { |
83 | 85 | }) |
84 | 86 | } |
85 | 87 | } |
| 88 | + |
| 89 | +func TestAcceptor_SetTLSConfig(t *testing.T) { |
| 90 | + sessionSettings := NewSessionSettings() |
| 91 | + sessionSettings.Set(config.BeginString, BeginStringFIX42) |
| 92 | + sessionSettings.Set(config.SenderCompID, "sender") |
| 93 | + sessionSettings.Set(config.TargetCompID, "target") |
| 94 | + |
| 95 | + genericSettings := NewSettings() |
| 96 | + |
| 97 | + genericSettings.GlobalSettings().Set("SocketAcceptPort", "5001") |
| 98 | + _, err := genericSettings.AddSession(sessionSettings) |
| 99 | + require.NoError(t, err) |
| 100 | + |
| 101 | + logger, err := NewScreenLogFactory().Create() |
| 102 | + require.NoError(t, err) |
| 103 | + acceptor := &Acceptor{settings: genericSettings, globalLog: logger} |
| 104 | + defer acceptor.Stop() |
| 105 | + // example of a customized tls.Config that loads the certificates dynamically by the `GetCertificate` function |
| 106 | + // as opposed to the Certificates slice, that is static in nature, and is only populated once and needs application restart to reload the certs. |
| 107 | + customizedTLSConfig := tls.Config{ |
| 108 | + Certificates: []tls.Certificate{}, |
| 109 | + GetCertificate: func(*tls.ClientHelloInfo) (*tls.Certificate, error) { |
| 110 | + cert, err := tls.LoadX509KeyPair("_test_data/localhost.crt", "_test_data/localhost.key") |
| 111 | + if err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + return &cert, nil |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + acceptor.SetTLSConfig(&customizedTLSConfig) |
| 119 | + assert.NoError(t, acceptor.Start()) |
| 120 | + assert.Len(t, acceptor.listeners, 1) |
| 121 | + |
| 122 | + conn, err := tls.Dial("tcp", "localhost:5001", &tls.Config{ |
| 123 | + InsecureSkipVerify: true, |
| 124 | + }) |
| 125 | + require.NoError(t, err) |
| 126 | + assert.NotNil(t, conn) |
| 127 | + defer conn.Close() |
| 128 | +} |
0 commit comments